Imported Upstream version 1.7.6p1
[debian/sudo] / missing.h
1 /*
2  * Copyright (c) 1996, 1998-2005, 2008-2010
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
4  *
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.
8  *
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.
16  *
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.
20  */
21
22 #ifndef _SUDO_MISSING_H
23 #define _SUDO_MISSING_H
24
25 #include <stdio.h>
26 #ifdef __STDC__
27 # include <stdarg.h>
28 #else
29 # include <varargs.h>
30 #endif
31
32 /*
33  * Macros that may be missing on some Operating Systems
34  */
35
36 /* Deal with ANSI stuff reasonably.  */
37 #ifndef  __P
38 # if defined (__cplusplus) || defined (__STDC__)
39 #  define __P(args)             args
40 # else
41 #  define __P(args)             ()
42 # endif
43 #endif /* __P */
44
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)
48 #endif
49
50 /* For silencing gcc warnings about rcsids */
51 #ifndef __unused
52 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ > 7)
53 #  define __unused      __attribute__((__unused__))
54 # else
55 #  define __unused
56 # endif
57 #endif
58
59 /* For catching format string mismatches */
60 #ifndef __printflike
61 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7)
62 #  define __printflike(f, v)    __attribute__((__format__ (__printf__, f, v)))
63 # else
64 #  define __printflike(f, v)
65 # endif
66 #endif
67
68 /*
69  * Some systems lack full limit definitions.
70  */
71 #ifndef OPEN_MAX
72 # define OPEN_MAX       256
73 #endif
74
75 #ifndef INT_MAX
76 # define INT_MAX        0x7fffffff
77 #endif
78
79 #ifndef PATH_MAX
80 # ifdef MAXPATHLEN
81 #  define PATH_MAX              MAXPATHLEN
82 # else
83 #  ifdef _POSIX_PATH_MAX
84 #   define PATH_MAX             _POSIX_PATH_MAX
85 #  else
86 #   define PATH_MAX             1024
87 #  endif
88 # endif
89 #endif
90
91 #ifndef MAXHOSTNAMELEN
92 # define MAXHOSTNAMELEN         64
93 #endif
94
95 /*
96  * Posix versions for those without...
97  */
98 #ifndef _S_IFMT
99 # define _S_IFMT                S_IFMT
100 #endif /* _S_IFMT */
101 #ifndef _S_IFREG
102 # define _S_IFREG               S_IFREG
103 #endif /* _S_IFREG */
104 #ifndef _S_IFDIR
105 # define _S_IFDIR               S_IFDIR
106 #endif /* _S_IFDIR */
107 #ifndef _S_IFLNK
108 # define _S_IFLNK               S_IFLNK
109 #endif /* _S_IFLNK */
110 #ifndef S_ISREG
111 # define S_ISREG(m)             (((m) & _S_IFMT) == _S_IFREG)
112 #endif /* S_ISREG */
113 #ifndef S_ISDIR
114 # define S_ISDIR(m)             (((m) & _S_IFMT) == _S_IFDIR)
115 #endif /* S_ISDIR */
116
117 /*
118  * Some OS's may not have this.
119  */
120 #ifndef S_IRWXU
121 # define S_IRWXU                0000700         /* rwx for owner */
122 #endif /* S_IRWXU */
123
124 /*
125  * These should be defined in <unistd.h> but not everyone has them.
126  */
127 #ifndef STDIN_FILENO
128 # define        STDIN_FILENO    0
129 #endif
130 #ifndef STDOUT_FILENO
131 # define        STDOUT_FILENO   1
132 #endif
133 #ifndef STDERR_FILENO
134 # define        STDERR_FILENO   2
135 #endif
136
137 /*
138  * These should be defined in <unistd.h> but not everyone has them.
139  */
140 #ifndef SEEK_SET
141 # define        SEEK_SET        0
142 #endif
143 #ifndef SEEK_CUR
144 # define        SEEK_CUR        1
145 #endif
146 #ifndef SEEK_END
147 # define        SEEK_END        2
148 #endif
149
150 /*
151  * BSD defines these in <sys/param.h> but others may not.
152  */
153 #ifndef MIN
154 # define MIN(a,b) (((a)<(b))?(a):(b))
155 #endif
156 #ifndef MAX
157 # define MAX(a,b) (((a)>(b))?(a):(b))
158 #endif
159
160 /*
161  * Simple isblank() macro and function for systems without it.
162  */
163 #ifndef HAVE_ISBLANK
164 int isblank __P((int));
165 # define isblank(_x)    ((_x) == ' ' || (_x) == '\t')
166 #endif
167
168 /*
169  * Old BSD systems lack strchr(), strrchr(), memset() and memcpy()
170  */
171 #if !defined(HAVE_STRCHR) && !defined(strchr)
172 # define strchr(_s, _c) index(_s, _c)
173 #endif
174 #if !defined(HAVE_STRRCHR) && !defined(strrchr)
175 # define strrchr(_s, _c)        rindex(_s, _c)
176 #endif
177 #if !defined(HAVE_MEMCPY) && !defined(memcpy)
178 # define memcpy(_d, _s, _n)     (bcopy(_s, _d, _n))
179 #endif
180 #if !defined(HAVE_MEMSET) && !defined(memset)
181 # define memset(_s, _x, _n)     (bzero(_s, _n))
182 #endif
183
184 /*
185  * NCR's SVr4 has _innetgr(3) instead of innetgr(3) for some reason.
186  */
187 #ifdef HAVE__INNETGR
188 # define innetgr(n, h, u, d)    (_innetgr(n, h, u, d))
189 # define HAVE_INNETGR 1
190 #endif /* HAVE__INNETGR */
191
192 /*
193  * On POSIX systems, O_NOCTTY is the default so some OS's may lack this define.
194  */
195 #ifndef O_NOCTTY
196 # define O_NOCTTY       0
197 #endif /* O_NOCTTY */
198
199 /*
200  * Emulate POSIX signals via sigvec(2)
201  */
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 *));
218 #endif
219
220 /*
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.
223  */
224 #ifdef HAVE_SIGACTION
225 # ifndef HAVE_SIGACTION_T
226 typedef struct sigaction sigaction_t;
227 # endif
228 # ifndef SA_INTERRUPT
229 #  define SA_INTERRUPT  0
230 # endif
231 # ifndef SA_RESTART
232 #  define SA_RESTART    0
233 # endif
234 #endif
235
236 /*
237  * If dirfd() does not exists, hopefully dd_fd does.
238  */
239 #if !defined(HAVE_DIRFD) && defined(HAVE_DD_FD)
240 # define dirfd(_d)      ((_d)->dd_fd)
241 # define HAVE_DIRFD
242 #endif
243
244 /*
245  * Define futimes() in terms of futimesat() if needed.
246  */
247 #if !defined(HAVE_FUTIMES) && defined(HAVE_FUTIMESAT)
248 # define futimes(_f, _tv)       futimesat(_f, NULL, _tv)
249 # define HAVE_FUTIMES
250 #endif
251
252 #if !defined(HAVE_KILLPG) && !defined(killpg)
253 # define killpg(s)      kill(-(s))
254 #endif
255
256 /*
257  * If we lack getprogname(), emulate with __progname if possible.
258  * Otherwise, add a prototype for use with our own getprogname.c.
259  */
260 #ifndef HAVE_GETPROGNAME
261 # ifdef HAVE___PROGNAME
262 extern const char *__progname;
263 #  define getprogname()          (__progname)
264 # else
265 const char *getprogname __P((void));
266 #endif /* HAVE___PROGNAME */
267 #endif /* !HAVE_GETPROGNAME */
268
269 #ifndef timevalclear
270 # define timevalclear(tv)       ((tv)->tv_sec = (tv)->tv_usec = 0)
271 #endif
272 #ifndef timevalisset
273 # define timevalisset(tv)       ((tv)->tv_sec || (tv)->tv_usec)
274 #endif
275 #ifndef timevalcmp
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))
280 #endif
281 #ifndef timevaladd
282 # define timevaladd(tv1, tv2)                                                  \
283     do {                                                                       \
284         (tv1)->tv_sec += (tv2)->tv_sec;                                        \
285         (tv1)->tv_usec += (tv2)->tv_usec;                                      \
286         if ((tv1)->tv_usec >= 1000000) {                                       \
287             (tv1)->tv_sec++;                                                   \
288             (tv1)->tv_usec -= 1000000;                                         \
289         }                                                                      \
290     } while (0)
291 #endif
292 #ifndef timevalsub
293 # define timevalsub(tv1, tv2)                                                  \
294     do {                                                                       \
295         (tv1)->tv_sec -= (tv2)->tv_sec;                                        \
296         (tv1)->tv_usec -= (tv2)->tv_usec;                                      \
297         if ((tv1)->tv_usec < 0) {                                              \
298             (tv1)->tv_sec--;                                                   \
299             (tv1)->tv_usec += 1000000;                                         \
300         }                                                                      \
301     } while (0)
302 #endif
303
304 /* Not all systems define NSIG in signal.h */
305 #if !defined(NSIG)
306 # if defined(_NSIG)
307 #  define NSIG _NSIG
308 # elif defined(__NSIG)
309 #  define NSIG __NSIG
310 # else
311 #  define NSIG 64
312 # endif
313 #endif
314
315 #ifndef WCOREDUMP
316 # define WCOREDUMP(x)   ((x) & 0x80)
317 #endif
318
319 /*
320  * HP-UX does not declare innetgr() or getdomainname().
321  * Solaris does not declare getdomainname().
322  */
323 #if defined(__hpux)
324 int innetgr __P((const char *, const char *, const char *, const char *));
325 #endif
326 #if defined(__hpux) || defined(__sun)
327 int getdomainname __P((char *, size_t));
328 #endif
329
330 /* Functions "missing" from libc. */
331
332 struct timeval;
333 struct timespec;
334
335 #ifndef HAVE_CLOSEFROM
336 void closefrom          __P((int));
337 #endif
338 #ifndef HAVE_GETCWD
339 char *getcwd            __P((char *, size_t size));
340 #endif
341 #ifndef HAVE_GETLINE
342 ssize_t getline         __P((char **, size_t *, FILE *));
343 #endif
344 #ifndef HAVE_UTIMES
345 int utimes              __P((const char *, const struct timeval *));
346 #endif
347 #ifdef HAVE_FUTIME
348 int futimes             __P((int, const struct timeval *));
349 #endif
350 #ifndef HAVE_SNPRINTF
351 int snprintf            __P((char *, size_t, const char *, ...))
352                             __printflike(3, 4);
353 #endif
354 #ifndef HAVE_VSNPRINTF
355 int vsnprintf           __P((char *, size_t, const char *, va_list))
356                             __printflike(3, 0);
357 #endif
358 #ifndef HAVE_ASPRINTF
359 int asprintf            __P((char **, const char *, ...))
360                             __printflike(2, 3);
361 #endif
362 #ifndef HAVE_VASPRINTF
363 int vasprintf           __P((char **, const char *, va_list))
364                             __printflike(2, 0);
365 #endif
366 #ifndef HAVE_STRCASECMP
367 int strcasecmp          __P((const char *, const char *));
368 #endif
369 #ifndef HAVE_STRLCAT
370 size_t strlcat          __P((char *, const char *, size_t));
371 #endif
372 #ifndef HAVE_STRLCPY
373 size_t strlcpy          __P((char *, const char *, size_t));
374 #endif
375 #ifndef HAVE_MEMRCHR
376 void *memrchr           __P((const void *, int, size_t));
377 #endif
378 #ifndef HAVE_MKSTEMPS
379 int mkstemps            __P((char *, int));
380 #endif
381 #ifndef HAVE_NANOSLEEP
382 int nanosleep           __P((const struct timespec *, struct timespec *));
383 #endif
384 #ifndef HAVE_SETENV
385 int setenv              __P((const char *, const char *, int));
386 #endif
387 #ifndef HAVE_UNSETENV
388 int unsetenv            __P((const char *));
389 #endif
390 #ifndef HAVE_STRSIGNAL
391 char *strsignal         __P((int));
392 #endif
393 #ifndef HAVE_SETSID
394 pid_t setsid            __P((void));
395 #endif
396
397 #endif /* _SUDO_MISSING_H */