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

Diff of /trunk/util.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 91 by siliconforks, Wed May 7 03:59:56 2008 UTC revision 450 by siliconforks, Tue Aug 11 18:39:03 2009 UTC
# Line 1  Line 1 
1  /*  /*
2      util.c - general purpose utility routines      util.c - general purpose utility routines
3      Copyright (C) 2007, 2008 siliconforks.com      Copyright (C) 2007, 2008, 2009 siliconforks.com
4    
5      This program is free software; you can redistribute it and/or modify      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      it under the terms of the GNU General Public License as published by
# Line 17  Line 17 
17      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.      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"  #include "util.h"
25    
26  #include <assert.h>  #include <assert.h>
27  #include <errno.h>  #include <errno.h>
28    #include <limits.h>
29  #include <stdarg.h>  #include <stdarg.h>
30  #include <stdio.h>  #include <stdio.h>
31    #include <stdint.h>
32  #include <string.h>  #include <string.h>
33  #include <strings.h>  #include <strings.h>
34    
# Line 41  Line 47 
47    vfprintf(stderr, format, ap);    vfprintf(stderr, format, ap);
48    va_end(ap);    va_end(ap);
49    fputc('\n', stderr);    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);    fprintf(stderr, "Try `%s --help' for more information.\n", program);
61    exit(EXIT_FAILURE);    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      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      exit(EXIT_SUCCESS);
98    }
99    
100    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  void * xmalloc(size_t size) {  void * xmalloc(size_t size) {
118    void * result = malloc(size);    void * result = malloc(size);
119    if (result == NULL) {    if (result == NULL) {
# Line 69  Line 138 
138    return result;    return result;
139  }  }
140    
141    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  char * xgetcwd(void) {  char * xgetcwd(void) {
161    char * result = getcwd(NULL, 0);    char * result = getcwd(NULL, 0);
162    if (result == NULL) {    if (result == NULL) {
# Line 157  Line 245 
245    }    }
246  }  }
247    
248    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    static int is_slash(char c) {
274    #ifdef _WIN32
275      return c == '/' || c == '\\';
276    #else
277      return c == '/';
278    #endif
279    }
280    
281  char * make_path(const char * parent, const char * relative_path) {  char * make_path(const char * parent, const char * relative_path) {
282    size_t parent_length = strlen(parent);    size_t parent_length = strlen(parent);
283    size_t relative_path_length = strlen(relative_path);    size_t relative_path_length = strlen(relative_path);
284    char * result = xmalloc(parent_length + relative_path_length + 2);    size_t result_length = addst(parent_length, relative_path_length);
285      int parent_ends_with_slash = parent_length > 0 && is_slash(parent[parent_length - 1]);
286      if (parent_ends_with_slash) {
287        result_length = addst(result_length, 1);
288      }
289      else {
290        result_length = addst(result_length, 2);
291      }
292      char * result = xmalloc(result_length);
293    strcpy(result, parent);    strcpy(result, parent);
294    result[parent_length] = '/';    char * p = result + parent_length;
295    strcpy(result + parent_length + 1, relative_path);    if (! parent_ends_with_slash) {
296        *p = '/';
297        ++p;
298      }
299      strcpy(p, relative_path);
300    return result;    return result;
301  }  }
302    
# Line 283  Line 416 
416    
417    copy_stream(source, destination);    copy_stream(source, destination);
418    
419    #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    fclose(source);    fclose(source);
429    fclose(destination);    fclose(destination);
430  }  }
431    
432  int directory_is_empty(const char * directory) {  bool directory_is_empty(const char * directory) {
433      bool result = true;
434    DIR * dir = xopendir(directory);    DIR * dir = xopendir(directory);
   int num_entries = 0;  
435    struct dirent * e;    struct dirent * e;
436    while ((e = readdir(dir)) != NULL) {    while ((e = readdir(dir)) != NULL) {
437      if (strcmp(e->d_name, ".") != 0 &&      if (strcmp(e->d_name, ".") != 0 &&
438          strcmp(e->d_name, "..") != 0) {          strcmp(e->d_name, "..") != 0) {
439        num_entries++;        result = false;
440          break;
441      }      }
442    }    }
443    closedir(dir);    closedir(dir);
444    return num_entries == 0;    return result;
445  }  }
446    
447  static struct DirListEntry * recursive_dir_list(const char * root, const char * directory_wrt_root, struct DirListEntry * head) {  static struct DirListEntry * recursive_dir_list(const char * root, const char * directory_wrt_root, struct DirListEntry * head) {
# Line 324  Line 467 
467        head = recursive_dir_list(root, entry_wrt_root, head);        head = recursive_dir_list(root, entry_wrt_root, head);
468        free(entry_wrt_root);        free(entry_wrt_root);
469      }      }
470    #ifndef _WIN32
471        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    #endif
485      else {      else {
486        fatal("unknown file type: %s", entry);        fatal("unknown file type: %s", entry);
487      }      }
# Line 346  Line 504 
504      list = next;      list = next;
505    }    }
506  }  }
507    
508    #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    }
523    #endif
524    
525    #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          return -1;
541        }
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          if (size == INT_MAX) {
557            free(*s);
558            return -1;
559          }
560          int new_size;
561          if (size > INT_MAX / 2) {
562            new_size = INT_MAX;
563          }
564          else {
565            new_size = 2 * size;
566          }
567          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

Legend:
Removed from v.91  
changed lines
  Added in v.450

  ViewVC Help
Powered by ViewVC 1.1.24