home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / perl / Perl / x2p / util.c < prev    next >
C/C++ Source or Header  |  1995-06-21  |  4KB  |  221 lines

  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 "EXTERN.h"
  12. #include "a2p.h"
  13. #include "INTERN.h"
  14. #include "util.h"
  15.  
  16. #define FLUSH
  17.  
  18. static char nomem[] = "Out of memory!\n";
  19.  
  20. /* paranoid version of malloc */
  21.  
  22.  
  23. Malloc_t
  24. safemalloc(size)
  25. MEM_SIZE size;
  26. {
  27.     char *ptr;
  28.     Malloc_t malloc();
  29.  
  30.     ptr = (char *) malloc(size?size:1);    /* malloc(0) is NASTY on our system */
  31. #ifdef DEBUGGING
  32.     if (debug & 128)
  33.     fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size);
  34. #endif
  35.     if (ptr != Nullch)
  36.     return ptr;
  37.     else {
  38.     fputs(nomem,stdout) FLUSH;
  39.     exit(1);
  40.     }
  41.     /*NOTREACHED*/
  42. }
  43.  
  44. /* paranoid version of realloc */
  45.  
  46. Malloc_t
  47. saferealloc(where,size)
  48. char *where;
  49. MEM_SIZE size;
  50. {
  51.     char *ptr;
  52.     Malloc_t realloc();
  53.  
  54.     ptr = (char *)
  55.         realloc(where,size?size:1);    /* realloc(0) is NASTY on our system */
  56. #ifdef DEBUGGING
  57.     if (debug & 128) {
  58.     fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
  59.     fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size);
  60.     }
  61. #endif
  62.     if (ptr != Nullch)
  63.     return ptr;
  64.     else {
  65.     fputs(nomem,stdout) FLUSH;
  66.     exit(1);
  67.     }
  68.     /*NOTREACHED*/
  69. }
  70.  
  71. /* safe version of free */
  72.  
  73. void
  74. safefree(where)
  75. char *where;
  76. {
  77. #ifdef DEBUGGING
  78.     if (debug & 128)
  79.     fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
  80. #endif
  81.     free(where);
  82. }
  83.  
  84. /* safe version of string copy */
  85.  
  86. char *
  87. safecpy(to,from,len)
  88. char *to;
  89. register char *from;
  90. register int len;
  91. {
  92.     register char *dest = to;
  93.  
  94.     if (from != Nullch) 
  95.     for (len--; len && (*dest++ = *from++); len--) ;
  96.     *dest = '\0';
  97.     return to;
  98. }
  99.  
  100. /* copy a string up to some (non-backslashed) delimiter, if any */
  101.  
  102. char *
  103. cpytill(to,from,delim)
  104. register char *to, *from;
  105. register int delim;
  106. {
  107.     for (; *from; from++,to++) {
  108.     if (*from == '\\') {
  109.         if (from[1] == delim)
  110.         from++;
  111.         else if (from[1] == '\\')
  112.         *to++ = *from++;
  113.     }
  114.     else if (*from == delim)
  115.         break;
  116.     *to = *from;
  117.     }
  118.     *to = '\0';
  119.     return from;
  120. }
  121.  
  122.  
  123. char *
  124. cpy2(to,from,delim)
  125. register char *to, *from;
  126. register int delim;
  127. {
  128.     for (; *from; from++,to++) {
  129.     if (*from == '\\')
  130.         *to++ = *from++;
  131.     else if (*from == '$')
  132.         *to++ = '\\';
  133.     else if (*from == delim)
  134.         break;
  135.     *to = *from;
  136.     }
  137.     *to = '\0';
  138.     return from;
  139. }
  140.  
  141. /* return ptr to little string in big string, NULL if not found */
  142.  
  143. char *
  144. instr(big, little)
  145. char *big, *little;
  146.  
  147. {
  148.     register char *t, *s, *x;
  149.  
  150.     for (t = big; *t; t++) {
  151.     for (x=t,s=little; *s; x++,s++) {
  152.         if (!*x)
  153.         return Nullch;
  154.         if (*s != *x)
  155.         break;
  156.     }
  157.     if (!*s)
  158.         return t;
  159.     }
  160.     return Nullch;
  161. }
  162.  
  163. /* copy a string to a safe spot */
  164.  
  165. char *
  166. savestr(str)
  167. char *str;
  168. {
  169.     register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
  170.  
  171.     (void)strcpy(newaddr,str);
  172.     return newaddr;
  173. }
  174.  
  175. /* grow a static string to at least a certain length */
  176.  
  177. void
  178. growstr(strptr,curlen,newlen)
  179. char **strptr;
  180. int *curlen;
  181. int newlen;
  182. {
  183.     if (newlen > *curlen) {        /* need more room? */
  184.     if (*curlen)
  185.         *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
  186.     else
  187.         *strptr = safemalloc((MEM_SIZE)newlen);
  188.     *curlen = newlen;
  189.     }
  190. }
  191.  
  192. /*VARARGS1*/
  193. void
  194. croak(pat,a1,a2,a3,a4)
  195. char *pat;
  196. int a1,a2,a3,a4;
  197. {
  198.     fprintf(stderr,pat,a1,a2,a3,a4);
  199.     exit(1);
  200. }
  201.  
  202. /*VARARGS1*/
  203. void
  204. fatal(pat,a1,a2,a3,a4)
  205. char *pat;
  206. int a1,a2,a3,a4;
  207. {
  208.     fprintf(stderr,pat,a1,a2,a3,a4);
  209.     exit(1);
  210. }
  211.  
  212. /*VARARGS1*/
  213. void
  214. warn(pat,a1,a2,a3,a4)
  215. char *pat;
  216. int a1,a2,a3,a4;
  217. {
  218.     fprintf(stderr,pat,a1,a2,a3,a4);
  219. }
  220.  
  221.