linux ls 源码 (自写)

  1 #include <stdio.h>
  2 #include <unistd.h>
  3 #include <sys/types.h>
  4 #include <sys/stat.h>
  5 #include <dirent.h>
  6 #include <errno.h>
  7 #include <string.h>
  8 #include <time.h>
  9 #include <pwd.h>
 10 #include <grp.h>
 11 #include <glob.h>
 12 #define BUFSIZE 1024
 13 static int is_dot_dir(const char *path)
 14 {
 15     char *p = NULL;
 16 
 17     p = strrchr(path, '.');
 18     if (p != NULL) {
 19         if (!strcmp(p, ".") || !strcmp(p, "..") || !strcmp(p,"./"))
 20             return 1;
 21     }
 22 
 23     return 0;
 24 }
 25 
 26 static int myino(const char *path)
 27 {
 28     DIR *dp = NULL;
 29     struct dirent *entry = NULL;
 30     char buf[BUFSIZE]={};
 31     dp = opendir(path);
 32     int i;
 33     if (dp == NULL)
 34     {
 35         perror("opendir()");
 36         return 1;
 37     }
 38     while (1)
 39     {
 40         entry = readdir(dp);
 41         if (entry == NULL)
 42         {
 43             if (errno)
 44             {
 45                 perror("readdir()");
 46                 goto ERROR;
 47             }
 48             break;
 49         }
 50 
 51         if(!is_dot_dir(entry->d_name))
 52         {
 53             strcpy(buf,entry->d_name);
 54             printf("     %ld %s",entry->d_ino,buf);
 55         }
 56 
 57     }
 58         printf("\n");
 59 
 60 
 61 ERROR:
 62     closedir(dp);
 63     return 1;
 64 
 65 }
 66 
 67 
 68 static int mypath(const char *path)
 69 {
 70     struct stat mystat;
 71     if (lstat(path, &mystat) == -1)
 72     {
 73         perror("stat()");
 74         return 1;
 75     }
 76 
 77     
 78     switch(mystat.st_mode & S_IFMT)// 类型
 79     {
 80         case S_IFREG:
 81             printf("-");
 82             break;
 83         case S_IFDIR:
 84             putchar('d');
 85             break;
 86         default:
 87             break;
 88     }
 89     
 90     
 91     if (mystat.st_mode & S_IRUSR)// 权限
 92         putchar('r');
 93     else
 94         putchar('-');
 95     if (mystat.st_mode & S_IWUSR)
 96         putchar('w');
 97     else 
 98         putchar('-');
 99     if (mystat.st_mode & S_IXUSR)
100     {
101         if (mystat.st_mode & S_ISUID) 
102         {
103             putchar('s');
104         } else 
105             putchar('x');
106     } else
107         putchar('-');
108 
109     
110     printf(" %ld ", mystat.st_nlink);// 硬链接
111 
112     // 文件拥有者名
113     struct passwd *pwd = NULL; 
114     pwd = getpwuid(mystat.st_uid);
115     printf("%s ", pwd->pw_name);
116 
117     // 文件所属组
118     struct group *grp = NULL;
119     grp = getgrgid(mystat.st_gid);
120     printf("%s ", grp->gr_name);
121 
122     // 总字节个数 磁盘空间大小st_blocks/2 k
123     printf("%ld ", mystat.st_size);
124 
125     // 获取文件时间 atime mtime ctime
126     struct tm *tmp = NULL;
127     tmp = localtime(&mystat.st_mtim.tv_sec);//将时间错转换成已分解的结构体
128     // if error
129     char buf[BUFSIZE] = {};
130     strftime(buf, BUFSIZE, "%m月  %d %H:%M", tmp);
131     printf("%s ", buf);
132 
133     // 文件名
134     printf("%s ", path);
135 
136     putchar('\n');
137     return 0;
138 }
139 
140 static int mydir(const char *path)
141 {
142 
143     DIR *dp = NULL;
144     struct dirent *entry = NULL;
145     char buf[BUFSIZE]={};
146     dp = opendir(path);
147     if (dp == NULL)
148     {
149         perror("opendir()");
150         return 1;
151     }
152     while (1)
153     {
154         entry = readdir(dp);
155         if (entry == NULL)
156         {
157             if (errno)
158             {
159                 perror("readdir()");
160                 goto ERROR;
161             }
162             break;
163         }
164 
165 
166             printf("%s ", entry->d_name);
167 
168     }
169             printf("\n");
170 
171     closedir(dp);
172     return 0;
173 
174 ERROR:
175     closedir(dp);
176     return 1;
177             
178 }
179 
180 
181 
182 static int myndir(const char *path)
183 {
184 
185     DIR *dp = NULL;
186     struct dirent *entry = NULL;
187     char buf[BUFSIZE]={};
188     dp = opendir(path);
189     if (dp == NULL)
190     {
191         perror("opendir()");
192         return 1;
193     }
194     while (1)
195     {
196         entry = readdir(dp);
197         if (entry == NULL)
198         {
199             if (errno)
200             {
201                 perror("readdir()");
202                 goto ERROR;
203             }
204             break;
205         }
206         if(!is_dot_dir(entry->d_name))
207         {
208             strcpy(buf,entry->d_name);
209             printf("%s ",buf);
210         }
211 
212             
213 
214     }
215             printf("\n");
216 
217     closedir(dp);
218     return 0;
219 
220 ERROR:
221     closedir(dp);
222     return 1;
223             
224 }
225             
226 
227 int main(int argc, char **argv)
228 {
229     if(argc < 2)
230         return 1;
231     char *optstring = "-l:a:i:h:";
232     int c;
233 
234     while (1) {
235         c = getopt(argc, argv, optstring);    
236         if (c == -1)
237             break;
238         switch (c) {
239             case 'l':
240                 mypath(argv[2]);//显示文件的详细信息
241         
242                 break;
243             case 'a':
244                 mydir(argv[2]);//显示目录文件名(带隐藏文件)
245                 
246                 break;
247             case 'i':
248                 myino(argv[2]);//显示文件的inod号
249                 break;
250             case 'h':
251                 myndir(argv[2]);//显示目录名(不带隐藏文件)
252                     break;
253             case '?':
254                 printf("不认识此选项%s\n", argv[optind-1]);
255                 break;
256             case 1:
257                 printf("非选项参数%s\n", argv[optind-1]);
258                 break;
259             default:
260                 break;
261         }
262     }
263 
264     return 0;
265 
266 }

 

转载于:https://www.cnblogs.com/lee-888/p/10538516.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值