# 2.11BSD#431 - /usr/src/new/PORT/perl.1.0.16/util.c

##### Functions

    PL_setenv         X
    cpytill           X
    envix             X
    fatal             X
    growstr           X
    instr             X
    safecat           X
    safecpy           X
    safefree          X
    safemalloc        X
    saferealloc       X
    savestr           X

##### Macros

    FLUSH             X
    MEM_SIZE          X

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

#### Defined functions

PL_setenv defined in line 225; used 2 times

- in /usr/src/new/PORT/perl.1.0.16/stab.c line 252
- in /usr/src/new/PORT/perl.1.0.16/util.h line 31

cpytill defined in line 137; used 4 times

- in /usr/src/new/PORT/perl.1.0.16/perly.c line 928, 967, 1608
- in /usr/src/new/PORT/perl.1.0.16/util.h line 20

envix defined in line 260; used 2 times

- in line 229
- in /usr/src/new/PORT/perl.1.0.16/util.h line 32

fatal defined in line 205; used 27 times

- in /usr/src/new/PORT/perl.1.0.16/arg.c line 46, 125, 161, 191, 224, 268, 1314, 1325, 1874, 1928, 1966
- in /usr/src/new/PORT/perl.1.0.16/cmd.c line 324
- in /usr/src/new/PORT/perl.1.0.16/perly.c line 101, 141, 153, 223, 926-930(2), 950, 969, 993-997(2), 1069-1074(2), 1559, 1614, 1641

growstr defined in line 189; used 3 times

- in /usr/src/new/PORT/perl.1.0.16/perl.h line 85
- in /usr/src/new/PORT/perl.1.0.16/search.c line 140
- in /usr/src/new/PORT/perl.1.0.16/util.h line 35

instr defined in line 155; used 3 times

- in /usr/src/new/PORT/perl.1.0.16/arg.c line 1935
- in /usr/src/new/PORT/perl.1.0.16/cmd.c line 232
- in /usr/src/new/PORT/perl.1.0.16/util.h line 21

safecat defined in line 108; used 1 times

- in /usr/src/new/PORT/perl.1.0.16/util.h line 19

safecpy defined in line 91; used 2 times

- in /usr/src/new/PORT/perl.1.0.16/search.c line 141
- in /usr/src/new/PORT/perl.1.0.16/util.h line 18

safefree defined in line 79; used 44 times

- in /usr/src/new/PORT/perl.1.0.16/arg.c line 310, 782, 808-810(2), 845, 864, 888, 947, 1030, 1772, 1784, 2156
- in /usr/src/new/PORT/perl.1.0.16/array.c line 91-92(2)
- in /usr/src/new/PORT/perl.1.0.16/hash.c line 72, 121-124(3)
- in /usr/src/new/PORT/perl.1.0.16/perl.y line 223, 413, 419
- in /usr/src/new/PORT/perl.1.0.16/perly.c line 206, 327, 1079, 1088-1089(2), 1417, 2249, 2271, 2532, 2557, 2619
- in /usr/src/new/PORT/perl.1.0.16/search.c line 118-122(2), 183, 550
- in /usr/src/new/PORT/perl.1.0.16/stab.c line 176, 214-224(3), 254, 266
- in /usr/src/new/PORT/perl.1.0.16/str.c line 277, 284

safemalloc defined in line 30; used 37 times

- in line 181, 199, 236, 254
- in /usr/src/new/PORT/perl.1.0.16/arg.c line 281, 581, 614, 654, 801, 831, 1060, 1087, 1095, 1257
- in /usr/src/new/PORT/perl.1.0.16/array.c line 72-74(2)
- in /usr/src/new/PORT/perl.1.0.16/hash.c line 76, 173-175(2)
- in /usr/src/new/PORT/perl.1.0.16/perly.c line 796, 810, 908, 959, 1015, 1060, 1137, 1157, 1356-1358(2), 1420, 2259, 2355, 2392
- in /usr/src/new/PORT/perl.1.0.16/search.c line 186
- in /usr/src/new/PORT/perl.1.0.16/str.c line 253, 513
- in /usr/src/new/PORT/perl.1.0.16/util.h line 16

saferealloc defined in line 53; used 9 times

- in line 197, 248
- in /usr/src/new/PORT/perl.1.0.16/arg.c line 1708
- in /usr/src/new/PORT/perl.1.0.16/array.c line 39
- in /usr/src/new/PORT/perl.1.0.16/hash.c line 144
- in /usr/src/new/PORT/perl.1.0.16/perly.c line 1849
- in /usr/src/new/PORT/perl.1.0.16/search.c line 472
- in /usr/src/new/PORT/perl.1.0.16/str.c line 510
- in /usr/src/new/PORT/perl.1.0.16/util.h line 17

savestr defined in line 177; used 43 times

- in /usr/src/new/PORT/perl.1.0.16/arg.c line 1746-1751(2)
- in /usr/src/new/PORT/perl.1.0.16/form.c line 250
- in /usr/src/new/PORT/perl.1.0.16/hash.c line 78
- in /usr/src/new/PORT/perl.1.0.16/perly.c line 64, 119, 329, 482-490(3), 512, 520, 546, 558, 570, 576, 586, 592, 602, 620, 628, 636, 646, 664-668(2), 678, 710, 724, 744, 750, 758, 764, 772-776(2), 798, 1049, 2404
- in /usr/src/new/PORT/perl.1.0.16/search.c line 184, 548
- in /usr/src/new/PORT/perl.1.0.16/stab.c line 215-225(3)
- in /usr/src/new/PORT/perl.1.0.16/util.h line 29

#### Defined variables

an defined in line 28; used 4 times

- in line 40, 64-65(2), 84

nomem defined in line 24; used 2 times

- in line 45, 71

#### Defined macros

FLUSH defined in line 21; used 2 times

- in line 45, 71

MEM_SIZE defined in line 22; used 5 times

- in line 32, 56, 181, 197-199(2)

Last modified: 2002-12-19 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 1606 |