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