-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
365 lines (357 loc) · 12.7 KB
/
list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
long least = 0;
long huge = 0;
int day = 0;
char rec_fpath[20000][260]; // 存储递归文件路径
char user_fpath[200][260]; // 存储用户输入文件路径
int rec_top = 0, user_top = 0;
int rec_id = 0, user_id = 0; // 分别控制递归和用户输入文件路径
// 限制size或者modify_time打印
void limit_print(char *d, long st_size, long mtime, int is_least, int is_huge, int is_modify, long t)
{
if (is_least && is_huge) //是否文件大小限制
{
if (st_size >= least && st_size <= huge)
if (is_modify && t - mtime <= day * 86400) //是否有时间限制
printf("%15ld \t %s\n", st_size, d);
else if (is_modify == 0)
printf("%15ld \t %s\n", st_size, d);
else
;
else
;
}
else if (is_least)
{
if (st_size >= least)
if (is_modify && t - mtime <= day * 86400)
printf("%15ld \t %s\n", st_size, d);
else if (is_modify == 0)
printf("%15ld \t %s\n", st_size, d);
else
;
else
;
}
else if (is_huge)
{
if (st_size <= huge)
if (is_modify && t - mtime <= day * 86400)
printf("%15ld \t %s\n", st_size, d);
else if (is_modify == 0)
printf("%15ld \t %s\n", st_size, d);
else
;
else
;
}
else
printf("%15ld \t %s\n", st_size, d);
}
void printdir(char *dir, int is_rec, int is_A, int is_least, int is_huge, int is_modify, long t)
{
struct dirent *entry; //文件信息结构体
DIR *dp; //类似于FILE,存放目录有关信息
struct stat statbuf; //文件信息
if ((dp = opendir(dir)) == NULL)
{
printf("Can't open directory %s\n", dir);
return;
}
else
{
while ((entry = readdir(dp)) != NULL)
{
if (entry->d_name[0] == '.' && is_A == 0)
continue;
char d[260]; //获取目录的绝对路径
strcpy(d, dir);
strcat(d, "/");
strcat(d, entry->d_name);
stat(d, &statbuf);
if (is_rec) //是否递归打印
if (S_ISDIR(statbuf.st_mode) && entry->d_name[strlen(entry->d_name) - 1] != '.')
strcpy(rec_fpath[rec_top++], d);
limit_print(d, statbuf.st_size, statbuf.st_mtime, is_least, is_huge, is_modify, t);
}
while (rec_id < rec_top)
{
if (!is_least && !is_huge)
printf("%s:\n", rec_fpath[rec_id]);
printdir(rec_fpath[rec_id++], is_rec, is_A, is_least, is_huge, is_modify, t);
}
}
closedir(dp);
return;
}
// 判断字符串是否全是数字
int is_digit(char *str)
{
return (strspn(str, "0123456789") == strlen(str));
}
int main(int argc, char *argv[])
{
time_t t = time(0);
struct stat st;
char *wd = ".";
char dest[50] = "";
int flag = 0; // 显式地终止命令选项分析
int is_rec = 0, is_A = 0, is_least = 0, is_huge = 0, is_modify = 0;
if (argc >= 2)
{
int i = 1;
while (i < argc) //处理多个目录以及各种参数
{
if (argv[i][0] == '-' && flag == 0)
{
if (argv[i][1] == 'r')
{
if(strlen(argv[i]) > 2)
{
printf("'-r' option error\n");
return 0;
}
is_rec = 1;
i++;
}
else if (argv[i][1] == 'a')
{
if (strlen(argv[i]) > 2)
{
printf("'-a' option error\n");
return 0;
}
is_A = 1;
i++;
}
else if (argv[i][1] == 'l')
{
is_least = 1;
if (strlen(argv[i]) > 2) // 处理"-l20"这类命令选项
{
strcpy(dest, &(argv[i][2]));
if (!is_digit(dest))
{
printf("'-l' option have an error arg\n");
return 0;
}
else
{
least = atoi(dest);
i += 1;
continue;
}
}
if (i + 1 == argc || !is_digit(argv[i + 1])) // 缺少参数或者参数出错
{
printf("'-l' option missing a arg or arg error\n");
return 0;
}
least = atoi(argv[i + 1]);
i += 2;
}
else if (argv[i][1] == 'h')
{
is_huge = 1;
if (strlen(argv[i]) > 2) // 处理"-h20"这类命令选项
{
strcpy(dest, &(argv[i][2]));
if (!is_digit(dest))
{
printf("'-h' option have an error arg\n");
return 0;
}
else
{
huge = atoi(dest);
i += 1;
continue;
}
}
if (i + 1 == argc || !is_digit(argv[i + 1])) // 缺少参数或者参数出错
{
printf("'-h' option missing a arg or arg error\n");
return 0;
}
huge = atoi(argv[i + 1]);
i += 2;
}
else if (argv[i][1] == 'm')
{
is_modify = 1;
if (strlen(argv[i]) > 2) // 处理"-l20"这类命令选项
{
strcpy(dest, &(argv[i][2]));
if (!is_digit(dest))
{
printf("'-m' option have an error arg\n");
return 0;
}
else
{
day = atoi(dest);
i += 1;
continue;
}
}
if (i + 1 == argc || !is_digit(argv[i + 1])) // 缺少参数或者参数出错
{
printf("'-m' option missing a arg or arg error\n");
return 0;
}
day = atoi(argv[i + 1]);
i += 2;
}
else if (argv[i][1] == '-')
{
if (strlen(argv[i]) > 2) // -----------------处理长选项
{
if(strcmp(argv[i], "--least") == 0) //-------------least
{
is_least = 1;
if (i + 1 == argc || !is_digit(argv[i + 1])) // 缺少参数或者参数出错
{
printf("'--least' option missing a arg or arg error\n");
return 0;
}
least = atoi(argv[i + 1]);
i += 2;
continue;
}
else if (strstr(argv[i], "--least=") != NULL) //-------------least=
{
is_least = 1;
strcpy(dest, &(argv[i][8]));
if (!is_digit(dest))
{
printf("'--least' option have an error arg\n");
return 0;
}
else
{
least = atoi(dest);
i += 1;
continue;
}
}
else if (strcmp(argv[i], "--huge") == 0) //--------------huge
{
is_huge = 1;
if (i + 1 == argc || !is_digit(argv[i + 1])) // 缺少参数或者参数出错
{
printf("'--huge' option missing a arg or arg error\n");
return 0;
}
huge = atoi(argv[i + 1]);
i += 2;
continue;
}
else if (strstr(argv[i], "--huge=") != NULL) //------------huge=
{
is_huge = 1;
strcpy(dest, &(argv[i][7]));
if (!is_digit(dest))
{
printf("'--huge' option have an error arg\n");
return 0;
}
else
{
huge = atoi(dest);
i += 1;
continue;
}
}
else if (strcmp(argv[i], "--modify") == 0) //--------------modify
{
is_modify = 1;
if (i + 1 == argc || !is_digit(argv[i + 1])) // 缺少参数或者参数出错
{
printf("'--modify' option missing a arg or arg error\n");
return 0;
}
day = atoi(argv[i + 1]);
i += 2;
continue;
}
else if (strstr(argv[i], "--modify=") != NULL) //---------modify=
{
is_modify = 1;
strcpy(dest, &(argv[i][9]));
if (!is_digit(dest))
{
printf("'--modify' option have an error arg\n");
return 0;
}
else
{
day = atoi(dest);
i += 1;
continue;
}
}
else if (strcmp(argv[i], "--recursive") == 0) //--------------recursive
{
is_rec = 1;
i ++;
continue;
}
else if (strcmp(argv[i], "--all") == 0) //--------------all
{
is_A = 1;
i ++;
continue;
}
else
{
printf("list: invalid option '%s'\n", argv[i]);
return 0;
}
}
flag = 1;
i++;
}
else
{
printf("list: invalid option '%s'\n", argv[i]);
return 0;
}
continue;
}
strcpy(user_fpath[user_top++], argv[i]); // 存储用户输入的所有路径
i++;
}
while (user_id < user_top) // 遍历用户输入的所有路径
{
wd = user_fpath[user_id];
stat(wd, &st);
if (!S_ISDIR(st.st_mode))
{
if (st.st_size == 0)
printf("Failed! %s not exist.\n", wd);
else
limit_print(argv[optind], st.st_size, st.st_mtime, is_least, is_huge, is_modify, t);
}
else
{
printf("%s:\n", wd);
printdir(wd, is_rec, is_A, is_least, is_huge, is_modify, t);
}
user_id ++;
}
if(strcmp(wd, ".") == 0 && user_top == 0) // 没有输入路径就默认.
{
printf("%s: \n", wd);
printdir(wd, is_rec, is_A, is_least, is_huge, is_modify, t);
}
}
else
printdir(wd, is_rec, is_A, is_least, is_huge, is_modify, t);
return 0;
}