#include
#include
#include
#include
#include
#include
int main(int argc, char*argv[])
{
int opt;
int fildes;
struct stat file_stat;
off_t file_size;
mode_t file_mode;
char mode[] = "----------";
int pos;
mode_t m;
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%sn", argv[2]);
break;
case 's':
printf("Size:t%lldn", (long long)file_size);
break;
case 'm':
printf("Mode:t%dn", file_mode);
printf("Mode:t%07on", file_mode);
/*
* XXX: S_ISUID and S_ISUID not processed
*/
m = file_mode;
pos = 0;
if (S_ISREG(m))
/* mode[0] = '-' */;
else if (S_ISDIR(m))
mode[pos] = 'd';
else if (S_ISCHR(m))
mode[pos] = 'c';
else if (S_ISBLK(m))
mode[pos] = 'b';
else if (S_ISFIFO(m))
mode[pos] = 'p';
else if (S_ISLNK(m))
mode[pos] = 'l';
else if (S_ISSOCK(m))
mode[pos] = 's';
else if (S_ISWHT(m))
mode[pos] = 'w';
pos++;
if (S_IRUSR & m)
mode[pos] = 'r';
pos++;
if (S_IWUSR & m)
mode[pos] = 'w';
pos++;
if (S_IXUSR & m)
mode[pos] = 'x';
pos++;
if (S_IRGRP & m)
mode[pos] = 'r';
pos++;
if (S_IWGRP & m)
mode[pos] = 'w';
pos++;
if (S_IXGRP & m)
mode[pos] = 'x';
pos++;
if (S_IROTH & m)
mode[pos] = 'r';
pos++;
if (S_IWOTH & m)
mode[pos] = 'w';
pos++;
if (S_IXOTH & m)
mode[pos] = 'x';
printf("Mode:t%sn", mode);
break;
case '?':
printf("Unknow arguement.n");
break;
}
}
return 0;
}