Commit | Line | Data |
378cc40b |
1 | /* $Header: util.c,v 2.0 88/06/05 00:16:07 root Exp $ |
8d063cd8 |
2 | * |
3 | * $Log: util.c,v $ |
378cc40b |
4 | * Revision 2.0 88/06/05 00:16:07 root |
5 | * Baseline version 2.0. |
8d063cd8 |
6 | * |
7 | */ |
8 | |
9 | #include <stdio.h> |
10 | |
11 | #include "handy.h" |
12 | #include "EXTERN.h" |
13 | #include "a2p.h" |
14 | #include "INTERN.h" |
15 | #include "util.h" |
16 | |
17 | #define FLUSH |
18 | #define MEM_SIZE unsigned int |
19 | |
20 | static char nomem[] = "Out of memory!\n"; |
21 | |
22 | /* paranoid version of malloc */ |
23 | |
24 | static int an = 0; |
25 | |
26 | char * |
27 | safemalloc(size) |
28 | MEM_SIZE size; |
29 | { |
30 | char *ptr; |
31 | char *malloc(); |
32 | |
33 | ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */ |
34 | #ifdef DEBUGGING |
35 | if (debug & 128) |
36 | fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size); |
37 | #endif |
38 | if (ptr != Nullch) |
39 | return ptr; |
40 | else { |
41 | fputs(nomem,stdout) FLUSH; |
42 | exit(1); |
43 | } |
44 | /*NOTREACHED*/ |
45 | } |
46 | |
47 | /* paranoid version of realloc */ |
48 | |
49 | char * |
50 | saferealloc(where,size) |
51 | char *where; |
52 | MEM_SIZE size; |
53 | { |
54 | char *ptr; |
55 | char *realloc(); |
56 | |
57 | ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */ |
58 | #ifdef DEBUGGING |
59 | if (debug & 128) { |
60 | fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++); |
61 | fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size); |
62 | } |
63 | #endif |
64 | if (ptr != Nullch) |
65 | return ptr; |
66 | else { |
67 | fputs(nomem,stdout) FLUSH; |
68 | exit(1); |
69 | } |
70 | /*NOTREACHED*/ |
71 | } |
72 | |
73 | /* safe version of free */ |
74 | |
75 | safefree(where) |
76 | char *where; |
77 | { |
78 | #ifdef DEBUGGING |
79 | if (debug & 128) |
80 | fprintf(stderr,"0x%x: (%05d) free\n",where,an++); |
81 | #endif |
82 | free(where); |
83 | } |
84 | |
85 | /* safe version of string copy */ |
86 | |
87 | char * |
88 | safecpy(to,from,len) |
89 | char *to; |
90 | register char *from; |
91 | register int len; |
92 | { |
93 | register char *dest = to; |
94 | |
95 | if (from != Nullch) |
96 | for (len--; len && (*dest++ = *from++); len--) ; |
97 | *dest = '\0'; |
98 | return to; |
99 | } |
100 | |
101 | #ifdef undef |
102 | /* safe version of string concatenate, with \n deletion and space padding */ |
103 | |
104 | char * |
105 | safecat(to,from,len) |
106 | char *to; |
107 | register char *from; |
108 | register int len; |
109 | { |
110 | register char *dest = to; |
111 | |
112 | len--; /* leave room for null */ |
113 | if (*dest) { |
114 | while (len && *dest++) len--; |
115 | if (len) { |
116 | len--; |
117 | *(dest-1) = ' '; |
118 | } |
119 | } |
120 | if (from != Nullch) |
121 | while (len && (*dest++ = *from++)) len--; |
122 | if (len) |
123 | dest--; |
124 | if (*(dest-1) == '\n') |
125 | dest--; |
126 | *dest = '\0'; |
127 | return to; |
128 | } |
129 | #endif |
130 | |
131 | /* copy a string up to some (non-backslashed) delimiter, if any */ |
132 | |
133 | char * |
134 | cpytill(to,from,delim) |
135 | register char *to, *from; |
136 | register int delim; |
137 | { |
138 | for (; *from; from++,to++) { |
378cc40b |
139 | if (*from == '\\') { |
140 | if (from[1] == delim) |
141 | from++; |
142 | else if (from[1] == '\\') |
143 | *to++ = *from++; |
144 | } |
8d063cd8 |
145 | else if (*from == delim) |
146 | break; |
147 | *to = *from; |
148 | } |
149 | *to = '\0'; |
150 | return from; |
151 | } |
152 | |
378cc40b |
153 | |
8d063cd8 |
154 | char * |
155 | cpy2(to,from,delim) |
156 | register char *to, *from; |
157 | register int delim; |
158 | { |
159 | for (; *from; from++,to++) { |
378cc40b |
160 | if (*from == '\\') |
8d063cd8 |
161 | *to++ = *from++; |
162 | else if (*from == '$') |
163 | *to++ = '\\'; |
164 | else if (*from == delim) |
165 | break; |
166 | *to = *from; |
167 | } |
168 | *to = '\0'; |
169 | return from; |
170 | } |
171 | |
172 | /* return ptr to little string in big string, NULL if not found */ |
173 | |
174 | char * |
175 | instr(big, little) |
176 | char *big, *little; |
177 | |
178 | { |
179 | register char *t, *s, *x; |
180 | |
181 | for (t = big; *t; t++) { |
182 | for (x=t,s=little; *s; x++,s++) { |
183 | if (!*x) |
184 | return Nullch; |
185 | if (*s != *x) |
186 | break; |
187 | } |
188 | if (!*s) |
189 | return t; |
190 | } |
191 | return Nullch; |
192 | } |
193 | |
194 | /* copy a string to a safe spot */ |
195 | |
196 | char * |
197 | savestr(str) |
198 | char *str; |
199 | { |
200 | register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1)); |
201 | |
202 | (void)strcpy(newaddr,str); |
203 | return newaddr; |
204 | } |
205 | |
206 | /* grow a static string to at least a certain length */ |
207 | |
208 | void |
209 | growstr(strptr,curlen,newlen) |
210 | char **strptr; |
211 | int *curlen; |
212 | int newlen; |
213 | { |
214 | if (newlen > *curlen) { /* need more room? */ |
215 | if (*curlen) |
216 | *strptr = saferealloc(*strptr,(MEM_SIZE)newlen); |
217 | else |
218 | *strptr = safemalloc((MEM_SIZE)newlen); |
219 | *curlen = newlen; |
220 | } |
221 | } |
222 | |
223 | /*VARARGS1*/ |
224 | fatal(pat,a1,a2,a3,a4) |
225 | char *pat; |
226 | { |
227 | fprintf(stderr,pat,a1,a2,a3,a4); |
228 | exit(1); |
229 | } |
230 | |
231 | static bool firstsetenv = TRUE; |
232 | extern char **environ; |
233 | |
234 | void |
235 | setenv(nam,val) |
236 | char *nam, *val; |
237 | { |
238 | register int i=envix(nam); /* where does it go? */ |
239 | |
240 | if (!environ[i]) { /* does not exist yet */ |
241 | if (firstsetenv) { /* need we copy environment? */ |
242 | int j; |
243 | #ifndef lint |
244 | char **tmpenv = (char**) /* point our wand at memory */ |
245 | safemalloc((i+2) * sizeof(char*)); |
246 | #else |
247 | char **tmpenv = Null(char **); |
248 | #endif /* lint */ |
249 | |
250 | firstsetenv = FALSE; |
251 | for (j=0; j<i; j++) /* copy environment */ |
252 | tmpenv[j] = environ[j]; |
253 | environ = tmpenv; /* tell exec where it is now */ |
254 | } |
255 | #ifndef lint |
256 | else |
257 | environ = (char**) saferealloc((char*) environ, |
258 | (i+2) * sizeof(char*)); |
259 | /* just expand it a bit */ |
260 | #endif /* lint */ |
261 | environ[i+1] = Nullch; /* make sure it's null terminated */ |
262 | } |
263 | environ[i] = safemalloc(strlen(nam) + strlen(val) + 2); |
264 | /* this may or may not be in */ |
265 | /* the old environ structure */ |
266 | sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */ |
267 | } |
268 | |
269 | int |
270 | envix(nam) |
271 | char *nam; |
272 | { |
273 | register int i, len = strlen(nam); |
274 | |
275 | for (i = 0; environ[i]; i++) { |
276 | if (strnEQ(environ[i],nam,len) && environ[i][len] == '=') |
277 | break; /* strnEQ must come first to avoid */ |
278 | } /* potential SEGV's */ |
279 | return i; |
280 | } |