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_MISSING_H
23 #define _SUDO_MISSING_H
33 * Macros that may be missing on some Operating Systems
36 /* Deal with ANSI stuff reasonably. */
38 # if defined (__cplusplus) || defined (__STDC__)
39 # define __P(args) args
45 /* Define away __attribute__ for non-gcc or old gcc */
46 #if !defined(__GNUC__) || __GNUC__ < 2 || __GNUC__ == 2 && __GNUC_MINOR__ < 5
47 # define __attribute__(x)
50 /* For silencing gcc warnings about rcsids */
52 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ > 7)
53 # define __unused __attribute__((__unused__))
59 /* For catching format string mismatches */
61 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7)
62 # define __printflike(f, v) __attribute__((__format__ (__printf__, f, v)))
64 # define __printflike(f, v)
69 * Some systems lack full limit definitions.
76 # define INT_MAX 0x7fffffff
81 # define PATH_MAX MAXPATHLEN
83 # ifdef _POSIX_PATH_MAX
84 # define PATH_MAX _POSIX_PATH_MAX
86 # define PATH_MAX 1024
91 #ifndef MAXHOSTNAMELEN
92 # define MAXHOSTNAMELEN 64
96 * Posix versions for those without...
99 # define _S_IFMT S_IFMT
102 # define _S_IFREG S_IFREG
103 #endif /* _S_IFREG */
105 # define _S_IFDIR S_IFDIR
106 #endif /* _S_IFDIR */
108 # define _S_IFLNK S_IFLNK
109 #endif /* _S_IFLNK */
111 # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
114 # define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
118 * Some OS's may not have this.
121 # define S_IRWXU 0000700 /* rwx for owner */
125 * These should be defined in <unistd.h> but not everyone has them.
128 # define STDIN_FILENO 0
130 #ifndef STDOUT_FILENO
131 # define STDOUT_FILENO 1
133 #ifndef STDERR_FILENO
134 # define STDERR_FILENO 2
138 * These should be defined in <unistd.h> but not everyone has them.
151 * BSD defines these in <sys/param.h> but others may not.
154 # define MIN(a,b) (((a)<(b))?(a):(b))
157 # define MAX(a,b) (((a)>(b))?(a):(b))
161 * Simple isblank() macro and function for systems without it.
164 int isblank __P((int));
165 # define isblank(_x) ((_x) == ' ' || (_x) == '\t')
169 * Old BSD systems lack strchr(), strrchr(), memset() and memcpy()
171 #if !defined(HAVE_STRCHR) && !defined(strchr)
172 # define strchr(_s, _c) index(_s, _c)
174 #if !defined(HAVE_STRRCHR) && !defined(strrchr)
175 # define strrchr(_s, _c) rindex(_s, _c)
177 #if !defined(HAVE_MEMCPY) && !defined(memcpy)
178 # define memcpy(_d, _s, _n) (bcopy(_s, _d, _n))
180 #if !defined(HAVE_MEMSET) && !defined(memset)
181 # define memset(_s, _x, _n) (bzero(_s, _n))
185 * NCR's SVr4 has _innetgr(3) instead of innetgr(3) for some reason.
188 # define innetgr(n, h, u, d) (_innetgr(n, h, u, d))
189 # define HAVE_INNETGR 1
190 #endif /* HAVE__INNETGR */
193 * On POSIX systems, O_NOCTTY is the default so some OS's may lack this define.
197 #endif /* O_NOCTTY */
200 * Emulate POSIX signals via sigvec(2)
202 #ifndef HAVE_SIGACTION
203 # define SA_ONSTACK SV_ONSTACK
204 # define SA_RESTART SV_INTERRUPT /* opposite effect */
205 # define SA_RESETHAND SV_RESETHAND
206 # define sa_handler sv_handler
207 # define sa_mask sv_mask
208 # define sa_flags sv_flags
209 typedef struct sigvec sigaction_t;
210 typedef int sigset_t;
211 int sigaction __P((int sig, const sigaction_t *act, sigaction_t *oact));
212 int sigemptyset __P((sigset_t *));
213 int sigfillset __P((sigset_t *));
214 int sigaddset __P((sigset_t *, int));
215 int sigdelset __P((sigset_t *, int));
216 int sigismember __P((sigset_t *, int));
217 int sigprocmask __P((int, const sigset_t *, sigset_t *));
221 * Extra sugar for POSIX signals to deal with the above emulation
222 * as well as the fact that SunOS has a SA_INTERRUPT flag.
224 #ifdef HAVE_SIGACTION
225 # ifndef HAVE_SIGACTION_T
226 typedef struct sigaction sigaction_t;
228 # ifndef SA_INTERRUPT
229 # define SA_INTERRUPT 0
232 # define SA_RESTART 0
237 * If dirfd() does not exists, hopefully dd_fd does.
239 #if !defined(HAVE_DIRFD) && defined(HAVE_DD_FD)
240 # define dirfd(_d) ((_d)->dd_fd)
245 * Define futimes() in terms of futimesat() if needed.
247 #if !defined(HAVE_FUTIMES) && defined(HAVE_FUTIMESAT)
248 # define futimes(_f, _tv) futimesat(_f, NULL, _tv)
249 # define HAVE_FUTIMES
252 #if !defined(HAVE_KILLPG) && !defined(killpg)
253 # define killpg(s) kill(-(s))
257 * If we lack getprogname(), emulate with __progname if possible.
258 * Otherwise, add a prototype for use with our own getprogname.c.
260 #ifndef HAVE_GETPROGNAME
261 # ifdef HAVE___PROGNAME
262 extern const char *__progname;
263 # define getprogname() (__progname)
265 const char *getprogname __P((void));
266 #endif /* HAVE___PROGNAME */
267 #endif /* !HAVE_GETPROGNAME */
270 # define timevalclear(tv) ((tv)->tv_sec = (tv)->tv_usec = 0)
273 # define timevalisset(tv) ((tv)->tv_sec || (tv)->tv_usec)
276 # define timevalcmp(tv1, tv2, op) \
277 (((tv1)->tv_sec == (tv2)->tv_sec) ? \
278 ((tv1)->tv_usec op (tv2)->tv_usec) : \
279 ((tv1)->tv_sec op (tv2)->tv_sec))
282 # define timevaladd(tv1, tv2) \
284 (tv1)->tv_sec += (tv2)->tv_sec; \
285 (tv1)->tv_usec += (tv2)->tv_usec; \
286 if ((tv1)->tv_usec >= 1000000) { \
288 (tv1)->tv_usec -= 1000000; \
293 # define timevalsub(tv1, tv2) \
295 (tv1)->tv_sec -= (tv2)->tv_sec; \
296 (tv1)->tv_usec -= (tv2)->tv_usec; \
297 if ((tv1)->tv_usec < 0) { \
299 (tv1)->tv_usec += 1000000; \
304 /* Not all systems define NSIG in signal.h */
308 # elif defined(__NSIG)
316 # define WCOREDUMP(x) ((x) & 0x80)
320 * HP-UX does not declare innetgr() or getdomainname().
321 * Solaris does not declare getdomainname().
324 int innetgr __P((const char *, const char *, const char *, const char *));
326 #if defined(__hpux) || defined(__sun)
327 int getdomainname __P((char *, size_t));
330 /* Functions "missing" from libc. */
335 #ifndef HAVE_CLOSEFROM
336 void closefrom __P((int));
339 char *getcwd __P((char *, size_t size));
342 ssize_t getline __P((char **, size_t *, FILE *));
345 int utimes __P((const char *, const struct timeval *));
348 int futimes __P((int, const struct timeval *));
350 #ifndef HAVE_SNPRINTF
351 int snprintf __P((char *, size_t, const char *, ...))
354 #ifndef HAVE_VSNPRINTF
355 int vsnprintf __P((char *, size_t, const char *, va_list))
358 #ifndef HAVE_ASPRINTF
359 int asprintf __P((char **, const char *, ...))
362 #ifndef HAVE_VASPRINTF
363 int vasprintf __P((char **, const char *, va_list))
366 #ifndef HAVE_STRCASECMP
367 int strcasecmp __P((const char *, const char *));
370 size_t strlcat __P((char *, const char *, size_t));
373 size_t strlcpy __P((char *, const char *, size_t));
376 void *memrchr __P((const void *, int, size_t));
378 #ifndef HAVE_MKSTEMPS
379 int mkstemps __P((char *, int));
381 #ifndef HAVE_NANOSLEEP
382 int nanosleep __P((const struct timespec *, struct timespec *));
385 int setenv __P((const char *, const char *, int));
387 #ifndef HAVE_UNSETENV
388 int unsetenv __P((const char *));
390 #ifndef HAVE_STRSIGNAL
391 char *strsignal __P((int));
394 pid_t setsid __P((void));
397 #endif /* _SUDO_MISSING_H */