Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/stat.html
2024-02-19 00:21:47 -05:00

311 lines
10 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>stat</title>
</head>
<body bgcolor="white">
<basefont size="3"> <a name="stat"></a> <a name="tag_03_716"></a><!-- stat -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_716_01"></a>NAME</h4>
<blockquote>stat - get file status</blockquote>
<h4><a name="tag_03_716_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><code><tt>#include &lt;<a href="../basedefs/sys/stat.h.html">sys/stat.h</a>&gt;<br>
<br>
int stat(const char *restrict</tt> <i>path</i><tt>, struct stat *restrict</tt> <i>buf</i><tt>);<br>
</tt></code></p>
</blockquote>
<h4><a name="tag_03_716_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>stat</i>() function shall obtain information about the named file and write it to the area pointed to by the <i>buf</i>
argument. The <i>path</i> 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 <i>stat</i>() to fail. In particular, the system may deny the existence of the file specified by <i>path</i>.</p>
<p>If the named file is a symbolic link, the <i>stat</i>() 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.</p>
<p>The <i>buf</i> argument is a pointer to a <b>stat</b> structure, as defined in the <a href=
"../basedefs/sys/stat.h.html"><i>&lt;sys/stat.h&gt;</i></a> header, into which information is placed concerning the file.</p>
<p>The <i>stat</i>() function shall update any time-related fields (as described in the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap04.html#tag_04_07">Section 4.7, File Times Update</a>), before writing
into the <b>stat</b> structure.</p>
<p>Unless otherwise specified, the structure members <i>st_mode</i>, <i>st_ino</i>, <i>st_dev</i>, <i>st_uid</i>, <i>st_gid</i>,
<i>st_atime</i>, <i>st_ctime</i>, and <i>st_mtime</i> shall have meaningful values for all file types defined in this volume of
IEEE&nbsp;Std&nbsp;1003.1-2001. The value of the member <i>st_nlink</i> shall be set to the number of links to the file.</p>
</blockquote>
<h4><a name="tag_03_716_04"></a>RETURN VALUE</h4>
<blockquote>
<p>Upon successful completion, 0 shall be returned. Otherwise, -1 shall be returned and <i>errno</i> set to indicate the error.</p>
</blockquote>
<h4><a name="tag_03_716_05"></a>ERRORS</h4>
<blockquote>
<p>The <i>stat</i>() function shall fail if:</p>
<dl compact>
<dt>[EACCES]</dt>
<dd>Search permission is denied for a component of the path prefix.</dd>
<dt>[EIO]</dt>
<dd>An error occurred while reading from the file system.</dd>
<dt>[ELOOP]</dt>
<dd>A loop exists in symbolic links encountered during resolution of the <i>path</i> argument.</dd>
<dt>[ENAMETOOLONG]</dt>
<dd>
The length of the <i>path</i> argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.</dd>
<dt>[ENOENT]</dt>
<dd>A component of <i>path</i> does not name an existing file or <i>path</i> is an empty string.</dd>
<dt>[ENOTDIR]</dt>
<dd>A component of the path prefix is not a directory.</dd>
<dt>[EOVERFLOW]</dt>
<dd>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 <i>buf</i>.</dd>
</dl>
<p>The <i>stat</i>() function may fail if:</p>
<dl compact>
<dt>[ELOOP]</dt>
<dd>More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the <i>path</i> argument.</dd>
<dt>[ENAMETOOLONG]</dt>
<dd>
As a result of encountering a symbolic link in resolution of the <i>path</i> argument, the length of the substituted pathname
string exceeded {PATH_MAX}.</dd>
<dt>[EOVERFLOW]</dt>
<dd>A value to be stored would overflow one of the members of the <b>stat</b> structure.</dd>
</dl>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_716_06"></a>EXAMPLES</h4>
<blockquote>
<h5><a name="tag_03_716_06_01"></a>Obtaining File Status Information</h5>
<p>The following example shows how to obtain file status information for a file named <b>/home/cnd/mod1</b>. The structure variable
<i>buffer</i> is defined for the <b>stat</b> structure.</p>
<pre>
<tt>#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;fcntl.h&gt;
<br>
struct stat buffer;
int status;
...
status = stat("/home/cnd/mod1", &amp;buffer);
</tt>
</pre>
<h5><a name="tag_03_716_06_02"></a>Getting Directory Information</h5>
<p>The following example fragment gets status information for each entry in a directory. The call to the <i>stat</i>() function
stores file information in the <b>stat</b> structure pointed to by <i>statbuf</i>. The lines that follow the <i>stat</i>() call
format the fields in the <b>stat</b> structure for presentation to the user of the program.</p>
<pre>
<tt>#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;dirent.h&gt;
#include &lt;pwd.h&gt;
#include &lt;grp.h&gt;
#include &lt;time.h&gt;
#include &lt;locale.h&gt;
#include &lt;langinfo.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdint.h&gt;
<br>
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) {
<br>
/* Get entry's information. */
if (stat(dp-&gt;d_name, &amp;statbuf) == -1)
continue;
<br>
/* Print out type, permissions, and number of links. */
printf("%10.10s", sperm (statbuf.st_mode));
printf("%4d", statbuf.st_nlink);
<br>
/* Print out owner's name if it is found using getpwuid(). */
if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
printf(" %-8.8s", pwd-&gt;pw_name);
else
printf(" %-8d", statbuf.st_uid);
<br>
/* Print out group name if it is found using getgrgid(). */
if ((grp = getgrgid(statbuf.st_gid)) != NULL)
printf(" %-8.8s", grp-&gt;gr_name);
else
printf(" %-8d", statbuf.st_gid);
<br>
/* Print size of file. */
printf(" %9jd", (intmax_t)statbuf.st_size);
<br>
tm = localtime(&amp;statbuf.st_mtime);
<br>
/* Get localized date string. */
strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);
<br>
printf(" %s %s\n", datestring, dp-&gt;d_name);
}
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_716_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_716_08"></a>RATIONALE</h4>
<blockquote>
<p>The intent of the paragraph describing &quot;additional or alternate file access control mechanisms&quot; is to allow a secure
implementation where a process with a label that does not dominate the file's label cannot perform a <i>stat</i>() 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 <a href="../functions/fstat.html"><i>fstat</i>()</a> function calls
even though it has a valid file descriptor open for writing.</p>
</blockquote>
<h4><a name="tag_03_716_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_716_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="fstat.html"><i>fstat</i>()</a> , <a href="lstat.html"><i>lstat</i>()</a> , <a href=
"readlink.html"><i>readlink</i>()</a> , <a href="symlink.html"><i>symlink</i>()</a> , the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/sys/stat.h.html"><i>&lt;sys/stat.h&gt;</i></a>, <a href=
"../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_716_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
</blockquote>
<h4><a name="tag_03_716_12"></a>Issue 5</h4>
<blockquote>
<p>Large File Summit extensions are added.</p>
</blockquote>
<h4><a name="tag_03_716_13"></a>Issue 6</h4>
<blockquote>
<p>In the SYNOPSIS, the optional include of the <a href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> header is
removed.</p>
<p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p>
<ul>
<li>
<p>The requirement to include <a href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> has been removed. Although <a
href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> was required for conforming implementations of previous POSIX
specifications, it was not required for UNIX applications.</p>
</li>
<li>
<p>The [EIO] mandatory error condition is added.</p>
</li>
<li>
<p>The [ELOOP] mandatory error condition is added.</p>
</li>
<li>
<p>The [EOVERFLOW] mandatory error condition is added. This change is to support large files.</p>
</li>
<li>
<p>The [ENAMETOOLONG] and the second [EOVERFLOW] optional error conditions are added.</p>
</li>
</ul>
<p>The following changes were made to align with the IEEE&nbsp;P1003.1a draft standard:</p>
<ul>
<li>
<p>Details are added regarding the treatment of symbolic links.</p>
</li>
<li>
<p>The [ELOOP] optional error condition is added.</p>
</li>
</ul>
<p>The DESCRIPTION is updated to avoid use of the term &quot;must&quot; for application requirements.</p>
<p>The <b>restrict</b> keyword is added to the <i>stat</i>() prototype for alignment with the ISO/IEC&nbsp;9899:1999 standard.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>