blob: aeb8bf8b9d776d19f813e26e26dd00cdfc154919 [file] [log] [blame] [raw]
Nicholas Marriottf71b3052010-12-31 22:12:33 +00001/* $Id: compat.h,v 1.32 2010-12-31 22:12:33 nicm Exp $ */
Nicholas Marriotta9e3d5c2009-06-25 16:47:00 +00002
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 */
Nicholas Marriott58684ea2010-10-27 21:34:59 +000018
19#ifndef COMPAT_H
20#define COMPAT_H
Nicholas Marriott914f8582010-10-27 21:33:15 +000021
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
Nicholas Marriotta9e3d5c2009-06-25 16:47:00 +000032
Nicholas Marriottf71b3052010-12-31 22:12:33 +000033#ifndef HAVE_BSD_TYPES
Nicholas Marriott2f517112009-08-20 12:49:18 +000034typedef 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
Nicholas Marriotta9e3d5c2009-06-25 16:47:00 +000040#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"
Nicholas Marriott48c36d62009-07-21 13:07:50 +000045#define _PATH_DEV "/dev/"
Nicholas Marriotta9e3d5c2009-06-25 16:47:00 +000046#endif
47
48#ifdef HAVE_QUEUE_H
Nicholas Marriott802e1292009-06-25 17:02:59 +000049#include <sys/queue.h>
Nicholas Marriotta9e3d5c2009-06-25 16:47:00 +000050#else
51#include "compat/queue.h"
52#endif
53
54#ifdef HAVE_TREE_H
Nicholas Marriott802e1292009-06-25 17:02:59 +000055#include <sys/tree.h>
Nicholas Marriotta9e3d5c2009-06-25 16:47:00 +000056#else
57#include "compat/tree.h"
58#endif
Nicholas Marriott9e7a5fa2010-05-19 21:49:57 +000059
Nicholas Marriott802e1292009-06-25 17:02:59 +000060#ifdef HAVE_BITSTRING_H
61#include <bitstring.h>
62#else
63#include "compat/bitstring.h"
Nicholas Marriotta9e3d5c2009-06-25 16:47:00 +000064#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
Nicholas Marriott32312e02009-06-25 20:27:32 +000082#ifdef HAVE_VIS
83#include <vis.h>
84#else
85#include "compat/vis.h"
Nicholas Marriott09cc5302009-08-20 05:34:58 +000086#endif
87
Nicholas Marriott914f8582010-10-27 21:33:15 +000088#ifdef HAVE_IMSG
Tiago Cunha60134ce2010-06-06 13:00:47 +000089#include <imsg.h>
Nicholas Marriott914f8582010-10-27 21:33:15 +000090#else
91#include "compat/imsg.h"
Tiago Cunhac2745512009-09-03 20:54:39 +000092#endif
93
Nicholas Marriottf54482a2010-09-07 19:32:58 +000094#ifdef HAVE_STDINT_H
95#include <stdint.h>
96#else
97#include <inttypes.h>
98#endif
99
Nicholas Marriottf71b3052010-12-31 22:12:33 +0000100#ifdef BROKEN_CMSG_FIRSTHDR
Nicholas Marriott26682252009-09-02 12:30:56 +0000101#undef CMSG_FIRSTHDR
102#define CMSG_FIRSTHDR(mhdr) \
Nicholas Marriott9c01a3d2010-05-19 21:40:49 +0000103 ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
104 (struct cmsghdr *)(mhdr)->msg_control : \
Nicholas Marriott26682252009-09-02 12:30:56 +0000105 (struct cmsghdr *)NULL)
Nicholas Marriott9c01a3d2010-05-19 21:40:49 +0000106#endif
107
Nicholas Marriott9c01a3d2010-05-19 21:40:49 +0000108#ifndef CMSG_ALIGN
Nicholas Marriottf71b3052010-12-31 22:12:33 +0000109#ifdef _CMSG_DATA_ALIGN
Nicholas Marriott9c01a3d2010-05-19 21:40:49 +0000110#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))
Nicholas Marriott26682252009-09-02 12:30:56 +0000122#endif
123
Nicholas Marriottec818b82009-07-01 22:46:13 +0000124#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)
Nicholas Marriottec818b82009-07-01 22:46:13 +0000134#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
Tiago Cunhac2745512009-09-03 20:54:39 +0000157#endif
158
Nicholas Marriott698e0032009-08-20 12:25:17 +0000159#ifndef HAVE_BZERO
Nicholas Marriotta6fc49a2010-11-11 20:45:49 +0000160#undef bzero
Nicholas Marriott9c01a3d2010-05-19 21:40:49 +0000161#define bzero(buf, len) memset(buf, 0, len);
Nicholas Marriott698e0032009-08-20 12:25:17 +0000162#endif
163
Nicholas Marriott2287ec72010-10-27 20:21:01 +0000164#ifndef HAVE_CLOSEFROM
165/* closefrom.c */
Nicholas Marriott2287ec72010-10-27 20:21:01 +0000166void closefrom(int);
167#endif
168
Nicholas Marriott1f1623e2009-07-02 07:31:02 +0000169#ifndef HAVE_STRCASESTR
170/* strcasestr.c */
171char *strcasestr(const char *, const char *);
172#endif
173
Nicholas Marriott8f9858b2009-08-16 16:15:53 +0000174#ifndef HAVE_STRSEP
175/* strsep.c */
176char *strsep(char **, const char *);
177#endif
178
Nicholas Marriotta9e3d5c2009-06-25 16:47:00 +0000179#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 */
Nicholas Marriotta6fc49a2010-11-11 20:45:49 +0000201#include <sys/ioctl.h>
Nicholas Marriotta9e3d5c2009-06-25 16:47:00 +0000202pid_t forkpty(int *, char *, struct termios *, struct winsize *);
203#endif
204
205#ifndef HAVE_ASPRINTF
206/* asprintf.c */
Nicholas Marriott278effd2010-05-19 21:31:39 +0000207int asprintf(char **, const char *, ...);
208int vasprintf(char **, const char *, va_list);
Nicholas Marriotta9e3d5c2009-06-25 16:47:00 +0000209#endif
210
211#ifndef HAVE_FGETLN
212/* fgetln.c */
Nicholas Marriott278effd2010-05-19 21:31:39 +0000213char *fgetln(FILE *, size_t *);
214#endif
215
216#ifndef HAVE_SETENV
217/* setenv.c */
Nicholas Marriotta9c69762010-06-05 16:29:40 +0000218int setenv(const char *, const char *, int);
219int unsetenv(const char *);
Nicholas Marriotta9e3d5c2009-06-25 16:47:00 +0000220#endif
221
Nicholas Marriott58684ea2010-10-27 21:34:59 +0000222#ifdef HAVE_GETOPT
223#include <getopt.h>
224#else
Nicholas Marriotta9e3d5c2009-06-25 16:47:00 +0000225/* 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
Nicholas Marriott58684ea2010-10-27 21:34:59 +0000239
240#endif /* COMPAT_H */