1157184Sache/* Copyright (c) 1993-2002
2157184Sache *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3157184Sache *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4157184Sache * Copyright (c) 1987 Oliver Laumann
5157184Sache *
6157184Sache * This program is free software; you can redistribute it and/or modify
7157184Sache * it under the terms of the GNU General Public License as published by
8157184Sache * the Free Software Foundation; either version 2, or (at your option)
9157184Sache * any later version.
10157184Sache *
11157184Sache * This program is distributed in the hope that it will be useful,
12157184Sache * but WITHOUT ANY WARRANTY; without even the implied warranty of
13157184Sache * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14157184Sache * GNU General Public License for more details.
15157184Sache *
16157184Sache * You should have received a copy of the GNU General Public License
17157184Sache * along with this program (see the file COPYING); if not, write to the
18157184Sache * Free Software Foundation, Inc.,
19157184Sache * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
20157184Sache *
21157184Sache ****************************************************************
22157184Sache * $Id: os.h,v 1.10 1994/05/31 12:32:22 mlschroe Exp $ FAU
23157184Sache */
24157184Sache
25157184Sache#include <stdio.h>
26157184Sache#include <errno.h>
27157184Sache
28157184Sache#include <sys/param.h>
29157184Sache
30157184Sache/* In strict ANSI mode, HP-UX machines define __hpux but not hpux */
31157184Sache#if defined(__hpux) && !defined(hpux)
32157184Sache# define hpux
33157184Sache#endif
34157184Sache
35157184Sache#if defined(__bsdi__) || defined(__386BSD__) || defined(_CX_UX) || defined(hpux) || defined(_IBMR2) || defined(linux)
36157184Sache# include <signal.h>
37157184Sache#endif /* __bsdi__ || __386BSD__ || _CX_UX || hpux || _IBMR2 || linux */
38157184Sache
39157184Sache#ifdef ISC
40157184Sache# ifdef ENAMETOOLONG
41157184Sache#  undef ENAMETOOLONG
42157184Sache# endif
43157184Sache# ifdef ENOTEMPTY
44157184Sache#  undef ENOTEMPTY
45157184Sache# endif
46157184Sache# include <sys/bsdtypes.h>
47157184Sache# include <net/errno.h>
48157184Sache#endif
49157184Sache
50157184Sache#ifdef sun
51157184Sache# define getpgrp __getpgrp
52157184Sache# define exit __exit
53157184Sache#endif
54157184Sache#ifdef POSIX
55157184Sache# include <unistd.h>
56157184Sache# if defined(__STDC__)
57157184Sache#  include <stdlib.h>
58157184Sache# endif /* __STDC__ */
59157184Sache#endif /* POSIX */
60157184Sache#ifdef sun
61157184Sache# undef getpgrp
62157184Sache# undef exit
63157184Sache#endif /* sun */
64157184Sache
65157184Sache#ifndef linux /* all done in <errno.h> */
66157184Sacheextern int errno;
67157184Sache#endif /* linux */
68157184Sache#ifndef HAVE_STRERROR
69157184Sache/* No macros, please */
70157184Sache#undef strerror
71157184Sache#endif
72157184Sache
73157184Sache#if !defined(SYSV) && !defined(linux)
74157184Sache# ifdef NEWSOS
75157184Sache#  define strlen ___strlen___
76157184Sache#  include <strings.h>
77157184Sache#  undef strlen
78157184Sache# else /* NEWSOS */
79157184Sache#  include <strings.h>
80157184Sache# endif /* NEWSOS */
81157184Sache#else /* SYSV */
82157184Sache# if defined(SVR4) || defined(NEWSOS)
83157184Sache#  define strlen ___strlen___
84157184Sache#  include <string.h>
85157184Sache#  undef strlen
86157184Sache#  if !defined(NEWSOS) && !defined(__hpux)
87157184Sache    extern size_t strlen(const char *);
88157184Sache#  endif
89157184Sache# else /* SVR4 */
90157184Sache#  include <string.h>
91157184Sache# endif /* SVR4 */
92157184Sache#endif /* SYSV */
93157184Sache
94157184Sache#ifdef USEVARARGS
95157184Sache# if defined(__STDC__)
96157184Sache#  include <stdarg.h>
97157184Sache#  define VA_LIST(var) va_list var;
98157184Sache#  define VA_DOTS ...
99157184Sache#  define VA_DECL
100157184Sache#  define VA_START(ap, fmt) va_start(ap, fmt)
101157184Sache#  define VA_ARGS(ap) ap
102157184Sache#  define VA_END(ap) va_end(ap)
103157184Sache# else
104157184Sache#  include <varargs.h>
105157184Sache#  define VA_LIST(var) va_list var;
106157184Sache#  define VA_DOTS va_alist
107157184Sache#  define VA_DECL va_dcl
108157184Sache#  define VA_START(ap, fmt) va_start(ap)
109157184Sache#  define VA_ARGS(ap) ap
110157184Sache#  define VA_END(ap) va_end(ap)
111157184Sache# endif
112157184Sache#else
113157184Sache# define VA_LIST(var)
114157184Sache# define VA_DOTS p1, p2, p3, p4, p5, p6
115157184Sache# define VA_DECL unsigned long VA_DOTS;
116157184Sache# define VA_START(ap, fmt)
117157184Sache# define VA_ARGS(ap) VA_DOTS
118157184Sache# define VA_END(ap)
119157184Sache# undef vsnprintf
120157184Sache# define vsnprintf xsnprintf
121157184Sache#endif
122157184Sache
123157184Sache#if !defined(sun) && !defined(B43) && !defined(ISC) && !defined(pyr) && !defined(_CX_UX)
124157184Sache# include <time.h>
125157184Sache#endif
126157184Sache#include <sys/time.h>
127157184Sache
128157184Sache#ifdef M_UNIX   /* SCO */
129157184Sache# include <sys/stream.h>
130157184Sache# include <sys/ptem.h>
131157184Sache# define ftruncate(fd, s) chsize(fd, s)
132157184Sache#endif
133157184Sache
134157184Sache#ifdef SYSV
135157184Sache# define index strchr
136157184Sache# define rindex strrchr
137157184Sache# define bzero(poi,len) memset(poi,0,len)
138157184Sache# define bcmp memcmp
139157184Sache# define killpg(pgrp,sig) kill( -(pgrp), sig)
140157184Sache#endif
141157184Sache
142157184Sache#ifndef HAVE_GETCWD
143157184Sache# define getcwd(b,l) getwd(b)
144157184Sache#endif
145157184Sache
146157184Sache#ifndef USEBCOPY
147157184Sache# ifdef USEMEMMOVE
148157184Sache#  define bcopy(s,d,len) memmove(d,s,len)
149157184Sache# else
150157184Sache#  ifdef USEMEMCPY
151157184Sache#   define bcopy(s,d,len) memcpy(d,s,len)
152157184Sache#  else
153157184Sache#   define NEED_OWN_BCOPY
154157184Sache#   define bcopy xbcopy
155157184Sache#  endif
156157184Sache# endif
157157184Sache#endif
158157184Sache
159157184Sache#ifdef hpux
160157184Sache# define setreuid(ruid, euid) setresuid(ruid, euid, -1)
161157184Sache# define setregid(rgid, egid) setresgid(rgid, egid, -1)
162157184Sache#endif
163157184Sache
164157184Sache#if defined(HAVE_SETEUID) || defined(HAVE_SETREUID)
165157184Sache# define USE_SETEUID
166157184Sache#endif
167157184Sache
168157184Sache#if !defined(HAVE__EXIT) && !defined(_exit)
169157184Sache#define _exit(x) exit(x)
170157184Sache#endif
171157184Sache
172157184Sache#ifndef HAVE_UTIMES
173157184Sache# define utimes utime
174157184Sache#endif
175157184Sache
176157184Sache#ifdef BUILTIN_TELNET
177157184Sache# include <netinet/in.h>
178157184Sache# include <arpa/inet.h>
179157184Sache#endif
180157184Sache
181157184Sache#if defined(USE_LOCALE) && (!defined(HAVE_SETLOCALE) || !defined(HAVE_STRFTIME))
182157184Sache# undef USE_LOCALE
183157184Sache#endif
184157184Sache
185157184Sache/*****************************************************************
186157184Sache *    terminal handling
187157184Sache */
188157184Sache
189157184Sache#ifdef POSIX
190157184Sache# include <termios.h>
191157184Sache# ifdef hpux
192157184Sache#  include <bsdtty.h>
193157184Sache# endif /* hpux */
194157184Sache# ifdef NCCS
195157184Sache#  define MAXCC NCCS
196157184Sache# else
197157184Sache#  define MAXCC 256
198157184Sache# endif
199157184Sache#else /* POSIX */
200157184Sache# ifdef TERMIO
201157184Sache#  include <termio.h>
202157184Sache#  ifdef NCC
203157184Sache#   define MAXCC NCC
204157184Sache#  else
205157184Sache#   define MAXCC 256
206157184Sache#  endif
207157184Sache#  ifdef CYTERMIO
208157184Sache#   include <cytermio.h>
209157184Sache#  endif
210157184Sache# else /* TERMIO */
211157184Sache#  include <sgtty.h>
212157184Sache# endif /* TERMIO */
213157184Sache#endif /* POSIX */
214157184Sache
215157184Sache#ifndef VDISABLE
216157184Sache# ifdef _POSIX_VDISABLE
217157184Sache#  define VDISABLE _POSIX_VDISABLE
218157184Sache# else
219157184Sache#  define VDISABLE 0377
220157184Sache# endif /* _POSIX_VDISABLE */
221157184Sache#endif /* !VDISABLE */
222157184Sache
223157184Sache
224157184Sache/* on sgi, regardless of the stream head's read mode (RNORM/RMSGN/RMSGD)
225157184Sache * TIOCPKT mode causes data loss if our buffer is too small (IOSIZE)
226157184Sache * to hold the whole packet at first read().
227157184Sache * (Marc Boucher)
228157184Sache *
229157184Sache * matthew green:
230157184Sache * TIOCPKT is broken on dgux 5.4.1 generic AViiON mc88100
231157184Sache *
232157184Sache * Joe Traister: On AIX4, programs like irc won't work if screen
233157184Sache * uses TIOCPKT (select fails to return on pty read).
234157184Sache */
235157184Sache#if defined(sgi) || defined(DGUX) || defined(_IBMR2)
236157184Sache# undef TIOCPKT
237157184Sache#endif
238157184Sache
239157184Sache/* linux ncurses is broken, we have to use our own tputs */
240157184Sache#if defined(linux) && defined(TERMINFO)
241157184Sache# define tputs xtputs
242157184Sache#endif
243157184Sache
244157184Sache/* Alexandre Oliva: SVR4 style ptys don't work with osf */
245157184Sache#ifdef __osf__
246157184Sache# undef HAVE_SVR4_PTYS
247157184Sache#endif
248157184Sache
249157184Sache/*****************************************************************
250157184Sache *   utmp handling
251157184Sache */
252157184Sache
253157184Sache#ifdef GETUTENT
254157184Sache  typedef char *slot_t;
255157184Sache#else
256157184Sache  typedef int slot_t;
257157184Sache#endif
258157184Sache
259157184Sache#if defined(UTMPOK) || defined(BUGGYGETLOGIN)
260157184Sache# if defined(SVR4) && !defined(DGUX) && !defined(__hpux) && !defined(linux)
261157184Sache#  include <utmpx.h>
262157184Sache#  define UTMPFILE	UTMPX_FILE
263157184Sache#  define utmp		utmpx
264157184Sache#  define getutent	getutxent
265157184Sache#  define getutid	getutxid
266157184Sache#  define getutline	getutxline
267157184Sache#  define pututline	pututxline
268157184Sache#  define setutent	setutxent
269157184Sache#  define endutent	endutxent
270157184Sache#  define ut_time	ut_xtime
271157184Sache# else /* SVR4 */
272157184Sache#  include <utmp.h>
273157184Sache# endif /* SVR4 */
274157184Sache# ifdef apollo
275157184Sache   /*
276157184Sache    * We don't have GETUTENT, so we dig into utmp ourselves.
277157184Sache    * But we save the permanent filedescriptor and
278157184Sache    * open utmp just when we need to.
279157184Sache    * This code supports an unsorted utmp. jw.
280157184Sache    */
281157184Sache#  define UTNOKEEP
282157184Sache# endif /* apollo */
283157184Sache
284157184Sache# ifndef UTMPFILE
285157184Sache#  ifdef UTMP_FILE
286157184Sache#   define UTMPFILE	UTMP_FILE
287157184Sache#  else
288157184Sache#   ifdef _PATH_UTMP
289157184Sache#    define UTMPFILE	_PATH_UTMP
290157184Sache#   else
291157184Sache#    define UTMPFILE	"/etc/utmp"
292157184Sache#   endif /* _PATH_UTMP */
293157184Sache#  endif
294157184Sache# endif
295157184Sache
296157184Sache#endif /* UTMPOK || BUGGYGETLOGIN */
297157184Sache
298157184Sache#if !defined(UTMPOK) && defined(USRLIMIT)
299157184Sache# undef USRLIMIT
300157184Sache#endif
301157184Sache
302157184Sache#ifdef LOGOUTOK
303157184Sache# ifndef LOGINDEFAULT
304157184Sache#  define LOGINDEFAULT 0
305157184Sache# endif
306157184Sache#else
307157184Sache# ifdef LOGINDEFAULT
308157184Sache#  undef LOGINDEFAULT
309157184Sache# endif
310157184Sache# define LOGINDEFAULT 1
311157184Sache#endif
312157184Sache
313157184Sache
314157184Sache/*****************************************************************
315157184Sache *    file stuff
316157184Sache */
317157184Sache
318157184Sache#ifndef F_OK
319157184Sache#define F_OK 0
320157184Sache#endif
321157184Sache#ifndef X_OK
322157184Sache#define X_OK 1
323157184Sache#endif
324157184Sache#ifndef W_OK
325157184Sache#define W_OK 2
326157184Sache#endif
327157184Sache#ifndef R_OK
328157184Sache#define R_OK 4
329157184Sache#endif
330157184Sache
331157184Sache#ifndef S_IFIFO
332157184Sache#define S_IFIFO  0010000
333157184Sache#endif
334157184Sache#ifndef S_IREAD
335157184Sache#define S_IREAD  0000400
336157184Sache#endif
337157184Sache#ifndef S_IWRITE
338157184Sache#define S_IWRITE 0000200
339157184Sache#endif
340157184Sache#ifndef S_IEXEC
341157184Sache#define S_IEXEC  0000100
342157184Sache#endif
343157184Sache
344157184Sache#if defined(S_IFIFO) && defined(S_IFMT) && !defined(S_ISFIFO)
345157184Sache#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
346157184Sache#endif
347157184Sache#if defined(S_IFSOCK) && defined(S_IFMT) && !defined(S_ISSOCK)
348157184Sache#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
349157184Sache#endif
350157184Sache#if defined(S_IFCHR) && defined(S_IFMT) && !defined(S_ISCHR)
351157184Sache#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
352157184Sache#endif
353157184Sache#if defined(S_IFDIR) && defined(S_IFMT) && !defined(S_ISDIR)
354157184Sache#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
355157184Sache#endif
356157184Sache#if defined(S_IFLNK) && defined(S_IFMT) && !defined(S_ISLNK)
357157184Sache#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
358157184Sache#endif
359157184Sache
360157184Sache/*
361157184Sache * SunOS 4.1.3: `man 2V open' has only one line that mentions O_NOBLOCK:
362157184Sache *
363157184Sache *     O_NONBLOCK     Same as O_NDELAY above.
364157184Sache *
365157184Sache * on the very same SunOS 4.1.3, I traced the open system call and found
366157184Sache * that an open("/dev/ttyy08", O_RDWR|O_NONBLOCK|O_NOCTTY) was blocked,
367157184Sache * whereas open("/dev/ttyy08", O_RDWR|O_NDELAY  |O_NOCTTY) went through.
368157184Sache *
369157184Sache * For this simple reason I now favour O_NDELAY. jw. 4.5.95
370157184Sache */
371157184Sache#if defined(sun) && !defined(SVR4)
372157184Sache# undef O_NONBLOCK
373157184Sache#endif
374157184Sache
375157184Sache#if !defined(O_NONBLOCK) && defined(O_NDELAY)
376157184Sache# define O_NONBLOCK O_NDELAY
377157184Sache#endif
378157184Sache
379157184Sache#if !defined(FNBLOCK) && defined(FNONBLOCK)
380157184Sache# define FNBLOCK FNONBLOCK
381157184Sache#endif
382157184Sache#if !defined(FNBLOCK) && defined(FNDELAY)
383157184Sache# define FNBLOCK FNDELAY
384157184Sache#endif
385157184Sache#if !defined(FNBLOCK) && defined(O_NONBLOCK)
386157184Sache# define FNBLOCK O_NONBLOCK
387157184Sache#endif
388157184Sache
389157184Sache#ifndef POSIX
390157184Sache#undef mkfifo
391157184Sache#define mkfifo(n,m) mknod(n,S_IFIFO|(m),0)
392157184Sache#endif
393157184Sache
394157184Sache#if !defined(HAVE_LSTAT) && !defined(lstat)
395157184Sache# define lstat stat
396157184Sache#endif
397157184Sache
398157184Sache/*****************************************************************
399157184Sache *    signal handling
400157184Sache */
401157184Sache
402157184Sache#ifdef SIGVOID
403157184Sache# define SIGRETURN
404157184Sache# define sigret_t void
405157184Sache#else
406157184Sache# define SIGRETURN return 0;
407157184Sache# define sigret_t int
408157184Sache#endif
409157184Sache
410157184Sache/* Geeeee, reverse it? */
411157184Sache#if defined(SVR4) || (defined(SYSV) && defined(ISC)) || defined(_AIX) || defined(linux) || defined(ultrix) || defined(__386BSD__) || defined(__bsdi__) || defined(POSIX) || defined(NeXT)
412157184Sache# define SIGHASARG
413157184Sache#endif
414157184Sache
415157184Sache#ifdef SIGHASARG
416157184Sache# define SIGPROTOARG   (int)
417157184Sache# define SIGDEFARG     (sigsig) int sigsig;
418157184Sache# define SIGARG        0
419157184Sache#else
420157184Sache# define SIGPROTOARG   (void)
421157184Sache# define SIGDEFARG     ()
422157184Sache# define SIGARG
423157184Sache#endif
424157184Sache
425157184Sache#ifndef SIGCHLD
426157184Sache#define SIGCHLD SIGCLD
427157184Sache#endif
428157184Sache
429157184Sache#if defined(POSIX) || defined(hpux)
430157184Sache# define signal xsignal
431157184Sache#else
432157184Sache# ifdef USESIGSET
433157184Sache#  define signal sigset
434157184Sache# endif /* USESIGSET */
435157184Sache#endif
436157184Sache
437157184Sache/* used in screen.c and attacher.c */
438157184Sache#ifndef NSIG		/* kbeal needs these w/o SYSV */
439157184Sache# define NSIG 32
440157184Sache#endif /* !NSIG */
441157184Sache
442157184Sache
443157184Sache/*****************************************************************
444157184Sache *    Wait stuff
445157184Sache */
446157184Sache
447157184Sache#if (!defined(sysV68) && !defined(M_XENIX)) || defined(NeXT) || defined(M_UNIX)
448157184Sache# include <sys/wait.h>
449157184Sache#endif
450157184Sache
451157184Sache#ifndef WTERMSIG
452157184Sache# ifndef BSDWAIT /* if wait is NOT a union: */
453157184Sache#  define WTERMSIG(status) (status & 0177)
454157184Sache# else
455157184Sache#  define WTERMSIG(status) status.w_T.w_Termsig
456157184Sache# endif
457157184Sache#endif
458157184Sache
459157184Sache#ifndef WSTOPSIG
460157184Sache# ifndef BSDWAIT /* if wait is NOT a union: */
461157184Sache#  define WSTOPSIG(status) ((status >> 8) & 0377)
462157184Sache# else
463157184Sache#  define WSTOPSIG(status) status.w_S.w_Stopsig
464157184Sache# endif
465157184Sache#endif
466157184Sache
467157184Sache/* NET-2 uses WCOREDUMP */
468157184Sache#if defined(WCOREDUMP) && !defined(WIFCORESIG)
469157184Sache# define WIFCORESIG(status) WCOREDUMP(status)
470157184Sache#endif
471157184Sache
472157184Sache#ifndef WIFCORESIG
473157184Sache# ifndef BSDWAIT /* if wait is NOT a union: */
474157184Sache#  define WIFCORESIG(status) (status & 0200)
475157184Sache# else
476157184Sache#  define WIFCORESIG(status) status.w_T.w_Coredump
477157184Sache# endif
478157184Sache#endif
479157184Sache
480157184Sache#ifndef WEXITSTATUS
481157184Sache# ifndef BSDWAIT /* if wait is NOT a union: */
482157184Sache#  define WEXITSTATUS(status) ((status >> 8) & 0377)
483157184Sache# else
484157184Sache#  define WEXITSTATUS(status) status.w_T.w_Retcode
485157184Sache# endif
486157184Sache#endif
487157184Sache
488157184Sache
489157184Sache/*****************************************************************
490157184Sache *    select stuff
491157184Sache */
492157184Sache
493157184Sache#if defined(M_XENIX) || defined(M_UNIX) || defined(_SEQUENT_)
494157184Sache#include <sys/select.h>		/* for timeval + FD... */
495157184Sache#endif
496157184Sache
497157184Sache/*
498157184Sache * SunOS 3.5 - Tom Schmidt - Micron Semiconductor, Inc - 27-Jul-93
499157184Sache * tschmidt@vax.micron.com
500157184Sache */
501157184Sache#ifndef FD_SET
502157184Sache# ifndef SUNOS3
503157184Sachetypedef struct fd_set { int fds_bits[1]; } fd_set;
504157184Sache# endif
505157184Sache# define FD_ZERO(fd) ((fd)->fds_bits[0] = 0)
506157184Sache# define FD_SET(b, fd) ((fd)->fds_bits[0] |= 1 << (b))
507157184Sache# define FD_ISSET(b, fd) ((fd)->fds_bits[0] & 1 << (b))
508157184Sache# define FD_SETSIZE 32
509157184Sache#endif
510157184Sache
511157184Sache
512157184Sache/*****************************************************************
513157184Sache *    user defineable stuff
514157184Sache */
515157184Sache
516157184Sache#ifndef TERMCAP_BUFSIZE
517157184Sache# define TERMCAP_BUFSIZE 2048
518157184Sache#endif
519157184Sache
520157184Sache#ifndef MAXPATHLEN
521157184Sache# define MAXPATHLEN 1024
522157184Sache#endif
523157184Sache
524157184Sache/*
525157184Sache * you may try to vary this value. Use low values if your (VMS) system
526157184Sache * tends to choke when pasting. Use high values if you want to test
527157184Sache * how many characters your pty's can buffer.
528157184Sache */
529157184Sache#define IOSIZE		4096
530157184Sache
531