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