1 /* $Header: util.c,v 2.0 88/06/05 00:15:11 root Exp $
4 * Revision 2.0 88/06/05 00:15:11 root
5 * Baseline version 2.0.
14 static char nomem[] = "Out of memory!\n";
16 /* paranoid version of malloc */
29 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
32 fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size);
37 fputs(nomem,stdout) FLUSH;
43 /* paranoid version of realloc */
46 saferealloc(where,size)
54 fatal("Null realloc");
55 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
58 fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
59 fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size);
65 fputs(nomem,stdout) FLUSH;
71 /* safe version of free */
78 fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
86 /* safe version of string copy */
94 register char *dest = to;
97 for (len--; len && (*dest++ = *from++); len--) ;
104 /* safe version of string concatenate, with \n deletion and space padding */
112 register char *dest = to;
114 len--; /* leave room for null */
116 while (len && *dest++) len--;
123 while (len && (*dest++ = *from++)) len--;
126 if (*(dest-1) == '\n')
133 /* copy a string up to some (non-backslashed) delimiter, if any */
136 cpytill(to,from,delim)
137 register char *to, *from;
140 for (; *from; from++,to++) {
142 if (from[1] == delim)
144 else if (from[1] == '\\')
147 else if (*from == delim)
155 /* return ptr to little string in big string, NULL if not found */
156 /* This routine was donated by Corey Satten. */
161 register char *little;
163 register char *s, *x;
164 register int first = *little++;
171 for (x=big,s=little; *s; /**/ ) {
191 register char *table;
193 register int len = str->str_cur;
195 str_grow(str,len+128);
198 for (i = 1; i < 128; i++) {
205 if (table[*s] == len)
209 str->str_pok |= 2; /* deep magic */
213 static unsigned char freq[] = {
214 1, 2, 84, 151, 154, 155, 156, 157,
215 165, 246, 250, 3, 158, 7, 18, 29,
216 40, 51, 62, 73, 85, 96, 107, 118,
217 129, 140, 147, 148, 149, 150, 152, 153,
218 255, 182, 224, 205, 174, 176, 180, 217,
219 233, 232, 236, 187, 235, 228, 234, 226,
220 222, 219, 211, 195, 188, 193, 185, 184,
221 191, 183, 201, 229, 181, 220, 194, 162,
222 163, 208, 186, 202, 200, 218, 198, 179,
223 178, 214, 166, 170, 207, 199, 209, 206,
224 204, 160, 212, 216, 215, 192, 175, 173,
225 243, 172, 161, 190, 203, 189, 164, 230,
226 167, 248, 227, 244, 242, 255, 241, 231,
227 240, 253, 169, 210, 245, 237, 249, 247,
228 239, 168, 252, 251, 254, 238, 223, 221,
229 213, 225, 177, 197, 171, 196, 159, 4,
230 5, 6, 8, 9, 10, 11, 12, 13,
231 14, 15, 16, 17, 19, 20, 21, 22,
232 23, 24, 25, 26, 27, 28, 30, 31,
233 32, 33, 34, 35, 36, 37, 38, 39,
234 41, 42, 43, 44, 45, 46, 47, 48,
235 49, 50, 52, 53, 54, 55, 56, 57,
236 58, 59, 60, 61, 63, 64, 65, 66,
237 67, 68, 69, 70, 71, 72, 74, 75,
238 76, 77, 78, 79, 80, 81, 82, 83,
239 86, 87, 88, 89, 90, 91, 92, 93,
240 94, 95, 97, 98, 99, 100, 101, 102,
241 103, 104, 105, 106, 108, 109, 110, 111,
242 112, 113, 114, 115, 116, 117, 119, 120,
243 121, 122, 123, 124, 125, 126, 127, 128,
244 130, 131, 132, 133, 134, 135, 136, 137,
245 138, 139, 141, 142, 143, 144, 145, 146
253 register char *table;
255 register int len = str->str_cur;
259 str_grow(str,len+128);
260 table = str->str_ptr + len; /* actually points at final '\0' */
262 for (i = 1; i < 128; i++) {
266 while (s >= str->str_ptr) {
269 if (table[*s] == len)
273 str->str_pok |= 2; /* deep magic */
275 s = str->str_ptr; /* deeper magic */
276 for (i = 0; i < len; i++) {
277 if (freq[s[i]] < frequency) {
279 frequency = freq[s[i]];
282 str->str_rare = s[rarest];
283 str->str_prev = rarest;
286 fprintf(stderr,"rarest char %c at %d\n",str->str_rare, str->str_prev);
292 bminstr(big, biglen, littlestr)
299 register char *little = littlestr->str_ptr;
300 int littlelen = littlestr->str_cur;
301 register char *table = little + littlelen;
303 s = big + biglen - littlelen;
305 if (tmp = table[*s]) {
309 if (strnEQ(s,little,littlelen))
319 fbminstr(big, bigend, littlestr)
321 register char *bigend;
326 register int littlelen;
327 register char *little;
328 register char *table;
330 register char *oldlittle;
334 if (littlestr->str_pok != 3)
335 return instr(big,littlestr->str_ptr);
337 littlelen = littlestr->str_cur;
338 table = littlestr->str_ptr + littlelen;
339 s = big + --littlelen;
340 oldlittle = little = table - 1;
343 if (tmp = table[*s]) {
347 tmp = littlelen; /* less expensive than calling strncmp() */
350 if (*--s == *--little)
352 s = olds + 1; /* here we pay the price for failure */
354 if (s < bigend) /* fake up continue to outer loop */
365 screaminstr(bigstr, littlestr)
369 register char *s, *x;
370 register char *big = bigstr->str_ptr;
372 register int previous;
374 register char *little;
376 if ((pos = screamfirst[littlestr->str_rare]) < 0)
378 little = littlestr->str_ptr;
380 previous = littlestr->str_prev;
382 while (pos < previous) {
383 if (!(pos += screamnext[pos]))
387 if (big[pos] != first)
389 for (x=big+pos+1,s=little; *s; /**/ ) {
399 } while (pos += screamnext[pos]);
403 /* copy a string to a safe spot */
409 register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
411 (void)strcpy(newaddr,str);
415 /* grow a static string to at least a certain length */
418 growstr(strptr,curlen,newlen)
423 if (newlen > *curlen) { /* need more room? */
425 *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
427 *strptr = safemalloc((MEM_SIZE)newlen);
435 mess(pat,a1,a2,a3,a4)
441 sprintf(s,pat,a1,a2,a3,a4);
445 sprintf(s," at %s line %ld",
446 in_eval?filename:origfilename, (long)line);
450 last_in_stab->stab_io &&
451 last_in_stab->stab_io->lines ) {
452 sprintf(s,", <%s> line %ld",
453 last_in_stab == argvstab ? "" : last_in_stab->stab_name,
454 (long)last_in_stab->stab_io->lines);
462 fatal(pat,a1,a2,a3,a4)
466 extern char *e_tmpname;
468 mess(pat,a1,a2,a3,a4);
470 str_set(stabent("@",TRUE)->stab_val,tokenbuf);
473 fputs(tokenbuf,stderr);
478 exit(errno?errno:(statusvalue?statusvalue:255));
482 warn(pat,a1,a2,a3,a4)
485 mess(pat,a1,a2,a3,a4);
486 fputs(tokenbuf,stderr);
490 static bool firstsetenv = TRUE;
491 extern char **environ;
497 register int i=envix(nam); /* where does it go? */
499 if (!environ[i]) { /* does not exist yet */
500 if (firstsetenv) { /* need we copy environment? */
503 char **tmpenv = (char**) /* point our wand at memory */
504 safemalloc((i+2) * sizeof(char*));
506 char **tmpenv = Null(char **);
510 for (j=0; j<i; j++) /* copy environment */
511 tmpenv[j] = environ[j];
512 environ = tmpenv; /* tell exec where it is now */
516 environ = (char**) saferealloc((char*) environ,
517 (i+2) * sizeof(char*));
518 /* just expand it a bit */
520 environ[i+1] = Nullch; /* make sure it's null terminated */
522 environ[i] = safemalloc((MEM_SIZE)(strlen(nam) + strlen(val) + 2));
523 /* this may or may not be in */
524 /* the old environ structure */
525 sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
532 register int i, len = strlen(nam);
534 for (i = 0; environ[i]; i++) {
535 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
536 break; /* strnEQ must come first to avoid */
537 } /* potential SEGV's */
542 unlnk(f) /* unlink all versions of a file */
547 for (i = 0; unlink(f) >= 0; i++) ;