#include <sys/stat.h> int stat(const char *file, struct stat *sbuf);
This function obtains the status of the file file and stores it in sbuf, which has this structure:
struct stat { time_t st_atime; /* time of last modification */ time_t st_ctime; /* '' */ dev_t st_dev; /* The drive number (0 = a:) */ gid_t st_gid; /* getgid() */ ino_t st_ino; /* starting cluster or a unique identifier */ mode_t st_mode; /* file mode - S_IF* and S_IRUSR/S_IWUSR */ time_t st_mtime; /* '' */ nlink_t st_nlink; /* 2 + number of subdirs, or 1 for files */ off_t st_size; /* size of file in bytes */ off_t st_blksize; /* the size of transfer buffer */ uid_t st_uid; /* getuid() */ };
Zero on success, nonzero on failure (and errno set).
not ANSI, POSIX
struct stat s; stat("data.txt", &s); if (S_ISDIR(s.st_mode)) printf("is directory\n");
Supplying a 100% Unix-compatible f?stat()
functions under DOS is an
implementation nightmare. The following notes describe some of the
obscure points specific to their behavior in DJGPP.
1. The `drive' for character devices (like con
, /dev/null
and others is returned as -1. For drives networked by Novell Netware, it
is returned as -2.
2. The starting cluster number of a file serves as its inode number. For
files whose starting cluster number is inaccessible (empty files, files on
networked drives, etc.) the st_inode
field will be invented
in a way which guarantees that no two different files will get the same
inode number (thus it is unique). This invented inode will also be
different from any real cluster number of any local file. However, only
for local, non-empty files/directories the inode is guaranteed to be
consistent between stat()
and fstat()
function calls.
3. The WRITE access mode bit is set only for the user (unless the file is read-only, hidden or system). EXECUTE bit is set for directories, files which can be executed from the DOS prompt (batch files, .com, .dll and .exe executables) or run by go32 extender.
4. Size of directories is reported as the number of its files (sans `.' and `..' entries) multiplied by 32 bytes (the size of directory entry). On FAT filesystems that support the LFN API (such as Windows 9x), the reported size of the directory accounts for additional space used to store the long filenames.
5. Time stamp for root directories is taken from the volume label entry, if that's available; otherwise, it is reported as 1-Jan-1980.
6. The variable section _djstat_flags controls what hard-to-get fields of
struct stat
are needed by the application.
7. stat()
should not be used to get an up-to-date info about a file
which is open and has been written to, because stat()
will only
return correct data after the file is closed. Use section fstat while the
file is open.
8. The number of links st_nlink
is always 1 for files other than
directories. For directories, it is the number of subdirectories plus
2. This is so that programs written for Unix that depend on this to
optimize recursive traversal of the directory tree, will still work.
Go to the first, previous, next, last section, table of contents.