1 |
/* |
2 |
util.c - general purpose utility routines |
3 |
Copyright (C) 2007, 2008 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 |
#define _GNU_SOURCE |
21 |
|
22 |
#include <config.h> |
23 |
|
24 |
#include "util.h" |
25 |
|
26 |
#include <assert.h> |
27 |
#include <errno.h> |
28 |
#include <limits.h> |
29 |
#include <stdarg.h> |
30 |
#include <stdio.h> |
31 |
#include <stdint.h> |
32 |
#include <string.h> |
33 |
#include <strings.h> |
34 |
|
35 |
#include <dirent.h> |
36 |
#include <libgen.h> |
37 |
#include <sys/stat.h> |
38 |
#include <sys/types.h> |
39 |
#include <unistd.h> |
40 |
|
41 |
const char * program = NULL; |
42 |
|
43 |
void fatal(const char * format, ...) { |
44 |
fprintf(stderr, "%s: ", program); |
45 |
va_list ap; |
46 |
va_start(ap, format); |
47 |
vfprintf(stderr, format, ap); |
48 |
va_end(ap); |
49 |
fputc('\n', stderr); |
50 |
exit(EXIT_FAILURE); |
51 |
} |
52 |
|
53 |
void fatal_command_line(const char * format, ...) { |
54 |
fprintf(stderr, "%s: ", program); |
55 |
va_list ap; |
56 |
va_start(ap, format); |
57 |
vfprintf(stderr, format, ap); |
58 |
va_end(ap); |
59 |
fputc('\n', stderr); |
60 |
fprintf(stderr, "Try `%s --help' for more information.\n", program); |
61 |
exit(EXIT_FAILURE); |
62 |
} |
63 |
|
64 |
void fatal_source(const char * source_file, unsigned int line_number, const char * format, ...) { |
65 |
fprintf(stderr, "%s:%s:%u: ", program, source_file, line_number); |
66 |
va_list ap; |
67 |
va_start(ap, format); |
68 |
vfprintf(stderr, format, ap); |
69 |
va_end(ap); |
70 |
fputc('\n', stderr); |
71 |
exit(EXIT_FAILURE); |
72 |
} |
73 |
|
74 |
void version(void) { |
75 |
printf("%s %s\n", program, VERSION); |
76 |
printf("Character encoding support: "); |
77 |
#if HAVE_ICONV |
78 |
printf("iconv\n"); |
79 |
#elif HAVE_MULTIBYTETOWIDECHAR |
80 |
printf("MultiByteToWideChar\n"); |
81 |
#else |
82 |
printf("none\n"); |
83 |
#endif |
84 |
exit(EXIT_SUCCESS); |
85 |
} |
86 |
|
87 |
size_t addst(size_t x, size_t y) { |
88 |
if (SIZE_MAX - x < y) { |
89 |
fatal("integer overflow"); |
90 |
} |
91 |
return x + y; |
92 |
} |
93 |
|
94 |
size_t mulst(size_t x, size_t y) { |
95 |
if (x == 0 || y == 0) { |
96 |
return 0; |
97 |
} |
98 |
if (SIZE_MAX / x < y) { |
99 |
fatal("integer overflow"); |
100 |
} |
101 |
return x * y; |
102 |
} |
103 |
|
104 |
void * xmalloc(size_t size) { |
105 |
void * result = malloc(size); |
106 |
if (result == NULL) { |
107 |
fatal("out of memory"); |
108 |
} |
109 |
return result; |
110 |
} |
111 |
|
112 |
void * xrealloc(void * p, size_t size) { |
113 |
void * result = realloc(p, size); |
114 |
if (result == NULL) { |
115 |
fatal("out of memory"); |
116 |
} |
117 |
return result; |
118 |
} |
119 |
|
120 |
char * xstrdup(const char * s) { |
121 |
char * result = strdup(s); |
122 |
if (result == NULL) { |
123 |
fatal("out of memory"); |
124 |
} |
125 |
return result; |
126 |
} |
127 |
|
128 |
char * xstrndup(const char * s, size_t size) { |
129 |
char * result = strndup(s, size); |
130 |
if (result == NULL) { |
131 |
fatal("out of memory"); |
132 |
} |
133 |
return result; |
134 |
} |
135 |
|
136 |
int xasprintf(char ** s, const char * template, ...) { |
137 |
va_list a; |
138 |
va_start(a, template); |
139 |
int result = vasprintf(s, template, a); |
140 |
va_end(a); |
141 |
if (result < 0) { |
142 |
fatal("out of memory"); |
143 |
} |
144 |
return result; |
145 |
} |
146 |
|
147 |
char * xgetcwd(void) { |
148 |
char * result = getcwd(NULL, 0); |
149 |
if (result == NULL) { |
150 |
fatal("out of memory"); |
151 |
} |
152 |
return result; |
153 |
} |
154 |
|
155 |
FILE * xfopen(const char * file, const char * mode) { |
156 |
FILE * result = fopen(file, mode); |
157 |
if (result == NULL) { |
158 |
fatal("cannot open file: %s", file); |
159 |
} |
160 |
return result; |
161 |
} |
162 |
|
163 |
DIR * xopendir(const char * directory) { |
164 |
DIR * result = opendir(directory); |
165 |
if (result == NULL) { |
166 |
fatal("cannot open directory: %s", directory); |
167 |
} |
168 |
return result; |
169 |
} |
170 |
|
171 |
void xlstat(const char * file, struct stat * buf) { |
172 |
#ifdef _WIN32 |
173 |
return xstat(file, buf); |
174 |
#else |
175 |
if (lstat(file, buf) == -1) { |
176 |
fatal("cannot stat file: %s", file); |
177 |
} |
178 |
#endif |
179 |
} |
180 |
|
181 |
void xstat(const char * file, struct stat * buf) { |
182 |
if (stat(file, buf) == -1) { |
183 |
fatal("cannot stat file: %s", file); |
184 |
} |
185 |
} |
186 |
|
187 |
void xmkdir(const char * directory) { |
188 |
int result; |
189 |
#ifdef _WIN32 |
190 |
result = mkdir(directory); |
191 |
#else |
192 |
result = mkdir(directory, 0755); |
193 |
#endif |
194 |
if (result == -1) { |
195 |
fatal("cannot create directory: %s", directory); |
196 |
} |
197 |
} |
198 |
|
199 |
void mkdir_if_necessary(const char * directory) { |
200 |
struct stat buf; |
201 |
if (stat(directory, &buf) == 0) { |
202 |
if (! S_ISDIR(buf.st_mode)) { |
203 |
fatal("not a directory: %s", directory); |
204 |
} |
205 |
} |
206 |
else { |
207 |
if (errno == ENOENT) { |
208 |
xmkdir(directory); |
209 |
} |
210 |
else { |
211 |
fatal("cannot stat directory: %s", directory); |
212 |
} |
213 |
} |
214 |
} |
215 |
|
216 |
void mkdirs(const char * directory) { |
217 |
char * d = xmalloc(strlen(directory) + 1); |
218 |
for (const char * p = directory; *p != '\0'; p++) { |
219 |
if (*p == '/' && p > directory) { |
220 |
strncpy(d, directory, p - directory); |
221 |
d[p - directory] = '\0'; |
222 |
mkdir_if_necessary(d); |
223 |
} |
224 |
} |
225 |
mkdir_if_necessary(directory); |
226 |
free(d); |
227 |
} |
228 |
|
229 |
void xchdir(const char * directory) { |
230 |
if (chdir(directory) == -1) { |
231 |
fatal("cannot change directory: %s", directory); |
232 |
} |
233 |
} |
234 |
|
235 |
bool str_starts_with(const char * string, const char * prefix) { |
236 |
const char * string_ptr = string; |
237 |
const char * prefix_ptr = prefix; |
238 |
while (*string_ptr != '\0' && *prefix_ptr != '\0') { |
239 |
if (*string_ptr != *prefix_ptr) { |
240 |
return false; |
241 |
} |
242 |
string_ptr++; |
243 |
prefix_ptr++; |
244 |
} |
245 |
if (*string_ptr == '\0' && *prefix_ptr != '\0') { |
246 |
return false; |
247 |
} |
248 |
return true; |
249 |
} |
250 |
|
251 |
bool str_ends_with(const char * string, const char * suffix) { |
252 |
size_t string_length = strlen(string); |
253 |
size_t suffix_length = strlen(suffix); |
254 |
if (string_length < suffix_length) { |
255 |
return false; |
256 |
} |
257 |
return strcmp(string + string_length - suffix_length, suffix) == 0; |
258 |
} |
259 |
|
260 |
char * make_path(const char * parent, const char * relative_path) { |
261 |
size_t parent_length = strlen(parent); |
262 |
size_t relative_path_length = strlen(relative_path); |
263 |
size_t result_length = addst(parent_length, relative_path_length); |
264 |
result_length = addst(result_length, 2); |
265 |
char * result = xmalloc(result_length); |
266 |
strcpy(result, parent); |
267 |
result[parent_length] = '/'; |
268 |
strcpy(result + parent_length + 1, relative_path); |
269 |
return result; |
270 |
} |
271 |
|
272 |
char * make_canonical_path(const char * relative_path) { |
273 |
char * original_directory = xgetcwd(); |
274 |
char * base = make_basename(relative_path); |
275 |
char * dir = make_dirname(relative_path); |
276 |
|
277 |
xchdir(dir); |
278 |
char * canonical_dir = xgetcwd(); |
279 |
char * result = make_path(canonical_dir, base); |
280 |
|
281 |
free(canonical_dir); |
282 |
free(base); |
283 |
free(dir); |
284 |
xchdir(original_directory); |
285 |
free(original_directory); |
286 |
|
287 |
return result; |
288 |
} |
289 |
|
290 |
char * make_basename(const char * path) { |
291 |
char * copy = xstrdup(path); |
292 |
char * result = xstrdup(basename(copy)); |
293 |
free(copy); |
294 |
return result; |
295 |
} |
296 |
|
297 |
char * make_dirname(const char * path) { |
298 |
char * copy = xstrdup(path); |
299 |
char * result = xstrdup(dirname(copy)); |
300 |
free(copy); |
301 |
return result; |
302 |
} |
303 |
|
304 |
int is_same_file(const char * file1, const char * file2) { |
305 |
#ifdef _WIN32 |
306 |
#define FILECMP strcasecmp |
307 |
#else |
308 |
#define FILECMP strcmp |
309 |
#endif |
310 |
if (FILECMP(file1, file2) == 0) { |
311 |
return 1; |
312 |
} |
313 |
|
314 |
char * canonical1 = make_canonical_path(file1); |
315 |
char * canonical2 = make_canonical_path(file2); |
316 |
int cmp = FILECMP(canonical1, canonical2); |
317 |
free(canonical1); |
318 |
free(canonical2); |
319 |
if (cmp == 0) { |
320 |
return 1; |
321 |
} |
322 |
|
323 |
#ifndef _WIN32 |
324 |
struct stat buf1; |
325 |
if (stat(file1, &buf1) == -1) { |
326 |
if (errno == ENOENT) { |
327 |
return 0; |
328 |
} |
329 |
else { |
330 |
fatal("cannot stat file: %s", file1); |
331 |
} |
332 |
} |
333 |
struct stat buf2; |
334 |
if (stat(file2, &buf2) == -1) { |
335 |
if (errno == ENOENT) { |
336 |
return 0; |
337 |
} |
338 |
else { |
339 |
fatal("cannot stat file: %s", file2); |
340 |
} |
341 |
} |
342 |
if (buf1.st_dev == buf2.st_dev && |
343 |
buf1.st_ino == buf2.st_ino) { |
344 |
return 1; |
345 |
} |
346 |
#endif |
347 |
return 0; |
348 |
#undef FILECMP |
349 |
} |
350 |
|
351 |
int contains_file(const char * file1, const char * file2) { |
352 |
int result = 0; |
353 |
char * ancestor = make_canonical_path(file1); |
354 |
char * d = make_canonical_path(file2); |
355 |
char * parent = make_dirname(d); |
356 |
while (strcmp(d, parent) != 0) { |
357 |
if (is_same_file(ancestor, parent)) { |
358 |
result = 1; |
359 |
break; |
360 |
} |
361 |
free(d); |
362 |
d = parent; |
363 |
parent = make_dirname(d); |
364 |
} |
365 |
free(d); |
366 |
free(parent); |
367 |
free(ancestor); |
368 |
return result; |
369 |
} |
370 |
|
371 |
void copy_stream(FILE * source, FILE * destination) { |
372 |
unsigned char buffer[8192]; |
373 |
for (;;) { |
374 |
int bytes_read = fread(buffer, 1, sizeof(buffer), source); |
375 |
if (bytes_read == 0) { |
376 |
break; |
377 |
} |
378 |
fwrite(buffer, 1, bytes_read, destination); |
379 |
} |
380 |
} |
381 |
|
382 |
void copy_file(const char * source_file, const char * destination_file) { |
383 |
FILE * source = xfopen(source_file, "rb"); |
384 |
FILE * destination = xfopen(destination_file, "wb"); |
385 |
|
386 |
copy_stream(source, destination); |
387 |
|
388 |
#ifndef _WIN32 |
389 |
/* copy permissions */ |
390 |
struct stat buf; |
391 |
if (fstat(fileno(source), &buf) == -1) { |
392 |
fatal("cannot stat file: %s", source_file); |
393 |
} |
394 |
fchmod(fileno(destination), buf.st_mode); |
395 |
#endif |
396 |
|
397 |
fclose(source); |
398 |
fclose(destination); |
399 |
} |
400 |
|
401 |
bool directory_is_empty(const char * directory) { |
402 |
bool result = true; |
403 |
DIR * dir = xopendir(directory); |
404 |
struct dirent * e; |
405 |
while ((e = readdir(dir)) != NULL) { |
406 |
if (strcmp(e->d_name, ".") != 0 && |
407 |
strcmp(e->d_name, "..") != 0) { |
408 |
result = false; |
409 |
break; |
410 |
} |
411 |
} |
412 |
closedir(dir); |
413 |
return result; |
414 |
} |
415 |
|
416 |
static struct DirListEntry * recursive_dir_list(const char * root, const char * directory_wrt_root, struct DirListEntry * head) { |
417 |
char * directory = directory_wrt_root == NULL? xstrdup(root): make_path(root, directory_wrt_root); |
418 |
DIR * dir = xopendir(directory); |
419 |
struct dirent * e; |
420 |
while ((e = readdir(dir)) != NULL) { |
421 |
if (strcmp(e->d_name, ".") == 0 || |
422 |
strcmp(e->d_name, "..") == 0) { |
423 |
continue; |
424 |
} |
425 |
char * entry = make_path(directory, e->d_name); |
426 |
char * entry_wrt_root = directory_wrt_root == NULL? xstrdup(e->d_name): make_path(directory_wrt_root, e->d_name); |
427 |
struct stat buf; |
428 |
xlstat(entry, &buf); |
429 |
if (S_ISREG(buf.st_mode)) { |
430 |
struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry)); |
431 |
p->name = entry_wrt_root; |
432 |
p->next = head; |
433 |
head = p; |
434 |
} |
435 |
else if (S_ISDIR(buf.st_mode)) { |
436 |
head = recursive_dir_list(root, entry_wrt_root, head); |
437 |
free(entry_wrt_root); |
438 |
} |
439 |
#ifndef _WIN32 |
440 |
else if (S_ISLNK(buf.st_mode)) { |
441 |
/* check what it points to */ |
442 |
xstat(entry, &buf); |
443 |
if (S_ISREG(buf.st_mode)) { |
444 |
struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry)); |
445 |
p->name = entry_wrt_root; |
446 |
p->next = head; |
447 |
head = p; |
448 |
} |
449 |
else { |
450 |
fatal("refusing to follow symbolic link: %s", entry); |
451 |
} |
452 |
} |
453 |
#endif |
454 |
else { |
455 |
fatal("unknown file type: %s", entry); |
456 |
} |
457 |
free(entry); |
458 |
} |
459 |
closedir(dir); |
460 |
free(directory); |
461 |
return head; |
462 |
} |
463 |
|
464 |
struct DirListEntry * make_recursive_dir_list(const char * directory) { |
465 |
return recursive_dir_list(directory, NULL, NULL); |
466 |
} |
467 |
|
468 |
void free_dir_list(struct DirListEntry * list) { |
469 |
while (list != NULL) { |
470 |
struct DirListEntry * next = list->next; |
471 |
free(list->name); |
472 |
free(list); |
473 |
list = next; |
474 |
} |
475 |
} |
476 |
|
477 |
#ifndef HAVE_STRNDUP |
478 |
char * strndup(const char * s, size_t size) { |
479 |
size_t length = strlen(s); |
480 |
if (length > size) { |
481 |
char * result = xmalloc(size + 1); |
482 |
strncpy(result, s, size); |
483 |
result[size] = '\0'; |
484 |
return result; |
485 |
} |
486 |
else { |
487 |
char * result = xmalloc(length + 1); |
488 |
strcpy(result, s); |
489 |
return result; |
490 |
} |
491 |
} |
492 |
#endif |
493 |
|
494 |
#ifndef HAVE_VASPRINTF |
495 |
int vasprintf(char ** s, const char * template, va_list a) { |
496 |
int size = 100; |
497 |
*s = malloc(size); |
498 |
if (*s == NULL) { |
499 |
return -1; |
500 |
} |
501 |
|
502 |
va_list copy; |
503 |
va_copy(copy, a); |
504 |
int result = vsnprintf(*s, size, template, copy); |
505 |
if (result >= size) { |
506 |
int new_size = result; |
507 |
if (new_size == INT_MAX) { |
508 |
free(*s); |
509 |
return -1; |
510 |
} |
511 |
new_size++; |
512 |
char * new_s = realloc(*s, new_size); |
513 |
if (new_s == NULL) { |
514 |
free(*s); |
515 |
return -1; |
516 |
} |
517 |
*s = new_s; |
518 |
size = new_size; |
519 |
va_copy(copy, a); |
520 |
result = vsnprintf(*s, size, template, copy); |
521 |
assert(result == size - 1); |
522 |
} |
523 |
else if (result == -1) { |
524 |
while (result == -1) { |
525 |
if (size == INT_MAX) { |
526 |
free(*s); |
527 |
return -1; |
528 |
} |
529 |
int new_size; |
530 |
if (size > INT_MAX / 2) { |
531 |
new_size = INT_MAX; |
532 |
} |
533 |
else { |
534 |
new_size = 2 * size; |
535 |
} |
536 |
char * new_s = realloc(*s, new_size); |
537 |
if (new_s == NULL) { |
538 |
free(*s); |
539 |
return -1; |
540 |
} |
541 |
*s = new_s; |
542 |
size = new_size; |
543 |
va_copy(copy, a); |
544 |
result = vsnprintf(*s, size, template, copy); |
545 |
} |
546 |
assert(result <= size - 1); |
547 |
} |
548 |
|
549 |
return result; |
550 |
} |
551 |
#endif |
552 |
|
553 |
#ifndef HAVE_ASPRINTF |
554 |
int asprintf(char ** s, const char * template, ...) { |
555 |
va_list a; |
556 |
va_start(a, template); |
557 |
int result = vasprintf(s, template, a); |
558 |
va_end(a); |
559 |
return result; |
560 |
} |
561 |
#endif |