FSTATAT(3)                 POSIX Programmer's Manual                FSTATAT(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       fstatat, lstat, stat -- get file status

SSYYNNOOPPSSIISS
       #include <fcntl.h>
       #include <sys/stat.h>

       int fstatat(int fd, const char *restrict _p_a_t_h,
           struct stat *restrict _b_u_f, int _f_l_a_g);
       int lstat(const char *restrict _p_a_t_h, struct stat *restrict _b_u_f);
       int stat(const char *restrict _p_a_t_h, struct stat *restrict _b_u_f);

DDEESSCCRRIIPPTTIIOONN
       The _s_t_a_t() function shall obtain information about the named file and
       write it to the area pointed to by the _b_u_f argument. The _p_a_t_h argument
       points to a pathname naming a file. Read, write, or execute permission
       of the named file is not required. An implementation that provides
       additional or alternate file access control mechanisms may, under
       implementation-defined conditions, cause _s_t_a_t() to fail. In particular,
       the system may deny the existence of the file specified by _p_a_t_h.

       If the named file is a symbolic link, the _s_t_a_t() function shall
       continue pathname resolution using the contents of the symbolic link,
       and shall return information pertaining to the resulting file if the
       file exists.

       The _b_u_f argument is a pointer to a ssttaatt structure, as defined in the
       _<_s_y_s_/_s_t_a_t_._h_> header, into which information is placed concerning the
       file.

       The _s_t_a_t() function shall update any time-related fields (as described
       in the Base Definitions volume of POSIX.1-2017, _S_e_c_t_i_o_n _4_._9, _F_i_l_e _T_i_m_e_s
       _U_p_d_a_t_e), before writing into the ssttaatt structure.

       If the named file is a shared memory object, the implementation shall
       update in the ssttaatt structure pointed to by the _b_u_f argument the _s_t___u_i_d,
       _s_t___g_i_d, _s_t___s_i_z_e, and _s_t___m_o_d_e fields, and only the S_IRUSR, S_IWUSR,
       S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH file permission bits need be
       valid. The implementation may update other fields and flags.

       If the named file is a typed memory object, the implementation shall
       update in the ssttaatt structure pointed to by the _b_u_f argument the _s_t___u_i_d,
       _s_t___g_i_d, _s_t___s_i_z_e, and _s_t___m_o_d_e fields, and only the S_IRUSR, S_IWUSR,
       S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH file permission bits need be
       valid. The implementation may update other fields and flags.

       For all other file types defined in this volume of POSIX.1-2017, the
       structure members _s_t___m_o_d_e, _s_t___i_n_o, _s_t___d_e_v, _s_t___u_i_d, _s_t___g_i_d, _s_t___a_t_i_m,
       _s_t___c_t_i_m, and _s_t___m_t_i_m shall have meaningful values and the value of the
       member _s_t___n_l_i_n_k shall be set to the number of links to the file.

       The _l_s_t_a_t() function shall be equivalent to _s_t_a_t(), except when _p_a_t_h
       refers to a symbolic link. In that case _l_s_t_a_t() shall return
       information about the link, while _s_t_a_t() shall return information about
       the file the link references.

       For symbolic links, the _s_t___m_o_d_e member shall contain meaningful
       information when used with the file type macros. The file mode bits in
       _s_t___m_o_d_e are unspecified. The structure members _s_t___i_n_o, _s_t___d_e_v, _s_t___u_i_d,
       _s_t___g_i_d, _s_t___a_t_i_m, _s_t___c_t_i_m, and _s_t___m_t_i_m shall have meaningful values and
       the value of the _s_t___n_l_i_n_k member shall be set to the number of (hard)
       links to the symbolic link.  The value of the _s_t___s_i_z_e member shall be
       set to the length of the pathname contained in the symbolic link not
       including any terminating null byte.

       The _f_s_t_a_t_a_t() function shall be equivalent to the _s_t_a_t() or _l_s_t_a_t()
       function, depending on the value of _f_l_a_g (see below), except in the
       case where _p_a_t_h specifies a relative path. In this case the status
       shall be retrieved from a file relative to the directory associated
       with the file descriptor _f_d instead of the current working directory.
       If the access mode of the open file description associated with the
       file descriptor is not O_SEARCH, the function shall check whether
       directory searches are permitted using the current permissions of the
       directory underlying the file descriptor. If the access mode is
       O_SEARCH, the function shall not perform the check.

       Values for _f_l_a_g are constructed by a bitwise-inclusive OR of flags from
       the following list, defined in _<_f_c_n_t_l_._h_>:

       AT_SYMLINK_NOFOLLOW
             If _p_a_t_h names a symbolic link, the status of the symbolic link is
             returned.

       If _f_s_t_a_t_a_t() is passed the special value AT_FDCWD in the _f_d parameter,
       the current working directory shall be used and the behavior shall be
       identical to a call to _s_t_a_t() or _l_s_t_a_t() respectively, depending on
       whether or not the AT_SYMLINK_NOFOLLOW bit is set in _f_l_a_g.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, these functions shall return 0.  Otherwise,
       these functions shall return -1 and set _e_r_r_n_o to indicate the error.

