forked from michaelrsweet/epm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.c
172 lines (139 loc) · 3.69 KB
/
run.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* External program function for the ESP Package Manager (EPM).
*
* Copyright 1999-2014 by Michael R Sweet
* Copyright 1999-2005 by Easy Software Products.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/*
* Include necessary headers...
*/
#include "epm.h"
#include <stdarg.h>
#include <fcntl.h>
#include <sys/wait.h>
/*
* 'run_command()' - Run an external program.
*/
int /* O - Exit status */
run_command(const char *directory, /* I - Directory for command or NULL */
const char *command, /* I - Command string */
...) /* I - Additional arguments as needed */
{
va_list ap; /* Argument pointer */
int pid, /* Child process ID */
status, /* Status of child */
argc; /* Number of arguments */
char argbuf[10240], /* Argument buffer */
*argptr, /* Argument string pointer */
*argv[100]; /* Argument strings */
/*
* Format the command string...
*/
va_start(ap, command);
vsnprintf(argbuf, sizeof(argbuf) - 1, command, ap);
argbuf[sizeof(argbuf) - 1] = '\0';
if (Verbosity > 1)
puts(argbuf);
/*
* Parse the argument string; arguments can be separated by whitespace
* and quoted by " and '...
*/
argv[0] = argbuf;
for (argptr = argbuf, argc = 1; *argptr != '\0' && argc < 99; argptr ++)
if (isspace(*argptr & 255))
{
*argptr++ = '\0';
while (isspace(*argptr & 255))
argptr ++;
if (*argptr != '\0')
{
argv[argc] = argptr;
argc ++;
}
argptr --;
}
else if (*argptr == '\'')
{
if (argptr == argv[argc - 1])
argv[argc - 1] ++;
for (argptr ++; *argptr && *argptr != '\''; argptr ++)
if (*argptr == '\\' && argptr[1])
memmove(argptr, argptr + 1, strlen(argptr));
if (*argptr == '\'')
memmove(argptr, argptr + 1, strlen(argptr));
argptr --;
}
else if (*argptr == '\"')
{
if (argptr == argv[argc - 1])
argv[argc - 1] ++;
for (argptr ++; *argptr && *argptr != '\"'; argptr ++)
if (*argptr == '\\' && argptr[1])
memmove(argptr, argptr + 1, strlen(argptr));
if (*argptr == '\"')
memmove(argptr, argptr + 1, strlen(argptr));
argptr --;
}
argv[argc] = NULL;
/*
* Execute the command...
*/
if ((pid = fork()) == 0)
{
/*
* Child comes here... Redirect stdin, stdout, and stderr to /dev/null
* if !Verbosity...
*/
if (Verbosity < 2)
{
close(0);
close(1);
close(2);
open("/dev/null", O_RDWR);
dup(0);
dup(0);
}
/*
* Change directories...
*/
if (directory)
chdir(directory);
/*
* Execute the program; if an error occurs, exit with the UNIX error...
*/
execvp(argv[0], argv);
fprintf(stderr, "epm: Unable to execute \"%s\" program: %s\n", argv[0],
strerror(errno));
exit(errno);
}
else if (pid < 0)
{
/*
* Error - can't fork!
*/
perror("epm: fork failed");
return (1);
}
/*
* Fork successful - wait for the child and return the error status...
*/
if (wait(&status) != pid)
{
fputs("epm: Got exit status from wrong program!\n", stderr);
return (1);
}
else if (WIFSIGNALED(status))
return (-WTERMSIG(status));
else
return (WEXITSTATUS(status));
}