3 * $Id: utils.c,v 1.2 1997/08/04 10:24:26 mdw Exp $
5 * Miscellaneous useful bits of code.
7 * (c) 1997 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1997/08/04 10:24:26 mdw
33 * Sources placed under CVS control.
35 * Revision 1.1 1997/07/21 13:47:42 mdw
40 /*----- Header files ------------------------------------------------------*/
42 /* --- ANSI headers --- */
50 /* --- Local headers --- */
55 /*----- Program name handling ---------------------------------------------*/
57 /* --- Static data --- */
59 static const char *myname
= 0; /* What's my name? */
65 * Returns: Pointer to the program name.
67 * Use: Returns the program name.
70 const char *quis(void)
77 * Arguments: @const char *p@ = pointer to program name
81 * Use: Tells the utils library what the program's name is.
85 # if defined(__riscos)
87 # elif defined(__unix) || defined(unix)
94 void ego(const char *p
)
104 /*----- Error reporting ---------------------------------------------------*/
108 * Arguments: @const char *f@ = a @printf@-style format string
109 * @...@ = other arguments
113 * Use: Reports an error.
116 void moan(const char *f
, ...)
120 fprintf(stderr
, "%s: ", myname
);
121 vfprintf(stderr
, f
, ap
);
128 * Arguments: @const char *f@ = a @printf@-style format string
129 * @...@ = other arguments
133 * Use: Reports an error and hari-kiris. Like @moan@ above, only
137 void die(const char *f
, ...)
141 fprintf(stderr
, "%s: ", myname
);
142 vfprintf(stderr
, f
, ap
);
148 /*----- Trace messages ----------------------------------------------------*/
150 #if defined(TRACING) || !defined(NDEBUG)
152 /* --- Static data --- */
154 static FILE *tracefp
= 0; /* Where does debugging go? */
155 static unsigned int tracelvl
= 0; /* How much tracing gets done? */
159 * Arguments: @unsigned int lvl@ = trace level for output
160 * @const char *f@ = a @printf@-style format string
161 * @...@ = other arguments
165 * Use: Reports a message to the trace output.
168 void trace(unsigned int lvl
, const char *f
, ...)
171 if ((lvl
& tracing()) == 0)
174 fprintf(tracefp
, "*** %s: ", myname
);
175 vfprintf(tracefp
, f
, ap
);
180 /* --- @traceblk@ --- *
182 * Arguments: @unsigned int lvl@ = trace level for output
183 * @const char *hdr@ = some header string to write
184 * @const void *blk@ = pointer to a block of memory to dump
185 * @size_t sz@ = size of the block of memory
189 * Use: Dumps the contents of a block to the trace output.
192 void traceblk(unsigned int lvl
, const char *hdr
, const void *blk
, size_t sz
)
194 const unsigned char *p
= blk
;
199 /* --- Skip if the trace level is too high --- */
201 if ((lvl
& tracing()) == 0)
204 /* --- Now start work --- */
206 fprintf(tracefp
, "*** %s: %s\n", myname
, hdr
);
209 fprintf(tracefp
, "*** %s: %08lu : ", myname
, o
);
210 for (i
= 0; i
< 8; i
++) {
212 fprintf(tracefp
, "%02x ", p
[i
]);
214 fputs("** ", tracefp
);
216 fputs(": ", tracefp
);
217 for (i
= 0; i
< 8; i
++) {
219 fputc(isprint(p
[i
]) ? p
[i
] : '.', tracefp
);
223 fputc('\n', tracefp
);
224 c
= (sz
>= 8) ?
8 : sz
;
225 sz
-= c
, p
+= c
, o
+= c
;
230 /* --- @traceon@ --- *
232 * Arguments: @FILE *fp@ = a file to trace on
233 * @unsigned int lvl@ = trace level to set
237 * Use: Enables tracing to a file.
240 void traceon(FILE *fp
, unsigned int lvl
)
247 /* --- @tracesetlvl@ --- *
249 * Arguments: @unsigned int lvl@ = trace level to set
253 * Use: Sets the tracing level.
256 void tracesetlvl(unsigned int lvl
) { tracelvl
= lvl
; }
258 /* --- @tracing@ --- *
262 * Returns: Zero if not tracing, tracing level if tracing.
264 * Use: Informs the caller whether tracing is enabled.
267 unsigned int tracing(void) { return (tracefp ? tracelvl
: 0u); }
271 /*----- Memory management functions ---------------------------------------*/
273 /* --- @xmalloc@ --- *
275 * Arguments: @size_t sz@ = size of block to allocate
277 * Returns: Pointer to allocated block.
279 * Use: Allocates memory. If the memory isn't available, we don't
280 * hang aroung long enough for a normal function return.
283 void *xmalloc(size_t sz
)
285 void *p
= malloc(sz
);
287 die("not enough memory");
291 /* --- @xstrdup@ --- *
293 * Arguments: @const char *s@ = pointer to a string
295 * Returns: Pointer to a copy of the string.
297 * Use: Copies a string (like @strdup@ would, if it existed).
300 char *xstrdup(const char *s
)
302 size_t sz
= strlen(s
) + 1;
303 char *p
= xmalloc(sz
);
308 /* --- @xrealloc@ --- *
310 * Arguments: @void *p@ = pointer to a block of memory
311 * @size_t sz@ = new size desired for the block
313 * Returns: Pointer to the resized memory block (which is almost
314 * certainly not in the same place any more).
316 * Use: Resizes a memory block.
319 void *xrealloc(void *p
, size_t sz
)
323 die("not enough memory");
327 /*----- That's all, folks -------------------------------------------------*/