This repository was archived by the owner on Oct 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmysys2.c
More file actions
72 lines (62 loc) · 1.61 KB
/
mysys2.c
File metadata and controls
72 lines (62 loc) · 1.61 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#define MAX_BUFFLEN 1024
#define MAX_NUM 100
int mysys(const char *cmdstring)
{
pid_t pid;
int status = -1;
if (cmdstring == NULL)
return 1;
if ((pid = fork()) < 0)
status = -1;
else if (pid == 0)
{
execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
exit(127);
}
else
{
while (waitpid(pid, &status, 0) < 0)
{
if (errno != EINTR)
{
status = -1;
break;
}
}
}
return status;
}
int main()
{
//char *argv[] = {"ls", "/", NULL};
//execvp("ls", argv);
int res;
res = mysys("");
printf("[Status] %d\n", res);
res = mysys("pwd");
printf("[Status] %d\n", res);
res = mysys("echo ,HELLO WORLD , sdfa sdfadf ss ");
printf("[Status] %d\n", res);
res = mysys("echo /G");
printf("[Status] %d\n", res);
res = mysys("echo ,,");
printf("[Status] %d\n", res);
res = mysys("echo");
printf("[Status] %d\n", res);
res = mysys("asdfasdf");
printf("[Status] %d\n", res);
printf("----------------------------------------------------------\n");
res = mysys("echo HELLO WORLD");
printf("[Status] %d\n", res);
printf("----------------------------------------------------------\n");
res = mysys("ls /");
printf("[Status] %d\n", res);
printf("----------------------------------------------------------\n");
return 0;
}