annotate lwar/util.c @ 273:88bbffa404ea 2.5 2.5

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