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)
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. */
28 #if defined(USG) || defined(STDC_HEADERS)
30 #define bcopy(src, dst, len) memcpy((dst), (src), (len))
33 #if defined(STDC_HEADERS)
47 /* Print an error message and exit */
53 fprintf(stderr
,"%s: ",myname
);
56 _doprnt(str
,&iggy
,stderr
);
58 vfprintf(stderr
,str
,iggy
);
75 fprintf(stderr
,"%s: ",myname
);
78 _doprnt(str
,&iggy
,stderr
);
80 vfprintf(stderr
,str
,iggy
);
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 */
99 static struct id __id_s
[N_FILE
];
101 /* Internal routine to get a filename from __id_s */
108 for(n
=0;n
<N_FILE
;n
++) {
110 return __id_s
[n
].name
;
112 return "{Unknown file pointer}";
115 /* Panic on failing fopen */
124 ret
=fopen(name
,mode
);
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
);
136 for(n
=0;n
<N_FILE
;n
++)
137 if(__id_s
[n
].fp
==(FILE *)0)
140 panic("Internal error: too many files open");
142 __id_s
[n
].name
=(char *)ck_malloc(strlen(name
)+1);
143 strcpy(__id_s
[n
].name
,name
);
148 /* Panic on failing fwrite */
150 ck_fwrite(ptr
,size
,nmemb
,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 */
164 if(fclose(stream
)==EOF
)
165 panic("Couldn't close %s",__fp_name(stream
));
168 /* Panic on failing malloc */
179 panic("Couldn't allocate memory");
183 /* Panic on failing realloc */
191 ret
=realloc(ptr
,size
);
193 panic("Couldn't re-allocate memory");
197 /* Return a malloc()'d copy of a string */
204 ret
=(char *)ck_malloc(strlen(str
)+2);
209 #if !defined(USG) && !defined(STDC_HEADERS)
211 * memchr - search for a byte
216 memchr(s
, ucharwanted
, size
)
226 uc
= (ucharwanted
&0xFF);
227 for (n
= size
; n
> 0; n
--)
228 if (((*scan
)&0xFF) == uc
)
229 return((VOID
*)scan
);
237 #if !defined(STDC_HEADERS)
239 * memmove - copy bytes, being careful about overlap.
243 memmove(dst
, src
, size
)
257 if (s
<= d
&& s
+ (size
-1) >= d
) {
258 /* Overlap, must copy right-to-left. */
261 for (n
= size
; n
> 0; n
--)
264 for (n
= size
; n
> 0; n
--)
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. */
280 #define MIN_ALLOCATE 50
287 b
=(struct buffer
*)ck_malloc(sizeof(struct buffer
));
288 b
->allocated
=MIN_ALLOCATE
;
289 b
->b
=(char *)ck_malloc(MIN_ALLOCATE
);
300 b
=(struct buffer
*)bb
;
314 bb
=(struct buffer
*)b
;
326 b
=(struct buffer
*)bb
;
327 if(b
->length
+n
>b
->allocated
) {
329 b
->b
=(char *)ck_realloc(b
->b
,b
->allocated
);
331 bcopy(p
,b
->b
+b
->length
,n
);
342 b
=(struct buffer
*)bb
;
343 if(b
->length
+1>b
->allocated
) {
345 b
->b
=(char *)ck_realloc(b
->b
,b
->allocated
);
357 b
=(struct buffer
*)bb
;