3 * $Id: utils.h,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
47 /*----- Required header files ---------------------------------------------*/
52 /*----- Storing and retrieving numbers ------------------------------------*
54 * These use big-endian conventions, because they seem more usual in network
55 * applications. I actually think that little-endian is more sensible...
59 ((((unsigned char)(p)[0] & 0xFF) << 24) | \
60 (((unsigned char)(p)[1] & 0xFF) << 16) | \
61 (((unsigned char)(p)[2] & 0xFF) << 8) | \
62 (((unsigned char)(p)[3] & 0xFF) << 0))
64 #define store32(p, v) \
65 ((p)[0] = ((unsigned long)(v) >> 24) & 0xFF, \
66 (p)[1] = ((unsigned long)(v) >> 16) & 0xFF, \
67 (p)[2] = ((unsigned long)(v) >> 8) & 0xFF, \
68 (p)[3] = ((unsigned long)(v) >> 0) & 0xFF)
70 /* --- Little-endian versions (for MD5) --- */
73 ((((unsigned char)(p)[0] & 0xFF) << 0) | \
74 (((unsigned char)(p)[1] & 0xFF) << 8) | \
75 (((unsigned char)(p)[2] & 0xFF) << 16) | \
76 (((unsigned char)(p)[3] & 0xFF) << 24))
78 #define store32_l(p, v) \
79 ((p)[0] = ((unsigned long)(v) >> 0) & 0xFF, \
80 (p)[1] = ((unsigned long)(v) >> 8) & 0xFF, \
81 (p)[2] = ((unsigned long)(v) >> 16) & 0xFF, \
82 (p)[3] = ((unsigned long)(v) >> 24) & 0xFF)
84 /*----- Other macros ------------------------------------------------------*/
88 * Arguments: @obj@ = some object
90 * Use: Writes zero bytes over the object.
93 #define burn(obj) ((void)memset(&obj, 0, sizeof(obj)))
95 /*----- Program name handling ---------------------------------------------*/
101 * Returns: Pointer to the program name.
103 * Use: Returns the program name.
106 extern const char *quis(void);
110 * Arguments: @const char *p@ = pointer to program name
114 * Use: Tells the utils library what the program's name is.
117 extern void ego(const char */
*p*/
);
119 /*----- Error reporting ---------------------------------------------------*/
123 * Arguments: @const char *f@ = a @printf@-style format string
124 * @...@ = other arguments
128 * Use: Reports an error.
131 extern void moan(const char */
*f*/
, ...);
135 * Arguments: @const char *f@ = a @printf@-style format string
136 * @...@ = other arguments
140 * Use: Reports an error and hari-kiris. Like @moan@ above, only
144 extern void die(const char */
*f*/
, ...);
146 /*----- Trace messages ----------------------------------------------------*/
148 #if !defined(NDEBUG) && !defined(TRACING)
156 * Arguments: @unsigned int lvl@ = trace level for output
157 * @const char *f@ = a @printf@-style format string
158 * @...@ = other arguments
162 * Use: Reports a message to the trace output.
165 extern void trace(unsigned int /*lvl*/, const char */
*f*/
, ...);
167 /* --- @traceblk@ --- *
169 * Arguments: @unsigned int lvl@ = trace level for output
170 * @const char *hdr@ = some header string to write
171 * @const void *blk@ = pointer to a block of memory to dump
172 * @size_t sz@ = size of the block of memory
176 * Use: Dumps the contents of a block to the trace output.
179 extern void traceblk(unsigned int /*lvl*/, const char */
*hdr*/
,
180 const void */
*blk*/
, size_t /*sz*/);
182 /* --- @traceon@ --- *
184 * Arguments: @FILE *fp@ = a file to trace on
185 * @unsigned int lvl@ = trace level to set
189 * Use: Enables tracing to a file.
192 extern void traceon(FILE */
*fp*/
, unsigned int /*lvl*/);
194 /* --- @tracesetlvl@ --- *
196 * Arguments: @unsigned int lvl@ = trace level to set
200 * Use: Sets the tracing level.
203 extern void tracesetlvl(unsigned int /*lvl*/);
205 /* --- @tracing@ --- *
209 * Returns: Zero if not tracing, tracing level if tracing.
211 * Use: Informs the caller whether tracing is enabled.
214 extern unsigned int tracing(void);
218 /* --- Some hacky macros --- */
222 # define IF_TRACING(lvl, x) if ((lvl) & tracing()) x
225 # define IF_TRACING(lvl, x)
228 /*----- Memory management functions ---------------------------------------*/
230 /* --- @xmalloc@ --- *
232 * Arguments: @size_t sz@ = size of block to allocate
234 * Returns: Pointer to allocated block.
236 * Use: Allocates memory. If the memory isn't available, we don't
237 * hang aroung long enough for a normal function return.
240 extern void *xmalloc(size_t /*sz*/);
242 /* --- @xstrdup@ --- *
244 * Arguments: @const char *s@ = pointer to a string
246 * Returns: Pointer to a copy of the string.
248 * Use: Copies a string (like @strdup@ would, if it existed).
251 extern char *xstrdup(const char */
*s*/
);
253 /* --- @xrealloc@ --- *
255 * Arguments: @void *p@ = pointer to a block of memory
256 * @size_t sz@ = new size desired for the block
258 * Returns: Pointer to the resized memory block (which is almost
259 * certainly not in the same place any more).
261 * Use: Resizes a memory block.
264 extern void *xrealloc(void */
*p*/
, size_t /*sz*/);
266 /*----- That's all, folks -------------------------------------------------*/