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