compat_defs.h revision 1.29 1 /* $NetBSD: compat_defs.h,v 1.29 2003/07/27 07:56:37 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 /* Heimdal expects this one. */
326
327 #undef RCSID
328 #define RCSID(x)
329
330 /* pwd_mkdb expects this one. */
331
332 #undef __SCCSID
333 #define __SCCSID(x)
334
335 /* Some definitions not available on all systems. */
336
337 /* <errno.h> */
338
339 #ifndef EFTYPE
340 #define EFTYPE EIO
341 #endif
342
343 /* <fcntl.h> */
344
345 #ifndef O_EXLOCK
346 #define O_EXLOCK 0
347 #endif
348 #ifndef O_SHLOCK
349 #define O_SHLOCK 0
350 #endif
351
352 /* <limits.h> */
353
354 #ifndef UID_MAX
355 #define UID_MAX 32767
356 #endif
357 #ifndef GID_MAX
358 #define GID_MAX UID_MAX
359 #endif
360
361 #ifndef UQUAD_MAX
362 #define UQUAD_MAX ((u_quad_t)-1)
363 #endif
364 #ifndef QUAD_MAX
365 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
366 #endif
367 #ifndef QUAD_MIN
368 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
369 #endif
370 #ifndef ULLONG_MAX
371 #define ULLONG_MAX ((unsigned long long)-1)
372 #endif
373 #ifndef LLONG_MAX
374 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
375 #endif
376 #ifndef LLONG_MIN
377 #define LLONG_MIN ((long long)(~LLONG_MAX))
378 #endif
379
380 /* <paths.h> */
381
382 #ifndef _PATH_BSHELL
383 #define _PATH_BSHELL PATH_BSHELL
384 #endif
385 #ifndef _PATH_DEFPATH
386 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
387 #endif
388 #ifndef _PATH_DEV
389 #define _PATH_DEV "/dev/"
390 #endif
391 #ifndef _PATH_DEVNULL
392 #define _PATH_DEVNULL _PATH_DEV "null"
393 #endif
394 #ifndef _PATH_TMP
395 #define _PATH_TMP "/tmp/"
396 #endif
397 #ifndef _PATH_DEFTAPE
398 #define _PATH_DEFTAPE "/dev/nrst0"
399 #endif
400
401 /* <stdarg.h> */
402
403 #ifndef _BSD_VA_LIST_
404 #define _BSD_VA_LIST_ va_list
405 #endif
406
407 /* <stdint.h> */
408
409 #ifndef UINT32_MAX
410 #define UINT32_MAX 0xffffffffU
411 #endif
412
413 /* <stdlib.h> */
414
415 #ifndef __GNUC__
416 # if HAVE_ALLOCA_H
417 # include <alloca.h>
418 # else
419 # ifndef alloca /* predefined by HP cc +Olibcalls */
420 char *alloca ();
421 # endif
422 # endif
423 #endif
424
425 /* avoid prototype conflicts with host */
426 #define cgetcap __nbcompat_cgetcap
427 #define cgetclose __nbcompat_cgetclose
428 #define cgetent __nbcompat_cgetent
429 #define cgetfirst __nbcompat_cgetfirst
430 #define cgetmatch __nbcompat_cgetmatch
431 #define cgetnext __nbcompat_cgetnext
432 #define cgetnum __nbcompat_cgetnum
433 #define cgetset __nbcompat_cgetset
434 #define cgetstr __nbcompat_cgetstr
435 #define cgetustr __nbcompat_cgetustr
436
437 char *cgetcap(char *, const char *, int);
438 int cgetclose(void);
439 int cgetent(char **, char **, const char *);
440 int cgetfirst(char **, char **);
441 int cgetmatch(const char *, const char *);
442 int cgetnext(char **, char **);
443 int cgetnum(char *, const char *, long *);
444 int cgetset(const char *);
445 int cgetstr(char *, const char *, char **);
446 int cgetustr(char *, const char *, char **);
447
448 /* <sys/endian.h> */
449
450 #ifdef HAVE_SYS_ENDIAN_H
451 #include <sys/endian.h>
452 #else
453 #if WORDS_BIGENDIAN
454 #define htobe16(x) (x)
455 #define htobe32(x) (x)
456 #define htobe64(x) (x)
457 #define htole16(x) bswap16((u_int16_t)(x))
458 #define htole32(x) bswap32((u_int32_t)(x))
459 #define htole64(x) bswap64((u_int64_t)(x))
460 #else
461 #define htobe16(x) bswap16((u_int16_t)(x))
462 #define htobe32(x) bswap32((u_int32_t)(x))
463 #define htobe64(x) bswap64((u_int64_t)(x))
464 #define htole16(x) (x)
465 #define htole32(x) (x)
466 #define htole64(x) (x)
467 #endif
468 #define be16toh(x) htobe16(x)
469 #define be32toh(x) htobe32(x)
470 #define be64toh(x) htobe64(x)
471 #define le16toh(x) htole16(x)
472 #define le32toh(x) htole32(x)
473 #define le64toh(x) htole64(x)
474 #endif
475
476 /* <sys/mman.h> */
477
478 #ifndef MAP_FILE
479 #define MAP_FILE 0
480 #endif
481
482 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
483 #ifndef MAP_ANON
484 #ifdef MAP_ANONYMOUS
485 #define MAP_ANON MAP_ANONYMOUS
486 #endif
487 #endif
488
489 /* <sys/param.h> */
490
491 #undef BIG_ENDIAN
492 #undef LITTLE_ENDIAN
493 #define BIG_ENDIAN 4321
494 #define LITTLE_ENDIAN 1234
495
496 #undef BYTE_ORDER
497 #if WORDS_BIGENDIAN
498 #define BYTE_ORDER BIG_ENDIAN
499 #else
500 #define BYTE_ORDER LITTLE_ENDIAN
501 #endif
502
503 #ifndef DEV_BSIZE
504 #define DEV_BSIZE (1 << 9)
505 #endif
506
507 #undef MIN
508 #undef MAX
509 #define MIN(a,b) ((a) < (b) ? (a) : (b))
510 #define MAX(a,b) ((a) > (b) ? (a) : (b))
511
512 #ifndef MAXBSIZE
513 #define MAXBSIZE (64 * 1024)
514 #endif
515 #ifndef MAXFRAG
516 #define MAXFRAG 8
517 #endif
518 #ifndef MAXPHYS
519 #define MAXPHYS (64 * 1024)
520 #endif
521
522 /* XXX needed by makefs; this should be done in a better way */
523 #undef btodb
524 #define btodb(x) ((x) << 9)
525
526 #undef setbit
527 #undef clrbit
528 #undef isset
529 #undef isclr
530 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
531 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
532 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
533 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
534
535 #ifndef powerof2
536 #define powerof2(x) ((((x)-1)&(x))==0)
537 #endif
538
539 #undef roundup
540 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
541
542 /* <sys/stat.h> */
543
544 #ifndef ALLPERMS
545 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
546 #endif
547 #ifndef DEFFILEMODE
548 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
549 #endif
550 #ifndef S_ISTXT
551 #ifdef S_ISVTX
552 #define S_ISTXT S_ISVTX
553 #else
554 #define S_ISTXT 0
555 #endif
556 #endif
557
558 /* <sys/syslimits.h> */
559
560 #ifndef LINE_MAX
561 #define LINE_MAX 2048
562 #endif
563
564 /* <sys/time.h> */
565
566 #ifndef timercmp
567 #define timercmp(tvp, uvp, cmp) \
568 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
569 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
570 ((tvp)->tv_sec cmp (uvp)->tv_sec))
571 #endif
572 #ifndef timeradd
573 #define timeradd(tvp, uvp, vvp) \
574 do { \
575 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
576 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
577 if ((vvp)->tv_usec >= 1000000) { \
578 (vvp)->tv_sec++; \
579 (vvp)->tv_usec -= 1000000; \
580 } \
581 } while (/* CONSTCOND */ 0)
582 #endif
583 #ifndef timersub
584 #define timersub(tvp, uvp, vvp) \
585 do { \
586 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
587 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
588 if ((vvp)->tv_usec < 0) { \
589 (vvp)->tv_sec--; \
590 (vvp)->tv_usec += 1000000; \
591 } \
592 } while (/* CONSTCOND */ 0)
593 #endif
594
595 /* <sys/types.h> */
596
597 #if !HAVE_U_QUAD_T
598 /* #define, not typedef, as quad_t exists as a struct on some systems */
599 #define quad_t long long
600 #define u_quad_t unsigned long long
601 #define strtoq strtoll
602 #define strtouq strtoull
603 #endif
604
605 #endif /* !__NETBSD_COMPAT_DEFS_H__ */
606