Summary of Chapter 3 of UNIX Linux programming practice course

Keywords: Programming Linux shell

  • Under Linux, everything is a file, so the directory is only a special file. It can be opened, read, and close just like the file, but the function is changed to opendir(),readdir(),closedir()
  • Data structure used to read directory file -- struct directory
    #include <dirent.h>
    
    struct dirent {
           ino_t          d_ino;       /* Inode number */
           off_t          d_off;       /* Not an offset; see below */
           unsigned short d_reclen;    /* Length of this record */
           unsigned char  d_type;      /* Type of file; not supported
                                          by all filesystem types */
           char           d_name[256]; /* Null-terminated filename */
    };

           

  • Data structure used to store file state -- struct stat

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    int stat(const char *path, struct stat *buf);
    
    struct stat {
        dev_t     st_dev;     /* ID of device containing file */
        ino_t     st_ino;     /* inode number */
        mode_t    st_mode;    /* protection */
        nlink_t   st_nlink;   /* number of hard links */
        uid_t     st_uid;     /* user ID of owner */
        gid_t     st_gid;     /* group ID of owner */
        dev_t     st_rdev;    /* device ID (if special file) */
        off_t     st_size;    /* total size, in bytes */
        blksize_t st_blksize; /* blocksize for file system I/O */
        blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
        time_t    st_atime;   /* time of last access */
        time_t    st_mtime;   /* time of last modification */
        time_t    st_ctime;   /* time of last status change */
    };

       

  • Data structure used to store user information -- struct passwd

    #include < sys / types.h >
    #include < pwd.h >
    
    struct passwd * getpwnam(const char * name ); //Get user information through user name
    
    struct passwd * getpwuid(uid_t  uid );        //Get user information through user ID
    
    struct passwd {
        char   *pw_name;       /* username */
        char   *pw_passwd;     /* user password */
        uid_t   pw_uid;        /* user ID */
        gid_t   pw_gid;        /* group ID */
        char   *pw_gecos;      /* user information */
        char   *pw_dir;        /* home directory */
        char   *pw_shell;      /* shell program */
    }
    
    

      

  • Data structure used to store user group information -- struct group

    #include <sys/types.h>
    #include <grp.h>
    
    struct group *getgrnam(const char *name);  //Get user group information by user group name
    
    struct group *getgrgid(gid_t gid);         //Get user group information through user group ID
    
    struct group {
        char   *gr_name;       /* group name */
        char   *gr_passwd;     /* group password */
        gid_t   gr_gid;        /* group ID */
        char  **gr_mem;        /* group members */
    };

     

  • Three file special properties:

    1. SUID: generally applied to binary files; the executor will temporarily have all the permissions of the file owner

    2. SGID: for binary files, SGID has the same effect as SUID, so that the executor temporarily has all the permissions of the user group to which the file belongs; for directory, the file group created in the directory is the same as the directory group

    3. SBIT: only valid for the directory, so that only the file owner and root can delete the files created in the directory, and other users cannot delete the files in the directory

Posted by akufen on Sat, 07 Dec 2019 17:00:31 -0800