/[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 427 by siliconforks, Wed Feb 18 16:08:33 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      exit(EXIT_SUCCESS);
94    }
95    
96    size_t addst(size_t x, size_t y) {
97      if (SIZE_MAX - x < y) {
98        fatal("integer overflow");
99      }
100      return x + y;
101    }
102    
103    size_t mulst(size_t x, size_t y) {
104      if (x == 0 || y == 0) {
105        return 0;
106      }
107      if (SIZE_MAX / x < y) {
108        fatal("integer overflow");
109      }
110      return x * y;
111    }
112    
113  void * xmalloc(size_t size) {  void * xmalloc(size_t size) {
114    void * result = malloc(size);    void * result = malloc(size);
115    if (result == NULL) {    if (result == NULL) {
# Line 70  Line 134 
134    return result;    return result;
135  }  }
136    
137    char * xstrndup(const char * s, size_t size) {
138      char * result = strndup(s, size);
139      if (result == NULL) {
140        fatal("out of memory");
141      }
142      return result;
143    }
144    
145    int xasprintf(char ** s, const char * template, ...) {
146      va_list a;
147      va_start(a, template);
148      int result = vasprintf(s, template, a);
149      va_end(a);
150      if (result < 0) {
151        fatal("out of memory");
152      }
153      return result;
154    }
155    
156  char * xgetcwd(void) {  char * xgetcwd(void) {
157    char * result = getcwd(NULL, 0);    char * result = getcwd(NULL, 0);
158    if (result == NULL) {    if (result == NULL) {
# Line 158  Line 241 
241    }    }
242  }  }
243    
244    bool str_starts_with(const char * string, const char * prefix) {
245      const char * string_ptr = string;
246      const char * prefix_ptr = prefix;
247      while (*string_ptr != '\0' && *prefix_ptr != '\0') {
248        if (*string_ptr != *prefix_ptr) {
249          return false;
250        }
251        string_ptr++;
252        prefix_ptr++;
253      }
254      if (*string_ptr == '\0' && *prefix_ptr != '\0') {
255        return false;
256      }
257      return true;
258    }
259    
260    bool str_ends_with(const char * string, const char * suffix) {
261      size_t string_length = strlen(string);
262      size_t suffix_length = strlen(suffix);
263      if (string_length < suffix_length) {
264        return false;
265      }
266      return strcmp(string + string_length - suffix_length, suffix) == 0;
267    }
268    
269  char * make_path(const char * parent, const char * relative_path) {  char * make_path(const char * parent, const char * relative_path) {
270    size_t parent_length = strlen(parent);    size_t parent_length = strlen(parent);
271    size_t relative_path_length = strlen(relative_path);    size_t relative_path_length = strlen(relative_path);
272    char * result = xmalloc(parent_length + relative_path_length + 2);    size_t result_length = addst(parent_length, relative_path_length);
273      result_length = addst(result_length, 2);
274      char * result = xmalloc(result_length);
275    strcpy(result, parent);    strcpy(result, parent);
276    result[parent_length] = '/';    result[parent_length] = '/';
277    strcpy(result + parent_length + 1, relative_path);    strcpy(result + parent_length + 1, relative_path);
# Line 284  Line 394 
394    
395    copy_stream(source, destination);    copy_stream(source, destination);
396    
397    #ifndef _WIN32
398      /* copy permissions */
399      struct stat buf;
400      if (fstat(fileno(source), &buf) == -1) {
401        fatal("cannot stat file: %s", source_file);
402      }
403      fchmod(fileno(destination), buf.st_mode);
404    #endif
405    
406    fclose(source);    fclose(source);
407    fclose(destination);    fclose(destination);
408  }  }
409    
410  int directory_is_empty(const char * directory) {  bool directory_is_empty(const char * directory) {
411      bool result = true;
412    DIR * dir = xopendir(directory);    DIR * dir = xopendir(directory);
   int num_entries = 0;  
413    struct dirent * e;    struct dirent * e;
414    while ((e = readdir(dir)) != NULL) {    while ((e = readdir(dir)) != NULL) {
415      if (strcmp(e->d_name, ".") != 0 &&      if (strcmp(e->d_name, ".") != 0 &&
416          strcmp(e->d_name, "..") != 0) {          strcmp(e->d_name, "..") != 0) {
417        num_entries++;        result = false;
418          break;
419      }      }
420    }    }
421    closedir(dir);    closedir(dir);
422    return num_entries == 0;    return result;
423  }  }
424    
425  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 445 
445        head = recursive_dir_list(root, entry_wrt_root, head);        head = recursive_dir_list(root, entry_wrt_root, head);
446        free(entry_wrt_root);        free(entry_wrt_root);
447      }      }
448    #ifndef _WIN32
449        else if (S_ISLNK(buf.st_mode)) {
450          /* check what it points to */
451          xstat(entry, &buf);
452          if (S_ISREG(buf.st_mode)) {
453            struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
454            p->name = entry_wrt_root;
455            p->next = head;
456            head = p;
457          }
458          else {
459            fatal("refusing to follow symbolic link: %s", entry);
460          }
461        }
462    #endif
463      else {      else {
464        fatal("unknown file type: %s", entry);        fatal("unknown file type: %s", entry);
465      }      }
# Line 348  Line 483 
483    }    }
484  }  }
485    
486    #ifndef HAVE_STRNDUP
487    char * strndup(const char * s, size_t size) {
488      size_t length = strlen(s);
489      if (length > size) {
490        char * result = xmalloc(size + 1);
491        strncpy(result, s, size);
492        result[size] = '\0';
493        return result;
494      }
495      else {
496        char * result = xmalloc(length + 1);
497        strcpy(result, s);
498        return result;
499      }
500    }
501    #endif
502    
503  #ifndef HAVE_VASPRINTF  #ifndef HAVE_VASPRINTF
504  int vasprintf(char ** s, const char * template, va_list a) {  int vasprintf(char ** s, const char * template, va_list a) {
505    int size = 100;    int size = 100;
# Line 360  Line 512 
512    va_copy(copy, a);    va_copy(copy, a);
513    int result = vsnprintf(*s, size, template, copy);    int result = vsnprintf(*s, size, template, copy);
514    if (result >= size) {    if (result >= size) {
     /* TODO: check for overflow? */  
515      int new_size = result;      int new_size = result;
516      if (new_size == INT_MAX) {      if (new_size == INT_MAX) {
517        free(*s);        free(*s);
518        return - 1;        return -1;
519      }      }
520      new_size++;      new_size++;
521      char * new_s = realloc(*s, new_size);      char * new_s = realloc(*s, new_size);
# Line 380  Line 531 
531    }    }
532    else if (result == -1) {    else if (result == -1) {
533      while (result == -1) {      while (result == -1) {
534        if (size > INT_MAX / 2) {        if (size == INT_MAX) {
535          free(*s);          free(*s);
536          return - 1;          return -1;
537          }
538          int new_size;
539          if (size > INT_MAX / 2) {
540            new_size = INT_MAX;
541          }
542          else {
543            new_size = 2 * size;
544        }        }
       int new_size = 2 * size;  
545        char * new_s = realloc(*s, new_size);        char * new_s = realloc(*s, new_size);
546        if (new_s == NULL) {        if (new_s == NULL) {
547          free(*s);          free(*s);
# Line 411  Line 568 
568    return result;    return result;
569  }  }
570  #endif  #endif
   

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

  ViewVC Help
Powered by ViewVC 1.1.24