Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C program: how to get parent directory's inode number?

Tags:

linux

inode

How to get directory inode number say /home/laks/file.txt I need the inode number of laks directory. Any built-in function is already available? I think i could use stat() if i cut the file name...but any other solution to this without removing file name.

like image 759
webminal.org Avatar asked Nov 05 '22 17:11

webminal.org


1 Answers

#include <libgen.h>
#include <sys/stat.h>
...
struct stat statbuf;
if (stat(dirname(argv[1]), &statbuf) != -1)
    process_inode_number(statbuf.st_ino);

Note that dirname() may modify the string, so if you still need it, or if it may be a string literal (which is in read-only memory), then use strdup() to make a copy of the string for dirname().

like image 101
mark4o Avatar answered Nov 12 '22 10:11

mark4o