用C语言实现 ls -al 功能

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 #include <sys/stat.h>
 6 #include <dirent.h>
 7 #include <pwd.h>
 8 #include <utime.h>
 9 #include <time.h>
10 #include <grp.h>
11 
12 #ifndef FILE_MAX
13 #define FILE_MAX 1024
14 #endif
15 
16 char fileName[FILE_MAX][FILENAME_MAX];
17 
18 int rwxMode[] = {0,S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP, S_IROTH, S_IWOTH, S_IXOTH};
19 
20 char getFileType(mode_t mode){
21   if(S_ISDIR(mode)) return 'd';
22   else if(S_ISCHR(mode))  return 'c';
23   else if(S_ISBLK(mode)) return 'b';
24   else if(S_ISFIFO(mode)) return 'f';
25   else if(S_ISLNK(mode)) return 'l';
26   else if(S_ISSOCK(mode)) return 's';
27   else return '-';
28 }
29 
30 void sort(char array[][FILENAME_MAX], int n){
31   int i,j;
32   char temp[FILENAME_MAX];
33   for(i = n-1; i >= 0; --i)
34     for(j = 0; j < i; ++j)
35       if(strcmp(array[j], array[j+1]) > 0){
36         strcpy(temp, array[j]);
37         strcpy(array[j], array[j+1]);
38         strcpy(array[j+1], temp);
39       }
40 }
41 
42 int main(int argc, char *argv[]){
43   char *dirPath;
44   if(argc >= 2) dirPath = argv[1];
45   else{
46     dirPath = malloc(FILENAME_MAX);
47     if(getcwd(dirPath, FILENAME_MAX) == NULL){
48       perror("getcwd returns error");
49       exit(EXIT_FAILURE);
50     }
51   }
52   
53   DIR * dirp = opendir(dirPath);
54   if(dirp == NULL){
55     perror("opendir returns error");
56     exit(EXIT_FAILURE);
57   }
58   int filen = 0, i, j;
59   while(1){
60     struct dirent * fileInfo = readdir(dirp);
61     if(fileInfo == NULL) break;
62     strcpy(fileName[filen++], fileInfo->d_name);
63   }
64   sort(fileName, filen);
65   for(i = 0; i < filen; ++i){
66     struct passwd * userInfo;
67     struct group * groupInfo;
68     struct stat fileStat;
69     struct tm *mTime;
70     char fileMode[11], filePath[FILENAME_MAX];
71     if(dirPath[strlen(dirPath)-1] != '/')
72       sprintf(filePath,"%s/%s",dirPath, fileName[i]);
73     else sprintf(filePath, "%s%s",dirPath, fileName[i]);
74     if(stat(filePath, &fileStat) < 0){
75       perror("stat returns error");
76       exit(EXIT_FAILURE);
77     }
78     strcpy(fileMode, "-rwxrwxrwx");
79     fileMode[0] = getFileType(fileStat.st_mode);
80     for(j = 1; j < 10; ++j)
81       if(!(fileStat.st_mode & rwxMode[j])) fileMode[j] = '-';
82 
83     userInfo = getpwuid(fileStat.st_uid);
84     groupInfo = getgrgid(fileStat.st_gid);
85     if(userInfo == NULL || groupInfo == NULL){
86       perror("getpwuid returns error");
87       exit(EXIT_FAILURE);
88     }
89     mTime = localtime(&fileStat.st_mtime);
90     printf("%s %2d %8s %8s %5d %2d月 %2d %02d:%02d %s\n",fileMode,fileStat.st_nlink,userInfo->pw_name, groupInfo->gr_name,(int)fileStat.st_size,mTime->tm_mon+1, mTime->tm_mday, mTime->tm_hour, mTime->tm_min, fileName[i]); 
91   }
92   if(argc < 2) free(dirPath);
93   return 0;
94 }

 

转载于:https://www.cnblogs.com/aclove/p/3760674.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值