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