⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 library_13.html

📁 Linux程序员的工作手册
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<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 theGNU 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 ina <CODE>struct stat</CODE> object.  All three functions are declared in theheader 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 thefile 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 getdescribe the file that the link points to.  If the link points to anonexistent file name, then <CODE>stat</CODE> fails, reporting a nonexistentfile.<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 conditionsare 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 anopen 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 notfollow symbolic links.  If <VAR>filename</VAR> is the name of a symboliclink, <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 fileattributes, contains two kinds of information: the file type code, andthe access permission bits.  This section discusses only the type code,which you can use to tell whether the file is a directory, whether it isa 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 ofthe file mode.  First of all, for each type of file, there is a <DFN>predicate macro</DFN> which examines a file mode value and returnstrue or false--is the file of that type, or not.  Secondly, you canmask out the rest of the file mode to get just a file type code.You can compare this against various constants for the supported filetypes.<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> onthat 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 (adevice 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 devicelike 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 apipe.  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 forcompatibility with BSD.  The mode can be bitwise ANDed with<CODE>S_IFMT</CODE> to extract the file type code, and compared to theappropriate type code constant.  For example,<P><PRE>S_ISCHR (<VAR>mode</VAR>)</PRE><P>is equivalent to:<P><PRE>((<VAR>mode</VAR> &#38; 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 modevalue.<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 acharacter-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-orienteddevice 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 namesdefined on the system.  Each file also has a <DFN>group</DFN>, which is oneof the defined groups.  The file owner can often be useful for showingyou who edited the file (especially when you edit with GNU Emacs), butits main purpose is for access control.<P>The file owner and group play a role in determining access because thefile has one set of access permission bits for the user that is theowner, 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 thisdata.<P>When a file is created, its owner is set from the effective user ID ofthe 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 groupID may be set from either effective group ID of the process, or thegroup ID of the directory that contains the file, depending on thesystem where the file is stored.  When you access a remote file system,it behaves according to its own rule, not according to the system yourprogram is running on.  Thus, your program must be prepared to encountereither 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 usingthe <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-IDand set-group-ID bits of the file's permissions.  (This is because thosebits may not be appropriate for the new owner.)  The other filepermission 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 theowner.  When you access a remote file system, the behavior you encounteris determined by the system that actually holds the file, not by thesystem 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 filewith open file descriptor <VAR>filedes</VAR>.<P>The return value from <CODE>fchown</CODE> is <CODE>0</CODE> on success and <CODE>-1</CODE>on failure.  The following <CODE>errno</CODE> error codes are defined for thisfunction:<P><DL COMPACT><DT><CODE>EBADF</CODE><DD>The <VAR>filedes</VAR> argument is not a valid file descriptor.<P><DT><CODE>EINVAL</CODE><DD>The <VAR>filedes</VAR> argument corresponds to a pipe or socket, not an ordinaryfile.<P><DT><CODE>EPERM</CODE><DD>This process lacks permission to make the requested change.  Fordetails, see <CODE>chmod</CODE>, above.<P><DT><CODE>EROFS</CODE><DD>The file resides on a read-only file system.</DL><P><H3><A NAME="SEC205" HREF="library_toc.html#SEC205" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC205">The Mode Bits for Access Permission</A></H3><P>The <DFN>file mode</DFN>, stored in the <CODE>st_mode</CODE> field of the fileattributes, contains two kinds of information: the file type code, and

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -