Imported Upstream version 1.6.6
[debian/sudo] / compat.h
1 /*
2  * Copyright (c) 1996, 1998-2002 Todd C. Miller <Todd.Miller@courtesan.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * 4. Products derived from this software may not be called "Sudo" nor
20  *    may "Sudo" appear in their names without specific prior written
21  *    permission from the author.
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
26  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Sudo: compat.h,v 1.63 2002/01/25 18:38:22 millert Exp $
35  */
36
37 #ifndef _SUDO_COMPAT_H
38 #define _SUDO_COMPAT_H
39
40 /*
41  * Macros that may be missing on some Operating Systems
42  */
43
44 /* Deal with ANSI stuff reasonably.  */
45 #ifndef  __P
46 # if defined (__cplusplus) || defined (__STDC__)
47 #  define __P(args)             args
48 # else
49 #  define __P(args)             ()
50 # endif
51 #endif /* __P */
52
53 /*
54  * Some systems (ie ISC V/386) do not define MAXPATHLEN even in param.h
55  */
56 #ifndef MAXPATHLEN
57 # define MAXPATHLEN             1024
58 #endif
59
60 /*
61  * Some systems do not define MAXHOSTNAMELEN.
62  */
63 #ifndef MAXHOSTNAMELEN
64 # define MAXHOSTNAMELEN         64
65 #endif
66
67 /*
68  * 4.2BSD lacks FD_* macros (we only use FD_SET and FD_ZERO)
69  */
70 #ifndef FD_SETSIZE
71 # define FD_SET(fd, fds)        ((fds) -> fds_bits[0] |= (1 << (fd)))
72 # define FD_ZERO(fds)           ((fds) -> fds_bits[0] = 0)
73 #endif /* !FD_SETSIZE */
74
75 /*
76  * Posix versions for those without...
77  */
78 #ifndef _S_IFMT
79 # define _S_IFMT                S_IFMT
80 #endif /* _S_IFMT */
81 #ifndef _S_IFREG
82 # define _S_IFREG               S_IFREG
83 #endif /* _S_IFREG */
84 #ifndef _S_IFDIR
85 # define _S_IFDIR               S_IFDIR
86 #endif /* _S_IFDIR */
87 #ifndef _S_IFLNK
88 # define _S_IFLNK               S_IFLNK
89 #endif /* _S_IFLNK */
90 #ifndef S_ISREG
91 # define S_ISREG(m)             (((m) & _S_IFMT) == _S_IFREG)
92 #endif /* S_ISREG */
93 #ifndef S_ISDIR
94 # define S_ISDIR(m)             (((m) & _S_IFMT) == _S_IFDIR)
95 #endif /* S_ISDIR */
96
97 /*
98  * Some OS's may not have this.
99  */
100 #ifndef S_IRWXU
101 # define S_IRWXU                0000700         /* rwx for owner */
102 #endif /* S_IRWXU */
103
104 /*
105  * In case this is not defined in <sys/types.h> or <sys/select.h>
106  */
107 #ifndef howmany
108 # define howmany(x, y)  (((x) + ((y) - 1)) / (y))
109 #endif
110
111 /*
112  * These should be defined in <unistd.h> but not everyone has them.
113  */
114 #ifndef STDIN_FILENO
115 # define        STDIN_FILENO    0
116 #endif
117 #ifndef STDOUT_FILENO
118 # define        STDOUT_FILENO   1
119 #endif
120 #ifndef STDERR_FILENO
121 # define        STDERR_FILENO   2
122 #endif
123
124 /*
125  * These should be defined in <unistd.h> but not everyone has them.
126  */
127 #ifndef SEEK_SET
128 # define        SEEK_SET        0
129 #endif
130 #ifndef SEEK_CUR
131 # define        SEEK_CUR        1
132 #endif
133 #ifndef SEEK_END
134 # define        SEEK_END        2
135 #endif
136
137 /*
138  * BSD defines these in <sys/param.h> but others may not.
139  */
140 #ifndef MIN
141 # define MIN(a,b) (((a)<(b))?(a):(b))
142 #endif
143 #ifndef MAX
144 # define MAX(a,b) (((a)>(b))?(a):(b))
145 #endif
146
147 /*
148  * Simple isblank() macro for systems without it.
149  */
150 #ifndef HAVE_ISBLANK
151 # define isblank(_x)    ((_x) == ' ' || (_x) == '\t')
152 #endif
153
154 /*
155  * Old BSD systems lack strchr(), strrchr(), memset() and memcpy()
156  */
157 #if !defined(HAVE_STRCHR) && !defined(strchr)
158 # define strchr(_s, _c) index(_s, _c)
159 #endif
160 #if !defined(HAVE_STRRCHR) && !defined(strrchr)
161 # define strrchr(_s, _c)        rindex(_s, _c)
162 #endif
163 #if !defined(HAVE_MEMCPY) && !defined(memcpy)
164 # define memcpy(_d, _s, _n)     (bcopy(_s, _d, _n))
165 #endif
166 #if !defined(HAVE_MEMSET) && !defined(memset)
167 # define memset(_s, _x, _n)     (bzero(_s, _n))
168 #endif
169
170 /*
171  * Emulate sete[ug]id() via setres[ug]id(2) or setre[ug]id(2)
172  */
173 #ifndef HAVE_SETEUID
174 # ifdef __hpux
175 #  define seteuid(_EUID)        (setresuid((uid_t) -1, _EUID, (uid_t) -1))
176 # else
177 #  define seteuid(_EUID)        (setreuid((uid_t) -1, _EUID))
178 # endif /* __hpux */
179 #endif /* HAVE_SETEUID */
180 #ifndef HAVE_SETEGID
181 # ifdef __hpux
182 #  define setegid(_EGID)        (setresgid((gid_t) -1, _EGID, (gid_t) -1))
183 # else
184 #  define setegid(_EGID)        (setregid((gid_t) -1, _EGID))
185 # endif /* __hpux */
186 #endif /* HAVE_SETEGID */
187
188 /*
189  * Emulate setreuid() for HP-UX via setresuid(2)
190  */
191 #if !defined(HAVE_SETREUID) && defined(__hpux)
192 # define setreuid(_RUID, _EUID) (setresuid(_RUID, _EUID, (uid_t) -1))
193 # define HAVE_SETREUID
194 #endif /* !HAVE_SETEUID && __hpux */
195
196 /*
197  * NCR's SVr4 has _innetgr(3) instead of innetgr(3) for some reason.
198  */
199 #ifdef HAVE__INNETGR
200 # define innetgr(n, h, u, d)    (_innetgr(n, h, u, d))
201 # define HAVE_INNETGR 1
202 #endif /* HAVE__INNETGR */
203
204 /*
205  * On POSIX systems, O_NOCTTY is the default so some OS's may lack this define.
206  */
207 #ifndef O_NOCTTY
208 # define O_NOCTTY       0
209 #endif /* O_NOCTTY */
210
211 /*
212  * Emulate POSIX signals via sigvec(2)
213  */
214 #ifndef HAVE_SIGACTION
215 # define SA_ONSTACK     SV_ONSTACK
216 # define SA_RESTART     SV_INTERRUPT            /* opposite effect */
217 # define SA_RESETHAND   SV_RESETHAND
218 # define sa_handler     sv_handler
219 # define sa_mask        sv_mask
220 # define sa_flags       sv_flags
221 typedef struct sigvec sigaction_t;
222 typedef int sigset_t;
223 int sigaction __P((int sig, const sigaction_t *act, sigaction_t *oact));
224 int sigemptyset __P((sigset_t *));
225 int sigfillset __P((sigset_t *));
226 int sigaddset __P((sigset_t *, int));
227 int sigdelset __P((sigset_t *, int));
228 int sigismember __P((sigset_t *, int));
229 int sigprocmask __P((int, const sigset_t *, sigset_t *));
230 #endif
231
232 /*
233  * Extra sugar for POSIX signals to deal with the above emulation
234  * as well as the fact that SunOS has a SA_INTERRUPT flag.
235  */
236 #ifdef HAVE_SIGACTION
237 # ifndef HAVE_SIGACTION_T
238 typedef struct sigaction sigaction_t;
239 # endif
240 # ifndef SA_INTERRUPT 
241 #  define SA_INTERRUPT  0
242 # endif
243 # ifndef SA_RESTART 
244 #  define SA_RESTART    0
245 # endif
246 #endif
247
248 /*
249  * HP-UX 9.x has RLIMIT_* but no RLIM_INFINITY.
250  * Using -1 works because we only check for RLIM_INFINITY and do not set it.
251  */
252 #ifndef RLIM_INFINITY
253 # define RLIM_INFINITY  (-1)
254 #endif
255
256 #endif /* _SUDO_COMPAT_H */