/
command.c
264 lines (191 loc) · 5.76 KB
/
command.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
/* fork.c - example of a fork in a program */
/* The program asks for UNIX commands to be typed and inputted to a string */
/* The string is then "parsed" by locating blanks etc. */
/* Each command and sorresponding arguments are put in a args array */
/* execvp is called to execute these commands in child process */
/* spawned by fork() */
/* cc -o fork fork.c */
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
char *getusername() {
static struct passwd *ent;
static int cur_uid=-1;
if (getuid()==cur_uid) {
return ent->pw_name;
}
if (cur_uid==-1) cur_uid=getuid();
setpwent();
ent=getpwuid(cur_uid);
endpwent();
if (!ent) return "(null)";
return ent->pw_name;
}
char *getuserdir() {
static struct passwd *ent;
static int cur_uid=-1;
if (getuid()==cur_uid) {
return ent->pw_dir;
//return "~";
}
if (cur_uid==-1) cur_uid=getuid();
setpwent();
ent=getpwuid(cur_uid);
endpwent();
if (!ent) {
// fprintf (stdout, "whoo\n");
return "/"; // a std dir
}
return ent->pw_dir;
// return "~";
}
char *gethost() {
static char nome[100];
if (gethostname (nome, 12)) return "(null)";
return nome;
}
void init_banner() {
fprintf (stdout,"Tosco Shell v 0.00001 - _extended_\n\n");
}
void main () {
char buf[1024];
char *args[64];
char pwd[128];
init_banner();
for (;;)
{
/*
* Prompt for and read a command.
*/
// user@host[pwd] :
printf ("%s@%s[%s]: ", getusername() , gethost(), getcwd(pwd, sizeof(pwd)) );
if (gets (buf) == NULL)
{
printf ("\n");
exit (0);
}
/*
* Split the string into arguments.
*/
parse (buf, args);
/*
* Execute the command.
*/
execute (args);
}
}
/*
* parse--split the command in buf into
* individual arguments.
*/
parse (buf, args)
char *buf;
char **args;
{
while (*buf != (long) NULL)
{
/*
* Strip whitespace. Use nulls, so
* that the previous argument is terminated
* automatically.
*/
while ((*buf == ' ') || (*buf == '\t')) *buf++ = (long) NULL;
/*
* Save the argument.
*/
*args++ = buf;
/*
* Skip over the argument.
*/
while ((*buf != (long) NULL) && (*buf != ' ') && (*buf != '\t')) buf++;
}
*args = NULL;
}
/*
* execute--spawn a child process and execute
* the program.
*/
execute (args)
char **args;
{
int pid, status;
/*
* check exit.
*/
// fprintf (stdout,"Debug: %s - %s %s\n", *args, args[0], args[1]);
if ( *args==NULL ) return;
// echo
if ( strncmp (args[0], "echo" , 4) == 0) {
fprintf (stdout, "%s\n", args[1]);
return;
}
// exit
if ( strncmp (args[0], "exit" , 4) == 0) {
fprintf (stdout, "Quit. \n");
exit ();
}
// chdir
if ( strncmp (args[0], "cd", 2) == 0 ) {
if (!args[1]) args[1]=(char *) getuserdir();
chdir (args[1]);
return;
}
// setenv e getenv
if ( strncmp (args[0], "setenv", 6) == 0 ) {
if (strsep (args[1], "=") !=0 ) {
fprintf (stdout, "No = allowed punx\n");
return;
}
if ( strncmp (args[2], "=", 1)==0) {
fprintf (stdout, "Warning, the use of = is deprecated !\n");
fprintf (stdout, "%s=%s\n",args[1], args[3]);
setenv (args[1], args[3], 1);
return;
}
fprintf (stdout, "%s=%s\n",args[1], args[2]);
setenv (args[1], args[2], 1);
return;
}
if ( strncmp (args[0], "getenv", 6) == 0 ) {
fprintf (stdout, "%s=%s\n", args[1], getenv(args[1]));
return ;
}
/*
* Get a child process.
*/
if ((pid = fork ()) < 0)
{
perror ("fork");
exit (1);
/* NOTE: perror() produces a short error message on the standard
error describing the last error encountered during a call to
a system or library function.
*/
}
/*
* The child executes the code inside the if.
*/
if (pid == 0)
{
execvp (*args, args);
perror (*args);
exit (1);
/* NOTE: The execv() vnd execvp versions of execl() are useful when the
number of arguments is unknown in advance;
The arguments to execv() and execvp() are the name
of the file to be executed and a vector of strings contain-
ing the arguments. The last argument string must be fol-
lowed by a 0 pointer.
execlp() and execvp() are called with the same arguments as
execl() and execv(), but duplicate the shell's actions in
searching for an executable file in a list of directories.
The directory list is obtained from the environment.
*/
}
/*
* The parent executes the wait.
*/
while (wait (&status) != pid)
/* empty */ ;
}