/[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 191 by siliconforks, Tue Sep 23 03:49:29 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>
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    size_t addst(size_t x, size_t y) {
65      if (SIZE_MAX - x < y) {
66        fatal("integer overflow");
67      }
68      return x + y;
69    }
70    
71    size_t mulst(size_t x, size_t y) {
72      if (x == 0 || y == 0) {
73        return 0;
74      }
75      if (SIZE_MAX / x < y) {
76        fatal("integer overflow");
77      }
78      return x * y;
79    }
80    
81  void * xmalloc(size_t size) {  void * xmalloc(size_t size) {
82    void * result = malloc(size);    void * result = malloc(size);
83    if (result == NULL) {    if (result == NULL) {
# Line 69  Line 102 
102    return result;    return result;
103  }  }
104    
105    char * xstrndup(const char * s, size_t size) {
106      char * result = strndup(s, size);
107      if (result == NULL) {
108        fatal("out of memory");
109      }
110      return result;
111    }
112    
113    int xasprintf(char ** s, const char * template, ...) {
114      va_list a;
115      va_start(a, template);
116      int result = vasprintf(s, template, a);
117      va_end(a);
118      if (result < 0) {
119        fatal("out of memory");
120      }
121      return result;
122    }
123    
124  char * xgetcwd(void) {  char * xgetcwd(void) {
125    char * result = getcwd(NULL, 0);    char * result = getcwd(NULL, 0);
126    if (result == NULL) {    if (result == NULL) {
# Line 157  Line 209 
209    }    }
210  }  }
211    
212    bool str_starts_with(const char * string, const char * prefix) {
213      const char * string_ptr = string;
214      const char * prefix_ptr = prefix;
215      while (*string_ptr != '\0' && *prefix_ptr != '\0') {
216        if (*string_ptr != *prefix_ptr) {
217          return false;
218        }
219        string_ptr++;
220        prefix_ptr++;
221      }
222      if (*string_ptr == '\0' && *prefix_ptr != '\0') {
223        return false;
224      }
225      return true;
226    }
227    
228    bool str_ends_with(const char * string, const char * suffix) {
229      size_t string_length = strlen(string);
230      size_t suffix_length = strlen(suffix);
231      if (string_length < suffix_length) {
232        return false;
233      }
234      return strcmp(string + string_length - suffix_length, suffix) == 0;
235    }
236    
237  char * make_path(const char * parent, const char * relative_path) {  char * make_path(const char * parent, const char * relative_path) {
238    size_t parent_length = strlen(parent);    size_t parent_length = strlen(parent);
239    size_t relative_path_length = strlen(relative_path);    size_t relative_path_length = strlen(relative_path);
240    char * result = xmalloc(parent_length + relative_path_length + 2);    size_t result_length = addst(parent_length, relative_path_length);
241      result_length = addst(result_length, 2);
242      char * result = xmalloc(result_length);
243    strcpy(result, parent);    strcpy(result, parent);
244    result[parent_length] = '/';    result[parent_length] = '/';
245    strcpy(result + parent_length + 1, relative_path);    strcpy(result + parent_length + 1, relative_path);
# Line 287  Line 366 
366    fclose(destination);    fclose(destination);
367  }  }
368    
369  int directory_is_empty(const char * directory) {  bool directory_is_empty(const char * directory) {
370      bool result = true;
371    DIR * dir = xopendir(directory);    DIR * dir = xopendir(directory);
   int num_entries = 0;  
372    struct dirent * e;    struct dirent * e;
373    while ((e = readdir(dir)) != NULL) {    while ((e = readdir(dir)) != NULL) {
374      if (strcmp(e->d_name, ".") != 0 &&      if (strcmp(e->d_name, ".") != 0 &&
375          strcmp(e->d_name, "..") != 0) {          strcmp(e->d_name, "..") != 0) {
376        num_entries++;        result = false;
377          break;
378      }      }
379    }    }
380    closedir(dir);    closedir(dir);
381    return num_entries == 0;    return result;
382  }  }
383    
384  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 346  Line 426 
426      list = next;      list = next;
427    }    }
428  }  }
429    
430    #ifndef HAVE_STRNDUP
431    char * strndup(const char * s, size_t size) {
432      size_t length = strlen(s);
433      if (length > size) {
434        char * result = xmalloc(size + 1);
435        strncpy(result, s, size);
436        result[size] = '\0';
437        return result;
438      }
439      else {
440        char * result = xmalloc(length + 1);
441        strcpy(result, s);
442        return result;
443      }
444    }
445    #endif
446    
447    #ifndef HAVE_VASPRINTF
448    int vasprintf(char ** s, const char * template, va_list a) {
449      int size = 100;
450      *s = malloc(size);
451      if (*s == NULL) {
452        return -1;
453      }
454    
455      va_list copy;
456      va_copy(copy, a);
457      int result = vsnprintf(*s, size, template, copy);
458      if (result >= size) {
459        int new_size = result;
460        if (new_size == INT_MAX) {
461          free(*s);
462          return -1;
463        }
464        new_size++;
465        char * new_s = realloc(*s, new_size);
466        if (new_s == NULL) {
467          free(*s);
468          return -1;
469        }
470        *s = new_s;
471        size = new_size;
472        va_copy(copy, a);
473        result = vsnprintf(*s, size, template, copy);
474        assert(result == size - 1);
475      }
476      else if (result == -1) {
477        while (result == -1) {
478          if (size == INT_MAX) {
479            free(*s);
480            return -1;
481          }
482          int new_size;
483          if (size > INT_MAX / 2) {
484            new_size = INT_MAX;
485          }
486          else {
487            new_size = 2 * size;
488          }
489          char * new_s = realloc(*s, new_size);
490          if (new_s == NULL) {
491            free(*s);
492            return -1;
493          }
494          *s = new_s;
495          size = new_size;
496          va_copy(copy, a);
497          result = vsnprintf(*s, size, template, copy);
498        }
499        assert(result <= size - 1);
500      }
501    
502      return result;
503    }
504    #endif
505    
506    #ifndef HAVE_ASPRINTF
507    int asprintf(char ** s, const char * template, ...) {
508      va_list a;
509      va_start(a, template);
510      int result = vasprintf(s, template, a);
511      va_end(a);
512      return result;
513    }
514    #endif

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

  ViewVC Help
Powered by ViewVC 1.1.24