/[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 359 by siliconforks, Sun Oct 26 05:09:51 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        else if (S_ISLNK(buf.st_mode)) {
430          /* check what it points to */
431          xstat(entry, &buf);
432          if (S_ISREG(buf.st_mode)) {
433            struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
434            p->name = entry_wrt_root;
435            p->next = head;
436            head = p;
437          }
438          else {
439            fatal("refusing to follow symbolic link: %s", entry);
440          }
441        }
442      else {      else {
443        fatal("unknown file type: %s", entry);        fatal("unknown file type: %s", entry);
444      }      }
# Line 338  Line 461 
461      list = next;      list = next;
462    }    }
463  }  }
464    
465    #ifndef HAVE_STRNDUP
466    char * strndup(const char * s, size_t size) {
467      size_t length = strlen(s);
468      if (length > size) {
469        char * result = xmalloc(size + 1);
470        strncpy(result, s, size);
471        result[size] = '\0';
472        return result;
473      }
474      else {
475        char * result = xmalloc(length + 1);
476        strcpy(result, s);
477        return result;
478      }
479    }
480    #endif
481    
482    #ifndef HAVE_VASPRINTF
483    int vasprintf(char ** s, const char * template, va_list a) {
484      int size = 100;
485      *s = malloc(size);
486      if (*s == NULL) {
487        return -1;
488      }
489    
490      va_list copy;
491      va_copy(copy, a);
492      int result = vsnprintf(*s, size, template, copy);
493      if (result >= size) {
494        int new_size = result;
495        if (new_size == INT_MAX) {
496          free(*s);
497          return -1;
498        }
499        new_size++;
500        char * new_s = realloc(*s, new_size);
501        if (new_s == NULL) {
502          free(*s);
503          return -1;
504        }
505        *s = new_s;
506        size = new_size;
507        va_copy(copy, a);
508        result = vsnprintf(*s, size, template, copy);
509        assert(result == size - 1);
510      }
511      else if (result == -1) {
512        while (result == -1) {
513          if (size == INT_MAX) {
514            free(*s);
515            return -1;
516          }
517          int new_size;
518          if (size > INT_MAX / 2) {
519            new_size = INT_MAX;
520          }
521          else {
522            new_size = 2 * size;
523          }
524          char * new_s = realloc(*s, new_size);
525          if (new_s == NULL) {
526            free(*s);
527            return -1;
528          }
529          *s = new_s;
530          size = new_size;
531          va_copy(copy, a);
532          result = vsnprintf(*s, size, template, copy);
533        }
534        assert(result <= size - 1);
535      }
536    
537      return result;
538    }
539    #endif
540    
541    #ifndef HAVE_ASPRINTF
542    int asprintf(char ** s, const char * template, ...) {
543      va_list a;
544      va_start(a, template);
545      int result = vasprintf(s, template, a);
546      va_end(a);
547      return result;
548    }
549    #endif

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

  ViewVC Help
Powered by ViewVC 1.1.24