compat_defs.h revision 1.31.2.1 1 /* $NetBSD: compat_defs.h,v 1.31.2.1 2004/06/14 20:19:44 tron 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 #ifndef __RENAME
91 #define __RENAME(x)
92 #endif
93 #undef __aconst
94 #define __aconst
95 #undef __dead
96 #define __dead
97 #undef __restrict
98 #define __restrict
99
100 /* Dirent support. */
101
102 #if HAVE_DIRENT_H
103 # include <dirent.h>
104 # define NAMLEN(dirent) (strlen((dirent)->d_name))
105 #else
106 # define dirent direct
107 # define NAMLEN(dirent) ((dirent)->d_namlen)
108 # if HAVE_SYS_NDIR_H
109 # include <sys/ndir.h>
110 # endif
111 # if HAVE_SYS_DIR_H
112 # include <sys/dir.h>
113 # endif
114 # if HAVE_NDIR_H
115 # include <ndir.h>
116 # endif
117 #endif
118
119 /* Type substitutes. */
120
121 #if !HAVE_ID_T
122 typedef unsigned long id_t;
123 #endif
124
125 #if !HAVE_SOCKLEN_T
126 typedef int socklen_t;
127 #endif
128
129 #if !HAVE_U_LONG
130 typedef unsigned long u_long;
131 #endif
132
133 /* Prototypes for replacement functions. */
134
135 #if !HAVE_ATOLL
136 long long int atoll(const char *);
137 #endif
138
139 #if !HAVE_ASPRINTF
140 int asprintf(char **, const char *, ...);
141 #endif
142
143 #if !HAVE_ASNPRINTF
144 int asnprintf(char **, size_t, const char *, ...);
145 #endif
146
147 #if !HAVE_BASENAME
148 char *basename(char *);
149 #endif
150
151 #if !HAVE_DECL_OPTIND
152 int getopt(int, char *const *, const char *);
153 extern char *optarg;
154 extern int optind, opterr, optopt;
155 #endif
156
157 #if !HAVE_DIRNAME
158 char *dirname(char *);
159 #endif
160
161 #if !HAVE_DIRFD
162 #if HAVE_DIR_DD_FD
163 #define dirfd(dirp) ((dirp)->dd_fd)
164 #else
165 #error cannot figure out how to turn a DIR * into a fd
166 #endif
167 #endif
168
169 #if !HAVE_ERR_H
170 void err(int, const char *, ...);
171 void errx(int, const char *, ...);
172 void warn(const char *, ...);
173 void warnx(const char *, ...);
174 #endif
175
176 #if !HAVE_FGETLN
177 char *fgetln(FILE *, size_t *);
178 #endif
179
180 #if !HAVE_FLOCK
181 # define LOCK_SH 0x01
182 # define LOCK_EX 0x02
183 # define LOCK_NB 0x04
184 # define LOCK_UN 0x08
185 int flock(int, int);
186 #endif
187
188 #if !HAVE_FPARSELN
189 # define FPARSELN_UNESCESC 0x01
190 # define FPARSELN_UNESCCONT 0x02
191 # define FPARSELN_UNESCCOMM 0x04
192 # define FPARSELN_UNESCREST 0x08
193 # define FPARSELN_UNESCALL 0x0f
194 char *fparseln(FILE *, size_t *, size_t *, const char [3], int);
195 #endif
196
197 #if !HAVE_ISSETUGID
198 int issetugid(void);
199 #endif
200
201 #if !HAVE_ISBLANK && !defined(isblank)
202 #define isblank(x) ((x) == ' ' || (x) == '\t')
203 #endif
204
205 #if !HAVE_LCHFLAGS
206 int lchflags(const char *, u_long);
207 #endif
208
209 #if !HAVE_LCHMOD
210 int lchmod(const char *, mode_t);
211 #endif
212
213 #if !HAVE_LCHOWN
214 int lchown(const char *, uid_t, gid_t);
215 #endif
216
217 #if !HAVE_MACHINE_BSWAP_H
218 #define bswap16(x) ((((x) << 8) & 0xff00) | (((x) >> 8) & 0x00ff))
219
220 #define bswap32(x) ((((x) << 24) & 0xff000000) | \
221 (((x) << 8) & 0x00ff0000) | \
222 (((x) >> 8) & 0x0000ff00) | \
223 (((x) >> 24) & 0x000000ff))
224
225 #define bswap64(x) (((u_int64_t)bswap32((x)) << 32) | \
226 ((u_int64_t)bswap32((x) >> 32)))
227 #endif
228
229 #if !HAVE_MKSTEMP
230 int mkstemp(char *);
231 #endif
232
233 #if !HAVE_MKDTEMP
234 char *mkdtemp(char *);
235 #endif
236
237 #if !HAVE_MKSTEMP || !HAVE_MKDTEMP
238 /* This is a prototype for the internal function. */
239 int gettemp(char *, int *, int);
240 #endif
241
242 #if !HAVE_PREAD
243 ssize_t pread(int, void *, size_t, off_t);
244 #endif
245
246 #if !HAVE_PWCACHE_USERDB
247 int uid_from_user(const char *, uid_t *);
248 int pwcache_userdb(int (*)(int), void (*)(void),
249 struct passwd * (*)(const char *), struct passwd * (*)(uid_t));
250 int gid_from_group(const char *, gid_t *);
251 int pwcache_groupdb(int (*)(int), void (*)(void),
252 struct group * (*)(const char *), struct group * (*)(gid_t));
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 **, char **, const char *);
461 int cgetfirst(char **, char **);
462 int cgetmatch(const char *, const char *);
463 int cgetnext(char **, char **);
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