I use the stat system call on Linux and retrieve the file information.
char *parent_dir; // for example: /run/atd.pid/ struct stat buf; stat(parent_dir, &buf);
buf structure type:
struct stat { dev_t st_dev; ino_t st_ino; mode_t st_mode; nlink_t st_nlink; uid_t st_uid; gid_t st_gid; dev_t st_rdev; off_t st_size; blksize_t st_blksize; blkcnt_t st_blocks; time_t st_atime; time_t st_mtime; time_t st_ctime; };
I get a number of hard links like: buf.st_nlink .
My problem is that I cannot compare the number of hard links with an integer value. I tried to initialize another nlink_t and then compare my variable with stat variable, but it does not work. I also tried this link .
An alternative way to cast nlink_t to int , but it does not work. always returns the same number.
int parse_to_int(nlink_t *source) { int buffer_size = sizeof(*source); char buffer[buffer_size]; snprintf(&buffer[0], buffer_size, "%lu", (unsigned long)source); int val = atoi(buffer); return val; }
Any idea?
Program output when I use the parse_to_int function:
get stat for: /run/nm-dhclient-wlan0.conf/ nlink_t: 321 get stat for: /run/wpa_supplicant/ nlink_t: 321 get stat for: /run/udisks2/ nlink_t: 321 get stat for: /run/nm-dns-dnsmasq.conf/ nlink_t: 321 ...
gkiko source share