Fix up mix of man(7)/mdoc(7).
[netbsd-mini2440.git] / usr.bin / sed / utils.c
blob2d39cfc9cbb0c598badbbec2baae4f7343cbd735
1 /* Functions from hack's utils library.
2 Copyright (C) 1989-1991 Free Software Foundation, Inc.
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.
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.
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. */
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. */
21 #ifdef __STDC__
22 #define VOID void
23 #else
24 #define VOID char
25 #endif
27 #include <stdio.h>
28 #if defined(USG) || defined(STDC_HEADERS)
29 #include <string.h>
30 #define bcopy(src, dst, len) memcpy((dst), (src), (len))
31 #else
32 #endif
33 #if defined(STDC_HEADERS)
34 #include <stdlib.h>
35 #else
36 VOID *malloc();
37 VOID *realloc();
38 #endif
40 VOID *ck_malloc();
42 char *myname;
44 #ifdef __STDC__
45 #include <stdarg.h>
47 /* Print an error message and exit */
48 void
49 panic(char *str, ...)
51 va_list iggy;
53 fprintf(stderr,"%s: ",myname);
54 va_start(iggy,str);
55 #ifdef NO_VFPRINTF
56 _doprnt(str,&iggy,stderr);
57 #else
58 vfprintf(stderr,str,iggy);
59 #endif
60 va_end(iggy);
61 putc('\n',stderr);
62 exit(4);
65 #else
66 #include <varargs.h>
68 void
69 panic(str,va_alist)
70 char *str;
71 va_dcl
73 va_list iggy;
75 fprintf(stderr,"%s: ",myname);
76 va_start(iggy);
77 #ifdef NO_VFPRINTF
78 _doprnt(str,&iggy,stderr);
79 #else
80 vfprintf(stderr,str,iggy);
81 #endif
82 va_end(iggy);
83 putc('\n',stderr);
84 exit(4);
87 #endif
89 /* Store information about files opened with ck_fopen
90 so that error messages from ck_fread, etc can print the
91 name of the file that had the error */
92 #define N_FILE 32
94 struct id {
95 FILE *fp;
96 char *name;
99 static struct id __id_s[N_FILE];
101 /* Internal routine to get a filename from __id_s */
102 char *
103 __fp_name(fp)
104 FILE *fp;
106 int n;
108 for(n=0;n<N_FILE;n++) {
109 if(__id_s[n].fp==fp)
110 return __id_s[n].name;
112 return "{Unknown file pointer}";
115 /* Panic on failing fopen */
116 FILE *
117 ck_fopen(name,mode)
118 char *name;
119 char *mode;
121 FILE *ret;
122 int n;
124 ret=fopen(name,mode);
125 if(ret==(FILE *)0)
126 panic("Couldn't open file %s",name);
127 for(n=0;n<N_FILE;n++) {
128 if(ret==__id_s[n].fp) {
129 free((VOID *)__id_s[n].name);
130 __id_s[n].name=(char *)ck_malloc(strlen(name)+1);
131 strcpy(__id_s[n].name,name);
132 break;
135 if(n==N_FILE) {
136 for(n=0;n<N_FILE;n++)
137 if(__id_s[n].fp==(FILE *)0)
138 break;
139 if(n==N_FILE)
140 panic("Internal error: too many files open");
141 __id_s[n].fp=ret;
142 __id_s[n].name=(char *)ck_malloc(strlen(name)+1);
143 strcpy(__id_s[n].name,name);
145 return ret;
148 /* Panic on failing fwrite */
149 void
150 ck_fwrite(ptr,size,nmemb,stream)
151 char *ptr;
152 int size,nmemb;
153 FILE *stream;
155 if(fwrite(ptr,size,nmemb,stream)!=nmemb)
156 panic("couldn't write %d items to %s",nmemb,__fp_name(stream));
159 /* Panic on failing fclose */
160 void
161 ck_fclose(stream)
162 FILE *stream;
164 if(fclose(stream)==EOF)
165 panic("Couldn't close %s",__fp_name(stream));
168 /* Panic on failing malloc */
169 VOID *
170 ck_malloc(size)
171 int size;
173 VOID *ret;
175 if(!size)
176 size++;
177 ret=malloc(size);
178 if(ret==(VOID *)0)
179 panic("Couldn't allocate memory");
180 return ret;
183 /* Panic on failing realloc */
184 VOID *
185 ck_realloc(ptr,size)
186 VOID *ptr;
187 int size;
189 VOID *ret;
191 ret=realloc(ptr,size);
192 if(ret==(VOID *)0)
193 panic("Couldn't re-allocate memory");
194 return ret;
197 /* Return a malloc()'d copy of a string */
198 char *
199 ck_strdup(str)
200 char *str;
202 char *ret;
204 ret=(char *)ck_malloc(strlen(str)+2);
205 strcpy(ret,str);
206 return ret;
209 #if !defined(USG) && !defined(STDC_HEADERS)
211 * memchr - search for a byte
215 VOID *
216 memchr(s, ucharwanted, size)
217 VOID *s;
218 int ucharwanted;
219 int size;
221 register char *scan;
222 register n;
223 register uc;
225 scan = (char *)s;
226 uc = (ucharwanted&0xFF);
227 for (n = size; n > 0; n--)
228 if (((*scan)&0xFF) == uc)
229 return((VOID *)scan);
230 else
231 scan++;
233 return 0;
235 #endif
237 #if !defined(STDC_HEADERS)
239 * memmove - copy bytes, being careful about overlap.
242 VOID *
243 memmove(dst, src, size)
244 VOID *dst;
245 VOID *src;
246 int size;
248 register char *d;
249 register char *s;
250 register int n;
252 if (size <= 0)
253 return(dst);
255 s = (char *)src;
256 d = (char *)dst;
257 if (s <= d && s + (size-1) >= d) {
258 /* Overlap, must copy right-to-left. */
259 s += size-1;
260 d += size-1;
261 for (n = size; n > 0; n--)
262 *d-- = *s--;
263 } else
264 for (n = size; n > 0; n--)
265 *d++ = *s++;
267 return(dst);
269 #endif
271 /* Implement a variable sized buffer of 'stuff'. We don't know what it is,
272 nor do we care, as long as it doesn't mind being aligned by malloc. */
274 struct buffer {
275 int allocated;
276 int length;
277 char *b;
280 #define MIN_ALLOCATE 50
282 VOID *
283 init_buffer()
285 struct buffer *b;
287 b=(struct buffer *)ck_malloc(sizeof(struct buffer));
288 b->allocated=MIN_ALLOCATE;
289 b->b=(char *)ck_malloc(MIN_ALLOCATE);
290 b->length=0;
291 return (VOID *)b;
294 void
295 flush_buffer(bb)
296 VOID *bb;
298 struct buffer *b;
300 b=(struct buffer *)bb;
301 free(b->b);
302 b->b=0;
303 b->allocated=0;
304 b->length=0;
305 free(b);
309 size_buffer(b)
310 VOID *b;
312 struct buffer *bb;
314 bb=(struct buffer *)b;
315 return bb->length;
318 void
319 add_buffer(bb,p,n)
320 VOID *bb;
321 char *p;
322 int n;
324 struct buffer *b;
326 b=(struct buffer *)bb;
327 if(b->length+n>b->allocated) {
328 b->allocated*=2;
329 b->b=(char *)ck_realloc(b->b,b->allocated);
331 bcopy(p,b->b+b->length,n);
332 b->length+=n;
335 void
336 add1_buffer(bb,ch)
337 VOID *bb;
338 int ch;
340 struct buffer *b;
342 b=(struct buffer *)bb;
343 if(b->length+1>b->allocated) {
344 b->allocated*=2;
345 b->b=(char *)ck_realloc(b->b,b->allocated);
347 b->b[b->length]=ch;
348 b->length++;
351 char *
352 get_buffer(bb)
353 VOID *bb;
355 struct buffer *b;
357 b=(struct buffer *)bb;
358 return b->b;