-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsh.c
64 lines (57 loc) · 1.3 KB
/
psh.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define MAXARGS 20
#define ARGLEN 100
char *makestring(char*);
void execute(char*[]);
int main()
{
char *arglist[MAXARGS + 1];
int numargs;
char argbuf[ARGLEN];
numargs = 0;
while(numargs < MAXARGS){
printf("Arg[%d]", numargs);
if(fgets(argbuf, ARGLEN, stdin) && *argbuf != '\n'){
arglist[numargs++] = makestring(argbuf);
} else{
if(numargs > 0){
arglist[numargs] = NULL;
execute(arglist);
numargs = 0;
}
}
}
}
void execute(char *arglist[])
{
int pid, existstatus;
pid = fork();
switch( pid ){
case -1:
perror("fork failed!");
exit(1);
case 0:
execvp(arglist[0], arglist);
perror("execvp failed");
exit(1);
default:
wait(&existstatus);
printf("child exited with status %d, %d\n", existstatus >> 8,
existstatus & 0377);
}
}
char *makestring(char *buf)
{
char *cp = NULL;
buf[strlen(buf) - 1] = '\0';
cp = (char*) malloc(strlen(buf) + 1);
if(cp == NULL){
fprintf(stderr, "no memory\n");
exit(1);
}
strcpy(cp, buf);
return cp;
}