dce373e6a35146a54db1c489284704c86c79d2d6
[become] / src / utils.h
1 /* -*-c-*-
2 *
3 * $Id: utils.h,v 1.2 1997/08/04 10:24:26 mdw Exp $
4 *
5 * Miscellaneous useful bits of code.
6 *
7 * (c) 1997 Mark Wooding
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of `become'
13 *
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.
18 *
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.
23 *
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.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: utils.h,v $
32 * Revision 1.2 1997/08/04 10:24:26 mdw
33 * Sources placed under CVS control.
34 *
35 * Revision 1.1 1997/07/21 13:47:42 mdw
36 * Initial revision
37 *
38 */
39
40 #ifndef UTILS_H
41 #define UTILS_H
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 /*----- Required header files ---------------------------------------------*/
48
49 #include <stddef.h>
50 #include <stdlib.h>
51
52 /*----- Storing and retrieving numbers ------------------------------------*
53 *
54 * These use big-endian conventions, because they seem more usual in network
55 * applications. I actually think that little-endian is more sensible...
56 */
57
58 #define load32(p) \
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))
63
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)
69
70 /* --- Little-endian versions (for MD5) --- */
71
72 #define load32_l(p) \
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))
77
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)
83
84 /*----- Other macros ------------------------------------------------------*/
85
86 /* --- @burn@ --- *
87 *
88 * Arguments: @obj@ = some object
89 *
90 * Use: Writes zero bytes over the object.
91 */
92
93 #define burn(obj) ((void)memset(&obj, 0, sizeof(obj)))
94
95 /*----- Program name handling ---------------------------------------------*/
96
97 /* --- @quis@ --- *
98 *
99 * Arguments: ---
100 *
101 * Returns: Pointer to the program name.
102 *
103 * Use: Returns the program name.
104 */
105
106 extern const char *quis(void);
107
108 /* --- @ego@ --- *
109 *
110 * Arguments: @const char *p@ = pointer to program name
111 *
112 * Returns: ---
113 *
114 * Use: Tells the utils library what the program's name is.
115 */
116
117 extern void ego(const char */*p*/);
118
119 /*----- Error reporting ---------------------------------------------------*/
120
121 /* --- @moan@ --- *
122 *
123 * Arguments: @const char *f@ = a @printf@-style format string
124 * @...@ = other arguments
125 *
126 * Returns: ---
127 *
128 * Use: Reports an error.
129 */
130
131 extern void moan(const char */*f*/, ...);
132
133 /* --- @die@ --- *
134 *
135 * Arguments: @const char *f@ = a @printf@-style format string
136 * @...@ = other arguments
137 *
138 * Returns: Never.
139 *
140 * Use: Reports an error and hari-kiris. Like @moan@ above, only
141 * more permanent.
142 */
143
144 extern void die(const char */*f*/, ...);
145
146 /*----- Trace messages ----------------------------------------------------*/
147
148 #if !defined(NDEBUG) && !defined(TRACING)
149 # define TRACING
150 #endif
151
152 #ifdef TRACING
153
154 /* --- @trace@ --- *
155 *
156 * Arguments: @unsigned int lvl@ = trace level for output
157 * @const char *f@ = a @printf@-style format string
158 * @...@ = other arguments
159 *
160 * Returns: ---
161 *
162 * Use: Reports a message to the trace output.
163 */
164
165 extern void trace(unsigned int /*lvl*/, const char */*f*/, ...);
166
167 /* --- @traceblk@ --- *
168 *
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
173 *
174 * Returns: ---
175 *
176 * Use: Dumps the contents of a block to the trace output.
177 */
178
179 extern void traceblk(unsigned int /*lvl*/, const char */*hdr*/,
180 const void */*blk*/, size_t /*sz*/);
181
182 /* --- @traceon@ --- *
183 *
184 * Arguments: @FILE *fp@ = a file to trace on
185 * @unsigned int lvl@ = trace level to set
186 *
187 * Returns: ---
188 *
189 * Use: Enables tracing to a file.
190 */
191
192 extern void traceon(FILE */*fp*/, unsigned int /*lvl*/);
193
194 /* --- @tracesetlvl@ --- *
195 *
196 * Arguments: @unsigned int lvl@ = trace level to set
197 *
198 * Returns: ---
199 *
200 * Use: Sets the tracing level.
201 */
202
203 extern void tracesetlvl(unsigned int /*lvl*/);
204
205 /* --- @tracing@ --- *
206 *
207 * Arguments: ---
208 *
209 * Returns: Zero if not tracing, tracing level if tracing.
210 *
211 * Use: Informs the caller whether tracing is enabled.
212 */
213
214 extern unsigned int tracing(void);
215
216 #endif
217
218 /* --- Some hacky macros --- */
219
220 #ifdef TRACING
221 # define T(x) x
222 # define IF_TRACING(lvl, x) if ((lvl) & tracing()) x
223 #else
224 # define T(x)
225 # define IF_TRACING(lvl, x)
226 #endif
227
228 /*----- Memory management functions ---------------------------------------*/
229
230 /* --- @xmalloc@ --- *
231 *
232 * Arguments: @size_t sz@ = size of block to allocate
233 *
234 * Returns: Pointer to allocated block.
235 *
236 * Use: Allocates memory. If the memory isn't available, we don't
237 * hang aroung long enough for a normal function return.
238 */
239
240 extern void *xmalloc(size_t /*sz*/);
241
242 /* --- @xstrdup@ --- *
243 *
244 * Arguments: @const char *s@ = pointer to a string
245 *
246 * Returns: Pointer to a copy of the string.
247 *
248 * Use: Copies a string (like @strdup@ would, if it existed).
249 */
250
251 extern char *xstrdup(const char */*s*/);
252
253 /* --- @xrealloc@ --- *
254 *
255 * Arguments: @void *p@ = pointer to a block of memory
256 * @size_t sz@ = new size desired for the block
257 *
258 * Returns: Pointer to the resized memory block (which is almost
259 * certainly not in the same place any more).
260 *
261 * Use: Resizes a memory block.
262 */
263
264 extern void *xrealloc(void */*p*/, size_t /*sz*/);
265
266 /*----- That's all, folks -------------------------------------------------*/
267
268 #ifdef __cplusplus
269 }
270 #endif
271
272 #endif