3 * $Id: utils.h,v 1.1 1997/07/21 13:47:42 mdw Exp $
5 * Miscellaneous useful bits of code.
7 * (c) 1997 Mark Wooding
10 /*----- Licencing 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
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1997/07/21 13:47:42 mdw
44 /*----- Required header files ---------------------------------------------*/
49 /*----- Storing and retrieving numbers ------------------------------------*
51 * These use big-endian conventions, because they seem more usual in network
52 * applications. I actually think that little-endian is more sensible...
56 ((((unsigned char)(p)[0] & 0xFF) << 24) | \
57 (((unsigned char)(p)[1] & 0xFF) << 16) | \
58 (((unsigned char)(p)[2] & 0xFF) << 8) | \
59 (((unsigned char)(p)[3] & 0xFF) << 0))
61 #define store32(p, v) \
62 ((p)[0] = ((unsigned long)(v) >> 24) & 0xFF, \
63 (p)[1] = ((unsigned long)(v) >> 16) & 0xFF, \
64 (p)[2] = ((unsigned long)(v) >> 8) & 0xFF, \
65 (p)[3] = ((unsigned long)(v) >> 0) & 0xFF)
67 /* --- Little-endian versions (for MD5) --- */
70 ((((unsigned char)(p)[0] & 0xFF) << 0) | \
71 (((unsigned char)(p)[1] & 0xFF) << 8) | \
72 (((unsigned char)(p)[2] & 0xFF) << 16) | \
73 (((unsigned char)(p)[3] & 0xFF) << 24))
75 #define store32_l(p, v) \
76 ((p)[0] = ((unsigned long)(v) >> 0) & 0xFF, \
77 (p)[1] = ((unsigned long)(v) >> 8) & 0xFF, \
78 (p)[2] = ((unsigned long)(v) >> 16) & 0xFF, \
79 (p)[3] = ((unsigned long)(v) >> 24) & 0xFF)
81 /*----- Program name handling ---------------------------------------------*/
87 * Returns: Pointer to the program name.
89 * Use: Returns the program name.
92 extern const char *quis(void);
96 * Arguments: @const char *p@ = pointer to program name
100 * Use: Tells the utils library what the program's name is.
103 extern void ego(const char */*p*/);
105 /*----- Error reporting ---------------------------------------------------*/
109 * Arguments: @const char *f@ = a @printf@-style format string
110 * @...@ = other arguments
114 * Use: Reports an error.
117 extern void moan(const char */*f*/, ...);
121 * Arguments: @const char *f@ = a @printf@-style format string
122 * @...@ = other arguments
126 * Use: Reports an error and hari-kiris. Like @moan@ above, only
130 extern void die(const char */*f*/, ...);
132 /*----- Memory management functions ---------------------------------------*/
134 /* --- @xmalloc@ --- *
136 * Arguments: @size_t sz@ = size of block to allocate
138 * Returns: Pointer to allocated block.
140 * Use: Allocates memory. If the memory isn't available, we don't
141 * hang aroung long enough for a normal function return.
144 extern void *xmalloc(size_t /*sz*/);
146 /* --- @xstrdup@ --- *
148 * Arguments: @const char *s@ = pointer to a string
150 * Returns: Pointer to a copy of the string.
152 * Use: Copies a string (like @strdup@ would, if it existed).
155 extern char *xstrdup(const char */*s*/);
157 /* --- @xrealloc@ --- *
159 * Arguments: @void *p@ = pointer to a block of memory
160 * @size_t sz@ = new size desired for the block
162 * Returns: Pointer to the resized memory block (which is almost
163 * certainly not in the same place any more).
165 * Use: Resizes a memory block.
168 extern void *xrealloc(void */*p*/, size_t /*sz*/);
170 /*----- That's all, folks -------------------------------------------------*/