home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacPerl 5.0.3 / MacPerl Source ƒ / Perl5 / x2p / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-26  |  4.8 KB  |  267 lines  |  [TEXT/MPS ]

  1. /* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $
  2.  *
  3.  *    Copyright (c) 1991, Larry Wall
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  * $Log:    util.c,v $
  9.  */
  10.  
  11. #include <stdio.h>
  12.  
  13. #include "handy.h"
  14. #include "EXTERN.h"
  15. #include "a2p.h"
  16. #include "INTERN.h"
  17. #include "util.h"
  18.  
  19. #define FLUSH
  20. #define MEM_SIZE unsigned int
  21.  
  22. static char nomem[] = "Out of memory!\n";
  23.  
  24. /* paranoid version of malloc */
  25.  
  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. /* copy a string up to some (non-backslashed) delimiter, if any */
  103.  
  104. char *
  105. cpytill(to,from,delim)
  106. register char *to, *from;
  107. register int delim;
  108. {
  109.     for (; *from; from++,to++) {
  110.     if (*from == '\\') {
  111.         if (from[1] == delim)
  112.         from++;
  113.         else if (from[1] == '\\')
  114.         *to++ = *from++;
  115.     }
  116.     else if (*from == delim)
  117.         break;
  118.     *to = *from;
  119.     }
  120.     *to = '\0';
  121.     return from;
  122. }
  123.  
  124.  
  125. char *
  126. cpy2(to,from,delim)
  127. register char *to, *from;
  128. register int delim;
  129. {
  130.     for (; *from; from++,to++) {
  131.     if (*from == '\\')
  132.         *to++ = *from++;
  133.     else if (*from == '$')
  134.         *to++ = '\\';
  135.     else if (*from == delim)
  136.         break;
  137.     *to = *from;
  138.     }
  139.     *to = '\0';
  140.     return from;
  141. }
  142.  
  143. /* return ptr to little string in big string, NULL if not found */
  144.  
  145. char *
  146. instr(big, little)
  147. char *big, *little;
  148.  
  149. {
  150.     register char *t, *s, *x;
  151.  
  152.     for (t = big; *t; t++) {
  153.     for (x=t,s=little; *s; x++,s++) {
  154.         if (!*x)
  155.         return Nullch;
  156.         if (*s != *x)
  157.         break;
  158.     }
  159.     if (!*s)
  160.         return t;
  161.     }
  162.     return Nullch;
  163. }
  164.  
  165. /* copy a string to a safe spot */
  166.  
  167. char *
  168. savestr(str)
  169. char *str;
  170. {
  171.     register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
  172.  
  173.     (void)strcpy(newaddr,str);
  174.     return newaddr;
  175. }
  176.  
  177. /* grow a static string to at least a certain length */
  178.  
  179. void
  180. growstr(strptr,curlen,newlen)
  181. char **strptr;
  182. int *curlen;
  183. int newlen;
  184. {
  185.     if (newlen > *curlen) {        /* need more room? */
  186.     if (*curlen)
  187.         *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
  188.     else
  189.         *strptr = safemalloc((MEM_SIZE)newlen);
  190.     *curlen = newlen;
  191.     }
  192. }
  193.  
  194. /*VARARGS1*/
  195. croak(pat,a1,a2,a3,a4)
  196. char *pat;
  197. {
  198.     fprintf(stderr,pat,a1,a2,a3,a4);
  199.     exit(1);
  200. }
  201.  
  202. /*VARARGS1*/
  203. fatal(pat,a1,a2,a3,a4)
  204. char *pat;
  205. {
  206.     fprintf(stderr,pat,a1,a2,a3,a4);
  207.     exit(1);
  208. }
  209.  
  210. /*VARARGS1*/
  211. warn(pat,a1,a2,a3,a4)
  212. char *pat;
  213. {
  214.     fprintf(stderr,pat,a1,a2,a3,a4);
  215. }
  216.  
  217. static bool firstsetenv = TRUE;
  218. extern char **environ;
  219.  
  220. void
  221. setenv(nam,val)
  222. char *nam, *val;
  223. {
  224.     register int i=envix(nam);        /* where does it go? */
  225.  
  226.     if (!environ[i]) {            /* does not exist yet */
  227.     if (firstsetenv) {        /* need we copy environment? */
  228.         int j;
  229. #ifndef lint
  230.         char **tmpenv = (char**)    /* point our wand at memory */
  231.         safemalloc((i+2) * sizeof(char*));
  232. #else
  233.         char **tmpenv = Null(char **);
  234. #endif /* lint */
  235.     
  236.         firstsetenv = FALSE;
  237.         for (j=0; j<i; j++)        /* copy environment */
  238.         tmpenv[j] = environ[j];
  239.         environ = tmpenv;        /* tell exec where it is now */
  240.     }
  241. #ifndef lint
  242.     else
  243.         environ = (char**) saferealloc((char*) environ,
  244.         (i+2) * sizeof(char*));
  245.                     /* just expand it a bit */
  246. #endif /* lint */
  247.     environ[i+1] = Nullch;    /* make sure it's null terminated */
  248.     }
  249.     environ[i] = safemalloc(strlen(nam) + strlen(val) + 2);
  250.                     /* this may or may not be in */
  251.                     /* the old environ structure */
  252.     sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
  253. }
  254.  
  255. int
  256. envix(nam)
  257. char *nam;
  258. {
  259.     register int i, len = strlen(nam);
  260.  
  261.     for (i = 0; environ[i]; i++) {
  262.     if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
  263.         break;            /* strnEQ must come first to avoid */
  264.     }                    /* potential SEGV's */
  265.     return i;
  266. }
  267.