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

Annotation of /trunk/util.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 449 - (hide annotations)
Tue Aug 11 17:23:20 2009 UTC (10 years, 2 months ago) by siliconforks
File MIME type: text/plain
File size: 13894 byte(s)
Update --version output.
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 448 assert(parent_length > 0);
285     assert(relative_path_length > 0);
286 siliconforks 106 size_t result_length = addst(parent_length, relative_path_length);
287 siliconforks 448 int parent_ends_with_slash = is_slash(parent[parent_length - 1]);
288     if (parent_ends_with_slash) {
289     result_length = addst(result_length, 1);
290     }
291     else {
292     result_length = addst(result_length, 2);
293     }
294 siliconforks 106 char * result = xmalloc(result_length);
295 siliconforks 2 strcpy(result, parent);
296 siliconforks 448 char * p = result + parent_length;
297     if (! parent_ends_with_slash) {
298     *p = '/';
299     ++p;
300     }
301     strcpy(p, relative_path);
302 siliconforks 2 return result;
303     }
304    
305     char * make_canonical_path(const char * relative_path) {
306     char * original_directory = xgetcwd();
307     char * base = make_basename(relative_path);
308     char * dir = make_dirname(relative_path);
309    
310     xchdir(dir);
311     char * canonical_dir = xgetcwd();
312     char * result = make_path(canonical_dir, base);
313    
314     free(canonical_dir);
315     free(base);
316     free(dir);
317     xchdir(original_directory);
318     free(original_directory);
319    
320     return result;
321     }
322    
323     char * make_basename(const char * path) {
324     char * copy = xstrdup(path);
325     char * result = xstrdup(basename(copy));
326     free(copy);
327     return result;
328     }
329    
330     char * make_dirname(const char * path) {
331     char * copy = xstrdup(path);
332     char * result = xstrdup(dirname(copy));
333     free(copy);
334     return result;
335     }
336    
337     int is_same_file(const char * file1, const char * file2) {
338     #ifdef _WIN32
339     #define FILECMP strcasecmp
340     #else
341     #define FILECMP strcmp
342     #endif
343     if (FILECMP(file1, file2) == 0) {
344     return 1;
345     }
346    
347     char * canonical1 = make_canonical_path(file1);
348     char * canonical2 = make_canonical_path(file2);
349     int cmp = FILECMP(canonical1, canonical2);
350     free(canonical1);
351     free(canonical2);
352     if (cmp == 0) {
353     return 1;
354     }
355    
356     #ifndef _WIN32
357     struct stat buf1;
358     if (stat(file1, &buf1) == -1) {
359     if (errno == ENOENT) {
360     return 0;
361     }
362     else {
363     fatal("cannot stat file: %s", file1);
364     }
365     }
366     struct stat buf2;
367     if (stat(file2, &buf2) == -1) {
368     if (errno == ENOENT) {
369     return 0;
370     }
371     else {
372     fatal("cannot stat file: %s", file2);
373     }
374     }
375     if (buf1.st_dev == buf2.st_dev &&
376     buf1.st_ino == buf2.st_ino) {
377     return 1;
378     }
379     #endif
380     return 0;
381     #undef FILECMP
382     }
383    
384     int contains_file(const char * file1, const char * file2) {
385     int result = 0;
386     char * ancestor = make_canonical_path(file1);
387     char * d = make_canonical_path(file2);
388     char * parent = make_dirname(d);
389     while (strcmp(d, parent) != 0) {
390     if (is_same_file(ancestor, parent)) {
391     result = 1;
392     break;
393     }
394     free(d);
395     d = parent;
396     parent = make_dirname(d);
397     }
398     free(d);
399     free(parent);
400     free(ancestor);
401     return result;
402     }
403    
404     void copy_stream(FILE * source, FILE * destination) {
405     unsigned char buffer[8192];
406     for (;;) {
407     int bytes_read = fread(buffer, 1, sizeof(buffer), source);
408     if (bytes_read == 0) {
409     break;
410     }
411     fwrite(buffer, 1, bytes_read, destination);
412     }
413     }
414    
415     void copy_file(const char * source_file, const char * destination_file) {
416     FILE * source = xfopen(source_file, "rb");
417     FILE * destination = xfopen(destination_file, "wb");
418    
419     copy_stream(source, destination);
420    
421 siliconforks 359 #ifndef _WIN32
422     /* copy permissions */
423     struct stat buf;
424     if (fstat(fileno(source), &buf) == -1) {
425     fatal("cannot stat file: %s", source_file);
426     }
427     fchmod(fileno(destination), buf.st_mode);
428     #endif
429    
430 siliconforks 2 fclose(source);
431     fclose(destination);
432     }
433    
434 siliconforks 106 bool directory_is_empty(const char * directory) {
435     bool result = true;
436 siliconforks 2 DIR * dir = xopendir(directory);
437     struct dirent * e;
438     while ((e = readdir(dir)) != NULL) {
439     if (strcmp(e->d_name, ".") != 0 &&
440     strcmp(e->d_name, "..") != 0) {
441 siliconforks 106 result = false;
442     break;
443 siliconforks 2 }
444     }
445     closedir(dir);
446 siliconforks 106 return result;
447 siliconforks 2 }
448    
449     static struct DirListEntry * recursive_dir_list(const char * root, const char * directory_wrt_root, struct DirListEntry * head) {
450     char * directory = directory_wrt_root == NULL? xstrdup(root): make_path(root, directory_wrt_root);
451     DIR * dir = xopendir(directory);
452     struct dirent * e;
453     while ((e = readdir(dir)) != NULL) {
454     if (strcmp(e->d_name, ".") == 0 ||
455     strcmp(e->d_name, "..") == 0) {
456     continue;
457     }
458     char * entry = make_path(directory, e->d_name);
459     char * entry_wrt_root = directory_wrt_root == NULL? xstrdup(e->d_name): make_path(directory_wrt_root, e->d_name);
460     struct stat buf;
461     xlstat(entry, &buf);
462     if (S_ISREG(buf.st_mode)) {
463     struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
464     p->name = entry_wrt_root;
465     p->next = head;
466     head = p;
467     }
468     else if (S_ISDIR(buf.st_mode)) {
469     head = recursive_dir_list(root, entry_wrt_root, head);
470     free(entry_wrt_root);
471     }
472 siliconforks 364 #ifndef _WIN32
473 siliconforks 359 else if (S_ISLNK(buf.st_mode)) {
474     /* check what it points to */
475     xstat(entry, &buf);
476     if (S_ISREG(buf.st_mode)) {
477     struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
478     p->name = entry_wrt_root;
479     p->next = head;
480     head = p;
481     }
482     else {
483     fatal("refusing to follow symbolic link: %s", entry);
484     }
485     }
486 siliconforks 364 #endif
487 siliconforks 2 else {
488     fatal("unknown file type: %s", entry);
489     }
490     free(entry);
491     }
492     closedir(dir);
493     free(directory);
494     return head;
495     }
496    
497     struct DirListEntry * make_recursive_dir_list(const char * directory) {
498     return recursive_dir_list(directory, NULL, NULL);
499     }
500    
501     void free_dir_list(struct DirListEntry * list) {
502     while (list != NULL) {
503     struct DirListEntry * next = list->next;
504     free(list->name);
505     free(list);
506     list = next;
507     }
508     }
509 siliconforks 99
510 siliconforks 125 #ifndef HAVE_STRNDUP
511     char * strndup(const char * s, size_t size) {
512     size_t length = strlen(s);
513     if (length > size) {
514     char * result = xmalloc(size + 1);
515     strncpy(result, s, size);
516     result[size] = '\0';
517     return result;
518     }
519     else {
520     char * result = xmalloc(length + 1);
521     strcpy(result, s);
522     return result;
523     }
524 siliconforks 106 }
525     #endif
526    
527 siliconforks 99 #ifndef HAVE_VASPRINTF
528     int vasprintf(char ** s, const char * template, va_list a) {
529     int size = 100;
530     *s = malloc(size);
531     if (*s == NULL) {
532     return -1;
533     }
534    
535     va_list copy;
536     va_copy(copy, a);
537     int result = vsnprintf(*s, size, template, copy);
538     if (result >= size) {
539     int new_size = result;
540     if (new_size == INT_MAX) {
541     free(*s);
542 siliconforks 106 return -1;
543 siliconforks 99 }
544     new_size++;
545     char * new_s = realloc(*s, new_size);
546     if (new_s == NULL) {
547     free(*s);
548     return -1;
549     }
550     *s = new_s;
551     size = new_size;
552     va_copy(copy, a);
553     result = vsnprintf(*s, size, template, copy);
554     assert(result == size - 1);
555     }
556     else if (result == -1) {
557     while (result == -1) {
558 siliconforks 106 if (size == INT_MAX) {
559 siliconforks 99 free(*s);
560 siliconforks 106 return -1;
561 siliconforks 99 }
562 siliconforks 106 int new_size;
563     if (size > INT_MAX / 2) {
564     new_size = INT_MAX;
565     }
566     else {
567     new_size = 2 * size;
568     }
569 siliconforks 99 char * new_s = realloc(*s, new_size);
570     if (new_s == NULL) {
571     free(*s);
572     return -1;
573     }
574     *s = new_s;
575     size = new_size;
576     va_copy(copy, a);
577     result = vsnprintf(*s, size, template, copy);
578     }
579     assert(result <= size - 1);
580     }
581    
582     return result;
583     }
584     #endif
585    
586     #ifndef HAVE_ASPRINTF
587     int asprintf(char ** s, const char * template, ...) {
588     va_list a;
589     va_start(a, template);
590     int result = vasprintf(s, template, a);
591     va_end(a);
592     return result;
593     }
594     #endif

  ViewVC Help
Powered by ViewVC 1.1.24