/[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 364 by siliconforks, Sun Oct 26 21:08:56 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 275  Line 375 
375    
376    copy_stream(source, destination);    copy_stream(source, destination);
377    
378    #ifndef _WIN32
379      /* copy permissions */
380      struct stat buf;
381      if (fstat(fileno(source), &buf) == -1) {
382        fatal("cannot stat file: %s", source_file);
383      }
384      fchmod(fileno(destination), buf.st_mode);
385    #endif
386    
387    fclose(source);    fclose(source);
388    fclose(destination);    fclose(destination);
389  }  }
390    
391  int directory_is_empty(const char * directory) {  bool directory_is_empty(const char * directory) {
392      bool result = true;
393    DIR * dir = xopendir(directory);    DIR * dir = xopendir(directory);
   int num_entries = 0;  
394    struct dirent * e;    struct dirent * e;
395    while ((e = readdir(dir)) != NULL) {    while ((e = readdir(dir)) != NULL) {
396      if (strcmp(e->d_name, ".") != 0 &&      if (strcmp(e->d_name, ".") != 0 &&
397          strcmp(e->d_name, "..") != 0) {          strcmp(e->d_name, "..") != 0) {
398        num_entries++;        result = false;
399          break;
400      }      }
401    }    }
402    closedir(dir);    closedir(dir);
403    return num_entries == 0;    return result;
404  }  }
405    
406  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 316  Line 426 
426        head = recursive_dir_list(root, entry_wrt_root, head);        head = recursive_dir_list(root, entry_wrt_root, head);
427        free(entry_wrt_root);        free(entry_wrt_root);
428      }      }
429    #ifndef _WIN32
430        else if (S_ISLNK(buf.st_mode)) {
431          /* check what it points to */
432          xstat(entry, &buf);
433          if (S_ISREG(buf.st_mode)) {
434            struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
435            p->name = entry_wrt_root;
436            p->next = head;
437            head = p;
438          }
439          else {
440            fatal("refusing to follow symbolic link: %s", entry);
441          }
442        }
443    #endif
444      else {      else {
445        fatal("unknown file type: %s", entry);        fatal("unknown file type: %s", entry);
446      }      }
# Line 338  Line 463 
463      list = next;      list = next;
464    }    }
465  }  }
466    
467    #ifndef HAVE_STRNDUP
468    char * strndup(const char * s, size_t size) {
469      size_t length = strlen(s);
470      if (length > size) {
471        char * result = xmalloc(size + 1);
472        strncpy(result, s, size);
473        result[size] = '\0';
474        return result;
475      }
476      else {
477        char * result = xmalloc(length + 1);
478        strcpy(result, s);
479        return result;
480      }
481    }
482    #endif
483    
484    #ifndef HAVE_VASPRINTF
485    int vasprintf(char ** s, const char * template, va_list a) {
486      int size = 100;
487      *s = malloc(size);
488      if (*s == NULL) {
489        return -1;
490      }
491    
492      va_list copy;
493      va_copy(copy, a);
494      int result = vsnprintf(*s, size, template, copy);
495      if (result >= size) {
496        int new_size = result;
497        if (new_size == INT_MAX) {
498          free(*s);
499          return -1;
500        }
501        new_size++;
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        assert(result == size - 1);
512      }
513      else if (result == -1) {
514        while (result == -1) {
515          if (size == INT_MAX) {
516            free(*s);
517            return -1;
518          }
519          int new_size;
520          if (size > INT_MAX / 2) {
521            new_size = INT_MAX;
522          }
523          else {
524            new_size = 2 * size;
525          }
526          char * new_s = realloc(*s, new_size);
527          if (new_s == NULL) {
528            free(*s);
529            return -1;
530          }
531          *s = new_s;
532          size = new_size;
533          va_copy(copy, a);
534          result = vsnprintf(*s, size, template, copy);
535        }
536        assert(result <= size - 1);
537      }
538    
539      return result;
540    }
541    #endif
542    
543    #ifndef HAVE_ASPRINTF
544    int asprintf(char ** s, const char * template, ...) {
545      va_list a;
546      va_start(a, template);
547      int result = vasprintf(s, template, a);
548      va_end(a);
549      return result;
550    }
551    #endif

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

  ViewVC Help
Powered by ViewVC 1.1.24