man pages get built in the build- directories
[debian/sudo] / compat.h
1 /*
2  * Copyright (c) 1996, 1998-2005, 2008
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  * $Sudo: compat.h,v 1.90 2008/11/09 14:13:12 millert Exp $
22  */
23
24 #ifndef _SUDO_COMPAT_H
25 #define _SUDO_COMPAT_H
26
27 /*
28  * Macros that may be missing on some Operating Systems
29  */
30
31 /* Deal with ANSI stuff reasonably.  */
32 #ifndef  __P
33 # if defined (__cplusplus) || defined (__STDC__)
34 #  define __P(args)             args
35 # else
36 #  define __P(args)             ()
37 # endif
38 #endif /* __P */
39
40 /* Define away __attribute__ for non-gcc or old gcc */
41 #if !defined(__GNUC__) || __GNUC__ < 2 || __GNUC__ == 2 && __GNUC_MINOR__ < 5
42 # define __attribute__(x)
43 #endif
44
45 /* For silencing gcc warnings about rcsids */
46 #ifndef __unused
47 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ > 7)
48 #  define __unused      __attribute__((__unused__))
49 # else
50 #  define __unused
51 # endif
52 #endif
53
54 /* For catching format string mismatches */
55 #ifndef __printflike
56 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7)
57 #  define __printflike(f, v)    __attribute__((__format__ (__printf__, f, v)))
58 # else
59 #  define __printflike(f, v)
60 # endif
61 #endif
62
63 /*
64  * Some systems lack full limit definitions.
65  */
66 #ifndef OPEN_MAX
67 # define OPEN_MAX       256
68 #endif
69
70 #ifndef INT_MAX
71 # define INT_MAX        0x7fffffff
72 #endif
73
74 #ifndef PATH_MAX
75 # ifdef MAXPATHLEN
76 #  define PATH_MAX              MAXPATHLEN
77 # else
78 #  ifdef _POSIX_PATH_MAX
79 #   define PATH_MAX             _POSIX_PATH_MAX
80 #  else
81 #   define PATH_MAX             1024
82 #  endif
83 # endif
84 #endif
85
86 #ifndef MAXHOSTNAMELEN
87 # define MAXHOSTNAMELEN         64
88 #endif
89
90 /*
91  * Posix versions for those without...
92  */
93 #ifndef _S_IFMT
94 # define _S_IFMT                S_IFMT
95 #endif /* _S_IFMT */
96 #ifndef _S_IFREG
97 # define _S_IFREG               S_IFREG
98 #endif /* _S_IFREG */
99 #ifndef _S_IFDIR
100 # define _S_IFDIR               S_IFDIR
101 #endif /* _S_IFDIR */
102 #ifndef _S_IFLNK
103 # define _S_IFLNK               S_IFLNK
104 #endif /* _S_IFLNK */
105 #ifndef S_ISREG
106 # define S_ISREG(m)             (((m) & _S_IFMT) == _S_IFREG)
107 #endif /* S_ISREG */
108 #ifndef S_ISDIR
109 # define S_ISDIR(m)             (((m) & _S_IFMT) == _S_IFDIR)
110 #endif /* S_ISDIR */
111
112 /*
113  * Some OS's may not have this.
114  */
115 #ifndef S_IRWXU
116 # define S_IRWXU                0000700         /* rwx for owner */
117 #endif /* S_IRWXU */
118
119 /*
120  * These should be defined in <unistd.h> but not everyone has them.
121  */
122 #ifndef STDIN_FILENO
123 # define        STDIN_FILENO    0
124 #endif
125 #ifndef STDOUT_FILENO
126 # define        STDOUT_FILENO   1
127 #endif
128 #ifndef STDERR_FILENO
129 # define        STDERR_FILENO   2
130 #endif
131
132 /*
133  * These should be defined in <unistd.h> but not everyone has them.
134  */
135 #ifndef SEEK_SET
136 # define        SEEK_SET        0
137 #endif
138 #ifndef SEEK_CUR
139 # define        SEEK_CUR        1
140 #endif
141 #ifndef SEEK_END
142 # define        SEEK_END        2
143 #endif
144
145 /*
146  * BSD defines these in <sys/param.h> but others may not.
147  */
148 #ifndef MIN
149 # define MIN(a,b) (((a)<(b))?(a):(b))
150 #endif
151 #ifndef MAX
152 # define MAX(a,b) (((a)>(b))?(a):(b))
153 #endif
154
155 /*
156  * Simple isblank() macro and function for systems without it.
157  */
158 #ifndef HAVE_ISBLANK
159 int isblank __P((int));
160 # define isblank(_x)    ((_x) == ' ' || (_x) == '\t')
161 #endif
162
163 /*
164  * Old BSD systems lack strchr(), strrchr(), memset() and memcpy()
165  */
166 #if !defined(HAVE_STRCHR) && !defined(strchr)
167 # define strchr(_s, _c) index(_s, _c)
168 #endif
169 #if !defined(HAVE_STRRCHR) && !defined(strrchr)
170 # define strrchr(_s, _c)        rindex(_s, _c)
171 #endif
172 #if !defined(HAVE_MEMCPY) && !defined(memcpy)
173 # define memcpy(_d, _s, _n)     (bcopy(_s, _d, _n))
174 #endif
175 #if !defined(HAVE_MEMSET) && !defined(memset)
176 # define memset(_s, _x, _n)     (bzero(_s, _n))
177 #endif
178
179 /*
180  * NCR's SVr4 has _innetgr(3) instead of innetgr(3) for some reason.
181  */
182 #ifdef HAVE__INNETGR
183 # define innetgr(n, h, u, d)    (_innetgr(n, h, u, d))
184 # define HAVE_INNETGR 1
185 #endif /* HAVE__INNETGR */
186
187 /*
188  * On POSIX systems, O_NOCTTY is the default so some OS's may lack this define.
189  */
190 #ifndef O_NOCTTY
191 # define O_NOCTTY       0
192 #endif /* O_NOCTTY */
193
194 /*
195  * Emulate POSIX signals via sigvec(2)
196  */
197 #ifndef HAVE_SIGACTION
198 # define SA_ONSTACK     SV_ONSTACK
199 # define SA_RESTART     SV_INTERRUPT            /* opposite effect */
200 # define SA_RESETHAND   SV_RESETHAND
201 # define sa_handler     sv_handler
202 # define sa_mask        sv_mask
203 # define sa_flags       sv_flags
204 typedef struct sigvec sigaction_t;
205 typedef int sigset_t;
206 int sigaction __P((int sig, const sigaction_t *act, sigaction_t *oact));
207 int sigemptyset __P((sigset_t *));
208 int sigfillset __P((sigset_t *));
209 int sigaddset __P((sigset_t *, int));
210 int sigdelset __P((sigset_t *, int));
211 int sigismember __P((sigset_t *, int));
212 int sigprocmask __P((int, const sigset_t *, sigset_t *));
213 #endif
214
215 /*
216  * Extra sugar for POSIX signals to deal with the above emulation
217  * as well as the fact that SunOS has a SA_INTERRUPT flag.
218  */
219 #ifdef HAVE_SIGACTION
220 # ifndef HAVE_SIGACTION_T
221 typedef struct sigaction sigaction_t;
222 # endif
223 # ifndef SA_INTERRUPT
224 #  define SA_INTERRUPT  0
225 # endif
226 # ifndef SA_RESTART
227 #  define SA_RESTART    0
228 # endif
229 #endif
230
231 /*
232  * If dirfd() does not exists, hopefully dd_fd does.
233  */
234 #if !defined(HAVE_DIRFD) && defined(HAVE_DD_FD)
235 # define dirfd(_d)      ((_d)->dd_fd)
236 # define HAVE_DIRFD
237 #endif
238
239 /*
240  * Define futimes() in terms of futimesat() if needed.
241  */
242 #if !defined(HAVE_FUTIMES) && defined(HAVE_FUTIMESAT)
243 # define futimes(_f, _tv)       futimesat(_f, NULL, _tv)
244 # define HAVE_FUTIMES
245 #endif
246
247 /*
248  * If we lack getprogname(), emulate with __progname if possible.
249  * Otherwise, add a prototype for use with our own getprogname.c.
250  */
251 #ifndef HAVE_GETPROGNAME
252 # ifdef HAVE___PROGNAME
253 extern const char *__progname;
254 #  define getprogname()          (__progname)
255 # else
256 const char *getprogname __P((void));
257 #endif /* HAVE___PROGNAME */
258 #endif /* !HAVE_GETPROGNAME */
259
260 #ifndef timespecclear
261 # define timespecclear(ts)      (ts)->tv_sec = (ts)->tv_nsec = 0
262 #endif
263 #ifndef timespecisset
264 # define timespecisset(ts)      ((ts)->tv_sec || (ts)->tv_nsec)
265 #endif
266 #ifndef timespecsub
267 # define timespecsub(minuend, subrahend, difference)                           \
268     do {                                                                       \
269             (difference)->tv_sec = (minuend)->tv_sec - (subrahend)->tv_sec;    \
270             (difference)->tv_nsec = (minuend)->tv_nsec - (subrahend)->tv_nsec; \
271             if ((difference)->tv_nsec < 0) {                                   \
272                     (difference)->tv_nsec += 1000000000L;                      \
273                     (difference)->tv_sec--;                                    \
274             }                                                                  \
275     } while (0)
276 #endif
277
278 #endif /* _SUDO_COMPAT_H */