📄 library_13.html
字号:
(st.st_blocks * 512 < st.st_size)
</PRE>
<P>
This test is not perfect because a file that is just slightly sparse
might not be detected as sparse at all. For practical applications,
this is not a problem.
<P>
<DT><CODE>unsigned int st_blksize</CODE>
<DD>The optimal block size for reading of writing this file. You might use
this size for allocating the buffer space for reading of writing the
file.
</DL>
<P>
Some of the file attributes have special data type names which exist
specifically for those attributes. (They are all aliases for well-known
integer types that you know and love.) These typedef names are defined
in the header file <TT>`sys/types.h'</TT> as well as in <TT>`sys/stat.h'</TT>.
Here is a list of them.
<P>
<A NAME="IDX785"></A>
<U>Data Type:</U> <B>mode_t</B><P>
This is an integer data type used to represent file modes. In the
GNU system, this is equivalent to <CODE>unsigned int</CODE>.
<P>
<A NAME="IDX786"></A>
<P>
<A NAME="IDX787"></A>
<U>Data Type:</U> <B>ino_t</B><P>
This is an arithmetic data type used to represent file serial numbers.
(In Unix jargon, these are sometimes called <DFN>inode numbers</DFN>.)
In the GNU system, this type is equivalent to <CODE>unsigned long int</CODE>.
<P>
<A NAME="IDX788"></A>
<U>Data Type:</U> <B>dev_t</B><P>
This is an arithmetic data type used to represent file device numbers.
In the GNU system, this is equivalent to <CODE>int</CODE>.
<P>
<A NAME="IDX789"></A>
<U>Data Type:</U> <B>nlink_t</B><P>
This is an arithmetic data type used to represent file link counts.
In the GNU system, this is equivalent to <CODE>unsigned short int</CODE>.
<P>
<H3><A NAME="SEC202" HREF="library_toc.html#SEC202" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC202">Reading the Attributes of a File</A></H3>
<P>
To examine the attributes of files, use the functions <CODE>stat</CODE>,
<CODE>fstat</CODE> and <CODE>lstat</CODE>. They return the attribute information in
a <CODE>struct stat</CODE> object. All three functions are declared in the
header file <TT>`sys/stat.h'</TT>.
<P>
<A NAME="IDX790"></A>
<U>Function:</U> int <B>stat</B> <I>(const char *<VAR>filename</VAR>, struct stat *<VAR>buf</VAR>)</I><P>
The <CODE>stat</CODE> function returns information about the attributes of the
file named by <VAR>filename</VAR> in the structure pointed at by <VAR>buf</VAR>.
<P>
If <VAR>filename</VAR> is the name of a symbolic link, the attributes you get
describe the file that the link points to. If the link points to a
nonexistent file name, then <CODE>stat</CODE> fails, reporting a nonexistent
file.
<P>
The return value is <CODE>0</CODE> if the operation is successful, and <CODE>-1</CODE>
on failure. In addition to the usual file name syntax errors
(see section <A HREF="library_10.html#SEC115" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_10.html#SEC115">File Name Errors</A>, the following <CODE>errno</CODE> error conditions
are defined for this function:
<P>
<DL COMPACT>
<DT><CODE>ENOENT</CODE>
<DD>The file named by <VAR>filename</VAR> doesn't exist.
</DL>
<P>
<A NAME="IDX791"></A>
<U>Function:</U> int <B>fstat</B> <I>(int <VAR>filedes</VAR>, struct stat *<VAR>buf</VAR>)</I><P>
The <CODE>fstat</CODE> function is like <CODE>stat</CODE>, except that it takes an
open file descriptor as an argument instead of a file name.
See section <A HREF="library_12.html#SEC171" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_12.html#SEC171">Low-Level Input/Output</A>.
<P>
Like <CODE>stat</CODE>, <CODE>fstat</CODE> returns <CODE>0</CODE> on success and <CODE>-1</CODE>
on failure. The following <CODE>errno</CODE> error conditions are defined for
<CODE>fstat</CODE>:
<P>
<DL COMPACT>
<DT><CODE>EBADF</CODE>
<DD>The <VAR>filedes</VAR> argument is not a valid file descriptor.
</DL>
<P>
<A NAME="IDX792"></A>
<U>Function:</U> int <B>lstat</B> <I>(const char *<VAR>filename</VAR>, struct stat *<VAR>buf</VAR>)</I><P>
The <CODE>lstat</CODE> function is like <CODE>stat</CODE>, except that it does not
follow symbolic links. If <VAR>filename</VAR> is the name of a symbolic
link, <CODE>lstat</CODE> returns information about the link itself; otherwise,
<CODE>lstat</CODE> works like <CODE>stat</CODE>. See section <A HREF="library_13.html#SEC196" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_13.html#SEC196">Symbolic Links</A>.
<P>
<H3><A NAME="SEC203" HREF="library_toc.html#SEC203" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC203">Testing the Type of a File</A></H3>
<P>
The <DFN>file mode</DFN>, stored in the <CODE>st_mode</CODE> field of the file
attributes, contains two kinds of information: the file type code, and
the access permission bits. This section discusses only the type code,
which you can use to tell whether the file is a directory, whether it is
a socket, and so on. For information about the access permission,
section <A HREF="library_13.html#SEC205" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_13.html#SEC205">The Mode Bits for Access Permission</A>.
<P>
There are two predefined ways you can access the file type portion of
the file mode. First of all, for each type of file, there is a
<DFN>predicate macro</DFN> which examines a file mode value and returns
true or false--is the file of that type, or not. Secondly, you can
mask out the rest of the file mode to get just a file type code.
You can compare this against various constants for the supported file
types.
<P>
All of the symbols listed in this section are defined in the header file
<TT>`sys/stat.h'</TT>.
<A NAME="IDX793"></A>
<P>
The following predicate macros test the type of a file, given the value
<VAR>m</VAR> which is the <CODE>st_mode</CODE> field returned by <CODE>stat</CODE> on
that file:
<P>
<A NAME="IDX794"></A>
<U>Macro:</U> int <B>S_ISDIR</B> <I>(mode_t <VAR>m</VAR>)</I><P>
This macro returns nonzero if the file is a directory.
<P>
<A NAME="IDX795"></A>
<U>Macro:</U> int <B>S_ISCHR</B> <I>(mode_t <VAR>m</VAR>)</I><P>
This macro returns nonzero if the file is a character special file (a
device like a terminal).
<P>
<A NAME="IDX796"></A>
<U>Macro:</U> int <B>S_ISBLK</B> <I>(mode_t <VAR>m</VAR>)</I><P>
This macro returns nonzero if the file is a block special file (a device
like a disk).
<P>
<A NAME="IDX797"></A>
<U>Macro:</U> int <B>S_ISREG</B> <I>(mode_t <VAR>m</VAR>)</I><P>
This macro returns nonzero if the file is a regular file.
<P>
<A NAME="IDX798"></A>
<U>Macro:</U> int <B>S_ISFIFO</B> <I>(mode_t <VAR>m</VAR>)</I><P>
This macro returns nonzero if the file is a FIFO special file, or a
pipe. See section <A HREF="library_14.html#SEC211" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_14.html#SEC211">Pipes and FIFOs</A>.
<P>
<A NAME="IDX799"></A>
<U>Macro:</U> int <B>S_ISLNK</B> <I>(mode_t <VAR>m</VAR>)</I><P>
This macro returns nonzero if the file is a symbolic link.
See section <A HREF="library_13.html#SEC196" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_13.html#SEC196">Symbolic Links</A>.
<P>
<A NAME="IDX800"></A>
<U>Macro:</U> int <B>S_ISSOCK</B> <I>(mode_t <VAR>m</VAR>)</I><P>
This macro returns nonzero if the file is a socket. See section <A HREF="library_15.html#SEC216" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC216">Sockets</A>.
<P>
An alterate non-POSIX method of testing the file type is supported for
compatibility with BSD. The mode can be bitwise ANDed with
<CODE>S_IFMT</CODE> to extract the file type code, and compared to the
appropriate type code constant. For example,
<P>
<PRE>
S_ISCHR (<VAR>mode</VAR>)
</PRE>
<P>
is equivalent to:
<P>
<PRE>
((<VAR>mode</VAR> & S_IFMT) == S_IFCHR)
</PRE>
<P>
<A NAME="IDX801"></A>
<U>Macro:</U> int <B>S_IFMT</B><P>
This is a bit mask used to extract the file type code portion of a mode
value.
<P>
These are the symbolic names for the different file type codes:
<P>
<DL COMPACT>
<A NAME="IDX802"></A>
<DT><CODE>S_IFDIR</CODE>
<DD>This macro represents the value of the file type code for a directory file.
<P>
<A NAME="IDX803"></A>
<DT><CODE>S_IFCHR</CODE>
<DD>This macro represents the value of the file type code for a
character-oriented device file.
<P>
<A NAME="IDX804"></A>
<DT><CODE>S_IFBLK</CODE>
<DD>This macro represents the value of the file type code for a block-oriented
device file.
<P>
<A NAME="IDX805"></A>
<DT><CODE>S_IFREG</CODE>
<DD>This macro represents the value of the file type code for a regular file.
<P>
<A NAME="IDX806"></A>
<DT><CODE>S_IFLNK</CODE>
<DD>This macro represents the value of the file type code for a symbolic link.
<P>
<A NAME="IDX807"></A>
<DT><CODE>S_IFSOCK</CODE>
<DD>This macro represents the value of the file type code for a socket.
<P>
<A NAME="IDX808"></A>
<DT><CODE>S_IFIFO</CODE>
<DD>This macro represents the value of the file type code for a FIFO or pipe.
</DL>
<P>
<A NAME="IDX809"></A>
<A NAME="IDX810"></A>
<A NAME="IDX811"></A>
<H3><A NAME="SEC204" HREF="library_toc.html#SEC204" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC204">File Owner</A></H3>
<P>
Every file has an <DFN>owner</DFN> which is one of the registered user names
defined on the system. Each file also has a <DFN>group</DFN>, which is one
of the defined groups. The file owner can often be useful for showing
you who edited the file (especially when you edit with GNU Emacs), but
its main purpose is for access control.
<P>
The file owner and group play a role in determining access because the
file has one set of access permission bits for the user that is the
owner, another set that apply to users who belong to the file's group,
and a third set of bits that apply to everyone else. See section <A HREF="library_13.html#SEC206" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_13.html#SEC206">How Your Access to a File is Decided</A>, for the details of how access is decided based on this
data.
<P>
When a file is created, its owner is set from the effective user ID of
the process that creates it (see section <A HREF="library_25.html#SEC431" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_25.html#SEC431">The Persona of a Process</A>). The file's group
ID may be set from either effective group ID of the process, or the
group ID of the directory that contains the file, depending on the
system where the file is stored. When you access a remote file system,
it behaves according to its own rule, not according to the system your
program is running on. Thus, your program must be prepared to encounter
either kind of behavior, no matter what kind of system you run it on.
<A NAME="IDX812"></A>
<A NAME="IDX813"></A>
<P>
You can change the owner and/or group owner of an existing file using
the <CODE>chown</CODE> function. This is the primitive for the <CODE>chown</CODE>
and <CODE>chgrp</CODE> shell commands.
<A NAME="IDX814"></A>
<P>
The prototype for this function is declared in <TT>`unistd.h'</TT>.
<P>
<A NAME="IDX815"></A>
<U>Function:</U> int <B>chown</B> <I>(const char *<VAR>filename</VAR>, uid_t <VAR>owner</VAR>, gid_t <VAR>group</VAR>)</I><P>
The <CODE>chown</CODE> function changes the owner of the file <VAR>filename</VAR> to
<VAR>owner</VAR>, and its group owner to <VAR>group</VAR>.
<P>
Changing the owner of the file on certain systems clears the set-user-ID
and set-group-ID bits of the file's permissions. (This is because those
bits may not be appropriate for the new owner.) The other file
permission bits are not changed.
<P>
The return value is <CODE>0</CODE> on success and <CODE>-1</CODE> on failure.
In addition to the usual file name syntax errors (see section <A HREF="library_10.html#SEC115" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_10.html#SEC115">File Name Errors</A>),
the following <CODE>errno</CODE> error conditions are defined for this function:
<P>
<DL COMPACT>
<DT><CODE>EPERM</CODE>
<DD>This process lacks permission to make the requested change.
<P>
Only privileged users or the file's owner can change the file's group.
On most file systems, only privileged users can change the file owner;
some file systems allow you to change the owner if you are currently the
owner. When you access a remote file system, the behavior you encounter
is determined by the system that actually holds the file, not by the
system your program is running on.
<P>
See section <A HREF="library_27.html#SEC464" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_27.html#SEC464">Optional Features in File Support</A>, for information about the
<CODE>_POSIX_CHOWN_RESTRICTED</CODE> macro.
<P>
<DT><CODE>EROFS</CODE>
<DD>The file is on a read-only file system.
</DL>
<P>
<A NAME="IDX816"></A>
<U>Function:</U> int <B>fchown</B> <I>(int <VAR>filedes</VAR>, int <VAR>owner</VAR>, int <VAR>group</VAR>)</I><P>
This is like <CODE>chown</CODE>, except that it changes the owner of the file
with open file descriptor <VAR>filedes</VAR>.
<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -