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