compat_defs.h revision 1.19 1 /* $NetBSD: compat_defs.h,v 1.19 2002/09/30 00:39:21 lukem Exp $ */
2
3 #ifndef __NETBSD_COMPAT_DEFS_H__
4 #define __NETBSD_COMPAT_DEFS_H__
5
6 /* Work around some complete brain damage. */
7
8 /*
9 * Linux: <features.h> turns on _POSIX_SOURCE by default, even though the
10 * program (not the OS) should do that. Preload <features.h> to keep any
11 * of this crap from being pulled in, and undefine _POSIX_SOURCE.
12 */
13
14 #if defined(__linux__) && HAVE_FEATURES_H
15 #include <features.h>
16 #endif
17
18 #undef _POSIX_SOURCE
19 #undef _POSIX_C_SOURCE
20
21 /* System headers needed for (re)definitions below. */
22
23 #include <sys/types.h>
24 #include <sys/mman.h>
25 #include <sys/param.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <grp.h>
31 #include <limits.h>
32 #include <paths.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #if HAVE_SYS_CDEFS_H
39 #include <sys/cdefs.h>
40 #endif
41 #if HAVE_SYS_SYSLIMITS_H
42 #include <sys/syslimits.h>
43 #endif
44 #if HAVE_SYS_SYSMACROS_H
45 /* major(), minor() on SVR4 */
46 #include <sys/sysmacros.h>
47 #endif
48 #if HAVE_INTTYPES_H
49 #include <inttypes.h>
50 #endif
51 #if HAVE_STDDEF_H
52 #include <stddef.h>
53 #endif
54
55 /* We don't include <pwd.h> here, so that "compat_pwd.h" works. */
56 struct passwd;
57
58 /* Assume an ANSI compiler for the host. */
59
60 #undef __P
61 #define __P(x) x
62
63 #ifndef __BEGIN_DECLS
64 #define __BEGIN_DECLS
65 #endif
66 #ifndef __END_DECLS
67 #define __END_DECLS
68 #endif
69
70 /* Some things usually in BSD <sys/cdefs.h>. */
71
72 #ifndef __CONCAT
73 #define __CONCAT(x,y) x ## y
74 #endif
75 #if !defined(__attribute__) && !defined(__GNUC__)
76 #define __attribute__(x)
77 #endif
78 #ifndef __RENAME
79 #define __RENAME(x)
80 #endif
81 #undef __aconst
82 #define __aconst
83 #undef __dead
84 #define __dead
85 #undef __restrict
86 #define __restrict
87
88 /* Dirent support. */
89
90 #if HAVE_DIRENT_H
91 # include <dirent.h>
92 # define NAMLEN(dirent) (strlen((dirent)->d_name))
93 #else
94 # define dirent direct
95 # define NAMLEN(dirent) ((dirent)->d_namlen)
96 # if HAVE_SYS_NDIR_H
97 # include <sys/ndir.h>
98 # endif
99 # if HAVE_SYS_DIR_H
100 # include <sys/dir.h>
101 # endif
102 # if HAVE_NDIR_H
103 # include <ndir.h>
104 # endif
105 #endif
106
107 /* Type substitutes. */
108
109 #if !HAVE_ID_T
110 typedef unsigned long id_t;
111 #endif
112
113 /* Prototypes for replacement functions. */
114
115 #if !HAVE_ASPRINTF
116 int asprintf(char **, const char *, ...);
117 #endif
118
119 #if !HAVE_ASNPRINTF
120 int asnprintf(char **, size_t, const char *, ...);
121 #endif
122
123 #if !HAVE_BASENAME
124 char *basename(char *);
125 #endif
126
127 #if !HAVE_DECL_OPTIND
128 int getopt(int, char *const *, const char *);
129 extern char *optarg;
130 extern int optind, opterr, optopt;
131 #endif
132
133 #if !HAVE_DIRNAME
134 char *dirname(char *);
135 #endif
136
137 #if !HAVE_DIRFD
138 #if HAVE_DIR_DD_FD
139 #define dirfd(dirp) ((dirp)->dd_fd)
140 #else
141 #error cannot figure out how to turn a DIR * into a fd
142 #endif
143 #endif
144
145 #if !HAVE_ERR_H
146 void err(int, const char *, ...);
147 void errx(int, const char *, ...);
148 void warn(const char *, ...);
149 void warnx(const char *, ...);
150 #endif
151
152 #if !HAVE_FGETLN
153 char *fgetln(FILE *, size_t *);
154 #endif
155
156 #if !HAVE_FLOCK
157 # define LOCK_SH 0x01
158 # define LOCK_EX 0x02
159 # define LOCK_NB 0x04
160 # define LOCK_UN 0x08
161 int flock(int, int);
162 #endif
163
164 #if !HAVE_FPARSELN
165 # define FPARSELN_UNESCESC 0x01
166 # define FPARSELN_UNESCCONT 0x02
167 # define FPARSELN_UNESCCOMM 0x04
168 # define FPARSELN_UNESCREST 0x08
169 # define FPARSELN_UNESCALL 0x0f
170 char *fparseln(FILE *, size_t *, size_t *, const char [3], int);
171 #endif
172
173 #if !HAVE_ISSETUGID
174 int issetugid(void);
175 #endif
176
177 #if !HAVE_ISBLANK && !defined(isblank)
178 #define isblank(x) ((x) == ' ' || (x) == '\t')
179 #endif
180
181 #if !HAVE_MACHINE_BSWAP_H
182 #define bswap16(x) ((((x) << 8) & 0xff00) | (((x) >> 8) & 0x00ff))
183
184 #define bswap32(x) ((((x) << 24) & 0xff000000) | \
185 (((x) << 8) & 0x00ff0000) | \
186 (((x) >> 8) & 0x0000ff00) | \
187 (((x) >> 24) & 0x000000ff))
188
189 #define bswap64(x) (((u_int64_t)bswap32((x)) << 32) | \
190 ((u_int64_t)bswap32((x) >> 32)))
191 #endif
192
193 #if !HAVE_MKSTEMP
194 int mkstemp(char *);
195 #endif
196
197 #if !HAVE_MKDTEMP
198 char *mkdtemp(char *);
199 #endif
200
201 #if !HAVE_MKSTEMP || !HAVE_MKDTEMP
202 /* This is a prototype for the internal function. */
203 int gettemp(char *, int *, int);
204 #endif
205
206 #if !HAVE_PREAD
207 ssize_t pread(int, void *, size_t, off_t);
208 #endif
209
210 #if !HAVE_PWCACHE_USERDB
211 int uid_from_user(const char *, uid_t *);
212 int pwcache_userdb(int (*)(int), void (*)(void),
213 struct passwd * (*)(const char *), struct passwd * (*)(uid_t));
214 int gid_from_group(const char *, gid_t *);
215 int pwcache_groupdb(int (*)(int), void (*)(void),
216 struct group * (*)(const char *), struct group * (*)(gid_t));
217 #endif
218
219 #if !HAVE_PWRITE
220 ssize_t pwrite(int, const void *, size_t, off_t);
221 #endif
222
223 #if !HAVE_SETENV
224 int setenv(const char *, const char *, int);
225 #endif
226
227 #if !HAVE_DECL_SETGROUPENT
228 int setgroupent(int);
229 #endif
230
231 #if !HAVE_DECL_SETPASSENT
232 int setpassent(int);
233 #endif
234
235 #if !HAVE_SETPROGNAME
236 const char *getprogname(void);
237 void setprogname(const char *);
238 #endif
239
240 #if !HAVE_SNPRINTF
241 int snprintf(char *, size_t, const char *, ...);
242 #endif
243
244 #if !HAVE_STRLCAT
245 size_t strlcat(char *, const char *, size_t);
246 #endif
247
248 #if !HAVE_STRLCPY
249 size_t strlcpy(char *, const char *, size_t);
250 #endif
251
252 #if !HAVE_STRSEP
253 char *strsep(char **, const char *);
254 #endif
255
256 #if !HAVE_USER_FROM_UID
257 const char *user_from_uid(uid_t, int);
258 const char *group_from_gid(gid_t, int);
259 #endif
260
261 #if !HAVE_VASPRINTF
262 int vasprintf(char **, const char *, va_list);
263 #endif
264
265 #if !HAVE_VASNPRINTF
266 int vasnprintf(char **, size_t, const char *, va_list);
267 #endif
268
269 #if !HAVE_VSNPRINTF
270 int vsnprintf(char *, size_t, const char *, va_list);
271 #endif
272
273 /*
274 * getmode() and setmode() are always defined, as these function names
275 * exist but with very different meanings on other OS's. The compat
276 * versions here simply accept an octal mode number; the "u+x,g-w" type
277 * of syntax is not accepted.
278 */
279
280 #define getmode __nbcompat_getmode
281 #define setmode __nbcompat_setmode
282
283 mode_t getmode(const void *, mode_t);
284 void *setmode(const char *);
285
286 /* Eliminate assertions embedded in binaries. */
287
288 #undef _DIAGASSERT
289 #define _DIAGASSERT(x)
290
291 /* Heimdal expects this one. */
292
293 #undef RCSID
294 #define RCSID(x)
295
296 /* Some definitions not available on all systems. */
297
298 /* <errno.h> */
299
300 #ifndef EFTYPE
301 #define EFTYPE EIO
302 #endif
303
304 /* <fcntl.h> */
305
306 #ifndef O_EXLOCK
307 #define O_EXLOCK 0
308 #endif
309 #ifndef O_SHLOCK
310 #define O_SHLOCK 0
311 #endif
312
313 /* <limits.h> */
314
315 #ifndef UID_MAX
316 #define UID_MAX 32767
317 #endif
318 #ifndef GID_MAX
319 #define GID_MAX UID_MAX
320 #endif
321
322 #ifndef UQUAD_MAX
323 #define UQUAD_MAX ((u_quad_t)-1)
324 #endif
325 #ifndef QUAD_MAX
326 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
327 #endif
328 #ifndef QUAD_MIN
329 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
330 #endif
331 #ifndef ULLONG_MAX
332 #define ULLONG_MAX ((unsigned long long)-1)
333 #endif
334 #ifndef LLONG_MAX
335 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
336 #endif
337 #ifndef LLONG_MIN
338 #define LLONG_MIN ((long long)(~LLONG_MAX))
339 #endif
340
341 /* <paths.h> */
342
343 #ifndef _PATH_BSHELL
344 #define _PATH_BSHELL PATH_BSHELL
345 #endif
346 #ifndef _PATH_DEFPATH
347 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
348 #endif
349 #ifndef _PATH_DEV
350 #define _PATH_DEV "/dev/"
351 #endif
352 #ifndef _PATH_DEVNULL
353 #define _PATH_DEVNULL _PATH_DEV "null"
354 #endif
355 #ifndef _PATH_TMP
356 #define _PATH_TMP "/tmp/"
357 #endif
358
359 /* <stdarg.h> */
360
361 #ifndef _BSD_VA_LIST_
362 #define _BSD_VA_LIST_ va_list
363 #endif
364
365 /* <stdint.h> */
366
367 #ifndef UINT32_MAX
368 #define UINT32_MAX 0xffffffffU
369 #endif
370
371 /* <stdlib.h> */
372
373 #ifndef __GNUC__
374 # if HAVE_ALLOCA_H
375 # include <alloca.h>
376 # else
377 # ifndef alloca /* predefined by HP cc +Olibcalls */
378 char *alloca ();
379 # endif
380 # endif
381 #endif
382
383 /* <sys/endian.h> */
384
385 #ifndef HAVE_SYS_ENDIAN_H
386 #if WORDS_BIGENDIAN
387 #define htobe16(x) (x)
388 #define htobe32(x) (x)
389 #define htobe64(x) (x)
390 #define htole16(x) bswap16((u_int16_t)(x))
391 #define htole32(x) bswap32((u_int32_t)(x))
392 #define htole64(x) bswap64((u_int64_t)(x))
393 #else
394 #define htobe16(x) bswap16((u_int16_t)(x))
395 #define htobe32(x) bswap32((u_int32_t)(x))
396 #define htobe64(x) bswap64((u_int64_t)(x))
397 #define htole16(x) (x)
398 #define htole32(x) (x)
399 #define htole64(x) (x)
400 #endif
401 #define be16toh(x) htobe16(x)
402 #define be32toh(x) htobe32(x)
403 #define be64toh(x) htobe64(x)
404 #define le16toh(x) htole16(x)
405 #define le32toh(x) htole32(x)
406 #define le64toh(x) htole64(x)
407 #endif
408
409 /* <sys/mman.h> */
410
411 #ifndef MAP_FILE
412 #define MAP_FILE 0
413 #endif
414
415 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
416 #ifndef MAP_ANON
417 #ifdef MAP_ANONYMOUS
418 #define MAP_ANON MAP_ANONYMOUS
419 #endif
420 #endif
421
422 /* <sys/param.h> */
423
424 #undef BIG_ENDIAN
425 #undef LITTLE_ENDIAN
426 #define BIG_ENDIAN 4321
427 #define LITTLE_ENDIAN 1234
428
429 #undef BYTE_ORDER
430 #if WORDS_BIGENDIAN
431 #define BYTE_ORDER BIG_ENDIAN
432 #else
433 #define BYTE_ORDER LITTLE_ENDIAN
434 #endif
435
436 #ifndef DEV_BSIZE
437 #define DEV_BSIZE (1 << 9)
438 #endif
439
440 #undef MIN
441 #undef MAX
442 #define MIN(a,b) ((a) < (b) ? (a) : (b))
443 #define MAX(a,b) ((a) > (b) ? (a) : (b))
444
445 #ifndef MAXBSIZE
446 #define MAXBSIZE (64 * 1024)
447 #endif
448 #ifndef MAXFRAG
449 #define MAXFRAG 8
450 #endif
451 #ifndef MAXPHYS
452 #define MAXPHYS (64 * 1024)
453 #endif
454
455 /* XXX needed by makefs; this should be done in a better way */
456 #undef btodb
457 #define btodb(x) ((x) << 9)
458
459 #undef setbit
460 #undef clrbit
461 #undef isset
462 #undef isclr
463 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
464 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
465 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
466 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
467
468 #ifndef powerof2
469 #define powerof2(x) ((((x)-1)&(x))==0)
470 #endif
471
472 #undef roundup
473 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
474
475 /* <sys/stat.h> */
476
477 #ifndef ALLPERMS
478 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
479 #endif
480 #ifndef DEFFILEMODE
481 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
482 #endif
483 #ifndef S_ISTXT
484 #ifdef S_ISVTX
485 #define S_ISTXT S_ISVTX
486 #else
487 #define S_ISTXT 0
488 #endif
489 #endif
490
491 /* <sys/syslimits.h> */
492
493 #ifndef LINE_MAX
494 #define LINE_MAX 2048
495 #endif
496
497 /* <sys/time.h> */
498
499 #ifndef timercmp
500 #define timercmp(tvp, uvp, cmp) \
501 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
502 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
503 ((tvp)->tv_sec cmp (uvp)->tv_sec))
504 #endif
505 #ifndef timeradd
506 #define timeradd(tvp, uvp, vvp) \
507 do { \
508 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
509 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
510 if ((vvp)->tv_usec >= 1000000) { \
511 (vvp)->tv_sec++; \
512 (vvp)->tv_usec -= 1000000; \
513 } \
514 } while (/* CONSTCOND */ 0)
515 #endif
516 #ifndef timersub
517 #define timersub(tvp, uvp, vvp) \
518 do { \
519 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
520 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
521 if ((vvp)->tv_usec < 0) { \
522 (vvp)->tv_sec--; \
523 (vvp)->tv_usec += 1000000; \
524 } \
525 } while (/* CONSTCOND */ 0)
526 #endif
527
528 /* <sys/types.h> */
529
530 #if !HAVE_U_QUAD_T
531 /* #define, not typedef, as quad_t exists as a struct on some systems */
532 #define quad_t long long
533 #define u_quad_t unsigned long long
534 #define strtouq strtoull
535 #endif
536
537 #endif /* !__NETBSD_COMPAT_DEFS_H__ */
538