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

Diff of /trunk/util.c

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

revision 311 by siliconforks, Mon Oct 13 17:51:29 2008 UTC revision 449 by siliconforks, Tue Aug 11 17:23:20 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 61  Line 61 
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) {  void version(void) {
84    printf("%s %s\n", program, VERSION);    printf("%s %s\n", program, VERSION);
85    printf("Character encoding support: ");    printf("Character encoding support: ");
# Line 71  Line 90 
90  #else  #else
91    printf("none\n");    printf("none\n");
92  #endif  #endif
93      printf("Copyright (C) 2009 siliconforks.com\n");
94      printf("License GPLv2+: GNU GPL version 2 or later <http://siliconforks.com/jscoverage/license.html>\n");
95      printf("This is free software: you are free to change and redistribute it.\n");
96      printf("There is NO WARRANTY, to the extent permitted by law.\n");
97    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
98  }  }
99    
# Line 247  Line 270 
270    return strcmp(string + string_length - suffix_length, suffix) == 0;    return strcmp(string + string_length - suffix_length, suffix) == 0;
271  }  }
272    
273    static int is_slash(char c) {
274    #ifdef _WIN32
275      return c == '/' || c == '\\';
276    #else
277      return c == '/';
278    #endif
279    }
280    
281  char * make_path(const char * parent, const char * relative_path) {  char * make_path(const char * parent, const char * relative_path) {
282    size_t parent_length = strlen(parent);    size_t parent_length = strlen(parent);
283    size_t relative_path_length = strlen(relative_path);    size_t relative_path_length = strlen(relative_path);
284      assert(parent_length > 0);
285      assert(relative_path_length > 0);
286    size_t result_length = addst(parent_length, relative_path_length);    size_t result_length = addst(parent_length, relative_path_length);
287    result_length = addst(result_length, 2);    int parent_ends_with_slash = is_slash(parent[parent_length - 1]);
288      if (parent_ends_with_slash) {
289        result_length = addst(result_length, 1);
290      }
291      else {
292        result_length = addst(result_length, 2);
293      }
294    char * result = xmalloc(result_length);    char * result = xmalloc(result_length);
295    strcpy(result, parent);    strcpy(result, parent);
296    result[parent_length] = '/';    char * p = result + parent_length;
297    strcpy(result + parent_length + 1, relative_path);    if (! parent_ends_with_slash) {
298        *p = '/';
299        ++p;
300      }
301      strcpy(p, relative_path);
302    return result;    return result;
303  }  }
304    
# Line 375  Line 418 
418    
419    copy_stream(source, destination);    copy_stream(source, destination);
420    
421    #ifndef _WIN32
422      /* copy permissions */
423      struct stat buf;
424      if (fstat(fileno(source), &buf) == -1) {
425        fatal("cannot stat file: %s", source_file);
426      }
427      fchmod(fileno(destination), buf.st_mode);
428    #endif
429    
430    fclose(source);    fclose(source);
431    fclose(destination);    fclose(destination);
432  }  }
# Line 417  Line 469 
469        head = recursive_dir_list(root, entry_wrt_root, head);        head = recursive_dir_list(root, entry_wrt_root, head);
470        free(entry_wrt_root);        free(entry_wrt_root);
471      }      }
472    #ifndef _WIN32
473        else if (S_ISLNK(buf.st_mode)) {
474          /* check what it points to */
475          xstat(entry, &buf);
476          if (S_ISREG(buf.st_mode)) {
477            struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
478            p->name = entry_wrt_root;
479            p->next = head;
480            head = p;
481          }
482          else {
483            fatal("refusing to follow symbolic link: %s", entry);
484          }
485        }
486    #endif
487      else {      else {
488        fatal("unknown file type: %s", entry);        fatal("unknown file type: %s", entry);
489      }      }

Legend:
Removed from v.311  
changed lines
  Added in v.449

  ViewVC Help
Powered by ViewVC 1.1.24