Imported Debian patch 1.6.9p11-2
[debian/sudo] / compat.h
1 /*
2  * Copyright (c) 1996, 1998-2005 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  * Sponsored in part by the Defense Advanced Research Projects
17  * Agency (DARPA) and Air Force Research Laboratory, Air Force
18  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
19  *
20  * $Sudo: compat.h,v 1.76.2.4 2007/06/12 01:28:41 millert Exp $
21  */
22
23 #ifndef _SUDO_COMPAT_H
24 #define _SUDO_COMPAT_H
25
26 /*
27  * Macros that may be missing on some Operating Systems
28  */
29
30 /* Deal with ANSI stuff reasonably.  */
31 #ifndef  __P
32 # if defined (__cplusplus) || defined (__STDC__)
33 #  define __P(args)             args
34 # else
35 #  define __P(args)             ()
36 # endif
37 #endif /* __P */
38
39 /* Define away __attribute__ for non-gcc or old gcc */
40 #if !defined(__GNUC__) || __GNUC__ < 2 || __GNUC__ == 2 && __GNUC_MINOR__ < 5
41 # define __attribute__(x)
42 #endif
43  
44 /* For silencing gcc warnings about rcsids */
45 #ifndef __unused
46 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ > 7)
47 #  define __unused      __attribute__((__unused__))
48 # else
49 #  define __unused
50 # endif
51 #endif
52
53 /* For catching format string mismatches */
54 #ifndef __printflike
55 # if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7)
56 #  define __printflike(f, v)    __attribute__((__format__ (__printf__, f, v)))
57 # else
58 #  define __printflike(f, v)
59 # endif
60 #endif
61
62 /*
63  * Some systems lack full limit definitions.
64  */
65 #ifndef OPEN_MAX
66 # define OPEN_MAX       256
67 #endif
68
69 #ifndef INT_MAX
70 # define INT_MAX        0x7fffffff
71 #endif
72
73 #ifndef PATH_MAX
74 # ifdef MAXPATHLEN
75 #  define PATH_MAX              MAXPATHLEN
76 # else
77 #  ifdef _POSIX_PATH_MAX
78 #   define PATH_MAX             _POSIX_PATH_MAX
79 #  else
80 #   define PATH_MAX             1024
81 #  endif
82 # endif
83 #endif
84
85 #ifndef MAXHOSTNAMELEN
86 # define MAXHOSTNAMELEN         64
87 #endif
88
89 /*
90  * Posix versions for those without...
91  */
92 #ifndef _S_IFMT
93 # define _S_IFMT                S_IFMT
94 #endif /* _S_IFMT */
95 #ifndef _S_IFREG
96 # define _S_IFREG               S_IFREG
97 #endif /* _S_IFREG */
98 #ifndef _S_IFDIR
99 # define _S_IFDIR               S_IFDIR
100 #endif /* _S_IFDIR */
101 #ifndef _S_IFLNK
102 # define _S_IFLNK               S_IFLNK
103 #endif /* _S_IFLNK */
104 #ifndef S_ISREG
105 # define S_ISREG(m)             (((m) & _S_IFMT) == _S_IFREG)
106 #endif /* S_ISREG */
107 #ifndef S_ISDIR
108 # define S_ISDIR(m)             (((m) & _S_IFMT) == _S_IFDIR)
109 #endif /* S_ISDIR */
110
111 /*
112  * Some OS's may not have this.
113  */
114 #ifndef S_IRWXU
115 # define S_IRWXU                0000700         /* rwx for owner */
116 #endif /* S_IRWXU */
117
118 /*
119  * These should be defined in <unistd.h> but not everyone has them.
120  */
121 #ifndef STDIN_FILENO
122 # define        STDIN_FILENO    0
123 #endif
124 #ifndef STDOUT_FILENO
125 # define        STDOUT_FILENO   1
126 #endif
127 #ifndef STDERR_FILENO
128 # define        STDERR_FILENO   2
129 #endif
130
131 /*
132  * These should be defined in <unistd.h> but not everyone has them.
133  */
134 #ifndef SEEK_SET
135 # define        SEEK_SET        0
136 #endif
137 #ifndef SEEK_CUR
138 # define        SEEK_CUR        1
139 #endif
140 #ifndef SEEK_END
141 # define        SEEK_END        2
142 #endif
143
144 /*
145  * BSD defines these in <sys/param.h> but others may not.
146  */
147 #ifndef MIN
148 # define MIN(a,b) (((a)<(b))?(a):(b))
149 #endif
150 #ifndef MAX
151 # define MAX(a,b) (((a)>(b))?(a):(b))
152 #endif
153
154 /*
155  * Simple isblank() macro for systems without it.
156  */
157 #ifndef HAVE_ISBLANK
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 */