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

Annotation of /trunk/util.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.24