-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths.c
More file actions
180 lines (153 loc) · 2.79 KB
/
s.c
File metadata and controls
180 lines (153 loc) · 2.79 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define WDLEN 24
#define LINE 256
#define MAXARGS 5
typedef struct Cmd Cmd;
struct Cmd
{
char *name;
void (*func)(int, char **);
};
void printlst(char**);
int getln(FILE* , char*, int);
char **getlst2(char*, int, int);
char **getlst3(char*, int, int);
int isspc(int);
int gtargc(char**);
void exec(char**);
void freelist(char**, int);
void cecho(int, char**);
void ccd(int, char**);
void cexit(int, char**);
void cpwd(int, char**);
Cmd cmd[] = {
{ "echo", cecho },
{ "cd", ccd },
{ "exit", cexit },
{ "pwd", cpwd },
{ 0 }
};
int main()
{
char line[LINE], **list;
FILE *f;
f = fdopen(0, "r");
while (getln(f, line, LINE) > 0) {
// printf("line:\t%s", line);
list = getlst3(line, MAXARGS, WDLEN);
// printlst(list);
exec(list);
freelist(list, MAXARGS);
}
return 0;
}
int gtargc(char **args)
{
int c;
c = 0;
while (args[c])
c++;
return c;
}
void cpwd(int argc, char **args)
{
char p[64];
getcwd(p, 64);
printf("%s\n", p);
}
void cecho(int argc, char **args)
{
puts("define: echo");
}
void ccd(int argc, char **args)
{
if (chdir(args[1]) == 0)
cpwd(0, 0);
else
printf("could not cd\n");
}
void cexit(int argc, char **args)
{
puts("define: exit");
}
void exec(char **list)
{
Cmd *cp;
for (cp = cmd; cp->name; cp++)
if (strcmp(list[0], cp->name) == 0) {
(*cp->func)(gtargc(list), list);
return;
}
switch (fork()) {
case -1:
puts("err fork");
exit(0);
case 0:
execv(list[0], list);
puts("err execv");
exit(0);
}
}
void printlst(char **list)
{
int i;
i = 0;
if (list[i] == 0) {
puts("empty list...");
return;
}
for ( ; list[i]; i++)
printf("token: %s\n", list[i]);
}
int getln(FILE *f, char *b, int lim)
{
int n, c;
n = 0;
while ((c = getc(f)) > 0 && n < lim && c != '\n') {
b[n++] = c;
}
// if (c && n < lim)
// b[n++] = '\n';
b[n] = 0;
return n;
}
int isspc(int c)
{
return (c == '\t'|| c == ' ') ? 1 : 0;
}
char **getlst3(char *b, int maxargs, int wdlen)
{
char **list, *tok;
int n;
list = malloc(maxargs * sizeof(char*));
for (n = 0; n < maxargs; n++)
list[n] = malloc(wdlen * sizeof(char));
tok = strtok(b, " ");
n = 0;
while (tok) {
list[n++] = strndup(tok, wdlen - 1);
tok = strtok(0, " ");
}
list[n] = 0;
return list;
}
char **getlst2(char *b, int maxargs, int wdlen)
{
static char *list[32];
char *tok;
int n;
tok = strtok(b, " ");
for (n = 0; tok; n++, tok = strtok(0, " "))
list[n] = strndup(tok, wdlen);
return list;
}
void freelist(char **list, int maxargs)
{
int i;
for (i = 0; i < maxargs; i++)
free(list[i]);
free(list);
}