home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl560.zip / x2p / util.c < prev    next >
C/C++ Source or Header  |  1999-07-20  |  4KB  |  222 lines

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