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

Annotation of /trunk/util.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 550 - (hide annotations)
Wed Apr 21 12:22:41 2010 UTC (9 years, 6 months ago) by siliconforks
File MIME type: text/plain
File size: 13953 byte(s)
When creating a file, always set write bit (to avoid creating read-only file).

1 siliconforks 2 /*
2     util.c - general purpose utility routines
3 siliconforks 505 Copyright (C) 2007, 2008, 2009, 2010 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 505 printf("Copyright (C) 2010 siliconforks.com\n");
94 siliconforks 449 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 siliconforks 550 /* permissions should always include write bit (to avoid creating read-only file) */
426     fchmod(fileno(destination), buf.st_mode | S_IWUSR);
427 siliconforks 359 #endif
428    
429 siliconforks 2 fclose(source);
430     fclose(destination);
431     }
432    
433 siliconforks 106 bool directory_is_empty(const char * directory) {
434     bool result = true;
435 siliconforks 2 DIR * dir = xopendir(directory);
436     struct dirent * e;
437     while ((e = readdir(dir)) != NULL) {
438     if (strcmp(e->d_name, ".") != 0 &&
439     strcmp(e->d_name, "..") != 0) {
440 siliconforks 106 result = false;
441     break;
442 siliconforks 2 }
443     }
444     closedir(dir);
445 siliconforks 106 return result;
446 siliconforks 2 }
447    
448     static struct DirListEntry * recursive_dir_list(const char * root, const char * directory_wrt_root, struct DirListEntry * head) {
449     char * directory = directory_wrt_root == NULL? xstrdup(root): make_path(root, directory_wrt_root);
450     DIR * dir = xopendir(directory);
451     struct dirent * e;
452     while ((e = readdir(dir)) != NULL) {
453     if (strcmp(e->d_name, ".") == 0 ||
454     strcmp(e->d_name, "..") == 0) {
455     continue;
456     }
457     char * entry = make_path(directory, e->d_name);
458     char * entry_wrt_root = directory_wrt_root == NULL? xstrdup(e->d_name): make_path(directory_wrt_root, e->d_name);
459     struct stat buf;
460     xlstat(entry, &buf);
461     if (S_ISREG(buf.st_mode)) {
462     struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
463     p->name = entry_wrt_root;
464     p->next = head;
465     head = p;
466     }
467     else if (S_ISDIR(buf.st_mode)) {
468     head = recursive_dir_list(root, entry_wrt_root, head);
469     free(entry_wrt_root);
470     }
471 siliconforks 364 #ifndef _WIN32
472 siliconforks 359 else if (S_ISLNK(buf.st_mode)) {
473     /* check what it points to */
474     xstat(entry, &buf);
475     if (S_ISREG(buf.st_mode)) {
476     struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
477     p->name = entry_wrt_root;
478     p->next = head;
479     head = p;
480     }
481     else {
482     fatal("refusing to follow symbolic link: %s", entry);
483     }
484     }
485 siliconforks 364 #endif
486 siliconforks 2 else {
487     fatal("unknown file type: %s", entry);
488     }
489     free(entry);
490     }
491     closedir(dir);
492     free(directory);
493     return head;
494     }
495    
496     struct DirListEntry * make_recursive_dir_list(const char * directory) {
497     return recursive_dir_list(directory, NULL, NULL);
498     }
499    
500     void free_dir_list(struct DirListEntry * list) {
501     while (list != NULL) {
502     struct DirListEntry * next = list->next;
503     free(list->name);
504     free(list);
505     list = next;
506     }
507     }
508 siliconforks 99
509 siliconforks 125 #ifndef HAVE_STRNDUP
510     char * strndup(const char * s, size_t size) {
511     size_t length = strlen(s);
512     if (length > size) {
513     char * result = xmalloc(size + 1);
514     strncpy(result, s, size);
515     result[size] = '\0';
516     return result;
517     }
518     else {
519     char * result = xmalloc(length + 1);
520     strcpy(result, s);
521     return result;
522     }
523 siliconforks 106 }
524     #endif
525    
526 siliconforks 99 #ifndef HAVE_VASPRINTF
527     int vasprintf(char ** s, const char * template, va_list a) {
528     int size = 100;
529     *s = malloc(size);
530     if (*s == NULL) {
531     return -1;
532     }
533    
534     va_list copy;
535     va_copy(copy, a);
536     int result = vsnprintf(*s, size, template, copy);
537     if (result >= size) {
538     int new_size = result;
539     if (new_size == INT_MAX) {
540     free(*s);
541 siliconforks 106 return -1;
542 siliconforks 99 }
543     new_size++;
544     char * new_s = realloc(*s, new_size);
545     if (new_s == NULL) {
546     free(*s);
547     return -1;
548     }
549     *s = new_s;
550     size = new_size;
551     va_copy(copy, a);
552     result = vsnprintf(*s, size, template, copy);
553     assert(result == size - 1);
554     }
555     else if (result == -1) {
556     while (result == -1) {
557 siliconforks 106 if (size == INT_MAX) {
558 siliconforks 99 free(*s);
559 siliconforks 106 return -1;
560 siliconforks 99 }
561 siliconforks 106 int new_size;
562     if (size > INT_MAX / 2) {
563     new_size = INT_MAX;
564     }
565     else {
566     new_size = 2 * size;
567     }
568 siliconforks 99 char * new_s = realloc(*s, new_size);
569     if (new_s == NULL) {
570     free(*s);
571     return -1;
572     }
573     *s = new_s;
574     size = new_size;
575     va_copy(copy, a);
576     result = vsnprintf(*s, size, template, copy);
577     }
578     assert(result <= size - 1);
579     }
580    
581     return result;
582     }
583     #endif
584    
585     #ifndef HAVE_ASPRINTF
586     int asprintf(char ** s, const char * template, ...) {
587     va_list a;
588     va_start(a, template);
589     int result = vasprintf(s, template, a);
590     va_end(a);
591     return result;
592     }
593     #endif

  ViewVC Help
Powered by ViewVC 1.1.24