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