Calling a File's Information

By Danny Kalev, ITworld |  How-to

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

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Ask a Question
randomness