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