Add printf-style format checking
[openafs.git] / src / config / stds.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #ifndef OPENAFS_AFS_CONFIG_STDS_H
11 #define OPENAFS_AFS_CONFIG_STDS_H       1
12
13 #include <afs/param.h>
14 #include <sys/types.h>
15
16 #define IN                      /* indicates a parameter is read in */
17 #define OUT                     /* indicates a parameter is sent out (a ptr) */
18 #define INOUT                   /* indicates a parameter is read in and sent out (a ptr) */
19
20 #ifndef MACRO_BEGIN
21 #define MACRO_BEGIN     do {
22 #endif
23 #ifndef MACRO_END
24 #define MACRO_END       } while (0)
25 #endif
26
27 typedef void *opaque;
28
29 #ifndef _ATT4
30 #if defined(__HIGHC__)
31 /*
32  * keep HC from complaining about the use of "old-style" function definitions
33  * with prototypes
34  */
35 pragma Off(Prototype_override_warnings);
36 #endif /* defined(__HIGHC__) */
37 #endif
38
39 /* Now some types to enhance portability.  Always use these on the wire or when
40  * laying out shared structures on disk. */
41
42 /* Imagine that this worked...
43 #if (sizeof(long) != 4) || (sizeof(short) != 2)
44 #error We require size of long and pointers to be equal
45 #endif */
46
47 #define MAX_AFS_INT32 0x7FFFFFFF
48 #define MIN_AFS_INT32 (-MAX_AFS_INT32 - 1)
49 #define MAX_AFS_UINT32 0xFFFFFFFF
50 #define MAX_AFS_INT64 0x7FFFFFFFFFFFFFFFL
51 #define MIN_AFS_INT64 (-MAX_AFS_INT64 - 1)
52 #define MAX_AFS_UINT64 0xFFFFFFFFFFFFFFFFL
53
54 typedef short afs_int16;
55 typedef unsigned short afs_uint16;
56 #ifdef  AFS_64BIT_ENV
57 typedef int afs_int32;
58 typedef unsigned int afs_uint32;
59 #if defined(AFS_NT40_ENV) && defined(_MSC_VER)
60 typedef __int64 afs_int64;
61 typedef unsigned __int64 afs_uint64;
62 #else
63 typedef long long afs_int64;
64 typedef unsigned long long afs_uint64;
65 #endif
66 #define ZeroInt64(a)       (a = 0)
67 #define AssignInt64(a, b) *(b) = (a) 
68 #define IncInt64(a) (*(a))++
69 #define IncUInt64(a) (*(a))++
70 #define DecInt64(a) (*(a))--
71 #define DecUInt64(a) (*(a))--
72 #define GTInt64(a,b) ((a) > (b))
73 #define GEInt64(a,b) ((a) >= (b))
74 #define LEInt64(a,b) ((a) <= (b))
75 #define LTInt64(a,b) ((a) < (b))
76 #define AddInt64(a,b,c) *(c) = (afs_int64)(a) + (afs_int64)(b)
77 #define AddUInt64(a,b,c) *(c) = (afs_uint64)(a) + (afs_uint64)(b)
78 #define SubtractInt64(a,b,c) *(c) = (afs_int64)(a) - (afs_int64)(b)
79 #define SubtractUInt64(a,b,c) *(c) = (afs_uint64)(a) - (afs_uint64)(b)
80 #define CompareInt64(a,b) (afs_int64)(a) - (afs_int64)(b)
81 #define CompareUInt64(a,b) (afs_uint64)(a) - (afs_uint64)(b)
82 #define NonZeroInt64(a)                (a)
83 #define Int64ToInt32(a)    (a) & MAX_AFS_UINT32
84 #define FillInt64(t,h,l) (t) = ((afs_int64)(h) << 32) | (l);
85 #define SplitInt64(t,h,l) (h) = ((afs_int64)t) >> 32; (l) = (t) & MAX_AFS_UINT32;
86 #define RoundInt64ToInt32(a)    (a > MAX_AFS_UINT32) ? MAX_AFS_UINT32 : a;
87 #define RoundInt64ToInt31(a)    (a > MAX_AFS_INT32) ? MAX_AFS_INT32 : a;
88 #else /* AFS_64BIT_ENV */
89 typedef long afs_int32;
90 typedef unsigned long afs_uint32;
91
92 struct Int64 {
93     afs_int32 high;
94     afs_uint32 low;
95 };
96 typedef struct Int64 afs_int64;
97
98 struct u_Int64 {
99     afs_uint32 high;
100     afs_uint32 low;
101 };
102 typedef struct u_Int64 afs_uint64;
103 #define ZeroInt64(a) ((a).high = (a).low = 0)
104 #define AssignInt64(a, b) (b)->high = (a).high; (b)->low = (a).low
105 #define IncInt64(a) ((++((a)->low)) ? 0 : (a)->high++ )
106 #define IncUInt64(a) ((++((a)->low)) ? 0 : (a)->high++ )
107 #define DecInt64(a) (((a)->low)-- ? 0 : (a)->high-- )
108 #define DecUInt64(a) (((a)->low)-- ? 0 : (a)->high-- )
109 #define GTInt64(a,b) (((a).high > (b).high) || (((a).high == (b).high) && ((a).low > (b).low)))
110 #define GEInt64(a,b) (((a).high > (b).high) || (((a).high == (b).high) && ((a).low >= (b).low)))
111 #define LEInt64(a,b) (((a).high < (b).high) || (((a).high == (b).high) && ((a).low <= (b).low)))
112 #define LTInt64(a,b) (((a).high < (b).high) || (((a).high == (b).high) && ((a).low < (b).low)))
113 #define CompareInt64(a,b) (((afs_int32)(a).high - (afs_int32)(b).high) || (((a).high == (b).high) && ((a).low - (b).low))) 
114 #define AddInt64(a, b, c) {  afs_int64 _a, _b; _a = a; _b = b; (c)->low = _a.low + _b.low; (c)->high = _a.high + _b.high + ((c)->low < _b.low); } 
115 #define SubtractInt64(a, b, c) { afs_int64 _a, _b; _a = a; _b = b; (c)->low = _a.low - _b.low;  (c)->high = _a.high - _b.high - (_a.low < _b.low); } 
116 #define CompareUInt64(a,b) (((afs_uint32)(a).high - (afs_uint32)(b).high) || (((a).high == (b).high) && ((a).low - (b).low))) 
117 #define AddUInt64(a, b, c) {  afs_uint64 _a, _b; _a = a; _b = b; (c)->low = _a.low + _b.low; (c)->high = _a.high + _b.high + ((c)->low < _b.low); } 
118 #define SubtractUInt64(a, b, c) { afs_uint64 _a, _b; _a = a; _b = b; (c)->low = _a.low - _b.low;  (c)->high = _a.high - _b.high - (_a.low < _b.low); } 
119 #define NonZeroInt64(a)   (a).low || (a).high
120 #define Int64ToInt32(a)    (a).low
121 #define FillInt64(t,h,l) (t).high = (h); (t).low = (l);
122 #define SplitInt64(t,h,l) (h) = (t).high; (l) = (t).low;
123 #define RoundInt64ToInt32(a)    (a.high > 0) ? MAX_AFS_UINT32 : a.low;
124 #define RoundInt64ToInt31(a)    (a.high > 0) ? MAX_AFS_INT32 : a.low;
125 #endif /* AFS_64BIT_ENV */
126
127 /* AFS_64BIT_CLIENT should presently be set only for AFS_64BIT_ENV systems */
128
129 #ifdef AFS_64BIT_CLIENT
130 typedef afs_int64 afs_size_t;
131 typedef afs_uint64 afs_offs_t;
132 #else /* AFS_64BIT_CLIENT */
133 typedef afs_int32 afs_size_t;
134 typedef afs_uint32 afs_offs_t;
135 #endif /* AFS_64BIT_CLIENT */
136
137 #ifdef AFS_LARGEFILE_ENV
138 typedef afs_int64 afs_foff_t;
139 typedef afs_uint64 afs_fsize_t;
140 typedef afs_int64 afs_sfsize_t;
141 #define SplitOffsetOrSize(t,h,l) SplitInt64(t,h,l)
142 #else /* !AFS_LARGEFILE_ENV */
143 typedef afs_int32 afs_foff_t;
144 typedef afs_uint32 afs_fsize_t;
145 typedef afs_int32 afs_sfsize_t;
146 #define SplitOffsetOrSize(t,h,l) (h) = 0; (l) = (t);
147 #endif /* !AFS_LARGEFILE_ENV */
148
149 /* Maximum integer sizes.  Also what is expected by %lld, %llu in
150  * afs_snprintf. */
151 #ifdef AFS_64BIT_CLIENT
152 typedef afs_int64 afs_intmax_t;
153 typedef afs_uint64 afs_uintmax_t;
154 #else /* !AFS_64BIT_CLIENT */
155 typedef afs_int32 afs_intmax_t;
156 typedef afs_uint32 afs_uintmax_t;
157 #endif /* !AFS_64BIT_CLIENT */
158
159 /* you still have to include <netinet/in.h> to make these work */
160
161 #define hton32 htonl
162 #define hton16 htons
163 #define ntoh32 ntohl
164 #define ntoh16 ntohs
165
166
167 /* Since there is going to be considerable use of 64 bit integers we provide
168  * some assistence in this matter.  The hyper type is supposed to be compatible
169  * with the afsHyper type: the same macros will work on both. */
170
171 #if     defined(AFS_64BIT_ENV) && 0
172
173 typedef unsigned long afs_hyper_t;
174
175 #define hcmp(a,b)       ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0))
176 #define hsame(a,b)      ((a) == (b))
177 #define hiszero(a)      ((a) == 0)
178 #define hfitsin32(a)    ((a) & 0xffffffff00000000) == 0)
179 #define hset(a,b)       ((a) = (b))
180 #define hzero(a)        ((a) = 0)
181 #define hones(a)        ((a) = ~((unsigned long)0))
182 #define hget32(i,a)     ((i) = (unsigned int)(a))
183 #define hget64(hi,lo,a) ((lo) = ((unsigned int)(a)), (hi) = ((a) & (0xffffffff00000000)))
184 #define hset32(a,i)     ((a) = ((unsigned int)(i)))
185 #define hset64(a,hi,lo) ((a) = (((hi) << 32) | (lo)))
186 #define hgetlo(a)       ((a) & 0xffffffff)
187 #define hgethi(a)       (((unsigned int)(a)) >> 32)
188 #define hadd(a,b)       ((a) += (b))
189 /* XXX */
190 #define hadd32(a,b)     ((a) += (b))
191 #define hshlft(a,n)     ((a)<<(n))
192
193 #else /* AFS_64BIT_ENV */
194
195 typedef struct afs_hyper_t {    /* unsigned 64 bit integers */
196     unsigned int high;
197     unsigned int low;
198 } afs_hyper_t;
199
200 #define hcmp(a,b) ((a).high<(b).high? -1 : ((a).high > (b).high? 1 : \
201     ((a).low <(b).low? -1 : ((a).low > (b).low? 1 : 0))))
202 #define hsame(a,b) ((a).low == (b).low && (a).high == (b).high)
203 #define hiszero(a) ((a).low == 0 && (a).high == 0)
204 #define hfitsin32(a) ((a).high == 0)
205 #define hset(a,b) ((a) = (b))
206 #define hzero(a) ((a).low = 0, (a).high = 0)
207 #define hones(a) ((a).low = 0xffffffff, (a).high = 0xffffffff)
208 #define hget32(i,a) ((i) = (a).low)
209 #define hget64(hi,lo,a) ((lo) = (a).low, (hi) = (a).high)
210 #define hset32(a,i) ((a).high = 0, (a).low = (i))
211 #define hset64(a,hi,lo) ((a).high = (hi), (a).low = (lo))
212 #define hgetlo(a) ((a).low)
213 #define hgethi(a) ((a).high)
214 #define hshlft(a,n)                                                        \
215      { /*Shift Left n bits*/                                               \
216         int s = sizeof((a).low) * 8; /*#bits*/                             \
217         if ((n) <= 0) { /*No shift*/                                       \
218         } else if ((n) >= 2*s) { /*Shift off all bits*/                    \
219            (a).high = (a).low = 0;                                         \
220         } else if ((n) < s) { /*Part of low shifted into high*/            \
221            (a).high = ((a).high<<(n)) | (((a).low>>(s-(n))) & (1<<(n))-1); \
222            (a).low  = (a).low << (n);                                      \
223         } else if ((n) >= s) { /*All of low shifted into high plus some*/  \
224            (a).high = (a).low << ((n)-s);                                  \
225            (a).low=0;                                                      \
226         }                                                                  \
227      }
228
229 /* The algorithm here is to check for two cases that cause a carry.  If the top
230  * two bits are different then if the sum has the top bit off then there must
231  * have been a carry.  If the top bits are both one then there is always a
232  * carry.  We assume 32 bit ints and twos complement arithmetic. */
233
234 #define SIGN 0x80000000
235 #define hadd32(a,i) \
236     ((void)((((a).low ^ (int)(i)) & SIGN) \
237       ? (((((a).low + (int)(i)) & SIGN) == 0) && (a).high++) \
238       : (((a).low & (int)(i) & SIGN) && (a).high++)), \
239      (a).low += (int)(i))
240
241 #define hadd(a,b) (hadd32(a,(b).low), (a).high += (b).high)
242 #endif /* AFS_64BIT_ENV */
243
244 #ifndef KERNEL
245 #ifndef AFS_NT40_ENV
246 #define max(a, b)               ((a) < (b) ? (b) : (a))
247 #define min(a, b)               ((a) > (b) ? (b) : (a))
248 #endif
249 /*#define abs(x)                  ((x) >= 0 ? (x) : -(x))*/
250 #endif
251
252 /* minumum length of string to pass to int_to_base64 */
253 typedef char b64_string_t[8];
254 #ifndef AFS_NT40_ENV
255 #if defined(AFS_HPUX_ENV) || defined(AFS_USR_HPUX_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV))
256 char *int_to_base64();
257 int base64_to_int();
258 #else
259 char *int_to_base64(b64_string_t s, int a);
260 int base64_to_int(char *s);
261 #endif
262 #endif /* AFS_NT40_ENV */
263 /*
264  * The afsUUID data type is built in to RX
265  */
266 struct afsUUID {
267     afs_uint32 time_low;
268     afs_uint16 time_mid;
269     afs_uint16 time_hi_and_version;
270     char clock_seq_hi_and_reserved;
271     char clock_seq_low;
272     char node[6];
273 };
274 typedef struct afsUUID afsUUID;
275
276 /* for now, demand attach fileserver is only support on unix pthreads builds */
277 #if defined(DEMAND_ATTACH_ENABLE) && defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
278 #define AFS_DEMAND_ATTACH_FS 1
279 #endif
280
281 /* A macro that can be used when printf'ing 64 bit integers, as Unix and 
282  * windows use a different format string
283  */
284 #ifdef AFS_NT40_ENV
285 # define AFS_INT64_FMT "I64d"
286 # define AFS_UINT64_FMT "I64u"
287 # define AFS_PTR_FMT   "p"
288 # define AFS_SIZET_FMT "Iu"
289 #else
290 # define AFS_INT64_FMT "lld"
291 # define AFS_UINT64_FMT "llu"
292 # define AFS_PTR_FMT   "p"
293 # ifdef PRINTF_TAKES_Z_LEN
294 #  define AFS_SIZET_FMT "zu"
295 # else
296 #  define AFS_SIZET_FMT "lu"
297 # endif /* PRINTF_TAKES_Z_LEN */
298 #endif /* AFS_NT40_ENV */
299
300 /* Functions to safely cast afs_int32 and afs_uint32 so they can be used in 
301  * printf statemements with %ld and %lu
302  */
303 #ifdef AFS_NT40_ENV
304 #define static_inline __inline static
305 #define hdr_static_inline(x) __inline static x
306 #elif defined(AFS_HPUX_ENV) || defined(AFS_USR_HPUX_ENV)
307 /* The HPUX compiler can segfault on 'static __inline', so fall back to
308  * just 'static' so we can at least compile */
309 #define static_inline static
310 #define hdr_static_inline(x) static x
311 #elif defined(AFS_AIX_ENV) || defined(AFS_USR_AIX_ENV)
312 #define static_inline static
313 #define hdr_static_inline(x) static x
314 #elif defined(AFS_SGI_ENV) || defined(AFS_USR_SGI_ENV)
315 #define static_inline static
316 #define hdr_static_inline(x) x
317 #else
318 #define static_inline static inline
319 #define hdr_static_inline(x) static inline x
320 #endif
321
322 hdr_static_inline(long) afs_printable_int32_ld(afs_int32 d) { return (long) d; }
323
324 hdr_static_inline(unsigned long) afs_printable_uint32_lu(afs_uint32 d) { return (unsigned long) d; }
325
326 #ifdef AFS_64BITUSERPOINTER_ENV
327 #define afs_pointer_to_int(p)      ((afs_uint32)  (afs_uint64) (p))
328 #define afs_int_to_pointer(i)     ((void *) (afs_uint64) (i))
329 #else
330 #define afs_pointer_to_int(p)      ((afs_uint32)   (p))
331 #define afs_int_to_pointer(i)      ((void *)  (i))
332 #endif
333
334 #if !defined(__GNUC__) || __GNUC__ < 2
335 #define AFS_UNUSED
336 #define AFS_ATTRIBUTE_FORMAT(style,x,y)
337 #else
338 #define AFS_UNUSED __attribute__((unused))
339 #define AFS_ATTRIBUTE_FORMAT(style,x,y) __attribute__((format(style, x, y)))
340 #endif
341
342 #endif /* OPENAFS_CONFIG_AFS_STDS_H */