2 * Copyright (c) 1996, 1998-2005, 2008-2010
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22 #ifndef _SUDO_COMPAT_H
23 #define _SUDO_COMPAT_H
26 * Macros that may be missing on some Operating Systems
29 /* Deal with ANSI stuff reasonably. */
31 # if defined (__cplusplus) || defined (__STDC__)
32 # define __P(args) args
38 /* Define away __attribute__ for non-gcc or old gcc */
39 #if !defined(__GNUC__) || __GNUC__ < 2 || __GNUC__ == 2 && __GNUC_MINOR__ < 5
40 # define __attribute__(x)
43 /* For silencing gcc warnings about rcsids */
45 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ > 7)
46 # define __unused __attribute__((__unused__))
52 /* For catching format string mismatches */
54 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7)
55 # define __printflike(f, v) __attribute__((__format__ (__printf__, f, v)))
57 # define __printflike(f, v)
62 * Some systems lack full limit definitions.
69 # define INT_MAX 0x7fffffff
74 # define PATH_MAX MAXPATHLEN
76 # ifdef _POSIX_PATH_MAX
77 # define PATH_MAX _POSIX_PATH_MAX
79 # define PATH_MAX 1024
84 #ifndef MAXHOSTNAMELEN
85 # define MAXHOSTNAMELEN 64
89 * Posix versions for those without...
92 # define _S_IFMT S_IFMT
95 # define _S_IFREG S_IFREG
98 # define _S_IFDIR S_IFDIR
101 # define _S_IFLNK S_IFLNK
102 #endif /* _S_IFLNK */
104 # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
107 # define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
111 * Some OS's may not have this.
114 # define S_IRWXU 0000700 /* rwx for owner */
118 * These should be defined in <unistd.h> but not everyone has them.
121 # define STDIN_FILENO 0
123 #ifndef STDOUT_FILENO
124 # define STDOUT_FILENO 1
126 #ifndef STDERR_FILENO
127 # define STDERR_FILENO 2
131 * These should be defined in <unistd.h> but not everyone has them.
144 * BSD defines these in <sys/param.h> but others may not.
147 # define MIN(a,b) (((a)<(b))?(a):(b))
150 # define MAX(a,b) (((a)>(b))?(a):(b))
154 * Simple isblank() macro and function for systems without it.
157 int isblank __P((int));
158 # define isblank(_x) ((_x) == ' ' || (_x) == '\t')
162 * Old BSD systems lack strchr(), strrchr(), memset() and memcpy()
164 #if !defined(HAVE_STRCHR) && !defined(strchr)
165 # define strchr(_s, _c) index(_s, _c)
167 #if !defined(HAVE_STRRCHR) && !defined(strrchr)
168 # define strrchr(_s, _c) rindex(_s, _c)
170 #if !defined(HAVE_MEMCPY) && !defined(memcpy)
171 # define memcpy(_d, _s, _n) (bcopy(_s, _d, _n))
173 #if !defined(HAVE_MEMSET) && !defined(memset)
174 # define memset(_s, _x, _n) (bzero(_s, _n))
178 * NCR's SVr4 has _innetgr(3) instead of innetgr(3) for some reason.
181 # define innetgr(n, h, u, d) (_innetgr(n, h, u, d))
182 # define HAVE_INNETGR 1
183 #endif /* HAVE__INNETGR */
186 * On POSIX systems, O_NOCTTY is the default so some OS's may lack this define.
190 #endif /* O_NOCTTY */
193 * Emulate POSIX signals via sigvec(2)
195 #ifndef HAVE_SIGACTION
196 # define SA_ONSTACK SV_ONSTACK
197 # define SA_RESTART SV_INTERRUPT /* opposite effect */
198 # define SA_RESETHAND SV_RESETHAND
199 # define sa_handler sv_handler
200 # define sa_mask sv_mask
201 # define sa_flags sv_flags
202 typedef struct sigvec sigaction_t;
203 typedef int sigset_t;
204 int sigaction __P((int sig, const sigaction_t *act, sigaction_t *oact));
205 int sigemptyset __P((sigset_t *));
206 int sigfillset __P((sigset_t *));
207 int sigaddset __P((sigset_t *, int));
208 int sigdelset __P((sigset_t *, int));
209 int sigismember __P((sigset_t *, int));
210 int sigprocmask __P((int, const sigset_t *, sigset_t *));
214 * Extra sugar for POSIX signals to deal with the above emulation
215 * as well as the fact that SunOS has a SA_INTERRUPT flag.
217 #ifdef HAVE_SIGACTION
218 # ifndef HAVE_SIGACTION_T
219 typedef struct sigaction sigaction_t;
221 # ifndef SA_INTERRUPT
222 # define SA_INTERRUPT 0
225 # define SA_RESTART 0
230 * If dirfd() does not exists, hopefully dd_fd does.
232 #if !defined(HAVE_DIRFD) && defined(HAVE_DD_FD)
233 # define dirfd(_d) ((_d)->dd_fd)
238 * Define futimes() in terms of futimesat() if needed.
240 #if !defined(HAVE_FUTIMES) && defined(HAVE_FUTIMESAT)
241 # define futimes(_f, _tv) futimesat(_f, NULL, _tv)
242 # define HAVE_FUTIMES
245 #if !defined(HAVE_KILLPG) && !defined(killpg)
246 # define killpg(s) kill(-(s))
250 * If we lack getprogname(), emulate with __progname if possible.
251 * Otherwise, add a prototype for use with our own getprogname.c.
253 #ifndef HAVE_GETPROGNAME
254 # ifdef HAVE___PROGNAME
255 extern const char *__progname;
256 # define getprogname() (__progname)
258 const char *getprogname __P((void));
259 #endif /* HAVE___PROGNAME */
260 #endif /* !HAVE_GETPROGNAME */
263 # define timevalclear(tv) ((tv)->tv_sec = (tv)->tv_usec = 0)
266 # define timevalisset(tv) ((tv)->tv_sec || (tv)->tv_usec)
269 # define timevalcmp(tv1, tv2, op) \
270 (((tv1)->tv_sec == (tv2)->tv_sec) ? \
271 ((tv1)->tv_usec op (tv2)->tv_usec) : \
272 ((tv1)->tv_sec op (tv2)->tv_sec))
275 # define timevaladd(tv1, tv2) \
277 (tv1)->tv_sec += (tv2)->tv_sec; \
278 (tv1)->tv_usec += (tv2)->tv_usec; \
279 if ((tv1)->tv_usec >= 1000000) { \
281 (tv1)->tv_usec -= 1000000; \
286 # define timevalsub(tv1, tv2) \
288 (tv1)->tv_sec -= (tv2)->tv_sec; \
289 (tv1)->tv_usec -= (tv2)->tv_usec; \
290 if ((tv1)->tv_usec < 0) { \
292 (tv1)->tv_usec += 1000000; \
297 /* Not all systems define NSIG in signal.h */
301 # elif defined(__NSIG)
309 # define WCOREDUMP(x) ((x) & 0x80)
312 #endif /* _SUDO_COMPAT_H */