compat_defs.h revision 1.31 1 /* $NetBSD: compat_defs.h,v 1.31 2003/10/27 00:12:42 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 #if !HAVE_SOCKLEN_T
114 typedef int socklen_t;
115 #endif
116
117 #if !HAVE_U_LONG
118 typedef unsigned long u_long;
119 #endif
120
121 /* Prototypes for replacement functions. */
122
123 #if !HAVE_ATOLL
124 long long int atoll(const char *);
125 #endif
126
127 #if !HAVE_ASPRINTF
128 int asprintf(char **, const char *, ...);
129 #endif
130
131 #if !HAVE_ASNPRINTF
132 int asnprintf(char **, size_t, const char *, ...);
133 #endif
134
135 #if !HAVE_BASENAME
136 char *basename(char *);
137 #endif
138
139 #if !HAVE_DECL_OPTIND
140 int getopt(int, char *const *, const char *);
141 extern char *optarg;
142 extern int optind, opterr, optopt;
143 #endif
144
145 #if !HAVE_DIRNAME
146 char *dirname(char *);
147 #endif
148
149 #if !HAVE_DIRFD
150 #if HAVE_DIR_DD_FD
151 #define dirfd(dirp) ((dirp)->dd_fd)
152 #else
153 #error cannot figure out how to turn a DIR * into a fd
154 #endif
155 #endif
156
157 #if !HAVE_ERR_H
158 void err(int, const char *, ...);
159 void errx(int, const char *, ...);
160 void warn(const char *, ...);
161 void warnx(const char *, ...);
162 #endif
163
164 #if !HAVE_FGETLN
165 char *fgetln(FILE *, size_t *);
166 #endif
167
168 #if !HAVE_FLOCK
169 # define LOCK_SH 0x01
170 # define LOCK_EX 0x02
171 # define LOCK_NB 0x04
172 # define LOCK_UN 0x08
173 int flock(int, int);
174 #endif
175
176 #if !HAVE_FPARSELN
177 # define FPARSELN_UNESCESC 0x01
178 # define FPARSELN_UNESCCONT 0x02
179 # define FPARSELN_UNESCCOMM 0x04
180 # define FPARSELN_UNESCREST 0x08
181 # define FPARSELN_UNESCALL 0x0f
182 char *fparseln(FILE *, size_t *, size_t *, const char [3], int);
183 #endif
184
185 #if !HAVE_ISSETUGID
186 int issetugid(void);
187 #endif
188
189 #if !HAVE_ISBLANK && !defined(isblank)
190 #define isblank(x) ((x) == ' ' || (x) == '\t')
191 #endif
192
193 #if !HAVE_LCHFLAGS
194 int lchflags(const char *, u_long);
195 #endif
196
197 #if !HAVE_LCHMOD
198 int lchmod(const char *, mode_t);
199 #endif
200
201 #if !HAVE_LCHOWN
202 int lchown(const char *, uid_t, gid_t);
203 #endif
204
205 #if !HAVE_MACHINE_BSWAP_H
206 #define bswap16(x) ((((x) << 8) & 0xff00) | (((x) >> 8) & 0x00ff))
207
208 #define bswap32(x) ((((x) << 24) & 0xff000000) | \
209 (((x) << 8) & 0x00ff0000) | \
210 (((x) >> 8) & 0x0000ff00) | \
211 (((x) >> 24) & 0x000000ff))
212
213 #define bswap64(x) (((u_int64_t)bswap32((x)) << 32) | \
214 ((u_int64_t)bswap32((x) >> 32)))
215 #endif
216
217 #if !HAVE_MKSTEMP
218 int mkstemp(char *);
219 #endif
220
221 #if !HAVE_MKDTEMP
222 char *mkdtemp(char *);
223 #endif
224
225 #if !HAVE_MKSTEMP || !HAVE_MKDTEMP
226 /* This is a prototype for the internal function. */
227 int gettemp(char *, int *, int);
228 #endif
229
230 #if !HAVE_PREAD
231 ssize_t pread(int, void *, size_t, off_t);
232 #endif
233
234 #if !HAVE_PWCACHE_USERDB
235 int uid_from_user(const char *, uid_t *);
236 int pwcache_userdb(int (*)(int), void (*)(void),
237 struct passwd * (*)(const char *), struct passwd * (*)(uid_t));
238 int gid_from_group(const char *, gid_t *);
239 int pwcache_groupdb(int (*)(int), void (*)(void),
240 struct group * (*)(const char *), struct group * (*)(gid_t));
241 #endif
242
243 #if !HAVE_PWRITE
244 ssize_t pwrite(int, const void *, size_t, off_t);
245 #endif
246
247 #if !HAVE_SETENV
248 int setenv(const char *, const char *, int);
249 #endif
250
251 #if !HAVE_DECL_SETGROUPENT
252 int setgroupent(int);
253 #endif
254
255 #if !HAVE_DECL_SETPASSENT
256 int setpassent(int);
257 #endif
258
259 #if !HAVE_SETPROGNAME
260 const char *getprogname(void);
261 void setprogname(const char *);
262 #endif
263
264 #if !HAVE_SNPRINTF
265 int snprintf(char *, size_t, const char *, ...);
266 #endif
267
268 #if !HAVE_STRLCAT
269 size_t strlcat(char *, const char *, size_t);
270 #endif
271
272 #if !HAVE_STRLCPY
273 size_t strlcpy(char *, const char *, size_t);
274 #endif
275
276 #if !HAVE_STRSEP
277 char *strsep(char **, const char *);
278 #endif
279
280 #if !HAVE_STRSUFTOLL
281 long long strsuftoll(const char *, const char *, long long, long long);
282 long long strsuftollx(const char *, const char *,
283 long long, long long, char *, size_t);
284 #endif
285
286 #if !HAVE_STRTOLL
287 long long strtoll(const char *, char **, int);
288 #endif
289
290 #if !HAVE_USER_FROM_UID
291 const char *user_from_uid(uid_t, int);
292 const char *group_from_gid(gid_t, int);
293 #endif
294
295 #if !HAVE_VASPRINTF
296 int vasprintf(char **, const char *, va_list);
297 #endif
298
299 #if !HAVE_VASNPRINTF
300 int vasnprintf(char **, size_t, const char *, va_list);
301 #endif
302
303 #if !HAVE_VSNPRINTF
304 int vsnprintf(char *, size_t, const char *, va_list);
305 #endif
306
307 /*
308 * getmode() and setmode() are always defined, as these function names
309 * exist but with very different meanings on other OS's. The compat
310 * versions here simply accept an octal mode number; the "u+x,g-w" type
311 * of syntax is not accepted.
312 */
313
314 #define getmode __nbcompat_getmode
315 #define setmode __nbcompat_setmode
316
317 mode_t getmode(const void *, mode_t);
318 void *setmode(const char *);
319
320 /* Eliminate assertions embedded in binaries. */
321
322 #undef _DIAGASSERT
323 #define _DIAGASSERT(x)
324
325 /* Various sources use this */
326 #undef __RCSID
327 #define __RCSID(x)
328 #undef __SCCSID
329 #define __SCCSID(x)
330 #undef __COPYRIGHT
331 #define __COPYRIGHT(x)
332 #undef __KERNEL_RCSID
333 #define __KERNEL_RCSID(x,y)
334
335 /* Heimdal expects this one. */
336
337 #undef RCSID
338 #define RCSID(x)
339
340 /* Some definitions not available on all systems. */
341
342 /* <errno.h> */
343
344 #ifndef EFTYPE
345 #define EFTYPE EIO
346 #endif
347
348 /* <fcntl.h> */
349
350 #ifndef O_EXLOCK
351 #define O_EXLOCK 0
352 #endif
353 #ifndef O_SHLOCK
354 #define O_SHLOCK 0
355 #endif
356
357 /* <limits.h> */
358
359 #ifndef UID_MAX
360 #define UID_MAX 32767
361 #endif
362 #ifndef GID_MAX
363 #define GID_MAX UID_MAX
364 #endif
365
366 #ifndef UQUAD_MAX
367 #define UQUAD_MAX ((u_quad_t)-1)
368 #endif
369 #ifndef QUAD_MAX
370 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
371 #endif
372 #ifndef QUAD_MIN
373 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
374 #endif
375 #ifndef ULLONG_MAX
376 #define ULLONG_MAX ((unsigned long long)-1)
377 #endif
378 #ifndef LLONG_MAX
379 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
380 #endif
381 #ifndef LLONG_MIN
382 #define LLONG_MIN ((long long)(~LLONG_MAX))
383 #endif
384
385 /* <paths.h> */
386
387 #ifndef _PATH_BSHELL
388 #define _PATH_BSHELL PATH_BSHELL
389 #endif
390 #ifndef _PATH_DEFPATH
391 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
392 #endif
393 #ifndef _PATH_DEV
394 #define _PATH_DEV "/dev/"
395 #endif
396 #ifndef _PATH_DEVNULL
397 #define _PATH_DEVNULL _PATH_DEV "null"
398 #endif
399 #ifndef _PATH_TMP
400 #define _PATH_TMP "/tmp/"
401 #endif
402 #ifndef _PATH_DEFTAPE
403 #define _PATH_DEFTAPE "/dev/nrst0"
404 #endif
405
406 /* <stdarg.h> */
407
408 #ifndef _BSD_VA_LIST_
409 #define _BSD_VA_LIST_ va_list
410 #endif
411
412 /* <stdint.h> */
413
414 #if !defined(SIZE_MAX) && defined(SIZE_T_MAX)
415 #define SIZE_MAX SIZE_T_MAX
416 #endif
417
418 #ifndef UINT32_MAX
419 #define UINT32_MAX 0xffffffffU
420 #endif
421
422 /* <stdlib.h> */
423
424 #ifndef __GNUC__
425 # if HAVE_ALLOCA_H
426 # include <alloca.h>
427 # else
428 # ifndef alloca /* predefined by HP cc +Olibcalls */
429 char *alloca ();
430 # endif
431 # endif
432 #endif
433
434 /* avoid prototype conflicts with host */
435 #define cgetcap __nbcompat_cgetcap
436 #define cgetclose __nbcompat_cgetclose
437 #define cgetent __nbcompat_cgetent
438 #define cgetfirst __nbcompat_cgetfirst
439 #define cgetmatch __nbcompat_cgetmatch
440 #define cgetnext __nbcompat_cgetnext
441 #define cgetnum __nbcompat_cgetnum
442 #define cgetset __nbcompat_cgetset
443 #define cgetstr __nbcompat_cgetstr
444 #define cgetustr __nbcompat_cgetustr
445
446 char *cgetcap(char *, const char *, int);
447 int cgetclose(void);
448 int cgetent(char **, char **, const char *);
449 int cgetfirst(char **, char **);
450 int cgetmatch(const char *, const char *);
451 int cgetnext(char **, char **);
452 int cgetnum(char *, const char *, long *);
453 int cgetset(const char *);
454 int cgetstr(char *, const char *, char **);
455 int cgetustr(char *, const char *, char **);
456
457 /* <sys/endian.h> */
458
459 #ifdef HAVE_SYS_ENDIAN_H
460 #include <sys/endian.h>
461 #else
462 #if WORDS_BIGENDIAN
463 #define htobe16(x) (x)
464 #define htobe32(x) (x)
465 #define htobe64(x) (x)
466 #define htole16(x) bswap16((u_int16_t)(x))
467 #define htole32(x) bswap32((u_int32_t)(x))
468 #define htole64(x) bswap64((u_int64_t)(x))
469 #else
470 #define htobe16(x) bswap16((u_int16_t)(x))
471 #define htobe32(x) bswap32((u_int32_t)(x))
472 #define htobe64(x) bswap64((u_int64_t)(x))
473 #define htole16(x) (x)
474 #define htole32(x) (x)
475 #define htole64(x) (x)
476 #endif
477 #define be16toh(x) htobe16(x)
478 #define be32toh(x) htobe32(x)
479 #define be64toh(x) htobe64(x)
480 #define le16toh(x) htole16(x)
481 #define le32toh(x) htole32(x)
482 #define le64toh(x) htole64(x)
483 #endif
484
485 /* <sys/mman.h> */
486
487 #ifndef MAP_FILE
488 #define MAP_FILE 0
489 #endif
490
491 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
492 #ifndef MAP_ANON
493 #ifdef MAP_ANONYMOUS
494 #define MAP_ANON MAP_ANONYMOUS
495 #endif
496 #endif
497
498 /* <sys/param.h> */
499
500 #undef BIG_ENDIAN
501 #undef LITTLE_ENDIAN
502 #define BIG_ENDIAN 4321
503 #define LITTLE_ENDIAN 1234
504
505 #undef BYTE_ORDER
506 #if WORDS_BIGENDIAN
507 #define BYTE_ORDER BIG_ENDIAN
508 #else
509 #define BYTE_ORDER LITTLE_ENDIAN
510 #endif
511
512 #ifndef DEV_BSIZE
513 #define DEV_BSIZE (1 << 9)
514 #endif
515
516 #undef MIN
517 #undef MAX
518 #define MIN(a,b) ((a) < (b) ? (a) : (b))
519 #define MAX(a,b) ((a) > (b) ? (a) : (b))
520
521 #ifndef MAXBSIZE
522 #define MAXBSIZE (64 * 1024)
523 #endif
524 #ifndef MAXFRAG
525 #define MAXFRAG 8
526 #endif
527 #ifndef MAXPHYS
528 #define MAXPHYS (64 * 1024)
529 #endif
530
531 /* XXX needed by makefs; this should be done in a better way */
532 #undef btodb
533 #define btodb(x) ((x) << 9)
534
535 #undef setbit
536 #undef clrbit
537 #undef isset
538 #undef isclr
539 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
540 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
541 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
542 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
543
544 #ifndef powerof2
545 #define powerof2(x) ((((x)-1)&(x))==0)
546 #endif
547
548 #undef roundup
549 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
550
551 /* <sys/stat.h> */
552
553 #ifndef ALLPERMS
554 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
555 #endif
556 #ifndef DEFFILEMODE
557 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
558 #endif
559 #ifndef S_ISTXT
560 #ifdef S_ISVTX
561 #define S_ISTXT S_ISVTX
562 #else
563 #define S_ISTXT 0
564 #endif
565 #endif
566
567 /* <sys/syslimits.h> */
568
569 #ifndef LINE_MAX
570 #define LINE_MAX 2048
571 #endif
572
573 /* <sys/time.h> */
574
575 #ifndef timercmp
576 #define timercmp(tvp, uvp, cmp) \
577 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
578 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
579 ((tvp)->tv_sec cmp (uvp)->tv_sec))
580 #endif
581 #ifndef timeradd
582 #define timeradd(tvp, uvp, vvp) \
583 do { \
584 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
585 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
586 if ((vvp)->tv_usec >= 1000000) { \
587 (vvp)->tv_sec++; \
588 (vvp)->tv_usec -= 1000000; \
589 } \
590 } while (/* CONSTCOND */ 0)
591 #endif
592 #ifndef timersub
593 #define timersub(tvp, uvp, vvp) \
594 do { \
595 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
596 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
597 if ((vvp)->tv_usec < 0) { \
598 (vvp)->tv_sec--; \
599 (vvp)->tv_usec += 1000000; \
600 } \
601 } while (/* CONSTCOND */ 0)
602 #endif
603
604 /* <sys/types.h> */
605
606 #if !HAVE_U_QUAD_T
607 /* #define, not typedef, as quad_t exists as a struct on some systems */
608 #define quad_t long long
609 #define u_quad_t unsigned long long
610 #define strtoq strtoll
611 #define strtouq strtoull
612 #endif
613
614 #endif /* !__NETBSD_COMPAT_DEFS_H__ */
615