-
Notifications
You must be signed in to change notification settings - Fork 0
/
unix.c
85 lines (72 loc) · 2.03 KB
/
unix.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
#include <unistd.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#define BOOL int
#define FALSE 0
#define TRUE 1
#define PREPARE() pid_t pif[argc-1]; int si[argc-1];
#define RUN() Run(argv[i], &pif[i-1], null_output, own_directory)
#define WAIT_AND_READ() Wait(&pif[i-1], &si[i-1], Num(i, parallel, null_output, own_directory))
#define CORRECT() TRUE
#define UNIX_KILL() kill(getpid(), SIGKILL)
int FindNextParam(char* command) {
BOOL dcommas = FALSE;
int i;
for (i = 0; (dcommas || command[i] != ' ') && command[i] != '\0'; i++)
if (command[i] == '"')
dcommas ^= TRUE;
if (command[i] != '\0')
command[i++] = '\0';
return i;
}
BOOL Run(char* command, pid_t *pif, int null_output, int own_directory) {
*pif = fork();
if (*pif == -1)
return FALSE;
if (*pif == 0) {
#define LIMIT 256
char* first_parameter = command;
char** parameters = malloc(LIMIT*sizeof(char*));
int i;
for (i = 0; i < LIMIT; i++) {
if (command[0] == '\0')
break;
else {
parameters[i] = malloc(LIMIT*sizeof(char));
int next = FindNextParam(command);
strcpy(parameters[i], command);
command += next;
}
}
parameters[i] = NULL;
if (null_output)
dup2(open("/dev/null", 0), 1);
if (own_directory) {
char* dir = strdup(first_parameter);
chdir(dirname(dir));
free(dir);
}
execv(first_parameter, parameters);
for (; i >= 0; i--)
free(parameters[i]);
free(parameters);
return FALSE;
}
return TRUE;
}
BOOL Wait(pid_t *pif, int* si, int i) {
struct rusage ru;
if(wait4(*pif, si, 0, &ru) == 0)
return FALSE;
if (!WIFSIGNALED(*si) || WTERMSIG(*si) != SIGKILL)
return Read(pif, i, &ru);
return FALSE;
}
BOOL Read(pid_t *pif, int i, struct rusage *ru) {
int memory = ru->ru_maxrss;
printf(PEAK_PRINT, i, memory, memory << 10);
return TRUE;
}