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