标签:style blog http ar color os sp for strong
stat, fstat, fstatat 和 lstat函数:
文件类型:
1 #include "apue.h" 2 3 int main(int argc, char *argv[]) 4 { 5 int i; 6 struct stat buf; 7 char *ptr; 8 9 for (i = 1; i < argc; i++) 10 { 11 printf("%s: ", argv[i]); 12 if (lstat(argv[i], &buf) < 0) 13 { 14 err_ret("lstat error"); 15 continue; 16 } 17 18 if (S_ISREG(buf.st_mode)) 19 { 20 ptr = "regular"; 21 } 22 else if (S_ISDIR(buf.st_mode)) 23 { 24 ptr = "directory"; 25 } 26 else if (S_ISCHR(buf.st_mode)) 27 { 28 ptr = "character special"; 29 } 30 else if (S_ISBLK(buf.st_mode)) 31 { 32 ptr = "block special"; 33 } 34 else if (S_ISFIFO(buf.st_mode)) 35 { 36 ptr = "fifo"; 37 } 38 else if (S_ISLNK(buf.st_mode)) 39 { 40 ptr = "symbolic link"; 41 } 42 else if (S_ISSOCK(buf.st_mode)) 43 { 44 ptr = "socket"; 45 } 46 else { 47 ptr = "** unknown mode **"; 48 } 49 50 printf("%s\n", ptr); 51 } 52 53 exit(0); 54 }
设置用户ID和设置组ID:
新文件和目录的所有权:
函数access和faccessat:
1 #include "apue.h" 2 #include <fcntl.h> 3 4 int main(int argc, char *argv[]) 5 { 6 if (argc != 2) 7 { 8 err_quit("usage: a.out <pathname>"); 9 } 10 11 if (access(argv[1], R_OK) < 0) 12 { 13 err_ret("access error for %s", argv[1]); 14 } 15 else 16 { 17 printf("read access ok\n"); 18 } 19 20 if (open(argv[1], O_RDONLY) < 0) 21 { 22 err_ret("open error for %s", argv[1]); 23 } 24 else 25 { 26 printf("open for reading ok\n"); 27 } 28 29 exit(0); 30 }
函数umask:
1 #include "apue.h" 2 #include <fcntl.h> 3 4 #define RWRWRW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) 5 6 int main(void) 7 { 8 umask(0); 9 if (creat("foo", RWRWRW) < 0) 10 { 11 err_sys("creat error for foo"); 12 } 13 14 umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); 15 if (creat("bar", RWRWRW) < 0) 16 { 17 err_sys("creat error for bar"); 18 } 19 20 exit(0); 21 }
函数chmod, fchmod和fchmodat:
1 #include "apue.h" 2 3 int main(void) 4 { 5 struct stat statbuf; 6 7 if (stat("foo", &statbuf) < 0) 8 { 9 err_sys("stat error for foo"); 10 } 11 12 if (chmod("foo", (statbuf.st_mode & ~S_IXGRP | S_ISGID)) < 0) 13 { 14 err_sys("chmod error for foo"); 15 } 16 17 if (chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) 18 { 19 err_sys("chmod error for bar"); 20 } 21 22 exit(0); 23 }
标签:style blog http ar color os sp for strong
原文地址:http://www.cnblogs.com/skycore/p/4106674.html