2 * Copyright (c) 1996, 1998-2005, 2008
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.
21 * $Sudo: compat.h,v 1.90 2008/11/09 14:13:12 millert Exp $
24 #ifndef _SUDO_COMPAT_H
25 #define _SUDO_COMPAT_H
28 * Macros that may be missing on some Operating Systems
31 /* Deal with ANSI stuff reasonably. */
33 # if defined (__cplusplus) || defined (__STDC__)
34 # define __P(args) args
40 /* Define away __attribute__ for non-gcc or old gcc */
41 #if !defined(__GNUC__) || __GNUC__ < 2 || __GNUC__ == 2 && __GNUC_MINOR__ < 5
42 # define __attribute__(x)
45 /* For silencing gcc warnings about rcsids */
47 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ > 7)
48 # define __unused __attribute__((__unused__))
54 /* For catching format string mismatches */
56 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7)
57 # define __printflike(f, v) __attribute__((__format__ (__printf__, f, v)))
59 # define __printflike(f, v)
64 * Some systems lack full limit definitions.
71 # define INT_MAX 0x7fffffff
76 # define PATH_MAX MAXPATHLEN
78 # ifdef _POSIX_PATH_MAX
79 # define PATH_MAX _POSIX_PATH_MAX
81 # define PATH_MAX 1024
86 #ifndef MAXHOSTNAMELEN
87 # define MAXHOSTNAMELEN 64
91 * Posix versions for those without...
94 # define _S_IFMT S_IFMT
97 # define _S_IFREG S_IFREG
100 # define _S_IFDIR S_IFDIR
101 #endif /* _S_IFDIR */
103 # define _S_IFLNK S_IFLNK
104 #endif /* _S_IFLNK */
106 # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
109 # define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
113 * Some OS's may not have this.
116 # define S_IRWXU 0000700 /* rwx for owner */
120 * These should be defined in <unistd.h> but not everyone has them.
123 # define STDIN_FILENO 0
125 #ifndef STDOUT_FILENO
126 # define STDOUT_FILENO 1
128 #ifndef STDERR_FILENO
129 # define STDERR_FILENO 2
133 * These should be defined in <unistd.h> but not everyone has them.
146 * BSD defines these in <sys/param.h> but others may not.
149 # define MIN(a,b) (((a)<(b))?(a):(b))
152 # define MAX(a,b) (((a)>(b))?(a):(b))
156 * Simple isblank() macro and function for systems without it.
159 int isblank __P((int));
160 # define isblank(_x) ((_x) == ' ' || (_x) == '\t')
164 * Old BSD systems lack strchr(), strrchr(), memset() and memcpy()
166 #if !defined(HAVE_STRCHR) && !defined(strchr)
167 # define strchr(_s, _c) index(_s, _c)
169 #if !defined(HAVE_STRRCHR) && !defined(strrchr)
170 # define strrchr(_s, _c) rindex(_s, _c)
172 #if !defined(HAVE_MEMCPY) && !defined(memcpy)
173 # define memcpy(_d, _s, _n) (bcopy(_s, _d, _n))
175 #if !defined(HAVE_MEMSET) && !defined(memset)
176 # define memset(_s, _x, _n) (bzero(_s, _n))
180 * NCR's SVr4 has _innetgr(3) instead of innetgr(3) for some reason.
183 # define innetgr(n, h, u, d) (_innetgr(n, h, u, d))
184 # define HAVE_INNETGR 1
185 #endif /* HAVE__INNETGR */
188 * On POSIX systems, O_NOCTTY is the default so some OS's may lack this define.
192 #endif /* O_NOCTTY */
195 * Emulate POSIX signals via sigvec(2)
197 #ifndef HAVE_SIGACTION
198 # define SA_ONSTACK SV_ONSTACK
199 # define SA_RESTART SV_INTERRUPT /* opposite effect */
200 # define SA_RESETHAND SV_RESETHAND
201 # define sa_handler sv_handler
202 # define sa_mask sv_mask
203 # define sa_flags sv_flags
204 typedef struct sigvec sigaction_t;
205 typedef int sigset_t;
206 int sigaction __P((int sig, const sigaction_t *act, sigaction_t *oact));
207 int sigemptyset __P((sigset_t *));
208 int sigfillset __P((sigset_t *));
209 int sigaddset __P((sigset_t *, int));
210 int sigdelset __P((sigset_t *, int));
211 int sigismember __P((sigset_t *, int));
212 int sigprocmask __P((int, const sigset_t *, sigset_t *));
216 * Extra sugar for POSIX signals to deal with the above emulation
217 * as well as the fact that SunOS has a SA_INTERRUPT flag.
219 #ifdef HAVE_SIGACTION
220 # ifndef HAVE_SIGACTION_T
221 typedef struct sigaction sigaction_t;
223 # ifndef SA_INTERRUPT
224 # define SA_INTERRUPT 0
227 # define SA_RESTART 0
232 * If dirfd() does not exists, hopefully dd_fd does.
234 #if !defined(HAVE_DIRFD) && defined(HAVE_DD_FD)
235 # define dirfd(_d) ((_d)->dd_fd)
240 * Define futimes() in terms of futimesat() if needed.
242 #if !defined(HAVE_FUTIMES) && defined(HAVE_FUTIMESAT)
243 # define futimes(_f, _tv) futimesat(_f, NULL, _tv)
244 # define HAVE_FUTIMES
248 * If we lack getprogname(), emulate with __progname if possible.
249 * Otherwise, add a prototype for use with our own getprogname.c.
251 #ifndef HAVE_GETPROGNAME
252 # ifdef HAVE___PROGNAME
253 extern const char *__progname;
254 # define getprogname() (__progname)
256 const char *getprogname __P((void));
257 #endif /* HAVE___PROGNAME */
258 #endif /* !HAVE_GETPROGNAME */
260 #ifndef timespecclear
261 # define timespecclear(ts) (ts)->tv_sec = (ts)->tv_nsec = 0
263 #ifndef timespecisset
264 # define timespecisset(ts) ((ts)->tv_sec || (ts)->tv_nsec)
267 # define timespecsub(minuend, subrahend, difference) \
269 (difference)->tv_sec = (minuend)->tv_sec - (subrahend)->tv_sec; \
270 (difference)->tv_nsec = (minuend)->tv_nsec - (subrahend)->tv_nsec; \
271 if ((difference)->tv_nsec < 0) { \
272 (difference)->tv_nsec += 1000000000L; \
273 (difference)->tv_sec--; \
278 #endif /* _SUDO_COMPAT_H */