stat()--Get File Information


  Syntax
 #include <sys/stat.h>

 int stat(const char *path, struct stat *buf);    
  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes.

The stat() function gets status information about a specified file and places it in the area of memory pointed to by the buf argument.

If the named file is a symbolic link, stat() resolves the symbolic link. It also returns information about the resulting file.


Parameters

path
(Input) A pointer to the null-terminated path name of the file from which information is required.

This parameter is assumed to be represented in the CCSID (coded character set identifier) currently in effect for the job. If the CCSID of the job is 65535, this parameter is assumed to be represented in the default CCSID of the job.

See QlgStat()--Get File Information (using NLS-enabled path name) for a description and an example of supplying the path in any CCSID.


buf
(Output) A pointer to the area to which the information should be written.

The information is returned in the following stat structure, as defined in the <sys/stat.h> header file:

Values of time_t are given in terms of seconds since a fixed point in time called the Epoch.

You can examine properties of a mode_t value from the st_mode field using a collection of macros defined in the <sys/stat.h> header file. If mode is a mode_t value, then:

S_ISBLK(mode)
Is nonzero for block special files
S_ISCHR(mode)
Is nonzero for character special files
S_ISDIR(mode)
Is nonzero for directories
S_ISFIFO(mode)
Is nonzero for pipes and FIFO special files
S_ISREG(mode)
Is nonzero for regular files
S_ISLNK(mode)
Is nonzero for symbolic links
S_ISSOCK(mode)
Is nonzero for local sockets
S_ISNATIVE(mode)
Is nonzero for operating system native objects

Authorities

Note: Adopted authority is not used.

Authorization Required for stat()

Return Value

0
stat() was successful. The information is returned in buf.
-1
stat() was not successful. The errno global variable is set to indicate the error.

Error Conditions

If stat() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

If interaction with a file server is required to access the object, errno could indicate one of the following errors:



Error Messages

The following messages may be sent from this function:



Usage Notes

  1. This function will fail with error code [ENOTSAFE] when both of the following conditions occur:

    • Where multiple threads exist in the job.
    • The object this function is operating on resides in a file system that is not threadsafe. Only the following file systems are threadsafe for this function:
      • "Root" (/)
      • QOpenSys
      • User-defined
      • QNTC
      • QSYS.LIB
      • Independent ASP QSYS.LIB
      • QOPT
      • Network File System
      • QFileSvr.400

  2. "Root" (/), QOpenSys, and User-Defined File System Differences

    The st_allocsize value can be influenced by the setting of the disk storage option attribute. See Qp0lSetAttr()--Set Attributes for more information.

  3. QSYS.LIB and Independent ASP QSYS.LIB File System Differences

    The stat() function could return zero for the st_atime value (in the stat structure) under some conditions.

    The S_ISDIR(mode) macro will be nonzero for *LIB objects. It will also be nonzero for *FILE objects, unless the object is a save file.

    The S_ISREG(mode) macro will be nonzero for *MBR and *USRSPC objects. It will also be nonzero for *FILE objects when the object is a save file.

    The S_ISNATIVE(mode) macro will be nonzero for all other object types.

  4. QDLS File System Differences

    If the date corresponding to the st_atime, st_mtime, or st_ctime value precedes 1970, stat() returns zero for that value. Also, if the specified path is /QDLS, stat() returns zero for all three values st_atime, st_mtime, and st_ctime.

    The S_ISDIR(mode) macro will be nonzero for *FLR objects.

    The S_ISREG(mode) macro will be nonzero for *DOC objects.

    The S_ISNATIVE(mode) macro will always be zero.

  5. QOPT File System Differences

    The value for st_atime will always be zero. The value for st_ctime will always be the creation date and time of the file or directory.

    The user, group, and other mode bits are always on for an object that exists on a volume not formatted in Universal Disk Format (UDF).

    If the object exists on a volume formatted in Universal Disk Format (UDF), the authorization that is checked for the object and preceding directories in the path name follows the rules described in Authorization Required for stat(), "." If the object exists on a volume formatted in some other media format, no authorization checks are made on the object or on each directory in the path name. The volume authorization list is checked for *USE authority regardless of the media format of the volume.

    stat() on /QOPT will always return 2,147,483,647 for size fields.

    stat() on optical volumes will return the volume capacity or 2,147,483,647, whichever is smaller.

    The file access time is not changed.

  6. Network File System Differences

    Local access to remote files through the Network File System may produce unexpected results due to conditions at the server. Once a file is open, subsequent requests to perform operations on the file can fail because file attributes are checked at the server on each request. If permissions on the file are made more restrictive at the server or the file is unlinked or made unavailable by the server for another client, your operation on an open file descriptor will fail when the local Network File System receives these updates. The local Network File System also impacts operations that retrieve file attributes. Recent changes at the server may not be available at your client yet, and old values may be returned from operations. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.)

  7. QFileSvr.400 File System Differences

    The value of st_vfs will always be 0 for remote objects accessed via QFileSvr.400.

    The st_uid and st_gid fields that are returned for the object represent uids and gids on the remote system. These uids and gids may not exist on the local system or, if they do exist, may not represent the expected users or groups.

  8. This function will fail with the [EOVERFLOW] error if the file size in bytes cannot be represented correctly in the structure pointed to by buf (the file is larger than 2GB minus 1 byte).

  9. When you develop in C-based languages and this function is compiled with _LARGE_FILES defined, it will be mapped to stat64(). Note that the type of the buf parameter, struct stat *, also will be mapped to type struct stat64 *.

  10. When you develop in C-based languages and this function is compiled with _64_BIT_TIME defined, it will be mapped to stat64_time64(). This mapping will occur whether or not _LARGE_FILES is also defined. Note that the type of the buffer parameter, struct stat *, also will be mapped to type struct stat64_time64 *. See stat64_time64() for more information about this structure.


Related Information


Example

The following example gets status information about a file.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>

main() {
  struct stat info;

  if (stat("/", &info) != 0)
    perror("stat() error");
  else {
    puts("stat() returned the following information about root f/s:");
    printf("  inode:   %d\n",   (int) info.st_ino);
    printf(" dev id:   %d\n",   (int) info.st_dev);
    printf("   mode:   %08x\n",       info.st_mode);
    printf("  links:   %d\n",         info.st_nlink);
    printf("    uid:   %d\n",   (int) info.st_uid);
    printf("    gid:   %d\n",   (int) info.st_gid);
  }
}

Output: note that the following information will vary from system to system.

stat() returned the following information about root f/s:
  inode:   0
 dev id:   1
   mode:   010001ed
  links:   3
    uid:   137
    gid:   500


API introduced: V3R1

[ Back to top | UNIX-Type APIs | APIs by category ]