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