EERRRROORRSS
       These functions shall fail if:

       EEAACCCCEESS Search permission is denied for a component of the path prefix.

       EEIIOO    An error occurred while reading from the file system.

       EELLOOOOPP  A loop exists in symbolic links encountered during resolution of
              the _p_a_t_h argument.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a component of a pathname is longer than
              {NAME_MAX}.

       EENNOOEENNTT A component of _p_a_t_h does not name an existing file or _p_a_t_h is an
              empty string.

       EENNOOTTDDIIRR
              A component of the path prefix names an existing file that is
              neither a directory nor a symbolic link to a directory, or the
              _p_a_t_h argument contains at least one non-<slash> character and
              ends with one or more trailing <slash> characters and the last
              pathname component names an existing file that is neither a
              directory nor a symbolic link to a directory.

       EEOOVVEERRFFLLOOWW
              The file size in bytes or the number of blocks allocated to the
              file or the file serial number cannot be represented correctly
              in the structure pointed to by _b_u_f.

       The _f_s_t_a_t_a_t() function shall fail if:

       EEAACCCCEESS The access mode of the open file description associated with _f_d
              is not O_SEARCH and the permissions of the directory underlying
              _f_d do not permit directory searches.

       EEBBAADDFF  The _p_a_t_h argument does not specify an absolute path and the _f_d
              argument is neither AT_FDCWD nor a valid file descriptor open
              for reading or searching.

       EENNOOTTDDIIRR
              The _p_a_t_h argument is not an absolute path and _f_d is a file
              descriptor associated with a non-directory file.

       These functions may fail if:

       EELLOOOOPP  More than {SYMLOOP_MAX} symbolic links were encountered during
              resolution of the _p_a_t_h argument.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a pathname exceeds {PATH_MAX}, or pathname
              resolution of a symbolic link produced an intermediate result
              with a length that exceeds {PATH_MAX}.

       EEOOVVEERRFFLLOOWW
              A value to be stored would overflow one of the members of the
              ssttaatt structure.

       The _f_s_t_a_t_a_t() function may fail if:

       EEIINNVVAALL The value of the _f_l_a_g argument is not valid.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   OObbttaaiinniinngg FFiillee SSttaattuuss IInnffoorrmmaattiioonn
       The following example shows how to obtain file status information for a
       file named //hhoommee//ccnndd//mmoodd11.  The structure variable _b_u_f_f_e_r is defined
       for the ssttaatt structure.


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

           struct stat buffer;
           int         status;
           ...
           status = stat("/home/cnd/mod1", &buffer);

   GGeettttiinngg DDiirreeccttoorryy IInnffoorrmmaattiioonn
       The following example fragment gets status information for each entry
       in a directory. The call to the _s_t_a_t() function stores file information
       in the ssttaatt structure pointed to by _s_t_a_t_b_u_f.  The lines that follow the
       _s_t_a_t() call format the fields in the ssttaatt structure for presentation to
       the user of the program.


           #include <sys/types.h>
           #include <sys/stat.h>
           #include <dirent.h>
           #include <pwd.h>
           #include <grp.h>
           #include <time.h>
           #include <locale.h>
           #include <langinfo.h>
           #include <stdio.h>
           #include <stdint.h>

           struct dirent  *dp;
           struct stat     statbuf;
           struct passwd  *pwd;
           struct group   *grp;
           struct tm      *tm;
           char            datestring[256];
           ...
           /* Loop through directory entries. */
           while ((dp = readdir(dir)) != NULL) {

               /* Get entry's information. */
               if (stat(dp->d_name, &statbuf) == -1)
                   continue;

               /* Print out type, permissions, and number of links. */
               printf("%10.10s", sperm (statbuf.st_mode));
               printf("%4d", statbuf.st_nlink);

               /* Print out owner's name if it is found using getpwuid(). */
               if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
                   printf(" %-8.8s", pwd->pw_name);
               else
                   printf(" %-8d", statbuf.st_uid);

               /* Print out group name if it is found using getgrgid(). */
               if ((grp = getgrgid(statbuf.st_gid)) != NULL)
                   printf(" %-8.8s", grp->gr_name);
               else
                   printf(" %-8d", statbuf.st_gid);

               /* Print size of file. */
               printf(" %9jd", (intmax_t)statbuf.st_size);

               tm = localtime(&statbuf.st_mtime);

               /* Get localized date string. */
               strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);

               printf(" %s %s\n", datestring, dp->d_name);
           }

   OObbttaaiinniinngg SSyymmbboolliicc LLiinnkk SSttaattuuss IInnffoorrmmaattiioonn
       The following example shows how to obtain status information for a
       symbolic link named //mmoodduulleess//ppaassss11.  The structure variable _b_u_f_f_e_r is
       defined for the ssttaatt structure. If the _p_a_t_h argument specified the
       pathname for the file pointed to by the symbolic link (//hhoommee//ccnndd//mmoodd11),
       the results of calling the function would be the same as those returned
       by a call to the _s_t_a_t() function.


           #include <sys/stat.h>

           struct stat buffer;
           int status;
           ...
           status = lstat("/modules/pass1", &buffer);

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       None.

RRAATTIIOONNAALLEE
       The intent of the paragraph describing ``additional or alternate file
       access control mechanisms'' is to allow a secure implementation where a
       process with a label that does not dominate the file's label cannot
       perform a _s_t_a_t() function. This is not related to read permission; a
       process with a label that dominates the file's label does not need read
       permission.  An implementation that supports write-up operations could
       fail _f_s_t_a_t() function calls even though it has a valid file descriptor
       open for writing.

       The purpose of the _f_s_t_a_t_a_t() function is to obtain the status of files
       in directories other than the current working directory without
       exposure to race conditions. Any part of the path of a file could be
       changed in parallel to a call to _s_t_a_t(), resulting in unspecified
       behavior. By opening a file descriptor for the target directory and
       using the _f_s_t_a_t_a_t() function it can be guaranteed that the file for
       which status is returned is located relative to the desired directory.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _a_c_c_e_s_s(), _c_h_m_o_d(), _f_d_o_p_e_n_d_i_r(), _f_s_t_a_t(), _m_k_n_o_d(), _r_e_a_d_l_i_n_k(), _s_y_m_l_i_n_k()

       The Base Definitions volume of POSIX.1-2017, _S_e_c_t_i_o_n _4_._9, _F_i_l_e _T_i_m_e_s
       _U_p_d_a_t_e, <<ffccnnttll..hh>>, <<ssyyss__ssttaatt..hh>>, <<ssyyss__ttyyppeess..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2017                           FSTATAT(3)
