文件状态信息
文件狀態(tài)信息
例1:
mystat.c
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/stat.h> #include <unistd.h> #include <sys/types.h> #include <errno.h>#define ERROR(flag) \if(flag) \{ \printf("%d: ",__LINE__); \fflush(stdout); \perror("error"); \exit(errno); \} int main(int argc, char *argv[]) {struct stat buf;if (argc != 2){printf("Usage: my_stat <filename>\n");exit(0);}int ret = stat(argv[1], &buf);ERROR(ret == -1);printf("device is: %d\n", buf.st_dev);printf("inode is: %d\n", buf.st_ino);printf("mode is: %o\n", buf.st_mode);printf("number of hard links is: %d\n", buf.st_nlink);printf("user ID of owner is: %d\n", buf.st_uid);printf("group ID of owner is: %d\n", buf.st_gid);printf("device type (if inode device) is: %d\n", buf.st_rdev);printf("total size, in bytes is: %d\n", buf.st_size);printf(" blocksize for filesystem I/O is: %d\n", buf.st_blksize);printf("number of blocks allocated is: %d\n", buf.st_blocks);printf("time of last access is: %s", ctime(&buf.st_atime));printf("time of last modification is: %s", ctime(&buf.st_mtime));printf("time of last change is: %s", ctime(&buf.st_ctime));return 0; }編譯/鏈接/執(zhí)行后, ?輸出結果如下:
觀察輸出結果, 與前面的命令的輸出結果一致
?
例2: chmod接口API
mychmod.c
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <errno.h>#define ERROR(flag) \if(flag) \{ \printf("%d: ",__LINE__); \fflush(stdout); \perror("error"); \exit(errno); \}int main(int argc, char ** argv) {int mode; int mode_u; int mode_g; int mode_o; mode = atoi(argv[1]);mode_u = mode / 100;mode_g = (mode - (mode_u*100)) / 10;mode_o = mode - (mode_u*100) - (mode_g*10);mode = (mode_u * 8 * 8) + (mode_g * 8) + mode_o; int ret = chmod(argv[2], mode);ERROR(ret == -1);return 0; }編譯鏈接執(zhí)行, 輸出結果如下:
文件的讀寫等屬性發(fā)生改變
?
例3: chown接口API
mychown.c
#include <stdio.h> #include <errno.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <fcntl.h>#define ERROR(flag) \if(flag) \{ \printf("%d: ",__LINE__); \fflush(stdout); \perror("error"); \exit(errno); \}int main(int argc,char *argv[]) {//int ret = chown(argv[1],500,500);int ret = chown(argv[1],7,7);ERROR(ret == -1);return 0; }編譯鏈接執(zhí)行, 結果輸出如下:
文件的擁有者屬性發(fā)生變化
?
轉載于:https://www.cnblogs.com/zhanglong71/p/5119965.html
總結
- 上一篇: 计算机网络之物理层:5、数据的交换方式(
- 下一篇: (王道408考研数据结构)第八章排序-第