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
|
/*
* loadermisc.c - miscellaneous loader functions that don't seem to fit
* anywhere else (yet) (was misc.c)
* JKFIXME: need to break out into reasonable files based on function
*
* Erik Troan <ewt@redhat.com>
* Matt Wilson <msw@redhat.com>
* Michael Fulbright <msf@redhat.com>
* Jeremy Katz <katzj@redhat.com>
*
* Copyright 1999 - 2002 Red Hat, Inc.
*
* This software may be freely redistributed under the terms of the GNU
* General Public License.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include "log.h"
int copyFileFd(int infd, char * dest) {
int outfd;
char buf[4096];
int i;
int rc = 0;
outfd = open(dest, O_CREAT | O_RDWR, 0666);
if (outfd < 0) {
logMessage("failed to open %s: %s", dest, strerror(errno));
return 1;
}
while ((i = read(infd, buf, sizeof(buf))) > 0) {
if (write(outfd, buf, i) != i) {
rc = 1;
break;
}
}
close(outfd);
return rc;
}
int copyFile(char * source, char * dest) {
int infd = -1;
int rc;
infd = open(source, O_RDONLY);
if (infd < 0) {
logMessage("failed to open %s: %s", source, strerror(errno));
return 1;
}
rc = copyFileFd(infd, dest);
close(infd);
return rc;
}
char * readLine(FILE * f) {
char buf[1024];
fgets(buf, sizeof(buf), f);
/* chop */
buf[strlen(buf) - 1] = '\0';
return strdup(buf);
}
int simpleStringCmp(const void * a, const void * b) {
const char * first = *((const char **) a);
const char * second = *((const char **) b);
return strcmp(first, second);
}
char * sdupprintf(const char *format, ...) {
char *buf = NULL;
char c;
va_list ap1, ap2;
size_t size = 0;
va_start(ap1, format);
va_copy(ap2, ap1);
/* XXX requires C99 vsnprintf behavior */
size = vsnprintf(&c, 1, format, ap1) + 1;
if (size == -1) {
printf("ERROR: vsnprintf behavior is not C99\n");
abort();
}
va_end(ap1);
buf = malloc(size);
if (buf == NULL)
return NULL;
vsnprintf(buf, size, format, ap2);
va_end (ap2);
return buf;
}
|