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