2 * Copyright (c) 1996, 1998-2005 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * Sponsored in part by the Defense Advanced Research Projects
17 * Agency (DARPA) and Air Force Research Laboratory, Air Force
18 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
20 * $Sudo: compat.h,v 1.76.2.4 2007/06/12 01:28:41 millert Exp $
23 #ifndef _SUDO_COMPAT_H
24 #define _SUDO_COMPAT_H
27 * Macros that may be missing on some Operating Systems
30 /* Deal with ANSI stuff reasonably. */
32 # if defined (__cplusplus) || defined (__STDC__)
33 # define __P(args) args
39 /* Define away __attribute__ for non-gcc or old gcc */
40 #if !defined(__GNUC__) || __GNUC__ < 2 || __GNUC__ == 2 && __GNUC_MINOR__ < 5
41 # define __attribute__(x)
44 /* For silencing gcc warnings about rcsids */
46 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ > 7)
47 # define __unused __attribute__((__unused__))
53 /* For catching format string mismatches */
55 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7)
56 # define __printflike(f, v) __attribute__((__format__ (__printf__, f, v)))
58 # define __printflike(f, v)
63 * Some systems lack full limit definitions.
70 # define INT_MAX 0x7fffffff
75 # define PATH_MAX MAXPATHLEN
77 # ifdef _POSIX_PATH_MAX
78 # define PATH_MAX _POSIX_PATH_MAX
80 # define PATH_MAX 1024
85 #ifndef MAXHOSTNAMELEN
86 # define MAXHOSTNAMELEN 64
90 * Posix versions for those without...
93 # define _S_IFMT S_IFMT
96 # define _S_IFREG S_IFREG
99 # define _S_IFDIR S_IFDIR
100 #endif /* _S_IFDIR */
102 # define _S_IFLNK S_IFLNK
103 #endif /* _S_IFLNK */
105 # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
108 # define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
112 * Some OS's may not have this.
115 # define S_IRWXU 0000700 /* rwx for owner */
119 * These should be defined in <unistd.h> but not everyone has them.
122 # define STDIN_FILENO 0
124 #ifndef STDOUT_FILENO
125 # define STDOUT_FILENO 1
127 #ifndef STDERR_FILENO
128 # define STDERR_FILENO 2
132 * These should be defined in <unistd.h> but not everyone has them.
145 * BSD defines these in <sys/param.h> but others may not.
148 # define MIN(a,b) (((a)<(b))?(a):(b))
151 # define MAX(a,b) (((a)>(b))?(a):(b))
155 * Simple isblank() macro for systems without it.
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
246 * If we lack getprogname(), emulate with __progname if possible.
247 * Otherwise, add a prototype for use with our own getprogname.c.
249 #ifndef HAVE_GETPROGNAME
250 # ifdef HAVE___PROGNAME
251 extern const char *__progname;
252 # define getprogname() (__progname)
254 const char *getprogname __P((void));
255 #endif /* HAVE___PROGNAME */
256 #endif /* !HAVE_GETPROGNAME */
258 #ifndef timespecclear
259 # define timespecclear(ts) (ts)->tv_sec = (ts)->tv_nsec = 0
261 #ifndef timespecisset
262 # define timespecisset(ts) ((ts)->tv_sec || (ts)->tv_nsec)
265 # define timespecsub(minuend, subrahend, difference) \
267 (difference)->tv_sec = (minuend)->tv_sec - (subrahend)->tv_sec; \
268 (difference)->tv_nsec = (minuend)->tv_nsec - (subrahend)->tv_nsec; \
269 if ((difference)->tv_nsec < 0) { \
270 (difference)->tv_nsec += 1000000000L; \
271 (difference)->tv_sec--; \
276 #endif /* _SUDO_COMPAT_H */