chiark / gitweb /
Initial revision
[become] / src / utils.h
1 /* -*-c-*-
2  *
3  * $Id: utils.h,v 1.1 1997/07/21 13:47:42 mdw Exp $
4  *
5  * Miscellaneous useful bits of code.
6  *
7  * (c) 1997 Mark Wooding
8  */
9
10 /*----- Licencing 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
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------*
30  *
31  * $Log: utils.h,v $
32  * Revision 1.1  1997/07/21 13:47:42  mdw
33  * Initial revision
34  *
35  */
36
37 #ifndef UTILS_H
38 #define UTILS_H
39
40 #ifdef __cplusplus
41   extern "C" {
42 #endif
43
44 /*----- Required header files ---------------------------------------------*/
45
46 #include <stddef.h>
47 #include <stdlib.h>
48
49 /*----- Storing and retrieving numbers ------------------------------------*
50  *
51  * These use big-endian conventions, because they seem more usual in network
52  * applications.  I actually think that little-endian is more sensible...
53  */
54
55 #define load32(p)                                                       \
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))
60
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)
66
67 /* --- Little-endian versions (for MD5) --- */
68
69 #define load32_l(p)                                                     \
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))
74
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)
80
81 /*----- Program name handling ---------------------------------------------*/
82
83 /* --- @quis@ --- *
84  *
85  * Arguments:   ---
86  *
87  * Returns:     Pointer to the program name.
88  *
89  * Use:         Returns the program name.
90  */
91
92 extern const char *quis(void);
93
94 /* --- @ego@ --- *
95  *
96  * Arguments:   @const char *p@ = pointer to program name
97  *
98  * Returns:     ---
99  *
100  * Use:         Tells the utils library what the program's name is.
101  */
102
103 extern void ego(const char */*p*/);
104
105 /*----- Error reporting ---------------------------------------------------*/
106
107 /* --- @moan@ --- *
108  *
109  * Arguments:   @const char *f@ = a @printf@-style format string
110  *              @...@ = other arguments
111  *
112  * Returns:     ---
113  *
114  * Use:         Reports an error.
115  */
116
117 extern void moan(const char */*f*/, ...);
118
119 /* --- @die@ --- *
120  *
121  * Arguments:   @const char *f@ = a @printf@-style format string
122  *              @...@ = other arguments
123  *
124  * Returns:     Never.
125  *
126  * Use:         Reports an error and hari-kiris.  Like @moan@ above, only
127  *              more permanent.
128  */
129
130 extern void die(const char */*f*/, ...);
131
132 /*----- Memory management functions ---------------------------------------*/
133
134 /* --- @xmalloc@ --- *
135  *
136  * Arguments:   @size_t sz@ = size of block to allocate
137  *
138  * Returns:     Pointer to allocated block.
139  *
140  * Use:         Allocates memory.  If the memory isn't available, we don't
141  *              hang aroung long enough for a normal function return.
142  */
143
144 extern void *xmalloc(size_t /*sz*/);
145
146 /* --- @xstrdup@ --- *
147  *
148  * Arguments:   @const char *s@ = pointer to a string
149  *
150  * Returns:     Pointer to a copy of the string.
151  *
152  * Use:         Copies a string (like @strdup@ would, if it existed).
153  */
154
155 extern char *xstrdup(const char */*s*/);
156
157 /* --- @xrealloc@ --- *
158  *
159  * Arguments:   @void *p@ = pointer to a block of memory
160  *              @size_t sz@ = new size desired for the block
161  *
162  * Returns:     Pointer to the resized memory block (which is almost
163  *              certainly not in the same place any more).
164  *
165  * Use:         Resizes a memory block.
166  */
167
168 extern void *xrealloc(void */*p*/, size_t /*sz*/);
169
170 /*----- That's all, folks -------------------------------------------------*/
171
172 #ifdef __cplusplus
173   }
174 #endif
175
176 #endif