compat_defs.h revision 1.34 1 /* $NetBSD: compat_defs.h,v 1.34 2004/04/24 15:46:24 matt 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 #endif
249
250 #if !HAVE_PWRITE
251 ssize_t pwrite(int, const void *, size_t, off_t);
252 #endif
253
254 #if !HAVE_SETENV
255 int setenv(const char *, const char *, int);
256 #endif
257
258 #if !HAVE_DECL_SETGROUPENT
259 int setgroupent(int);
260 #endif
261
262 #if !HAVE_DECL_SETPASSENT
263 int setpassent(int);
264 #endif
265
266 #if !HAVE_SETPROGNAME
267 const char *getprogname(void);
268 void setprogname(const char *);
269 #endif
270
271 #if !HAVE_SNPRINTF
272 int snprintf(char *, size_t, const char *, ...);
273 #endif
274
275 #if !HAVE_STRLCAT
276 size_t strlcat(char *, const char *, size_t);
277 #endif
278
279 #if !HAVE_STRLCPY
280 size_t strlcpy(char *, const char *, size_t);
281 #endif
282
283 #if !HAVE_STRSEP
284 char *strsep(char **, const char *);
285 #endif
286
287 #if !HAVE_STRSUFTOLL
288 long long strsuftoll(const char *, const char *, long long, long long);
289 long long strsuftollx(const char *, const char *,
290 long long, long long, char *, size_t);
291 #endif
292
293 #if !HAVE_STRTOLL
294 long long strtoll(const char *, char **, int);
295 #endif
296
297 #if !HAVE_USER_FROM_UID
298 const char *user_from_uid(uid_t, int);
299 const char *group_from_gid(gid_t, int);
300 #endif
301
302 #if !HAVE_VASPRINTF
303 int vasprintf(char **, const char *, va_list);
304 #endif
305
306 #if !HAVE_VASNPRINTF
307 int vasnprintf(char **, size_t, const char *, va_list);
308 #endif
309
310 #if !HAVE_VSNPRINTF
311 int vsnprintf(char *, size_t, const char *, va_list);
312 #endif
313
314 /*
315 * getmode() and setmode() are always defined, as these function names
316 * exist but with very different meanings on other OS's. The compat
317 * versions here simply accept an octal mode number; the "u+x,g-w" type
318 * of syntax is not accepted.
319 */
320
321 #define getmode __nbcompat_getmode
322 #define setmode __nbcompat_setmode
323
324 mode_t getmode(const void *, mode_t);
325 void *setmode(const char *);
326
327 /* Eliminate assertions embedded in binaries. */
328
329 #undef _DIAGASSERT
330 #define _DIAGASSERT(x)
331
332 /* Various sources use this */
333 #undef __RCSID
334 #define __RCSID(x)
335 #undef __SCCSID
336 #define __SCCSID(x)
337 #undef __COPYRIGHT
338 #define __COPYRIGHT(x)
339 #undef __KERNEL_RCSID
340 #define __KERNEL_RCSID(x,y)
341
342 /* Heimdal expects this one. */
343
344 #undef RCSID
345 #define RCSID(x)
346
347 /* Some definitions not available on all systems. */
348
349 /* <errno.h> */
350
351 #ifndef EFTYPE
352 #define EFTYPE EIO
353 #endif
354
355 /* <fcntl.h> */
356
357 #ifndef O_EXLOCK
358 #define O_EXLOCK 0
359 #endif
360 #ifndef O_SHLOCK
361 #define O_SHLOCK 0
362 #endif
363
364 /* <limits.h> */
365
366 #ifndef UID_MAX
367 #define UID_MAX 32767
368 #endif
369 #ifndef GID_MAX
370 #define GID_MAX UID_MAX
371 #endif
372
373 #ifndef UQUAD_MAX
374 #define UQUAD_MAX ((u_quad_t)-1)
375 #endif
376 #ifndef QUAD_MAX
377 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
378 #endif
379 #ifndef QUAD_MIN
380 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
381 #endif
382 #ifndef ULLONG_MAX
383 #define ULLONG_MAX ((unsigned long long)-1)
384 #endif
385 #ifndef LLONG_MAX
386 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
387 #endif
388 #ifndef LLONG_MIN
389 #define LLONG_MIN ((long long)(~LLONG_MAX))
390 #endif
391
392 /* <paths.h> */
393
394 #ifndef _PATH_BSHELL
395 #define _PATH_BSHELL PATH_BSHELL
396 #endif
397 #ifndef _PATH_DEFPATH
398 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
399 #endif
400 #ifndef _PATH_DEV
401 #define _PATH_DEV "/dev/"
402 #endif
403 #ifndef _PATH_DEVNULL
404 #define _PATH_DEVNULL _PATH_DEV "null"
405 #endif
406 #ifndef _PATH_TMP
407 #define _PATH_TMP "/tmp/"
408 #endif
409 #ifndef _PATH_DEFTAPE
410 #define _PATH_DEFTAPE "/dev/nrst0"
411 #endif
412
413 /* <stdarg.h> */
414
415 #ifndef _BSD_VA_LIST_
416 #define _BSD_VA_LIST_ va_list
417 #endif
418
419 /* <stdint.h> */
420
421 #if !defined(SIZE_MAX) && defined(SIZE_T_MAX)
422 #define SIZE_MAX SIZE_T_MAX
423 #endif
424
425 #ifndef UINT32_MAX
426 #define UINT32_MAX 0xffffffffU
427 #endif
428
429 /* <stdlib.h> */
430
431 #ifndef __GNUC__
432 # if HAVE_ALLOCA_H
433 # include <alloca.h>
434 # else
435 # ifndef alloca /* predefined by HP cc +Olibcalls */
436 char *alloca ();
437 # endif
438 # endif
439 #endif
440
441 /* avoid prototype conflicts with host */
442 #define cgetcap __nbcompat_cgetcap
443 #define cgetclose __nbcompat_cgetclose
444 #define cgetent __nbcompat_cgetent
445 #define cgetfirst __nbcompat_cgetfirst
446 #define cgetmatch __nbcompat_cgetmatch
447 #define cgetnext __nbcompat_cgetnext
448 #define cgetnum __nbcompat_cgetnum
449 #define cgetset __nbcompat_cgetset
450 #define cgetstr __nbcompat_cgetstr
451 #define cgetustr __nbcompat_cgetustr
452
453 char *cgetcap(char *, const char *, int);
454 int cgetclose(void);
455 int cgetent(char **, const char * const *, const char *);
456 int cgetfirst(char **, const char * const *);
457 int cgetmatch(const char *, const char *);
458 int cgetnext(char **, const char * const *);
459 int cgetnum(char *, const char *, long *);
460 int cgetset(const char *);
461 int cgetstr(char *, const char *, char **);
462 int cgetustr(char *, const char *, char **);
463
464 /* <sys/endian.h> */
465
466 #ifdef HAVE_SYS_ENDIAN_H
467 #include <sys/endian.h>
468 #else
469 #if WORDS_BIGENDIAN
470 #define htobe16(x) (x)
471 #define htobe32(x) (x)
472 #define htobe64(x) (x)
473 #define htole16(x) bswap16((u_int16_t)(x))
474 #define htole32(x) bswap32((u_int32_t)(x))
475 #define htole64(x) bswap64((u_int64_t)(x))
476 #else
477 #define htobe16(x) bswap16((u_int16_t)(x))
478 #define htobe32(x) bswap32((u_int32_t)(x))
479 #define htobe64(x) bswap64((u_int64_t)(x))
480 #define htole16(x) (x)
481 #define htole32(x) (x)
482 #define htole64(x) (x)
483 #endif
484 #define be16toh(x) htobe16(x)
485 #define be32toh(x) htobe32(x)
486 #define be64toh(x) htobe64(x)
487 #define le16toh(x) htole16(x)
488 #define le32toh(x) htole32(x)
489 #define le64toh(x) htole64(x)
490 #endif
491
492 /* <sys/mman.h> */
493
494 #ifndef MAP_FILE
495 #define MAP_FILE 0
496 #endif
497
498 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
499 #ifndef MAP_ANON
500 #ifdef MAP_ANONYMOUS
501 #define MAP_ANON MAP_ANONYMOUS
502 #endif
503 #endif
504
505 /* <sys/param.h> */
506
507 #undef BIG_ENDIAN
508 #undef LITTLE_ENDIAN
509 #define BIG_ENDIAN 4321
510 #define LITTLE_ENDIAN 1234
511
512 #undef BYTE_ORDER
513 #if WORDS_BIGENDIAN
514 #define BYTE_ORDER BIG_ENDIAN
515 #else
516 #define BYTE_ORDER LITTLE_ENDIAN
517 #endif
518
519 #ifndef DEV_BSIZE
520 #define DEV_BSIZE (1 << 9)
521 #endif
522
523 #undef MIN
524 #undef MAX
525 #define MIN(a,b) ((a) < (b) ? (a) : (b))
526 #define MAX(a,b) ((a) > (b) ? (a) : (b))
527
528 #ifndef MAXBSIZE
529 #define MAXBSIZE (64 * 1024)
530 #endif
531 #ifndef MAXFRAG
532 #define MAXFRAG 8
533 #endif
534 #ifndef MAXPHYS
535 #define MAXPHYS (64 * 1024)
536 #endif
537
538 /* XXX needed by makefs; this should be done in a better way */
539 #undef btodb
540 #define btodb(x) ((x) << 9)
541
542 #undef setbit
543 #undef clrbit
544 #undef isset
545 #undef isclr
546 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
547 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
548 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
549 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
550
551 #ifndef powerof2
552 #define powerof2(x) ((((x)-1)&(x))==0)
553 #endif
554
555 #undef roundup
556 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
557
558 /* <sys/stat.h> */
559
560 #ifndef ALLPERMS
561 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
562 #endif
563 #ifndef DEFFILEMODE
564 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
565 #endif
566 #ifndef S_ISTXT
567 #ifdef S_ISVTX
568 #define S_ISTXT S_ISVTX
569 #else
570 #define S_ISTXT 0
571 #endif
572 #endif
573
574 /* <sys/syslimits.h> */
575
576 #ifndef LINE_MAX
577 #define LINE_MAX 2048
578 #endif
579
580 /* <sys/time.h> */
581
582 #ifndef timercmp
583 #define timercmp(tvp, uvp, cmp) \
584 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
585 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
586 ((tvp)->tv_sec cmp (uvp)->tv_sec))
587 #endif
588 #ifndef timeradd
589 #define timeradd(tvp, uvp, vvp) \
590 do { \
591 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
592 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
593 if ((vvp)->tv_usec >= 1000000) { \
594 (vvp)->tv_sec++; \
595 (vvp)->tv_usec -= 1000000; \
596 } \
597 } while (/* CONSTCOND */ 0)
598 #endif
599 #ifndef timersub
600 #define timersub(tvp, uvp, vvp) \
601 do { \
602 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
603 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
604 if ((vvp)->tv_usec < 0) { \
605 (vvp)->tv_sec--; \
606 (vvp)->tv_usec += 1000000; \
607 } \
608 } while (/* CONSTCOND */ 0)
609 #endif
610
611 /* <sys/types.h> */
612
613 #if !HAVE_U_QUAD_T
614 /* #define, not typedef, as quad_t exists as a struct on some systems */
615 #define quad_t long long
616 #define u_quad_t unsigned long long
617 #define strtoq strtoll
618 #define strtouq strtoull
619 #endif
620
621 #endif /* !__NETBSD_COMPAT_DEFS_H__ */
622