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