a05a18034f03a7c19af9ca5312a6de82253bdcc6
[screen-lua.git] / src / os.h
bloba05a18034f03a7c19af9ca5312a6de82253bdcc6
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 #if !defined(SYSV) && !defined(linux)
79 # ifdef NEWSOS
80 # define strlen ___strlen___
81 # include <strings.h>
82 # undef strlen
83 # else /* NEWSOS */
84 # include <strings.h>
85 # endif /* NEWSOS */
86 #else /* SYSV */
87 # if defined(SVR4) || defined(NEWSOS)
88 # define strlen ___strlen___
89 # include <string.h>
90 # undef strlen
91 # if !defined(NEWSOS) && !defined(__hpux)
92 extern size_t strlen(const char *);
93 # endif
94 # else /* SVR4 */
95 # include <string.h>
96 # endif /* SVR4 */
97 #endif /* SYSV */
99 #ifdef USEVARARGS
100 # if defined(__STDC__)
101 # include <stdarg.h>
102 # define VA_LIST(var) va_list var;
103 # define VA_DOTS ...
104 # define VA_DECL
105 # define VA_START(ap, fmt) va_start(ap, fmt)
106 # define VA_ARGS(ap) ap
107 # define VA_END(ap) va_end(ap)
108 # else
109 # include <varargs.h>
110 # define VA_LIST(var) va_list var;
111 # define VA_DOTS va_alist
112 # define VA_DECL va_dcl
113 # define VA_START(ap, fmt) va_start(ap)
114 # define VA_ARGS(ap) ap
115 # define VA_END(ap) va_end(ap)
116 # endif
117 #else
118 # define VA_LIST(var)
119 # define VA_DOTS p1, p2, p3, p4, p5, p6
120 # define VA_DECL unsigned long VA_DOTS;
121 # define VA_START(ap, fmt)
122 # define VA_ARGS(ap) VA_DOTS
123 # define VA_END(ap)
124 # undef vsnprintf
125 # define vsnprintf xsnprintf
126 #endif
128 #if !defined(sun) && !defined(B43) && !defined(ISC) && !defined(pyr) && !defined(_CX_UX)
129 # include <time.h>
130 #endif
131 #include <sys/time.h>
133 #ifdef M_UNIX /* SCO */
134 # include <sys/stream.h>
135 # include <sys/ptem.h>
136 # define ftruncate(fd, s) chsize(fd, s)
137 #endif
139 #ifdef SYSV
140 # define index strchr
141 # define rindex strrchr
142 # define bzero(poi,len) memset(poi,0,len)
143 # define bcmp memcmp
144 # define killpg(pgrp,sig) kill( -(pgrp), sig)
145 #endif
147 #ifndef HAVE_GETCWD
148 # define getcwd(b,l) getwd(b)
149 #endif
151 #ifndef USEBCOPY
152 # ifdef USEMEMMOVE
153 # define bcopy(s,d,len) memmove(d,s,len)
154 # else
155 # ifdef USEMEMCPY
156 # define bcopy(s,d,len) memcpy(d,s,len)
157 # else
158 # define NEED_OWN_BCOPY
159 # define bcopy xbcopy
160 # endif
161 # endif
162 #endif
164 #if defined(HAVE_SETRESUID) && !defined(HAVE_SETREUID)
165 # define setreuid(ruid, euid) setresuid(ruid, euid, -1)
166 # define setregid(rgid, egid) setresgid(rgid, egid, -1)
167 #endif
169 #if defined(HAVE_SETEUID) || defined(HAVE_SETREUID) || defined(HAVE_SETRESUID)
170 # define USE_SETEUID
171 #endif
173 #if !defined(HAVE__EXIT) && !defined(_exit)
174 #define _exit(x) exit(x)
175 #endif
177 #ifndef HAVE_UTIMES
178 # define utimes utime
179 #endif
180 #ifndef HAVE_VSNPRINTF
181 # define vsnprintf xvsnprintf
182 #endif
184 #ifdef BUILTIN_TELNET
185 # include <netinet/in.h>
186 # include <arpa/inet.h>
187 #endif
189 #if defined(USE_LOCALE) && (!defined(HAVE_SETLOCALE) || !defined(HAVE_STRFTIME))
190 # undef USE_LOCALE
191 #endif
193 /*****************************************************************
194 * terminal handling
197 #ifdef POSIX
198 # include <termios.h>
199 # ifdef hpux
200 # include <bsdtty.h>
201 # endif /* hpux */
202 # ifdef NCCS
203 # define MAXCC NCCS
204 # else
205 # define MAXCC 256
206 # endif
207 #else /* POSIX */
208 # ifdef TERMIO
209 # include <termio.h>
210 # ifdef NCC
211 # define MAXCC NCC
212 # else
213 # define MAXCC 256
214 # endif
215 # ifdef CYTERMIO
216 # include <cytermio.h>
217 # endif
218 # else /* TERMIO */
219 # include <sgtty.h>
220 # endif /* TERMIO */
221 #endif /* POSIX */
223 #ifndef VDISABLE
224 # ifdef _POSIX_VDISABLE
225 # define VDISABLE _POSIX_VDISABLE
226 # else
227 # define VDISABLE 0377
228 # endif /* _POSIX_VDISABLE */
229 #endif /* !VDISABLE */
232 /* on sgi, regardless of the stream head's read mode (RNORM/RMSGN/RMSGD)
233 * TIOCPKT mode causes data loss if our buffer is too small (IOSIZE)
234 * to hold the whole packet at first read().
235 * (Marc Boucher)
237 * matthew green:
238 * TIOCPKT is broken on dgux 5.4.1 generic AViiON mc88100
240 * Joe Traister: On AIX4, programs like irc won't work if screen
241 * uses TIOCPKT (select fails to return on pty read).
243 #if defined(sgi) || defined(DGUX) || defined(_IBMR2)
244 # undef TIOCPKT
245 #endif
247 /* linux ncurses is broken, we have to use our own tputs */
248 #if defined(linux) && defined(TERMINFO)
249 # define tputs xtputs
250 #endif
252 /* Alexandre Oliva: SVR4 style ptys don't work with osf */
253 #ifdef __osf__
254 # undef HAVE_SVR4_PTYS
255 #endif
257 /*****************************************************************
258 * utmp handling
261 #ifdef GETUTENT
262 typedef char *slot_t;
263 #else
264 typedef int slot_t;
265 #endif
267 #if defined(UTMPOK) || defined(BUGGYGETLOGIN)
268 # if defined(SVR4) && !defined(DGUX) && !defined(__hpux) && !defined(linux)
269 # include <utmpx.h>
270 # define UTMPFILE UTMPX_FILE
271 # define utmp utmpx
272 # define getutent getutxent
273 # define getutid getutxid
274 # define getutline getutxline
275 # define pututline pututxline
276 # define setutent setutxent
277 # define endutent endutxent
278 # define ut_time ut_xtime
279 # else /* SVR4 */
280 # include <utmp.h>
281 # endif /* SVR4 */
282 # ifdef apollo
284 * We don't have GETUTENT, so we dig into utmp ourselves.
285 * But we save the permanent filedescriptor and
286 * open utmp just when we need to.
287 * This code supports an unsorted utmp. jw.
289 # define UTNOKEEP
290 # endif /* apollo */
292 # ifndef UTMPFILE
293 # ifdef UTMP_FILE
294 # define UTMPFILE UTMP_FILE
295 # else
296 # ifdef _PATH_UTMP
297 # define UTMPFILE _PATH_UTMP
298 # else
299 # define UTMPFILE "/etc/utmp"
300 # endif /* _PATH_UTMP */
301 # endif
302 # endif
304 #endif /* UTMPOK || BUGGYGETLOGIN */
306 #if !defined(UTMPOK) && defined(USRLIMIT)
307 # undef USRLIMIT
308 #endif
310 #ifdef LOGOUTOK
311 # ifndef LOGINDEFAULT
312 # define LOGINDEFAULT 0
313 # endif
314 #else
315 # ifdef LOGINDEFAULT
316 # undef LOGINDEFAULT
317 # endif
318 # define LOGINDEFAULT 1
319 #endif
322 /*****************************************************************
323 * file stuff
326 #ifndef F_OK
327 #define F_OK 0
328 #endif
329 #ifndef X_OK
330 #define X_OK 1
331 #endif
332 #ifndef W_OK
333 #define W_OK 2
334 #endif
335 #ifndef R_OK
336 #define R_OK 4
337 #endif
339 #ifndef S_IFIFO
340 #define S_IFIFO 0010000
341 #endif
342 #ifndef S_IREAD
343 #define S_IREAD 0000400
344 #endif
345 #ifndef S_IWRITE
346 #define S_IWRITE 0000200
347 #endif
348 #ifndef S_IEXEC
349 #define S_IEXEC 0000100
350 #endif
352 #if defined(S_IFIFO) && defined(S_IFMT) && !defined(S_ISFIFO)
353 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
354 #endif
355 #if defined(S_IFSOCK) && defined(S_IFMT) && !defined(S_ISSOCK)
356 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
357 #endif
358 #if defined(S_IFCHR) && defined(S_IFMT) && !defined(S_ISCHR)
359 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
360 #endif
361 #if defined(S_IFDIR) && defined(S_IFMT) && !defined(S_ISDIR)
362 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
363 #endif
364 #if defined(S_IFLNK) && defined(S_IFMT) && !defined(S_ISLNK)
365 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
366 #endif
369 * SunOS 4.1.3: `man 2V open' has only one line that mentions O_NOBLOCK:
371 * O_NONBLOCK Same as O_NDELAY above.
373 * on the very same SunOS 4.1.3, I traced the open system call and found
374 * that an open("/dev/ttyy08", O_RDWR|O_NONBLOCK|O_NOCTTY) was blocked,
375 * whereas open("/dev/ttyy08", O_RDWR|O_NDELAY |O_NOCTTY) went through.
377 * For this simple reason I now favour O_NDELAY. jw. 4.5.95
379 #if defined(sun) && !defined(SVR4)
380 # undef O_NONBLOCK
381 #endif
383 #if !defined(O_NONBLOCK) && defined(O_NDELAY)
384 # define O_NONBLOCK O_NDELAY
385 #endif
387 #if !defined(FNBLOCK) && defined(FNONBLOCK)
388 # define FNBLOCK FNONBLOCK
389 #endif
390 #if !defined(FNBLOCK) && defined(FNDELAY)
391 # define FNBLOCK FNDELAY
392 #endif
393 #if !defined(FNBLOCK) && defined(O_NONBLOCK)
394 # define FNBLOCK O_NONBLOCK
395 #endif
397 #ifndef POSIX
398 #undef mkfifo
399 #define mkfifo(n,m) mknod(n,S_IFIFO|(m),0)
400 #endif
402 #if !defined(HAVE_LSTAT) && !defined(lstat)
403 # define lstat stat
404 #endif
406 /*****************************************************************
407 * signal handling
410 #ifdef SIGVOID
411 # define SIGRETURN
412 # define sigret_t void
413 #else
414 # define SIGRETURN return 0;
415 # define sigret_t int
416 #endif
418 /* Geeeee, reverse it? */
419 #if defined(SVR4) || (defined(SYSV) && defined(ISC)) || defined(_AIX) || defined(linux) || defined(ultrix) || defined(__386BSD__) || defined(__bsdi__) || defined(POSIX) || defined(NeXT)
420 # define SIGHASARG
421 #endif
423 #ifdef SIGHASARG
424 # define SIGPROTOARG (int)
425 # define SIGDEFARG (sigsig) int sigsig;
426 # define SIGARG 0
427 #else
428 # define SIGPROTOARG (void)
429 # define SIGDEFARG ()
430 # define SIGARG
431 #endif
433 #ifndef SIGCHLD
434 #define SIGCHLD SIGCLD
435 #endif
437 #if defined(POSIX) || defined(hpux)
438 # define signal xsignal
439 #else
440 # ifdef USESIGSET
441 # define signal sigset
442 # endif /* USESIGSET */
443 #endif
445 /* used in screen.c and attacher.c */
446 #ifndef NSIG /* kbeal needs these w/o SYSV */
447 # define NSIG 32
448 #endif /* !NSIG */
451 /*****************************************************************
452 * Wait stuff
455 #if (!defined(sysV68) && !defined(M_XENIX)) || defined(NeXT) || defined(M_UNIX)
456 # include <sys/wait.h>
457 #endif
459 #ifndef WTERMSIG
460 # ifndef BSDWAIT /* if wait is NOT a union: */
461 # define WTERMSIG(status) (status & 0177)
462 # else
463 # define WTERMSIG(status) status.w_T.w_Termsig
464 # endif
465 #endif
467 #ifndef WSTOPSIG
468 # ifndef BSDWAIT /* if wait is NOT a union: */
469 # define WSTOPSIG(status) ((status >> 8) & 0377)
470 # else
471 # define WSTOPSIG(status) status.w_S.w_Stopsig
472 # endif
473 #endif
475 /* NET-2 uses WCOREDUMP */
476 #if defined(WCOREDUMP) && !defined(WIFCORESIG)
477 # define WIFCORESIG(status) WCOREDUMP(status)
478 #endif
480 #ifndef WIFCORESIG
481 # ifndef BSDWAIT /* if wait is NOT a union: */
482 # define WIFCORESIG(status) (status & 0200)
483 # else
484 # define WIFCORESIG(status) status.w_T.w_Coredump
485 # endif
486 #endif
488 #ifndef WEXITSTATUS
489 # ifndef BSDWAIT /* if wait is NOT a union: */
490 # define WEXITSTATUS(status) ((status >> 8) & 0377)
491 # else
492 # define WEXITSTATUS(status) status.w_T.w_Retcode
493 # endif
494 #endif
497 /*****************************************************************
498 * select stuff
501 #if defined(M_XENIX) || defined(M_UNIX) || defined(_SEQUENT_)
502 #include <sys/select.h> /* for timeval + FD... */
503 #endif
506 * SunOS 3.5 - Tom Schmidt - Micron Semiconductor, Inc - 27-Jul-93
507 * tschmidt@vax.micron.com
509 #ifndef FD_SET
510 # ifndef SUNOS3
511 typedef struct fd_set { int fds_bits[1]; } fd_set;
512 # endif
513 # define FD_ZERO(fd) ((fd)->fds_bits[0] = 0)
514 # define FD_SET(b, fd) ((fd)->fds_bits[0] |= 1 << (b))
515 # define FD_ISSET(b, fd) ((fd)->fds_bits[0] & 1 << (b))
516 # define FD_SETSIZE 32
517 #endif
520 /*****************************************************************
521 * user defineable stuff
524 #ifndef TERMCAP_BUFSIZE
525 # define TERMCAP_BUFSIZE 2048
526 #endif
528 #ifndef MAXPATHLEN
529 # define MAXPATHLEN 1024
530 #endif
533 * you may try to vary this value. Use low values if your (VMS) system
534 * tends to choke when pasting. Use high values if you want to test
535 * how many characters your pty's can buffer.
537 #define IOSIZE 4096