Stat, Fstat, Fstatat, and Lstat functions:
- The STAT function returns the information structure associated with the pathname named file;
- The Fstat function returns information about a file that has been opened in the file descriptor fd;
- The Lstat function is similar to the STAT function, but returns information about the symbolic link when the named file is a symbolic link;
- The Fstatat function returns file statistics relative to the path name of the currently open directory (with the FD parameter pointing to it).
- The specific definitions of struct stat are as follows:
File type: the
- File types include: normal files, directory files, block special files, character special files, FIFO, sockets and symbolic links, file types are represented by St_mode in the stat structure;
- Print file types for each command line parameter:
1#include"apue.h"2 3 intMainintargcChar*argv[])4 {5 inti;6 structstat buf;7 Char*ptr;8 9 for(i =1; i < argc; i++)Ten { Oneprintf"%s:", Argv[i]); A if(Lstat (Argv[i], &buf) <0) - { -Err_ret ("Lstat Error"); the Continue; - } - - if(S_isreg (buf.st_mode)) + { -PTR ="Regular"; + } A Else if(S_isdir (buf.st_mode)) at { -PTR ="Directory"; - } - Else if(S_ISCHR (buf.st_mode)) - { -PTR ="Character Special"; in } - Else if(S_ISBLK (buf.st_mode)) to { +PTR ="Block Special"; - } the Else if(S_isfifo (buf.st_mode)) * { $PTR ="FIFO";Panax Notoginseng } - Else if(S_islnk (buf.st_mode)) the { +PTR ="Symbolic Link"; A } the Else if(S_issock (buf.st_mode)) + { -PTR ="Socket"; $ } $ Else { -PTR ="* * Unknown mode * *"; - } the -printf"%s\n", PTR);Wuyi } the -Exit0); Wu}
View Code
set the user ID and set the group ID:
- The actual user ID and the actual group ID are taken from the login password when landing, the valid user ID and valid group ID and the satellite group ID determine the file access rights; The saved settings user ID and the saved Settings group ID contain a valid user ID and a copy of the valid group ID when executing the program;
- Typically, a valid user ID equals the actual user ID, and the valid group ID equals the actual group ID;
- Set User ID (Set-user-id): When executing this file, set the valid user ID of the process to the user ID of the file owner; set the group ID (set-group-id): When this file is executed, the valid group ID of the process is set to the group ID of the file owner.
Ownership of new files and directories:
- The user ID of the new file is set to the valid user ID of the process;
- For Linux 3.2.0, by default, the group ID of the new file depends on whether the setting group ID bit of the directory it resides in is set, and if set, the new filegroup ID is set to the directory's group ID, otherwise set to the valid group ID of the process.
Functions Access and Faccessat:
- Access and FACCESSAT functions are tested by the actual user ID and the actual group ID;
- Access function instance:
1#include"apue.h"2#include <fcntl.h>3 4 intMainintargcChar*argv[])5 {6 if(ARGC! =2)7 {8Err_quit ("usage:a.out <pathname>");9 }Ten One if(Access (argv[1], R_OK) <0) A { -Err_ret ("access error for%s", argv[1]); - } the Else - { -printf"Read access ok\n"); - } + - if(Open (argv[1], o_rdonly) <0) + { AErr_ret ("open error for%s", argv[1]); at } - Else - { -printf"Open for reading ok\n"); - } - inExit0); -}
View Code
function Umask:
- The Umask function creates a masking word for the process settings file mode;
- In the file mode to create a bitmask of 1 bits, the corresponding bit in the document mode must be closed (XOR);
- Umask Function Example:
1#include"apue.h"2#include <fcntl.h>3 4 #defineRWRWRW (S_irusr | S_IWUSR | S_irgrp | S_iwgrp | S_iroth | S_iwoth)5 6 intMainvoid)7 {8Umask0);9 if(Creat ("Foo", RWRWRW) <0)Ten { OneErr_sys ("creat error for Foo"); A } - -Umask (S_irgrp | S_iwgrp | S_iroth |s_iwoth); the if(Creat ("Bar", RWRWRW) <0) - { -Err_sys ("creat error for Bar"); - } + -Exit0); +}
View Code
Functions chmod, Fchmod, and Fchmodat:
- chmod, Fchmod, and Fchmodat are used to change file access rights;
- chmod instance functions:
1#include"apue.h"2 3 intMainvoid)4 {5 structstat statbuf;6 7 if(Stat ("Foo", &statbuf) <0)8 {9Err_sys ("stat error for Foo");Ten } One A if(Chmod ("Foo", (Statbuf.st_mode & ~S_IXGRP | S_isgid)) <0) - { -Err_sys ("chmod error for Foo"); the } - - if(Chmod ("Bar", S_IRUSR | S_IWUSR | S_irgrp | S_iroth) <0) - { +Err_sys ("chmod error for Bar"); - } + AExit0); at}
View Code
Apue reading notes: Files and directories