Mercurial > repos > miller-lab > genome_diversity
comparison genome_diversity/src/lib.c @ 17:a3af29edcce2
Uploaded Miller Lab Devshed version a51c894f5bed
author | miller-lab |
---|---|
date | Fri, 28 Sep 2012 11:57:18 -0400 |
parents | 2c498d40ecde |
children |
comparison
equal
deleted
inserted
replaced
16:be0e2223c531 | 17:a3af29edcce2 |
---|---|
1 // lib.c -- a little library of C procudures | |
2 | |
3 #include "lib.h" | |
4 | |
5 char *argv0; | |
6 | |
7 /* print_argv0 ---------------------------------------- print name of program */ | |
8 void print_argv0(void) | |
9 { | |
10 if (argv0) { | |
11 char *p = strrchr(argv0, '/'); | |
12 (void)fprintf(stderr, "%s: ", p ? p+1 : argv0); | |
13 } | |
14 } | |
15 | |
16 /* fatal ---------------------------------------------- print message and die */ | |
17 void fatal(const char *msg) | |
18 { | |
19 fatalf("%s", msg); | |
20 } | |
21 | |
22 /* fatalf --------------------------------- format message, print it, and die */ | |
23 void fatalf(const char *fmt, ...) | |
24 { | |
25 va_list ap; | |
26 va_start(ap, fmt); | |
27 fflush(stdout); | |
28 print_argv0(); | |
29 (void)vfprintf(stderr, fmt, ap); | |
30 (void)fputc('\n', stderr); | |
31 va_end(ap); | |
32 exit(1); | |
33 } | |
34 | |
35 /* ckopen -------------------------------------- open file; check for success */ | |
36 FILE *ckopen(const char *name, const char *mode) | |
37 { | |
38 FILE *fp; | |
39 | |
40 if ((fp = fopen(name, mode)) == NULL) | |
41 fatalf("Cannot open %s.", name); | |
42 return fp; | |
43 } | |
44 | |
45 /* ckalloc -------------------------------- allocate space; check for success */ | |
46 void *ckalloc(size_t amount) | |
47 { | |
48 void *p; | |
49 | |
50 if ((long)amount < 0) /* was "<= 0" -CR */ | |
51 fatal("ckalloc: request for negative space."); | |
52 if (amount == 0) | |
53 amount = 1; /* ANSI portability hack */ | |
54 if ((p = malloc(amount)) == NULL) | |
55 fatalf("Ran out of memory trying to allocate %lu.", | |
56 (unsigned long)amount); | |
57 return p; | |
58 } | |
59 | |
60 /* same_string ------------------ determine whether two strings are identical */ | |
61 bool same_string(const char *s, const char *t) | |
62 { | |
63 return (strcmp(s, t) == 0); | |
64 } | |
65 | |
66 /* copy_string ---------------------- save string s somewhere; return address */ | |
67 char *copy_string(const char *s) | |
68 { | |
69 char *p = ckalloc(strlen(s)+1); /* +1 to hold '\0' */ | |
70 return strcpy(p, s); | |
71 } |