I. Permissions for the directory
(1) The directory file access rights are divided into three groups, respectively, the owner, the user, and others. There are 3 permission bits for each permission group, read, write, and execute, respectively.
Note: You can use the STAT function to get the status information for a directory file. The permissions are st_mode in the stat structure.
(2) Test Directory access permissions: The program gets the directory file status information, if it is a non-directory file, then the program exits. The program checks whether the owner user of the catalog file has read-write and point-to permissions and outputs the results in full.
1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4#include <sys/stat.h>5 intMainvoid)6 {7 structStat buf;/*Store file status information*/8 if(Stat (" /Home", &buf) = =-1){/*Get file status information*/9Perror ("fail to stat");TenExit1); One } A if(! S_isdir (Buf.st_mode)) {/*Non-catalog files*/ -printf"This is not a directory file\n"); -Exit1); the } - if(S_irusr & Buf.st_mode)/*owner user has read directory permissions*/ -printf"user can read the dir\n"); - if(S_iwusr & Buf.st_mode)/*owner user has write directory permission*/ +printf"user can write the dir\n"); - if(S_ixusr & Buf.st_mode)/*owner user has execute directory permission*/ +printf"user can through the dir\n"); A return 0; at}
(3)
Two Create a directory
(1) function
mkdir (const char* pathname,mode_t mode);
(2) return
Success: 0
Failed:-1
(3) Implement Create directory
1#include <stdio.h>2#include <unistd.h>3#include <stdlib.h>4#include <sys/stat.h>5 intMainvoid)6 {7 if(MkDir ("/home/tmp", S_IRUSR | S_IWUSR | S_IXUSR) = =-1){/*Chong8 Build a directory*/9Perror ("fail to mkdir");TenExit1); One } Aprintf"successfully make a dir\n");/*Output hint Information*/ - return 0; -}
(4)
Third, delete a directory
(1) function: int rmdir (const char*pathname)
return value:
Success: 1
Failed:-1
(2) Realize
1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4 intMainvoid)5 {6 if(RmDir ("/home/tmp") == -1){/*Output a directory*/7Perror ("fail to Rmkdir");8Exit1);9 }Tenprintf"successfully remove a dir\n");/*Output hint Information*/ One return 0; A}
(3)
Directory operations in Linux <1>