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

##### Functions

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

##### Macros

    FLUSH             X
    MEM_SIZE          X

       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: }

#### Defined functions

cpytill defined in line 134; used 4 times

- in /usr/src/new/PORT/perl.1.0.0/perly.c line 897, 936, 1573
- in /usr/src/new/PORT/perl.1.0.0/util.h line 20

envix defined in line 252; used 2 times

- in line 221
- in /usr/src/new/PORT/perl.1.0.0/util.h line 32

fatal defined in line 202; used 26 times

- in /usr/src/new/PORT/perl.1.0.0/arg.c line 28, 107, 143, 173, 206, 237, 1267, 1278, 1827, 1881
- in /usr/src/new/PORT/perl.1.0.0/cmd.c line 321
- in /usr/src/new/PORT/perl.1.0.0/perly.c line 90, 130, 142, 210, 895-899(2), 919, 938, 962-966(2), 1038-1043(2), 1524, 1579, 1606

growstr defined in line 186; used 3 times

- in /usr/src/new/PORT/perl.1.0.0/perl.h line 59
- in /usr/src/new/PORT/perl.1.0.0/search.c line 137
- in /usr/src/new/PORT/perl.1.0.0/util.h line 35

instr defined in line 152; used 3 times

- in /usr/src/new/PORT/perl.1.0.0/arg.c line 1888
- in /usr/src/new/PORT/perl.1.0.0/cmd.c line 229
- in /usr/src/new/PORT/perl.1.0.0/util.h line 21

safecat defined in line 105; used 1 times

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

safecpy defined in line 88; used 2 times

- in /usr/src/new/PORT/perl.1.0.0/search.c line 138
- in /usr/src/new/PORT/perl.1.0.0/util.h line 18

safefree defined in line 76; used 41 times

- in /usr/src/new/PORT/perl.1.0.0/arg.c line 279, 741, 767-769(2), 804, 823, 847, 906, 989, 1725, 1737, 2099
- in /usr/src/new/PORT/perl.1.0.0/array.c line 91-92(2)
- in /usr/src/new/PORT/perl.1.0.0/hash.c line 72, 121-124(3)
- in /usr/src/new/PORT/perl.1.0.0/perl.y line 218, 408, 414
- in /usr/src/new/PORT/perl.1.0.0/perly.c line 314, 1048, 1057-1058(2), 1386, 2214, 2236
- in /usr/src/new/PORT/perl.1.0.0/search.c line 114-118(2), 180, 547
- in /usr/src/new/PORT/perl.1.0.0/stab.c line 170-175(2), 202-212(3), 242, 254
- in /usr/src/new/PORT/perl.1.0.0/str.c line 274, 281

safemalloc defined in line 27; used 34 times

- in line 178, 196, 228, 246
- in /usr/src/new/PORT/perl.1.0.0/arg.c line 250, 545, 578, 618, 760, 790, 1210
- in /usr/src/new/PORT/perl.1.0.0/array.c line 72-74(2)
- in /usr/src/new/PORT/perl.1.0.0/hash.c line 76, 173-175(2)
- in /usr/src/new/PORT/perl.1.0.0/perly.c line 765, 779, 877, 928, 984, 1029, 1106, 1126, 1325-1327(2), 1389, 2224, 2320, 2357
- in /usr/src/new/PORT/perl.1.0.0/search.c line 183
- in /usr/src/new/PORT/perl.1.0.0/str.c line 250, 510
- in /usr/src/new/PORT/perl.1.0.0/util.h line 16

saferealloc defined in line 50; used 12 times

- in line 194, 240
- in /usr/src/new/PORT/perl.1.0.0/arg.c line 1019, 1046, 1054, 1661
- in /usr/src/new/PORT/perl.1.0.0/array.c line 39
- in /usr/src/new/PORT/perl.1.0.0/hash.c line 144
- in /usr/src/new/PORT/perl.1.0.0/perly.c line 1814
- in /usr/src/new/PORT/perl.1.0.0/search.c line 469
- in /usr/src/new/PORT/perl.1.0.0/str.c line 507
- in /usr/src/new/PORT/perl.1.0.0/util.h line 17

savestr defined in line 174; used 43 times

- in /usr/src/new/PORT/perl.1.0.0/arg.c line 1699-1704(2)
- in /usr/src/new/PORT/perl.1.0.0/form.c line 250
- in /usr/src/new/PORT/perl.1.0.0/hash.c line 78
- in /usr/src/new/PORT/perl.1.0.0/perly.c line 53, 108, 316, 455-463(3), 485, 493, 515, 527, 539, 545, 555, 561, 571, 589, 597, 605, 615, 633-637(2), 647, 679, 693, 713, 719, 727, 733, 741-745(2), 767, 1018, 2369
- in /usr/src/new/PORT/perl.1.0.0/search.c line 181, 545
- in /usr/src/new/PORT/perl.1.0.0/stab.c line 203-213(3)
- in /usr/src/new/PORT/perl.1.0.0/util.h line 29

setenv defined in line 217; used 2 times

- in /usr/src/new/PORT/perl.1.0.0/stab.c line 240
- in /usr/src/new/PORT/perl.1.0.0/util.h line 31

#### Defined variables

an defined in line 25; used 4 times

- in line 37, 61-62(2), 81

nomem defined in line 21; used 2 times

- in line 42, 68

#### Defined macros

FLUSH defined in line 18; used 2 times

- in line 42, 68

MEM_SIZE defined in line 19; used 5 times

- in line 29, 53, 178, 194-196(2)

Last modified: 1988-01-31 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 1003 |