1/* $Id: compat.h,v 1.1.1.2 2011/08/17 18:40:05 jmmv Exp $ */
2
3/*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#ifndef COMPAT_H
20#define COMPAT_H
21
22#ifndef __GNUC__
23#define __attribute__(a)
24#endif
25
26#ifndef __dead
27#define __dead __attribute__ ((__noreturn__))
28#endif
29#ifndef __packed
30#define __packed __attribute__ ((__packed__))
31#endif
32
33#ifndef HAVE_BSD_TYPES
34typedef uint8_t u_int8_t;
35typedef uint16_t u_int16_t;
36typedef uint32_t u_int32_t;
37typedef uint64_t u_int64_t;
38#endif
39
40#ifndef HAVE_PATHS_H
41#define	_PATH_BSHELL	"/bin/sh"
42#define	_PATH_TMP	"/tmp/"
43#define _PATH_DEVNULL	"/dev/null"
44#define _PATH_TTY	"/dev/tty"
45#define _PATH_DEV	"/dev/"
46#endif
47
48#ifdef HAVE_QUEUE_H
49#include <sys/queue.h>
50#else
51#include "compat/queue.h"
52#endif
53
54#ifdef HAVE_TREE_H
55#include <sys/tree.h>
56#else
57#include "compat/tree.h"
58#endif
59
60#ifdef HAVE_BITSTRING_H
61#include <bitstring.h>
62#else
63#include "compat/bitstring.h"
64#endif
65
66#ifdef HAVE_PATHS_H
67#include <paths.h>
68#endif
69
70#ifdef HAVE_FORKPTY
71#ifdef HAVE_LIBUTIL_H
72#include <libutil.h>
73#endif
74#ifdef HAVE_PTY_H
75#include <pty.h>
76#endif
77#ifdef HAVE_UTIL_H
78#include <util.h>
79#endif
80#endif
81
82#ifdef HAVE_VIS
83#include <vis.h>
84#else
85#include "compat/vis.h"
86#endif
87
88#ifdef HAVE_IMSG
89#include <imsg.h>
90#else
91#include "compat/imsg.h"
92#endif
93
94#ifdef HAVE_STDINT_H
95#include <stdint.h>
96#else
97#include <inttypes.h>
98#endif
99
100#ifdef BROKEN_CMSG_FIRSTHDR
101#undef CMSG_FIRSTHDR
102#define CMSG_FIRSTHDR(mhdr) \
103	((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
104	    (struct cmsghdr *)(mhdr)->msg_control :	    \
105	    (struct cmsghdr *)NULL)
106#endif
107
108#ifndef CMSG_ALIGN
109#ifdef _CMSG_DATA_ALIGN
110#define CMSG_ALIGN _CMSG_DATA_ALIGN
111#else
112#define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1))
113#endif
114#endif
115
116#ifndef CMSG_SPACE
117#define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
118#endif
119
120#ifndef CMSG_LEN
121#define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
122#endif
123
124#ifndef INFTIM
125#define INFTIM -1
126#endif
127
128#ifndef WAIT_ANY
129#define WAIT_ANY -1
130#endif
131
132#ifndef SUN_LEN
133#define SUN_LEN(sun) (sizeof (sun)->sun_path)
134#endif
135
136#ifndef timercmp
137#define	timercmp(tvp, uvp, cmp)						\
138	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
139	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
140	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
141#endif
142
143#ifndef timeradd
144#define	timeradd(tvp, uvp, vvp)						\
145	do {								\
146		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
147		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
148		if ((vvp)->tv_usec >= 1000000) {			\
149			(vvp)->tv_sec++;				\
150			(vvp)->tv_usec -= 1000000;			\
151		}							\
152	} while (0)
153#endif
154
155#ifndef TTY_NAME_MAX
156#define TTY_NAME_MAX 32
157#endif
158
159#ifndef HAVE_BZERO
160#undef bzero
161#define bzero(buf, len) memset(buf, 0, len);
162#endif
163
164#ifndef HAVE_CLOSEFROM
165/* closefrom.c */
166void	closefrom(int);
167#endif
168
169#ifndef HAVE_STRCASESTR
170/* strcasestr.c */
171char		*strcasestr(const char *, const char *);
172#endif
173
174#ifndef HAVE_STRSEP
175/* strsep.c */
176char		*strsep(char **, const char *);
177#endif
178
179#ifndef HAVE_STRTONUM
180/* strtonum.c */
181long long	 strtonum(const char *, long long, long long, const char **);
182#endif
183
184#ifndef HAVE_STRLCPY
185/* strlcpy.c */
186size_t	 	 strlcpy(char *, const char *, size_t);
187#endif
188
189#ifndef HAVE_STRLCAT
190/* strlcat.c */
191size_t	 	 strlcat(char *, const char *, size_t);
192#endif
193
194#ifndef HAVE_DAEMON
195/* daemon.c */
196int	 	 daemon(int, int);
197#endif
198
199#ifndef HAVE_FORKPTY
200/* forkpty.c */
201#include <sys/ioctl.h>
202pid_t		 forkpty(int *, char *, struct termios *, struct winsize *);
203#endif
204
205#ifndef HAVE_ASPRINTF
206/* asprintf.c */
207int		 asprintf(char **, const char *, ...);
208int		 vasprintf(char **, const char *, va_list);
209#endif
210
211#ifndef HAVE_FGETLN
212/* fgetln.c */
213char		*fgetln(FILE *, size_t *);
214#endif
215
216#ifndef HAVE_SETENV
217/* setenv.c */
218int		 setenv(const char *, const char *, int);
219int		 unsetenv(const char *);
220#endif
221
222#ifdef HAVE_GETOPT
223#include <getopt.h>
224#else
225/* getopt.c */
226extern int	BSDopterr;
227extern int	BSDoptind;
228extern int	BSDoptopt;
229extern int	BSDoptreset;
230extern char    *BSDoptarg;
231int	BSDgetopt(int, char *const *, const char *);
232#define getopt(ac, av, o)  BSDgetopt(ac, av, o)
233#define opterr             BSDopterr
234#define optind             BSDoptind
235#define optopt             BSDoptopt
236#define optreset           BSDoptreset
237#define optarg             BSDoptarg
238#endif
239
240#endif /* COMPAT_H */
241