2 * Copyright (c) 1996, 1998-2004 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.80 2004/09/10 16:31:15 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
40 * Some systems lack full limit definitions.
47 # define INT_MAX 0x7fffffff
52 # define PATH_MAX MAXPATHLEN
54 # ifdef _POSIX_PATH_MAX
55 # define PATH_MAX _POSIX_PATH_MAX
57 # define PATH_MAX 1024
62 #ifndef MAXHOSTNAMELEN
63 # define MAXHOSTNAMELEN 64
67 * Posix versions for those without...
70 # define _S_IFMT S_IFMT
73 # define _S_IFREG S_IFREG
76 # define _S_IFDIR S_IFDIR
79 # define _S_IFLNK S_IFLNK
82 # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
85 # define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
89 * Some OS's may not have this.
92 # define S_IRWXU 0000700 /* rwx for owner */
96 * These should be defined in <unistd.h> but not everyone has them.
99 # define STDIN_FILENO 0
101 #ifndef STDOUT_FILENO
102 # define STDOUT_FILENO 1
104 #ifndef STDERR_FILENO
105 # define STDERR_FILENO 2
109 * These should be defined in <unistd.h> but not everyone has them.
122 * BSD defines these in <sys/param.h> but others may not.
125 # define MIN(a,b) (((a)<(b))?(a):(b))
128 # define MAX(a,b) (((a)>(b))?(a):(b))
132 * Simple isblank() macro for systems without it.
135 # define isblank(_x) ((_x) == ' ' || (_x) == '\t')
139 * Old BSD systems lack strchr(), strrchr(), memset() and memcpy()
141 #if !defined(HAVE_STRCHR) && !defined(strchr)
142 # define strchr(_s, _c) index(_s, _c)
144 #if !defined(HAVE_STRRCHR) && !defined(strrchr)
145 # define strrchr(_s, _c) rindex(_s, _c)
147 #if !defined(HAVE_MEMCPY) && !defined(memcpy)
148 # define memcpy(_d, _s, _n) (bcopy(_s, _d, _n))
150 #if !defined(HAVE_MEMSET) && !defined(memset)
151 # define memset(_s, _x, _n) (bzero(_s, _n))
155 * NCR's SVr4 has _innetgr(3) instead of innetgr(3) for some reason.
158 # define innetgr(n, h, u, d) (_innetgr(n, h, u, d))
159 # define HAVE_INNETGR 1
160 #endif /* HAVE__INNETGR */
163 * On POSIX systems, O_NOCTTY is the default so some OS's may lack this define.
167 #endif /* O_NOCTTY */
170 * Emulate POSIX signals via sigvec(2)
172 #ifndef HAVE_SIGACTION
173 # define SA_ONSTACK SV_ONSTACK
174 # define SA_RESTART SV_INTERRUPT /* opposite effect */
175 # define SA_RESETHAND SV_RESETHAND
176 # define sa_handler sv_handler
177 # define sa_mask sv_mask
178 # define sa_flags sv_flags
179 typedef struct sigvec sigaction_t;
180 typedef int sigset_t;
181 int sigaction __P((int sig, const sigaction_t *act, sigaction_t *oact));
182 int sigemptyset __P((sigset_t *));
183 int sigfillset __P((sigset_t *));
184 int sigaddset __P((sigset_t *, int));
185 int sigdelset __P((sigset_t *, int));
186 int sigismember __P((sigset_t *, int));
187 int sigprocmask __P((int, const sigset_t *, sigset_t *));
191 * Extra sugar for POSIX signals to deal with the above emulation
192 * as well as the fact that SunOS has a SA_INTERRUPT flag.
194 #ifdef HAVE_SIGACTION
195 # ifndef HAVE_SIGACTION_T
196 typedef struct sigaction sigaction_t;
198 # ifndef SA_INTERRUPT
199 # define SA_INTERRUPT 0
202 # define SA_RESTART 0
207 * If dirfd() does not exists, hopefully dd_fd does.
209 #if !defined(HAVE_DIRFD) && defined(HAVE_DD_FD)
210 # define dirfd(_d) ((_d)->dd_fd)
215 * Define futimes() in terms of futimesat() if needed.
217 #if !defined(HAVE_FUTIMES) && defined(HAVE_FUTIMESAT)
218 # define futimes(_f, _tv) futimesat(_f, NULL, _tv)
219 # define HAVE_FUTIMES
223 * If we lack getprogname(), emulate with __progname if possible.
224 * Otherwise, add a prototype for use with our own getprogname.c.
226 #ifndef HAVE_GETPROGNAME
227 # ifdef HAVE___PROGNAME
228 extern const char *__progname;
229 # define getprogname() (__progname)
231 const char *getprogname __P((void));
232 #endif /* HAVE___PROGNAME */
233 #endif /* !HAVE_GETPROGNAME */
235 #ifndef HAVE_TIMESPEC
240 #endif /* !HAVE_TIMESPEC */
242 #ifndef timespecclear
243 # define timespecclear(ts) (ts)->tv_sec = (ts)->tv_nsec = 0
245 #ifndef timespecisset
246 # define timespecisset(ts) ((ts)->tv_sec || (ts)->tv_nsec)
249 # define timespecsub(minuend, subrahend, difference) \
251 (difference)->tv_sec = (minuend)->tv_sec - (subrahend)->tv_sec; \
252 (difference)->tv_nsec = (minuend)->tv_nsec - (subrahend)->tv_nsec; \
253 if ((difference)->tv_nsec < 0) { \
254 (difference)->tv_nsec += 1000000000L; \
255 (difference)->tv_sec--; \
260 #endif /* _SUDO_COMPAT_H */