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

envix defined in line 260; used 2 times
safecat defined in line 108; used 1 times

Defined variables

an defined in line 28; used 4 times
nomem defined in line 24; used 2 times

Defined macros

FLUSH defined in line 21; used 2 times
MEM_SIZE defined in line 22; used 5 times
Last modified: 1988-02-03
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 4175
Valid CSS Valid XHTML 1.0 Strict