Mercurial > hg-old > index.cgi
annotate src/util.c @ 302:eff969272fb9
resolve incomplete references done
author | lost |
---|---|
date | Wed, 21 Jan 2009 06:14:08 +0000 |
parents | 48945dac8178 |
children |
rev | line source |
---|---|
296
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
1 /* |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
2 util.c |
300 | 3 Copyright © 2009 William Astle |
296
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
4 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
5 This file is part of LWLINK. |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
6 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
7 LWLINK is free software: you can redistribute it and/or modify it under the |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
10 version. |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
11 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
15 more details. |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
16 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
19 */ |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
20 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
21 /* |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
22 Utility functions |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
23 */ |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
24 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
25 #define __util_c_seen__ |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
26 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
27 #include <malloc.h> |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
28 #include <stdio.h> |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
29 #include <stdlib.h> |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
30 #include <string.h> |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
31 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
32 #include "util.h" |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
33 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
34 void *lw_malloc(int size) |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
35 { |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
36 void *ptr; |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
37 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
38 ptr = malloc(size); |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
39 if (!ptr) |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
40 { |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
41 // bail out; memory allocation error |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
42 fprintf(stderr, "lw_malloc(): Memory allocation error\n"); |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
43 exit(1); |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
44 } |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
45 return ptr; |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
46 } |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
47 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
48 void *lw_realloc(void *optr, int size) |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
49 { |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
50 void *ptr; |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
51 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
52 if (size == 0) |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
53 { |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
54 lw_free(optr); |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
55 return; |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
56 } |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
57 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
58 ptr = realloc(optr, size); |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
59 if (!ptr) |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
60 { |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
61 fprintf(stderr, "lw_realloc(): memory allocation error\n"); |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
62 exit(1); |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
63 } |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
64 } |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
65 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
66 void lw_free(void *ptr) |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
67 { |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
68 if (ptr) |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
69 free(ptr); |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
70 } |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
71 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
72 char *lw_strdup(const char *s) |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
73 { |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
74 char *d; |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
75 |
298 | 76 if (!s) |
77 return NULL; | |
78 | |
296
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
79 d = strdup(s); |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
80 if (!d) |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
81 { |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
82 fprintf(stderr, "lw_strdup(): memory allocation error\n"); |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
83 exit(1); |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
84 } |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
85 |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
86 return d; |
14d835cf02d9
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
87 } |