Merge branch 'master' into cmd-alias
[screen-lua.git] / src / os.h
blob5bf417d64f1b5615be60123a0fb16135180bdc87
1 /* Copyright (c) 2008
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Micah Cowan (micah@cowan.name)
5 * Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
6 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
7 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
8 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
9 * Copyright (c) 1987 Oliver Laumann
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3, or (at your option)
14 * any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program (see the file COPYING); if not, see
23 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 ****************************************************************
27 * $Id$ FAU
30 #include <stdio.h>
31 #include <errno.h>
33 #include <sys/param.h>
35 /* In strict ANSI mode, HP-UX machines define __hpux but not hpux */
36 #if defined(__hpux) && !defined(hpux)
37 # define hpux
38 #endif
40 #if defined(__bsdi__) || defined(__386BSD__) || defined(_CX_UX) || defined(hpux) || defined(_IBMR2) || defined(linux)
41 # include <signal.h>
42 #endif /* __bsdi__ || __386BSD__ || _CX_UX || hpux || _IBMR2 || linux */
44 #ifdef ISC
45 # ifdef ENAMETOOLONG
46 # undef ENAMETOOLONG
47 # endif
48 # ifdef ENOTEMPTY
49 # undef ENOTEMPTY
50 # endif
51 # include <sys/bsdtypes.h>
52 # include <net/errno.h>
53 #endif
55 #ifdef sun
56 # define getpgrp __getpgrp
57 # define exit __exit
58 #endif
59 #ifdef POSIX
60 # include <unistd.h>
61 # if defined(__STDC__)
62 # include <stdlib.h>
63 # endif /* __STDC__ */
64 #endif /* POSIX */
65 #ifdef sun
66 # undef getpgrp
67 # undef exit
68 #endif /* sun */
70 #ifndef linux /* all done in <errno.h> */
71 extern int errno;
72 #endif /* linux */
73 #ifndef HAVE_STRERROR
74 /* No macros, please */
75 #undef strerror
76 #endif
78 #ifdef HAVE_STRINGS_H
79 # include <strings.h>
80 #endif
81 #ifdef HAVE_STRING_H
82 # include <string.h>
83 #endif
85 #ifdef USEVARARGS
86 # if defined(__STDC__)
87 # include <stdarg.h>
88 # define VA_LIST(var) va_list var;
89 # define VA_DOTS ...
90 # define VA_DECL
91 # define VA_START(ap, fmt) va_start(ap, fmt)
92 # define VA_ARGS(ap) ap
93 # define VA_END(ap) va_end(ap)
94 # else
95 # include <varargs.h>
96 # define VA_LIST(var) va_list var;
97 # define VA_DOTS va_alist
98 # define VA_DECL va_dcl
99 # define VA_START(ap, fmt) va_start(ap)
100 # define VA_ARGS(ap) ap
101 # define VA_END(ap) va_end(ap)
102 # endif
103 #else
104 # define VA_LIST(var)
105 # define VA_DOTS p1, p2, p3, p4, p5, p6
106 # define VA_DECL unsigned long VA_DOTS;
107 # define VA_START(ap, fmt)
108 # define VA_ARGS(ap) VA_DOTS
109 # define VA_END(ap)
110 # undef vsnprintf
111 # define vsnprintf xsnprintf
112 #endif
114 #if !defined(sun) && !defined(B43) && !defined(ISC) && !defined(pyr) && !defined(_CX_UX)
115 # include <time.h>
116 #endif
117 #include <sys/time.h>
119 #ifdef M_UNIX /* SCO */
120 # include <sys/stream.h>
121 # include <sys/ptem.h>
122 # define ftruncate(fd, s) chsize(fd, s)
123 #endif
125 #ifdef SYSV
126 # define index strchr
127 # define rindex strrchr
128 # define bzero(poi,len) memset(poi,0,len)
129 # define bcmp memcmp
130 # define killpg(pgrp,sig) kill( -(pgrp), sig)
131 #endif
133 #ifndef HAVE_GETCWD
134 # define getcwd(b,l) getwd(b)
135 #endif
137 #ifndef USEBCOPY
138 # ifdef USEMEMMOVE
139 # define bcopy(s,d,len) memmove(d,s,len)
140 # else
141 # ifdef USEMEMCPY
142 # define bcopy(s,d,len) memcpy(d,s,len)
143 # else
144 # define NEED_OWN_BCOPY
145 # define bcopy xbcopy
146 # endif
147 # endif
148 #endif
150 #if defined(HAVE_SETRESUID) && !defined(HAVE_SETREUID)
151 # define setreuid(ruid, euid) setresuid(ruid, euid, -1)
152 # define setregid(rgid, egid) setresgid(rgid, egid, -1)
153 #endif
155 #if defined(HAVE_SETEUID) || defined(HAVE_SETREUID) || defined(HAVE_SETRESUID)
156 # define USE_SETEUID
157 #endif
159 #if !defined(HAVE__EXIT) && !defined(_exit)
160 #define _exit(x) exit(x)
161 #endif
163 #ifndef HAVE_UTIMES
164 # define utimes utime
165 #endif
166 #ifndef HAVE_VSNPRINTF
167 # define vsnprintf xvsnprintf
168 #endif
170 #ifdef BUILTIN_TELNET
171 # include <netinet/in.h>
172 # include <arpa/inet.h>
173 #endif
175 #if defined(USE_LOCALE) && (!defined(HAVE_SETLOCALE) || !defined(HAVE_STRFTIME))
176 # undef USE_LOCALE
177 #endif
179 /*****************************************************************
180 * terminal handling
183 #ifdef POSIX
184 # include <termios.h>
185 # ifdef hpux
186 # include <bsdtty.h>
187 # endif /* hpux */
188 # ifdef NCCS
189 # define MAXCC NCCS
190 # else
191 # define MAXCC 256
192 # endif
193 #else /* POSIX */
194 # ifdef TERMIO
195 # include <termio.h>
196 # ifdef NCC
197 # define MAXCC NCC
198 # else
199 # define MAXCC 256
200 # endif
201 # ifdef CYTERMIO
202 # include <cytermio.h>
203 # endif
204 # else /* TERMIO */
205 # include <sgtty.h>
206 # endif /* TERMIO */
207 #endif /* POSIX */
209 #ifndef VDISABLE
210 # ifdef _POSIX_VDISABLE
211 # define VDISABLE _POSIX_VDISABLE
212 # else
213 # define VDISABLE 0377
214 # endif /* _POSIX_VDISABLE */
215 #endif /* !VDISABLE */
218 /* on sgi, regardless of the stream head's read mode (RNORM/RMSGN/RMSGD)
219 * TIOCPKT mode causes data loss if our buffer is too small (IOSIZE)
220 * to hold the whole packet at first read().
221 * (Marc Boucher)
223 * matthew green:
224 * TIOCPKT is broken on dgux 5.4.1 generic AViiON mc88100
226 * Joe Traister: On AIX4, programs like irc won't work if screen
227 * uses TIOCPKT (select fails to return on pty read).
229 #if defined(sgi) || defined(DGUX) || defined(_IBMR2)
230 # undef TIOCPKT
231 #endif
233 /* linux ncurses is broken, we have to use our own tputs */
234 #if defined(linux) && defined(TERMINFO)
235 # define tputs xtputs
236 #endif
238 /* Alexandre Oliva: SVR4 style ptys don't work with osf */
239 #ifdef __osf__
240 # undef HAVE_SVR4_PTYS
241 #endif
243 /*****************************************************************
244 * utmp handling
247 #ifdef GETUTENT
248 typedef char *slot_t;
249 #else
250 typedef int slot_t;
251 #endif
253 #if defined(UTMPOK) || defined(BUGGYGETLOGIN)
254 # if defined(SVR4) && !defined(DGUX) && !defined(__hpux) && !defined(linux)
255 # include <utmpx.h>
256 # define UTMPFILE UTMPX_FILE
257 # define utmp utmpx
258 # define getutent getutxent
259 # define getutid getutxid
260 # define getutline getutxline
261 # define pututline pututxline
262 # define setutent setutxent
263 # define endutent endutxent
264 # define ut_time ut_xtime
265 # else /* SVR4 */
266 # include <utmp.h>
267 # endif /* SVR4 */
268 # ifdef apollo
270 * We don't have GETUTENT, so we dig into utmp ourselves.
271 * But we save the permanent filedescriptor and
272 * open utmp just when we need to.
273 * This code supports an unsorted utmp. jw.
275 # define UTNOKEEP
276 # endif /* apollo */
278 # ifndef UTMPFILE
279 # ifdef UTMP_FILE
280 # define UTMPFILE UTMP_FILE
281 # else
282 # ifdef _PATH_UTMP
283 # define UTMPFILE _PATH_UTMP
284 # else
285 # define UTMPFILE "/etc/utmp"
286 # endif /* _PATH_UTMP */
287 # endif
288 # endif
290 #endif /* UTMPOK || BUGGYGETLOGIN */
292 #if !defined(UTMPOK) && defined(USRLIMIT)
293 # undef USRLIMIT
294 #endif
296 #ifdef LOGOUTOK
297 # ifndef LOGINDEFAULT
298 # define LOGINDEFAULT 0
299 # endif
300 #else
301 # ifdef LOGINDEFAULT
302 # undef LOGINDEFAULT
303 # endif
304 # define LOGINDEFAULT 1
305 #endif
308 /*****************************************************************
309 * file stuff
312 #ifndef F_OK
313 #define F_OK 0
314 #endif
315 #ifndef X_OK
316 #define X_OK 1
317 #endif
318 #ifndef W_OK
319 #define W_OK 2
320 #endif
321 #ifndef R_OK
322 #define R_OK 4
323 #endif
325 #ifndef S_IFIFO
326 #define S_IFIFO 0010000
327 #endif
328 #ifndef S_IREAD
329 #define S_IREAD 0000400
330 #endif
331 #ifndef S_IWRITE
332 #define S_IWRITE 0000200
333 #endif
334 #ifndef S_IEXEC
335 #define S_IEXEC 0000100
336 #endif
338 #if defined(S_IFIFO) && defined(S_IFMT) && !defined(S_ISFIFO)
339 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
340 #endif
341 #if defined(S_IFSOCK) && defined(S_IFMT) && !defined(S_ISSOCK)
342 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
343 #endif
344 #if defined(S_IFCHR) && defined(S_IFMT) && !defined(S_ISCHR)
345 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
346 #endif
347 #if defined(S_IFDIR) && defined(S_IFMT) && !defined(S_ISDIR)
348 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
349 #endif
350 #if defined(S_IFLNK) && defined(S_IFMT) && !defined(S_ISLNK)
351 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
352 #endif
355 * SunOS 4.1.3: `man 2V open' has only one line that mentions O_NOBLOCK:
357 * O_NONBLOCK Same as O_NDELAY above.
359 * on the very same SunOS 4.1.3, I traced the open system call and found
360 * that an open("/dev/ttyy08", O_RDWR|O_NONBLOCK|O_NOCTTY) was blocked,
361 * whereas open("/dev/ttyy08", O_RDWR|O_NDELAY |O_NOCTTY) went through.
363 * For this simple reason I now favour O_NDELAY. jw. 4.5.95
365 #if defined(sun) && !defined(SVR4)
366 # undef O_NONBLOCK
367 #endif
369 #if !defined(O_NONBLOCK) && defined(O_NDELAY)
370 # define O_NONBLOCK O_NDELAY
371 #endif
373 #if !defined(FNBLOCK) && defined(FNONBLOCK)
374 # define FNBLOCK FNONBLOCK
375 #endif
376 #if !defined(FNBLOCK) && defined(FNDELAY)
377 # define FNBLOCK FNDELAY
378 #endif
379 #if !defined(FNBLOCK) && defined(O_NONBLOCK)
380 # define FNBLOCK O_NONBLOCK
381 #endif
383 #ifndef POSIX
384 #undef mkfifo
385 #define mkfifo(n,m) mknod(n,S_IFIFO|(m),0)
386 #endif
388 #if !defined(HAVE_LSTAT) && !defined(lstat)
389 # define lstat stat
390 #endif
392 /*****************************************************************
393 * signal handling
396 #ifdef SIGVOID
397 # define SIGRETURN
398 # define sigret_t void
399 #else
400 # define SIGRETURN return 0;
401 # define sigret_t int
402 #endif
404 /* Geeeee, reverse it? */
405 #if defined(SVR4) || (defined(SYSV) && defined(ISC)) || defined(_AIX) || defined(linux) || defined(ultrix) || defined(__386BSD__) || defined(__bsdi__) || defined(POSIX) || defined(NeXT)
406 # define SIGHASARG
407 #endif
409 #ifdef SIGHASARG
410 # define SIGPROTOARG (int)
411 # define SIGDEFARG (sigsig) int sigsig;
412 # define SIGARG 0
413 #else
414 # define SIGPROTOARG (void)
415 # define SIGDEFARG ()
416 # define SIGARG
417 #endif
419 #ifndef SIGCHLD
420 #define SIGCHLD SIGCLD
421 #endif
423 #if defined(POSIX) || defined(hpux)
424 # define signal xsignal
425 #else
426 # ifdef USESIGSET
427 # define signal sigset
428 # endif /* USESIGSET */
429 #endif
431 /* used in screen.c and attacher.c */
432 #ifndef NSIG /* kbeal needs these w/o SYSV */
433 # define NSIG 32
434 #endif /* !NSIG */
437 /*****************************************************************
438 * Wait stuff
441 #if (!defined(sysV68) && !defined(M_XENIX)) || defined(NeXT) || defined(M_UNIX)
442 # include <sys/wait.h>
443 #endif
445 #ifndef WTERMSIG
446 # ifndef BSDWAIT /* if wait is NOT a union: */
447 # define WTERMSIG(status) (status & 0177)
448 # else
449 # define WTERMSIG(status) status.w_T.w_Termsig
450 # endif
451 #endif
453 #ifndef WSTOPSIG
454 # ifndef BSDWAIT /* if wait is NOT a union: */
455 # define WSTOPSIG(status) ((status >> 8) & 0377)
456 # else
457 # define WSTOPSIG(status) status.w_S.w_Stopsig
458 # endif
459 #endif
461 /* NET-2 uses WCOREDUMP */
462 #if defined(WCOREDUMP) && !defined(WIFCORESIG)
463 # define WIFCORESIG(status) WCOREDUMP(status)
464 #endif
466 #ifndef WIFCORESIG
467 # ifndef BSDWAIT /* if wait is NOT a union: */
468 # define WIFCORESIG(status) (status & 0200)
469 # else
470 # define WIFCORESIG(status) status.w_T.w_Coredump
471 # endif
472 #endif
474 #ifndef WEXITSTATUS
475 # ifndef BSDWAIT /* if wait is NOT a union: */
476 # define WEXITSTATUS(status) ((status >> 8) & 0377)
477 # else
478 # define WEXITSTATUS(status) status.w_T.w_Retcode
479 # endif
480 #endif
483 /*****************************************************************
484 * select stuff
487 #if defined(M_XENIX) || defined(M_UNIX) || defined(_SEQUENT_)
488 #include <sys/select.h> /* for timeval + FD... */
489 #endif
492 * SunOS 3.5 - Tom Schmidt - Micron Semiconductor, Inc - 27-Jul-93
493 * tschmidt@vax.micron.com
495 #ifndef FD_SET
496 # ifndef SUNOS3
497 typedef struct fd_set { int fds_bits[1]; } fd_set;
498 # endif
499 # define FD_ZERO(fd) ((fd)->fds_bits[0] = 0)
500 # define FD_SET(b, fd) ((fd)->fds_bits[0] |= 1 << (b))
501 # define FD_ISSET(b, fd) ((fd)->fds_bits[0] & 1 << (b))
502 # define FD_SETSIZE 32
503 #endif
506 /*****************************************************************
507 * user defineable stuff
510 #ifndef TERMCAP_BUFSIZE
511 # define TERMCAP_BUFSIZE 2048
512 #endif
514 #ifndef MAXPATHLEN
515 # define MAXPATHLEN 1024
516 #endif
519 * you may try to vary this value. Use low values if your (VMS) system
520 * tends to choke when pasting. Use high values if you want to test
521 * how many characters your pty's can buffer.
523 #define IOSIZE 4096