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

Diff of /trunk/util.c

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

revision 106 by siliconforks, Sat May 31 07:37:39 2008 UTC revision 448 by siliconforks, Tue Aug 11 17:16:32 2009 UTC
# Line 1  Line 1 
1  /*  /*
2      util.c - general purpose utility routines      util.c - general purpose utility routines
3      Copyright (C) 2007, 2008 siliconforks.com      Copyright (C) 2007, 2008, 2009 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 47  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 fatal_source(const char * source_file, unsigned int line_number, const char * format, ...) {
65      fprintf(stderr, "%s:%s:%u: ", program, source_file, line_number);
66      va_list ap;
67      va_start(ap, format);
68      vfprintf(stderr, format, ap);
69      va_end(ap);
70      fputc('\n', stderr);
71      exit(EXIT_FAILURE);
72    }
73    
74    void warn_source(const char * source_file, unsigned int line_number, const char * format, ...) {
75      fprintf(stderr, "%s:%s:%u: ", program, source_file, line_number);
76      va_list ap;
77      va_start(ap, format);
78      vfprintf(stderr, format, ap);
79      va_end(ap);
80      fputc('\n', stderr);
81    }
82    
83    void version(void) {
84      printf("%s %s\n", program, VERSION);
85      printf("Character encoding support: ");
86    #if HAVE_ICONV
87      printf("iconv\n");
88    #elif HAVE_MULTIBYTETOWIDECHAR
89      printf("MultiByteToWideChar\n");
90    #else
91      printf("none\n");
92    #endif
93      exit(EXIT_SUCCESS);
94    }
95    
96  size_t addst(size_t x, size_t y) {  size_t addst(size_t x, size_t y) {
97    if (SIZE_MAX - x < y) {    if (SIZE_MAX - x < y) {
98      fatal("integer overflow");      fatal("integer overflow");
# Line 224  Line 266 
266    return strcmp(string + string_length - suffix_length, suffix) == 0;    return strcmp(string + string_length - suffix_length, suffix) == 0;
267  }  }
268    
269    static int is_slash(char c) {
270    #ifdef _WIN32
271      return c == '/' || c == '\\';
272    #else
273      return c == '/';
274    #endif
275    }
276    
277  char * make_path(const char * parent, const char * relative_path) {  char * make_path(const char * parent, const char * relative_path) {
278    size_t parent_length = strlen(parent);    size_t parent_length = strlen(parent);
279    size_t relative_path_length = strlen(relative_path);    size_t relative_path_length = strlen(relative_path);
280      assert(parent_length > 0);
281      assert(relative_path_length > 0);
282    size_t result_length = addst(parent_length, relative_path_length);    size_t result_length = addst(parent_length, relative_path_length);
283    result_length = addst(result_length, 2);    int parent_ends_with_slash = is_slash(parent[parent_length - 1]);
284      if (parent_ends_with_slash) {
285        result_length = addst(result_length, 1);
286      }
287      else {
288        result_length = addst(result_length, 2);
289      }
290    char * result = xmalloc(result_length);    char * result = xmalloc(result_length);
291    strcpy(result, parent);    strcpy(result, parent);
292    result[parent_length] = '/';    char * p = result + parent_length;
293    strcpy(result + parent_length + 1, relative_path);    if (! parent_ends_with_slash) {
294        *p = '/';
295        ++p;
296      }
297      strcpy(p, relative_path);
298    return result;    return result;
299  }  }
300    
# Line 352  Line 414 
414    
415    copy_stream(source, destination);    copy_stream(source, destination);
416    
417    #ifndef _WIN32
418      /* copy permissions */
419      struct stat buf;
420      if (fstat(fileno(source), &buf) == -1) {
421        fatal("cannot stat file: %s", source_file);
422      }
423      fchmod(fileno(destination), buf.st_mode);
424    #endif
425    
426    fclose(source);    fclose(source);
427    fclose(destination);    fclose(destination);
428  }  }
# Line 394  Line 465 
465        head = recursive_dir_list(root, entry_wrt_root, head);        head = recursive_dir_list(root, entry_wrt_root, head);
466        free(entry_wrt_root);        free(entry_wrt_root);
467      }      }
468    #ifndef _WIN32
469        else if (S_ISLNK(buf.st_mode)) {
470          /* check what it points to */
471          xstat(entry, &buf);
472          if (S_ISREG(buf.st_mode)) {
473            struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
474            p->name = entry_wrt_root;
475            p->next = head;
476            head = p;
477          }
478          else {
479            fatal("refusing to follow symbolic link: %s", entry);
480          }
481        }
482    #endif
483      else {      else {
484        fatal("unknown file type: %s", entry);        fatal("unknown file type: %s", entry);
485      }      }
# Line 417  Line 503 
503    }    }
504  }  }
505    
506  #ifndef HAVE_CLOSESOCKET  #ifndef HAVE_STRNDUP
507  int closesocket(int s) {  char * strndup(const char * s, size_t size) {
508    return close(s);    size_t length = strlen(s);
509      if (length > size) {
510        char * result = xmalloc(size + 1);
511        strncpy(result, s, size);
512        result[size] = '\0';
513        return result;
514      }
515      else {
516        char * result = xmalloc(length + 1);
517        strcpy(result, s);
518        return result;
519      }
520  }  }
521  #endif  #endif
522    

Legend:
Removed from v.106  
changed lines
  Added in v.448

  ViewVC Help
Powered by ViewVC 1.1.24