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