-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
87 lines (83 loc) · 2.37 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
/* This file serves as a testbench for your library. You do not
* need to modify it.
*/
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include "libtree.h"
int
main(int argc, char *argv[])
{
struct tree_options opts = {.indent = 2, .sort=ALPHA};
char const *optstring = "+adpugsrtUhi:";
for (char c; (c = getopt(argc, argv, optstring)) != -1;) {
switch (c) {
case 'a':
opts.all = true;
break;
case 'd':
opts.dirsonly = true;
break;
case 'p':
opts.perms = true;
break;
case 'u':
opts.user = true;
break;
case 'g':
opts.group = true;
break;
case 's':
opts.size = true;
break;
case 'r':
opts.sort = RALPHA;
break;
case 't':
opts.sort = TIME;
break;
case 'U':
opts.sort = NONE;
break;
case 'i': {
char *end = optarg;
long int i = strtol(optarg, &end, 10);
if (*optarg != '\0' && *end == '\0')
opts.indent = i;
else
err(errno = EINVAL, "%s", optarg);
break;
}
case 'h':
case '?':
fprintf(stderr, "Usage: %s [-adpugsrtUh] [path...]\n", argv[0]);
exit(1);
}
}
#ifdef DEBUG
#define boolstr(b) (b ? "true" : "false")
fprintf(stderr,
"opts = {\n"
" .all = %5s, /* print hidden '.' files */\n"
" .dirsonly = %5s, /* list directories only */\n"
" .perms = %5s, /* print file type and permissions */ \n"
" .user = %5s, /* print the username of the file */\n"
" .group = %5s, /* print the group name of file */\n"
" .size = %5s, /* print file size in bytes */\n"
" .sort = %5s, /* sorting method to use */\n"
" .indent = %5d, /* indent size */"
"};\n",
boolstr(opts.all), boolstr(opts.dirsonly), boolstr(opts.perms), boolstr(opts.user),
boolstr(opts.group), boolstr(opts.size),
(char *[]){"NONE", "ALPHA", "RALPHA", "TIME"}[opts.sort], opts.indent);
#endif
if (optind < argc) {
for (int i = optind; i < argc; ++i) {
if (tree_print(argv[i], opts) == -1) err(errno, "printing tree for %s", argv[i]);
}
} else {
if (tree_print("./", opts) == -1) err(errno, "printing tree for ./");
}
}