1 |
/* |
2 |
util.c - general purpose utility routines |
3 |
Copyright (C) 2007 siliconforks.com |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License along |
16 |
with this program; if not, write to the Free Software Foundation, Inc., |
17 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#include "util.h" |
21 |
|
22 |
#include <assert.h> |
23 |
#include <errno.h> |
24 |
#include <stdarg.h> |
25 |
#include <stdio.h> |
26 |
#include <string.h> |
27 |
#include <strings.h> |
28 |
|
29 |
#include <dirent.h> |
30 |
#include <libgen.h> |
31 |
#include <sys/stat.h> |
32 |
#include <sys/types.h> |
33 |
#include <unistd.h> |
34 |
|
35 |
const char * program = NULL; |
36 |
|
37 |
void fatal(const char * format, ...) { |
38 |
fprintf(stderr, "%s: ", program); |
39 |
va_list ap; |
40 |
va_start(ap, format); |
41 |
vfprintf(stderr, format, ap); |
42 |
va_end(ap); |
43 |
fputc('\n', stderr); |
44 |
fprintf(stderr, "Try `%s --help' for more information.\n", program); |
45 |
exit(EXIT_FAILURE); |
46 |
} |
47 |
|
48 |
void * xmalloc(size_t size) { |
49 |
void * result = malloc(size); |
50 |
if (result == NULL) { |
51 |
fatal("out of memory"); |
52 |
} |
53 |
return result; |
54 |
} |
55 |
|
56 |
char * xstrdup(const char * s) { |
57 |
char * result = strdup(s); |
58 |
if (result == NULL) { |
59 |
fatal("out of memory"); |
60 |
} |
61 |
return result; |
62 |
} |
63 |
|
64 |
char * xgetcwd(void) { |
65 |
char * result = getcwd(NULL, 0); |
66 |
if (result == NULL) { |
67 |
fatal("out of memory"); |
68 |
} |
69 |
return result; |
70 |
} |
71 |
|
72 |
FILE * xfopen(const char * file, const char * mode) { |
73 |
FILE * result = fopen(file, mode); |
74 |
if (result == NULL) { |
75 |
fatal("cannot open file: %s", file); |
76 |
} |
77 |
return result; |
78 |
} |
79 |
|
80 |
DIR * xopendir(const char * directory) { |
81 |
DIR * result = opendir(directory); |
82 |
if (result == NULL) { |
83 |
fatal("cannot open directory: %s", directory); |
84 |
} |
85 |
return result; |
86 |
} |
87 |
|
88 |
void xlstat(const char * file, struct stat * buf) { |
89 |
#ifdef _WIN32 |
90 |
return xstat(file, buf); |
91 |
#else |
92 |
if (lstat(file, buf) == -1) { |
93 |
fatal("cannot stat file: %s", file); |
94 |
} |
95 |
#endif |
96 |
} |
97 |
|
98 |
void xstat(const char * file, struct stat * buf) { |
99 |
if (stat(file, buf) == -1) { |
100 |
fatal("cannot stat file: %s", file); |
101 |
} |
102 |
} |
103 |
|
104 |
void xmkdir(const char * directory) { |
105 |
int result; |
106 |
#ifdef _WIN32 |
107 |
result = mkdir(directory); |
108 |
#else |
109 |
result = mkdir(directory, 0755); |
110 |
#endif |
111 |
if (result == -1) { |
112 |
fatal("cannot create directory: %s", directory); |
113 |
} |
114 |
} |
115 |
|
116 |
void mkdir_if_necessary(const char * directory) { |
117 |
struct stat buf; |
118 |
if (stat(directory, &buf) == 0) { |
119 |
if (! S_ISDIR(buf.st_mode)) { |
120 |
fatal("not a directory: %s", directory); |
121 |
} |
122 |
} |
123 |
else { |
124 |
if (errno == ENOENT) { |
125 |
xmkdir(directory); |
126 |
} |
127 |
else { |
128 |
fatal("cannot stat directory: %s", directory); |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
void mkdirs(const char * directory) { |
134 |
char * d = xmalloc(strlen(directory) + 1); |
135 |
for (const char * p = directory; *p != '\0'; p++) { |
136 |
if (*p == '/' && p > directory) { |
137 |
strncpy(d, directory, p - directory); |
138 |
d[p - directory] = '\0'; |
139 |
mkdir_if_necessary(d); |
140 |
} |
141 |
} |
142 |
mkdir_if_necessary(directory); |
143 |
free(d); |
144 |
} |
145 |
|
146 |
void xchdir(const char * directory) { |
147 |
if (chdir(directory) == -1) { |
148 |
fatal("cannot change directory: %s", directory); |
149 |
} |
150 |
} |
151 |
|
152 |
char * make_path(const char * parent, const char * relative_path) { |
153 |
size_t parent_length = strlen(parent); |
154 |
size_t relative_path_length = strlen(relative_path); |
155 |
char * result = xmalloc(parent_length + relative_path_length + 2); |
156 |
strcpy(result, parent); |
157 |
result[parent_length] = '/'; |
158 |
strcpy(result + parent_length + 1, relative_path); |
159 |
return result; |
160 |
} |
161 |
|
162 |
char * make_canonical_path(const char * relative_path) { |
163 |
char * original_directory = xgetcwd(); |
164 |
char * base = make_basename(relative_path); |
165 |
char * dir = make_dirname(relative_path); |
166 |
|
167 |
xchdir(dir); |
168 |
char * canonical_dir = xgetcwd(); |
169 |
char * result = make_path(canonical_dir, base); |
170 |
|
171 |
free(canonical_dir); |
172 |
free(base); |
173 |
free(dir); |
174 |
xchdir(original_directory); |
175 |
free(original_directory); |
176 |
|
177 |
return result; |
178 |
} |
179 |
|
180 |
char * make_basename(const char * path) { |
181 |
char * copy = xstrdup(path); |
182 |
char * result = xstrdup(basename(copy)); |
183 |
free(copy); |
184 |
return result; |
185 |
} |
186 |
|
187 |
char * make_dirname(const char * path) { |
188 |
char * copy = xstrdup(path); |
189 |
char * result = xstrdup(dirname(copy)); |
190 |
free(copy); |
191 |
return result; |
192 |
} |
193 |
|
194 |
int is_same_file(const char * file1, const char * file2) { |
195 |
#ifdef _WIN32 |
196 |
#define FILECMP strcasecmp |
197 |
#else |
198 |
#define FILECMP strcmp |
199 |
#endif |
200 |
if (FILECMP(file1, file2) == 0) { |
201 |
return 1; |
202 |
} |
203 |
|
204 |
char * canonical1 = make_canonical_path(file1); |
205 |
char * canonical2 = make_canonical_path(file2); |
206 |
int cmp = FILECMP(canonical1, canonical2); |
207 |
free(canonical1); |
208 |
free(canonical2); |
209 |
if (cmp == 0) { |
210 |
return 1; |
211 |
} |
212 |
|
213 |
#ifndef _WIN32 |
214 |
struct stat buf1; |
215 |
if (stat(file1, &buf1) == -1) { |
216 |
if (errno == ENOENT) { |
217 |
return 0; |
218 |
} |
219 |
else { |
220 |
fatal("cannot stat file: %s", file1); |
221 |
} |
222 |
} |
223 |
struct stat buf2; |
224 |
if (stat(file2, &buf2) == -1) { |
225 |
if (errno == ENOENT) { |
226 |
return 0; |
227 |
} |
228 |
else { |
229 |
fatal("cannot stat file: %s", file2); |
230 |
} |
231 |
} |
232 |
if (buf1.st_dev == buf2.st_dev && |
233 |
buf1.st_ino == buf2.st_ino) { |
234 |
return 1; |
235 |
} |
236 |
#endif |
237 |
return 0; |
238 |
#undef FILECMP |
239 |
} |
240 |
|
241 |
int contains_file(const char * file1, const char * file2) { |
242 |
int result = 0; |
243 |
char * ancestor = make_canonical_path(file1); |
244 |
char * d = make_canonical_path(file2); |
245 |
char * parent = make_dirname(d); |
246 |
while (strcmp(d, parent) != 0) { |
247 |
if (is_same_file(ancestor, parent)) { |
248 |
result = 1; |
249 |
break; |
250 |
} |
251 |
free(d); |
252 |
d = parent; |
253 |
parent = make_dirname(d); |
254 |
} |
255 |
free(d); |
256 |
free(parent); |
257 |
free(ancestor); |
258 |
return result; |
259 |
} |
260 |
|
261 |
void copy_stream(FILE * source, FILE * destination) { |
262 |
unsigned char buffer[8192]; |
263 |
for (;;) { |
264 |
int bytes_read = fread(buffer, 1, sizeof(buffer), source); |
265 |
if (bytes_read == 0) { |
266 |
break; |
267 |
} |
268 |
fwrite(buffer, 1, bytes_read, destination); |
269 |
} |
270 |
} |
271 |
|
272 |
void copy_file(const char * source_file, const char * destination_file) { |
273 |
FILE * source = xfopen(source_file, "rb"); |
274 |
FILE * destination = xfopen(destination_file, "wb"); |
275 |
|
276 |
copy_stream(source, destination); |
277 |
|
278 |
fclose(source); |
279 |
fclose(destination); |
280 |
} |
281 |
|
282 |
int directory_is_empty(const char * directory) { |
283 |
DIR * dir = xopendir(directory); |
284 |
int num_entries = 0; |
285 |
struct dirent * e; |
286 |
while ((e = readdir(dir)) != NULL) { |
287 |
if (strcmp(e->d_name, ".") != 0 && |
288 |
strcmp(e->d_name, "..") != 0) { |
289 |
num_entries++; |
290 |
} |
291 |
} |
292 |
closedir(dir); |
293 |
return num_entries == 0; |
294 |
} |
295 |
|
296 |
static struct DirListEntry * recursive_dir_list(const char * root, const char * directory_wrt_root, struct DirListEntry * head) { |
297 |
char * directory = directory_wrt_root == NULL? xstrdup(root): make_path(root, directory_wrt_root); |
298 |
DIR * dir = xopendir(directory); |
299 |
struct dirent * e; |
300 |
while ((e = readdir(dir)) != NULL) { |
301 |
if (strcmp(e->d_name, ".") == 0 || |
302 |
strcmp(e->d_name, "..") == 0) { |
303 |
continue; |
304 |
} |
305 |
char * entry = make_path(directory, e->d_name); |
306 |
char * entry_wrt_root = directory_wrt_root == NULL? xstrdup(e->d_name): make_path(directory_wrt_root, e->d_name); |
307 |
struct stat buf; |
308 |
xlstat(entry, &buf); |
309 |
if (S_ISREG(buf.st_mode)) { |
310 |
struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry)); |
311 |
p->name = entry_wrt_root; |
312 |
p->next = head; |
313 |
head = p; |
314 |
} |
315 |
else if (S_ISDIR(buf.st_mode)) { |
316 |
head = recursive_dir_list(root, entry_wrt_root, head); |
317 |
free(entry_wrt_root); |
318 |
} |
319 |
else { |
320 |
fatal("unknown file type: %s", entry); |
321 |
} |
322 |
free(entry); |
323 |
} |
324 |
closedir(dir); |
325 |
free(directory); |
326 |
return head; |
327 |
} |
328 |
|
329 |
struct DirListEntry * make_recursive_dir_list(const char * directory) { |
330 |
return recursive_dir_list(directory, NULL, NULL); |
331 |
} |
332 |
|
333 |
void free_dir_list(struct DirListEntry * list) { |
334 |
while (list != NULL) { |
335 |
struct DirListEntry * next = list->next; |
336 |
free(list->name); |
337 |
free(list); |
338 |
list = next; |
339 |
} |
340 |
} |