-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
551 lines (507 loc) · 14.5 KB
/
main.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#include <assert.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define USERBUFSIZE 128
#define GROUPBUFSIZE 128
#define TIMEBUFSIZE 128
#define WRITEEND 1
#define READEND 0
#define BUFSIZE 128
static int err_code;
static int n_count = 0;
static int if_n = false;
void handle_error(char *fullname, char *action);
bool test_file(char *pathandname);
bool is_dir(char *pathandname);
const char *ftype_to_str(mode_t mode);
void list_file(char *pathandname, char *name, bool list_long, bool if_humanreadable);
void list_dir(char *dirname, bool list_long, bool list_all, bool recursive, bool if_humanreadable);
void hack()
{
int status;
// pipe from child to parent
int c2p[2];
pipe(c2p);
// child
if (fork() == 0)
{
// so instead of writing to terminal, it will write to the pipe.
close(1);
dup(c2p[WRITEEND]);
close(c2p[READEND]);
char *lala[] = {"/usr/bin/ls", NULL, NULL};
execve("/usr/bin/ls", lala, 0);
printf("exec failed!\n");
exit(1);
}
// parent
else
{
close(c2p[WRITEEND]);
char buf[BUFSIZE];
int n = read(c2p[READEND], buf, sizeof(buf));
while (n)
{
write(1, buf, n);
n = read(c2p[READEND], buf, sizeof(buf));
}
wait(&status);
}
exit(0);
}
#define NOT_YET_IMPLEMENTED(msg) \
do \
{ \
printf("Not yet implemented: " msg "\n"); \
exit(255); \
} while (0)
#define PRINT_ERROR(progname, what_happened, pathandname) \
do \
{ \
printf("%s: %s %s: %s\n", progname, what_happened, pathandname, \
strerror(errno)); \
} while (0)
/* PRINT_PERM_CHAR:
*/
#define PRINT_PERM_CHAR(mode, mask, ch) printf("%s", (mode & mask) ? ch : "-");
/*
* Get username for uid. Return 1 on failure, 0 otherwise.
*/
static int uname_for_uid(uid_t uid, char *buf, size_t buflen)
{
struct passwd *p = getpwuid(uid);
if (p == NULL)
{
handle_error(NULL, NULL);
return 1;
}
strncpy(buf, p->pw_name, buflen);
return 0;
}
/*
* Get group name for gid. Return 1 on failure, 0 otherwise.
*/
static int group_for_gid(gid_t gid, char *buf, size_t buflen)
{
struct group *g = getgrgid(gid);
if (g == NULL)
{
return 1;
}
strncpy(buf, g->gr_name, buflen);
return 0;
}
/*
* Format the supplied `struct timespec` in `ts` (e.g., from `stat.st_mtime`) as a
* string in `char *out`. Returns the length of the formatted string (see, `man
* 3 strftime`).
*/
static size_t date_string(struct timespec *ts, char *out, size_t len)
{
struct timespec now;
timespec_get(&now, TIME_UTC);
struct tm *t = localtime(&ts->tv_sec);
if (now.tv_sec < ts->tv_sec)
{
// Future time, treat with care.
return strftime(out, len, "%b %e %Y", t);
}
else
{
time_t difference = now.tv_sec - ts->tv_sec;
if (difference < 31556952ull)
{
return strftime(out, len, "%b %e %H:%M", t);
}
else
{
return strftime(out, len, "%b %e %Y", t);
}
}
}
/*
* Print help message and exit.
*/
static void help()
{
// TODO: add to this
printf("ls: List files\nUsage: ls [OPTION]... [FILE]...\n");
printf("-a\tlist all files, including files starting with . and the pseudo-files . and ... \n");
printf("-l\tuse a long listing format \n");
printf("-n\tsuppresses the listing of files and instead produces a count of the number of files that would be printed if they were listed.\n");
printf("-R\trecursively lists files in subdirectories\n");
printf("-h\tprints “human readable”. With -l and -s, print sizes like 1K 234M 2G etc.\n");
printf("--hack\tinvoke the system-supplied ls\n");
printf("--help\tdisplay this help and exit\n");
exit(0);
}
/*
* call this when there's been an error.
*/
void handle_error(char *what_happened, char *fullname)
{
// ignore the case when NULl is passed into the function.
if (what_happened)
PRINT_ERROR("ls", what_happened, fullname);
// TODO: your code here: inspect errno and set err_code accordingly.
/**
* bit
* 3: command-line was not found.
* 4: denied access to a file or directory
* 5: another type of error happened group_id function
* 6: encounter a erronor
* */
err_code |= 64;
switch (errno)
{
// not found
case ENOENT:
err_code |= 8;
break;
// denied
case EACCES:
err_code |= 16;
break;
// other error uname_for_uid error
default:
err_code |= 32;
break;
}
return;
}
/*
* test_file():
* Use this to test for whether a file or dir exists
*/
bool test_file(char *pathandname)
{
struct stat sb;
if (stat(pathandname, &sb))
{
handle_error("cannot access", pathandname);
return false;
}
return true;
}
/*
* is_dir(): tests whether the argument refers to a directory.
*/
bool is_dir(char *pathandname)
{
/* TODO: fillin */
struct stat sb;
stat(pathandname, &sb);
return (sb.st_mode & S_IFMT) == S_IFDIR;
}
/* convert the mode field in a struct stat to a file type, for -l printing */
// order: permission links gid uid size modifiedtime name
const char *ftype_to_str(mode_t mode)
{
const mode_t ori_mode = mode;
char *permissionstr = malloc(sizeof(char) * 11);
if (S_ISREG(mode))
{
permissionstr[0] = '-';
}
else if (S_ISDIR(mode))
{
permissionstr[0] = 'd';
}
else
{
permissionstr[0] = '?';
}
mode = ori_mode & S_IRWXU;
permissionstr[1] = mode & S_IRUSR ? 'r' : '-';
permissionstr[2] = mode & S_IWUSR ? 'w' : '-';
permissionstr[3] = mode & S_IXUSR ? 'x' : '-';
mode = ori_mode & S_IRWXG;
permissionstr[4] = mode & S_IRGRP ? 'r' : '-';
permissionstr[5] = mode & S_IWGRP ? 'w' : '-';
permissionstr[6] = mode & S_IXGRP ? 'x' : '-';
mode = ori_mode & S_IRWXO;
permissionstr[7] = mode & S_IROTH ? 'r' : '-';
permissionstr[8] = mode & S_IWOTH ? 'w' : '-';
permissionstr[9] = mode & S_IXOTH ? 'x' : '-';
permissionstr[10] = '\0';
return permissionstr;
}
void human_readable(long int bytes, char *buf)
{
memset(buf, '\0', 6);
if (bytes < 1024)
sprintf(buf, "%ld", bytes);
else if (bytes < 1024 * 1024)
sprintf(buf, "%.1lfK", (double)bytes / 1024);
else if (bytes < 1024 * 1024 * 1024)
sprintf(buf, "%.1lfM", (double)bytes / (1024 * 1024));
else
sprintf(buf, "%.1lfG", (double)bytes / (1024 * 1024 * 1024));
}
// get extension name of a file.
char *extname(char *filename)
{
char *dotptr;
for (dotptr = filename + strlen(filename) - 1; *dotptr != '.' || dotptr == filename; dotptr--)
if (dotptr == filename)
return "";
return dotptr + 1;
}
void list_file_long(char *pathandname, char *name, int if_humanreadable)
{
struct timespec ts;
struct stat sb;
mode_t mode;
char username[USERBUFSIZE];
char groupname[GROUPBUFSIZE];
char timebuf[TIMEBUFSIZE];
if (stat(pathandname, &sb) == -1)
{
handle_error("cannot get the stat of", pathandname);
return;
}
mode = sb.st_mode;
const char *permissionstr = ftype_to_str(mode);
printf("%4s %4ld", permissionstr, sb.st_nlink);
// if we cannot get username successfully, handle error and print its id instead.
if (uname_for_uid(sb.st_uid, username, USERBUFSIZE))
{
handle_error(NULL, pathandname);
printf(" %5d", sb.st_uid);
}
else
{
printf(" %5s", username);
}
if (group_for_gid(sb.st_gid, groupname, GROUPBUFSIZE))
{
handle_error(NULL, pathandname);
printf(" %5d", sb.st_gid);
}
else
{
printf(" %5s", groupname);
}
if (if_humanreadable)
{
char buf[6];
human_readable(sb.st_size, buf);
printf(" %7s", buf);
}
else
{
printf(" %7ld", sb.st_size);
}
time_t t = sb.st_mtime;
ts.tv_sec = t;
date_string(&ts, timebuf, TIMEBUFSIZE);
printf(" %7s", timebuf);
// free permission str
// check if the filename ends with a .link sign
if (strcmp(extname(name), "link") == 0)
{
int bufsiz = sb.st_size + 1;
char *strbuf = malloc(bufsiz);
// if reading link failure is met, handle error as other error and only print the name.
int nbytes = readlink(name, strbuf, bufsiz);
if (nbytes == -1)
{
handle_error(NULL, name);
printf(" %-4s\n", name);
}
else
{
printf(" %s -> %.*s\n", name, (int)nbytes, strbuf);
}
free((void *)strbuf);
}
else
{
printf(" %-4s\n", name);
}
free((void *)permissionstr);
}
/* list_file():
* implement the logic for listing a single file.
*/
void list_file(char *pathandname, char *name, bool list_long, bool if_humanreadable)
{
// ignore if n is specified
if (if_n)
{
n_count++;
return;
}
// if dir but not "." and "..", add "/" to its name;
if (is_dir(pathandname) && !(strcmp("..", name) == 0 || strcmp(".", name) == 0))
{
strcat(name, "/");
}
// if list long, print long;
if (list_long)
{
list_file_long(pathandname, name, if_humanreadable);
}
else
{
printf("%s\n", name);
}
}
/* list_dir():
* implement the logic for listing a directory.
*/
void list_dir(char *dirname, bool list_long, bool list_all, bool recursive, bool if_humanreadable)
{
if (recursive && !if_n)
{
printf("%s:\n", dirname);
}
char *pathandname;
char *pathnameArr[100];
int pathIndex = 0;
DIR *dirp;
struct dirent *dp;
// if we cannot open, then return
if ((dirp = opendir(dirname)) == NULL)
{
handle_error("cannot open", dirname);
return;
}
while ((dp = readdir(dirp)) != NULL)
{
// does not list if -a flag is not specified
if (!list_all && (dp->d_name[0] == '.' || strcmp(".", dp->d_name) == 0 || strcmp("..", dp->d_name) == 0))
{
continue;
}
pathandname = (char *)malloc(sizeof(dirname) + 3 + sizeof(dp->d_name));
strcpy(pathandname, dirname);
strcat(pathandname, "/");
strcat(pathandname, dp->d_name);
// if dp is directory & recursive, omit ".." and "." , append before list it and list call list_dir again
if (recursive && is_dir(pathandname) && !(strcmp(".", dp->d_name) == 0 || strcmp("..", dp->d_name) == 0))
{
list_file(pathandname, dp->d_name, list_long, if_humanreadable);
pathnameArr[pathIndex++] = pathandname;
}
// if dp is directory, append before list it.
else if (is_dir(pathandname))
{
strcat(pathandname, "/");
list_file(pathandname, dp->d_name, list_long, if_humanreadable);
free(pathandname);
}
// if dp is file, just list it.
else
{
list_file(pathandname, dp->d_name, list_long, if_humanreadable);
free(pathandname);
}
}
// prints out the listing subdirectories.
for (int i = 0; i < pathIndex; i++)
{
list_dir(pathnameArr[i], list_long, list_all, recursive, if_humanreadable);
free(pathnameArr[i]);
}
closedir(dirp);
return;
}
int main(int argc, char *argv[])
{
// This needs to be int since C does not specify whether char is signed or
// unsigned.
int opt;
err_code = 0;
bool if_humanreadable = false, list_long = false, list_all = false, list_recursive = false;
// We make use of getopt_long for argument parsing, and this
// (single-element) array is used as input to that function. The `struct
// option` helps us parse arguments of the form `--FOO`. Refer to `man 3
// getopt_long` for more information.
struct option opts[] = {
{.name = "help", .has_arg = no_argument, .flag = NULL, .val = '\a'},
{.name = "hack", .has_arg = no_argument, .flag = NULL, .val = '\b'},
};
// This loop is used for argument parsing. Refer to `man 3 getopt_long` to
// better understand what is going on here.
while ((opt = getopt_long(argc, argv, "1alRnh", opts, NULL)) != -1)
{
switch (opt)
{
case '\a':
// Handle the case that the user passed in `--help`. (In the
// long argument array above, we used '\a' to indicate this
// case.)
help();
break;
case '\b':
hack();
break;
case '1':
// Safe to ignore since this is default behavior for our version
// of ls.
break;
case 'a':
list_all = true;
break;
case 'l':
list_long = true;
break;
case 'R':
list_recursive = true;
break;
case 'n':
if_n = true;
break;
case 'h':
if_humanreadable = true;
break;
default:
printf("Unimplemented flag %d\n", opt);
break;
}
}
// If optind is smaller than argc there is still arguments, see if it is file or directories, and run those arguments with flags
if (optind < argc)
{
for (int i = optind; i < argc; i++)
{
char *name = argv[i];
if (!test_file(name))
continue;
if (is_dir(name))
{
// append path into dirname
list_dir(name, list_long, list_all, list_recursive, if_humanreadable);
}
else
{
list_file(name, name, list_long, if_humanreadable);
}
}
}
// else, we run current directories with flags.
else
{
list_dir(".", list_long, list_all, list_recursive, if_humanreadable);
}
// if if_n is turned on, display the count;
if (if_n)
{
printf("%d\n", n_count);
}
exit(err_code);
}