COHERENT manpages
This page displays the COHERENT manpage for stat() [Find file attributes].
List of available manpages
Index
stat() -- System Call
Find file attributes
#include <sys/stat.h>
int stat(file, statptr)
char *file; struct stat *statptr;
stat() returns a structure that contains the attributes of a file,
including protection information, file type, and file size.
file points to the path name of file. statptr points to a structure of the
type stat, as defined in the header file stat.h. For information on stat,
see the Lexicon entry for stat.h.
Example
The following example uses stat() to print a file's status.
#include <sys/stat.h>
main()
{
struct stat sbuf;
int status;
if (status = stat("/usr/include", &sbuf)) {
printf("Can't find\n");
exit(EXIT_FAILURE);
}
printf("uid = %d gid = %d\n", sbuf.st_uid, sbuf.st_gid);
}
See Also
chmod(),
chown(),
libc,
ls,
open(),
stat.h
POSIX Standard, §5.6.2
Diagnostics
stat() returns -1 if an error occurs, e.g., the file cannot be found.
Otherwise, it returns zero.
Notes
stat() differs from the related function fstat() mainly in that fstat()
accesses the file through its descriptor, which was returned by a
successful call to open(), whereas stat() takes the file's path name and
opens it before checking its status.
The call
stat("", &s)
is identical to
stat(".", &s)
Both calls succeed. The POSIX Standard forbids the former call -- in fact,
the POSIX Standard forbids the NULL string as a path name under any
circumstances; therefore you should never use the former call.