/[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 370 by siliconforks, Mon Oct 27 20:34:27 2008 UTC
# 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 version(void) {
75      printf("%s %s\n", program, VERSION);
76      printf("Character encoding support: ");
77    #if HAVE_ICONV
78      printf("iconv\n");
79    #elif HAVE_MULTIBYTETOWIDECHAR
80      printf("MultiByteToWideChar\n");
81    #else
82      printf("none\n");
83    #endif
84      exit(EXIT_SUCCESS);
85    }
86    
87    size_t addst(size_t x, size_t y) {
88      if (SIZE_MAX - x < y) {
89        fatal("integer overflow");
90      }
91      return x + y;
92    }
93    
94    size_t mulst(size_t x, size_t y) {
95      if (x == 0 || y == 0) {
96        return 0;
97      }
98      if (SIZE_MAX / x < y) {
99        fatal("integer overflow");
100      }
101      return x * y;
102    }
103    
104  void * xmalloc(size_t size) {  void * xmalloc(size_t size) {
105    void * result = malloc(size);    void * result = malloc(size);
106    if (result == NULL) {    if (result == NULL) {
# Line 70  Line 125 
125    return result;    return result;
126  }  }
127    
128    char * xstrndup(const char * s, size_t size) {
129      char * result = strndup(s, size);
130      if (result == NULL) {
131        fatal("out of memory");
132      }
133      return result;
134    }
135    
136    int xasprintf(char ** s, const char * template, ...) {
137      va_list a;
138      va_start(a, template);
139      int result = vasprintf(s, template, a);
140      va_end(a);
141      if (result < 0) {
142        fatal("out of memory");
143      }
144      return result;
145    }
146    
147  char * xgetcwd(void) {  char * xgetcwd(void) {
148    char * result = getcwd(NULL, 0);    char * result = getcwd(NULL, 0);
149    if (result == NULL) {    if (result == NULL) {
# Line 158  Line 232 
232    }    }
233  }  }
234    
235    bool str_starts_with(const char * string, const char * prefix) {
236      const char * string_ptr = string;
237      const char * prefix_ptr = prefix;
238      while (*string_ptr != '\0' && *prefix_ptr != '\0') {
239        if (*string_ptr != *prefix_ptr) {
240          return false;
241        }
242        string_ptr++;
243        prefix_ptr++;
244      }
245      if (*string_ptr == '\0' && *prefix_ptr != '\0') {
246        return false;
247      }
248      return true;
249    }
250    
251    bool str_ends_with(const char * string, const char * suffix) {
252      size_t string_length = strlen(string);
253      size_t suffix_length = strlen(suffix);
254      if (string_length < suffix_length) {
255        return false;
256      }
257      return strcmp(string + string_length - suffix_length, suffix) == 0;
258    }
259    
260  char * make_path(const char * parent, const char * relative_path) {  char * make_path(const char * parent, const char * relative_path) {
261    size_t parent_length = strlen(parent);    size_t parent_length = strlen(parent);
262    size_t relative_path_length = strlen(relative_path);    size_t relative_path_length = strlen(relative_path);
263    char * result = xmalloc(parent_length + relative_path_length + 2);    size_t result_length = addst(parent_length, relative_path_length);
264      result_length = addst(result_length, 2);
265      char * result = xmalloc(result_length);
266    strcpy(result, parent);    strcpy(result, parent);
267    result[parent_length] = '/';    result[parent_length] = '/';
268    strcpy(result + parent_length + 1, relative_path);    strcpy(result + parent_length + 1, relative_path);
# Line 284  Line 385 
385    
386    copy_stream(source, destination);    copy_stream(source, destination);
387    
388    #ifndef _WIN32
389      /* copy permissions */
390      struct stat buf;
391      if (fstat(fileno(source), &buf) == -1) {
392        fatal("cannot stat file: %s", source_file);
393      }
394      fchmod(fileno(destination), buf.st_mode);
395    #endif
396    
397    fclose(source);    fclose(source);
398    fclose(destination);    fclose(destination);
399  }  }
400    
401  int directory_is_empty(const char * directory) {  bool directory_is_empty(const char * directory) {
402      bool result = true;
403    DIR * dir = xopendir(directory);    DIR * dir = xopendir(directory);
   int num_entries = 0;  
404    struct dirent * e;    struct dirent * e;
405    while ((e = readdir(dir)) != NULL) {    while ((e = readdir(dir)) != NULL) {
406      if (strcmp(e->d_name, ".") != 0 &&      if (strcmp(e->d_name, ".") != 0 &&
407          strcmp(e->d_name, "..") != 0) {          strcmp(e->d_name, "..") != 0) {
408        num_entries++;        result = false;
409          break;
410      }      }
411    }    }
412    closedir(dir);    closedir(dir);
413    return num_entries == 0;    return result;
414  }  }
415    
416  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 436 
436        head = recursive_dir_list(root, entry_wrt_root, head);        head = recursive_dir_list(root, entry_wrt_root, head);
437        free(entry_wrt_root);        free(entry_wrt_root);
438      }      }
439    #ifndef _WIN32
440        else if (S_ISLNK(buf.st_mode)) {
441          /* check what it points to */
442          xstat(entry, &buf);
443          if (S_ISREG(buf.st_mode)) {
444            struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
445            p->name = entry_wrt_root;
446            p->next = head;
447            head = p;
448          }
449          else {
450            fatal("refusing to follow symbolic link: %s", entry);
451          }
452        }
453    #endif
454      else {      else {
455        fatal("unknown file type: %s", entry);        fatal("unknown file type: %s", entry);
456      }      }
# Line 348  Line 474 
474    }    }
475  }  }
476    
477    #ifndef HAVE_STRNDUP
478    char * strndup(const char * s, size_t size) {
479      size_t length = strlen(s);
480      if (length > size) {
481        char * result = xmalloc(size + 1);
482        strncpy(result, s, size);
483        result[size] = '\0';
484        return result;
485      }
486      else {
487        char * result = xmalloc(length + 1);
488        strcpy(result, s);
489        return result;
490      }
491    }
492    #endif
493    
494  #ifndef HAVE_VASPRINTF  #ifndef HAVE_VASPRINTF
495  int vasprintf(char ** s, const char * template, va_list a) {  int vasprintf(char ** s, const char * template, va_list a) {
496    int size = 100;    int size = 100;
# Line 360  Line 503 
503    va_copy(copy, a);    va_copy(copy, a);
504    int result = vsnprintf(*s, size, template, copy);    int result = vsnprintf(*s, size, template, copy);
505    if (result >= size) {    if (result >= size) {
     /* TODO: check for overflow? */  
506      int new_size = result;      int new_size = result;
507      if (new_size == INT_MAX) {      if (new_size == INT_MAX) {
508        free(*s);        free(*s);
509        return - 1;        return -1;
510      }      }
511      new_size++;      new_size++;
512      char * new_s = realloc(*s, new_size);      char * new_s = realloc(*s, new_size);
# Line 380  Line 522 
522    }    }
523    else if (result == -1) {    else if (result == -1) {
524      while (result == -1) {      while (result == -1) {
525        if (size > INT_MAX / 2) {        if (size == INT_MAX) {
526          free(*s);          free(*s);
527          return - 1;          return -1;
528          }
529          int new_size;
530          if (size > INT_MAX / 2) {
531            new_size = INT_MAX;
532          }
533          else {
534            new_size = 2 * size;
535        }        }
       int new_size = 2 * size;  
536        char * new_s = realloc(*s, new_size);        char * new_s = realloc(*s, new_size);
537        if (new_s == NULL) {        if (new_s == NULL) {
538          free(*s);          free(*s);
# Line 411  Line 559 
559    return result;    return result;
560  }  }
561  #endif  #endif
   

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

  ViewVC Help
Powered by ViewVC 1.1.24