1 #include<stdio.h>
2 #include<unistd.h>
3 #include<sys/types.h>
4 #include<sys/stat.h>
5 #include<pwd.h>
6 #include<grp.h>
7 #include<time.h>
8 #include<glob.h>
9 #include<dirent.h>
10 #include<errno.h>
11 #include<string.h>
12 static int l_show(char *path);
13 static int is_dir(char *path);
14 static int h_show(char *path);
15
16 static int my_blocks(char *path)
17 {
18 struct stat mystat;
19 int sum = 0;
20
21 if(lstat(path,&mystat) < 0)
22 {
23 perror("lstat()");
24 return -1;
25 }
26
27 sum = mystat.st_blocks;
28
29 return sum / 2;
30
31
32
33 }
34
35 static int a_show(char *path)
36 {
37 DIR *dp = NULL;
38 struct dirent *a = NULL;
39 dp = opendir(path);
40 char buf[1024] = {};
41 int sum = 0;
42 if(dp == NULL)
43 {
44 perror("opendir()");
45 return 1;
46 }
47
48 while(1)
49 {
50 a = readdir(dp);
51 if(a == NULL)
52 {
53 if(errno)
54 {
55 perror("readdir()");
56 return 1;
57 }
58 break;
59 }
60 memset(buf,'\0',1024);
61 strcpy(buf,path);
62 strcat(buf,"/");
63 strcat(buf,a->d_name);
64 sum += my_blocks(buf);
65 printf("%s ",a->d_name);
66 }
67 printf("\n");
68 printf("总用量:%dk\n",sum);
69 closedir(dp);
70 return 0;
71 }
72
73 static int i_show(char *path)
74 {
75 struct stat mystat;
76 DIR *dp = NULL;
77 struct dirent *entry;
78
79 if(lstat(path,&mystat) < 0)
80 {
81 perror("lstat()");
82 return -1;
83 }
84
85 if(S_ISDIR(mystat.st_mode))
86 {
87 dp = opendir(path);
88 if(dp == NULL)
89 {
90 perror("opendir()");
91 return -1;
92 }
93
94 while(1)
95 {
96 entry = readdir(dp);
97 if(entry == NULL)
98 {
99 if(errno)
100 {
101 perror("readdir()");
102 return -1;
103 }
104 break;
105 }
106 if(entry->d_name[0] == '.')
107 continue;
108 printf("%ld ",entry->d_ino);
109 printf("%s\n",entry->d_name);
110 }
111 closedir(dp);
112 }
113 else
114 {
115 printf("%s\n",path);
116 }
117
118
119 return 0;
120 }
121
122
123 int main(int argc,char *argv[])
124 {
125 char *optstring = "-laih";
126 struct stat mystat;
127 int c;
128 if(argc < 2)
129 return 1;
130
131
132 if(stat(argv[1],&mystat) == -1)
133 {
134 perror("stat()");
135 return 1;
136 }
137
138 while(1)
139 {
140 c = getopt(argc,argv,optstring);
141 if(c == -1)
142 break;
143 switch(c)
144 {
145 case'l':
146 is_dir(argv[1]);
147 break;
148 case'h':
149 h_show(argv[1]);
150 printf("\n");
151 break;
152 case'i':
153 i_show(argv[1]);
154 break;
155 case'a':
156 a_show(argv[1]);
157 break;
158 case'?':
159 break;
160 case'1':
161 printf("费选项参数%s\n",argv[optind-1]);
162 default:
163 break;
164
165 }
166 }
167
168 return 0;
169 }
170
171
172 static int h_show(char *path)
173 {
174 struct stat mystat;
175 DIR *dp = NULL;
176 struct dirent *entry;
177 int sum = 0;
178 char buf[1024] = {};
179
180 if(lstat(path,&mystat) < 0)
181 {
182 perror("lstat()");
183 return -1;
184 }
185
186 if(S_ISDIR(mystat.st_mode))
187 {
188 dp = opendir(path);
189 if(dp == NULL)
190 {
191 perror("opendir()");
192 return -1;
193 }
194
195
196 while(1)
197 {
198 entry = readdir(dp);
199 if(entry == NULL)
200 {
201 if(errno)
202 {
203 perror("readdir()");
204 return -1;
205 }
206 break;
207 }
208 if(entry->d_name[0] == '.')
209 continue;
210 printf("%s ",entry->d_name);
211 memset(buf,'\0',1024);
212 strcpy(buf,path);
213 strcat(buf,"/");
214 strcat(buf,entry->d_name);
215 sum += my_blocks(buf);
216 }
217 printf("总用量:%d k\n",sum);
218 closedir(dp);
219 }
220 else
221 {
222 printf("%s ",path);
223 }
224
225
226 return 0;
227 }
228
229
230
231
232 static int is_dir(char *path)
233 {
234 struct stat mystat;
235 DIR *dp = NULL;
236 struct dirent *entry;
237 char buf[1024] = {};
238 char add[1024] = {};
239
240 if(lstat(path,&mystat) < 0)
241 {
242 perror("lstat()");
243 return -1;
244 }
245
246
247 if(S_ISDIR(mystat.st_mode))
248 {
249 dp = opendir(path);
250 if(NULL == dp)
251 {
252 perror("opendir()");
253 return -1;
254 }
255
256 while(1)
257 {
258 memset(buf,'\0',1024);
259 strcpy(buf,path);
260 strcat(buf,"/");
261 entry = readdir(dp);
262 if(entry == NULL)
263 {
264 if(errno)
265 {
266 perror("readdir()");
267 return -1;
268 }
269 break;
270 }
271 memset(add,'\0',1024);
272 strcpy(add,entry->d_name);
273 if(add[0] == '.')
274 continue;
275 strcat(buf,entry->d_name);
276 l_show(buf);
277 printf("%s\n",entry->d_name);
278 }
279 closedir(dp);
280 }
281 else
282 l_show(path);
283
284 }
285
286 static int l_show(char *path)
287 {
288 struct stat mystat;
289 struct passwd *p = NULL;
290 struct group *grp = NULL;
291 struct tm *tme = NULL;
292 char buf[1024] = {};
293
294 if(lstat(path,&mystat) < 0)
295 {
296 perror("lstat()");
297 return -1;
298 }
299
300 //文件类型
301 switch(mystat.st_mode & S_IFMT)
302 {
303 case S_IFREG:
304 printf("-");
305 break;
306 case S_IFDIR:
307 printf("d");
308 break;
309 case S_IFBLK:
310 printf("b");
311 break;
312 case S_IFCHR:
313 printf("c");
314 break;
315 case S_IFIFO:
316 printf("p");
317 break;
318 case S_IFSOCK:
319 printf("s");
320 break;
321 case S_IFLNK:
322 printf("l");
323 break;
324 default:
325 break;
326 }
327 //文件拥有者权限
328 if(mystat.st_mode & S_IRUSR)
329 putchar('r');
330 else
331 putchar('-');
332 if(mystat.st_mode & S_IWUSR)
333 putchar('w');
334 else
335 putchar('-');
336 if(mystat.st_mode & S_IXUSR){
337 if(mystat.st_mode & S_ISUID){
338 putchar('s');
339 }else
340 putchar('x');
341 }else
342 putchar('-');
343
344 //文件所属组权限
345 if(mystat.st_mode & S_IRGRP)
346 putchar('r');
347 else
348 putchar('-');
349 if(mystat.st_mode & S_IWGRP)
350 putchar('w');
351 else
352 putchar('-');
353 if(mystat.st_mode & S_IXGRP){
354 putchar('x');
355 }else
356 putchar('-');
357
358 //文件其他组权限
359 if(mystat.st_mode & S_IROTH)
360 putchar('r');
361 else
362 putchar('-');
363 if(mystat.st_mode & S_IWOTH)
364 putchar('w');
365 else
366 putchar('-');
367 if(mystat.st_mode & S_IXOTH){
368 putchar('x');
369 }else
370 putchar('-');
371
372
373 //硬链接
374 printf(" %ld ",mystat.st_nlink);
375 //文件拥有者名字
376 p = getpwuid(mystat.st_uid);
377 printf("%s ",p->pw_name);
378
379
380 //文件所属组名字
381 grp = getgrgid(mystat.st_gid);
382 printf("%s ",grp->gr_name);
383
384 printf("%ld ",mystat.st_size);
385
386
387 tme = localtime(&mystat.st_mtim.tv_sec);
388 strftime(buf,1024,"%m月 %d %H:%M",tme);
389 printf("%s ",buf);
390
391
392 //printf(" %s ",argv[1]);
393
394 }