Calling a File's Information

An inode is a data structure that contains information about a file.

Linux provides three syscalls for obtaining this information. The

retrieved information is stored in a struct called stat. The syscalls

and the stat struct are declared in the

header. The first of these three syscalls is stat(), which has the following prototype: int stat(const char * pathname, struct stat *info); It takes a pathname as the first argument and a pointer to an empty stat struct, which is, in turn, filled with the file's information. A zero return value indicates success, whereas a nonzero value indicates an error. If the pathname is a symbolic link (symlink), then stat() follows it. The second syscall, lstat(), has the following prototype: int lstat(const char * pathname, struct stat *info); lstat() is similar to stat() except that it doesn't follow symlinks. Thus, you can use it to determine whether a file name is a symlink. Finally, the fstat() function takes a file descriptor, rather than a name, as its first argument. fd must be an open file's descriptor. Fstat () has the following prototype: int fstat(int fd, struct stat *info); The struct stat contains the following fields: st_dev -- A device number on which the file resides. st_ino -- A file's on-disk inode number. st_mode -- A file's mode. st_nlink -- The number of pathnames that reference this inode. st_uid
From CIO: 8 Free Online Courses to Grow Your Tech Skills
Join the discussion
Be the first to comment on this article. Our Commenting Policies