Imported Upstream version 1.8.7
[debian/sudo] / include / missing.h
1 /*
2  * Copyright (c) 1996, 1998-2005, 2008, 2009-2013
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 #include <stdarg.h>
27
28 /*
29  * Macros and functions that may be missing on some operating systems.
30  */
31
32 #ifndef __GNUC_PREREQ__
33 # ifdef __GNUC__
34 #  define __GNUC_PREREQ__(ma, mi) \
35         ((__GNUC__ > (ma)) || (__GNUC__ == (ma) && __GNUC_MINOR__ >= (mi)))
36 # else
37 #  define __GNUC_PREREQ__(ma, mi)       0
38 # endif
39 #endif
40
41 /* Define away __attribute__ for non-gcc or old gcc */
42 #if !defined(__attribute__) && !__GNUC_PREREQ__(2, 5)
43 # define __attribute__(x)
44 #endif
45
46 /* For catching format string mismatches */
47 #ifndef __printflike
48 # if __GNUC_PREREQ__(2, 7)
49 #  define __printflike(f, v)    __attribute__((__format__ (__printf__, f, v)))
50 # else
51 #  define __printflike(f, v)
52 # endif
53 #endif
54
55 /* Hint to compiler that returned pointer is unique (malloc but not realloc). */
56 #ifndef __malloc_like
57 # if __GNUC_PREREQ__(2, 96)
58 #  define __malloc_like         __attribute__((__malloc__))
59 # else
60 #  define __malloc_like
61 # endif
62 #endif
63
64 #ifndef __dso_public
65 # ifdef HAVE_DSO_VISIBILITY
66 #  if defined(__GNUC__)
67 #   define __dso_public __attribute__((__visibility__("default")))
68 #   define __dso_hidden __attribute__((__visibility__("hidden")))
69 #  elif defined(__SUNPRO_C)
70 #   define __dso_public __global
71 #   define __dso_hidden __hidden
72 #  else
73 #   define __dso_public __declspec(dllexport)
74 #   define __dso_hidden
75 #  endif
76 # else
77 #  define __dso_public
78 #  define __dso_hidden
79 # endif
80 #endif
81
82 /*
83  * Some systems lack full limit definitions.
84  */
85 #ifndef OPEN_MAX
86 # define OPEN_MAX       256
87 #endif
88
89 #ifndef INT_MAX
90 # define INT_MAX        0x7fffffff
91 #endif
92
93 #ifndef PATH_MAX
94 # ifdef _POSIX_PATH_MAX
95 #  define PATH_MAX              _POSIX_PATH_MAX
96 # else
97 #  define PATH_MAX              256
98 # endif
99 #endif
100
101 #ifndef HOST_NAME_MAX
102 # ifdef _POSIX_HOST_NAME_MAX
103 #  define HOST_NAME_MAX         _POSIX_HOST_NAME_MAX
104 # else
105 #  define HOST_NAME_MAX         255
106 # endif
107 #endif
108
109 /*
110  * Posix versions for those without...
111  */
112 #ifndef _S_IFMT
113 # define _S_IFMT                S_IFMT
114 #endif /* _S_IFMT */
115 #ifndef _S_IFREG
116 # define _S_IFREG               S_IFREG
117 #endif /* _S_IFREG */
118 #ifndef _S_IFDIR
119 # define _S_IFDIR               S_IFDIR
120 #endif /* _S_IFDIR */
121 #ifndef _S_IFLNK
122 # define _S_IFLNK               S_IFLNK
123 #endif /* _S_IFLNK */
124 #ifndef S_ISREG
125 # define S_ISREG(m)             (((m) & _S_IFMT) == _S_IFREG)
126 #endif /* S_ISREG */
127 #ifndef S_ISDIR
128 # define S_ISDIR(m)             (((m) & _S_IFMT) == _S_IFDIR)
129 #endif /* S_ISDIR */
130
131 /*
132  * Some OS's may not have this.
133  */
134 #ifndef S_IRWXU
135 # define S_IRWXU                0000700         /* rwx for owner */
136 #endif /* S_IRWXU */
137
138 /*
139  * These should be defined in <unistd.h> but not everyone has them.
140  */
141 #ifndef STDIN_FILENO
142 # define        STDIN_FILENO    0
143 #endif
144 #ifndef STDOUT_FILENO
145 # define        STDOUT_FILENO   1
146 #endif
147 #ifndef STDERR_FILENO
148 # define        STDERR_FILENO   2
149 #endif
150
151 /*
152  * BSD defines these in <sys/param.h> but we don't include that anymore.
153  */
154 #ifndef MIN
155 # define MIN(a,b) (((a)<(b))?(a):(b))
156 #endif
157 #ifndef MAX
158 # define MAX(a,b) (((a)>(b))?(a):(b))
159 #endif
160
161 /* Macros to set/clear/test flags. */
162 #undef SET
163 #define SET(t, f)       ((t) |= (f))
164 #undef CLR
165 #define CLR(t, f)       ((t) &= ~(f))
166 #undef ISSET
167 #define ISSET(t, f)     ((t) & (f))
168
169 /*
170  * Some systems define this in <sys/param.h> but we don't include that anymore.
171  */
172 #ifndef howmany
173 # define howmany(x, y)  (((x) + ((y) - 1)) / (y))
174 #endif
175
176 /*
177  * Older systems may be missing stddef.h and/or offsetof macro
178  */
179 #ifndef offsetof
180 # ifdef __offsetof
181 #  define offsetof(type, field) __offsetof(type, field)
182 # else
183 #  define offsetof(type, field) ((size_t)(&((type *)0)->field))
184 # endif
185 #endif
186
187 /*
188  * Simple isblank() macro and function for systems without it.
189  */
190 #ifndef HAVE_ISBLANK
191 int isblank(int);
192 # define isblank(_x)    ((_x) == ' ' || (_x) == '\t')
193 #endif
194
195 /*
196  * NCR's SVr4 has _innetgr(3) instead of innetgr(3) for some reason.
197  */
198 #ifdef HAVE__INNETGR
199 # define innetgr(n, h, u, d)    (_innetgr(n, h, u, d))
200 # define HAVE_INNETGR 1
201 #endif /* HAVE__INNETGR */
202
203 /*
204  * On POSIX systems, O_NOCTTY is the default so some OS's may lack this define.
205  */
206 #ifndef O_NOCTTY
207 # define O_NOCTTY       0
208 #endif /* O_NOCTTY */
209
210 /*
211  * Add IRIX-like sigaction_t for those without it.
212  * SA_RESTART is not required by POSIX; SunOS has SA_INTERRUPT instead.
213  */
214 #ifndef HAVE_SIGACTION_T
215 typedef struct sigaction sigaction_t;
216 #endif
217 #ifndef SA_INTERRUPT
218 # define SA_INTERRUPT   0
219 #endif
220 #ifndef SA_RESTART
221 # define SA_RESTART     0
222 #endif
223
224 /*
225  * If dirfd() does not exists, hopefully dd_fd does.
226  */
227 #if !defined(HAVE_DIRFD) && defined(HAVE_DD_FD)
228 # define dirfd(_d)      ((_d)->dd_fd)
229 # define HAVE_DIRFD
230 #endif
231
232 /*
233  * Define futimes() in terms of futimesat() if needed.
234  */
235 #if !defined(HAVE_FUTIMES) && defined(HAVE_FUTIMESAT)
236 # define futimes(_f, _tv)       futimesat(_f, NULL, _tv)
237 # define HAVE_FUTIMES
238 #endif
239
240 #if !defined(HAVE_KILLPG) && !defined(killpg)
241 # define killpg(s)      kill(-(s))
242 #endif
243
244 /*
245  * If we lack getprogname(), emulate with __progname if possible.
246  * Otherwise, add a prototype for use with our own getprogname.c.
247  */
248 #ifndef HAVE_GETPROGNAME
249 # ifdef HAVE___PROGNAME
250 extern const char *__progname;
251 #  define getprogname()         (__progname)
252 # else
253 const char *getprogname(void);
254 void setprogname(const char *);
255 # endif /* HAVE___PROGNAME */
256 #endif /* !HAVE_GETPROGNAME */
257
258 /*
259  * Declare errno if errno.h doesn't do it for us.
260  */
261 #if defined(HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
262 extern int errno;
263 #endif /* !HAVE_DECL_ERRNO */
264
265 #ifndef timevalclear
266 # define timevalclear(tv)       ((tv)->tv_sec = (tv)->tv_usec = 0)
267 #endif
268 #ifndef timevalisset
269 # define timevalisset(tv)       ((tv)->tv_sec || (tv)->tv_usec)
270 #endif
271 #ifndef timevalcmp
272 # define timevalcmp(tv1, tv2, op)                                              \
273     (((tv1)->tv_sec == (tv2)->tv_sec) ?                                        \
274         ((tv1)->tv_usec op (tv2)->tv_usec) :                                   \
275         ((tv1)->tv_sec op (tv2)->tv_sec))
276 #endif
277 #ifndef timevaladd
278 # define timevaladd(tv1, tv2)                                                  \
279     do {                                                                       \
280         (tv1)->tv_sec += (tv2)->tv_sec;                                        \
281         (tv1)->tv_usec += (tv2)->tv_usec;                                      \
282         if ((tv1)->tv_usec >= 1000000) {                                       \
283             (tv1)->tv_sec++;                                                   \
284             (tv1)->tv_usec -= 1000000;                                         \
285         }                                                                      \
286     } while (0)
287 #endif
288 #ifndef timevalsub
289 # define timevalsub(tv1, tv2)                                                  \
290     do {                                                                       \
291         (tv1)->tv_sec -= (tv2)->tv_sec;                                        \
292         (tv1)->tv_usec -= (tv2)->tv_usec;                                      \
293         if ((tv1)->tv_usec < 0) {                                              \
294             (tv1)->tv_sec--;                                                   \
295             (tv1)->tv_usec += 1000000;                                         \
296         }                                                                      \
297     } while (0)
298 #endif
299
300 /* Not all systems define NSIG in signal.h */
301 #if !defined(NSIG)
302 # if defined(_NSIG)
303 #  define NSIG _NSIG
304 # elif defined(__NSIG)
305 #  define NSIG __NSIG
306 # else
307 #  define NSIG 64
308 # endif
309 #endif
310
311 /* For sig2str() */
312 #ifndef SIG2STR_MAX
313 # define SIG2STR_MAX 32
314 #endif
315
316 #ifndef WCOREDUMP
317 # define WCOREDUMP(x)   ((x) & 0x80)
318 #endif
319
320 #ifndef HAVE_SETEUID
321 #  if defined(HAVE_SETRESUID)
322 #    define seteuid(u)  setresuid(-1, (u), -1)
323 #    define setegid(g)  setresgid(-1, (g), -1)
324 #    define HAVE_SETEUID 1
325 #  elif defined(HAVE_SETREUID)
326 #    define seteuid(u)  setreuid(-1, (u))
327 #    define setegid(g)  setregid(-1, (g))
328 #    define HAVE_SETEUID 1
329 #  endif
330 #endif /* HAVE_SETEUID */
331
332 /*
333  * HP-UX does not declare innetgr() or getdomainname().
334  * Solaris does not declare getdomainname().
335  */
336 #if defined(__hpux)
337 int innetgr(const char *, const char *, const char *, const char *);
338 #endif
339 #if defined(__hpux) || defined(__sun)
340 int getdomainname(char *, size_t);
341 #endif
342
343 /* Functions "missing" from libc. */
344
345 struct timeval;
346 struct timespec;
347
348 #ifndef HAVE_CLOSEFROM
349 void closefrom(int);
350 #endif
351 #ifndef HAVE_GETCWD
352 char *getcwd(char *, size_t size);
353 #endif
354 #ifndef HAVE_GETGROUPLIST
355 int getgrouplist(const char *, gid_t, gid_t *, int *);
356 #endif
357 #ifndef HAVE_GETLINE
358 ssize_t getline(char **, size_t *, FILE *);
359 #endif
360 #ifndef HAVE_UTIMES
361 int utimes(const char *, const struct timeval *);
362 #endif
363 #ifdef HAVE_FUTIME
364 int futimes(int, const struct timeval *);
365 #endif
366 #ifndef HAVE_SNPRINTF
367 int snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
368 #endif
369 #ifndef HAVE_VSNPRINTF
370 int vsnprintf(char *, size_t, const char *, va_list) __printflike(3, 0);
371 #endif
372 #ifndef HAVE_ASPRINTF
373 int asprintf(char **, const char *, ...) __printflike(2, 3);
374 #endif
375 #ifndef HAVE_VASPRINTF
376 int vasprintf(char **, const char *, va_list) __printflike(2, 0);
377 #endif
378 #ifndef HAVE_STRLCAT
379 size_t strlcat(char *, const char *, size_t);
380 #endif
381 #ifndef HAVE_STRLCPY
382 size_t strlcpy(char *, const char *, size_t);
383 #endif
384 #ifndef HAVE_MEMRCHR
385 void *memrchr(const void *, int, size_t);
386 #endif
387 #ifndef HAVE_MKDTEMP
388 char *mkdtemp(char *);
389 #endif
390 #ifndef HAVE_MKSTEMPS
391 int mkstemps(char *, int);
392 #endif
393 #ifndef HAVE_NANOSLEEP
394 int nanosleep(const struct timespec *, struct timespec *);
395 #endif
396 #ifndef HAVE_PW_DUP
397 struct passwd *pw_dup(const struct passwd *);
398 #endif
399 #ifndef HAVE_SETENV
400 int setenv(const char *, const char *, int);
401 #endif
402 #ifndef HAVE_UNSETENV
403 int unsetenv(const char *);
404 #endif
405 #ifndef HAVE_STRSIGNAL
406 char *strsignal(int);
407 #endif
408 #ifndef HAVE_SIG2STR
409 int sig2str(int, char *);
410 #endif
411
412 #endif /* _SUDO_MISSING_H */