home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities3 / gnu_sed_rx / c / utils < prev   
Text File  |  1994-02-25  |  5KB  |  327 lines

  1. /*  Functions from hack's utils library.
  2.     Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* These routines were written as part of a library (by hack), but since most
  19.    people don't have the library, here they are.  */
  20.  
  21. #include "config.h"
  22.  
  23. #ifdef __STDC__
  24. #define VOID void
  25. #else
  26. #define VOID char
  27. #endif
  28.  
  29. #include <stdio.h>
  30. #if HAVE_STRING_H || defined(STDC_HEADERS)
  31. #include <string.h>
  32. #else
  33. #include <strings.h>
  34. #endif
  35. #if defined(STDC_HEADERS)
  36. #include <stdlib.h>
  37. #else
  38. #ifdef RX_MEMDBUG
  39. #include <sys/types.h>
  40. #include <malloc.h>
  41. #else
  42. VOID *malloc();
  43. VOID *realloc();
  44. #endif /* ndef RX_MEMDBUG  */
  45. #endif
  46.  
  47. VOID *ck_malloc();
  48.  
  49. char *myname;
  50.  
  51.  
  52. #ifdef __STDC__
  53. #include <stdarg.h>
  54.  
  55. /* Print an error message and exit */
  56. void
  57. panic(char *str, ...)
  58. {
  59.     va_list iggy;
  60.  
  61.     fprintf(stderr,"%s: ",myname);
  62.     va_start(iggy,str);
  63. #ifdef HAVE_VPRINTF
  64.     vfprintf(stderr,str,iggy);
  65. #else
  66. #ifdef HAVE_DOPRNT
  67.     _doprnt(str,&iggy,stderr);
  68. #endif
  69. #endif
  70.     va_end(iggy);
  71.     putc('\n',stderr);
  72.     exit(4);
  73. }
  74.  
  75. #else
  76. #include <varargs.h>
  77.  
  78. void
  79. panic(str,va_alist)
  80. char *str;
  81. va_dcl
  82. {
  83.     va_list iggy;
  84.  
  85.     fprintf(stderr,"%s: ",myname);
  86.     va_start(iggy);
  87. #ifdef HAVE_VPRINTF
  88.     vfprintf(stderr,str,iggy);
  89. #else
  90. #ifdef HAVE_DOPRNT
  91.     _doprnt(str,&iggy,stderr);
  92. #endif
  93. #endif
  94.     va_end(iggy);
  95.     putc('\n',stderr);
  96.     exit(4);
  97. }
  98.  
  99. #endif
  100.  
  101. /* Store information about files opened with ck_fopen
  102.    so that error messages from ck_fread, etc can print the
  103.    name of the file that had the error */
  104. #define N_FILE 32
  105.  
  106. struct id {
  107.     FILE *fp;
  108.     char *name;
  109. };
  110.  
  111. static struct id __id_s[N_FILE];
  112.  
  113. /* Internal routine to get a filename from __id_s */
  114. char *
  115. __fp_name(fp)
  116. FILE *fp;
  117. {
  118.     int n;
  119.  
  120.     for(n=0;n<N_FILE;n++) {
  121.         if(__id_s[n].fp==fp)
  122.             return __id_s[n].name;
  123.     }
  124.     return "{Unknown file pointer}";
  125. }
  126.  
  127. /* Panic on failing fopen */
  128. FILE *
  129. ck_fopen(name,mode)
  130. char *name;
  131. char *mode;
  132. {
  133.     FILE    *ret;
  134.     int    n;
  135.  
  136.     ret=fopen(name,mode);
  137.     if(ret==(FILE *)0)
  138.         panic("Couldn't open file %s",name);
  139.     for(n=0;n<N_FILE;n++) {
  140.         if(ret==__id_s[n].fp) {
  141.             free((VOID *)__id_s[n].name);
  142.             __id_s[n].name=(char *)ck_malloc(strlen(name)+1);
  143.             strcpy(__id_s[n].name,name);
  144.             break;
  145.         }
  146.     }
  147.     if(n==N_FILE) {
  148.         for(n=0;n<N_FILE;n++)
  149.             if(__id_s[n].fp==(FILE *)0)
  150.                 break;
  151.         if(n==N_FILE)
  152.             panic("Internal error: too many files open");
  153.         __id_s[n].fp=ret;
  154.         __id_s[n].name=(char *)ck_malloc(strlen(name)+1);
  155.         strcpy(__id_s[n].name,name);
  156.     }
  157.     return ret;
  158. }
  159.  
  160. /* Panic on failing fwrite */
  161. void
  162. ck_fwrite(ptr,size,nmemb,stream)
  163. char *ptr;
  164. int size,nmemb;
  165. FILE *stream;
  166. {
  167.     if(fwrite(ptr,size,nmemb,stream)!=nmemb)
  168.         panic("couldn't write %d items to %s",nmemb,__fp_name(stream));
  169. }
  170.  
  171. /* Panic on failing fclose */
  172. void
  173. ck_fclose(stream)
  174. FILE *stream;
  175. {
  176.     if(fclose(stream)==EOF)
  177.         panic("Couldn't close %s",__fp_name(stream));
  178. }
  179.  
  180. /* Panic on failing malloc */
  181. VOID *
  182. ck_malloc(size)
  183. int size;
  184. {
  185.     VOID *ret;
  186.  
  187.     if(!size)
  188.         size++;
  189.     ret=malloc(size);
  190.     if(ret==(VOID *)0)
  191.         panic("Couldn't allocate memory");
  192.     return ret;
  193. }
  194.  
  195. /* Panic on failing malloc */
  196. VOID *
  197. xmalloc(size)
  198. int size;
  199. {
  200.   return ck_malloc (size);
  201. }
  202.  
  203. /* Panic on failing realloc */
  204. VOID *
  205. ck_realloc(ptr,size)
  206. VOID *ptr;
  207. int size;
  208. {
  209.     VOID *ret;
  210.  
  211.     if (!ptr)
  212.       return ck_malloc (size);
  213.     ret=realloc(ptr,size);
  214.     if(ret==(VOID *)0)
  215.         panic("Couldn't re-allocate memory");
  216.     return ret;
  217. }
  218.  
  219. /* Return a malloc()'d copy of a string */
  220. char *
  221. ck_strdup(str)
  222. char *str;
  223. {
  224.     char *ret;
  225.  
  226.     ret=(char *)ck_malloc(strlen(str)+2);
  227.     strcpy(ret,str);
  228.     return ret;
  229. }
  230.  
  231.  
  232. /* Implement a variable sized buffer of 'stuff'.  We don't know what it is,
  233.    nor do we care, as long as it doesn't mind being aligned by malloc. */
  234.  
  235. struct buffer {
  236.     int    allocated;
  237.     int    length;
  238.     char    *b;
  239. };
  240.  
  241. #define MIN_ALLOCATE 50
  242.  
  243. VOID *
  244. init_buffer()
  245. {
  246.     struct buffer *b;
  247.  
  248.     b=(struct buffer *)ck_malloc(sizeof(struct buffer));
  249.     b->allocated=MIN_ALLOCATE;
  250.     b->b=(char *)ck_malloc(MIN_ALLOCATE);
  251.     b->length=0;
  252.     return (VOID *)b;
  253. }
  254.  
  255. void
  256. flush_buffer(bb)
  257. VOID *bb;
  258. {
  259.     struct buffer *b;
  260.  
  261.     b=(struct buffer *)bb;
  262.     free(b->b);
  263.     b->b=0;
  264.     b->allocated=0;
  265.     b->length=0;
  266.     free(b);
  267. }
  268.  
  269. int
  270. size_buffer(b)
  271. VOID *b;
  272. {
  273.     struct buffer *bb;
  274.  
  275.     bb=(struct buffer *)b;
  276.     return bb->length;
  277. }
  278.  
  279. void
  280. add_buffer(bb,p,n)
  281. VOID *bb;
  282. char *p;
  283. int n;
  284. {
  285.     struct buffer *b;
  286.     int x;
  287.     char * cp;
  288.  
  289.     b=(struct buffer *)bb;
  290.     if(b->length+n>b->allocated) {
  291.         b->allocated = (b->length + n) * 2;
  292.         b->b=(char *)ck_realloc(b->b,b->allocated);
  293.     }
  294.     
  295.     x = n;
  296.     cp = b->b + b->length;
  297.     while (x--)
  298.       *cp++ = *p++;
  299.     b->length+=n;
  300. }
  301.  
  302. void
  303. add1_buffer(bb,ch)
  304. VOID *bb;
  305. int ch;
  306. {
  307.     struct buffer *b;
  308.  
  309.     b=(struct buffer *)bb;
  310.     if(b->length+1>b->allocated) {
  311.         b->allocated*=2;
  312.         b->b=(char *)ck_realloc(b->b,b->allocated);
  313.     }
  314.     b->b[b->length]=ch;
  315.     b->length++;
  316. }
  317.  
  318. char *
  319. get_buffer(bb)
  320. VOID *bb;
  321. {
  322.     struct buffer *b;
  323.  
  324.     b=(struct buffer *)bb;
  325.     return b->b;
  326. }
  327.