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

Diff of /trunk/util.c

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

revision 99 by siliconforks, Wed May 14 00:01:21 2008 UTC revision 449 by siliconforks, Tue Aug 11 17:23:20 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>
# Line 24  Line 28 
28  #include <limits.h>  #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 42  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 70  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 158  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);    assert(parent_length > 0);
285      assert(relative_path_length > 0);
286      size_t result_length = addst(parent_length, relative_path_length);
287      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      char * result = xmalloc(result_length);
295    strcpy(result, parent);    strcpy(result, parent);
296    result[parent_length] = '/';    char * p = result + parent_length;
297    strcpy(result + parent_length + 1, relative_path);    if (! parent_ends_with_slash) {
298        *p = '/';
299        ++p;
300      }
301      strcpy(p, relative_path);
302    return result;    return result;
303  }  }
304    
# Line 284  Line 418 
418    
419    copy_stream(source, destination);    copy_stream(source, destination);
420    
421    #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    fclose(source);    fclose(source);
431    fclose(destination);    fclose(destination);
432  }  }
433    
434  int directory_is_empty(const char * directory) {  bool directory_is_empty(const char * directory) {
435      bool result = true;
436    DIR * dir = xopendir(directory);    DIR * dir = xopendir(directory);
   int num_entries = 0;  
437    struct dirent * e;    struct dirent * e;
438    while ((e = readdir(dir)) != NULL) {    while ((e = readdir(dir)) != NULL) {
439      if (strcmp(e->d_name, ".") != 0 &&      if (strcmp(e->d_name, ".") != 0 &&
440          strcmp(e->d_name, "..") != 0) {          strcmp(e->d_name, "..") != 0) {
441        num_entries++;        result = false;
442          break;
443      }      }
444    }    }
445    closedir(dir);    closedir(dir);
446    return num_entries == 0;    return result;
447  }  }
448    
449  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 325  Line 469 
469        head = recursive_dir_list(root, entry_wrt_root, head);        head = recursive_dir_list(root, entry_wrt_root, head);
470        free(entry_wrt_root);        free(entry_wrt_root);
471      }      }
472    #ifndef _WIN32
473        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    #endif
487      else {      else {
488        fatal("unknown file type: %s", entry);        fatal("unknown file type: %s", entry);
489      }      }
# Line 348  Line 507 
507    }    }
508  }  }
509    
510    #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    }
525    #endif
526    
527  #ifndef HAVE_VASPRINTF  #ifndef HAVE_VASPRINTF
528  int vasprintf(char ** s, const char * template, va_list a) {  int vasprintf(char ** s, const char * template, va_list a) {
529    int size = 100;    int size = 100;
# Line 360  Line 536 
536    va_copy(copy, a);    va_copy(copy, a);
537    int result = vsnprintf(*s, size, template, copy);    int result = vsnprintf(*s, size, template, copy);
538    if (result >= size) {    if (result >= size) {
     /* TODO: check for overflow? */  
539      int new_size = result;      int new_size = result;
540      if (new_size == INT_MAX) {      if (new_size == INT_MAX) {
541        free(*s);        free(*s);
542        return - 1;        return -1;
543      }      }
544      new_size++;      new_size++;
545      char * new_s = realloc(*s, new_size);      char * new_s = realloc(*s, new_size);
# Line 380  Line 555 
555    }    }
556    else if (result == -1) {    else if (result == -1) {
557      while (result == -1) {      while (result == -1) {
558        if (size > INT_MAX / 2) {        if (size == INT_MAX) {
559          free(*s);          free(*s);
560          return - 1;          return -1;
561          }
562          int new_size;
563          if (size > INT_MAX / 2) {
564            new_size = INT_MAX;
565          }
566          else {
567            new_size = 2 * size;
568        }        }
       int new_size = 2 * size;  
569        char * new_s = realloc(*s, new_size);        char * new_s = realloc(*s, new_size);
570        if (new_s == NULL) {        if (new_s == NULL) {
571          free(*s);          free(*s);
# Line 411  Line 592 
592    return result;    return result;
593  }  }
594  #endif  #endif
   

Legend:
Removed from v.99  
changed lines
  Added in v.449

  ViewVC Help
Powered by ViewVC 1.1.24