/[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 125 by siliconforks, Mon Jun 2 17:52:38 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 45  Line 51 
51    exit(EXIT_FAILURE);    exit(EXIT_FAILURE);
52  }  }
53    
54    size_t addst(size_t x, size_t y) {
55      if (SIZE_MAX - x < y) {
56        fatal("integer overflow");
57      }
58      return x + y;
59    }
60    
61    size_t mulst(size_t x, size_t y) {
62      if (x == 0 || y == 0) {
63        return 0;
64      }
65      if (SIZE_MAX / x < y) {
66        fatal("integer overflow");
67      }
68      return x * y;
69    }
70    
71  void * xmalloc(size_t size) {  void * xmalloc(size_t size) {
72    void * result = malloc(size);    void * result = malloc(size);
73    if (result == NULL) {    if (result == NULL) {
# Line 69  Line 92 
92    return result;    return result;
93  }  }
94    
95    char * xstrndup(const char * s, size_t size) {
96      char * result = strndup(s, size);
97      if (result == NULL) {
98        fatal("out of memory");
99      }
100      return result;
101    }
102    
103    int xasprintf(char ** s, const char * template, ...) {
104      va_list a;
105      va_start(a, template);
106      int result = vasprintf(s, template, a);
107      va_end(a);
108      if (result < 0) {
109        fatal("out of memory");
110      }
111      return result;
112    }
113    
114  char * xgetcwd(void) {  char * xgetcwd(void) {
115    char * result = getcwd(NULL, 0);    char * result = getcwd(NULL, 0);
116    if (result == NULL) {    if (result == NULL) {
# Line 157  Line 199 
199    }    }
200  }  }
201    
202    bool str_starts_with(const char * string, const char * prefix) {
203      const char * string_ptr = string;
204      const char * prefix_ptr = prefix;
205      while (*string_ptr != '\0' && *prefix_ptr != '\0') {
206        if (*string_ptr != *prefix_ptr) {
207          return false;
208        }
209        string_ptr++;
210        prefix_ptr++;
211      }
212      if (*string_ptr == '\0' && *prefix_ptr != '\0') {
213        return false;
214      }
215      return true;
216    }
217    
218    bool str_ends_with(const char * string, const char * suffix) {
219      size_t string_length = strlen(string);
220      size_t suffix_length = strlen(suffix);
221      if (string_length < suffix_length) {
222        return false;
223      }
224      return strcmp(string + string_length - suffix_length, suffix) == 0;
225    }
226    
227  char * make_path(const char * parent, const char * relative_path) {  char * make_path(const char * parent, const char * relative_path) {
228    size_t parent_length = strlen(parent);    size_t parent_length = strlen(parent);
229    size_t relative_path_length = strlen(relative_path);    size_t relative_path_length = strlen(relative_path);
230    char * result = xmalloc(parent_length + relative_path_length + 2);    size_t result_length = addst(parent_length, relative_path_length);
231      result_length = addst(result_length, 2);
232      char * result = xmalloc(result_length);
233    strcpy(result, parent);    strcpy(result, parent);
234    result[parent_length] = '/';    result[parent_length] = '/';
235    strcpy(result + parent_length + 1, relative_path);    strcpy(result + parent_length + 1, relative_path);
# Line 287  Line 356 
356    fclose(destination);    fclose(destination);
357  }  }
358    
359  int directory_is_empty(const char * directory) {  bool directory_is_empty(const char * directory) {
360      bool result = true;
361    DIR * dir = xopendir(directory);    DIR * dir = xopendir(directory);
   int num_entries = 0;  
362    struct dirent * e;    struct dirent * e;
363    while ((e = readdir(dir)) != NULL) {    while ((e = readdir(dir)) != NULL) {
364      if (strcmp(e->d_name, ".") != 0 &&      if (strcmp(e->d_name, ".") != 0 &&
365          strcmp(e->d_name, "..") != 0) {          strcmp(e->d_name, "..") != 0) {
366        num_entries++;        result = false;
367          break;
368      }      }
369    }    }
370    closedir(dir);    closedir(dir);
371    return num_entries == 0;    return result;
372  }  }
373    
374  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 416 
416      list = next;      list = next;
417    }    }
418  }  }
419    
420    #ifndef HAVE_STRNDUP
421    char * strndup(const char * s, size_t size) {
422      size_t length = strlen(s);
423      if (length > size) {
424        char * result = xmalloc(size + 1);
425        strncpy(result, s, size);
426        result[size] = '\0';
427        return result;
428      }
429      else {
430        char * result = xmalloc(length + 1);
431        strcpy(result, s);
432        return result;
433      }
434    }
435    #endif
436    
437    #ifndef HAVE_VASPRINTF
438    int vasprintf(char ** s, const char * template, va_list a) {
439      int size = 100;
440      *s = malloc(size);
441      if (*s == NULL) {
442        return -1;
443      }
444    
445      va_list copy;
446      va_copy(copy, a);
447      int result = vsnprintf(*s, size, template, copy);
448      if (result >= size) {
449        int new_size = result;
450        if (new_size == INT_MAX) {
451          free(*s);
452          return -1;
453        }
454        new_size++;
455        char * new_s = realloc(*s, new_size);
456        if (new_s == NULL) {
457          free(*s);
458          return -1;
459        }
460        *s = new_s;
461        size = new_size;
462        va_copy(copy, a);
463        result = vsnprintf(*s, size, template, copy);
464        assert(result == size - 1);
465      }
466      else if (result == -1) {
467        while (result == -1) {
468          if (size == INT_MAX) {
469            free(*s);
470            return -1;
471          }
472          int new_size;
473          if (size > INT_MAX / 2) {
474            new_size = INT_MAX;
475          }
476          else {
477            new_size = 2 * size;
478          }
479          char * new_s = realloc(*s, new_size);
480          if (new_s == NULL) {
481            free(*s);
482            return -1;
483          }
484          *s = new_s;
485          size = new_size;
486          va_copy(copy, a);
487          result = vsnprintf(*s, size, template, copy);
488        }
489        assert(result <= size - 1);
490      }
491    
492      return result;
493    }
494    #endif
495    
496    #ifndef HAVE_ASPRINTF
497    int asprintf(char ** s, const char * template, ...) {
498      va_list a;
499      va_start(a, template);
500      int result = vasprintf(s, template, a);
501      va_end(a);
502      return result;
503    }
504    #endif

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

  ViewVC Help
Powered by ViewVC 1.1.24