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