2 * Copyright (c) 1996, 1998-2005, 2008, 2009-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
29 * Macros and functions that may be missing on some operating systems.
32 /* Define away __attribute__ for non-gcc or old gcc */
33 #if !defined(__GNUC__) || __GNUC__ < 2 || __GNUC__ == 2 && __GNUC_MINOR__ < 5
34 # define __attribute__(x)
37 /* For catching format string mismatches */
39 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7)
40 # define __printflike(f, v) __attribute__((__format__ (__printf__, f, v)))
42 # define __printflike(f, v)
47 * Some systems lack full limit definitions.
54 # define INT_MAX 0x7fffffff
59 # define PATH_MAX MAXPATHLEN
61 # ifdef _POSIX_PATH_MAX
62 # define PATH_MAX _POSIX_PATH_MAX
64 # define PATH_MAX 1024
69 #ifndef MAXHOSTNAMELEN
70 # define MAXHOSTNAMELEN 64
74 * Posix versions for those without...
77 # define _S_IFMT S_IFMT
80 # define _S_IFREG S_IFREG
83 # define _S_IFDIR S_IFDIR
86 # define _S_IFLNK S_IFLNK
89 # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
92 # define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
96 * Some OS's may not have this.
99 # define S_IRWXU 0000700 /* rwx for owner */
103 * These should be defined in <unistd.h> but not everyone has them.
106 # define STDIN_FILENO 0
108 #ifndef STDOUT_FILENO
109 # define STDOUT_FILENO 1
111 #ifndef STDERR_FILENO
112 # define STDERR_FILENO 2
116 * BSD defines these in <sys/param.h> but others may not.
119 # define MIN(a,b) (((a)<(b))?(a):(b))
122 # define MAX(a,b) (((a)>(b))?(a):(b))
126 * Older systems may be missing stddef.h and/or offsetof macro
130 # define offsetof(type, field) __offsetof(type, field)
132 # define offsetof(type, field) ((size_t)(&((type *)0)->field))
137 * Simple isblank() macro and function for systems without it.
141 # define isblank(_x) ((_x) == ' ' || (_x) == '\t')
145 * NCR's SVr4 has _innetgr(3) instead of innetgr(3) for some reason.
148 # define innetgr(n, h, u, d) (_innetgr(n, h, u, d))
149 # define HAVE_INNETGR 1
150 #endif /* HAVE__INNETGR */
153 * On POSIX systems, O_NOCTTY is the default so some OS's may lack this define.
157 #endif /* O_NOCTTY */
160 * Add IRIX-like sigaction_t for those without it.
161 * SA_RESTART is not required by POSIX; SunOS has SA_INTERRUPT instead.
163 #ifndef HAVE_SIGACTION_T
164 typedef struct sigaction sigaction_t;
167 # define SA_INTERRUPT 0
170 # define SA_RESTART 0
174 * If dirfd() does not exists, hopefully dd_fd does.
176 #if !defined(HAVE_DIRFD) && defined(HAVE_DD_FD)
177 # define dirfd(_d) ((_d)->dd_fd)
182 * Define futimes() in terms of futimesat() if needed.
184 #if !defined(HAVE_FUTIMES) && defined(HAVE_FUTIMESAT)
185 # define futimes(_f, _tv) futimesat(_f, NULL, _tv)
186 # define HAVE_FUTIMES
189 #if !defined(HAVE_KILLPG) && !defined(killpg)
190 # define killpg(s) kill(-(s))
194 * If we lack getprogname(), emulate with __progname if possible.
195 * Otherwise, add a prototype for use with our own getprogname.c.
197 #ifndef HAVE_GETPROGNAME
198 # ifdef HAVE___PROGNAME
199 extern const char *__progname;
200 # define getprogname() (__progname)
202 const char *getprogname(void);
203 void setprogname(const char *);
204 #endif /* HAVE___PROGNAME */
205 #endif /* !HAVE_GETPROGNAME */
208 * Declare errno if errno.h doesn't do it for us.
210 #if defined(HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
212 #endif /* !HAVE_DECL_ERRNO */
215 # define timevalclear(tv) ((tv)->tv_sec = (tv)->tv_usec = 0)
218 # define timevalisset(tv) ((tv)->tv_sec || (tv)->tv_usec)
221 # define timevalcmp(tv1, tv2, op) \
222 (((tv1)->tv_sec == (tv2)->tv_sec) ? \
223 ((tv1)->tv_usec op (tv2)->tv_usec) : \
224 ((tv1)->tv_sec op (tv2)->tv_sec))
227 # define timevaladd(tv1, tv2) \
229 (tv1)->tv_sec += (tv2)->tv_sec; \
230 (tv1)->tv_usec += (tv2)->tv_usec; \
231 if ((tv1)->tv_usec >= 1000000) { \
233 (tv1)->tv_usec -= 1000000; \
238 # define timevalsub(tv1, tv2) \
240 (tv1)->tv_sec -= (tv2)->tv_sec; \
241 (tv1)->tv_usec -= (tv2)->tv_usec; \
242 if ((tv1)->tv_usec < 0) { \
244 (tv1)->tv_usec += 1000000; \
249 /* Not all systems define NSIG in signal.h */
253 # elif defined(__NSIG)
261 # define WCOREDUMP(x) ((x) & 0x80)
265 # if defined(HAVE_SETRESUID)
266 # define seteuid(u) setresuid(-1, (u), -1)
267 # define setegid(g) setresgid(-1, (g), -1)
268 # define HAVE_SETEUID 1
269 # elif defined(HAVE_SETREUID)
270 # define seteuid(u) setreuid(-1, (u))
271 # define setegid(g) setregid(-1, (g))
272 # define HAVE_SETEUID 1
274 #endif /* HAVE_SETEUID */
277 * HP-UX does not declare innetgr() or getdomainname().
278 * Solaris does not declare getdomainname().
281 int innetgr(const char *, const char *, const char *, const char *);
283 #if defined(__hpux) || defined(__sun)
284 int getdomainname(char *, size_t);
287 /* Functions "missing" from libc. */
292 #ifndef HAVE_CLOSEFROM
296 char *getcwd(char *, size_t size);
298 #ifndef HAVE_GETGROUPLIST
299 int getgrouplist(const char *, gid_t, gid_t *, int *);
302 ssize_t getline(char **, size_t *, FILE *);
305 int utimes(const char *, const struct timeval *);
308 int futimes(int, const struct timeval *);
310 #ifndef HAVE_SNPRINTF
311 int snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
313 #ifndef HAVE_VSNPRINTF
314 int vsnprintf(char *, size_t, const char *, va_list) __printflike(3, 0);
316 #ifndef HAVE_ASPRINTF
317 int asprintf(char **, const char *, ...) __printflike(2, 3);
319 #ifndef HAVE_VASPRINTF
320 int vasprintf(char **, const char *, va_list) __printflike(2, 0);
323 size_t strlcat(char *, const char *, size_t);
326 size_t strlcpy(char *, const char *, size_t);
329 void *memrchr(const void *, int, size_t);
332 char *mkdtemp(char *);
334 #ifndef HAVE_MKSTEMPS
335 int mkstemps(char *, int);
337 #ifndef HAVE_NANOSLEEP
338 int nanosleep(const struct timespec *, struct timespec *);
341 struct passwd *pw_dup(const struct passwd *);
344 int setenv(const char *, const char *, int);
346 #ifndef HAVE_UNSETENV
347 int unsetenv(const char *);
349 #ifndef HAVE_STRSIGNAL
350 char *strsignal(int);
353 #endif /* _SUDO_MISSING_H */