stat (C System Call)

stat is a system call that is used to determine information about a file based on its file path.

Required Include Files

#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

Function Definition

int stat(const char *path, struct stat *buf);
Field Description
const char *path The file descriptor of the file that is being inquired.
struct stat *buf A structure where data about the file will be stored. A detailed look at all of the fields in this structure can be found in the struct stat page.
return value Returns a negative value on failure.

Code Snippet

An example of code that uses the stat() system call is below.

#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char **argv)
    if(argc != 2)    
        return 1;
    struct stat fileStat;
    if(stat(argv[1],&fileStat) < 0)    
        return 1;
    printf("Information for %s\n",argv[1]);
    printf("File Size: \t\t%d bytes\n",fileStat.st_size);
    printf("Number of Links: \t%d\n",fileStat.st_nlink);
    printf("File inode: \t\t%d\n",fileStat.st_ino);
    printf("File Permissions: \t");
    printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
    printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
    printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
    printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
    printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
    printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
    printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
    printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
    printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
    printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
    printf("The file %s a symbolic link\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");
    return 0;

The output of this program is shown in the following set of commands:

Information on the current files in the directory:

$ ls -l
total 16
-rwxr-xr-x 1 stargazer stargazer   36 2008-05-06 20:50
-rwxr-xr-x 1 stargazer stargazer 7780 2008-05-07 12:36 testProgram
-rw-r--r-- 1 stargazer stargazer 1229 2008-05-07 12:04 testProgram.c

Running the program with the file

$ ./testProgram

Information for
File Size:              36 bytes
Number of Links:        1
File inode:             180055
File Permissions:       -rwxr-xr-x

The file is not a symbolic link

Running the program with the files testProgram.c

$ ./testProgram testProgram.c

Information for testProgram.c
File Size:              1229 bytes
Number of Links:        1
File inode:             295487
File Permissions:       -rw-r--r--

The file is not a symbolic link

Running the program with the directory /home/stargazer

$ ./testProgram /home/stargazer

Information for /home/stargazer
File Size:              4096 bytes
Number of Links:        61
File inode:             32706
File Permissions:       drwxr-xr-x

The file is not a symbolic link
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License