

stat()               General Function (libc)               stat()




Find file attributes

#include <sys/stat.h>
iinntt ssttaatt(_f_i_l_e, _s_t_a_t_p_t_r)
cchhaarr *_f_i_l_e; ssttrruucctt ssttaatt *_s_t_a_t_p_t_r;

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 struc-
ture of the type stat, as defined in the header file stat.h.

The following summarizes the structure stat:


struct stat {
    dev_t st_dev;  /* Device */
    ino_t st_ino;  /* i-node number */
    unsigned short st_mode; /* Mode */
    short st_nlink;  /* Link count */
    short st_uid;  /* User id */
    short st_gid;  /* Group id */
    dev_t st_rdev;  /* Real device */
    fsize_t st_size;  /* Size */
    time_t st_atime;  /* Access time */
    time_t st_mtime;  /* Modify time */
    time_t st_ctime;  /* Change time */
};


The following  lists the legal  settings for the  element st_mode
which defines the file's attributes:


         SS_IIFFMMTT     0x0170000    File types
         SS_IIFFRREEGG    0x0100000    Ordinary file
         SS_IIFFDDIIRR    0x0040000    Directory
         SS_IIFFCCHHRR    0x0020000    Character-special file
         SS_IIFFBBLLKK    0x0060000    Block-special file
         SS_IISSUUIIDD    0x0004000    Set user identifier
         SS_IISSGGIIDD    0x0002000    Set group identifier
         SS_IISSVVTTXX    0x0001000    Save text bit
         SS_IIRREEAADD    0x0000400    Owner read permission
         SS_IIWWRRIITTEE   0x0000200    Owner write permission
         SS_IIEEXXEECC    0x0000100    Owner execute permission


st_dev and st_ino together form a unique description of the file.
The former is the device on which the file and its i-node reside,
and the  latter is the  index number of the  file.  st_mode gives
the permission bits,  as outlined above.  st_nlink gives the num-
ber of links to the file.   The user id and group id of the owner
are  st_uid and  st_gid, respectively.   st_rdev, which  is valid
only for special files, holds the major and minor numbers for the


COHERENT Lexicon                                           Page 1




stat()               General Function (libc)               stat()



file.

The entry  st_size gives the size  of the file, in  bytes.  For a
pipe, the size is the number of bytes waiting to be read from the
pipe.

Three entries for each  file give the last occurrences of various
events in  the file's history.  st_atime gives  the time the file
was last read or written to.  st_mtime gives the time of the last
modification, write for  files, create or delete entry for direc-
tories.  st_ctime  gives the last  change to the  attributes, not
including times and size.

***** 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(1);
    }



    printf("uid = %d gid = %d\n", sbuf.st_uid, sbuf.st_gid);
}


***** Files *****

<sys/stat.h>

***** See Also *****

chmod(), chown(), COHERENT system calls, ls, open()

***** 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.

***** Diagnostics *****

stat  returns -1  if an  error occurs, e.g.,  the file  cannot be


COHERENT Lexicon                                           Page 2




stat()               General Function (libc)               stat()



found.  Otherwise, it returns zero.
























































COHERENT Lexicon                                           Page 3


