home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perlkt40.zip / UTIL.C < prev    next >
C/C++ Source or Header  |  1996-06-13  |  6KB  |  295 lines

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