-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.c
203 lines (175 loc) · 5.03 KB
/
cli.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
#include <dirent.h>
#include <getopt.h>
#include "misc.h"
#include "cmd_mains.h"
int rm(const char *name, int vflag, int pflag, int rflag)
{
if (pflag && !prompt("rm: remove \'%s\'? [y/n] ", name))
return 0;
if (is_dir(name) && rflag) {
removedir(name, 1);
return 0;
}
if (unlink(name) == -1) {
error("can't delete file %s.", name);
}
if (vflag)
printf("removed '%s'\n", name);
return 0;
}
int ls(const char *name, int aflag, int iflag, int rflag)
{
DIR *dp;
struct dirent *ep;
static int count = 0, index = 1;
/* TODO: modify ouput to correctly align
* contents when i and r flags are set */
dp = opendir(name);
if (dp != NULL) {
while ((ep = readdir(dp)) != NULL) {
if (!aflag && ep->d_name[0] == '.')
continue;
if (ep->d_type == 4 && rflag)
ls(ep->d_name, aflag, iflag, rflag);
(iflag) ? printf("%2d: %s", index++, ep->d_name) : printf("%10s", ep->d_name);
if (count == 4) {
putchar('\n');
count = 0;
} else {
putchar('\t');
count++;
}
}
putchar('\n');
} else {
error("can't open directory %s.", name);
}
if (closedir(dp) == -1)
error("can't close directory %s.", name);
return 0;
}
int clear(void)
{
int i;
for (i = 0; i < SCREEN_HEIGHT; ++i)
putchar('\n');
return 0;
}
int makedir(const char *name, int vflag)
{
/* create dir with read/write priveleges */
if (mkdir(name, S_IRWXU) == -1)
error("can't make directory %s.", name);
if (vflag)
printf("created directory '%s'\n", name);
return 0;
}
int cat(const char *name, int nflag)
{
int c;
int count = 1;
FILE *fp;
if (is_dir(name)) {
fprintf(stderr, "error: '%s' is a directory\n", name);
return 1;
}
fp = fopen(name, "r");
if (fp == NULL)
error("can't open file %s.", name);
if (nflag)
printf("\t%d: ", count++);
/* unbuffered input is used because it makes it easier to detect
* newlines when the n flag is set. */
while ((c = getc(fp)) != EOF) {
putchar(c);
if (c == '\n' && nflag)
printf("\t%d: ", count++);
}
if (c != '\n')
putchar('\n');
fclose(fp);
return 0;
}
int pwd(void)
{
char buf[BUFSIZE];
getcwd(buf, BUFSIZE);
if (buf == NULL)
error("can't get cwd.");
puts(buf);
return 0;
}
int cd(const char *path)
{
if (chdir(path) == -1)
error("can't change to directory %s.", path);
return 0;
}
int removedir(const char *name, int rflag)
{
DIR *dp;
struct dirent *ep;
char cwd[NAME_MAX];
if (rflag) {
getcwd(cwd, NAME_MAX);
dp = opendir(name);
if (dp != NULL) {
cd(name);
while ((ep = readdir(dp)) != NULL) {
/* deleting the '.' or ".." directory does not
* work so they are skipped. Also. they don't
* have to be deleted for rmdir() to work */
if (strcmp(ep->d_name, ".") == 0 ||
strcmp(ep->d_name, "..") == 0)
continue;
if (is_dir(ep->d_name))
removedir(ep->d_name, 1);
else
rm(ep->d_name, 0, 0, 0);
}
} else
error("can't open directory %s.", name);
if (closedir(dp) == -1)
error("can't close directory %s.", name);
if (chdir(cwd) != -1) {
if (rmdir(name) == -1)
error("can't delete directory %s.", name);
} else
error("can't change to directory %s.", cwd);
} else {
if (rmdir(name) == -1)
error("can't delete directory %s.", name);
}
return 0;
}
int main(void)
{
char cwd[BUFSIZE], buf[BUFSIZE];
do {
getcwd(cwd, BUFSIZE);
printf("cli:%s%% ", (cwd == NULL) ? "" : cwd);
fgets(buf, BUFSIZE, stdin);
if (strlen(buf) == 0)
continue;
if (strncmp(buf, "rm", 2) == 0 && !isalnum(buf[2]))
rm_main(buf);
else if (strncmp(buf, "ls ", 2) == 0 && !isalnum(buf[2]))
ls_main(buf);
else if (strncmp(buf, "clear", 5) == 0 && !isalnum(buf[5]))
clear();
else if (strncmp(buf, "mkdir", 5) == 0 && !isalnum(buf[5]))
mkdir_main(buf);
else if (strncmp(buf, "cat", 3) == 0 && !isalnum(buf[3]))
cat_main(buf);
else if (strncmp(buf, "pwd", 3) == 0 && !isalnum(buf[3]))
pwd();
else if (strncmp(buf, "cd", 2) == 0 && !isalnum(buf[2]))
cd_main(buf);
else if (strncmp(buf, "help", 4) == 0 && !isalnum(buf[4]))
usage();
else
printf("cli: not a valid command.\n"
"type 'help' for a list of options\n\n");
} while (strcmp(buf, "exit\n") != 0);
return 0;
}