/[jscoverage]/trunk/util.c
ViewVC logotype

Contents of /trunk/util.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.24