Commit | Line | Data |
8665f9e4 |
1 | /* util.c |
a687059c |
2 | * |
4bb101f2 |
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999, |
8665f9e4 |
4 | * 2000, 2001, 2005 by Larry Wall and others |
a687059c |
5 | * |
d48672a2 |
6 | * You may distribute under the terms of either the GNU General Public |
7 | * License or the Artistic License, as specified in the README file. |
8d063cd8 |
8 | */ |
9 | |
8d063cd8 |
10 | #include "EXTERN.h" |
11 | #include "a2p.h" |
12 | #include "INTERN.h" |
13 | #include "util.h" |
14 | |
17c3b450 |
15 | #include <stdarg.h> |
8d063cd8 |
16 | #define FLUSH |
8d063cd8 |
17 | |
8665f9e4 |
18 | static const char nomem[] = "Out of memory!\n"; |
8d063cd8 |
19 | |
20 | /* paranoid version of malloc */ |
21 | |
8d063cd8 |
22 | |
75f92628 |
23 | Malloc_t |
e9cb6d14 |
24 | safemalloc(MEM_SIZE size) |
8d063cd8 |
25 | { |
6d82b384 |
26 | Malloc_t ptr; |
8d063cd8 |
27 | |
6d82b384 |
28 | /* malloc(0) is NASTY on some systems */ |
29 | ptr = malloc(size ? size : 1); |
8d063cd8 |
30 | #ifdef DEBUGGING |
31 | if (debug & 128) |
fb73857a |
32 | fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",(unsigned long)ptr, |
33 | an++,(long)size); |
8d063cd8 |
34 | #endif |
35 | if (ptr != Nullch) |
36 | return ptr; |
37 | else { |
38 | fputs(nomem,stdout) FLUSH; |
39 | exit(1); |
40 | } |
41 | /*NOTREACHED*/ |
2c5424a7 |
42 | return 0; |
8d063cd8 |
43 | } |
44 | |
45 | /* paranoid version of realloc */ |
46 | |
75f92628 |
47 | Malloc_t |
e9cb6d14 |
48 | saferealloc(Malloc_t where, MEM_SIZE size) |
8d063cd8 |
49 | { |
6d82b384 |
50 | Malloc_t ptr; |
8d063cd8 |
51 | |
6d82b384 |
52 | /* realloc(0) is NASTY on some systems */ |
53 | ptr = realloc(where, size ? size : 1); |
8d063cd8 |
54 | #ifdef DEBUGGING |
55 | if (debug & 128) { |
68dc0745 |
56 | fprintf(stderr,"0x%lx: (%05d) rfree\n",(unsigned long)where,an++); |
fb73857a |
57 | fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",(unsigned long)ptr,an++,(long)size); |
8d063cd8 |
58 | } |
59 | #endif |
60 | if (ptr != Nullch) |
61 | return ptr; |
62 | else { |
63 | fputs(nomem,stdout) FLUSH; |
64 | exit(1); |
65 | } |
66 | /*NOTREACHED*/ |
2c5424a7 |
67 | return 0; |
8d063cd8 |
68 | } |
69 | |
70 | /* safe version of free */ |
71 | |
6d82b384 |
72 | Free_t |
e9cb6d14 |
73 | safefree(Malloc_t where) |
8d063cd8 |
74 | { |
75 | #ifdef DEBUGGING |
76 | if (debug & 128) |
68dc0745 |
77 | fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++); |
8d063cd8 |
78 | #endif |
79 | free(where); |
80 | } |
81 | |
8d063cd8 |
82 | /* copy a string up to some (non-backslashed) delimiter, if any */ |
83 | |
84 | char * |
f0f333f4 |
85 | cpytill(register char *to, register char *from, register int delim) |
8d063cd8 |
86 | { |
87 | for (; *from; from++,to++) { |
378cc40b |
88 | if (*from == '\\') { |
89 | if (from[1] == delim) |
90 | from++; |
91 | else if (from[1] == '\\') |
92 | *to++ = *from++; |
93 | } |
8d063cd8 |
94 | else if (*from == delim) |
95 | break; |
96 | *to = *from; |
97 | } |
98 | *to = '\0'; |
99 | return from; |
100 | } |
101 | |
378cc40b |
102 | |
8d063cd8 |
103 | char * |
f0f333f4 |
104 | cpy2(register char *to, register char *from, register int delim) |
8d063cd8 |
105 | { |
106 | for (; *from; from++,to++) { |
378cc40b |
107 | if (*from == '\\') |
8d063cd8 |
108 | *to++ = *from++; |
109 | else if (*from == '$') |
110 | *to++ = '\\'; |
111 | else if (*from == delim) |
112 | break; |
113 | *to = *from; |
114 | } |
115 | *to = '\0'; |
116 | return from; |
117 | } |
118 | |
119 | /* return ptr to little string in big string, NULL if not found */ |
120 | |
121 | char * |
aab39148 |
122 | instr(char *big, const char *little) |
8d063cd8 |
123 | { |
aab39148 |
124 | register char *t, *x; |
125 | register const char *s; |
8d063cd8 |
126 | |
127 | for (t = big; *t; t++) { |
128 | for (x=t,s=little; *s; x++,s++) { |
129 | if (!*x) |
130 | return Nullch; |
131 | if (*s != *x) |
132 | break; |
133 | } |
134 | if (!*s) |
135 | return t; |
136 | } |
137 | return Nullch; |
138 | } |
139 | |
140 | /* copy a string to a safe spot */ |
141 | |
142 | char * |
aab39148 |
143 | savestr(const char *str) |
8d063cd8 |
144 | { |
8665f9e4 |
145 | register char * const newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1)); |
8d063cd8 |
146 | |
147 | (void)strcpy(newaddr,str); |
148 | return newaddr; |
149 | } |
150 | |
151 | /* grow a static string to at least a certain length */ |
152 | |
153 | void |
f0f333f4 |
154 | growstr(char **strptr, int *curlen, int newlen) |
8d063cd8 |
155 | { |
156 | if (newlen > *curlen) { /* need more room? */ |
157 | if (*curlen) |
f0f333f4 |
158 | *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen); |
8d063cd8 |
159 | else |
f0f333f4 |
160 | *strptr = (char *) safemalloc((MEM_SIZE)newlen); |
8d063cd8 |
161 | *curlen = newlen; |
162 | } |
163 | } |
164 | |
e50aee73 |
165 | void |
b7787f18 |
166 | fatal(const char *pat,...) |
8d063cd8 |
167 | { |
17c3b450 |
168 | #if defined(HAS_VPRINTF) |
55497cff |
169 | va_list args; |
170 | |
171 | va_start(args, pat); |
172 | vfprintf(stderr,pat,args); |
304b2fa9 |
173 | va_end(args); |
55497cff |
174 | #else |
8d063cd8 |
175 | fprintf(stderr,pat,a1,a2,a3,a4); |
55497cff |
176 | #endif |
8d063cd8 |
177 | exit(1); |
178 | } |
179 | |
f72d1791 |
180 | #if defined(DARWIN) |
8f1f23e8 |
181 | __private_extern__ /* warn() conflicts with libc */ |
182 | #endif |
9c8d0b29 |
183 | void |
b7787f18 |
184 | warn(const char *pat,...) |
a687059c |
185 | { |
17c3b450 |
186 | #if defined(HAS_VPRINTF) |
55497cff |
187 | va_list args; |
188 | |
189 | va_start(args, pat); |
190 | vfprintf(stderr,pat,args); |
304b2fa9 |
191 | va_end(args); |
55497cff |
192 | #else |
a687059c |
193 | fprintf(stderr,pat,a1,a2,a3,a4); |
55497cff |
194 | #endif |
a687059c |
195 | } |
196 | |