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

Diff of /trunk/util.c

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

revision 2 by siliconforks, Wed Aug 1 13:51:53 2007 UTC revision 311 by siliconforks, Mon Oct 13 17:51:29 2008 UTC
# Line 1  Line 1 
1  /*  /*
2      util.c - general purpose utility routines      util.c - general purpose utility routines
3      Copyright (C) 2007 siliconforks.com      Copyright (C) 2007, 2008 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 version(void) {
65      printf("%s %s\n", program, VERSION);
66      printf("Character encoding support: ");
67    #if HAVE_ICONV
68      printf("iconv\n");
69    #elif HAVE_MULTIBYTETOWIDECHAR
70      printf("MultiByteToWideChar\n");
71    #else
72      printf("none\n");
73    #endif
74      exit(EXIT_SUCCESS);
75    }
76    
77    size_t addst(size_t x, size_t y) {
78      if (SIZE_MAX - x < y) {
79        fatal("integer overflow");
80      }
81      return x + y;
82    }
83    
84    size_t mulst(size_t x, size_t y) {
85      if (x == 0 || y == 0) {
86        return 0;
87      }
88      if (SIZE_MAX / x < y) {
89        fatal("integer overflow");
90      }
91      return x * y;
92    }
93    
94  void * xmalloc(size_t size) {  void * xmalloc(size_t size) {
95    void * result = malloc(size);    void * result = malloc(size);
96    if (result == NULL) {    if (result == NULL) {
# Line 53  Line 99 
99    return result;    return result;
100  }  }
101    
102    void * xrealloc(void * p, size_t size) {
103      void * result = realloc(p, size);
104      if (result == NULL) {
105        fatal("out of memory");
106      }
107      return result;
108    }
109    
110  char * xstrdup(const char * s) {  char * xstrdup(const char * s) {
111    char * result = strdup(s);    char * result = strdup(s);
112    if (result == NULL) {    if (result == NULL) {
# Line 61  Line 115 
115    return result;    return result;
116  }  }
117    
118    char * xstrndup(const char * s, size_t size) {
119      char * result = strndup(s, size);
120      if (result == NULL) {
121        fatal("out of memory");
122      }
123      return result;
124    }
125    
126    int xasprintf(char ** s, const char * template, ...) {
127      va_list a;
128      va_start(a, template);
129      int result = vasprintf(s, template, a);
130      va_end(a);
131      if (result < 0) {
132        fatal("out of memory");
133      }
134      return result;
135    }
136    
137  char * xgetcwd(void) {  char * xgetcwd(void) {
138    char * result = getcwd(NULL, 0);    char * result = getcwd(NULL, 0);
139    if (result == NULL) {    if (result == NULL) {
# Line 149  Line 222 
222    }    }
223  }  }
224    
225    bool str_starts_with(const char * string, const char * prefix) {
226      const char * string_ptr = string;
227      const char * prefix_ptr = prefix;
228      while (*string_ptr != '\0' && *prefix_ptr != '\0') {
229        if (*string_ptr != *prefix_ptr) {
230          return false;
231        }
232        string_ptr++;
233        prefix_ptr++;
234      }
235      if (*string_ptr == '\0' && *prefix_ptr != '\0') {
236        return false;
237      }
238      return true;
239    }
240    
241    bool str_ends_with(const char * string, const char * suffix) {
242      size_t string_length = strlen(string);
243      size_t suffix_length = strlen(suffix);
244      if (string_length < suffix_length) {
245        return false;
246      }
247      return strcmp(string + string_length - suffix_length, suffix) == 0;
248    }
249    
250  char * make_path(const char * parent, const char * relative_path) {  char * make_path(const char * parent, const char * relative_path) {
251    size_t parent_length = strlen(parent);    size_t parent_length = strlen(parent);
252    size_t relative_path_length = strlen(relative_path);    size_t relative_path_length = strlen(relative_path);
253    char * result = xmalloc(parent_length + relative_path_length + 2);    size_t result_length = addst(parent_length, relative_path_length);
254      result_length = addst(result_length, 2);
255      char * result = xmalloc(result_length);
256    strcpy(result, parent);    strcpy(result, parent);
257    result[parent_length] = '/';    result[parent_length] = '/';
258    strcpy(result + parent_length + 1, relative_path);    strcpy(result + parent_length + 1, relative_path);
# Line 279  Line 379 
379    fclose(destination);    fclose(destination);
380  }  }
381    
382  int directory_is_empty(const char * directory) {  bool directory_is_empty(const char * directory) {
383      bool result = true;
384    DIR * dir = xopendir(directory);    DIR * dir = xopendir(directory);
   int num_entries = 0;  
385    struct dirent * e;    struct dirent * e;
386    while ((e = readdir(dir)) != NULL) {    while ((e = readdir(dir)) != NULL) {
387      if (strcmp(e->d_name, ".") != 0 &&      if (strcmp(e->d_name, ".") != 0 &&
388          strcmp(e->d_name, "..") != 0) {          strcmp(e->d_name, "..") != 0) {
389        num_entries++;        result = false;
390          break;
391      }      }
392    }    }
393    closedir(dir);    closedir(dir);
394    return num_entries == 0;    return result;
395  }  }
396    
397  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 338  Line 439 
439      list = next;      list = next;
440    }    }
441  }  }
442    
443    #ifndef HAVE_STRNDUP
444    char * strndup(const char * s, size_t size) {
445      size_t length = strlen(s);
446      if (length > size) {
447        char * result = xmalloc(size + 1);
448        strncpy(result, s, size);
449        result[size] = '\0';
450        return result;
451      }
452      else {
453        char * result = xmalloc(length + 1);
454        strcpy(result, s);
455        return result;
456      }
457    }
458    #endif
459    
460    #ifndef HAVE_VASPRINTF
461    int vasprintf(char ** s, const char * template, va_list a) {
462      int size = 100;
463      *s = malloc(size);
464      if (*s == NULL) {
465        return -1;
466      }
467    
468      va_list copy;
469      va_copy(copy, a);
470      int result = vsnprintf(*s, size, template, copy);
471      if (result >= size) {
472        int new_size = result;
473        if (new_size == INT_MAX) {
474          free(*s);
475          return -1;
476        }
477        new_size++;
478        char * new_s = realloc(*s, new_size);
479        if (new_s == NULL) {
480          free(*s);
481          return -1;
482        }
483        *s = new_s;
484        size = new_size;
485        va_copy(copy, a);
486        result = vsnprintf(*s, size, template, copy);
487        assert(result == size - 1);
488      }
489      else if (result == -1) {
490        while (result == -1) {
491          if (size == INT_MAX) {
492            free(*s);
493            return -1;
494          }
495          int new_size;
496          if (size > INT_MAX / 2) {
497            new_size = INT_MAX;
498          }
499          else {
500            new_size = 2 * size;
501          }
502          char * new_s = realloc(*s, new_size);
503          if (new_s == NULL) {
504            free(*s);
505            return -1;
506          }
507          *s = new_s;
508          size = new_size;
509          va_copy(copy, a);
510          result = vsnprintf(*s, size, template, copy);
511        }
512        assert(result <= size - 1);
513      }
514    
515      return result;
516    }
517    #endif
518    
519    #ifndef HAVE_ASPRINTF
520    int asprintf(char ** s, const char * template, ...) {
521      va_list a;
522      va_start(a, template);
523      int result = vasprintf(s, template, a);
524      va_end(a);
525      return result;
526    }
527    #endif

Legend:
Removed from v.2  
changed lines
  Added in v.311

  ViewVC Help
Powered by ViewVC 1.1.24