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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include "config.h"
#if HAVE_STRING_H
# include <string.h>
#endif
#include <stdlib.h>
#include <ctype.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/types.h>
#include "misc.h"
#include "spec.h"
#include "rpmlib.h"
#include "header.h"
#include "popt/popt.h"
void addOrAppendListEntry(Header h, int_32 tag, char *line)
{
int argc;
char **argv;
poptParseArgvString(line, &argc, &argv);
if (argc) {
headerAddOrAppendEntry(h, tag, RPM_STRING_ARRAY_TYPE, argv, argc);
}
FREE(argv);
}
/* Parse a simple part line that only take -n <pkg> or <pkg> */
/* <pkg> is return in name as a pointer into a static buffer */
int parseSimplePart(char *line, char **name, int *flag)
{
char *tok;
char linebuf[BUFSIZ];
static char buf[BUFSIZ];
strcpy(linebuf, line);
/* Throw away the first token (the %xxxx) */
strtok(linebuf, " \t\n");
if (!(tok = strtok(NULL, " \t\n"))) {
*name = NULL;
return 0;
}
if (!strcmp(tok, "-n")) {
if (!(tok = strtok(NULL, " \t\n"))) {
return 1;
}
*flag = PART_NAME;
} else {
*flag = PART_SUBNAME;
}
strcpy(buf, tok);
*name = buf;
return (strtok(NULL, " \t\n")) ? 1 : 0;
}
int parseYesNo(char *s)
{
if (!s || (s[0] == 'n' || s[0] == 'N') ||
!strcasecmp(s, "false") ||
!strcasecmp(s, "off") ||
!strcmp(s, "0")) {
return 0;
}
return 1;
}
char *findLastChar(char *s)
{
char *res = s;
while (*s) {
if (! isspace(*s)) {
res = s;
}
s++;
}
return res;
}
int parseNum(char *line, int *res)
{
char *s1;
s1 = NULL;
*res = strtoul(line, &s1, 10);
if ((*s1) || (s1 == line) || (*res == ULONG_MAX)) {
return 1;
}
return 0;
}
StringBuf getOutputFrom(char *dir, char *argv[],
char *writePtr, int writeBytesLeft,
int failNonZero)
{
int progPID;
int progDead;
int toProg[2];
int fromProg[2];
int status;
void *oldhandler;
int bytesWritten;
StringBuf readBuff;
int bytes;
unsigned char buf[8193];
oldhandler = signal(SIGPIPE, SIG_IGN);
pipe(toProg);
pipe(fromProg);
if (!(progPID = fork())) {
close(toProg[1]);
close(fromProg[0]);
dup2(toProg[0], 0); /* Make stdin the in pipe */
dup2(fromProg[1], 1); /* Make stdout the out pipe */
close(toProg[0]);
close(fromProg[1]);
if (dir) {
chdir(dir);
}
execvp(argv[0], argv);
rpmError(RPMERR_EXEC, "Couldn't exec %s", argv[0]);
_exit(RPMERR_EXEC);
}
if (progPID < 0) {
rpmError(RPMERR_FORK, "Couldn't fork %s", argv[0]);
return NULL;
}
close(toProg[0]);
close(fromProg[1]);
/* Do not block reading or writing from/to prog. */
fcntl(fromProg[0], F_SETFL, O_NONBLOCK);
fcntl(toProg[1], F_SETFL, O_NONBLOCK);
readBuff = newStringBuf();
progDead = 0;
do {
if (waitpid(progPID, &status, WNOHANG)) {
progDead = 1;
}
/* Write some stuff to the process if possible */
if (writeBytesLeft) {
if ((bytesWritten =
write(toProg[1], writePtr,
(1024<writeBytesLeft) ? 1024 : writeBytesLeft)) < 0) {
if (errno != EAGAIN) {
perror("getOutputFrom()");
exit(1);
}
bytesWritten = 0;
}
writeBytesLeft -= bytesWritten;
writePtr += bytesWritten;
} else {
close(toProg[1]);
}
/* Read any data from prog */
bytes = read(fromProg[0], buf, sizeof(buf)-1);
while (bytes > 0) {
buf[bytes] = '\0';
appendStringBuf(readBuff, buf);
bytes = read(fromProg[0], buf, sizeof(buf)-1);
}
/* terminate when prog dies */
} while (!progDead);
close(toProg[1]);
close(fromProg[0]);
signal(SIGPIPE, oldhandler);
if (writeBytesLeft) {
rpmError(RPMERR_EXEC, "failed to write all data to %s", argv[0]);
return NULL;
}
waitpid(progPID, &status, 0);
if (failNonZero && (!WIFEXITED(status) || WEXITSTATUS(status))) {
rpmError(RPMERR_EXEC, "%s failed", argv[0]);
return NULL;
}
return readBuff;
}
char *cleanFileName(char *name)
{
static char res[BUFSIZ];
char *copyTo, *copyFrom, copied;
/* Copy to fileName, eliminate duplicate "/" and trailing "/" */
copyTo = res;
copied = '\0';
copyFrom = name;
while (*copyFrom) {
if (*copyFrom != '/' || copied != '/') {
*copyTo++ = copied = *copyFrom;
}
copyFrom++;
}
*copyTo = '\0';
copyTo--;
if ((copyTo != res) && (*copyTo == '/')) {
*copyTo = '\0';
}
return res;
}
|