Apue reading notes: Files and directories

Source: Internet
Author: User
Tags lstat

Stat, Fstat, Fstatat, and Lstat functions:

    1. The STAT function returns the information structure associated with the pathname named file;
    2. The Fstat function returns information about a file that has been opened in the file descriptor fd;
    3. The Lstat function is similar to the STAT function, but returns information about the symbolic link when the named file is a symbolic link;
    4. The Fstatat function returns file statistics relative to the path name of the currently open directory (with the FD parameter pointing to it).
    5. The specific definitions of struct stat are as follows:

File type: the

    1. 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;
    2. 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:

    1. 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;
    2. Typically, a valid user ID equals the actual user ID, and the valid group ID equals the actual group ID;
    3. 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:

    1. The user ID of the new file is set to the valid user ID of the process;
    2. 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:

    1. Access and FACCESSAT functions are tested by the actual user ID and the actual group ID;
    2. 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:

    1. The Umask function creates a masking word for the process settings file mode;
    2. In the file mode to create a bitmask of 1 bits, the corresponding bit in the document mode must be closed (XOR);
    3. 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:

    1. chmod, Fchmod, and Fchmodat are used to change file access rights;
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.