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