文件名 main.c
代码: 全选
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char*argv[])
{
int opt;
int fildes;
struct stat file_stat;
off_t file_size;
mode_t file_mode;
if (argc != 3) { /*参数太少*/
printf("Too less arguement.\n");
exit(1);
}
if ((fildes = open(argv[2], O_RDONLY)) == -1) { /*打开文件出错*/
printf("Open File Error.\n");
exit(1);
}
if ((fstat(fildes, &file_stat)) == -1) { /*获取文件属性出错*/
printf("Fstat File Error.\n");
exit(1);
}
file_size = file_stat.st_size;
file_mode = file_stat.st_mode;
while ((opt = getopt(argc, argv, "nsm")) != -1) { /*按照第一个参数的匹配进行处理*/
switch(opt) {
case 'n':
printf("Name:\t%s\n", argv[2]);
break;
case 's':
printf("Size:\t%d\n", file_size);
break;
case 'm':
printf("Mode:\t%d\n", file_mode);
break;
case '?':
printf("Unknow arguement.\n");
break;
}
}
return 0;
}
gcc -o run main.c 没出错
但假如想测试输出文件权限时 run -m file
却只能得到33188 不能得到-rw-r--r--
请问怎样实现这种-rw-r--r--模式的输出