compat_defs.h revision 1.30 1 /* $NetBSD: compat_defs.h,v 1.30 2003/08/27 18:33:26 uwe 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 #if !defined(SIZE_MAX) && defined(SIZE_T_MAX)
410 #define SIZE_MAX SIZE_T_MAX
411 #endif
412
413 #ifndef UINT32_MAX
414 #define UINT32_MAX 0xffffffffU
415 #endif
416
417 /* <stdlib.h> */
418
419 #ifndef __GNUC__
420 # if HAVE_ALLOCA_H
421 # include <alloca.h>
422 # else
423 # ifndef alloca /* predefined by HP cc +Olibcalls */
424 char *alloca ();
425 # endif
426 # endif
427 #endif
428
429 /* avoid prototype conflicts with host */
430 #define cgetcap __nbcompat_cgetcap
431 #define cgetclose __nbcompat_cgetclose
432 #define cgetent __nbcompat_cgetent
433 #define cgetfirst __nbcompat_cgetfirst
434 #define cgetmatch __nbcompat_cgetmatch
435 #define cgetnext __nbcompat_cgetnext
436 #define cgetnum __nbcompat_cgetnum
437 #define cgetset __nbcompat_cgetset
438 #define cgetstr __nbcompat_cgetstr
439 #define cgetustr __nbcompat_cgetustr
440
441 char *cgetcap(char *, const char *, int);
442 int cgetclose(void);
443 int cgetent(char **, char **, const char *);
444 int cgetfirst(char **, char **);
445 int cgetmatch(const char *, const char *);
446 int cgetnext(char **, char **);
447 int cgetnum(char *, const char *, long *);
448 int cgetset(const char *);
449 int cgetstr(char *, const char *, char **);
450 int cgetustr(char *, const char *, char **);
451
452 /* <sys/endian.h> */
453
454 #ifdef HAVE_SYS_ENDIAN_H
455 #include <sys/endian.h>
456 #else
457 #if WORDS_BIGENDIAN
458 #define htobe16(x) (x)
459 #define htobe32(x) (x)
460 #define htobe64(x) (x)
461 #define htole16(x) bswap16((u_int16_t)(x))
462 #define htole32(x) bswap32((u_int32_t)(x))
463 #define htole64(x) bswap64((u_int64_t)(x))
464 #else
465 #define htobe16(x) bswap16((u_int16_t)(x))
466 #define htobe32(x) bswap32((u_int32_t)(x))
467 #define htobe64(x) bswap64((u_int64_t)(x))
468 #define htole16(x) (x)
469 #define htole32(x) (x)
470 #define htole64(x) (x)
471 #endif
472 #define be16toh(x) htobe16(x)
473 #define be32toh(x) htobe32(x)
474 #define be64toh(x) htobe64(x)
475 #define le16toh(x) htole16(x)
476 #define le32toh(x) htole32(x)
477 #define le64toh(x) htole64(x)
478 #endif
479
480 /* <sys/mman.h> */
481
482 #ifndef MAP_FILE
483 #define MAP_FILE 0
484 #endif
485
486 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
487 #ifndef MAP_ANON
488 #ifdef MAP_ANONYMOUS
489 #define MAP_ANON MAP_ANONYMOUS
490 #endif
491 #endif
492
493 /* <sys/param.h> */
494
495 #undef BIG_ENDIAN
496 #undef LITTLE_ENDIAN
497 #define BIG_ENDIAN 4321
498 #define LITTLE_ENDIAN 1234
499
500 #undef BYTE_ORDER
501 #if WORDS_BIGENDIAN
502 #define BYTE_ORDER BIG_ENDIAN
503 #else
504 #define BYTE_ORDER LITTLE_ENDIAN
505 #endif
506
507 #ifndef DEV_BSIZE
508 #define DEV_BSIZE (1 << 9)
509 #endif
510
511 #undef MIN
512 #undef MAX
513 #define MIN(a,b) ((a) < (b) ? (a) : (b))
514 #define MAX(a,b) ((a) > (b) ? (a) : (b))
515
516 #ifndef MAXBSIZE
517 #define MAXBSIZE (64 * 1024)
518 #endif
519 #ifndef MAXFRAG
520 #define MAXFRAG 8
521 #endif
522 #ifndef MAXPHYS
523 #define MAXPHYS (64 * 1024)
524 #endif
525
526 /* XXX needed by makefs; this should be done in a better way */
527 #undef btodb
528 #define btodb(x) ((x) << 9)
529
530 #undef setbit
531 #undef clrbit
532 #undef isset
533 #undef isclr
534 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
535 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
536 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
537 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
538
539 #ifndef powerof2
540 #define powerof2(x) ((((x)-1)&(x))==0)
541 #endif
542
543 #undef roundup
544 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
545
546 /* <sys/stat.h> */
547
548 #ifndef ALLPERMS
549 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
550 #endif
551 #ifndef DEFFILEMODE
552 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
553 #endif
554 #ifndef S_ISTXT
555 #ifdef S_ISVTX
556 #define S_ISTXT S_ISVTX
557 #else
558 #define S_ISTXT 0
559 #endif
560 #endif
561
562 /* <sys/syslimits.h> */
563
564 #ifndef LINE_MAX
565 #define LINE_MAX 2048
566 #endif
567
568 /* <sys/time.h> */
569
570 #ifndef timercmp
571 #define timercmp(tvp, uvp, cmp) \
572 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
573 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
574 ((tvp)->tv_sec cmp (uvp)->tv_sec))
575 #endif
576 #ifndef timeradd
577 #define timeradd(tvp, uvp, vvp) \
578 do { \
579 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
580 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
581 if ((vvp)->tv_usec >= 1000000) { \
582 (vvp)->tv_sec++; \
583 (vvp)->tv_usec -= 1000000; \
584 } \
585 } while (/* CONSTCOND */ 0)
586 #endif
587 #ifndef timersub
588 #define timersub(tvp, uvp, vvp) \
589 do { \
590 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
591 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
592 if ((vvp)->tv_usec < 0) { \
593 (vvp)->tv_sec--; \
594 (vvp)->tv_usec += 1000000; \
595 } \
596 } while (/* CONSTCOND */ 0)
597 #endif
598
599 /* <sys/types.h> */
600
601 #if !HAVE_U_QUAD_T
602 /* #define, not typedef, as quad_t exists as a struct on some systems */
603 #define quad_t long long
604 #define u_quad_t unsigned long long
605 #define strtoq strtoll
606 #define strtouq strtoull
607 #endif
608
609 #endif /* !__NETBSD_COMPAT_DEFS_H__ */
610