-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshell.c
47 lines (41 loc) · 847 Bytes
/
shell.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
/*
shell emulation (currently incomplete)
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#define TRUE 1
#define FALSE !TRUE
#define MAXLEN 100
int
main (void)
{
char s[MAXLEN];
while (TRUE)
{
printf ("%s ", "$");
fgets (s, MAXLEN, stdin);
/* printf ("%s", s); */
pid_t proc; /* process id */
int *status; /* waitpid status */
if ((proc = fork ()) != 0)
{
/* printf ("%s %u\n", "created process", proc); */
waitpid (-1, status, 0);
}
else
{
/* save the command and the arguments */
/* TODO: use getopts for the arguments */
char cmd[MAXLEN];
if (sscanf (s, "%s", cmd) > 0)
{
/* printf ("%s\n", cmd); */
char *const parm[] = { cmd, NULL };
execve (cmd, parm, 0);
}
}
}
return 0;
}