home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku200.zip / ckutio.c < prev    next >
C/C++ Source or Header  |  2001-12-08  |  420KB  |  14,291 lines

  1. #ifdef aegis
  2. char *ckxv = "Aegis Communications support, 8.0.278, 8 Dec 2001";
  3. #else
  4. #ifdef Plan9
  5. char *ckxv = "Plan 9 Communications support, 8.0.278, 8 Dec 2001";
  6. #else
  7. char *ckxv = "UNIX Communications support, 8.0.278, 8 Dec 2001";
  8. #endif /* Plan9 */
  9. #endif /* aegis */
  10.  
  11. /*  C K U T I O  */
  12.  
  13. /* C-Kermit interrupt, communications control and I/O functions for UNIX */
  14.  
  15. /*
  16.   Author: Frank da Cruz (fdc@columbia.edu),
  17.   Columbia University Academic Information Systems, New York City.
  18.  
  19.   Copyright (C) 1985, 2001,
  20.     Trustees of Columbia University in the City of New York.
  21.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  22.     copyright text in the ckcmai.c module for disclaimer and permissions.
  23. */
  24.  
  25. /*
  26.   NOTE TO CONTRIBUTORS: This file, and all the other C-Kermit files, must be
  27.   compatible with C preprocessors that support only #ifdef, #else, #endif,
  28.   #define, and #undef.  Please do not use #if, logical operators, or other
  29.   preprocessor features in any of the portable C-Kermit modules.  You can,
  30.   of course, use these constructions in platform-specific modules when they
  31.   are supported by all compilers/preprocessors that could be used on that
  32.   platform.
  33. */
  34.  
  35. extern int nettype;            /* Defined in ckcmai.c */
  36.  
  37. /* Includes */
  38.  
  39. #include "ckcsym.h"            /* This must go first   */
  40. #include "ckcdeb.h"            /* This must go second  */
  41.  
  42. #ifdef OSF13
  43. #ifdef CK_ANSIC
  44. #ifdef _NO_PROTO
  45. #undef _NO_PROTO
  46. #endif /* _NO_PROTO */
  47. #endif /* CK_ANSIC */
  48. #endif /* OSF13 */
  49.  
  50. #include <errno.h>            /* System error numbers */
  51.  
  52. #ifdef __386BSD__
  53. #define ENOTCONN 57
  54. #else
  55. #ifdef __bsdi__
  56. #define ENOTCONN 57
  57. #else
  58. #ifdef __FreeBSD__
  59. #define ENOTCONN 57
  60. #endif /* __FreeBSD__ */
  61. #endif /* __bsdi__ */
  62. #endif /* __386BSD__ */
  63.  
  64. #ifdef SCO_OSR504
  65. #define NBBY 8
  66. #endif /* SCO_OSR504 */
  67.  
  68. #ifdef Plan9
  69. #define SELECT
  70. #include <sys/time.h>
  71. #include <select.h>
  72. #define FD_SETSIZE (3 * sizeof(long) * 8)
  73. static struct timeval tv;
  74. #endif /* Plan9 */
  75.  
  76. #ifdef CLIX
  77. #include <sys/time.h>
  78. #endif /* CLIX */
  79.  
  80. #include "ckcnet.h"            /* Symbols for network types. */
  81. #ifdef CK_SSL
  82. #include "ck_ssl.h"
  83. #endif /* CK_SSL */
  84.  
  85. /*
  86.   The directory-related includes are here because we need to test some
  87.   file-system-related symbols to find out which system we're being compiled
  88.   under.  For example, MAXNAMLEN is defined in BSD4.2 but not 4.1.
  89. */
  90. #ifdef SDIRENT                /* Directory bits... */
  91. #define DIRENT
  92. #endif /* SDIRENT */
  93.  
  94. #ifdef XNDIR
  95. #include <sys/ndir.h>
  96. #else /* !XNDIR */
  97. #ifdef NDIR
  98. #include <ndir.h>
  99. #else /* !NDIR, !XNDIR */
  100. #ifdef RTU
  101. #include "/usr/lib/ndir.h"
  102. #else /* !RTU, !NDIR, !XNDIR */
  103. #ifdef DIRENT
  104. #ifdef SDIRENT
  105. #include <sys/dirent.h>
  106. #else
  107. #include <dirent.h>
  108. #endif /* SDIRENT */
  109. #else /* !RTU, !NDIR, !XNDIR, !DIRENT, i.e. all others */
  110. #include <sys/dir.h>
  111. #endif /* DIRENT */
  112. #endif /* RTU */
  113. #endif /* NDIR */
  114. #endif /* XNDIR */
  115.  
  116. #ifdef QNX
  117. #include <sys/dev.h>
  118. #endif /* QNX */
  119.  
  120. #ifdef HPUX5
  121. #ifndef TCPSOCKET
  122. /* I don't know why this is needed here since we never reference bzero(). */
  123. /* But without it C-Kermit won't link in an HP-UX 5.xx non-TCP build. */
  124. void
  125. bzero(s,n) char *s; int n; {
  126.     extern char * memset();
  127.     memset(s,0,n);
  128. }
  129. #endif /* TCPSOCKET */
  130. #endif /* HPUX5 */
  131.  
  132. /* Definition of HZ, used in msleep() */
  133.  
  134. #ifdef MIPS
  135. #define HZ ( 1000 / CLOCK_TICK )
  136. #else  /* MIPS */
  137. #ifdef ATTSV
  138. #ifndef NAP
  139. #ifdef TRS16
  140. #define HZ ( 1000 / CLOCK_TICK )
  141. #endif /* TRS16 */
  142. #ifdef NAPHACK
  143. #define nap(x) (void)syscall(3112, (x))
  144. #define NAP
  145. #endif /* NAPHACK */
  146. #endif /* NAP */
  147. #endif /* ATTSV */
  148. #endif /* MIPS */
  149.  
  150. #ifdef M_UNIX
  151. #undef NGROUPS_MAX        /* Prevent multiple definition warnings */
  152. #endif /* M_UNIX */
  153.  
  154. /*
  155.   NOTE: HP-UX 8.0 has a <sys/poll.h>, but there is no corresponding
  156.   library routine, so _poll comes up undefined at link time.
  157. */
  158. #ifdef CK_POLL
  159. #ifndef AIXRS            /* IBM AIX needs special handling */
  160. #include <poll.h>        /* "standard" (SVID) i/o multiplexing, etc */
  161. #else /* AIXRS */
  162. #ifdef SVR4            /* AIX 3.2 is like SVID... */
  163. #include <poll.h>
  164. #else                /* But AIX 3.1 is not ... */
  165. #include <sys/poll.h>        /* The include file is in include/sys */
  166. #define events reqevents    /* And it does not map IBM-specific member */
  167. #define revents rtnevents    /* names to the System V equivalents */
  168. #endif /* SVR4 */
  169. #endif /* AIXRS */
  170. #endif /* CK_POLL */
  171.  
  172. #include <signal.h>                     /* Signals */
  173.  
  174. /* For setjmp and longjmp */
  175.  
  176. #ifndef ZILOG
  177. #include <setjmp.h>
  178. #else
  179. #include <setret.h>
  180. #endif /* ZILOG */
  181.  
  182. /*
  183.   The following test differentiates between 4.1 BSD and 4.2 & later.
  184.   If you have a 4.1BSD system with the DIRENT library, this test could
  185.   mistakenly diagnose 4.2BSD and then later enable the use of system calls
  186.   that aren't defined.  If indeed there are such systems, we can use some
  187.   other way of testing for 4.1BSD, or add yet another compile-time switch.
  188. */
  189. #ifdef BSD4
  190. #ifdef MAXNAMLEN
  191. #ifndef FT21                /* Except for Fortune. */
  192. #ifndef FT18
  193. #ifndef BELLV10                /* And Bell Labs Research UNIX V10 */
  194. #define BSD42
  195. #endif /* BELLV10 */
  196. #endif /* FT18 */
  197. #endif /* FT21 */
  198. #endif /* MAXNAMLEN */
  199. #endif /* BSD4 */
  200. /*
  201.   Minix 2.0 support added by Terry McConnell,
  202.   Syracuse University <tmc@barnyard.syr.edu>
  203.   No more sgtty interface, posix compliant.
  204. */
  205. #ifdef MINIX2
  206. #define _MINIX   /* Needed for some Minix header files */
  207. #undef MINIX     /* Old minix 1.0: used sgtty interface */
  208. #define BSD44ORPOSIX
  209. #define SVORPOSIX
  210. #define DCLTIMEVAL
  211. #define NOFILEH
  212. #include <sys/types.h>
  213. #include <sys/ioctl.h>
  214. #include <termios.h>
  215. #include <limits.h>
  216. #undef TIOCGETC    /* defined in sys/ioctl.h, but not really supported */
  217. #define TANDEM 0
  218. #endif /* MINIX2 */
  219.  
  220. /*
  221.  MINIX 1.0 support added by Charles Hedrick,
  222.  Rutgers University <hedrick@aramis.rutgers.edu>.
  223.  MINIX also has V7 enabled.
  224. */
  225. #ifdef MINIX
  226. #define TANDEM 0
  227. #define MYREAD
  228. #define NOSYSIOCTLH
  229. #include <limits.h>
  230. #endif /* MINIX */
  231.  
  232. #ifdef CK_REDIR        /* <sys/wait.h> needed only for REDIRECT command. */
  233. /*
  234.   If anybody can figure out how to make this work with NeXTSTEP, be
  235.   my guest!  (NeXTBlah/NeXTBlah/bsd/sys/wait.h does not define WEXITSTATUS)
  236. */
  237. #ifndef CK_WAIT_H            /* If wait.h not already included... */
  238. #ifdef OSF                /* force OSF to select POSIX wait */
  239. #ifdef _BSD                /* instead of BSD (see ckcdeb.h) */
  240. #define CK_OSF_BSD
  241. #undef _BSD
  242. #endif /* _BSD */
  243. #endif /* OSF */
  244. #include <sys/wait.h>            /* Include it */
  245. #ifdef OSF
  246. #ifdef CK_OSF_BSD
  247. #define _BSD                /* Restore it */
  248. #undef CK_OSF_BSD
  249. #endif /* CK_OSF_BSD */
  250. #endif /* OSF */
  251. #endif /* CK_WAIT_H */
  252. #endif /* CK_REDIR */
  253.  
  254. #include "ckuver.h"            /* Version herald */
  255. char *ckxsys = HERALD;
  256.  
  257. #ifdef CK_UTSNAME
  258. #include <sys/utsname.h>
  259.  
  260. #ifdef TRU64                /* Tru64 UNIX 4.0 and later */
  261. /* Verified on Tru64 4.0F - might break on 4.0E or earlier */
  262. #include <sys/sysinfo.h>        /* (don't know about OSF/1 or DU) */
  263. #include <machine/hal_sysinfo.h>
  264. #endif /* TRU64 */
  265.  
  266. #ifdef SOLARIS25            /* Solaris 2.5 and later */
  267. #include <sys/systeminfo.h>        /* (don't know about earlier ones) */
  268. #endif /* SOLARIS25 */
  269.  
  270. #ifdef UW7
  271. #ifndef SYS_NMLN
  272. #define SYS_NMLN 257
  273. #endif /* NMLN */
  274. #endif /* UW7 */
  275. #ifdef HPUX9PLUS
  276. static int hpis800 = 0;
  277. #endif /* HPUX9PLUS */
  278. #ifdef SYS_NMLN
  279. #define CK_SYSNMLN SYS_NMLN
  280. #else
  281. #ifdef _SYS_NMLN
  282. #define CK_SYSNMLN _SYS_NMLN
  283. #else
  284. #ifdef UTSLEN
  285. #define CK_SYSNMLN UTSLEN
  286. #else
  287. #define CK_SYSNMLN 31
  288. #endif /* UTSLEN */
  289. #endif /* _SYS_NMLN */
  290. #endif /* SYS_NMLN */
  291. char unm_mch[CK_SYSNMLN+1] = { '\0', '\0' };
  292. char unm_mod[CK_SYSNMLN+1] = { '\0', '\0' };
  293. char unm_nam[CK_SYSNMLN+1] = { '\0', '\0' };
  294. char unm_rel[CK_SYSNMLN+1] = { '\0', '\0' };
  295. char unm_ver[CK_SYSNMLN+1] = { '\0', '\0' };
  296. #endif /* CK_UTSNAME */
  297.  
  298. #ifdef CIE
  299. #include <stat.h>            /* For chasing symlinks, etc. */
  300. #else
  301. #include <sys/stat.h>
  302. #endif /* CIE */
  303.  
  304. /* UUCP lockfile material... */
  305.  
  306. #ifndef NOUUCP
  307. #ifdef USETTYLOCK
  308. #ifdef USE_UU_LOCK
  309. #ifdef __FreeBSD__
  310. #include <libutil.h>            /* FreeBSD */
  311. #else
  312. #include <util.h>            /* OpenBSD */
  313. #endif /* __FreeBSD */
  314. #endif /* USE_UU_LOCK */
  315. #else  /* USETTYLOCK */
  316.  
  317. /* Name of UUCP tty device lockfile */
  318.  
  319. #ifdef LINUXFSSTND
  320. #ifndef HDBUUCP
  321. #define HDBUUCP
  322. #endif /* HDBUUCP */
  323. #endif /* LINUXFSSTND */
  324.  
  325. #ifdef ACUCNTRL
  326. #define LCKDIR
  327. #endif /* ACUCNTRL */
  328.  
  329. /*
  330.   PIDSTRING means use ASCII string to represent pid in lockfile.
  331. */
  332. #ifndef PIDSTRING
  333. #ifdef HDBUUCP
  334. #define PIDSTRING
  335. #else
  336. #ifdef BSD44
  337. #define PIDSTRING
  338. #else
  339. #ifdef RTAIX
  340. #define PIDSTRING
  341. #else
  342. #ifdef AIXRS
  343. #define PIDSTRING
  344. #else
  345. #ifdef COHERENT
  346. #define PIDSTRING
  347. #endif /* COHERENT */
  348. #endif /* AIXRS */
  349. #endif /* RTAIX */
  350. #endif /* BSD44 */
  351. #endif /* HDBUUCP */
  352. #endif /* PIDSTRING */
  353.  
  354. /* Now the PIDSTRING exceptions... */
  355.  
  356. #ifdef PIDSTRING
  357. #ifdef HPUX
  358. #undef PIDSTRING
  359. #endif /* HPUX */
  360. #endif /* PIDSTRING */
  361.  
  362. #ifdef __bsdi__                /* BSDI (at least thru 1.1) */
  363. #ifdef PIDSTRING
  364. #undef PIDSTRING
  365. #endif /* PIDSTRING */
  366. #endif /* __bsdi__ */
  367.  
  368. #ifdef OSF32                /* Digital UNIX (OSF/1) 3.2 */
  369. #ifdef PIDSTRING
  370. #undef PIDSTRING
  371. #endif /* PIDSTRING */
  372. #endif /* OSF32 */
  373.  
  374. /*
  375.   LOCK_DIR is the name of the lockfile directory.
  376.   If LOCK_DIR is already defined (e.g. on command line), we don't change it.
  377. */
  378.  
  379. #ifndef LOCK_DIR
  380. #ifdef BSD44
  381. #ifdef __386BSD__
  382. #define LOCK_DIR "/var/spool/lock"
  383. #else
  384. #ifdef __FreeBSD__
  385. #define LOCK_DIR "/var/spool/lock"
  386. #else
  387. #ifdef __NetBSD__
  388. #define LOCK_DIR "/var/spool/lock"
  389. #else
  390. #ifdef __OpenBSD__
  391. #define LOCK_DIR "/var/spool/lock"
  392. #else
  393. /* So which ones is this for? */
  394. /* Probably original 4.4BSD on Vangogh */
  395. /* Plus who knows about Mac OS X... It doesn't even have a cu program */
  396. #define LOCK_DIR "/var/spool/uucp"
  397. #endif /* __OpenBSD__ */
  398. #endif /* __NetBSD__ */
  399. #endif /* __FreeBSD__ */
  400. #endif /* __386BSD__ */
  401. #else
  402. #ifdef DGUX430
  403. #define LOCK_DIR "/var/spool/locks"
  404. #else
  405. #ifdef HPUX10
  406. #define LOCK_DIR "/var/spool/locks"
  407. #else
  408. #ifdef RTAIX                /* IBM RT PC AIX 2.2.1 */
  409. #define LOCK_DIR "/etc/locks"
  410. #else
  411. #ifdef AIXRS
  412. #define LOCK_DIR "/etc/locks"
  413. #else
  414. #ifdef ISIII
  415. #define LOCK_DIR "/etc/locks"
  416. #else
  417. #ifdef HDBUUCP
  418. #ifdef M_SYS5
  419. #define LOCK_DIR "/usr/spool/uucp"
  420. #else
  421. #ifdef M_UNIX
  422. #define LOCK_DIR "/usr/spool/uucp"
  423. #else
  424. #ifdef SVR4
  425. #define LOCK_DIR "/var/spool/locks"
  426. #else
  427. #ifdef SUNOS4
  428. #define LOCK_DIR "/var/spool/locks"
  429. #else
  430. #ifdef LINUXFSSTND
  431. #define LOCK_DIR "/var/lock";
  432. #else
  433. #define LOCK_DIR "/usr/spool/locks"
  434. #endif /* LINUXFSSTND */
  435. #endif /* SUNOS4 */
  436. #endif /* SVR4 */
  437. #endif /* M_UNIX */
  438. #endif /* M_SYS5 */
  439. #else
  440. #ifdef LCKDIR
  441. #define LOCK_DIR "/usr/spool/uucp/LCK"
  442. #else
  443. #ifdef COHERENT
  444. #define LOCK_DIR "/usr/spool/uucp"
  445. #else
  446. #define LOCK_DIR "/usr/spool/uucp"
  447. #endif /* COHERENT */
  448. #endif /* LCKDIR */
  449. #endif /* HDBUUCP */
  450. #endif /* ISIII */
  451. #endif /* AIXRS */
  452. #endif /* RTAIX */
  453. #endif /* HPUX10 */
  454. #endif /* DGUX430 */
  455. #endif /* BSD44 */
  456. #endif /* !LOCK_DIR (outside ifndef) */
  457.  
  458. #ifdef OSF2                /* OSF/1 2.0 or later */
  459. #ifdef LOCK_DIR                /* (maybe 1.x too, who knows...) */
  460. #undef LOCK_DIR
  461. #define LOCK_DIR "/var/spool/locks"
  462. #endif /* LOCK_DIR */
  463. #endif /* OSF2 */
  464.  
  465. #ifdef COMMENT
  466. /* Sorry no more lockf() -- we lock first and THEN open the device. */
  467. #ifdef SVR4
  468. #ifndef BSD44
  469. #ifndef LOCKF
  470. #define LOCKF                /* Use lockf() on tty device in SVR4 */
  471. #endif /* LOCKF */
  472. #endif /* BSD44 */
  473. #endif /* SVR4 */
  474. #endif /* COMMENT */
  475.  
  476. #ifdef NOLOCKF                /* But NOLOCKF cancels LOCKF */
  477. #ifdef LOCKF
  478. #undef LOCKF
  479. #endif /* LOCKF */
  480. #endif /* NOLOCKF */
  481.  
  482. /* More about this below... */
  483.  
  484. #endif /* USETTYLOCK */
  485. #endif /* NOUUCP */
  486.  
  487. /*
  488.   MYREAD means use our internally defined nonblocking buffered read routine.
  489. */
  490. #ifdef ATTSV
  491. #define MYREAD
  492. #endif /* ATTSV */
  493.  
  494. #ifdef ATT7300
  495. #ifndef MYREAD
  496. #define MYREAD
  497. #endif /* MYREAD */
  498. /* bits for attmodem: internal modem in use, restart getty */
  499. #define ISMODEM 1
  500. #define DOGETY 512
  501. #endif  /* ATT7300 */
  502.  
  503. #ifdef BSD42
  504. #define MYREAD
  505. #endif /* BSD42 */
  506.  
  507. #ifdef POSIX
  508. #define MYREAD
  509. #endif /* POSIX */
  510. #ifdef __bsdi__
  511. #ifndef O_NDELAY
  512. #define O_NDELAY O_NONBLOCK
  513. #endif /* O_NDELAY */
  514. #endif /* __bsdi__ */
  515.  
  516. /*
  517.  Variables available to outside world:
  518.  
  519.    dftty  -- Pointer to default tty name string, like "/dev/tty".
  520.    dfloc  -- 0 if dftty is console, 1 if external line.
  521.    dfprty -- Default parity
  522.    dfflow -- Default flow control
  523.    ckxech -- Flag for who echoes console typein:
  524.      1 - The program (system echo is turned off)
  525.      0 - The system (or front end, or terminal).
  526.    functions that want to do their own echoing should check this flag
  527.    before doing so.
  528.  
  529.    flfnam  -- Name of lock file, including its path, e.g.,
  530.                 "/usr/spool/uucp/LCK..cul0" or "/etc/locks/tty77"
  531.    lkflfn  -- Name of link to lock file, including its paths
  532.    haslock -- Flag set if this kermit established a uucp lock.
  533.    lockpid -- PID of other process that has desired line open, as string.
  534.    backgrd -- Flag indicating program executing in background ( & on
  535.                 end of shell command). Used to ignore INT and QUIT signals.
  536.    rtu_bug -- Set by stptrap().  RTU treats ^Z as EOF (but only when we handle
  537.                 SIGTSTP)
  538.  
  539.  Functions for assigned communication line (either external or console tty):
  540.  
  541.    sysinit()               -- System dependent program initialization
  542.    syscleanup()            -- System dependent program shutdown
  543.    ttopen(ttname,local,mdmtyp,timo) -- Open the named tty for exclusive access.
  544.    ttclos()                -- Close & reset the tty, releasing any access lock.
  545.    ttsspd(cps)             -- Set the transmission speed of the tty.
  546.    ttgspd()                -- Get (read) the the transmission speed of the tty.
  547.    ttpkt(speed,flow,parity) -- Put the tty in packet mode and set the speed.
  548.    ttvt(speed,flow)        -- Put the tty in virtual terminal mode.
  549.                                 or in DIALING or CONNECTED modem control state.
  550.    ttres()                 -- Restore original tty modes.
  551.    ttscarr(carrier)        -- Set carrier control mode, on/off/auto.
  552.    ttinl(dest,max,timo)    -- Timed read line from the tty.
  553.    ttinc(timo)             -- Timed read character from tty.
  554.    myread()                -- Raw mode bulk buffer read, gives subsequent
  555.                                 chars one at a time and simulates FIONREAD.
  556.    myunrd(c)               -- Places c back in buffer to be read (one only)
  557.    ttchk()                 -- See how many characters in tty input buffer.
  558.    ttxin(n,buf)            -- Read n characters from tty (untimed).
  559.    ttol(string,length)     -- Write a string to the tty.
  560.    ttoc(c)                 -- Write a character to the tty.
  561.    ttflui()                -- Flush tty input buffer.
  562.    ttsndb()                -- Send BREAK signal.
  563.    ttsndlb()               -- Send Long BREAK signal.
  564.  
  565.    ttlock(ttname)          -- "Lock" tty device against uucp collisions.
  566.    ttunlck()               -- Unlock tty device.
  567.  
  568.                               For ATT7300/Unix PC, System V:
  569.    attdial(ttname,speed,telnbr) -- dials ATT7300/Unix PC internal modem
  570.    offgetty(ttname)        -- Turns off getty(1m) for comms line
  571.    ongetty(ttname)         -- Restores getty() to comms line
  572. */
  573.  
  574. /*
  575. Functions for console terminal:
  576.  
  577.    congm()   -- Get console terminal modes.
  578.    concb(esc) -- Put the console in single-character wakeup mode with no echo.
  579.    conbin(esc) -- Put the console in binary (raw) mode.
  580.    conres()  -- Restore the console to mode obtained by congm().
  581.    conoc(c)  -- Unbuffered output, one character to console.
  582.    conol(s)  -- Unbuffered output, null-terminated string to the console.
  583.    conola(s) -- Unbuffered output, array of strings to the console.
  584.    conxo(n,s) -- Unbuffered output, n characters to the console.
  585.    conchk()  -- Check if characters available at console (bsd 4.2).
  586.                 Check if escape char (^\) typed at console (System III/V).
  587.    coninc(timo)  -- Timed get a character from the console.
  588.    congks(timo)  -- Timed get keyboard scan code.
  589.    conint()  -- Enable terminal interrupts on the console if not background.
  590.    connoi()  -- Disable terminal interrupts on the console if not background.
  591.  
  592. Time functions
  593.  
  594.    msleep(m) -- Millisecond sleep
  595.    ztime(&s) -- Return pointer to date/time string
  596.    rtimer() --  Reset timer
  597.    gtimer()  -- Get elapsed time since last call to rtimer()
  598. */
  599.  
  600. /* Conditional Includes */
  601.  
  602. /* Whether to include <sys/file.h> */
  603.  
  604. #ifdef RTU                /* RTU doesn't */
  605. #define NOFILEH
  606. #endif /* RTU */
  607.  
  608. #ifdef CIE                /* CIE does. */
  609. #undef NOFILEH
  610. #endif /* CIE */
  611.  
  612. #ifdef BSD41                /* 4.1 BSD doesn't */
  613. #define NOFILEH
  614. #endif /* BSD41 */
  615.  
  616. #ifdef is68k                /* Integrated Solutions 68000 UNIX  */
  617. #define NOFILEH                /* e.g. on Plexux P60 and Sun-1 */
  618. #endif /* is68k */
  619.  
  620. #ifdef MINIX                /* MINIX */
  621. #define NOFILEH
  622. #endif /* MINIX */
  623.  
  624. #ifdef COHERENT                /* Coherent */
  625. #define NOFILEH
  626. #endif /* COHERENT */
  627.  
  628. #ifndef NOFILEH                /* Now include if selected. */
  629. #include <sys/file.h>
  630. #endif /* NOFILEH */
  631.  
  632. /* POSIX */
  633.  
  634. #ifdef BSD44ORPOSIX            /* POSIX uses termios.h */
  635. #define TERMIOS
  636. #ifdef __bsdi__
  637. #ifdef POSIX
  638. #undef _POSIX_SOURCE            /* Get extra stuff from termios.h */
  639. #endif /* POSIX */
  640. #endif /* __bsdi__ */
  641. #include <termios.h>
  642. #ifdef LINUX
  643. #include <sys/ioctl.h>
  644. #endif /* LINUX */
  645. #ifdef QNX16
  646. #include <ioctl.h>
  647. #else
  648. #ifdef QNX6
  649. #include <ioctl.h>
  650. #endif /* QNX6 */
  651. #endif /* QNX16 */
  652. #ifdef __bsdi__
  653. #ifdef POSIX
  654. #define _POSIX_SOURCE
  655. #endif /* POSIX */
  656. #endif /* __bsdi__ */
  657. #ifndef BSD44                /* Really POSIX */
  658. #ifndef CK_QNX32            /* was CK_QNX32 */
  659. #define NOSYSIOCTLH            /* No ioctl's allowed. */
  660. #undef ultrix                /* Turn off any ultrix features. */
  661. #endif /* CK_QNX32 */
  662. #endif /* BSD44 */
  663. #endif /* POSIX */
  664.  
  665. /* System III, System V */
  666.  
  667. #ifdef ATTSV
  668. #ifndef BSD44
  669. #ifndef POSIX
  670. #include <termio.h>
  671. #endif /* POSIX */
  672. #endif /* BSD44 */
  673. #ifdef TERMIOX
  674. /* Need this for termiox structure, RTS/CTS and DTR/CD flow control */
  675. #include <termiox.h>
  676.   struct termiox rctsx;
  677. #else
  678. #ifdef STERMIOX
  679. #ifdef SCO_OSR504
  680. /* Sorry, this is truly disgusting but it's SCO's fault. */
  681. #ifndef _SVID3
  682. #define _CK_SVID3_X
  683. #define _SVID3
  684. #endif /* _SVID3 */
  685. #endif /* SCO_OSR504 */
  686. #include <sys/termiox.h>
  687.   struct termiox rctsx;
  688. #ifdef CK_SVID3_X
  689. #undef _SVID3
  690. #undef CK_SVID3_X
  691. #endif /* CK_SVID3_X */
  692. #endif /* STERMIOX */
  693. #endif /* TERMIOX */
  694. #endif /* ATTSV */
  695.  
  696. #ifdef COHERENT            /* Use termio.h, not sgtty.h for Coherent */
  697. #include <termio.h>
  698. #endif /* COHERENT */
  699.  
  700. #ifdef MINIX                /* MINIX uses ioctl's */
  701. #define NOSYSIOCTLH            /* but has no <sys/ioctl.h> */
  702. #endif /* MINIX */
  703.  
  704. /* Others */
  705.  
  706. #ifndef NOSYSIOCTLH            /* Others use ioctl() */
  707. #ifdef SUN4S5
  708. /*
  709.   This is to get rid of cpp warning messages that occur because all of
  710.   these symbols are defined by both termios.h and ioctl.h on the SUN.
  711. */
  712. #undef ECHO
  713. #undef NL0
  714. #undef NL1
  715. #undef TAB0
  716. #undef TAB1
  717. #undef TAB2
  718. #undef XTABS
  719. #undef CR0
  720. #undef CR1
  721. #undef CR2
  722. #undef CR3
  723. #undef FF0
  724. #undef FF1
  725. #undef BS0
  726. #undef BS1
  727. #undef TOSTOP
  728. #undef FLUSHO
  729. #undef PENDIN
  730. #undef NOFLSH
  731. #endif /* SUN4S5 */
  732. #include <sys/ioctl.h>
  733. #endif /* NOSYSIOCTLH */
  734. /*
  735.   We really, really, REALLY want FIONREAD, because it is the only way to find
  736.   out not just *if* stuff is waiting to be read, but how much, which is
  737.   critical to our sliding-window and streaming procedures, not to mention
  738.   efficiency of CONNECT, etc.
  739. */
  740. #ifdef BELLV10
  741. #include <sys/filio.h>            /* For FIONREAD */
  742. #ifdef FIONREAD
  743. #define MYREAD
  744. #endif /* MYREAD */
  745. #endif /* BELLV10 */
  746.  
  747. #ifndef FIONREAD
  748. /* It wasn't found in ioctl.h or term*.h - try these places: */
  749. #ifdef UNIXWARE
  750. #include <sys/filio.h>
  751. #else
  752. #ifdef SOLARIS
  753. #include <sys/filio.h>
  754. #endif /* SOLARIS */
  755. #endif /* UNIXWARE */
  756. #endif /* FIONREAD */
  757.  
  758. #ifdef XENIX /* Was M_UNIX but XENIX implies M_UNIX and applies to XENIX too */
  759. /*
  760.   <sys/socket.h> included above via "ckcnet.h" defines FIONREAD as
  761.   something.  Due to this, in_chk() uses the FIONREAD instead of RDCHK
  762.   and the hot keys during file transfer (X to cancel file etc) do not
  763.   work because FIONREAD doesn't work even though it is defined.
  764.  
  765.   NOTE: This might also be true elsewhere.
  766. */
  767. #ifdef FIONREAD
  768. #undef FIONREAD
  769. #endif /* FIONREAD */
  770. #endif /* XENIX */
  771.  
  772. #ifdef CK_SCOV5                /* Ditto for SCO OpenServer 5.0 */
  773. #ifdef FIONREAD
  774. #undef FIONREAD
  775. #endif /* FIONREAD */
  776. #endif /* XENIX */
  777.  
  778. /* Whether to include <fcntl.h> */
  779.  
  780. #ifndef is68k                /* Only a few don't have this one. */
  781. #ifndef BSD41
  782. #ifndef FT21
  783. #ifndef FT18
  784. #ifndef COHERENT
  785. #include <fcntl.h>
  786. #endif /* COHERENT */
  787. #endif /* FT18 */
  788. #endif /* FT21 */
  789. #endif /* BSD41 */
  790. #endif /* not is68k */
  791.  
  792. #ifdef COHERENT
  793. #ifdef _I386
  794. #include <fcntl.h>
  795. #else
  796. #include <sys/fcntl.h>
  797. #endif /* _I386 */
  798. #endif /* COHERENT */
  799.  
  800. #ifdef ATT7300                /* Unix PC, internal modem dialer */
  801. #include <sys/phone.h>
  802. #endif /* ATT7300 */
  803.  
  804. #ifdef HPUX                /* HP-UX variations. */
  805. #define HPUXJOBCTL
  806. #include <sys/modem.h>            /* HP-UX modem signals */
  807. #ifdef hp9000s500            /* Model 500 */
  808. #undef HPUXJOBCTL
  809. #endif /* hp9000s500 */
  810. #ifdef HPUXPRE65
  811. #undef HPUXJOBCTL
  812. typedef long mflag;
  813. #endif /* HPUXPRE65 */
  814. #ifdef HPUXJOBCTL
  815. #include <sys/bsdtty.h>            /* HP-UX Berkeley tty support */
  816. #endif /* HPUXJOBCTL */
  817. #endif /* HPUX */
  818.  
  819. /*
  820.   Which time.h files to include... See ckcdeb.h for defaults.
  821.   Note that 0, 1, 2, or all 3 of these can be included according to
  822.   the symbol definitions.
  823. */
  824. #ifndef NOTIMEH
  825. #ifdef TIMEH
  826. #include <time.h>
  827. #endif /* TIMEH */
  828. #endif /* NOTIMEH */
  829.  
  830. #ifndef NOSYSTIMEH
  831. #ifdef SYSTIMEH
  832. #include <sys/time.h>
  833. #endif /* SYSTIMEH */
  834. #endif /* NOSYSTIMEH */
  835.  
  836. #ifndef NOSYSTIMEBH
  837. #ifdef SYSTIMEBH
  838. #include <sys/timeb.h>
  839. #endif /* SYSTIMEBH */
  840. #endif /* NOSYSTIMEBH */
  841.  
  842. #ifndef NODCLTIMEVAL
  843. #ifdef DCLTIMEVAL
  844. /*
  845.   In certain POSIX builds (like Unixware 7), <[sys/]time.h> refuses to
  846.   define the structs we need to access the higher speeds, so we have to
  847.   do it ourselves.
  848. */
  849. struct timeval {
  850.     long tv_sec;
  851.     long tv_usec;
  852. };
  853. struct timezone {
  854.     int tz_minuteswest;
  855.     int tz_dsttime;
  856. };
  857. #endif /* DCLTIMEVAL */
  858. #endif /* NODCLTIMEVAL */
  859.  
  860. #ifdef __linux__
  861. /* THIS IS OBSOLETE since about Linux 0.92 */
  862. #ifdef OLINUXHISPEED
  863. #include <linux/serial.h>
  864. #endif /* OLINUXHISPEED */
  865. #ifdef __alpha__            /* Linux on DEC Alpha */
  866. #ifndef __GLIBC__            /* But not with glibc */
  867. #include <asm/termios.h>
  868. #endif /* __GLIBC__ */
  869. #endif /* __alpha__ */
  870. #endif /* __linux__ */
  871.  
  872. #ifdef NOIEXTEN                /* This is broken on some systems */
  873. #undef IEXTEN                /* like Convex/OS 9.1 */
  874. #endif /* NOIEXTEN */
  875. #ifndef IEXTEN                /* Turn off ^O/^V processing. */
  876. #define IEXTEN 0            /* Needed, at least, on BSDI. */
  877. #endif /* IEXTEN */
  878. /*
  879.   Pick up definitions needed for select() if we don't have them already.
  880.   Normally they come from <sys/types.h> but some systems get them from
  881.   <sys/select.h>...  Rather than hardwire all of them into the source, we
  882.   include it if SELECT_H is defined in compile-time CFLAGS.
  883. */
  884. #ifndef SCO_OSR504
  885. #ifdef SELECT_H
  886. #include <sys/select.h>
  887. #endif /* SELECT_H */
  888. #endif /* SCO_OSR504 */
  889.  
  890. #ifdef aegis
  891. #include "/sys/ins/base.ins.c"
  892. #include "/sys/ins/error.ins.c"
  893. #include "/sys/ins/ios.ins.c"
  894. #include "/sys/ins/sio.ins.c"
  895. #include "/sys/ins/pad.ins.c"
  896. #include "/sys/ins/time.ins.c"
  897. #include "/sys/ins/pfm.ins.c"
  898. #include "/sys/ins/pgm.ins.c"
  899. #include "/sys/ins/ec2.ins.c"
  900. #include "/sys/ins/type_uids.ins.c"
  901. #include <default_acl.h>
  902. #undef TIOCEXCL
  903. #undef FIONREAD
  904. #endif /* aegis */
  905.  
  906. #ifdef sxaE50                /* PFU Compact A SX/A TISP V10/L50 */
  907. #undef FIONREAD
  908. #endif /* sxaE50 */
  909.  
  910. /* The following #defines are catch-alls for those systems */
  911. /* that didn't have or couldn't find <file.h>... */
  912.  
  913. #ifndef FREAD
  914. #define FREAD 0x01
  915. #endif /* FREAD */
  916.  
  917. #ifndef FWRITE
  918. #define FWRITE 0x10
  919. #endif /* FWRITE */
  920.  
  921. #ifndef O_RDONLY
  922. #define O_RDONLY 000
  923. #endif /* O_RDONLY */
  924.  
  925. #ifdef SVORPOSIX
  926. /*
  927.   Modem signals are also forbidden in the POSIX world.  But some POSIX-based
  928.   platforms let us at them anyway if we know where to look.
  929. */
  930. #ifndef NEEDMDMDEFS
  931. /* Doesn't work for Linux */
  932. #ifdef UNIXWARE7
  933. #define NEEDMDMDEFS
  934. #endif /* UNIXWARE7 */
  935. #endif /* NEEDMDMDEFS */
  936.  
  937. #ifdef NEEDMDMDEFS
  938. #ifndef TIOCMGET
  939. #define TIOCMGET (('t'<<8)|29)
  940. #endif /* TIOCMGET */
  941.  
  942. #ifndef TIOCM_DTR
  943. #define TIOCM_DTR 0x0002
  944. #endif /* TIOCM_DTR */
  945. #ifndef TIOCM_RTS
  946. #define TIOCM_RTS 0x0004
  947. #endif /* TIOCM_RTS */
  948. #ifndef TIOCM_CTS
  949. #define TIOCM_CTS 0x0020
  950. #endif /* TIOCM_CTS */
  951. #ifndef TIOCM_CAR
  952. #define TIOCM_CAR 0x0040
  953. #endif /* TIOCM_CAR */
  954. #ifndef TIOCM_RNG
  955. #define TIOCM_RNG 0x0080
  956. #endif /* TIOCM_RNG */
  957. #ifndef TIOCM_DSR
  958. #define TIOCM_DSR 0x0100
  959. #endif /* TIOCM_DSR */
  960. #endif /* NEEDMDMDEFS */
  961. #endif /* SVORPOSIX */
  962.  
  963. /* Declarations */
  964.  
  965. #ifdef OXOS
  966. #undef TCGETA
  967. #undef TCSETA
  968. #undef TCSETAW
  969. #undef TCSETAF
  970. #define TCGETA TCGETS
  971. #define TCSETA TCSETS
  972. #define TCSETAW TCSETSW
  973. #define TCSETAF TCSETSF
  974. #define termio termios
  975. #endif /* OXOS */
  976.  
  977. #ifdef SVORPOSIX            /* AT&T Sys V or POSIX */
  978. #ifdef UNIXWAREPOSIX            /* UnixWare 7 POSIX build */
  979. /*
  980.   In Unixware POSIX builds, <[sys/]time.h> refuses to define the
  981.   structs we need to access the higher speeds, so we have to do it
  982.   ourselves.
  983. */
  984. struct timeval {
  985.     long tv_sec;
  986.     long tv_usec;
  987. };
  988. struct timezone {
  989.     int tz_minuteswest;
  990.     int tz_dsttime;
  991. };
  992. #endif /* UNIXWAREPOSIX */
  993. #endif /* SVORPOSIX */
  994.  
  995. #ifdef __GNUC__
  996. #ifdef XENIX
  997. /*
  998.   Because Xenix <time.h> doesn't declare time() if we're using gcc.
  999. */
  1000. time_t time();
  1001. #endif /* XENIX */
  1002. #endif /* __GNUC__ */
  1003.  
  1004. /* Special stuff for V7 input buffer peeking */
  1005.  
  1006. #ifdef  V7
  1007. int kmem[2] = { -1, -1};
  1008. char *initrawq(), *qaddr[2]={0,0};
  1009. #define CON 0
  1010. #define TTY 1
  1011. #endif /* V7 */
  1012.  
  1013. /* dftty is the device name of the default device for file transfer */
  1014. /* dfloc is 0 if dftty is the user's console terminal, 1 if an external line */
  1015.  
  1016. #ifdef BEOS
  1017.     char * dftty = NULL;
  1018.     char * dfmdm = "none";
  1019.     int dfloc = 0;                  /* that goes in local mode by default */
  1020. #else
  1021. #ifndef DFTTY
  1022. #ifdef PROVX1
  1023.     char *dftty = "/dev/com1.dout"; /* Only example so far of a system */
  1024.     char *dfmdm = "none";
  1025.     int dfloc = 1;                  /* that goes in local mode by default */
  1026. #else
  1027.     char *dftty = CTTNAM;               /* Remote by default, use normal */
  1028.     char *dfmdm = "none";
  1029.     int dfloc = 0;                      /* controlling terminal name. */
  1030. #endif /* PROVX1 */
  1031. #else
  1032.     char *dftty = DFTTY;        /* Default location specified on */
  1033.     char *dfmdm = "none";        /* command line. */
  1034.     int dfloc = 1;                      /* controlling terminal name. */
  1035. #endif /* DFTTY */
  1036. #endif /* BEOS */
  1037.  
  1038. #define CON_RES 0            /* Console state is "reset" */
  1039. #define CON_CB  1            /* Console state is CBREAK */
  1040. #define CON_BIN 2            /* Console state is binary */
  1041.     static int constate = CON_RES;
  1042.  
  1043. #define CONI_RES 0            /* Console interrupts are "reset" */
  1044. #define CONI_INT 1            /* Console intterupts are set */
  1045. #define CONI_NOI 2            /* Console intterupts are disabled */
  1046.     static int conistate = CONI_RES;
  1047.  
  1048. #ifdef CK_SMALL
  1049. #define CONBUFSIZ 15
  1050. #else
  1051. #define CONBUFSIZ 255
  1052. #endif /* CK_SMALL */
  1053.     static char conbuf[CONBUFSIZ];    /* Console readahead buffer */
  1054.     static int  conbufn = 0;        /* Chars in readahead buffer */
  1055.     static char *conbufp = conbuf;    /* Next char in readahead buffer */
  1056.  
  1057.     char cttnam[DEVNAMLEN+1] = { '\0', '\0' }; /* Determined at runtime */
  1058.  
  1059. #ifdef RTU
  1060.     int rtu_bug = 0;            /* set to 1 when returning from SIGTSTP */
  1061. #endif /* RTU */
  1062.  
  1063.     int dfprty = DEFPAR;                /* Default parity (0 = none) */
  1064.     int ttprty = 0;                     /* The parity that is in use. */
  1065.     static int ttpmsk = 0xff;        /* Parity stripping mask. */
  1066.     int ttmdm = 0;                      /* Modem in use. */
  1067.     int ttcarr = CAR_AUT;        /* Carrier handling mode. */
  1068.     int dfflow = FLO_NONE;        /* Default flow control is NONE */
  1069.     int backgrd = 0;                    /* Assume in foreground (no '&' ) */
  1070. #ifdef F_SETFL
  1071.     int iniflags = -1;            /* fcntl flags for ttyfd */
  1072. #endif /* F_SETFL */
  1073.     int fdflag = 0;            /* Flag for redirected stdio */
  1074.     int ttfdflg = 0;            /* Open File descriptor was given */
  1075.     int tvtflg = 0;            /* Flag that ttvt has been called */
  1076.     long ttspeed = -1L;            /* For saving speed */
  1077.     int ttflow = -9;            /* For saving flow */
  1078.     int ttld = -1;            /* Line discipline */
  1079.  
  1080. #ifdef sony_news
  1081.     static int km_con = -1;        /* Kanji mode for console tty */
  1082.     static int km_ext = -1;        /* Kanji mode for external device */
  1083. #endif /* sony_news */
  1084.  
  1085. #ifdef PARSENSE
  1086.     static int needpchk = 1;        /* Need parity check */
  1087. #else
  1088.     static int needpchk = 0;
  1089. #endif /* PARSENSE */
  1090.  
  1091.     extern int stopbits;        /* Stop bits */
  1092. #ifdef HWPARITY
  1093. /*
  1094.   Unfortunately we must do this with global variables rather than through the
  1095.   tt...() APIs to avoid changing the APIs and the many modules that use them.
  1096.   If hwparity != 0, this indicates 8 data bits + parity, rather than 7 data
  1097.   bits + parity or 8 data bits and no parity, and overrides the regular parity
  1098.   variable, which is communicated to this module thru ttpkt(), and represented
  1099.   locally by the ttprty variable.
  1100. */
  1101.     extern int hwparity;        /* Hardware parity */
  1102. #endif /* HWPARITY */
  1103.  
  1104. #ifdef TCPSOCKET
  1105. #ifdef TCP_NODELAY
  1106. static int nodelay_sav = -1;
  1107. #endif /* TCP_NODELAY */
  1108. #endif /* TCPSOCKET */
  1109.  
  1110. static int sigint_ign = 0;        /* SIGINT is ignored */
  1111.  
  1112. /*
  1113.   Having this module rely on external globals is bad, but fixing this
  1114.   requires overhaul of the ck*tio.c modules for all the different operating
  1115.   systems supported by C-Kermit.  Left for a future release.
  1116. */
  1117. extern int ttnproto;            /* Defined in ckcnet.c */
  1118. extern int ttnet;            /* Defined in ckcnet.c */
  1119. extern int nopush, xfrcan, xfrchr, xfrnum; /* Defined in ckcmai.c */
  1120. extern int suspend, wasclosed;
  1121. extern int inserver, local;
  1122.  
  1123. int ckxech = 0; /* 0 if system normally echoes console characters, else 1 */
  1124.  
  1125. int ckmaxfiles = 0;            /* Max number of open files */
  1126.  
  1127. #ifdef CK_ENCRYPTION            /* Kerberos */
  1128. #include "ckuath.h"
  1129. extern int me_encrypt, u_encrypt;
  1130. #endif /* CK_ENCRYPTION */
  1131.  
  1132. /* Declarations of variables global within this module */
  1133.  
  1134. #ifdef TTLEBUF                /* See ckcnet.h */
  1135. int ttpush = -1;
  1136. #define LEBUFSIZ 4096
  1137. static CHAR le_buf[LEBUFSIZ];
  1138. static int le_start = 0, le_end = 0, le_data = 0;
  1139. #endif /* TTLEBUF */
  1140.  
  1141. static int gotsigs = 0;
  1142.  
  1143. static time_t tcount = (time_t)0;    /* Elapsed time counter */
  1144.  
  1145. static SIGTYP (*saval)()     = NULL;    /* For saving alarm() handler */
  1146. static SIGTYP (*savquit)()   = NULL;    /* and other signal handlers */
  1147. #ifdef SIGUSR1
  1148. static SIGTYP (*savusr1)()   = NULL;
  1149. #endif /* SIGUSR1 */
  1150. #ifdef SIGUSR2
  1151. static SIGTYP (*savusr2)()   = NULL;
  1152. #endif /* SIGUSR2 */
  1153. #ifdef SIGPIPE
  1154. static SIGTYP (*savpipe)()   = NULL;
  1155. #endif /* SIGPIPE */
  1156. #ifdef SIGDANGER
  1157. static SIGTYP (*savdanger)() = NULL;
  1158. #endif /* SIGDANGER */
  1159.  
  1160. #ifndef NOJC
  1161. static SIGTYP (*jchdlr)()    = NULL;    /* For checking suspend handler */
  1162. #endif /* NOJC */
  1163. static int jcshell = -1;        /* And flag for result */
  1164.  
  1165. /*
  1166.   BREAKNULS is defined for systems that simulate sending a BREAK signal
  1167.   by sending a bunch of NUL characters at low speed.
  1168. */
  1169. #ifdef PROVX1
  1170. #ifndef BREAKNULS
  1171. #define BREAKNULS
  1172. #endif /* BREAKNULS */
  1173. #endif /* PROVX1 */
  1174.  
  1175. #ifdef V7
  1176. #ifndef BREAKNULS
  1177. #define BREAKNULS
  1178. #endif /* BREAKNULS */
  1179. #endif /* V7 */
  1180.  
  1181. #ifdef BREAKNULS
  1182. static char                /* A string of nulls */
  1183. *brnuls = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  1184. #endif /* BREAKNULS */
  1185.  
  1186. #ifdef CK_POSIX_SIG            /* Longjump buffers */
  1187. static sigjmp_buf sjbuf;        /* POSIX signal handling */
  1188. #else
  1189. static jmp_buf sjbuf;
  1190. #endif /* CK_POSIX_SIG */
  1191.  
  1192. #ifdef V7
  1193. static jmp_buf jjbuf;
  1194. #endif /* V7 */
  1195.  
  1196. /* static */                /* (Not static any more) */
  1197. int ttyfd = -1;                /* TTY file descriptor */
  1198.  
  1199. int ttpipe = 0;                /* NETCMD: Use pipe instead of ttyfd */
  1200. int ttpty  = 0;                         /* NETPTY: Use pty instead of ttfyd */
  1201.  
  1202. #ifdef NETCMD
  1203. #ifdef NETCONN
  1204. static int pipe0[2], pipe1[2];        /* Pipes for net i/o */
  1205. #endif /* NETCONN */
  1206. static PID_T ttpid = 0;            /* Process ID for fork */
  1207. static int fdin, fdout;            /* File descriptors for pipe */
  1208. static FILE * ttout = NULL;        /* File pointer for output pipe */
  1209. #ifdef DCLFDOPEN
  1210. /* fdopen() needs declaring because it's not declared in <stdio.h> */
  1211. _PROTOTYP( FILE * fdopen, (int, char *) );
  1212. #endif /* DCLFDOPEN */
  1213. #endif /* NETCMD */
  1214.  
  1215. extern int pexitstat, quiet;
  1216.  
  1217. #ifdef Plan9
  1218. int ttyctlfd  = -1;   /* TTY control channel - What? UNIX doesn't have one? */
  1219. int consctlfd = -1;            /* Console control channel */
  1220. int noisefd = -1;            /* tone channel */
  1221. static int ttylastspeed = -1;        /* So we can lie about the speed */
  1222. #endif /* Plan9 */
  1223.  
  1224. int telnetfd = 0;            /* File descriptor is for telnet */
  1225. #ifdef NETCONN
  1226. int x25fd = 0;                /* File descriptor is for X.25 */
  1227. #endif /* NETCONN */
  1228.  
  1229. char lockpid[16] = { '\0', '\0' };    /* PID stored in lockfile, as string */
  1230.  
  1231. static int lkf = 0,                     /* Line lock flag */
  1232.     cgmf = 0,                           /* Flag that console modes saved */
  1233.     xlocal = 0,                         /* Flag for tty local or remote */
  1234.     curcarr = 0;            /* Carrier mode: require/ignore. */
  1235.  
  1236. static int netconn = 0;            /* 1 if network connection active */
  1237.  
  1238. static char escchr;                     /* Escape or attn character */
  1239.  
  1240. #ifdef CK_SCO32V4
  1241. #include <sys/time.h>
  1242. #endif /* CK_SCO32V4 */
  1243.  
  1244. #ifdef HAVE_TV
  1245.     static struct timeval tv;        /* For getting time, from sys/time.h */
  1246. #endif /* HAVE_TV */
  1247. #ifdef HAVE_TZ
  1248.     static struct timezone tz;
  1249. #endif /* HAVE_TZ */
  1250.  
  1251. #ifdef OSF
  1252.     static struct timeb ftp;            /* And from sys/timeb.h */
  1253. #endif /* OSF */
  1254.  
  1255. #ifdef BSD29
  1256.     static long xclock;            /* For getting time from sys/time.h */
  1257.     static struct timeb ftp;            /* And from sys/timeb.h */
  1258. #endif /* BSD29 */
  1259.  
  1260. #ifdef BSD41
  1261.     static long xclock;            /* For getting time from sys/time.h */
  1262.     static struct timeb ftp;            /* And from sys/timeb.h */
  1263. #endif /* BSD41 */
  1264.  
  1265. #ifdef BELLV10
  1266.     static long xclock;            /* For getting time from sys/time.h */
  1267.     static struct timeb ftp;            /* And from sys/timeb.h */
  1268. #endif /* BELLV10 */
  1269.  
  1270. #ifdef FT21
  1271.     static long xclock;            /* For getting time from sys/time.h */
  1272.     static struct timeb ftp;            /* And from sys/timeb.h */
  1273. #endif /* FT21 */
  1274.  
  1275. #ifdef TOWER1
  1276.     static long xclock;            /* For getting time from sys/time.h */
  1277.     static struct timeb ftp;        /* And from sys/timeb.h */
  1278. #endif /* TOWER1 */
  1279.  
  1280. #ifdef COHERENT
  1281.     static long xclock;            /* For getting time from sys/time.h */
  1282.     static struct timeb ftp;        /* And from sys/timeb.h */
  1283. #endif /* COHERENT */
  1284.  
  1285. #ifdef V7
  1286.     static long xclock;
  1287. #endif /* V7 */
  1288.  
  1289. /* sgtty/termio information... */
  1290.  
  1291. #ifdef BSD44ORPOSIX            /* POSIX or BSD44 */
  1292.   static struct termios
  1293.     ttold, ttraw, tttvt, ttcur,
  1294.     ccold, ccraw, cccbrk;
  1295. #else                    /* BSD, V7, etc */
  1296.  
  1297. #ifdef COHERENT                /* Hack alert... */
  1298. #define ATTSV
  1299. #endif /* COHERENT */
  1300.  
  1301. #ifdef ATTSV
  1302.   static struct termio ttold = {0};    /* Init'd for word alignment, */
  1303.   static struct termio ttraw = {0};    /* which is important for some */
  1304.   static struct termio tttvt = {0};    /* systems, like Zilog... */
  1305.   static struct termio ttcur = {0};
  1306.   static struct termio ccold = {0};
  1307.   static struct termio ccraw = {0};
  1308.   static struct termio cccbrk = {0};
  1309. #else
  1310.   static struct sgttyb                  /* sgtty info... */
  1311.     ttold, ttraw, tttvt, ttcur,     /* for communication line */
  1312.     ccold, ccraw, cccbrk;        /* and for console */
  1313. #ifdef BELLV10
  1314.   static struct ttydevb            /* Device info... */
  1315.     tdold, tdcur;            /* for communication device */
  1316. #endif /* BELLV10 */
  1317. #ifdef TIOCGETC
  1318.   static struct tchars tchold, tchnoi;
  1319.  
  1320.   static int tcharf;
  1321. #endif /* TIOCGETC */
  1322. #ifdef TIOCGLTC
  1323.   static struct ltchars ltchold, ltchnoi;
  1324.   static int ltcharf;
  1325. #endif /* TIOCGLTC */
  1326.   int lmodef = 0;            /* Local modes */
  1327.   int lmode = 0;
  1328. #endif /* ATTSV */
  1329. #endif /* BSD44ORPOSIX */
  1330.  
  1331. #ifdef COMMENT
  1332. /* It picks up the speeds but they don't work */
  1333. #ifdef UNIXWARE                /* For higher serial speeds */
  1334. #ifdef UW7                /* in Unixware 7.0 */
  1335. #include <sys/asyc.h>            /* This picks up 57600 and 115200 */
  1336. #endif /* UW7 */
  1337. #endif /* UNIXWARE */
  1338. #endif /* COMMENT */
  1339.  
  1340. #ifdef PROVX1
  1341.   static struct sgttyb ttbuf;
  1342. #endif /* PROVX1 */
  1343.  
  1344. #ifdef ultrix
  1345. /* do we really need this? */
  1346.   static struct sgttyb vanilla;
  1347. #endif /* ultrix */
  1348.  
  1349. #ifdef ATT7300
  1350. static int attmodem = 0;                /* ATT7300 internal-modem status */
  1351. struct updata dialer = {0};        /* Condition dialer for data call */
  1352. #endif /* ATT7300 */
  1353.  
  1354. #ifndef NOUUCP
  1355. #define FLFNAML 128
  1356. #ifndef USETTYLOCK
  1357. #ifdef RTAIX
  1358. char lkflfn[FLFNAML] = { '\0', '\0' };    /* and possible link to it */
  1359. #endif /* RTAIX */
  1360. char lock2[FLFNAML] =  { '\0', '\0' };    /* Name of second lockfile */
  1361. #endif /* USETTYLOCK */
  1362. #else
  1363. #define FLFNAML 7
  1364. #endif /* NOUUCP */
  1365. char flfnam[FLFNAML+1] = { '\0', '\0' }; /* UUCP lock file path name */
  1366.  
  1367. int haslock = 0;            /* =1 if this kermit locked uucp */
  1368.  
  1369. #ifndef OXOS
  1370. #ifdef SVORPOSIX
  1371. static int conesc = 0;                  /* set to 1 if esc char (^\) typed */
  1372. #else
  1373. #ifdef V7
  1374. static int conesc = 0;
  1375. #else
  1376. #ifdef C70
  1377. static int conesc = 0;
  1378. #endif /* C70 */
  1379. #endif /* V7 */
  1380. #endif /* SVORPOSIX */
  1381. #endif /* OXOS */
  1382.  
  1383. /* Local copy of comm device name or network host */
  1384. static char ttnmsv[DEVNAMLEN+1] = { '\0', '\0' };
  1385. #ifdef USETTYLOCK
  1386. static char lockname[DEVNAMLEN+1];    /* Ditto, the part after "/dev/". */
  1387. #endif /* USETTYLOCK */
  1388.  
  1389. #ifdef aegis
  1390. static status_$t st;                    /* error status return value */
  1391. static short concrp = 0;                /* true if console is CRP pad */
  1392. static uid_$t ttyuid;                   /* tty type uid */
  1393. static uid_$t conuid;                   /* stdout type uid */
  1394.  
  1395. /* APOLLO Aegis main()
  1396.  * establish acl usage and cleanup handling
  1397.  *    this makes sure that CRP pads
  1398.  *    get restored to a usable mode
  1399.  */
  1400. main(argc,argv) int argc; char **argv; {
  1401.         status_$t status;
  1402.         pfm_$cleanup_rec dirty;
  1403.  
  1404.         PID_T pid = getpid();
  1405.  
  1406.         /* acl usage according to invoking environment */
  1407.         default_acl(USE_DEFENV);
  1408.  
  1409.         /* establish a cleanup continuation */
  1410.         status = pfm_$cleanup(dirty);
  1411.         if (status.all != pfm_$cleanup_set) {
  1412.                 /* only handle faults for the original process */
  1413.                 if (pid == getpid() && status.all > pgm_$max_severity) {
  1414.             /* blew up in main process */
  1415.             status_$t quo;
  1416.             pfm_$cleanup_rec clean;
  1417.  
  1418.             /* restore the console in any case */
  1419.             conres();
  1420.  
  1421.             /* attempt a clean exit */
  1422.             debug(F101, "cleanup fault status", "", status.all);
  1423.  
  1424.             /* doexit(), then send status to continuation */
  1425.             quo = pfm_$cleanup(clean);
  1426.             if (quo.all == pfm_$cleanup_set)
  1427.               doexit(pgm_$program_faulted,-1);
  1428.             else if (quo.all > pgm_$max_severity)
  1429.               pfm_$signal(quo); /* blew up in doexit() */
  1430.                 }
  1431.                 /* send to the original continuation */
  1432.                 pfm_$signal(status);
  1433.                 /*NOTREACHED*/
  1434.         }
  1435.         return(ckcmai(argc, argv));
  1436. }
  1437. #endif /* aegis */
  1438.  
  1439. /* ANSI-style prototypes for internal functions. */
  1440. /* Functions used outside this module are prototyped in ckcker.h. */
  1441.  
  1442. #ifdef apollo
  1443. _PROTOTYP( SIGTYP timerh, () );
  1444. _PROTOTYP( SIGTYP cctrap, () );
  1445. _PROTOTYP( SIGTYP esctrp, () );
  1446. _PROTOTYP( SIGTYP sig_ign, () );
  1447. #else
  1448. _PROTOTYP( SIGTYP timerh, (int) );
  1449. _PROTOTYP( SIGTYP cctrap, (int) );
  1450. _PROTOTYP( SIGTYP esctrp, (int) );
  1451. #endif /* apollo */
  1452. _PROTOTYP( int do_open, (char *) );
  1453. _PROTOTYP( static int in_chk, (int, int) );
  1454. _PROTOTYP( static int ttrpid, (char *) );
  1455. _PROTOTYP( static int ttchkpid, (char *) );
  1456. _PROTOTYP( static int ttlock, (char *) );
  1457. _PROTOTYP( static int ttunlck, (void) );
  1458. _PROTOTYP( int mygetbuf, (void) );
  1459. _PROTOTYP( int myfillbuf, (void) );
  1460. _PROTOTYP( VOID conbgt, (int) );
  1461. #ifdef ACUCNTRL
  1462. _PROTOTYP( VOID acucntrl, (char *, char *) );
  1463. #endif /* ACUCNTRL */
  1464.  
  1465. #ifdef BSD44ORPOSIX
  1466. _PROTOTYP( int carrctl, (struct termios *, int) );
  1467. #else
  1468. #ifdef ATTSV
  1469. _PROTOTYP( int carrctl, (struct termio *, int) );
  1470. #else
  1471. _PROTOTYP( int carrctl, (struct sgttyb *, int) );
  1472. #endif /* ATTSV */
  1473. #endif /* BSD44ORPOSIX */
  1474.  
  1475. #ifdef ATT7300
  1476. _PROTOTYP( int attdial, (char *, long, char *) );
  1477. _PROTOTYP( int offgetty, (char *) );
  1478. _PROTOTYP( int ongetty, (char *) );
  1479. #endif /* ATT7300 */
  1480.  
  1481. #ifdef BEOSORBEBOX
  1482. #ifdef SELECT
  1483.     /* BeOS is not capable of using SELECT on anything but sockets */
  1484. #undef SELECT
  1485. #endif /* SELECT */
  1486. #include <kernel/OS.h>
  1487. /* #ifdef BE_DR_7 */
  1488. static double time_started = 0.0;
  1489. struct ALARM_STRUCT {
  1490.     thread_id thread;
  1491.     int time;
  1492. };
  1493. static thread_id alarm_thread = -1;
  1494. static struct ALARM_STRUCT alarm_struct;
  1495. _PROTOTYP( long do_alarm, (void *) );
  1496. _PROTOTYP( unsigned int alarm, (unsigned int) );
  1497. _PROTOTYP( void alarm_expired, (void) );
  1498. /* #endif */ /* BE_DR_7 */
  1499. #endif /* BEOSORBEBOX */
  1500.  
  1501. #ifndef xunchar
  1502. #define xunchar(ch) (((ch) - 32 ) & 0xFF )    /* Character to number */
  1503. #endif /* xunchar */
  1504.  
  1505. #ifdef CK_ANSIC
  1506. static char *
  1507. xxlast(char *s, char c)
  1508. #else
  1509. static char *
  1510. xxlast(s,c) char *s; char c;
  1511. #endif /* CK_ANSIC */
  1512. /* xxlast */ {        /*  Last occurrence of character c in string s. */
  1513.     int i;
  1514.     for (i = (int)strlen(s); i > 0; i--)
  1515.       if (s[i-1] == c ) return(s + (i - 1));
  1516.     return(NULL);
  1517. }
  1518.  
  1519. /* Timeout handler for communication line input functions */
  1520.  
  1521. /*ARGSUSED*/
  1522. SIGTYP
  1523. timerh(foo) int foo; {
  1524.     ttimoff();
  1525. #ifdef BEOSORBEBOX
  1526. /* #ifdef BE_DR_7 */
  1527.     alarm_expired();
  1528. /* #endif */ /* BE_DR_7 */
  1529. #endif /* BEOSORBEBOX */
  1530. #ifdef CK_POSIX_SIG
  1531.     siglongjmp(sjbuf,1);
  1532. #else
  1533.     longjmp(sjbuf,1);
  1534. #endif /* CK_POSIX_SIG */
  1535. }
  1536.  
  1537. /*ARGSUSED*/
  1538. SIGTYP
  1539. xtimerh(foo) int foo; {            /* Like timerh() but does */
  1540. #ifdef BEOSORBEBOX            /* not reset the timer itslef */
  1541. /* #ifdef BE_DR_7 */
  1542.     alarm_expired();
  1543. /* #endif */ /* BE_DR_7 */
  1544. #endif /* BEOSORBEBOX */
  1545. #ifdef CK_POSIX_SIG
  1546.     siglongjmp(sjbuf,1);
  1547. #else
  1548.     longjmp(sjbuf,1);
  1549. #endif /* CK_POSIX_SIG */
  1550. }
  1551.  
  1552.  
  1553. /* Control-C trap for communication line input functions */
  1554.  
  1555. int cc_int;                /* Flag */
  1556. SIGTYP (* occt)();            /* For saving old SIGINT handler */
  1557.  
  1558. /*ARGSUSED*/
  1559. SIGTYP
  1560. cctrap(foo) int foo; {            /* Needs arg for ANSI C */
  1561.   cc_int = 1;                /* signal() prototype. */
  1562.   return;
  1563. }
  1564.  
  1565. /*  S Y S I N I T  --  System-dependent program initialization.  */
  1566.  
  1567. /*
  1568.  * ttgwsiz() returns:
  1569.  *    1    tt_rows and tt_cols are known, both altered, both > 0
  1570.  *    0    tt_rows and/or tt_cols are known, both altered, one or both <= 0
  1571.  *    -1   tt_rows and tt_cols are unknown and unaltered
  1572.  */
  1573.  
  1574. extern int tt_rows, tt_cols;
  1575.  
  1576. static int
  1577. xttgwsiz() {
  1578.     char *p;
  1579.     int rows = 0, cols = 0;
  1580.     p = getenv("LINES");
  1581.     debug(F110,"xttgwsiz LINES",p,0);
  1582.     if (p) {
  1583.     rows = atol(p);
  1584.     if (rows > 0) {
  1585.         p = getenv("COLUMNS");
  1586.         debug(F110,"xttgwsiz COLUMNS",p,0);
  1587.         if (p) {
  1588.         cols = atol(p);
  1589.         if (cols > 0) {
  1590.             tt_rows = rows;
  1591.             tt_cols = cols;
  1592.             return(1);
  1593.         }
  1594.         return(0);
  1595.         }
  1596.     }
  1597.     }
  1598.     return(-1);
  1599. }
  1600.  
  1601. #ifdef TTLEBUF
  1602. VOID
  1603. le_init() {                /* LocalEchoInit() */
  1604.     int i;
  1605.     for (i = 0; i < LEBUFSIZ; i++)
  1606.       le_buf[i] = '\0';
  1607.     le_start = 0;
  1608.     le_end = 0;
  1609.     le_data = 0;
  1610. }
  1611.  
  1612. VOID
  1613. le_clean() {                /* LocalEchoCleanup() */
  1614.     le_init();
  1615.     return;
  1616. }
  1617.  
  1618. int
  1619. le_inbuf() {
  1620.     int rc = 0;
  1621.     if (le_start != le_end) {
  1622.     rc = (le_end -
  1623.           le_start +
  1624.           LEBUFSIZ) % LEBUFSIZ;
  1625.     }
  1626.     debug(F111,"le_inbuf","chars waiting",rc);
  1627.     return(rc);
  1628. }
  1629.  
  1630. int
  1631. #ifdef CK_ANSIC
  1632. le_putchar(CHAR ch)
  1633. #else
  1634. le_putchar(ch) CHAR ch;
  1635. #endif /* CK_ANSIC */
  1636. /* le_putchar */ {
  1637. #ifdef COMMENT
  1638.     /* In UNIX we do not have another thread taking chars out of the buffer */
  1639.     while ((le_start - le_end == 1) ||
  1640.             (le_start == 0 && le_end == LEBUFSIZ - 1)) {
  1641.     /* Buffer is full */
  1642.         debug(F111,"le_putchar","Buffer is Full",ch);
  1643.         ReleaseLocalEchoMutex() ;
  1644.         msleep(250);
  1645.         RequestLocalEchoMutex( SEM_INDEFINITE_WAIT ) ;
  1646.     }
  1647. #else
  1648.     if ((le_start - le_end + LEBUFSIZ)%LEBUFSIZ == 1) {
  1649.         debug(F110,"le_putchar","buffer is full",0);
  1650.         return(-1);
  1651.     }
  1652. #endif /* COMMENT */
  1653.     le_buf[le_end++] = ch;
  1654.     if (le_end == LEBUFSIZ)
  1655.       le_end = 0;
  1656.     le_data = 1;
  1657.     return(0);
  1658. }
  1659.  
  1660. int
  1661. #ifdef CK_ANSIC
  1662. le_puts(CHAR * s, int n)
  1663. #else
  1664. le_puts(s,n) CHAR * s; int n;
  1665. #endif /* CK_ANSIC */
  1666. /* le_puts */ {
  1667.     int rc = 0;
  1668.     int i = 0;
  1669.     CHAR * p = (CHAR *)"le_puts";
  1670.     hexdump(p,s,n);
  1671.     for (i = 0; i < n; i++)
  1672.       rc = le_putchar((char)s[i]);
  1673.     debug(F101,"le_puts","",rc);
  1674.     return(rc);
  1675. }
  1676.  
  1677. int
  1678. #ifdef CK_ANSIC
  1679. le_putstr(CHAR * s)
  1680. #else
  1681. le_putstr(s) CHAR * s;
  1682. #endif /* CK_ANSIC */
  1683. /* le_puts */ {
  1684.     CHAR * p;
  1685.     int rc = 0;
  1686.     p = (CHAR *)"le_putstr";
  1687.     hexdump(p,s,(int)strlen((char *)s));
  1688.     for (p = s; *p && !rc; p++)
  1689.       rc = le_putchar(*p);
  1690.     return(rc);
  1691. }
  1692.  
  1693. int
  1694. #ifdef CK_ANSIC
  1695. le_getchar(CHAR * pch)
  1696. #else /* CK_ANSIC */
  1697. le_getchar(pch) CHAR * pch;
  1698. #endif /* CK_ANSIC */
  1699. /* le_gatchar */ {
  1700.     int rc = 0;
  1701.     if (le_start != le_end) {
  1702.         *pch = le_buf[le_start];
  1703.         le_buf[le_start] = 0;
  1704.         le_start++;
  1705.  
  1706.         if (le_start == LEBUFSIZ)
  1707.           le_start = 0;
  1708.  
  1709.         if (le_start == le_end) {
  1710.             le_data = 0;
  1711.         }
  1712.         rc++;
  1713.     } else {
  1714.         *pch = 0;
  1715.     }
  1716.     return(rc);
  1717. }
  1718. #endif /* TTLEBUF */
  1719.  
  1720. #ifdef COMMENT
  1721. /*
  1722.   Some systems like OSF/1 use TIOCGSIZE instead of TIOCGWINSZ.
  1723.   But as far as I know, whenever TIOCGSIZE is defined, it is
  1724.   equated to TIOCGWINSZ.  For cases where this is not done, try this:
  1725. */
  1726. #ifndef TIOCGWINSZ
  1727. #ifdef TIOCGSIZE
  1728. #define TIOCGWINSZ TIOCGSIZE
  1729. #endif /* TIOCGSIZE */
  1730. #endif /* TIOCGWINSZ */
  1731. #endif /* COMMENT */
  1732.  
  1733. int
  1734. ttgwsiz() {
  1735.     int x = 0;
  1736. #ifndef NONAWS
  1737. #ifdef QNX
  1738. /*
  1739.   NOTE: TIOCGWSIZ works here too, but only in the 32-bit version.
  1740.   This code works for both the 16- and 32-bit versions.
  1741. */
  1742.     extern int dev_size(int, int, int, int *, int *);
  1743.     int r, c;
  1744.  
  1745.     if (dev_size(0, -1, -1, &r, &c) == 0) {
  1746.     debug(F101,"ttgwsiz QNX r","",r);
  1747.     debug(F101,"ttgwsiz QNX c","",c);
  1748.     tt_rows = r;
  1749.     tt_cols = c;
  1750.     return ((r > 0 && c > 0) ? 1 : 0);
  1751.     } else return(xttgwsiz());
  1752. #else /* QNX */
  1753. #ifdef TIOCGWINSZ
  1754.  
  1755. /* Note, this was M_UNIX, changed to XENIX to allow cross compilation... */
  1756. #ifdef XENIX                /* SCO UNIX 3.2v4.0 */
  1757. #include <sys/stream.h>            /* typedef mblk_t needed by ptem.h */
  1758. #include <sys/ptem.h>            /* for ttgwsiz() */
  1759. #endif /* XENIX */
  1760.  
  1761. #ifdef I386IX                /* Ditto for Interactive */
  1762. #include <sys/stream.h>
  1763. #include <sys/ptem.h>
  1764. #endif /* I386IX */
  1765.  
  1766. /* Note, the above might be needed for some other older SVR3 Intel makes... */
  1767.  
  1768.     struct winsize w;
  1769. #ifdef IKSD
  1770.     if (inserver)
  1771.       return(xttgwsiz());
  1772. #endif /* IKSD */
  1773.     x = ioctl(0, (int)TIOCGWINSZ, (char *)&w);
  1774.     debug(F101,"ttgwsiz TIOCGWINSZ","",x);
  1775.     if (x < 0) {
  1776.     return(xttgwsiz());
  1777.     } else if (w.ws_row > 0 && w.ws_col > 0) {
  1778.     tt_rows = w.ws_row;
  1779.     tt_cols = w.ws_col;
  1780.     debug(F101,"ttgwsiz tt_rows","",tt_rows);
  1781.     debug(F101,"ttgwsiz tt_cols","",tt_cols);
  1782.     return(1);
  1783.     } else {
  1784.     debug(F100,"ttgwsiz TIOCGWINSZ 00","",0);
  1785.     return(xttgwsiz());
  1786.     }
  1787. #else
  1788.     return(xttgwsiz());
  1789. #endif /* TIOCGWINSZ */
  1790. #endif /* QNX */
  1791. #endif /* NONAWS */
  1792. }
  1793.  
  1794. SIGTYP
  1795. sighup(foo) int foo; {            /* SIGHUP handler */
  1796.     backgrd = 1;
  1797.     debug(F100,"***************","",0);
  1798.     debug(F100,"SIGHUP received","",0);
  1799.     debug(F100,"***************","",0);
  1800.     doexit(BAD_EXIT,-1);
  1801.     /*NOTREACHED*/
  1802.     SIGRETURN;                /* Shut picky compilers up... */
  1803. }
  1804.  
  1805. #ifdef CK_SCO32V4
  1806. /* Exists but there is no prototype in the header files */
  1807. _PROTOTYP( char * ttyname, (int) );
  1808. #else
  1809. #ifdef SV68R3V6
  1810. _PROTOTYP( char * ttyname, (int) );
  1811. #else
  1812. #ifdef ultrix
  1813. _PROTOTYP( char * ttyname, (int) );
  1814. #else
  1815. #ifdef HPUX6
  1816. _PROTOTYP( char * ttyname, (int) );
  1817. #else
  1818. #ifdef HPUX5
  1819. _PROTOTYP( char * ttyname, (int) );
  1820. #else
  1821. #ifdef PS2AIX10
  1822. _PROTOTYP( char * ttyname, (int) );
  1823. #else
  1824. #ifdef BSD42
  1825. _PROTOTYP( char * ttyname, (int) );
  1826. #endif /* BSD42 */
  1827. #endif /* PS2AIX10 */
  1828. #endif /* HPUX5 */
  1829. #endif /* HPUX6 */
  1830. #endif /* ultrix */
  1831. #endif /* SV68R3V6 */
  1832. #endif /* CK_SCO32V4 */
  1833.  
  1834. #ifndef SIGUSR1                /* User-defined signals */
  1835. #define SIGUSR1 30
  1836. #endif /* SIGUSR1 */
  1837.  
  1838. #ifndef SIGUSR2
  1839. #define SIGUSR2 31
  1840. #endif /* SIGUSR2 */
  1841.  
  1842. /*
  1843.   ignorsigs() sets certain signals to SIG_IGN.  But when a signal is
  1844.   ignored, it remains ignored across exec(), so we have to restore these
  1845.   signals before exec(), which is the purpose of restorsigs().
  1846. */
  1847. static VOID
  1848. ignorsigs() {                /* Ignore these signals */
  1849.     savquit = signal(SIGQUIT,SIG_IGN);    /* Ignore Quit signal */
  1850.  
  1851. #ifdef SIGDANGER            /* Ignore danger signals */
  1852. /*
  1853.   This signal is sent when the system is low on swap space.  Processes
  1854.   that don't handle it are candidates for termination.  If swap space doesn't
  1855.   clear out enough, we still might be terminated via kill() -- nothing we can
  1856.   do about that!  Conceivably, this could be improved by installing a real
  1857.   signal handler that warns the user, but that would be pretty complicated,
  1858.   since we are not always in control of the screen -- e.g. during remote-mode
  1859.   file transfer.
  1860. */
  1861.     savdanger = signal(SIGDANGER,SIG_IGN); /* e.g. in AIX */
  1862. #endif /* SIGDANGER */
  1863. #ifdef SIGPIPE
  1864. /*
  1865.   This one comes when a TCP/IP connection is broken by the remote.
  1866.   We prefer to catch this situation by examining error codes from write().
  1867. */
  1868.     savpipe = signal(SIGPIPE,SIG_IGN);
  1869. #endif /* SIGPIPE */
  1870.     savusr1 = signal(SIGUSR1,SIG_IGN);    /* Ignore user-defined signals */
  1871.     savusr2 = signal(SIGUSR2,SIG_IGN);
  1872. }
  1873.  
  1874. VOID
  1875. restorsigs() {                /* Restore these signals */
  1876.     (VOID) signal(SIGQUIT,savquit);    /* (used in ckufio.c) */
  1877. #ifdef SIGDANGER
  1878.     (VOID) signal(SIGDANGER,savdanger);
  1879. #endif /* SIGDANGER */
  1880. #ifdef SIGPIPE
  1881.     (VOID) signal(SIGPIPE,savpipe);
  1882. #endif /* SIGPIPE */
  1883.     (VOID) signal(SIGUSR1,savusr1);
  1884.     (VOID) signal(SIGUSR2,savusr2);
  1885. }
  1886.  
  1887. int
  1888. sysinit() {
  1889.     int x;
  1890.     char * s;
  1891. #ifdef CK_UTSNAME
  1892.     struct utsname name;
  1893. #endif /* CK_UTSNAME */
  1894.  
  1895.     extern char startupdir[];
  1896. /*
  1897.   BEFORE ANYTHING ELSE: Initialize the setuid package.
  1898.   Change to the user's real user and group ID.
  1899.   If this can't be done, don't run at all.
  1900. */
  1901.     x = priv_ini();
  1902. #ifdef SUIDDEBUG
  1903.     fprintf(stderr,"PRIV_INI=%d\n",x);
  1904. #endif /* SUIDDEBUG */
  1905.     if (x) {
  1906.     if (x & 1) fprintf(stderr,"Fatal: setuid failure.\n");
  1907.     if (x & 2) fprintf(stderr,"Fatal: setgid failure.\n");
  1908.     if (x & 4) fprintf(stderr,"Fatal: C-Kermit setuid to root!\n");
  1909.     exit(1);
  1910.     }
  1911.     signal(SIGINT,SIG_IGN);        /* Ignore interrupts at first */
  1912.     signal(SIGFPE,SIG_IGN);        /* Ignore floating-point exceptions */
  1913.     signal(SIGHUP,sighup);        /* Catch SIGHUP */
  1914.  
  1915. #ifndef NOJC
  1916. /*
  1917.   Get the initial job control state.
  1918.   If it is SIG_IGN, that means the shell does not support job control,
  1919.   and so we'd better not suspend ourselves.
  1920. */
  1921. #ifdef SIGTSTP
  1922.     jchdlr = signal(SIGTSTP,SIG_IGN);
  1923.     if (jchdlr == SIG_IGN) {
  1924.     jcshell = 0;
  1925.     debug(F100,"sysinit jchdlr: SIG_IGN","",0);
  1926.     } else if (jchdlr == SIG_DFL) {
  1927.     debug(F100,"sysinit jchdlr: SIG_DFL","",0);
  1928.     jcshell = 1;
  1929.     } else {
  1930.     debug(F100,"sysinit jchdlr: other","",0);
  1931.     jcshell = 3;
  1932.     }
  1933.     (VOID) signal(SIGTSTP,jchdlr);    /* Put it back... */
  1934. #endif /* SIGTSTP */
  1935. #endif /* NOJC */
  1936.  
  1937.     conbgt(0);                /* See if we're in the background */
  1938.     congm();                /* Get console modes */
  1939.  
  1940.     (VOID) signal(SIGALRM,SIG_IGN);    /* Ignore alarms */
  1941.  
  1942.     ignorsigs();            /* Ignore some other signals */
  1943.  
  1944. #ifdef F_SETFL
  1945.     iniflags = fcntl(0,F_GETFL,0);    /* Get stdin flags */
  1946. #endif /* F_SETFL */
  1947.  
  1948. #ifdef ultrix
  1949.     gtty(0,&vanilla);            /* Get sgtty info */
  1950. #else
  1951. #ifdef AUX
  1952.     set42sig();                /* Don't ask! (hakanson@cs.orst.edu) */
  1953. #endif /* AUX */
  1954. #endif /* ultrix */
  1955. /*
  1956.   Warning: on some UNIX systems (SVR4?), ttyname() reportedly opens /dev but
  1957.   never closes it.  If it is called often enough, we run out of file
  1958.   descriptors and subsequent open()'s of other devices or files can fail.
  1959. */
  1960.     s = NULL;
  1961. #ifndef MINIX
  1962.     if (isatty(0))            /* Name of controlling terminal */
  1963.       s = ttyname(0);
  1964.     else if (isatty(1))
  1965.       s = ttyname(1);
  1966.     else if (isatty(2))
  1967.       s = ttyname(2);
  1968.     debug(F110,"sysinit ttyname(0)",s,0);
  1969. #endif /* MINIX */
  1970.  
  1971. #ifdef BEOS
  1972.     if (!dftty)
  1973.       makestr(&dftty,s);
  1974. #endif /* BEOS */
  1975.  
  1976.     if (s)
  1977.       ckstrncpy((char *)cttnam,s,DEVNAMLEN+1);
  1978. #ifdef SVORPOSIX
  1979.     if (!cttnam[0])
  1980.       ctermid(cttnam);
  1981. #endif /* SVORPOSIX */
  1982.     if (!cttnam[0])
  1983.       ckstrncpy((char *)cttnam,dftty,DEVNAMLEN+1);
  1984.     debug(F110,"sysinit CTTNAM",CTTNAM,0);
  1985.     debug(F110,"sysinit cttnam",cttnam,0);
  1986.  
  1987.     ttgwsiz();                /* Get window (screen) dimensions. */
  1988.  
  1989. #ifdef _SC_OPEN_MAX
  1990.     ckmaxfiles = sysconf(_SC_OPEN_MAX);
  1991. #endif /* _SC_OPEN_MAX */
  1992.  
  1993. #ifdef Plan9
  1994.     if (!backgrd) {
  1995.         consctlfd = open("/dev/consctl", O_WRONLY);
  1996.         /*noisefd = open("/dev/noise", O_WRONLY)*/
  1997.     }
  1998.     ckxech = 1;
  1999. #endif /* Plan9 */
  2000.  
  2001. #ifdef CK_UTSNAME
  2002.     if (uname(&name) > -1) {
  2003.     ckstrncpy(unm_mch,name.machine,CK_SYSNMLN);
  2004.     ckstrncpy(unm_nam,name.sysname,CK_SYSNMLN);
  2005.     ckstrncpy(unm_rel,name.release,CK_SYSNMLN);
  2006.     ckstrncpy(unm_ver,name.version,CK_SYSNMLN);
  2007. #ifdef DEBUG
  2008.     if (deblog) {
  2009.         debug(F110,"sysinit uname machine",unm_mch,0);
  2010.         debug(F110,"sysinit uname sysname",unm_nam,0);
  2011.         debug(F110,"sysinit uname release",unm_rel,0);
  2012.         debug(F110,"sysinit uname version",unm_ver,0);
  2013.     }
  2014. #endif /* DEBUG */
  2015.  
  2016. #ifdef HPUX9PLUS
  2017.     if (name.machine[5] == '8')
  2018.       hpis800 = 1;
  2019.     else
  2020.       hpis800 = 0;
  2021.     debug(F101,"sysinit hpis800","",hpis800);
  2022. #endif /* HPUX9PLUS */
  2023. #ifdef TRU64
  2024.         getsysinfo(GSI_PLATFORM_NAME, unm_mod, CK_SYSNMLN, 0, 0);
  2025.         debug(F110,"sysinit getsysinfo model",unm_mod,0);
  2026. #endif /* TRU64 */
  2027. #ifdef SOLARIS25
  2028.         sysinfo(SI_PLATFORM, unm_mod, CK_SYSNMLN);
  2029.         debug(F110,"sysinit sysinfo model",unm_mod,0);
  2030. #endif /* SOLARIS25 */
  2031.     }
  2032. #endif /* CK_UTSNAME */
  2033.  
  2034. #ifdef CK_ENVIRONMENT
  2035.     {
  2036.     extern char tn_env_acct[], tn_env_disp[], tn_env_job[],
  2037.     tn_env_prnt[], tn_env_sys[], uidbuf[];
  2038.         extern char * whoami();
  2039.     char *p;
  2040. #ifdef CKSENDUID
  2041.         uidbuf[0] = '\0';
  2042. #ifdef IKSD
  2043.         if (!inserver) {
  2044. #endif /* IKSD */
  2045.             p = getenv("USER");
  2046.             debug(F110,"sysinit uidbuf from USER",uidbuf,0);
  2047.         if (!p) p = "";
  2048.             if (!*p) {
  2049.                 p = getenv("LOGNAME");
  2050.                 debug(F110,"sysinit uidbuf from LOGNAME",uidbuf,0);
  2051.             }
  2052.         if (!p) p = "";
  2053.             if (!*p) {
  2054.                 p = whoami();
  2055.                 debug(F110,"sysinit uidbuf from whoami()",uidbuf,0);
  2056.             }
  2057.         if (!p) p = "";
  2058.         ckstrncpy(uidbuf, *p ? p : "UNKNOWN", UIDBUFLEN);
  2059. #ifdef IKSD
  2060.         }
  2061. #endif /* IKSD */
  2062.     debug(F110,"sysinit final uidbuf",uidbuf,0);
  2063. #endif /* CKSENDUID */
  2064.  
  2065.     if ((p = getenv("JOB"))) ckstrncpy(tn_env_job,p,63);
  2066.     if ((p = getenv("ACCT"))) ckstrncpy(tn_env_acct,p,63);
  2067.     if ((p = getenv("PRINTER"))) ckstrncpy(tn_env_prnt,p,63);
  2068.     if ((p = getenv("DISPLAY"))) ckstrncpy(tn_env_disp,p,63);
  2069. #ifdef aegis
  2070.     ckstrncpy(tn_env_sys,"Aegis",64);
  2071. #else
  2072. #ifdef Plan9
  2073.     ckstrncpy(tn_env_sys,"Plan9",64);
  2074. #else
  2075.     ckstrncpy(tn_env_sys,"UNIX",64);
  2076. #endif /* Plan9 */
  2077. #endif /* aegis */
  2078.     }
  2079. #endif /* CK_ENVIRONMENT */
  2080. #ifdef CK_SNDLOC
  2081.     {
  2082.     extern char * tn_loc;
  2083.     char *p;
  2084.     if (p = getenv("LOCATION"))
  2085.       if (tn_loc = (char *)malloc((int)strlen(p)+1))
  2086.         strcpy(tn_loc,p);        /* safe */
  2087.     }
  2088. #endif /* CK_SNDLOC */
  2089.  
  2090.     ckstrncpy(startupdir, zgtdir(), CKMAXPATH);
  2091.     startupdir[CKMAXPATH] = '\0';
  2092.     x = strlen(startupdir);
  2093.     if (x <= 0) {
  2094.     startupdir[0] = '/';
  2095.     startupdir[1] = '\0';
  2096.     } else if (startupdir[x-1] != '/') {
  2097.     startupdir[x] = '/';
  2098.     startupdir[x+1] = '\0';
  2099.     }
  2100.     debug(F110,"sysinit startupdir",startupdir,0);
  2101. #ifdef TTLEBUF
  2102.     le_init();
  2103. #endif /* TTLEBUF */
  2104. #ifdef BSD44ORPOSIX
  2105.     /* This should catch the ncurses platforms */
  2106.     /* Some platforms don't have putenv(), like NeXTSTEP */
  2107.     putenv("NCURSES_NO_SETBUF=1");
  2108. #endif /* BSD44ORPOSIX */
  2109.     return(0);
  2110. }
  2111.  
  2112. /*  S Y S C L E A N U P  --  System-dependent program cleanup.  */
  2113.  
  2114. int
  2115. syscleanup() {
  2116. #ifdef F_SETFL
  2117.     if (iniflags > -1)
  2118.       fcntl(0,F_SETFL,iniflags);    /* Restore stdin flags */
  2119. #endif /* F_SETFL */
  2120. #ifdef ultrix
  2121.     stty(0,&vanilla);                   /* Get sgtty info */
  2122. #endif /* ultrix */
  2123. #ifdef NETCMD
  2124.     if (ttpid) kill(ttpid,9);
  2125. #endif /* NETCMD */
  2126.     return(0);
  2127. }
  2128.  
  2129. /*  T T O P E N  --  Open a tty for exclusive access.  */
  2130.  
  2131. /*
  2132.   Call with:
  2133.     ttname: character string - device name or network host name.
  2134.     lcl:
  2135.   If called with lcl < 0, sets value of lcl as follows:
  2136.   0: the terminal named by ttname is the job's controlling terminal.
  2137.   1: the terminal named by ttname is not the job's controlling terminal.
  2138.   But watch out: if a line is already open, or if requested line can't
  2139.   be opened, then lcl remains (and is returned as) -1.
  2140.     modem:
  2141.   Less than zero: ttname is a network host name.
  2142.   Zero or greater: ttname is a terminal device name.
  2143.   Zero means a local connection (don't use modem signals).
  2144.   Positive means use modem signals.
  2145.    timo:
  2146.   0 = no timer.
  2147.   nonzero = number of seconds to wait for open() to return before timing out.
  2148.  
  2149.   Returns:
  2150.     0 on success
  2151.    -5 if device is in use
  2152.    -4 if access to device is denied
  2153.    -3 if access to lock directory denied
  2154.    -2 upon timeout waiting for device to open
  2155.    -1 on other error
  2156. */
  2157. static int ttotmo = 0;            /* Timeout flag */
  2158. /* Flag kept here to avoid being clobbered by longjmp.  */
  2159.  
  2160. int
  2161. ttopen(ttname,lcl,modem,timo) char *ttname; int *lcl, modem, timo; {
  2162.  
  2163. #ifdef BSD44
  2164. #define ctermid(x) strcpy(x,"")
  2165. #else
  2166. #ifdef SVORPOSIX
  2167. #ifndef CIE
  2168.     extern char *ctermid();        /* Wish they all had this! */
  2169. #else                    /* CIE Regulus */
  2170. #define ctermid(x) strcpy(x,"")
  2171. #endif /* CIE */
  2172. #endif /* SVORPOSIX */
  2173. #endif /* BSD44 */
  2174.  
  2175. #ifdef ultrix
  2176.     int temp = 0;
  2177. #endif /* ultrix */
  2178.  
  2179. #ifndef OPENFIRST
  2180.     char fullname[DEVNAMLEN+1];
  2181. #endif /* OPENFIRST */
  2182.  
  2183.     char * fnam;            /* Full name after expansion */
  2184.  
  2185.     int y;
  2186.  
  2187. #ifndef pdp11
  2188. #define NAMEFD     /* Feature to allow name to be an open file descriptor */
  2189. #endif /* pdp11 */
  2190.  
  2191. #ifdef NAMEFD
  2192.     char *p;
  2193.     debug(F101,"ttopen telnetfd","",telnetfd);
  2194. #endif /* NAMEFD */
  2195.  
  2196.     debug(F110,"ttopen ttname",ttname,0);
  2197.     debug(F110,"ttopen ttnmsv",ttnmsv,0);
  2198.     debug(F101,"ttopen modem","",modem);
  2199.     debug(F101,"ttopen netconn","",netconn);
  2200.     debug(F101,"ttopen ttyfd","",ttyfd);
  2201.     debug(F101,"ttopen *lcl","",*lcl);
  2202.     debug(F101,"ttopen ttmdm","",ttmdm);
  2203.     debug(F101,"ttopen ttnet","",ttnet);
  2204.  
  2205.     ttpmsk = 0xff;
  2206.     lockpid[0] = '\0';
  2207.  
  2208.     if (ttyfd > -1) {            /* If device already opened */
  2209.         if (!strncmp(ttname,ttnmsv,DEVNAMLEN)) /* are new & old names equal? */
  2210.       return(0);            /* Yes, nothing to do - just return */
  2211.     ttnmsv[0] = '\0';        /* No, clear out old name */
  2212.     ttclos(ttyfd);            /* close old connection.  */
  2213.     }
  2214.     wasclosed = 0;            /* New connection, not closed yet. */
  2215.     ttpipe = 0;                /* Assume it's not a pipe */
  2216.     ttpty = 0;                /* or a pty... */
  2217.  
  2218. #ifdef NETCONN
  2219. /*
  2220.   This is a bit tricky...  Suppose that previously Kermit had dialed a telnet
  2221.   modem server ("set host xxx:2001, set modem type usr, dial ...").  Then the
  2222.   connection was closed (ttyfd = -1), and then a REDIAL command was given.  At
  2223.   this point we've obliterated the negative modem type hack, and so would
  2224.   treat the IP hostname as a device name, and would then fail because of "No
  2225.   such device or directory".  But the previous connection has left behind some
  2226.   clues, so let's use them...
  2227. */
  2228.     if (ttyfd < 0) {            /* Connection is not open */
  2229.     if (!strcmp(ttname,ttnmsv)) {    /* Old and new names the same? */
  2230.         if (((netconn > 0) && (ttmdm < 0)) ||
  2231.         ((ttnet > 0) &&
  2232.          (!ckstrchr(ttname,'/')) && (ckstrchr(ttname,':')))
  2233.         ) {
  2234.         int x, rc;
  2235.         x = (ttmdm < 0) ? -ttmdm : ttnet;
  2236.         rc = netopen(ttname, lcl, x);
  2237.         debug(F111,"ttopen REOPEN netopen",ttname,rc);
  2238.         if (rc > -1) {
  2239.             netconn = 1;
  2240.             xlocal = *lcl = 1;
  2241.         } else {
  2242.             netconn = 0;
  2243.         }
  2244.         gotsigs = 0;
  2245.         return(rc);
  2246.         }
  2247.     }
  2248.     }
  2249. #endif /* NETCONN */
  2250.  
  2251. #ifdef MAXNAMLEN
  2252.     debug(F100,"ttopen MAXNAMLEN defined","",0);
  2253. #else
  2254.     debug(F100,"ttopen MAXNAMLEN *NOT* defined","",0);
  2255. #endif
  2256.  
  2257. #ifdef BSD4
  2258.     debug(F100,"ttopen BSD4 defined","",0);
  2259. #else
  2260.     debug(F100,"ttopen BSD4 *NOT* defined","",0);
  2261. #endif /* BSD4 */
  2262.  
  2263. #ifdef BSD42
  2264.     debug(F100,"ttopen BSD42 defined","",0);
  2265. #else
  2266.     debug(F100,"ttopen BSD42 *NOT* defined","",0);
  2267. #endif /* BSD42 */
  2268.  
  2269. #ifdef MYREAD
  2270.     debug(F100,"ttopen MYREAD defined","",0);
  2271. #else
  2272.     debug(F100,"ttopen MYREAD *NOT* defined","",0);
  2273. #endif /* MYREAD */
  2274.  
  2275. #ifdef    NETCONN
  2276.     if (modem < 0) {            /* modem < 0 = code for network */
  2277.     int x;
  2278.     ttmdm = modem;
  2279.     modem = -modem;            /* Positive network type number */
  2280.     fdflag = 0;            /* Stdio not redirected. */
  2281.     netconn = 1;            /* And it's a network connection */
  2282.     debug(F111,"ttopen net",ttname,modem);
  2283. #ifdef NAMEFD
  2284.     for (p = ttname; isdigit(*p); p++) ; /* Check for all digits */
  2285.      if (*p == '\0' && (telnetfd || x25fd)) { /* Avoid X.121 addresses */
  2286.         ttyfd = atoi(ttname);    /* Is there a way to test it's open? */
  2287.         ttfdflg = 1;        /* We got an open file descriptor */
  2288.         debug(F111,"ttopen got open network fd",ttname,ttyfd);
  2289.         ckstrncpy(ttnmsv,ttname,DEVNAMLEN); /* Remember the "name". */
  2290.         x = 1;            /* Return code is "good". */
  2291.         if (telnetfd) {
  2292.         ttnet = NET_TCPB;
  2293.         if (ttnproto != NP_TCPRAW)
  2294.           ttnproto = NP_TELNET;
  2295. #ifdef SUNX25
  2296.         } else if (x25fd) {
  2297.         ttnet = NET_SX25;
  2298.         ttnproto = NP_NONE;
  2299. #endif /* SUNX25 */
  2300.         }
  2301.     } else {            /* Host name or address given */
  2302. #ifdef NETPTY
  2303.         if (modem == NET_PTY) {
  2304.         int x;
  2305.         if (nopush) {
  2306.             debug(F100,"ttopen PTY: nopush","",0);
  2307.             return(-1);
  2308.         }
  2309.                 ttnet = NET_PTY;
  2310.         ttnproto = NP_NONE;
  2311.                 netconn = 1;            /* but we don't use network i/o */
  2312.                 ttpty = 1;
  2313.                 debug(F110,"ttopen PTY",ttname,0);
  2314.         x = do_pty(ttname);
  2315.         if (x > -1) {
  2316.             ckstrncpy(ttnmsv,ttname,DEVNAMLEN);
  2317.             xlocal = *lcl = 1;    /* It's local */
  2318.         } else {
  2319.             ttpty = 0;
  2320.             netconn = 0;
  2321.         }
  2322.         gotsigs = 0;
  2323.         return(x);
  2324.         }
  2325. #endif /* NETPTY */
  2326. #ifdef NETCMD
  2327. /*
  2328.   dup2() is not available on older System V platforms like AT&T 3Bx.  For
  2329.   those systems we punt by not defining NETCMD, but we might be able to do
  2330.   better -- see workarounds for this problem in ckufio.c (search for dup2).
  2331. */
  2332.         if (modem == NET_CMD) {
  2333.         if (nopush) {
  2334.             debug(F100,"ttopen pipe: nopush","",0);
  2335.             return(-1);
  2336.         }
  2337.         if (pipe(pipe0) || pipe(pipe1)) {
  2338.             perror("Pipe error");
  2339.             return(-1);
  2340.         }
  2341.         ttpid = fork();        /* Make a fork */
  2342.  
  2343.         switch (ttpid) {
  2344.           case -1:        /* Error making fork */
  2345.             close(pipe0[0]);
  2346.             close(pipe0[1]);
  2347.             close(pipe1[0]);
  2348.             close(pipe1[1]);
  2349.             perror("Fork error");
  2350.             return(-1);
  2351.           case 0:        /* Child. */
  2352.             close(pipe0[0]);
  2353.             close(pipe1[1]);
  2354.             dup2(pipe0[1], 1);
  2355.             close(pipe0[1]);
  2356.             dup2(pipe1[0], 0);
  2357.             close(pipe1[0]);
  2358.             system(ttname);
  2359.             _exit(0);
  2360.           default:        /* Parent */
  2361.             close(pipe0[1]);
  2362.             close(pipe1[0]);
  2363.             fdin = pipe0[0];    /* Read from pipe */
  2364.             fdout = pipe1[1];    /* Write to pipe */
  2365.             ttout = fdopen(fdout,"w"); /* Get stream so we can */
  2366.             if (!ttout) {    /* make it unbuffered. */
  2367.             perror("fdopen failure");
  2368.             return(-1);
  2369.             }
  2370.             setbuf(ttout,NULL);
  2371.             ckstrncpy(ttnmsv,ttname,DEVNAMLEN);
  2372.             xlocal = *lcl = 1;    /* It's local */
  2373.             netconn = 1;    /* Call it a network connection */
  2374.             ttmdm = modem;    /* Remember network type */
  2375.             ttyfd = fdin;
  2376.             ttpipe = 1;
  2377.             gotsigs = 0;
  2378.             return(0);
  2379.         }
  2380.         }
  2381. #endif /* NETCMD */
  2382. #endif /* NAMEFD */
  2383.         x = netopen(ttname, lcl, modem); /* (see ckcnet.h) */
  2384.         if (x > -1) {
  2385.         ckstrncpy(ttnmsv,ttname,DEVNAMLEN);
  2386.         } else netconn = 0;
  2387. #ifdef NAMEFD
  2388.     }
  2389. #endif /* NAMEFD */
  2390.  
  2391. #ifdef sony_news            /* Sony NEWS */
  2392.     if (ioctl(ttyfd,TIOCKGET,&km_ext) < 0) { /* Get Kanji mode */
  2393.         perror("ttopen error getting Kanji mode (network)");
  2394.         debug(F111,"ttopen error getting Kanji mode","network",0);
  2395.         km_ext = -1;        /* Make sure this stays undefined. */
  2396.     }
  2397. #endif /* sony_news */
  2398.  
  2399.     xlocal = *lcl = 1;        /* Network connections are local. */
  2400.     debug(F101,"ttopen net x","",x);
  2401. #ifdef COMMENT
  2402. /* Let netopen() do this */
  2403.     if (x > -1 && !x25fd)
  2404.       x = tn_ini();            /* Initialize TELNET protocol */
  2405. #endif /* COMMENT */
  2406.     gotsigs = 0;
  2407.     return(x);
  2408.     } else {                /* Terminal device */
  2409. #endif    /* NETCONN */
  2410.  
  2411. #ifdef NAMEFD
  2412. /*
  2413.   This code lets you give Kermit an open file descriptor for a serial
  2414.   communication device, rather than a device name.  Kermit assumes that the
  2415.   line is already open, locked, conditioned with the right parameters, etc.
  2416. */
  2417.     for (p = ttname; isdigit(*p); p++) ; /* Check for all digits */
  2418.     if (*p == '\0') {
  2419.         ttyfd = atoi(ttname);    /* Is there a way to test it's open? */
  2420.         debug(F111,"ttopen got open fd",ttname,ttyfd);
  2421.         ckstrncpy(ttnmsv,ttname,DEVNAMLEN); /* Remember the "name". */
  2422.         if (ttyfd == 0)        /* If it's stdio... */
  2423.           xlocal = *lcl = 0;    /* we're in remote mode */
  2424.         else            /* otherwise */
  2425.           xlocal = *lcl = 1;    /* local mode. */
  2426.         netconn = 0;        /* Assume it's not a network. */
  2427.         tvtflg = 0;            /* Might need to initialize modes. */
  2428.         ttmdm = modem;        /* Remember modem type. */
  2429.         fdflag = 0;            /* Stdio not redirected. */
  2430.         ttfdflg = 1;        /* Flag we were opened this way. */
  2431.  
  2432. #ifdef sony_news            /* Sony NEWS */
  2433.         /* Get device Kanji mode */
  2434.         if (ioctl(ttyfd,TIOCKGET,&km_ext) < 0) {
  2435.         perror("ttopen error getting Kanji mode");
  2436.         debug(F101,"ttopen error getting Kanji mode","",0);
  2437.         km_ext = -1;        /* Make sure this stays undefined. */
  2438.         }
  2439. #endif /* sony_news */
  2440.         gotsigs = 0;
  2441.         return(0);            /* Return success */
  2442.     }
  2443. #endif /* NAMEFD */
  2444. #ifdef NETCONN
  2445.     }
  2446. #endif /* NETCONN */
  2447.  
  2448. /* Here we have to open a serial device of the given name. */
  2449.  
  2450.     netconn = 0;            /* So it's not a network connection */
  2451.     occt = signal(SIGINT, cctrap);    /* Set Control-C trap, save old one */
  2452.     sigint_ign = 0;
  2453.  
  2454.     tvtflg = 0;            /* Flag for use by ttvt(). */
  2455.                 /* 0 = ttvt not called yet for this device */
  2456.  
  2457.     fdflag = (!isatty(0) || !isatty(1)); /* Flag for stdio redirected */
  2458.     debug(F101,"ttopen fdflag","",fdflag);
  2459.  
  2460.     ttmdm = modem;                      /* Make this available to other fns */
  2461.     xlocal = *lcl;                      /* Make this available to other fns */
  2462.  
  2463. /* Code for handling bidirectional tty lines goes here. */
  2464. /* Use specified method for turning off logins and suppressing getty. */
  2465.  
  2466. #ifdef ACUCNTRL
  2467.     /* Should put call to priv_on() here, but that would be very risky! */
  2468.     acucntrl("disable",ttname);         /* acucntrl() program. */
  2469.     /* and priv_off() here... */
  2470. #else
  2471. #ifdef ATT7300
  2472.     if ((attmodem & DOGETY) == 0)       /* offgetty() program. */
  2473.       attmodem |= offgetty(ttname);    /* Remember response.  */
  2474. #endif /* ATT7300 */
  2475. #endif /* ACUCNTRL */
  2476.  
  2477. #ifdef OPENFIRST
  2478. /*
  2479.  1985-2001: opens device first then gets lock; reason:
  2480.  Kermit usually has to run setuid or setgid in order to create a lockfile.
  2481.  If you give a SET LINE command for a device that happens to be your job's
  2482.  controlling terminal, Kermit doesn't have to create a lockfile, and in fact
  2483.  should not create one, and would fail if it tried to if it did not have the
  2484.  required privileges.  But you can't find out if two tty device names are
  2485.  equivalent until you have a file descriptor that you can give to ttyname().
  2486.  But this can cause a race condition between Kermit and [m]getty.  So see
  2487.  the [#]else part...
  2488. */ 
  2489.  
  2490. /*
  2491.  In the following section, we open the tty device for read/write.
  2492.  If a modem has been specified via "set modem" prior to "set line"
  2493.  then the O_NDELAY parameter is used in the open, provided this symbol
  2494.  is defined (e.g. in fcntl.h), so that the program does not hang waiting
  2495.  for carrier (which in most cases won't be present because a connection
  2496.  has not been dialed yet).  O_NDELAY is removed later on in ttopen().  It
  2497.  would make more sense to first determine if the line is local before
  2498.  doing this, but because ttyname() requires a file descriptor, we have
  2499.  to open it first.  See do_open().
  2500.  
  2501.  Now open the device using the desired treatment of carrier.
  2502.  If carrier is REQUIRED, then open could hang forever, so an optional
  2503.  timer is provided.  If carrier is not required, the timer should never
  2504.  go off, and should do no harm...
  2505. */
  2506.     ttotmo = 0;                /* Flag no timeout */
  2507.     debug(F101,"ttopen timo","",timo);
  2508.     debug(F101,"ttopen xlocal","",xlocal);
  2509.     if (timo > 0) {
  2510.     int xx;
  2511.     saval = signal(SIGALRM,timerh);    /* Timed, set up timer. */
  2512.     xx = alarm(timo);        /* Timed open() */
  2513.     debug(F101,"ttopen alarm","",xx);
  2514.     if (
  2515. #ifdef CK_POSIX_SIG
  2516.         sigsetjmp(sjbuf,1)
  2517. #else
  2518.         setjmp(sjbuf)
  2519. #endif /* CK_POSIX_SIG */
  2520.         ) {
  2521.         ttotmo = 1;            /* Flag timeout. */
  2522.     } else ttyfd = do_open(ttname);
  2523.     ttimoff();
  2524.     debug(F111,"ttopen","modem",modem);
  2525.     debug(F101,"ttopen ttyfd","",ttyfd);
  2526.     debug(F101,"ttopen alarm return","",ttotmo);
  2527.     } else {
  2528.     errno = 0;
  2529.     ttyfd = do_open(ttname);
  2530.     }
  2531.     debug(F111,"ttopen ttyfd",ttname,ttyfd);
  2532.     if (ttyfd < 0) {            /* If couldn't open, fail. */
  2533.     debug(F101,"ttopen errno","",errno);
  2534.     if (errno > 0 && !quiet)
  2535.       perror(ttname);        /* Print message */
  2536.  
  2537. #ifdef ATT7300
  2538.     if (attmodem & DOGETY)        /* was getty(1m) running before us? */
  2539.       ongetty(ttnmsv);        /* yes, restart on tty line */
  2540.     attmodem &= ~DOGETY;        /* no phone in use, getty restored */
  2541. #else
  2542. #ifdef ACUCNTRL
  2543.         /* Should put call to priv_on() here, but that would be risky! */
  2544.     acucntrl("enable",ttname);    /* acucntrl() program. */
  2545.     /* and priv_off() here... */
  2546. #endif /* ACUNTRL */
  2547. #endif /* ATT7300 */
  2548.  
  2549.     signal(SIGINT,occt);        /* Put old Ctrl-C trap back. */
  2550.     if (errno == EACCES) {        /* Device is protected against user */
  2551.         debug(F110,"ttopen EACCESS",ttname,0); /* Return -4 */
  2552.         return(-4);
  2553.     } else return(ttotmo ? -2 : -1); /* Otherwise -2 if timeout, or -1 */
  2554.     }
  2555.  
  2556. #ifdef QNX
  2557.     {
  2558.     extern int qnxportlock;
  2559.     x = qnxopencount();
  2560.     debug(F101,"ttopen qnxopencount","",x);
  2561.     debug(F101,"ttopen qnxportlock","",qnxportlock);
  2562.     if (x < 0 && qnxportlock) {
  2563.         ttclos(0);
  2564.         printf("?Can't get port open count\n");
  2565.         printf("(Try again with SET QNX-PORT-LOCK OFF)\n");
  2566.         return(-1);            /* Indicate device is in use */
  2567.     }
  2568.     if (x > 1) {            /* 1 == me */
  2569.         if (qnxportlock)
  2570.           ttclos(0);
  2571.           return(-2);        /* Indicate device is in use */
  2572.         else if (!quiet)
  2573.           printf("WARNING: \"%s\" looks busy...\n",ttdev);
  2574.     }
  2575.     }
  2576. #endif /* QNX */
  2577.  
  2578. #ifdef Plan9
  2579.     /* take this opportunity to open the control channel */
  2580.     if (p9openttyctl(ttname) < 0)
  2581. #else
  2582.     /* Make sure it's a real tty. */
  2583.     if (!isatty(ttyfd) && strcmp(ttname,"/dev/null"))
  2584. #endif /* Plan9 */
  2585.       {
  2586.     fprintf(stderr,"%s is not a terminal device\n",ttname);
  2587.     debug(F111,"ttopen not a tty",ttname,errno);
  2588.     close(ttyfd);
  2589.     ttyfd = -1;
  2590.     wasclosed = 1;
  2591.     signal(SIGINT,occt);
  2592.     return(-1);
  2593.     }
  2594.  
  2595. #ifdef aegis
  2596.     /* Apollo C runtime claims that console pads are tty devices, which
  2597.      * is reasonable, but they aren't any good for packet transfer. */
  2598.     ios_$inq_type_uid((short)ttyfd, ttyuid, st);
  2599.     if (st.all != status_$ok) {
  2600.         fprintf(stderr, "problem getting tty object type: ");
  2601.         error_$print(st);
  2602.     } else if (ttyuid != sio_$uid) { /* reject non-SIO lines */
  2603.         close(ttyfd); ttyfd = -1;
  2604.         wasclosed = 1;
  2605.         errno = ENOTTY; perror(ttname);
  2606.         signal(SIGINT,occt);
  2607.         return(-1);
  2608.     }
  2609. #endif /* aegis */
  2610.  
  2611.     sigint_ign = (occt == SIG_IGN) ? 1 : 0;
  2612.  
  2613.     ckstrncpy(ttnmsv,ttname,DEVNAMLEN);    /* Keep copy of name locally. */
  2614.  
  2615. /* Caller wants us to figure out if line is controlling tty */
  2616.  
  2617.     if (*lcl < 0) {
  2618.         if (strcmp(ttname,CTTNAM) == 0) { /* "/dev/tty" always remote */
  2619.             xlocal = 0;
  2620.         debug(F111,"ttopen ttname=CTTNAM",ttname,xlocal);
  2621.         } else if (strcmp(ttname,cttnam) == 0) {
  2622.             xlocal = 0;
  2623.         debug(F111,"ttopen ttname=cttnam",ttname,xlocal);
  2624.     } else if (cttnam[0]) {
  2625. #ifdef BEBOX_DR7
  2626.             x = ttnmsv;            /* ttyname() is broken */
  2627. #else
  2628.             x = ttyname(ttyfd);         /* Get real name of ttname. */
  2629. #endif /* BEBOX_DR7 */
  2630.         if (!x) x = "";
  2631.         if (*x)
  2632.           xlocal = ((strncmp(x,cttnam,DEVNAMLEN) == 0) ? 0 : 1);
  2633.         else
  2634.           xlocal = 1;
  2635.             debug(F111,"ttopen ttyname(ttyfd) xlocal",x,xlocal);
  2636.         }
  2637.     }
  2638.  
  2639. #ifndef NOFDZERO
  2640. /* Note, the following code was added so that Unix "idle-line" snoopers */
  2641. /* would not think Kermit was idle when it was transferring files, and */
  2642. /* maybe log people out. */
  2643.     if (xlocal == 0) {            /* Remote mode */
  2644.     if (fdflag == 0) {        /* Standard i/o is not redirected */
  2645.         debug(F100,"ttopen setting ttyfd = 0","",0);
  2646. #ifdef LYNXOS
  2647.         /* On Lynx OS, fd 0 is open for read only. */
  2648.         dup2(ttyfd,0);
  2649. #endif /* LYNXOS */
  2650.         close(ttyfd);        /* Use file descriptor 0 */
  2651.         ttyfd = 0;
  2652.     } else {            /* Standard i/o is redirected */
  2653.         debug(F101,"ttopen stdio redirected","",ttyfd);
  2654.     }
  2655.     }
  2656. #endif /* NOFDZERO */
  2657.  
  2658. /* Now check if line is locked -- if so fail, else lock for ourselves */
  2659. /* Note: After having done this, don't forget to delete the lock if you */
  2660. /* leave ttopen() with an error condition. */
  2661.  
  2662.     lkf = 0;                            /* Check lock */
  2663.     if (xlocal > 0) {
  2664.     int xx; int xpid;
  2665.         if ((xx = ttlock(ttname)) < 0) { /* Can't lock it. */
  2666.             debug(F111,"ttopen ttlock fails",ttname,xx);
  2667.         /* WARNING - This close() can hang if tty is an empty socket... */
  2668.             close(ttyfd);        /* Close the device. */
  2669.         ttyfd = -1;            /* Erase its file descriptor. */
  2670.         wasclosed = 1;
  2671.         signal(SIGINT,occt);    /* Put old SIGINT back. */
  2672.         sigint_ign = (occt == SIG_IGN) ? 1 : 0;
  2673.         if (xx == -2) {        /* If lockfile says device in use, */
  2674. #ifndef NOUUCP
  2675.         debug(F111,"ttopen reading lockfile pid",flfnam,xx);
  2676.         xpid = ttrpid(flfnam);    /* Try to read pid from lockfile */
  2677.         if (xpid > -1) {    /* If we got a pid */
  2678.                     if (!quiet)
  2679.               printf("Locked by process %d\n",xpid); /* tell them. */
  2680.             sprintf(lockpid,"%d",xpid);    /* Record it too */
  2681.             debug(F110,"ttopen lockpid",lockpid,0);
  2682.         } else if (*flfnam) {
  2683.             extern char *DIRCMD;
  2684.             char *p = NULL;
  2685.             int x;
  2686.             x = (int)strlen(flfnam) + (int)strlen(DIRCMD) + 2;
  2687.             p = malloc(x);    /* Print a directory listing. */
  2688. /*
  2689.   Note: priv_on() won't help here, because we do not pass privs along to
  2690.   to inferior processes, in this case ls.  So if the real user does not have
  2691.   directory-listing access to the lockfile directory, this will result in
  2692.   something like "not found".  That's why we try this only as a last resort.
  2693. */
  2694.             if (p) {        /* If we got the space... */
  2695.             ckmakmsg(p,x,DIRCMD," ",flfnam,NULL);
  2696.             zsyscmd(p);    /* Get listing. */
  2697.             if (p) {    /* free the space */
  2698.                 free(p);
  2699.                 p = NULL;
  2700.             }
  2701.             }
  2702.         }
  2703. #endif /* NOUUCP */
  2704.         return(-5);        /* Code for device in use */
  2705.         } else return(-3);        /* Access denied */
  2706.         } else lkf = 1;
  2707.     }
  2708. #else  /* OPENFIRST */
  2709.  
  2710. /*
  2711.   27 Oct 2001: New simpler code that gets the lock first and then opens the
  2712.   device, which eliminates the race condition.  The downside is you can no
  2713.   longer say "set line /dev/ttyp0" or whatever, where /dev/ttyp0 is your login
  2714.   terminal, without trying to create a lockfile, which fails if C-Kermit lacks
  2715.   privs, and if it succeeds, it has created a lockfile where it didn't create
  2716.   one before.
  2717. */
  2718.     xlocal = *lcl;            /* Is the device my login terminal? */
  2719.     debug(F111,"ttopen xlocal","A",xlocal);
  2720.     fnam = ttname;
  2721.     if (strcmp(ttname,CTTNAM) && netconn == 0) {
  2722.     if (zfnqfp(ttname,DEVNAMLEN+1,fullname)) {
  2723.         if ((int)strlen(fullname) > 0)
  2724.           fnam = fullname;
  2725.     }
  2726.     }
  2727.     debug(F110,"ttopen fnam",fnam,0);
  2728.     if (xlocal < 0) {
  2729.     xlocal = (strcmp(fnam,CTTNAM) != 0);
  2730.     }
  2731.     debug(F111,"ttopen xlocal","B",xlocal);
  2732.  
  2733.     lkf = 0;                            /* No lock yet */
  2734.     if (xlocal > 0) {            /* If not... */
  2735.     int xx; int xpid;
  2736.     xx = ttlock(fnam);        /* Try to lock it. */
  2737.     debug(F101,"ttopen ttlock","",xx);
  2738.         if (xx < 0) {            /* Can't lock it. */
  2739.             debug(F111,"ttopen ttlock fails",fnam,xx);
  2740.         if (xx == -2) {        /* If lockfile says device in use, */
  2741. #ifndef NOUUCP
  2742.         debug(F111,"ttopen reading lockfile pid",flfnam,xx);
  2743.         xpid = ttrpid(flfnam);    /* Try to read pid from lockfile */
  2744.         if (xpid > -1) {    /* If we got a pid */
  2745.                     if (!quiet)
  2746.               printf("Locked by process %d\n",xpid); /* tell them. */
  2747.             ckstrncpy(lockpid,ckitoa(xpid),16);
  2748.             debug(F110,"ttopen lockpid",lockpid,0);
  2749. #ifndef NOPUSH
  2750.         } else if (flfnam[0] && !nopush) {
  2751.             extern char *DIRCMD;
  2752.             char *p = NULL;
  2753.             int x;
  2754.             x = (int)strlen(flfnam) + (int)strlen(DIRCMD) + 2;
  2755.             p = malloc(x);    /* Print a directory listing. */
  2756. /*
  2757.   Note: priv_on() won't help here, because we do not pass privs along to
  2758.   to inferior processes, in this case ls.  So if the real user does not have
  2759.   directory-listing access to the lockfile directory, this will result in
  2760.   something like "not found".  That's why we try this only as a last resort.
  2761. */
  2762.             if (p) {        /* If we got the space... */
  2763.             ckmakmsg(p,x,DIRCMD," ",flfnam,NULL);
  2764.             zsyscmd(p);    /* Get listing. */
  2765.             if (p) {    /* free the space */
  2766.                 free(p);
  2767.                 p = NULL;
  2768.             }
  2769.             }
  2770. #endif /* NOPUSH */
  2771.         }
  2772. #endif /* NOUUCP */
  2773.         return(-5);        /* Code for device in use */
  2774.         } else return(-3);        /* Access denied */
  2775.         } else lkf = 1;
  2776.     }
  2777.     /* Have lock -- now it's safe to open the device */
  2778.  
  2779.     debug(F101,"ttopen lkf","",lkf);
  2780.     debug(F101,"ttopen timo","",timo);
  2781.  
  2782.     ttotmo = 0;                /* Flag no timeout */
  2783.     if (timo > 0) {
  2784.     int xx;
  2785.     saval = signal(SIGALRM,timerh);    /* Timed, set up timer. */
  2786.     xx = alarm(timo);        /* Timed open() */
  2787.     debug(F101,"ttopen alarm","",xx);
  2788.     if (
  2789. #ifdef CK_POSIX_SIG
  2790.         sigsetjmp(sjbuf,1)
  2791. #else
  2792.         setjmp(sjbuf)
  2793. #endif /* CK_POSIX_SIG */
  2794.         ) {
  2795.         ttotmo = 1;            /* Flag timeout. */
  2796.     } else {
  2797.         ttyfd = do_open(fnam);
  2798.     }
  2799.     ttimoff();
  2800.     debug(F111,"ttopen timed ttyfd",fnam,ttyfd);
  2801.     } else {
  2802.     errno = 0;
  2803.     ttyfd = do_open(fnam);
  2804.     debug(F111,"ttopen untimed ttyfd",fnam,ttyfd);
  2805.     }
  2806.     if (ttyfd < 0) {            /* If couldn't open, fail. */
  2807.     debug(F111,"ttopen errno",fnam,errno);
  2808.     debug(F111,"ttopen xlocal","C",xlocal);
  2809.     if (xlocal == 0) {
  2810.         debug(F100,"ttopen substituting 0","",0);
  2811.         ttyfd = 0;
  2812.     } else {
  2813.         if (errno > 0 && !quiet) {
  2814.             debug(F111,"ttopen perror",fnam,errno);
  2815.         perror(fnam);        /* Print message */
  2816.         }
  2817.         if (ttunlck())                  /* Release the lock file */
  2818.           fprintf(stderr,"Warning, problem releasing lock\r\n");
  2819.     }
  2820.     }
  2821.  
  2822.     if (ttyfd < 0) {            /* ttyfd is still < 0? */
  2823. #ifdef ATT7300
  2824.     if (attmodem & DOGETY)        /* was getty(1m) running before us? */
  2825.       ongetty(ttnmsv);        /* yes, restart on tty line */
  2826.     attmodem &= ~DOGETY;        /* no phone in use, getty restored */
  2827. #else
  2828. #ifdef ACUCNTRL
  2829.         /* Should put call to priv_on() here, but that would be risky! */
  2830.     acucntrl("enable",fnam);    /* acucntrl() program. */
  2831.     /* and priv_off() here... */
  2832. #endif /* ACUNTRL */
  2833. #endif /* ATT7300 */
  2834.  
  2835.     signal(SIGINT,occt);        /* Put old Ctrl-C trap back. */
  2836.     if (errno == EACCES) {        /* Device is protected against user */
  2837.         debug(F110,"ttopen EACCESS",fnam,0); /* Return -4 */
  2838.         return(-4);
  2839.     } else return(ttotmo ? -2 : -1); /* Otherwise -2 if timeout, or -1 */
  2840.     }
  2841.  
  2842. /* Make sure it's a real tty. */
  2843.  
  2844. #ifdef Plan9
  2845.     /* take this opportunity to open the control channel */
  2846.     if (p9openttyctl(fnam) < 0)       
  2847. #else
  2848.       if (!isatty(ttyfd) && strcmp(fnam,"/dev/null"))
  2849. #endif /* Plan9 */
  2850.     {
  2851.         fprintf(stderr,"%s is not a terminal device\n",fnam);
  2852.         debug(F111,"ttopen not a tty",fnam,errno);
  2853.         if (ttunlck())        /* Release the lock file */
  2854.           fprintf(stderr,"Warning, problem releasing lock\r\n");
  2855.         close(ttyfd);
  2856.         ttyfd = -1;
  2857.         wasclosed = 1;
  2858.         signal(SIGINT,occt);
  2859.         return(-1);
  2860.     }
  2861.  
  2862. #ifdef aegis
  2863.     /*
  2864.       Apollo C runtime claims that console pads are tty devices, which
  2865.       is reasonable, but they aren't any good for packet transfer.
  2866.     */
  2867.     ios_$inq_type_uid((short)ttyfd, ttyuid, st);
  2868.     if (st.all != status_$ok) {
  2869.     fprintf(stderr, "problem getting tty object type: ");
  2870.     error_$print(st);
  2871.     } else if (ttyuid != sio_$uid) {    /* Reject non-SIO lines */
  2872.     close(ttyfd); ttyfd = -1;
  2873.     wasclosed = 1;
  2874.     errno = ENOTTY; perror(fnam);
  2875.     signal(SIGINT,occt);
  2876.     return(-1);
  2877.     }
  2878. #endif /* aegis */
  2879.  
  2880.     sigint_ign = (occt == SIG_IGN) ? 1 : 0;
  2881.  
  2882.     ckstrncpy(ttnmsv,ttname,DEVNAMLEN);    /* Keep copy of name locally. */
  2883.  
  2884. /* Caller wants us to figure out if line is controlling tty */
  2885.  
  2886.     if (*lcl < 0) {
  2887.     char * s;
  2888.         if (strcmp(fnam,CTTNAM) == 0) { /* "/dev/tty" always remote */
  2889.             xlocal = 0;
  2890.         debug(F111,"ttopen fnam=CTTNAM",fnam,xlocal);
  2891.         } else if (strcmp(fnam,cttnam) == 0) {
  2892.             xlocal = 0;
  2893.         debug(F111,"ttopen fnam=cttnam",fnam,xlocal);
  2894.     } else if (cttnam[0]) {
  2895. #ifdef BEBOX_DR7
  2896.             s = ttnmsv;            /* ttyname() is broken */
  2897. #else
  2898.             s = ttyname(ttyfd);         /* Get real name of ttname. */
  2899. #endif /* BEBOX_DR7 */
  2900.         if (!s) s = "";
  2901.         if (*s)
  2902.           xlocal = ((strncmp(s,cttnam,DEVNAMLEN) == 0) ? 0 : 1);
  2903.         else
  2904.           xlocal = 1;
  2905.             debug(F111,"ttopen ttyname(ttyfd) xlocal",s,xlocal);
  2906.         }
  2907.     }
  2908.  
  2909. #ifndef NOFDZERO
  2910. /* Note, the following code was added so that Unix "idle-line" snoopers */
  2911. /* would not think Kermit was idle when it was transferring files, and */
  2912. /* maybe log people out. */
  2913.     if (xlocal == 0) {            /* Remote mode */
  2914.     if (fdflag == 0) {        /* Standard i/o is not redirected */
  2915.         debug(F100,"ttopen setting ttyfd = 0","",0);
  2916. #ifdef LYNXOS
  2917.         /* On Lynx OS, fd 0 is open for read only. */
  2918.         dup2(ttyfd,0);
  2919. #endif /* LYNXOS */
  2920.         close(ttyfd);        /* Use file descriptor 0 */
  2921.         ttyfd = 0;
  2922.     } else {            /* Standard i/o is redirected */
  2923.         debug(F101,"ttopen stdio redirected","",ttyfd);
  2924.     }
  2925.     }
  2926. #endif /* NOFDZERO */
  2927. #endif /* OPENFIRST */
  2928.  
  2929. /* Got the line, now set the desired value for local. */
  2930.  
  2931.     if (*lcl != 0) *lcl = xlocal;
  2932.  
  2933. /* Some special stuff for v7... */
  2934.  
  2935. #ifdef  V7
  2936. #ifndef MINIX
  2937.     if (kmem[TTY] < 0) {        /*  If open, then skip this.  */
  2938.     qaddr[TTY] = initrawq(ttyfd);   /* Init the queue. */
  2939.     if ((kmem[TTY] = open("/dev/kmem", 0)) < 0) {
  2940.         fprintf(stderr, "Can't read /dev/kmem in ttopen.\n");
  2941.         perror("/dev/kmem");
  2942.         exit(1);
  2943.     }
  2944.     }
  2945. #endif /* !MINIX */
  2946. #endif /* V7 */
  2947.  
  2948. /* No failure returns after this point */
  2949.  
  2950. #ifdef ultrix
  2951.     ioctl(ttyfd, TIOCMODEM, &temp);
  2952. #ifdef TIOCSINUSE
  2953.     if (xlocal && ioctl(ttyfd, TIOCSINUSE, NULL) < 0) {
  2954.     if (!quiet)
  2955.       perror(fnam);
  2956.     }
  2957. #endif /* TIOCSINUSE */
  2958. #endif /* ultrix */
  2959.  
  2960. /* Get tty device settings  */
  2961.  
  2962. #ifdef BSD44ORPOSIX            /* POSIX */
  2963.     tcgetattr(ttyfd,&ttold);
  2964.     debug(F101,"ttopen tcgetattr ttold.c_lflag","",ttold.c_lflag);
  2965.     tcgetattr(ttyfd,&ttraw);
  2966.     debug(F101,"ttopen tcgetattr ttraw.c_lflag","",ttraw.c_lflag);
  2967.     tcgetattr(ttyfd,&tttvt);
  2968.     debug(F101,"ttopen tcgetattr tttvt.c_lflag","",tttvt.c_lflag);
  2969. #else                    /* BSD, V7, and all others */
  2970. #ifdef ATTSV                /* AT&T UNIX */
  2971.     ioctl(ttyfd,TCGETA,&ttold);
  2972.     debug(F101,"ttopen ioctl TCGETA ttold.c_lflag","",ttold.c_lflag);
  2973.     ioctl(ttyfd,TCGETA,&ttraw);
  2974.     ioctl(ttyfd,TCGETA,&tttvt);
  2975. #else
  2976. #ifdef BELLV10
  2977.     ioctl(ttyfd,TIOCGETP,&ttold);
  2978.     debug(F101,"ttopen BELLV10 ttold.sg_flags","",ttold.sg_flags);
  2979.     ioctl(ttyfd,TIOCGDEV,&tdold);
  2980.     debug(F101,"ttopen BELLV10 tdold.flags","",tdold.flags);
  2981. #else
  2982.     gtty(ttyfd,&ttold);
  2983.     debug(F101,"ttopen gtty ttold.sg_flags","",ttold.sg_flags);
  2984. #endif /* BELLV10 */
  2985.  
  2986. #ifdef sony_news            /* Sony NEWS */
  2987.     if (ioctl(ttyfd,TIOCKGET,&km_ext) < 0) { /* Get console Kanji mode */
  2988.     perror("ttopen error getting Kanji mode");
  2989.     debug(F101,"ttopen error getting Kanji mode","",0);
  2990.     km_ext = -1;            /* Make sure this stays undefined. */
  2991.     }
  2992. #endif /* sony_news */
  2993.  
  2994. #ifdef TIOCGETC
  2995.     debug(F100,"ttopen TIOCGETC","",0);
  2996.     tcharf = 0;                /* In remote mode, also get */
  2997.     if (xlocal == 0) {            /* special characters */
  2998.     if (ioctl(ttyfd,TIOCGETC,&tchold) < 0) {
  2999.         debug(F100,"ttopen TIOCGETC failed","",0);
  3000.     } else {
  3001.         tcharf = 1;            /* It worked. */
  3002.         ioctl(ttyfd,TIOCGETC,&tchnoi); /* Get another copy */
  3003.         debug(F100,"ttopen TIOCGETC ok","",0);
  3004.     }
  3005.     }
  3006. #else
  3007.     debug(F100,"ttopen TIOCGETC not defined","",0);
  3008. #endif /* TIOCGETC */
  3009.  
  3010. #ifdef TIOCGLTC
  3011.     debug(F100,"ttopen TIOCGLTC","",0);
  3012.     ltcharf = 0;            /* In remote mode, also get */
  3013.     if (xlocal == 0) {            /* local special characters */
  3014.     if (ioctl(ttyfd,TIOCGLTC,<chold) < 0) {
  3015.         debug(F100,"ttopen TIOCGLTC failed","",0);
  3016.     } else {
  3017.         ltcharf = 1;        /* It worked. */
  3018.         ioctl(ttyfd,TIOCGLTC,<chnoi); /* Get another copy */
  3019.         debug(F100,"ttopen TIOCGLTC ok","",0);
  3020.     }
  3021.     }
  3022. #else
  3023.     debug(F100,"ttopen TIOCGLTC not defined","",0);
  3024. #endif /* TIOCGLTC */
  3025.  
  3026. #ifdef TIOCLGET
  3027.     debug(F100,"ttopen TIOCLGET","",0);
  3028.     lmodef = 0;
  3029.     if (ioctl(ttyfd,TIOCLGET,&lmode) < 0) {
  3030.     debug(F100,"ttopen TIOCLGET failed","",0);
  3031.     } else {
  3032.     lmodef = 1;
  3033.     debug(F100,"ttopen TIOCLGET ok","",0);
  3034.     }
  3035. #endif /* TIOCLGET */
  3036.  
  3037. #ifdef BELLV10
  3038.     ioctl(ttyfd,TIOCGETP,&ttraw);
  3039.     ioctl(ttyfd,TIOCGETP,&tttvt);
  3040. #else
  3041.     gtty(ttyfd,&ttraw);                 /* And a copy of it for packets*/
  3042.     gtty(ttyfd,&tttvt);                 /* And one for virtual tty service */
  3043. #endif /* BELLV10 */
  3044.  
  3045. #endif /* ATTSV */
  3046. #endif /* BSD44ORPOSIX */
  3047.  
  3048. /* Section for changing line discipline.  It's restored in ttres(). */
  3049.  
  3050. #ifdef AIXRS
  3051. #ifndef AIX41
  3052.     { union txname ld_name; int ld_idx = 0;
  3053.       ttld = 0;
  3054.         do {
  3055.         ld_name.tx_which = ld_idx++;
  3056.         ioctl(ttyfd, TXGETCD, &ld_name);
  3057.       if (!strncmp(ld_name.tx_name, "rts", 3))
  3058.           ttld |= 1;
  3059.         } while (*ld_name.tx_name);
  3060.         debug(F101,"AIX line discipline","",ttld);
  3061.       }
  3062. #endif /* AIX41 */
  3063. #endif /* AIXRS */
  3064.  
  3065. #ifdef BSD41
  3066. /* For 4.1BSD only, force "old" tty driver, new one botches TANDEM. */
  3067.     { int k;
  3068.       ioctl(ttyfd, TIOCGETD, &ttld);    /* Get and save line discipline */
  3069.       debug(F101,"4.1bsd line discipline","",ttld);
  3070.       k = OTTYDISC;            /* Switch to "old" discipline */
  3071.       k = ioctl(ttyfd, TIOCSETD, &k);
  3072.       debug(F101,"4.1bsd tiocsetd","",k);
  3073.     }
  3074. #endif /* BSD41 */
  3075.  
  3076. #ifdef aegis
  3077.     /* This was previously done before the last two TCGETA or gtty above,
  3078.      * in both the ATTSV and not-ATTSV case.  If it is not okay to have only
  3079.      * one copy if it here instead, give us a shout!
  3080.      */
  3081.     sio_$control((short)ttyfd, sio_$raw_nl, false, st);
  3082.     if (xlocal) {       /* ignore breaks from local line */
  3083.         sio_$control((short)ttyfd, sio_$int_enable, false, st);
  3084.         sio_$control((short)ttyfd, sio_$quit_enable, false, st);
  3085.     }
  3086. #endif /* aegis */
  3087.  
  3088. #ifdef VXVE
  3089.     ttraw.c_line = 0;                   /* STTY line 0 for VX/VE */
  3090.     tttvt.c_line = 0;                   /* STTY line 0 for VX/VE */
  3091.     ioctl(ttyfd,TCSETA,&ttraw);
  3092. #endif /* vxve */
  3093.  
  3094. /* If O_NDELAY was used during open(), then remove it now. */
  3095.  
  3096. #ifdef O_NDELAY
  3097.     debug(F100,"ttopen O_NDELAY","",0);
  3098.     if (xlocal > 0) {
  3099.       if (fcntl(ttyfd, F_GETFL, 0) & O_NDELAY) {
  3100.     debug(F100,"ttopen fcntl O_NDELAY","",0);
  3101. #ifndef aegis
  3102.     if (fcntl(ttyfd,F_SETFL, fcntl(ttyfd, F_GETFL, 0) & ~O_NDELAY) < 0) {
  3103.         debug(F100,"ttopen fcntl failure to unset O_NDELAY","",0);
  3104.         perror("Can't unset O_NDELAY");
  3105.     }
  3106. #endif /* aegis */
  3107.     /* Some systems, notably Xenix (don't know how common this is in
  3108.      * other systems), need special treatment to get rid of the O_NDELAY
  3109.      * behaviour on read() with respect to carrier presence (i.e. read()
  3110.      * returning 0 when carrier absent), even though the above fcntl()
  3111.      * is enough to make read() wait for input when carrier is present.
  3112.      * This magic, in turn, requires CLOCAL for working when the carrier
  3113.      * is absent. But if xlocal == 0, presumably you already have CLOCAL
  3114.      * or you have a carrier, otherwise you wouldn't be running this.
  3115.      */
  3116.     debug(F101,"ttopen xlocal","",xlocal);
  3117. #ifdef ATTSV
  3118. #ifdef BSD44ORPOSIX
  3119. #ifdef COMMENT                /* 12 Aug 1997 */
  3120. #ifdef __bsdi__
  3121.     if (xlocal)
  3122.       ttraw.c_cflag |= CLOCAL;
  3123. #else
  3124. #ifdef __FreeBSD__
  3125.     if (xlocal)
  3126.       ttraw.c_cflag |= CLOCAL;
  3127. #endif /* __FreeBSD__ */
  3128. #endif /* __bsdi__ */
  3129. #else /* Not COMMENT */
  3130. #ifdef CLOCAL
  3131.     if (xlocal)            /* Unset this if it's defined. */
  3132.       ttraw.c_cflag |= CLOCAL;
  3133. #endif /* CLOCAL */
  3134. #endif /* COMMENT */
  3135.     debug(F101,"ttopen BSD44ORPOSIX calling tcsetattr","",TCSADRAIN);
  3136.     if (tcsetattr(ttyfd, TCSADRAIN, &ttraw) < 0) {
  3137.         debug(F100,"ttopen POSIX tcseattr fails","",0);
  3138.         perror("tcsetattr");
  3139.     }
  3140. #else /* !BSD44ORPOSIX */
  3141.     if (xlocal) {
  3142.         ttraw.c_cflag |= CLOCAL;
  3143.         debug(F100,"ttopen calling ioctl(TCSETA)","",0);
  3144.         errno = 0;
  3145.         if (ioctl(ttyfd, TCSETA, &ttraw) < 0) {
  3146.                 debug(F101,"ttopen ioctl(TCSETA) fails","",errno);
  3147.                 perror("ioctl(TCSETA)");
  3148.             }
  3149.     }
  3150. #endif /* BSD44ORPOSIX */
  3151. #endif /* ATTSV */
  3152. #ifndef NOCOTFMC /* = NO Close(Open()) To Force Mode Change */
  3153. /* Reportedly lets uugetty grab the device in SCO UNIX 3.2 / XENIX 2.3 */
  3154.     debug(F100,"ttopen executing close/open","",0);
  3155.     close( priv_opn(fnam, O_RDWR) ); /* Magic to force change. */
  3156. #endif /* NOCOTFMC */
  3157.       }
  3158.     }
  3159. #endif /* O_NDELAY */
  3160.  
  3161. /* Instruct the system how to treat the carrier, and set a few other tty
  3162.  * parameters.
  3163.  *
  3164.  * This also undoes the temporary setting of CLOCAL that may have been done
  3165.  * for the close(open()) above (except in Xenix).  Also throw in ~ECHO, to
  3166.  * prevent the other end of the line from sitting there talking to itself,
  3167.  * producing garbage when the user performs a connect.
  3168.  *
  3169.  * SCO Xenix unfortunately seems to ignore the actual state of CLOCAL.
  3170.  * Now it thinks CLOCAL is always on. It seems the only real solution for
  3171.  * Xenix is to switch between the lower and upper case device names.
  3172.  *
  3173.  * This section may at some future time expand into setting a complete
  3174.  * collection of tty parameters, or call a function shared with ttpkt()/
  3175.  * ttvt() that does so.  On the other hand, the initial parameters are not
  3176.  * that important, since ttpkt() or ttvt() should always fix that before
  3177.  * any communication is done.  Well, we'll see...
  3178.  */
  3179.     if (xlocal) {
  3180.         curcarr = -2;
  3181.     debug(F100,"ttopen calling carrctl","",0);
  3182.     carrctl(&ttraw, ttcarr == CAR_ON);
  3183.     debug(F100,"ttopen carrctl ok","",0);
  3184.  
  3185. #ifdef COHERENT
  3186. #define SVORPOSIX
  3187. #endif /* COHERENT */
  3188.  
  3189. #ifdef SVORPOSIX
  3190.     ttraw.c_lflag &= ~ECHO;
  3191.     ttold.c_lflag &= ~ECHO;
  3192. #ifdef BSD44ORPOSIX
  3193.     y = tcsetattr(ttyfd, TCSADRAIN, &ttraw);
  3194.     debug(F101,"ttopen tcsetattr","",y);
  3195. #else
  3196.     y = ioctl(ttyfd, TCSETA, &ttraw);
  3197.     debug(F100,"ttopen ioctl","",y);
  3198. #endif /* BSD44ORPOSIX */
  3199.  
  3200. #else /* BSD, etc */
  3201.     ttraw.sg_flags &= ~ECHO;
  3202.     ttold.sg_flags &= ~ECHO;
  3203. #ifdef BELLV10
  3204.     y = ioctl(ttyfd,TIOCSETP,&ttraw);
  3205.     debug(F100,"ttopen ioctl","",y);
  3206. #else
  3207.     y = stty(ttyfd,&ttraw);
  3208.     debug(F100,"ttopen stty","",y);
  3209. #endif /* BELLV10 */
  3210. #endif /* SVORPOSIX */
  3211.  
  3212. #ifdef COHERENT
  3213. #undef SVORPOSIX
  3214. #endif /* COHERENT */
  3215.  
  3216.     /* ttflui(); */  /*  This fails for some reason.  */
  3217.     }
  3218.  
  3219.     /* Get current speed */
  3220.  
  3221. #ifndef BEBOX
  3222.     ttspeed = ttgspd();
  3223. #else
  3224.     ttspeed = 19200;
  3225. #endif /* !BEBOX */
  3226.     debug(F101,"ttopen ttspeed","",ttspeed);
  3227.  
  3228.     /* Done, make entries in debug log, restore Ctrl-C trap, and return. */
  3229.  
  3230.     debug(F101,"ttopen ttyfd","",ttyfd);
  3231.     debug(F101,"ttopen *lcl","",*lcl);
  3232.     debug(F111,"ttopen lock file",flfnam,lkf);
  3233.     signal(SIGINT,occt);
  3234.     sigint_ign = (occt == SIG_IGN) ? 1 : 0;
  3235.     gotsigs = 0;
  3236.     return(0);
  3237. }
  3238.  
  3239.  
  3240. /*  D O _ O P E N  --  Do the right kind of open() call for the tty. */
  3241.  
  3242. int
  3243. do_open(ttname) char *ttname; {
  3244.     int flags;
  3245.  
  3246. #ifdef QNX6
  3247.     /* O_NONBLOCK on /dev/tty makes open() fail */
  3248.     return(priv_opn(ttname, O_RDWR |
  3249.             (
  3250.              ((int)strcmp(ttname,"/dev/tty") == 0) ?
  3251.              0 :
  3252.              (ttcarr != CAR_ON) ? O_NONBLOCK : 0)
  3253.             )
  3254.        ); 
  3255. #else  /* !QNX6 */
  3256.  
  3257. #ifndef    O_NDELAY            /* O_NDELAY not defined */
  3258.     return(priv_opn(ttname,2));
  3259. #else                    /* O_NDELAY defined */
  3260.  
  3261. #ifdef ATT7300
  3262. /*
  3263.  Open comms line without waiting for carrier so initial call does not hang
  3264.  because state of "modem" is likely unknown at the initial call  -jrd.
  3265.  If this is needed for the getty stuff to work, and the open would not work
  3266.  without O_NDELAY when getty is still on, then this special case is ok.
  3267.  Otherwise, get rid of it. -ske
  3268. */
  3269.     return(priv_opn(ttname, O_RDWR | O_NDELAY));
  3270.  
  3271. #else    /* !ATT7300 */
  3272.  
  3273.     /* Normal case. Use O_NDELAY according to SET CARRIER. See ttscarr(). */
  3274.     flags = O_RDWR;
  3275.     debug(F101,"do_open xlocal","",xlocal);
  3276.     debug(F111,"do_open flags A",ttname,flags);
  3277.     if (xlocal && (ttcarr != CAR_ON))
  3278.       flags |= O_NDELAY;
  3279.     debug(F111,"do_open flags B",ttname,flags);
  3280.     return(priv_opn(ttname, flags));
  3281. #endif /* !ATT7300 */
  3282. #endif /* O_NDELAY */
  3283. #endif /* QNX6 */
  3284. }
  3285.  
  3286. /*  T T C L O S  --  Close the TTY, releasing any lock.  */
  3287.  
  3288. static int ttc_state = 0;        /* ttclose() state */
  3289. static char * ttc_nam[] = { "setup", "hangup", "reset", "close" };
  3290.  
  3291. int
  3292. ttclos(foo) int foo; {            /* Arg req'd for signal() prototype */
  3293.     int xx, x = 0;
  3294.     extern int exithangup;
  3295.  
  3296.     debug(F101,"ttclos ttyfd","",ttyfd);
  3297.     debug(F101,"ttclos netconn","",netconn);
  3298.     debug(F101,"ttclos xlocal","",xlocal);
  3299. #ifdef NOFDZERO
  3300.     debug(F100,"ttclos NOFDZERO","",0);
  3301. #endif /* NOFDZERO */
  3302.  
  3303. #ifdef COMMENT
  3304. #ifdef TTLEBUF
  3305.     le_init();                /* No need for any of this */
  3306. #endif /* TTLEBUF */
  3307. #endif /* COMMENT */
  3308.  
  3309.     if (ttyfd < 0)            /* Wasn't open. */
  3310.       return(0);
  3311.  
  3312.     if (ttfdflg)            /* If we inherited ttyfd from */
  3313.       return(0);            /* another process, don't close it. */
  3314.  
  3315.     tvtflg = 0;                /* (some day get rid of this...) */
  3316.     gotsigs = 0;
  3317.  
  3318. #ifdef IKSD
  3319.     if (inserver) {
  3320. #ifdef TNCODE
  3321.           tn_push();                    /* Place any waiting data into input*/
  3322.           tn_sopt(DO,TELOPT_LOGOUT);    /* Send LOGOUT option before close */
  3323.           TELOPT_UNANSWERED_DO(TELOPT_LOGOUT) = 1;
  3324.           tn_reset();                   /* The Reset Telnet Option table.  */
  3325. #endif /* TNCODE */
  3326. #ifdef CK_SSL
  3327.       if (ssl_active_flag) {
  3328.           if (ssl_debug_flag)
  3329.         BIO_printf(bio_err,"calling SSL_shutdown(ssl)\n");
  3330.           SSL_shutdown(ssl_con);
  3331.           SSL_free(ssl_con);
  3332.           ssl_con = NULL;
  3333.           ssl_active_flag = 0;
  3334.       }
  3335.       if (tls_active_flag) {
  3336.           if (ssl_debug_flag)
  3337.         BIO_printf(bio_err,"calling SSL_shutdown(tls)\n");
  3338.           SSL_shutdown(tls_con);
  3339.           SSL_free(tls_con);
  3340.           tls_con = NULL;
  3341.           tls_active_flag = 0;
  3342.       }
  3343. #endif /* CK_SSL */
  3344.     }
  3345. #endif /* IKSD */
  3346. #ifdef NETCMD
  3347.     if (ttpipe) {            /* We've been using a pipe */
  3348.     /* ttpipe = 0; */
  3349.     if (ttpid > 0) {
  3350.         int wstat;
  3351.         int statusp;
  3352.         close(fdin);        /* Close these. */
  3353.         close(fdout);
  3354.         fdin = fdout = -1;
  3355.         kill(ttpid,1);        /* Kill fork with SIGHUP */
  3356.         while (1) {
  3357.         wstat = wait(&statusp);
  3358.         if (wstat == ttpid || wstat == -1)
  3359.           break;
  3360.         pexitstat = (statusp & 0xff) ? statusp : statusp >> 8;
  3361.         }
  3362.         ttpid = 0;
  3363.     }
  3364.     netconn = 0;
  3365.     wasclosed = 1;
  3366.     ttyfd = -1;
  3367.     return(0);
  3368.     }
  3369. #endif /* NETCMD */
  3370. #ifdef NETPTY
  3371.     if (ttpty) {
  3372. #ifndef NODOPTY
  3373.         end_pty();
  3374. #endif /* NODOPTY */
  3375.         close(ttyfd);
  3376.     netconn = 0;
  3377.     wasclosed = 1;
  3378.         ttpty = 0;
  3379.         ttyfd = -1;
  3380.         return(0);
  3381.     }
  3382. #endif /* NETPTY */
  3383.  
  3384. #ifdef    NETCONN
  3385.     if (netconn) {            /* If it's a network connection. */
  3386.     debug(F100,"ttclos closing net","",0);
  3387.     netclos();            /* Let the network module close it. */
  3388.     netconn = 0;            /* No more network connection. */
  3389.     debug(F101,"ttclos ttyfd after netclos","",ttyfd); /* Should be -1 */
  3390.     return(0);
  3391.     }
  3392. #endif    /* NETCONN */
  3393.  
  3394.     if (xlocal) {            /* We're closing a SET LINE device */
  3395. #ifdef FT21                /* Fortune 2.1-specific items ... */
  3396.     ioctl(ttyfd,TIOCHPCL, NULL);
  3397. #endif /* FT21 */
  3398. #ifdef ultrix                /* Ultrix-specific items ... */
  3399. #ifdef TIOCSINUSE
  3400.     /* Unset the INUSE flag that we set in ttopen() */
  3401.     ioctl(ttyfd, TIOCSINUSE, NULL);
  3402. #endif /* TIOCSINUSE */
  3403.     ioctl(ttyfd, TIOCNMODEM, &x);
  3404. #ifdef COMMENT
  3405.     /* What was this? */
  3406.     ioctl(ttyfd, TIOCNCAR, NULL);
  3407. #endif /* COMMENT */
  3408. #endif /* ultrix */
  3409.     }
  3410.  
  3411.     /* This is to prevent us from sticking in tthang() or close(). */
  3412.  
  3413. #ifdef O_NDELAY
  3414. #ifndef aegis
  3415.     if (ttyfd > 0) {            /* But skip it on stdin. */
  3416.     debug(F100,"ttclos setting O_NDELAY","",0);
  3417.     x = fcntl(ttyfd,F_SETFL,fcntl(ttyfd,F_GETFL, 0)|O_NDELAY);
  3418. #ifdef DEBUG
  3419.     if (deblog && x == -1) {
  3420.         perror("Warning - Can't set O_NDELAY");
  3421.         debug(F101,"ttclos fcntl failure to set O_NDELAY","",x);
  3422.     }
  3423. #endif /* DEBUG */
  3424.     }
  3425. #endif /* aegis */
  3426. #endif /* O_NDELAY */
  3427.  
  3428.     x = 0;
  3429.     ttc_state = 0;
  3430.     if (xlocal
  3431. #ifdef NOFDZERO
  3432.     || ttyfd > 0
  3433. #endif /* NOFDZERO */
  3434.     ) {
  3435.     saval = signal(SIGALRM,xtimerh); /* Enable timer interrupt. */
  3436.     xx = alarm(8);            /* Allow 8 seconds. */
  3437.     debug(F101,"ttclos alarm","",xx);
  3438.     if (
  3439. #ifdef CK_POSIX_SIG
  3440.         sigsetjmp(sjbuf,1)
  3441. #else
  3442.         setjmp(sjbuf)
  3443. #endif /* CK_POSIX_SIG */
  3444.         ) {                /* Timer went off? */
  3445.         x = -1;
  3446. #ifdef DEBUG
  3447.         debug(F111,"ttclos ALARM TRAP errno",ckitoa(ttc_state),errno);
  3448.         printf("ttclos() timeout: %s\n", ttc_nam[ttc_state]);
  3449. #endif /* DEBUG */
  3450.     }
  3451.     errno = 0;
  3452.     debug(F101,"ttclos A","",ttc_state);
  3453.     if (ttc_state < 1) {
  3454.         ttc_state = 1;
  3455.         debug(F101,"ttclos exithangup","",exithangup);
  3456.         if (exithangup) {
  3457.         alarm(8);        /* Re-arm the timer */
  3458.         debug(F101,"ttclos calling tthang()","",x);
  3459.         x = tthang();        /* Hang up first, then... */
  3460.         debug(F101,"ttclos tthang()","",x);
  3461.         }
  3462.     }
  3463.     debug(F101,"ttclos B","",ttc_state);
  3464.     if (ttc_state < 2) {
  3465.         ttc_state = 2;
  3466.         debug(F101,"ttclos calling ttres()","",x);
  3467.         alarm(8);            /* Re-arm the timer */
  3468.         x = ttres();        /* Reset device modes. */
  3469.         debug(F101,"ttclos ttres()","",x);
  3470.     }
  3471.     debug(F101,"ttclos C","",ttc_state);
  3472.     if (ttc_state < 3) {
  3473.         ttc_state = 3;
  3474.         errno = 0;
  3475.         debug(F101,"ttclos calling close","",x);
  3476.         alarm(8);            /* Re-arm the timer */
  3477.         x = close(ttyfd);        /* Close the device. */
  3478.         debug(F101,"ttclos close()","",x);
  3479.         if (x > -1)
  3480.           ttc_state = 4;
  3481.     }
  3482.     debug(F101,"ttclos D","",ttc_state);
  3483.     ttimoff();            /* Turn off timer. */
  3484.     if (x < 0) {
  3485.         printf("?WARNING - close failed: %s\n",ttnmsv);
  3486. #ifdef DEBUG
  3487.         if (deblog) {
  3488.         printf("errno = %d\n", errno);
  3489.         debug(F101,"ttclos failed","",errno);
  3490.         }
  3491. #endif /* DEBUG */
  3492.     }
  3493.     /* Unlock after closing but before any getty mumbo jumbo */
  3494.  
  3495.     debug(F100,"ttclos about to call ttunlck","",0);
  3496.         if (ttunlck())                  /* Release uucp-style lock */
  3497.       fprintf(stderr,"Warning, problem releasing lock\r\n");
  3498.     }
  3499.  
  3500. /* For bidirectional lines, restore getty if it was there before. */
  3501.  
  3502. #ifdef ACUCNTRL                /* 4.3BSD acucntrl() method. */
  3503.     if (xlocal) {
  3504.     debug(F100,"ttclos ACUCNTRL","",0);
  3505.     acucntrl("enable",ttnmsv);    /* Enable getty on the device. */
  3506.     }
  3507. #else
  3508. #ifdef ATT7300                /* ATT UNIX PC (3B1, 7300) method. */
  3509.     if (xlocal) {
  3510.     debug(F100,"ttclos ATT7300 ongetty","",0);
  3511.     if (attmodem & DOGETY)        /* Was getty(1m) running before us? */
  3512.       ongetty(ttnmsv);        /* Yes, restart getty on tty line */
  3513.     attmodem &= ~DOGETY;        /* No phone in use, getty restored */
  3514.     }
  3515. #endif /* ATT7300 */
  3516. #endif /* System-dependent getty-restoring methods */
  3517.  
  3518. #ifdef sony_news
  3519.     km_ext = -1;            /* Invalidate device's Kanji-mode */
  3520. #endif /* sony_news */
  3521.  
  3522.     ttyfd = -1;                         /* Invalidate the file descriptor. */
  3523.     wasclosed = 1;
  3524.     debug(F100,"ttclos done","",0);
  3525.     return(0);
  3526. }
  3527.  
  3528. /*  T T H A N G  --  Hangup phone line or network connection.  */
  3529. /*
  3530.   Returns:
  3531.   0 if it does nothing.
  3532.   1 if it believes that it hung up successfully.
  3533.  -1 if it believes that the hangup attempt failed.
  3534. */
  3535.  
  3536. #define HUPTIME 500            /* Milliseconds for hangup */
  3537.  
  3538. #ifdef COMMENT
  3539. /* The following didn't work but TIOCSDTR does work */
  3540. #ifdef UNIXWARE
  3541. /* Define HUP_POSIX to force non-POSIX builds to use the POSIX hangup method */
  3542. #ifndef POSIX                /* Such as Unixware 1.x, 2.x */
  3543. #ifndef HUP_POSIX
  3544. #define HUP_POSIX
  3545. #endif /* HUP_POSIX */
  3546. #endif /* POSIX */
  3547. #endif /* UNIXWARE */
  3548. #endif /* COMMENT */
  3549.  
  3550. #ifndef USE_TIOCSDTR
  3551. #ifdef __NetBSD__
  3552. /* Because the POSIX method (set output speed to 0) doesn't work in NetBSD */
  3553. #ifdef TIOCSDTR
  3554. #ifdef TIOCCDTR
  3555. #define USE_TIOCSDTR
  3556. #endif /* TIOCCDTR */
  3557. #endif /* TIOCSDTR */
  3558. #endif /* __NetBSD__ */
  3559. #endif /* USE_TIOCSDTR */
  3560.  
  3561. #ifndef HUP_CLOSE_POSIX
  3562. #ifdef OU8
  3563. #define HUP_CLOSE_POSIX
  3564. #else
  3565. #ifdef CK_SCOV5
  3566. #define HUP_CLOSE_POSIX
  3567. #endif /* CK_SCOV5 */
  3568. #endif /* OU8 */
  3569. #endif /* HUP_CLOSE_POSIX */
  3570.  
  3571. #ifdef NO_HUP_CLOSE_POSIX
  3572. #ifdef HUP_CLOSE_POSIX
  3573. #undef HUP_CLOSE_POSIX
  3574. #endif /* HUP_CLOSE_POSIX */
  3575. #endif /* NO_HUP_CLOSE_POSIX */
  3576.  
  3577. int
  3578. tthang() {
  3579. #ifdef NOLOCAL
  3580.     return(0);
  3581. #else
  3582.     int x = 0;                /* Sometimes used as return code. */
  3583. #ifndef POSIX
  3584.     int z;                /* worker */
  3585. #endif /* POSIX */
  3586.  
  3587. #ifdef COHERENT
  3588. #define SVORPOSIX
  3589. #endif /* COHERENT */
  3590.  
  3591. #ifdef SVORPOSIX            /* AT&T, POSIX, HPUX declarations. */
  3592.     int spdsav;                /* for saving speed */
  3593. #ifdef HUP_POSIX
  3594.     int spdsavi;
  3595. #else
  3596. #ifdef BSD44ORPOSIX
  3597.     int spdsavi;
  3598. #endif /* BSD44ORPOSIX */
  3599. #endif /* HUP_POSIX */
  3600. #ifdef HPUX
  3601. /*
  3602.   Early versions of HP-UX omitted the mflag typedef.  If you get complaints
  3603.   about it, just change it to long (or better still, unsigned long).
  3604. */
  3605.     mflag
  3606.       dtr_down = 00000000000,
  3607.       modem_rtn,
  3608.       modem_sav;
  3609.     char modem_state[64];
  3610. #endif /* HPUX */
  3611.     int flags;                /* fcntl flags */
  3612.     unsigned short ttc_save;
  3613. #endif /* SVORPOSIX */
  3614.  
  3615.     if (ttyfd < 0) return(0);           /* Don't do this if not open  */
  3616.     if (xlocal < 1) return(0);        /* Don't do this if not local */
  3617.  
  3618. #ifdef NETCMD
  3619.     if (ttpipe)
  3620.       return((ttclos(0) < 0) ? -1 : 1);
  3621. #endif /* NETCMD */
  3622. #ifdef NETPTY
  3623.     if (ttpty)
  3624.       return((ttclos(0) < 0) ? -1 : 1);
  3625. #endif /* NETPTY */
  3626. #ifdef NETCONN
  3627.     if (netconn) {            /* Network connection. */
  3628. #ifdef TN_COMPORT
  3629.         if (istncomport()) {
  3630.             int rc = tnc_set_dtr_state(0);
  3631.             if (rc >= 0) {
  3632.                 msleep(HUPTIME);
  3633.                 rc = tnc_set_dtr_state(1);
  3634.             }
  3635.             return(rc >= 0 ? 0 : -1);
  3636.         } else
  3637. #endif /* TN_COMPORT */
  3638.       return((netclos() < 0) ? -1 : 1); /* Just close it. */
  3639.   }
  3640. #endif /* NETCONN */
  3641.  
  3642. /* From here down, we handle real tty devices. */
  3643. #ifdef HUP_POSIX
  3644. /*
  3645.   e.g. for Unixware 2, where we don't have a full POSIX build, we
  3646.   still have to use POSIX-style hangup.  Thus the duplication of this
  3647.   and the next case, the only difference being we use a local termios
  3648.   struct here, since a different model is used elsewhere.
  3649.  
  3650.   NO LONGER USED as of C-Kermit 8.0 -- it turns out that this method,
  3651.   even though it compiles and executes without error, doesn't actually
  3652.   work (i.e. DTR does not drop), whereas the TIOCSDTR method works just fine,
  3653. */
  3654.     {
  3655.     struct termios ttcur;
  3656.     int x;
  3657.     debug(F100,"tthang HUP_POSIX style","",0);
  3658.     x = tcgetattr(ttyfd, &ttcur);    /* Get current attributes */
  3659.     debug(F111,"tthang tcgetattr",ckitoa(errno),x);
  3660.     if (x < 0) return(-1);
  3661.     spdsav = cfgetospeed(&ttcur);    /* Get current speed */
  3662.     debug(F111,"tthang cfgetospeed",ckitoa(errno),spdsav);
  3663.     spdsavi = cfgetispeed(&ttcur);    /* Get current speed */
  3664.     debug(F111,"tthang cfgetispeed",ckitoa(errno),spdsavi);
  3665.     x = cfsetospeed(&ttcur,B0);    /* Replace by 0 */
  3666.     debug(F111,"tthang cfsetospeed",ckitoa(errno),x);
  3667.     if (x < 0) return(-1);
  3668.     x = cfsetispeed(&ttcur,B0);
  3669.     debug(F111,"tthang cfsetispeed",ckitoa(errno),x);
  3670.     if (x < 0) return(-1);
  3671.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3672.     debug(F111,"tthang tcsetattr B0",ckitoa(errno),x);
  3673.     if (x < 0) return(-1);
  3674.     msleep(HUPTIME);        /* Sleep 0.5 sec */
  3675.     x = cfsetospeed(&ttcur,spdsav); /* Restore prev speed */
  3676.     if (x < 0) return(-1);
  3677.     debug(F111,"tthang cfsetospeed prev",ckitoa(errno),x);
  3678.     x = cfsetispeed(&ttcur,spdsavi);
  3679.     debug(F111,"tthang cfsetispeed prev",ckitoa(errno),x);
  3680.     if (x < 0) return(-1);
  3681.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3682.     debug(F111,"tthang tcsetattr restore",ckitoa(errno),x);
  3683.     if (x < 0) return(-1);
  3684.     return(1);
  3685.     }
  3686. #else
  3687. #ifdef BSD44ORPOSIX
  3688. #ifdef QNX
  3689.     {
  3690.     int x;
  3691.     x = tcdropline(ttyfd,500);
  3692.     debug(F101,"tthang QNX tcdropline","",x);
  3693.     ttcur.c_cflag |= CLOCAL;
  3694.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3695.     debug(F101,"tthang QNX tcsetattr restore","",x);
  3696.     if (x < 0) {
  3697.         debug(F101,"tthang QNX tcsetattr restore errno","",errno);
  3698.         return(-1);
  3699.     }
  3700.     /* Fix flags - ensure O_NONBLOCK is off */
  3701.  
  3702.     errno = 0;
  3703.     debug(F101,"tthang QNX iniflags","",iniflags);
  3704.     if (fcntl(ttyfd, F_SETFL, iniflags) == -1) {
  3705.         debug(F101,"tthang QNX F_SETFL errno","",errno);
  3706.         return(-1);
  3707.     }
  3708.     return(x);
  3709.     }
  3710. #else  /* QNX */
  3711.     {
  3712.     int x;
  3713. #ifdef USE_TIOCSDTR
  3714.     debug(F100,"tthang BSD44ORPOSIX USE_TIOCSDTR","",0);
  3715.     errno = 0;
  3716.     x = ioctl(ttyfd, TIOCCDTR, NULL);
  3717.     debug(F111,"tthang BSD44ORPOSIX ioctl TIOCCDTR",ckitoa(errno),x);
  3718.     if (x < 0) return(-1);
  3719.     msleep(HUPTIME);        /* Sleep 0.5 sec */
  3720.     errno = 0;
  3721.     x = ioctl(ttyfd, TIOCSDTR, NULL);
  3722.     debug(F111,"tthang BSD44ORPOSIX ioctl TIOCSDTR",ckitoa(errno),x);
  3723.     if (x < 0) return(-1);
  3724. #else  /* USE_TIOCSDTR */
  3725.  
  3726. #ifdef HUP_CLOSE_POSIX
  3727. /*
  3728.   In OSR5 versions where TIOCSDTR is not defined (up to and including at
  3729.   least 5.0.6a) the POSIX APIs in the "#else" part below are available but
  3730.   don't work, and no other APIs are available that do work.  In this case
  3731.   we have to drop DTR by brute force: close and reopen the port.  This
  3732.   code actually works, but all the steps are crucial: setting CLOCAL, the
  3733.   O_NDELAY manipulations, etc.
  3734. */
  3735.     debug(F100,"tthang HUP_CLOSE_POSIX close/open","",0);
  3736.     debug(F101,"tthang HUP_CLOSE_POSIX O_NONBLOCK","",O_NONBLOCK);
  3737.     debug(F101,"tthang HUP_CLOSE_POSIX O_NDELAY","",O_NDELAY);
  3738.     errno = 0;
  3739.     x = tcgetattr(ttyfd, &ttcur);    /* Get current attributes */
  3740.     debug(F101,"tthang HUP_CLOSE_POSIX tcgetattr","",x);
  3741.     if (x < 0) {
  3742.         debug(F101,"tthang HUP_CLOSE_POSIX tcgetattr errno","",errno);
  3743.         return(-1);
  3744.     }
  3745.     errno = 0;
  3746.  
  3747.     x = close(ttyfd);        /* Close without releasing lock */
  3748.     if (x < 0) {
  3749.         debug(F101,"tthang HUP_CLOSE_POSIX close errno","",errno);
  3750.         return(-1);
  3751.     }
  3752.     errno = 0;
  3753.     x = msleep(500);        /* Pause half a second */
  3754.     if (x < 0) {            /* Or if that doesn't work, 1 sec */
  3755.         debug(F101,"tthang HUP_CLOSE_POSIX msleep errno","",errno);
  3756.         sleep(1);
  3757.     }
  3758.     errno = 0;
  3759.     ttyfd = priv_opn(ttnmsv, (O_RDWR|O_NDELAY)); /* Reopen the device */
  3760.     debug(F111,"tthang HUP_CLOSE_POSIX reopen",ttnmsv,ttyfd);
  3761.     if (ttyfd < 0) {
  3762.         debug(F101,"tthang HUP_CLOSE_POSIX reopen errno","",errno);
  3763.         return(-1);
  3764.     }
  3765.     debug(F101,"tthang HUP_CLOSE_POSIX re-ttopen ttyfd","",ttyfd);
  3766.  
  3767.     /* Restore previous attributes */
  3768.  
  3769.     errno = 0;
  3770.     tvtflg = 0;
  3771.     ttcur.c_cflag |= CLOCAL;
  3772.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3773.     debug(F101,"tthang HUP_CLOSE_POSIX tcsetattr restore","",x);
  3774.     if (x < 0) {
  3775.         debug(F101,"tthang HUP_CLOSE_POSIX tcsetattr restore errno",
  3776.           "",errno);
  3777.         return(-1);
  3778.     }
  3779.     /* Fix flags - ensure O_NDELAY and O_NONBLOCK are off */
  3780.  
  3781.     errno = 0;
  3782.         if ((x = fcntl(ttyfd, F_GETFL, 0)) == -1) {
  3783.         debug(F101,"tthang HUP_CLOSE_POSIX F_GETFL errno","",errno);
  3784.         return(-1);
  3785.     }
  3786.     debug(F101,"tthang HUP_CLOSE_POSIX flags","",x);
  3787.     errno = 0;
  3788.         x &= ~(O_NONBLOCK|O_NDELAY);
  3789.     debug(F101,"tthang HUP_CLOSE_POSIX flags to set","",x);
  3790.     debug(F101,"tthang HUP_CLOSE_POSIX iniflags","",iniflags);
  3791.     if (fcntl(ttyfd, F_SETFL, x) == -1) {
  3792.         debug(F101,"tthang HUP_CLOSE_POSIX F_SETFL errno","",errno);
  3793.         return(-1);
  3794.     }
  3795. #ifdef DEBUG
  3796.     if (deblog) {
  3797.         if ((x = fcntl(ttyfd, F_GETFL, 0)) > -1) {
  3798.         debug(F101,"tthang HUP_CLOSE_POSIX flags","",x);
  3799.         debug(F101,"tthang HUP_CLOSE_POSIX flags & O_NONBLOCK",
  3800.               "",x&O_NONBLOCK);
  3801.         debug(F101,"tthang HUP_CLOSE_POSIX flags & O_NDELAY",
  3802.               "",x&O_NDELAY);
  3803.         }
  3804.     }
  3805. #endif /* DEBUG */
  3806.  
  3807. #else  /* HUP_CLOSE_POSIX */
  3808.     
  3809.     /* General BSD44ORPOSIX case (Linux, BSDI, FreeBSD, etc) */
  3810.  
  3811.     debug(F100,"tthang BSD44ORPOSIX B0","",0);
  3812.     x = tcgetattr(ttyfd, &ttcur);    /* Get current attributes */
  3813.     debug(F111,"tthang BSD44ORPOSIX tcgetattr",ckitoa(errno),x);
  3814.     if (x < 0) return(-1);
  3815.     spdsav = cfgetospeed(&ttcur);    /* Get current speed */
  3816.     debug(F111,"tthang BSD44ORPOSIX cfgetospeed",ckitoa(errno),spdsav);
  3817.     spdsavi = cfgetispeed(&ttcur);    /* Get current speed */
  3818.     debug(F111,"tthang BSD44ORPOSIX cfgetispeed",ckitoa(errno),spdsavi);
  3819.     x = cfsetospeed(&ttcur,B0);    /* Replace by 0 */
  3820.     debug(F111,"tthang BSD44ORPOSIX cfsetospeed",ckitoa(errno),x);
  3821.     if (x < 0) return(-1);
  3822.     x = cfsetispeed(&ttcur,B0);
  3823.     debug(F111,"tthang BSD44ORPOSIX cfsetispeed",ckitoa(errno),x);
  3824.     if (x < 0) return(-1);
  3825.     /* This gets EINVAL on NetBSD 1.4.1 because of B0... */
  3826.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3827.     debug(F111,"tthang BSD44ORPOSIX tcsetattr B0",ckitoa(errno),x);
  3828.     if (x < 0) return(-1);
  3829.     msleep(HUPTIME);        /* Sleep 0.5 sec */
  3830.     debug(F101,"tthang BSD44ORPOSIX restore output speed","",spdsav);
  3831.     x = cfsetospeed(&ttcur,spdsav); /* Restore prev speed */
  3832.     debug(F111,"tthang BSD44ORPOSIX cfsetospeed prev",ckitoa(errno),x);
  3833.     if (x < 0) return(-1);
  3834.     debug(F101,"tthang BSD44ORPOSIX restore input speed","",spdsavi);
  3835.     x = cfsetispeed(&ttcur,spdsavi);
  3836.     debug(F111,"tthang BSD44ORPOSIX cfsetispeed prev",ckitoa(errno),x);
  3837.     if (x < 0) return(-1);
  3838.     ttcur.c_cflag |= CLOCAL;    /* Don't expect CD after hangup */
  3839.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3840.     debug(F111,"tthang BSD44ORPOSIX tcsetattr restore",ckitoa(errno),x);
  3841.     if (x < 0) return(-1);
  3842.  
  3843. #endif /* HUP_CLOSE_POSIX */
  3844. #endif /* USE_TIOCSDTR */
  3845.  
  3846.     return(1);
  3847.     }
  3848.  
  3849. #endif /* QNX */
  3850. #else /* BSD44ORPOSIX */
  3851.  
  3852. #ifdef aegis                /* Apollo Aegis */
  3853.     sio_$control((short)ttyfd, sio_$dtr, false, st);    /* DTR down */
  3854.     msleep(HUPTIME);                    /* pause */
  3855.     sio_$control((short)ttyfd, sio_$dtr, true,  st);    /* DTR up */
  3856.     return(1);
  3857. #endif /* aegis */
  3858.  
  3859. #ifdef ANYBSD                /* Any BSD version. */
  3860. #ifdef TIOCCDTR                /* Except those that don't have this */
  3861.     debug(F100,"tthang BSD style","",0);
  3862.     if (ioctl(ttyfd,TIOCCDTR,0) < 0) {    /* Clear DTR. */
  3863.     debug(F101,"tthang TIOCCDTR fails","",errno);
  3864.     return(-1);
  3865.     }
  3866.     msleep(HUPTIME);            /* For about 1/2 sec */
  3867.     errno = 0;
  3868.     x = ioctl(ttyfd,TIOCSDTR,0);    /* Restore DTR */
  3869.     if (x < 0) {
  3870.     /*
  3871.       For some reason, this tends to fail with "no such device or address"
  3872.       but the operation still works, probably because of the close/open
  3873.       later on.  So let's not scare the user unnecessarily here.
  3874.     */
  3875.     debug(F101,"tthang TIOCSDTR errno","",errno); /* Log the error */
  3876.     x = 1;                /* Pretend we succeeded */
  3877.     } else if (x == 0) x = 1;        /* Success */
  3878. #ifdef COMMENT
  3879. #ifdef FT21
  3880.     ioctl(ttyfd, TIOCSAVEMODES, 0);
  3881.     ioctl(ttyfd, TIOCHPCL, 0);
  3882.     close(ttyfd);            /* Yes, must do this twice */
  3883.     if ((ttyfd = open(ttnmsv,2)) < 0)    /* on Fortune computers... */
  3884.       return(-1);            /* (but why?) */
  3885.     else x = 1;
  3886. #endif /* FT21 */
  3887. #endif /* COMMENT */
  3888. #endif /* TIOCCDTR */
  3889.     close(do_open(ttnmsv));        /* Clear i/o error condition */
  3890.     errno = 0;
  3891. #ifdef COMMENT
  3892. /* This is definitely dangerous.  Why was it here? */
  3893.     z = ttvt(ttspeed,ttflow);        /* Restore modes. */
  3894.     debug(F101,"tthang ttvt returns","",z);
  3895.     return(z < 0 ? -1 : 1);
  3896. #else
  3897.     return(x);
  3898. #endif /* COMMENT */
  3899. #endif /* ANYBSD */
  3900.  
  3901. #ifdef ATTSV
  3902. /* AT&T UNIX section, includes HP-UX and generic AT&T System III/V... */
  3903.  
  3904. #ifdef HPUX
  3905. /* Hewlett Packard allows explicit manipulation of modem signals. */
  3906.  
  3907. #ifdef COMMENT
  3908. /* Old way... */
  3909.     debug(F100,"tthang HP-UX style","",0);
  3910.     if (ioctl(ttyfd,MCSETAF,&dtr_down) < 0)        /* lower DTR */
  3911.       return(-1);                           /* oops, can't. */
  3912.     msleep(HUPTIME);                       /* Pause half a second. */
  3913.     x = 1;                           /* Set return code */
  3914.     if (ioctl(ttyfd,MCGETA,&modem_rtn) > -1) {     /* Get line status. */
  3915.     if ((modem_rtn & MDCD) != 0)             /* Check if CD is low. */
  3916.       x = -1;                                  /* CD didn't drop, fail. */
  3917.     } else x = -1;
  3918.  
  3919.     /* Even if above calls fail, RTS & DTR should be turned back on. */
  3920.     modem_rtn = MRTS | MDTR;
  3921.     if (ioctl(ttyfd,MCSETAF,&modem_rtn) < 0) x = -1;
  3922.     return(x);
  3923. #else
  3924. /* New way, from Hellmuth Michaelis */
  3925.     debug(F100,"tthang HP-UX style, HPUXDEBUG","",0);
  3926.     if (ioctl(ttyfd,MCGETA,&modem_rtn) == -1) { /* Get current status. */
  3927.     debug(F100,"tthang HP-UX: can't get modem lines, NO HANGUP!","",0);
  3928.     return(-1);
  3929.     }
  3930.     sprintf(modem_state,"%#lx",modem_rtn);
  3931.     debug(F110,"tthang HP-UX: modem lines = ",modem_state,0);
  3932.     modem_sav = modem_rtn;        /* Save current modem signals */
  3933.     modem_rtn &= ~MDTR;            /* Turn DTR bit off */
  3934.     sprintf(modem_state,"%#lx",modem_rtn);
  3935.     debug(F110,"tthang HP-UX: DTR down = ",modem_state,0);
  3936.     if (ioctl(ttyfd,MCSETAF,&modem_rtn) < 0) { /* lower DTR */
  3937.     debug(F100,"tthang HP-UX: can't lower DTR!","",0);
  3938.     return(-1);            /* oops, can't. */
  3939.     }
  3940.     msleep(HUPTIME);            /* Pause half a second. */
  3941.     x = 1;                /* Set return code */
  3942.     if (ioctl(ttyfd,MCGETA,&modem_rtn) > -1) { /* Get line status. */
  3943.     sprintf(modem_state,"%#lx",modem_rtn);
  3944.     debug(F110,"tthang HP-UX: modem lines got = ",modem_state,0);
  3945.     if ((modem_rtn & MDCD) != 0) {    /* Check if CD is low. */
  3946.         debug(F100,"tthang HP-UX: DCD not down","",0);
  3947.         x = -1;            /* CD didn't drop, fail. */
  3948.     } else {
  3949.         debug(F100,"tthang HP-UX: DCD down","",0);
  3950.     }
  3951.     } else {
  3952.     x = -1;
  3953.     debug(F100,"tthang HP-UX: can't get DCD status !","",0);
  3954.     }
  3955.  
  3956.     /* Even if above calls fail, DTR should be turned back on. */
  3957.  
  3958.     modem_sav |= MDTR;
  3959.     if (ioctl(ttyfd,MCSETAF,&modem_sav) < 0) {
  3960.     x = -1;
  3961.     debug(F100,"tthang HP-UX: can't set saved state","",0);
  3962.     } else {
  3963.     sprintf(modem_state,"%#lx",modem_sav);
  3964.     debug(F110,"tthang HP-UX: final modem lines = ",modem_state,0);
  3965.     }
  3966.     return(x);
  3967. #endif /* COMMENT */
  3968.  
  3969. #else /* AT&T but not HP-UX */
  3970.  
  3971. /* SVID for AT&T System V R3 defines ioctl's for handling modem signals. */
  3972. /* It is not known how many, if any, systems actually implement them, */
  3973. /* so we include them here in ifdef's. */
  3974.  
  3975. /*
  3976.   Unixware has the TIOCMxxx symbols defined, but calling ioctl() with them
  3977.   gives error 22 (invalid argument).
  3978. */
  3979. #ifndef _IBMR2
  3980. /*
  3981.   No modem-signal twiddling for IBM RT PC or RS/6000.
  3982.   In AIX 3.1 and earlier, the ioctl() call is broken.
  3983.   This code could be activated for AIX 3.1 with PTF 2006 or later
  3984.   (e.g. AIX 3.2), but close/open does the job too, so why bother.
  3985. */
  3986. #ifdef TIOCMBIS                /* Bit Set */
  3987. #ifdef TIOCMBIC                /* Bit Clear */
  3988. #ifdef TIOCM_DTR            /* DTR */
  3989.  
  3990. /* Clear DTR, sleep 300 msec, turn it back on. */
  3991. /* If any of the ioctl's return failure, go on to the next section. */
  3992.  
  3993.     z = TIOCM_DTR;            /* Code for DTR. */
  3994. #ifdef COMMENT
  3995. /*
  3996.   This was the cause of the troubles with the Solaris Port Monitor.
  3997.   The problem is: RTS never comes back on.  Moral: Don't do it!
  3998.   (But why doesn't it come back on?  See the TIOCMBIS call...)
  3999. */
  4000. #ifdef TIOCM_RTS            /* Lower RTS too if symbol is known. */
  4001.     z |= TIOCM_RTS;
  4002. #endif /* TIOCM_RTS */
  4003. #endif /* COMMENT */
  4004.  
  4005.     debug(F101,"tthang TIOCM signal mask","",z);
  4006.     if (ioctl(ttyfd,TIOCMBIC,&z) > -1) {   /* Try to lower DTR. */
  4007.     debug(F100,"tthang TIOCMBIC ok","",0);
  4008.     msleep(HUPTIME);           /* Pause half a second. */
  4009.     if (ioctl(ttyfd,TIOCMBIS,&z) > -1) { /* Try to turn it back on. */
  4010.         debug(F100,"tthang TIOCMBIS ok","",0);
  4011. #ifndef CLSOPN
  4012.         return(1);            /* Success, done. */
  4013. #endif /* CLSOPN */
  4014.     } else {            /* Couldn't raise, continue. */
  4015.         debug(F101,"tthang TIOCMBIS errno","",errno);
  4016.     }
  4017.     } else {                /* Couldn't lower, continue. */
  4018.      debug(F101,"tthang TIOCMBIC errno","",errno);
  4019.     }
  4020. #endif /* TIOCM_DTR */
  4021. #endif /* TIOCMBIC */
  4022. #endif /* TIOCMBIS */
  4023. #endif /* _IBMR2 */
  4024.  
  4025. /*
  4026.   General AT&T UNIX case, not HPUX.  The following code is highly suspect.  No
  4027.   two AT&T-based systems seem to do this the same way.  The object is simply
  4028.   to turn off DTR and then turn it back on.  SVID says the universal method
  4029.   for turning off DTR is to set the speed to zero, and this does seem to do
  4030.   the trick in all cases.  But neither SVID nor any known man pages say how to
  4031.   turn DTR back on again.  Some variants, like most Xenix implementations,
  4032.   raise DTR again when the speed is restored to a nonzero value.  Others
  4033.   require the device to be closed and opened again, but this is risky because
  4034.   getty could seize the device during the instant it is closed.
  4035. */
  4036.  
  4037. /* Return code for ioctl failures... */
  4038. #ifdef ATT6300
  4039.     x = 1;                /* ATT6300 doesn't want to fail... */
  4040. #else
  4041.     x = -1;
  4042. #endif /* ATT6300 */
  4043.  
  4044.     debug(F100,"tthang get settings","",0);
  4045.     if (ioctl(ttyfd,TCGETA,&ttcur) < 0) /* Get current settings. */
  4046.       return(x);            /* Fail if this doesn't work. */
  4047.     if ((flags = fcntl(ttyfd,F_GETFL,0)) < 0) /* Get device flags. */
  4048.       return(x);
  4049.     ttc_save = ttcur.c_cflag;        /* Remember current speed. */
  4050.     spdsav = ttc_save & CBAUD;
  4051.     debug(F101,"tthang speed","",spdsav);
  4052.  
  4053. #ifdef O_NDELAY
  4054.     debug(F100,"tthang turning O_NDELAY on","",0);
  4055.     fcntl(ttyfd, F_SETFL, flags | O_NDELAY); /* Activate O_NDELAY */
  4056. #endif /* O_NDELAY */
  4057.  
  4058. #ifdef ATT7300 /* This is the way it is SUPPOSED to work */
  4059.     ttcur.c_cflag &= ~CBAUD;        /* Change the speed to zero.  */
  4060. #else
  4061. #ifdef RTAIX
  4062.     ttcur.c_cflag &= ~CBAUD;        /* Change the speed to zero.  */
  4063. #else          /* This way really works but may be dangerous */
  4064. #ifdef u3b2
  4065.     ttcur.c_cflag = ~(CBAUD|CLOCAL);    /* Special for AT&T 3B2s */
  4066.                     /* (CLOCAL must be OFF) */
  4067. #else
  4068. #ifdef SCO3R2                /* SCO UNIX 3.2 */
  4069. /*
  4070.   This is complete nonsense, but an SCO user claimed this change made
  4071.   hanging up work.  Comments from other SCO UNIX 3.2 users would be
  4072.   appreciated.
  4073. */
  4074.     ttcur.c_cflag = CBAUD|B0;
  4075. #else
  4076. #ifdef AIXRS                /* AIX on RS/6000 */
  4077. /*
  4078.   Can't set speed to zero on AIX 3.1 on RS/6000 64-port adapter,
  4079.   even though you can do it on the built-in port and the 8- and 16-port
  4080.   adapters.  (Untested on 128-port adapter.)
  4081. */
  4082.     ttcur.c_cflag = CLOCAL|HUPCL|spdsav; /* Speed 0 causes EINVAL */
  4083. #else                    /* None of the above */
  4084. /*
  4085.   Set everything, including the speed, to zero, except for the CLOCAL
  4086.   and HUPCL bits.
  4087. */
  4088.     ttcur.c_cflag = CLOCAL|HUPCL;
  4089. #endif /* AIXRS */
  4090. #endif /* SCO3R2 */
  4091. #endif /* u3b2 */
  4092. #endif /* RTAIX */
  4093. #endif /* ATT7300 */
  4094.  
  4095. #ifdef COMMENT
  4096.     /* and if none of those work, try one of these... */
  4097.     ttcur.c_cflag = 0;
  4098.     ttcur.c_cflag = CLOCAL;
  4099.     ttcur.c_cflag &= ~(CBAUD|HUPCL);
  4100.     ttcur.c_cflag &= ~(CBAUD|CREAD);
  4101.     ttcur.c_cflag &= ~(CBAUD|CREAD|HUPCL);
  4102.     /* or other combinations */
  4103. #endif /* COMMENT */
  4104.  
  4105. #ifdef TCXONC
  4106.     debug(F100,"tthang TCXONC","",0);
  4107.     if (ioctl(ttyfd, TCXONC, 1) < 0) {
  4108.     debug(F101,"tthang TCXONC failed","",errno);
  4109.     }
  4110. #endif /* TCXONC */
  4111.  
  4112. #ifdef TIOCSTART
  4113.     debug(F100,"tthang TIOCSTART","",0);
  4114.     if (ioctl(ttyfd, TIOCSTART, 0) < 0) {
  4115.     debug(F101,"tthang TIOCSTART failed","",errno);
  4116.     }
  4117. #endif /* TIOCSTART */
  4118.  
  4119.     if (ioctl(ttyfd,TCSETAF,&ttcur) < 0) { /* Fail if we can't. */
  4120.     debug(F101,"tthang TCSETAF failed","",errno);
  4121.     fcntl(ttyfd, F_SETFL, flags);    /* Restore flags */
  4122.     return(-1);            /* before returning. */
  4123.     }
  4124.     msleep(300);            /* Give modem time to notice. */
  4125.  
  4126. #ifndef NOCOTFMC
  4127.  
  4128. /* Now, even though it doesn't say this in SVID or any man page, we have */
  4129. /* to close and reopen the device.  This is not necessary for all systems, */
  4130. /* but it's impossible to predict which ones need it and which ones don't. */
  4131.  
  4132. #ifdef ATT7300
  4133. /*
  4134.   Special handling for ATT 7300 UNIX PC and 3B1, which have "phone"
  4135.   related ioctl's for their internal modems.  attmodem has getty status and
  4136.   modem-in-use bit.  Reportedly the ATT7300/3B1 PIOCDISC call is necessary,
  4137.   but also ruins the file descriptor, and no other phone(7) ioctl call can fix
  4138.   it.  Whatever it does, it seems to escape detection with PIOCGETA and TCGETA.
  4139.   The only way to undo the damage is to close the fd and then reopen it.
  4140. */
  4141.     if (attmodem & ISMODEM) {
  4142.     debug(F100,"tthang attmodem close/open","",0);
  4143.     ioctl(ttyfd,PIOCUNHOLD,&dialer); /* Return call to handset. */
  4144.     ioctl(ttyfd,PIOCDISC,&dialer);    /* Disconnect phone. */
  4145.     close(ttyfd);            /* Close and reopen the fd. */
  4146.     ttyfd = priv_opn(ttnmsv, O_RDWR | O_NDELAY);
  4147.     attmodem &= ~ISMODEM;        /* Phone no longer in use. */
  4148.     }
  4149. #else /* !ATT7300 */
  4150. /* It seems we have to close and open the device for other AT&T systems */
  4151. /* too, and this is the place to do it.  The following code does the */
  4152. /* famous close(open(...)) magic by default.  If that doesn't work for you, */
  4153. /* then try uncommenting the following statement or putting -DCLSOPN in */
  4154. /* the makefile CFLAGS. */
  4155.  
  4156. /* #define CLSOPN */
  4157.  
  4158. #ifndef SCO32 /* Not needed by, and harmful to, SCO UNIX 3.2 / Xenix 2.3 */
  4159.  
  4160. #ifdef O_NDELAY
  4161. #define OPENFLGS O_RDWR | O_NDELAY
  4162. #else
  4163. #define OPENFLGS O_RDWR
  4164. #endif
  4165.  
  4166. #ifndef CLSOPN
  4167. /* This method is used by default, i.e. unless CLSOPN is defined. */
  4168. /* It is thought to be safer because there is no window where getty */
  4169. /* can seize control of the device.  The drawback is that it might not work. */
  4170.  
  4171.     debug(F101,"tthang close(open()), OPENFLGS","",OPENFLGS);
  4172.     close(priv_opn(ttnmsv, OPENFLGS));
  4173.  
  4174. #else
  4175. /* This method is used if you #define CLSOPN.  It is more likely to work */
  4176. /* than the previous method, but it's also more dangerous. */
  4177.  
  4178.     debug(F101,"tthang close/open, OPENFLGS","",OPENFLGS);
  4179.     close(ttyfd);
  4180.     msleep(10);
  4181.     ttyfd = priv_opn(ttnmsv, OPENFLGS);    /* Open it again */
  4182. #endif /* CLSOPN */
  4183. #undef OPENFLGS
  4184.  
  4185. #endif /* SCO32 */
  4186. #endif /* ATT7300 */
  4187.  
  4188. #endif /* NOCOTFMC */
  4189.  
  4190. /* Now put all flags & modes back the way we found them. */
  4191. /* (Does the order of ioctl & fcntl matter ? ) */
  4192.  
  4193.     debug(F100,"tthang restore settings","",0);
  4194.     ttcur.c_cflag = ttc_save;        /* Get old speed back. */
  4195.     if (ioctl(ttyfd,TCSETAF,&ttcur) < 0) /* ioctl parameters. */
  4196.       return(-1);
  4197. #ifdef O_NDELAY
  4198. /*
  4199.   This is required for IBM RT and RS/6000, probably helps elsewhere too (?).
  4200.   After closing a modem line, the modem will probably not be asserting
  4201.   carrier any more, so we should not require carrier any more.  If this
  4202.   causes trouble on non-IBM UNIXes, change the #ifdef to use _IBMR2 rather
  4203.   than O_NDELAY.
  4204. */
  4205.     flags &= ~O_NDELAY;            /* Don't require carrier on reopen */
  4206. #endif /* O_NDELAY */
  4207.     if (fcntl(ttyfd,F_SETFL,flags) < 0)    /* fcntl parameters */
  4208.       return(-1);
  4209.  
  4210.     return(1);
  4211. #endif /* not HPUX */
  4212. #endif /* ATTSV */
  4213. #endif /* BSD44ORPOSIX */
  4214. #endif /* HUP_POSIX */
  4215. #endif /* NOLOCAL */
  4216. }
  4217.  
  4218. /*
  4219.   Major change in 5A(174).  We used to use LPASS8, if it was defined, to
  4220.   allow 8-bit data and Xon/Xoff flow control at the same time.  But this
  4221.   LPASS8 business seems to have been causing trouble for everybody but me!
  4222.   For example, Annex terminal servers, commonly used with Encore computers,
  4223.   do not support LPASS8 even though the Encore itself does.  Ditto for many
  4224.   other terminal servers, TELNET connections, rlogin connections, etc etc.
  4225.   Now, reportedly, even vanilla 4.3 BSD systems can't do this right on their
  4226.   serial lines, even though LPASS8 is a feature of 4.3BSD.  So let's turn it
  4227.   off for everybody.  That means we goes back to using raw mode, with no
  4228.   flow control.  Phooey.
  4229.  
  4230.   NOTE: This must be done before the first reference to LPASS8 in this file,
  4231.   and after the last #include statment.
  4232. */
  4233. #ifdef LPASS8
  4234. #undef LPASS8
  4235. #endif /* LPASS8 */
  4236.  
  4237. /*  T T R E S  --  Restore terminal to "normal" mode.  */
  4238.  
  4239. /* ske@pkmab.se: There are two choices for what this function should do.
  4240.  * (1) Restore the tty to current "normal" mode, with carrier treatment
  4241.  * according to ttcarr, to be used after every kermit command. (2) Restore
  4242.  * the tty to the state it was in before kermit opened it. These choices
  4243.  * conflict, since ttold can't hold both choices of tty parameters.  ttres()
  4244.  * is currently being called as in choice (1), but ttold basically holds
  4245.  * the initial parameters, as in (2), and the description at the beginning
  4246.  * of this file says (2).
  4247.  *
  4248.  * I don't think restoring tty parameters after all kermit commands makes
  4249.  * much of a difference.  Restoring them upon exit from kermit may be of
  4250.  * some use in some cases (when the line is not restored automatically on
  4251.  * close, by the operating system).
  4252.  *
  4253.  * I can't choose which one it should be, so I haven't changed it. It
  4254.  * probably works as it is, too. It would probably even work even with
  4255.  * ttres() entirely deleted...
  4256.  *
  4257.  * (from fdc: Actually, this function operates in remote mode too, so
  4258.  * it restores the console (command) terminal to whatever mode it was
  4259.  * in before packet operations began, so that commands work right again.)
  4260.  */
  4261. int
  4262. ttres() {                               /* Restore the tty to normal. */
  4263.     int x;
  4264.  
  4265.     if (ttyfd < 0) return(-1);          /* Not open. */
  4266.  
  4267.     if (ttfdflg) return(0);        /* Don't mess with terminal modes if */
  4268.                     /* we got ttyfd from another process */
  4269. #ifdef    NETCONN
  4270.     if (netconn) {            /* Network connection */
  4271.         tvtflg = 0;
  4272. #ifdef TCPSOCKET
  4273. #ifdef TCP_NODELAY
  4274.         {
  4275.         extern int tcp_nodelay;    /* Just put this back if necessary */
  4276.         if (ttnet == NET_TCPB) {
  4277.         if (nodelay_sav > -1) {
  4278.             no_delay(nodelay_sav);
  4279.             nodelay_sav = -1;
  4280.         }
  4281.         }
  4282.         }
  4283. #endif /* TCP_NODELAY */
  4284. #ifdef TN_COMPORT
  4285.         if (istncomport()) {
  4286.             int rc = -1;
  4287.             if ((rc = tnsetflow(ttflow)) < 0)
  4288.           return(rc);
  4289.             if (ttspeed <= 0) 
  4290.           ttspeed = tnc_get_baud();
  4291.             else if ((rc = tnc_set_baud(ttspeed)) < 0)
  4292.           return(rc);
  4293.             tnc_set_datasize(8);
  4294.         tnc_set_stopsize(stopbits);
  4295.  
  4296. #ifdef HWPARITY
  4297.             if (hwparity) {
  4298.                 switch (hwparity) {
  4299.           case 'e':            /* Even */
  4300.                     debug(F100,"ttres 8 bits + even parity","",0);
  4301.                     tnc_set_parity(3);
  4302.                     break;
  4303.           case 'o':            /* Odd */
  4304.                     debug(F100,"ttres 8 bits + odd parity","",0);
  4305.                     tnc_set_parity(2);
  4306.                     break;
  4307.           case 'm':            /* Mark */
  4308.                     debug(F100,"ttres 8 bits + invalid parity: mark","",0);
  4309.                     tnc_set_parity(4);
  4310.                     break;
  4311.           case 's':            /* Space */
  4312.                     debug(F100,"ttres 8 bits + invalid parity: space","",0);
  4313.                     tnc_set_parity(5);
  4314.                     break;
  4315.                 }
  4316.             } else
  4317. #endif /* HWPARITY */
  4318.         {
  4319.                 tnc_set_parity(0);              /* None */
  4320.             }
  4321.             tvtflg = 0;
  4322.             return(0);
  4323.         }
  4324. #endif /* TN_COMPORT */
  4325. #endif /* TCPSOCKET */
  4326.     return(0);
  4327.     }
  4328. #endif    /* NETCONN */
  4329. #ifdef NETCMD
  4330.     if (ttpipe) return(0);
  4331. #endif /* NETCMD */
  4332. #ifdef NETPTY
  4333.     if (ttpty) return(0);
  4334. #endif /* NETPTY */
  4335.  
  4336. /* Real terminal device, so restore its original modes */
  4337.  
  4338. #ifdef BSD44ORPOSIX            /* For POSIX like this */
  4339.     debug(F100,"ttres BSD44ORPOSIX","",0);
  4340.     x = tcsetattr(ttyfd,TCSADRAIN,&ttold);
  4341. #else                    /* For all others... */
  4342. #ifdef ATTSV                            /* For AT&T versions... */
  4343.     debug(F100,"ttres ATTSV","",0);
  4344.     x = ioctl(ttyfd,TCSETAW,&ttold);    /* Restore tty modes this way. */
  4345. #else
  4346. /* Here we restore the modes for BSD */
  4347.  
  4348. #ifdef LPASS8                /* Undo "pass8" if it were done */
  4349.     if (lmodef) {
  4350.     if (ioctl(ttyfd,TIOCLSET,&lmode) < 0)
  4351.       debug(F100,"ttres TIOCLSET failed","",0);
  4352.     else
  4353.       debug(F100,"ttres TIOCLSET ok","",0);
  4354.     }
  4355. #endif /* LPASS8 */
  4356.  
  4357. #ifdef CK_DTRCTS           /* Undo hardware flow if it were done */
  4358.     if (lmodef) {
  4359.      if (ioctl(ttyfd,TIOCLSET,&lmode) < 0)
  4360.        debug(F100,"ttres TIOCLSET failed","",0);
  4361.      else
  4362.        debug(F100,"ttres TIOCLSET ok","",0);
  4363.     }
  4364. #endif /* CK_DTRCTS */
  4365.  
  4366. #ifdef TIOCGETC                /* Put back special characters */
  4367.     if (tcharf && (xlocal == 0)) {
  4368.     if (ioctl(ttyfd,TIOCSETC,&tchold) < 0)
  4369.       debug(F100,"ttres TIOCSETC failed","",0);
  4370.     else
  4371.       debug(F100,"ttres TIOCSETC ok","",0);
  4372.     }
  4373. #endif /* TIOCGETC */
  4374.  
  4375. #ifdef TIOCGLTC                /* Put back local special characters */
  4376.     if (ltcharf && (xlocal == 0)) {
  4377.     if (ioctl(ttyfd,TIOCSLTC,<chold) < 0)
  4378.       debug(F100,"ttres TIOCSLTC failed","",0);
  4379.     else
  4380.       debug(F100,"ttres TIOCSLTC ok","",0);
  4381.     }
  4382. #endif /* TIOCGLTC */
  4383.  
  4384. #ifdef BELLV10
  4385.     debug(F100,"ttres BELLV10","",0);
  4386.     x = ioctl(ttyfd,TIOCSETP,&ttold);    /* Restore both structs */
  4387.     x = ioctl(ttyfd,TIOCSDEV,&tdold);
  4388. #else
  4389.     debug(F100,"ttres stty","",0);
  4390.     x = stty(ttyfd,&ttold);             /* Restore tty modes the old way. */
  4391. #endif /* BELLV10 */
  4392.  
  4393.     if (!xlocal)
  4394.       msleep(100);            /* This replaces sleep(1)... */
  4395.                     /* Put back sleep(1) if tty is */
  4396.                     /* messed up after close. */
  4397. #endif /* ATTSV */
  4398. #endif /* BSD44ORPOSIX */
  4399.  
  4400.     debug(F101,"ttres result","",x);
  4401. #ifndef QNX
  4402.     if (x < 0) debug(F101,"ttres errno","",errno);
  4403. #endif /* QNX */
  4404.  
  4405. #ifdef AIXRS
  4406. #ifndef AIX41
  4407.     x = ioctl(ttyfd, ttld & 1 ? TXADDCD : TXDELCD, "rts");
  4408.     debug(F101,"ttres AIX line discipline rts restore","",x);
  4409. #endif /* AIX41 */
  4410. #endif /* AIXRS */
  4411.  
  4412. #ifdef BSD41
  4413.     if (ttld > -1) {            /* Put back line discipline */
  4414.     x = ioctl(ttyfd, TIOCSETD, &ttld);
  4415.     debug(F101,"ttres BSD41 line discipline restore","",x);
  4416.     if (x < 0) debug(F101,"...ioctl errno","",errno);
  4417.     ttld = -1;
  4418.     }
  4419. #endif /* BSD41 */
  4420.  
  4421. #ifdef sony_news
  4422.     x = xlocal ? km_ext : km_con;    /* Restore Kanji mode. */
  4423.     if (x != -1) {            /* Make sure we know original modes. */
  4424.     if (ioctl(ttyfd,TIOCKSET, &x) < 0) {
  4425.         perror("ttres can't set Kanji mode");
  4426.         debug(F101,"ttres error setting Kanji mode","",x);
  4427.         return(-1);
  4428.     }
  4429.     }
  4430.     debug(F100,"ttres set Kanji mode ok","",0);
  4431. #endif /* sony_news */
  4432.  
  4433.     tvtflg = 0;                /* Invalidate terminal mode settings */
  4434.     debug(F101,"ttres return code","",x);
  4435.     return(x);
  4436. }
  4437.  
  4438. #ifndef NOUUCP
  4439.  
  4440. /*  T T C H K P I D  --  Check lockfile pid  */
  4441. /*
  4442.   Read pid from lockfile named f, check that it's still valid.
  4443.   If so, return 1.
  4444.   On failure to read pid, return 1.
  4445.   Otherwise, try to delete lockfile f and return 0 if successful, else 1.
  4446. */
  4447. static int
  4448. ttchkpid(f) char *f; {
  4449.     int pid, mypid, x;
  4450.     pid = ttrpid(f);            /* Read pid from file. */
  4451.     if (pid > -1) {            /* If we were able to read the pid.. */
  4452.     debug(F101,"ttchkpid lock pid","",pid);
  4453.     errno = 0;            /* See if process still exists. */
  4454.     mypid = (int)getpid();        /* Get my own pid. */
  4455.     debug(F101,"ttchkpid my pid","",mypid);
  4456.     if (pid == mypid) {        /* It's me! */
  4457.         x = -1;            /* So I can delete it */
  4458.         errno = ESRCH;        /* pretend it's invalid */
  4459.     } else {            /* It's not me */
  4460.         x = kill((PID_T)pid, 0);    /* See if it's a live process */
  4461.         debug(F101,"ttchkpid kill errno","",errno);
  4462.     }
  4463.     debug(F101,"ttchkpid pid test","",x);
  4464.     if (x < 0 && errno == ESRCH) { /* pid is invalid */
  4465.         debug(F111,"removing stale lock",f,pid);
  4466.         if (!backgrd)
  4467.           printf("Removing stale lock %s (pid %d terminated)\n", f, pid);
  4468.         priv_on();
  4469.         x = unlink(f);        /* Remove the lockfile. */
  4470.         priv_off();
  4471.         debug(F111,"ttchkpid unlink",f,x);
  4472.         if (x > -1)
  4473.           return(0);        /* Device is not locked after all */
  4474.         else if (!backgrd)
  4475.           perror(f);
  4476.     }
  4477.     return(1);
  4478.     }
  4479.     return(1);                /* Failure to read pid */
  4480. }
  4481.  
  4482. #ifdef HPUX
  4483.  
  4484. /* Aliases (different drivers) for HP-UX dialout devices: */
  4485.  
  4486. static char *devprefix[] = { "tty", "ttyd", "cul", "cua", "cuad", "culd", "" };
  4487. static int ttydexists = 0;
  4488.  
  4489. #endif /* HPUX */
  4490.  
  4491. /*  T T R P I D  --  Read pid from lockfile "name" */
  4492.  
  4493. static int
  4494. ttrpid(name) char *name; {
  4495.     long len;
  4496.     int x, fd, pid;
  4497.     short spid;
  4498.     char buf[32];
  4499.  
  4500.     debug(F110,"ttrpid",name,0);
  4501.     if (!name) return(-1);
  4502.     if (!*name) return(-1);
  4503.     priv_on();
  4504.     len = zchki(name);            /* Get file length */
  4505.     priv_off();
  4506.     debug(F101,"ttrpid zchki","",len);
  4507.     if (len < 0)
  4508.       return(-1);
  4509.     if (len > 31)
  4510.       return(-1);
  4511.     priv_on();
  4512.     fd = open(name,O_RDONLY);        /* Try to open lockfile. */
  4513.     priv_off();
  4514.     debug(F101,"ttrpid fd","",fd);
  4515.     if (fd <= 0)
  4516.       return(-1);
  4517. /*
  4518.   Here we try to be flexible and allow for all different binary and string
  4519.   formats at runtime, rather than a specific format for each configuration
  4520.   hardwired at compile time.
  4521. */
  4522.     pid = -1;
  4523. #ifndef COHERENT
  4524. /*
  4525.   COHERENT uses a string PID but without leading spaces or 0's, so there is
  4526.   no way to tell from the file's length whether it contains a string or binary
  4527.   pid.  So for COHERENT only, we only allow string pids.  For all others, we
  4528.   decide based on the size of the lockfile.
  4529. */
  4530.     if (len > 4) {            /* If file > 4 bytes it's a string */
  4531. #endif /* COHERENT */
  4532.     x = read(fd,buf,(int)len);
  4533.     debug(F111,"ttrpid string read",buf,x);
  4534.     if (x < 0) {
  4535.         pid = -1;
  4536.     } else {
  4537.         buf[31] = '\0';
  4538.         x = sscanf(buf,"%d",&pid);    /* Get the integer pid from it. */
  4539.     }
  4540. #ifndef COHERENT
  4541.     } else if (len == 4) {        /* 4 bytes so binary */
  4542.     x = read(fd, (char *)&pid, 4);    /* Read the bytes into an int */
  4543.     debug(F101,"ttrpid integer read","",x);
  4544.     if (x < 4)
  4545.       pid = -1;
  4546.     } else if (len == 2) {        /* 2 bytes binary */
  4547.     x = read(fd, (char *)&spid, 2);    /* Read the bytes into a short */
  4548.     debug(F101,"ttrpid short read","",x);
  4549.     if (x < 2)
  4550.       pid = -1;
  4551.     else
  4552.       pid = spid;
  4553.     } else
  4554.       pid = -1;
  4555. #endif /* COHERENT */
  4556.     close(fd);                /* Close the lockfile */
  4557.     debug(F101,"ttrpid pid","",pid);
  4558.     return(pid);
  4559. }
  4560. #endif /* NOUUCP */
  4561.  
  4562. /*  T T L O C K  */
  4563.  
  4564. /*
  4565.   This function attempts to coordinate use of the communication device with
  4566.   other copies of Kermit and any other program that follows the UUCP
  4567.   device-locking conventions, which, unfortunately, vary among different UNIX
  4568.   implementations.  The idea is to look for a file of a certain name, the
  4569.   "lockfile", in a certain directory.  If such a file is found, then the line
  4570.   is presumed to be in use, and Kermit should not use it.  If no such file is
  4571.   found, Kermit attempts to create one so that other programs will not use the
  4572.   same line at the same time.  Because the lockfile and/or the directory it's
  4573.   in might lack write permission for the person running Kermit, Kermit could
  4574.   find itself running setuid to uucp or other user that does have the
  4575.   necessary permissions.  At startup, Kermit has changed its effective uid to
  4576.   the user's real uid, and so ttlock() must switch back to the original
  4577.   effective uid in order to create the lockfile, and then back again to the
  4578.   real uid to prevent unauthorized access to other directories or files owned
  4579.   by the user the program is setuid to.
  4580.  
  4581.   Totally rewritten for C-Kermit 5A to eliminate windows of vulnerability,
  4582.   based on suggestions from Warren Tucker.  Call with pointer to name of
  4583.   tty device.  Returns:
  4584.  
  4585.    0 on success
  4586.   -1 on failure
  4587.  
  4588.   Note: Once privileges are turned on using priv_on(), it is essential that
  4589.   they are turned off again before this function returns.
  4590. */
  4591. #ifdef SVR4                /* Lockfile uses device numbers. */
  4592. /*
  4593.   Although I can't find this in writing anywhere (e.g. in SVID for SVR4),
  4594.   it is the behavior of the "reference version" of SVR4, i.e. the Intel
  4595.   port from UNIX Systems Laboratories, then called Univel UnixWare,
  4596.   then called Novell UnixWare, then called SCO Unixware, then called Caldera
  4597.   Open UNIX...  It also makes much more sense than device-name-based lockfiles
  4598.   since there can be multiple names for the same device, symlinks, etc.
  4599. */
  4600. #ifndef LFDEVNO                /* Define this for SVR4 */
  4601. #ifndef AIXRS                /* But not for RS/6000 AIX 3.2, etc. */
  4602. #ifndef BSD44                /* If anybody else needs it... */
  4603. #ifndef __386BSD__
  4604. #ifndef __FreeBSD__
  4605. #ifndef HPUX10
  4606. #ifndef IRIX51                /* SGI IRIX 5.1 or later */
  4607. #ifndef CK_SCOV5            /* SCO Open Server 5.0 */
  4608. #define LFDEVNO
  4609. #endif /* CK_SCOV5 */
  4610. #endif /* IRIX51 */
  4611. #endif /* HPUX10 */
  4612. #endif /* __FreeBSD__ */
  4613. #endif /* __386BSD__ */
  4614. #endif /* BSD44 */
  4615. #endif /* AIXRS */
  4616. #endif /* LFDEVNO */            /* ... define it here or on CC */
  4617. #endif /* SVR4 */            /* command line. */
  4618.  
  4619. #ifdef COHERENT
  4620. #define LFDEVNO
  4621. #endif /* COHERENT */
  4622.  
  4623. /*
  4624.   For platforms where the lockfile name is made from device/major/minor
  4625.   device number, as in SVR4.  Which, if we must have lockfiles at all, is
  4626.   by far the best format, since it eliminates all the confusion that stems
  4627.   from multiple names (or drivers) for the same port, not to mention
  4628.   symlinks.  It might even be a good idea to start using this form even
  4629.   on platforms where it's not supported, alongside the normal forms for those
  4630.   platforms, in order to get people used to it...
  4631. */
  4632. #ifdef LFDEVNO
  4633. #ifndef major                /* If we didn't find it */
  4634. #ifdef SVR4                /* then for Sys V R4 */
  4635. #include <sys/mkdev.h>            /* look here */
  4636. #else                    /* or for SunOS versions */
  4637. #ifdef SUNOS4                /* ... */
  4638. #include <sys/sysmacros.h>        /* look here */
  4639. #else                    /* Otherwise take a chance: */
  4640. #define    major(dev) ( (int) ( ((unsigned)(dev) >> 8) & 0xff))
  4641. #define    minor(dev) ( (int) ( (dev) & 0xff))
  4642. #endif /* SUNOS4 */
  4643. #endif /* SVR4 */
  4644. #endif /* major */
  4645. #endif /* LFDEVNO */
  4646.  
  4647. /* No advisory locks if F_TLOCK and F_ULOCK are not defined at this point */
  4648.  
  4649. #ifdef LOCKF
  4650. #ifndef F_TLOCK
  4651. #undef LOCKF
  4652. #ifndef NOLOCKF
  4653. #define NOLOCKF
  4654. #endif /* NOLOCKF */
  4655. #endif /* F_TLOCK */
  4656. #endif /* LOCKF */
  4657.  
  4658. #ifdef LOCKF
  4659. #ifndef F_ULOCK
  4660. #undef LOCKF
  4661. #ifndef NOLOCKF
  4662. #define NOLOCKF
  4663. #endif /* NOLOCKF */
  4664. #endif /* F_ULOCK */
  4665. #endif /* LOCKF */
  4666.  
  4667. static char linkto[DEVNAMLEN+1];
  4668. static char * linkdev = NULL;
  4669.  
  4670. #ifndef NOUUCP
  4671. #ifdef USETTYLOCK
  4672. #ifdef LOCK_DIR
  4673. char * uucplockdir = LOCK_DIR;
  4674. #else
  4675. char * uucplockdir = "";
  4676. #endif /* LOCK_DIR */
  4677. #else
  4678. #ifdef LOCK_DIR
  4679. char * uucplockdir = LOCK_DIR;
  4680. #else
  4681. char * uucplockdir = "";
  4682. #endif /* LOCK_DIR */
  4683. #endif /* USETTYLOCK */
  4684. #else
  4685. char * uucplockdir = "";
  4686. #endif /* NOUUCP */
  4687.  
  4688. #ifdef QNX                /* Only for QNX4 */
  4689. int                    /* Visible to outside world */
  4690. qnxopencount() {            /* Get QNX device open count */
  4691.     struct _dev_info_entry info;
  4692.     int x;
  4693.  
  4694.     x = -1;                /* Unknown */
  4695.     if (ttyfd > -1) {
  4696.     if (!dev_info(ttyfd, &info)) {
  4697.         debug(F101,"ttlock QNX open_count","",info.open_count);
  4698.         x = info.open_count;
  4699.     }
  4700.     }
  4701.     return(x);
  4702. }
  4703. #endif /* QNX */
  4704.  
  4705. char *
  4706. ttglckdir() {                /* Get Lockfile directory name */
  4707. #ifdef __OpenBSD__
  4708.     return("/var/spool/lock");
  4709. #else /* __OpenBSD__ */
  4710. #ifdef __FreeBSD__
  4711.     return("/var/spool/lock");
  4712. #else  /* __FreeBSD__ */
  4713. #ifdef LOCK_DIR
  4714.     char * s = LOCK_DIR;
  4715. #endif /* LOCK_DIR */
  4716. #ifdef NOUUCP
  4717.     return("");
  4718. #else  /* NOUUCP */
  4719. #ifdef LOCK_DIR
  4720.     return(s);
  4721. #else  /* LOCK_DIR */
  4722.     return("");
  4723. #endif /* LOCK_DIR */
  4724. #endif /* NOUUCP */
  4725. #endif /* __FreeBSD__ */
  4726. #endif /* __OpenBSD__ */
  4727. }
  4728.  
  4729. static int
  4730. ttlock(ttdev) char *ttdev; {
  4731.  
  4732.     int x, n;
  4733.     int islink = 0;
  4734.  
  4735. #ifdef NOUUCP
  4736.     debug(F100,"ttlock NOUUCP","",0);
  4737.     ckstrncpy(flfnam,"NOLOCK",FLFNAML);
  4738.     haslock = 1;
  4739.     return(0);
  4740. #else /* !NOUUCP */
  4741.  
  4742. #ifdef USETTYLOCK
  4743.     haslock = 0;                        /* Not locked yet. */
  4744.     *flfnam = '\0';            /* Lockfile name is empty. */
  4745.     if (!strncmp(ttdev,"/dev/",5) && ttdev[5])
  4746.       ckstrncpy(lockname,ttdev+5,DEVNAMLEN);
  4747.     else
  4748.       ckstrncpy(lockname,ttdev,DEVNAMLEN);
  4749. /*
  4750.   This might be overkill, but it's not clear from the man pages whether
  4751.   ttylock() can be called without calling ttylocked() first, since the doc
  4752.   says that ttylocked() removes any stale lockfiles, but it does not say this
  4753.   about ttylock().  Also the docs don't say what ttylocked() returns in the
  4754.   case when it finds and removes a stale lockfile.  So one or both calls to
  4755.   to ttylocked() might be superfluous, but they should do no harm.  Also I'm
  4756.   assuming that we have to do all the same ID swapping, etc, with these
  4757.   routines as we do without them.  Thus the priv_on/off() sandwich.
  4758. */
  4759. #ifdef USE_UU_LOCK
  4760.     priv_on();                /* Turn on privs */
  4761.     x = uu_lock(lockname);        /* Try to set the lock */
  4762.     priv_off();                /* Turn privs off */
  4763.     debug(F111,"ttlock uu_lock",lockname,x);
  4764.     switch (x) {
  4765.       case UU_LOCK_INUSE:
  4766.     return(-2);
  4767.       case UU_LOCK_OK:
  4768. #ifdef BSD44
  4769.     ckmakmsg(flfnam,FLFNAML,"/var/spool/lock/LCK..",lockname,NULL,NULL);
  4770. #endif /* BSD44 */
  4771.     haslock = 1;
  4772.     return(0);
  4773.       default:
  4774.     return(-1);
  4775.     }
  4776. #else  /* USE_UU_LOCK */
  4777.     priv_on();                /* Turn on privs */
  4778.     if (ttylocked(lockname)) {        /* This should remove any stale lock */
  4779.     if (ttylocked(lockname)) {    /* so check again. */
  4780.         priv_off();
  4781.         return(-5);            /* Still locked, fail. */
  4782.     }
  4783.     }
  4784.     x = ttylock(lockname);        /* Lock it. */
  4785.     priv_off();                /* Turn off privs */
  4786.  
  4787.     debug(F111,"ttlock lockname",lockname,x);
  4788.     if (x > -1) {
  4789.     /*
  4790.       We don't really know the name of the lockfile, but
  4791.       this is what the man page says it is.  In USETTYLOCK
  4792.           builds, it is used only for display by SHOW COMM.
  4793.     */
  4794.     ckmakmsg(flfnam,FLFNAML,"/etc/locks/LCK..",lockname,NULL,NULL);
  4795.     haslock = 1;
  4796.     }
  4797.     return(x);
  4798. #endif /* USE_UU_LOCK */
  4799. #else  /* Systems that don't have ttylock()... */
  4800.  
  4801. #ifndef HPUX
  4802.  
  4803.     int lockfd;                /* File descriptor for lock file. */
  4804.     PID_T pid;                /* Process id of this process. */
  4805.     int tries;                /* How many times we've tried... */
  4806.     struct stat devbuf;            /* For device numbers (SVR4). */
  4807.  
  4808. #ifdef PIDSTRING
  4809.     char pid_str[32];            /* My pid in string format. */
  4810. #endif /* PIDSTRING */
  4811.  
  4812.     char *device, *devname;
  4813.  
  4814. #define LFNAML 256            /* Max length for lock file name. */
  4815.     char lockfil[LFNAML];        /* Lock file name */
  4816. #ifdef RTAIX
  4817.     char lklockf[LFNAML];        /* Name for link to lock file  */
  4818. #endif /* RTAIX */
  4819. #ifdef CKSYMLINK
  4820.     char symlock[LFNAML];        /* Name for symlink lockfile name */
  4821. #endif /* CKSYMLINK */
  4822.     char tmpnam[LFNAML+30];        /* Temporary lockfile name. */
  4823.     char *lockdir = LOCK_DIR;        /* Defined near top of this file, */
  4824.                     /* or on cc command line. */
  4825.     haslock = 0;                        /* Not locked yet. */
  4826.     *flfnam = '\0';            /* Lockfile name is empty. */
  4827.     lock2[0] = '\0';            /* Clear secondary lockfile name. */
  4828.     pid = getpid();            /* Get id of this process. */
  4829.  
  4830. /*  Construct name of lockfile and temporary file */
  4831.  
  4832. /*  device  = name of tty device without the path, e.g. "ttyh8" */
  4833. /*  lockfil = name of lock file, without path, e.g. "LCK..ttyh8" */
  4834.  
  4835.     device = ((devname = xxlast(ttdev,'/')) != NULL ? devname+1 : ttdev);
  4836.  
  4837.     if (stat(ttdev,&devbuf) < 0)
  4838.       return(-1);
  4839.  
  4840. #ifdef CKSYMLINK
  4841.     islink = 1;                /* Assume it's a symlink */
  4842.     linkto[0] = '\0';            /* But we don't know to what */
  4843. #ifdef COMMENT
  4844. /*
  4845.   This is undependable.  If it worked it would save the readlink call if
  4846.   we knew the device name was not a link.
  4847. */
  4848. #ifdef S_ISLNK
  4849.     islink = S_ISLNK(devbuf.st_mode);
  4850.     debug(F101,"ttlock stat S_ISLNK","",islink);
  4851. #endif /* S_ISLNK */
  4852. #endif /* COMMENT */
  4853.     if (islink) {
  4854.     n = readlink(ttdev,linkto,DEVNAMLEN); /* See if it's a link */
  4855.     debug(F111,"ttlock readlink",ttdev,n);
  4856.     if (n > -1)            /* It is */
  4857.       linkto[n] = '\0';
  4858.     else                /* It's not */
  4859.       islink = 0;
  4860.     debug(F111,"ttlock link",linkto,islink);
  4861.     }
  4862.     if (islink) {
  4863.     linkdev = (devname = xxlast(linkto,'/')) ? devname + 1 : linkto;
  4864.     debug(F110,"ttlock linkdev",linkdev,0);
  4865.     }
  4866. #endif /* CKSYMLINK */
  4867.  
  4868. /*
  4869.   On SCO platforms, if we don't have a symlink, then let's pretend the
  4870.   name given for the device is a symlink, because later we will change
  4871.   the name if it contains any uppercase characters.
  4872. */
  4873. #ifdef CK_SCOV5                /* SCO Open Server 5.0 */
  4874.     if (!islink) {
  4875.     islink = 1;
  4876.     ckstrncpy(linkto,ttdev,DEVNAMLEN);
  4877.     linkdev = (devname = xxlast(linkto,'/')) ? devname + 1 : linkto;
  4878.     debug(F110,"ttlock linkdev",linkdev,0);
  4879.     }
  4880. #else
  4881. #ifdef M_XENIX                /* SCO Xenix or UNIX */
  4882.     if (!islink) {
  4883.     islink = 1;
  4884.     ckstrncpy(linkto,ttdev,DEVNAMLEN);
  4885.     linkdev = (devname = xxlast(linkto,'/')) ? devname + 1 : linkto;
  4886.     debug(F110,"ttlock linkdev",linkdev,0);
  4887.     }
  4888. #endif /* M_XENIX */
  4889. #endif /* CK_SCOV5 */
  4890.  
  4891. #ifdef ISIII                /* Interactive System III, PC/IX */
  4892.     ckstrncpy(lockfil, device, DEVNAMLEN);
  4893. #else  /* not ISIII */
  4894. #ifdef LFDEVNO                /* Lockfilename has device numbers. */
  4895. #ifdef COHERENT
  4896.     sprintf(lockfil,"LCK..%d.%d",    /* SAFE */
  4897.         major(devbuf.st_rdev),       /* major device number */
  4898.         0x1f & minor(devbuf.st_rdev)); /* minor device number */
  4899. #else
  4900.     /* Note: %d changed to %u in 8.0 -- %u is part of SVID for SVR4 */
  4901.     /* Lockfile name format verified to agree with Solaris cu, Dec 2001 */
  4902.     sprintf(lockfil,"LK.%03u.%03u.%03u", /* SAFE */
  4903.         major(devbuf.st_dev),    /* device */
  4904.         major(devbuf.st_rdev),    /* major device number */
  4905.         minor(devbuf.st_rdev));    /* minor device number */
  4906. #endif /* COHERENT */
  4907. #else  /* Not LFDEVNO */
  4908. #ifdef PTX                /* Dynix PTX */
  4909.     if ((device != &ttdev[5]) && (strncmp(ttdev,"/dev/",5) == 0)) {
  4910.     if ((int)strlen(device) + 8 < LFNAML)
  4911.       sprintf(lockfil,"LCK..%.3s%s", &ttdev[5], device);
  4912.     else
  4913.       ckstrncpy(lockfil,"LOCKFILE_NAME_TOO_LONG",LFNAML);
  4914.     } else
  4915. #endif /* PTX */
  4916.       if ((int)strlen(device) + 5 < LFNAML)
  4917.     sprintf(lockfil,"LCK..%s", device);
  4918.       else
  4919.     ckstrncpy(lockfil,"LOCKFILE_NAME_TOO_LONG",LFNAML);
  4920. #ifdef RTAIX
  4921.     ckstrncpy(lklockf,device,DEVNAMLEN);
  4922. #endif /* RTAIX */
  4923. #ifdef CKSYMLINK
  4924.     symlock[0] = '\0';
  4925.     if (islink)
  4926.       ckmakmsg(symlock,LFNAML, "LCK..", linkdev, NULL, NULL);
  4927. #endif /* CKSYMLINK */
  4928. #endif /* LFDEVNO */
  4929. #endif /* ISIII */
  4930.  
  4931. #ifdef CK_SCOV5                /* SCO Open Server 5.0 */
  4932.     {
  4933.     /* Lowercase the entire filename. */
  4934.         /* SCO says we must do this in V5.0 and later. */
  4935.     /* BUT... watch out for devices -- like Digiboard Portserver */
  4936.     /* That can have hundreds of ports... */
  4937.     char *p = (char *)(lockfil + 5);
  4938.     while (*p) { if (isupper(*p)) *p = (char) tolower(*p); p++; }
  4939.     }
  4940. #ifdef CKSYMLINK
  4941.     if (islink) {            /* If no change */
  4942.     if (!strcmp(lockfil,symlock)) {    /* then no second lockfile needed */
  4943.         islink = 0;
  4944.         symlock[0] = '\0';
  4945.     }
  4946.     }
  4947. #endif /* CKSYMLINK */
  4948. #else
  4949. #ifdef M_XENIX                /* SCO Xenix or UNIX */
  4950.     {
  4951.     int x; char c;
  4952.     x = (int)strlen(lockfil) - 1;    /* Get last letter of device name. */
  4953.     if (x > 0) {            /* If it's uppercase, lower it. */
  4954.         c = lockfil[x];
  4955.         if (c >= 'A' && c <= 'Z') lockfil[x] += ('a' - 'A');
  4956.     }
  4957.     }
  4958. #ifdef CKSYMLINK
  4959.     if (islink) {
  4960.     if (!strcmp(lockfil,symlock)) {    /* No change */
  4961.         islink = 0;            /* so no second lockfile */
  4962.         symlock[0] = '\0';
  4963.     }
  4964.     }
  4965. #endif /* CKSYMLINK */
  4966. #endif /* M_XENIX */
  4967. #endif /* CK_SCOV5 */
  4968.  
  4969. /*  flfnam = full lockfile pathname, e.g. "/usr/spool/uucp/LCK..ttyh8" */
  4970. /*  tmpnam = temporary unique, e.g. "/usr/spool/uucp/LTMP..pid" */
  4971.  
  4972.     ckmakmsg(flfnam,LFNAML,lockdir,"/",lockfil,NULL);
  4973.  
  4974. #ifdef RTAIX
  4975.     ckmakmsg(lkflfn,FLFNAML,lockdir,"/",lklockf,NULL);
  4976. #endif /* RTAIX */
  4977.  
  4978. #ifndef LFDEVNO
  4979. #ifdef CKSYMLINK
  4980.     /* If it's a link then also make a lockfile for the real name */
  4981.     debug(F111,"ttlock link symlock",symlock,islink);
  4982.     if (islink && symlock[0]) {
  4983.     /* But only if the lockfile names would be different. */
  4984.     /* WARNING: They won't be, e.g. for /dev/ttyd2 => /hw/ttys/ttyd2 */
  4985.     ckmakmsg(lock2,FLFNAML,lockdir,"/",symlock,NULL);
  4986.     debug(F110,"ttlock lock2",lock2,0);
  4987.     if (!strcmp(lock2,flfnam)) {    /* Are lockfile names the same? */
  4988.         debug(F100,"ttlock lock2 cleared","",0);
  4989.         lock2[0] = '\0';        /* Clear secondary lockfile name. */
  4990.     }
  4991.     }
  4992. #endif /* CKSYMLINK */
  4993. #endif /* LFDEVNO */
  4994.  
  4995.     sprintf(tmpnam,"%s/LTMP.%05d",lockdir,(int) pid); /* safe */
  4996.     debug(F110,"ttlock flfnam",flfnam,0);
  4997.     debug(F110,"ttlock tmpnam",tmpnam,0);
  4998.  
  4999.     priv_on();                /* Turn on privileges if possible. */
  5000.     lockfd = creat(tmpnam, 0444);    /* Try to create temp lock file. */
  5001.     if (lockfd < 0) {            /* Create failed. */
  5002.     debug(F111,"ttlock creat failed",tmpnam,errno);
  5003.     if (errno == ENOENT) {
  5004.         perror(lockdir);
  5005.         printf("UUCP not installed or Kermit misconfigured\n");
  5006.     } else {
  5007.         if (!quiet)
  5008.           perror(lockdir);
  5009.         unlink(tmpnam);        /* Get rid of the temporary file. */
  5010.     }
  5011.     priv_off();            /* Turn off privileges!!! */
  5012.     return(-1);            /* Return failure code. */
  5013.     }
  5014. /* Now write the pid into the temp lockfile in the appropriate format */
  5015.  
  5016. #ifdef PIDSTRING            /* For Honey DanBer UUCP, */
  5017.     sprintf(                /* write PID as decimal string */
  5018.         pid_str,
  5019. #ifdef LINUXFSSTND            /* The "Linux File System Standard" */
  5020. #ifdef FSSTND10                /* Version 1.0 calls for */
  5021.         "%010d\n",            /* leading zeros */
  5022. #else                    /* while version 1.2 calls for */
  5023.         "%10d\n",            /* leading spaces */
  5024. #endif /* FSSTND10 */
  5025. #else
  5026. #ifdef COHERENT
  5027.         "%d\n",            /* with leading nothing */
  5028. #else
  5029.         "%10d\n",            /* with leading blanks */
  5030. #endif /* COHERENT */
  5031. #endif /* LINUXFSSTND */
  5032.         (int) pid
  5033.         );                /* safe */
  5034.     write(lockfd, pid_str, 11);
  5035.     debug(F111,"ttlock hdb pid string",pid_str,(int) pid);
  5036.  
  5037. #else /* Not PIDSTRING, use integer PID */
  5038.  
  5039.     write(lockfd, (char *)&pid, sizeof(pid) );
  5040.     debug(F101,"ttlock pid","",(int) pid);
  5041.  
  5042. #endif /* PIDSTRING */
  5043.  
  5044. /* Now try to rename the temp file to the real lock file name. */
  5045. /* This will fail if a lock file of that name already exists.  */
  5046.  
  5047.     close(lockfd);            /* Close the temp lockfile. */
  5048.     chmod(tmpnam,0444);            /* Permission for a valid lock. */
  5049.     tries = 0;
  5050.     while (!haslock && tries++ < 2) {
  5051.     haslock = (link(tmpnam,flfnam) == 0); /* Create a link to it. */
  5052.     if (haslock) {                  /* If we got the lockfile */
  5053. #ifdef RTAIX
  5054.         link(flfnam,lkflfn);
  5055. #endif /* RTAIX */
  5056. #ifdef CKSYMLINK
  5057. #ifndef LFDEVNO
  5058.         if (islink && lock2[0])
  5059.           link(flfnam,lock2);
  5060. #endif /* LFDEVNO */
  5061. #endif /* CKSYMLINK */
  5062.  
  5063. #ifdef COMMENT
  5064. /* Can't do this any more because device is not open yet so no ttyfd. */
  5065. #ifdef LOCKF
  5066. /*
  5067.   Advisory file locking works on SVR4, so we use it.  In fact, it is
  5068.   necessary in some cases, e.g. when SLIP is involved.  But it still doesn't
  5069.   seem to prevent multiple users accessing the same device by different names.
  5070. */
  5071.             while (lockf(ttyfd, F_TLOCK, 0L) != 0) {
  5072.                 debug(F111, "ttlock lockf returns errno", "", errno);
  5073.                 if ((++tries >= 3) || (errno != EAGAIN)) {
  5074.                     x = unlink(flfnam); /* remove the lockfile */
  5075. #ifdef RTAIX
  5076.             unlink(lkflfn);    /* And any links to it... */
  5077. #endif /* RTAIX */
  5078. #ifdef CKSYMLINK
  5079. #ifndef LFDEVNO
  5080.             if (islink && lock2[0])
  5081.               unlink(lock2);    /* ditto... */
  5082. #endif /* LFDEVNO */
  5083. #endif /* CKSYMLINK */
  5084.                     debug(F111,"ttlock unlink",flfnam,x);
  5085.                     haslock = 0;
  5086.             break;
  5087.         }
  5088.                 sleep(2);
  5089.         }
  5090.         if (haslock)        /* If we got an advisory lock */
  5091. #endif /* LOCKF */
  5092. #endif /* COMMENT */
  5093.           break;            /* We're done. */
  5094.  
  5095.     } else {            /* We didn't create a new lockfile. */
  5096.         priv_off();
  5097.         if (ttchkpid(flfnam)) {    /* Check existing lockfile */
  5098.         priv_on();        /* cause ttchkpid turns priv_off... */
  5099.         unlink(tmpnam);        /* Delete the tempfile */
  5100.         debug(F100,"ttlock found tty locked","",0);
  5101.         priv_off();        /* Turn off privs */
  5102.         return(-2);        /* Code for device is in use. */
  5103.         }
  5104.         priv_on();
  5105.     }
  5106.     }
  5107.     unlink(tmpnam);            /* Unlink (remove) the temp file. */
  5108.     priv_off();                /* Turn off privs */
  5109.     return(haslock ? 0 : -1);        /* Return link's return code. */
  5110.  
  5111. #else /* HPUX */
  5112.  
  5113. /*
  5114.   HP-UX gets its own copy of this routine, modeled after the observed behavior
  5115.   of the HP-UX 'cu' program.  HP-UX serial device names consist of a base name
  5116.   such as "tty", "ttyd", "cua", "cul", "cuad", or "culd", followed by a unit
  5117.   designator which is a string of digits, possibly containing an imbedded
  5118.   letter "p".  Examples (for base name "tty"):
  5119.  
  5120.      /dev/tty0, /dev/tty00, dev/ttyd00, /dev/tty0p0
  5121.  
  5122.   According to the HP-UX UUCP manual of 1988, the "0p0" notation has been
  5123.   used on Series 800 since HP-UX 2.00, and the "non-p" notation was used
  5124.   on other models.  In HP-UX 10.00, "0p0" notation was adopted for all models.
  5125.   However, we make and enforce no such distinctions; either notation is
  5126.   accepted on any model or HP-UX version as a valid unit designator.
  5127.  
  5128.   If a valid unit is specified (as opposed to a designer name or symlink), we
  5129.   check for all aliases of the given unit according to the devprefix[] array.
  5130.   If no lockfiles are found for the given unit, we can have the device; we
  5131.   create a lockfile LCK..name in the lockfile directory appropriate for the
  5132.   HP-UX version (/var/spool/locks for 10.00 and later, /usr/spool/uucp for
  5133.   9.xx and earlier).  If it is a "cua" or "cul" device, a second lockfile is
  5134.   created with the "ttyd" prefix.  This is exactly what cu does.
  5135.  
  5136.   If the "set line" device does not have a valid unit designator, then it is
  5137.   used literally and no synomyms are searched for and only one lockfile is
  5138.   created.
  5139.  
  5140.   -fdc, March 1998.
  5141. */
  5142. #define LFNAML 80            /* Max length for lock file name. */
  5143.  
  5144.     int lockfd;                /* File descriptor for lock file. */
  5145.     PID_T pid;                /* Process ID of this process. */
  5146.     int fpid;                /* pid found in existing lockfile. */
  5147.     int tries;                /* How many times we've tried... */
  5148.     int i, k;                /* Workers */
  5149.  
  5150.     char *device, *devname;        /* "/dev/xxx", "xxx" */
  5151.     char *unit, *p;            /* <instance>p<port> part of xxx */
  5152.  
  5153.     char lockfil[LFNAML];        /* Lockfile name (no path) */
  5154.     char tmpnam[LFNAML];        /* Temporary lockfile name. */
  5155.  
  5156. #ifdef HPUX10                /* Lockfile directory */
  5157.     char *lockdir = "/var/spool/locks";    /* Always this for 10.00 and higher */
  5158. #else  /* HP-UX 9.xx and below */
  5159. #ifdef LOCK_DIR
  5160.     char *lockdir = LOCK_DIR;        /* Defined near top of this file */
  5161. #else
  5162.     char *lockdir = "/usr/spool/uucp";    /* or not... */
  5163. #endif /* LOCK_DIR */
  5164. #endif /* HPUX10 */
  5165.  
  5166.     haslock = 0;                        /* Not locked yet. */
  5167.     *flfnam = '\0';            /* Lockfile name is empty. */
  5168.     lock2[0] = '\0';            /* Second one too. */
  5169.     pid = getpid();            /* Get my process ID */
  5170. /*
  5171.   Construct name of lockfile and temporary file...
  5172.   device  = name of tty device without the path, e.g. "tty0p0"
  5173.   lockfil = name of lock file, without path, e.g. "LCK..tty0p0"
  5174. */
  5175.     device = ((devname = xxlast(ttdev,'/')) != NULL ? devname+1 : ttdev);
  5176.     debug(F110,"TTLOCK device",device,0);
  5177.     ckmakmsg(lockfil,LFNAML,"LCK..",device,NULL,NULL);
  5178.  
  5179.     k = 0;                /* Assume device is not locked */
  5180.     n = 0;                /* Digit counter */
  5181.     unit = device;            /* Unit = <instance>p<port> */
  5182.     while (*unit && !isdigit(*unit))    /* Search for digit... */
  5183.       unit++;
  5184.     p = unit;                /* Verify <num>p<num> format... */
  5185.     debug(F110,"TTLOCK unit 1",unit,0);
  5186. /*
  5187.   The unit number is recognized as:
  5188.   (a) any sequence of digits that runs to the end of the string.
  5189.   (b) any (a) that includes one and only one letter "p", with at least
  5190.       one digit before and after it.
  5191. */
  5192.     while (isdigit(*p)) p++, n++;    /* Get a run of digits */
  5193.     if (*p && n > 0) {            /* Have a "p"? */
  5194.     if (*p == 'p' && isdigit(*(p+1))) {
  5195.         p++;
  5196.         n = 0;
  5197.         while (isdigit(*p)) p++, n++;
  5198.     }
  5199.     }
  5200.     if (n == 0 || *p) unit = "";
  5201.     debug(F110,"TTLOCK unit 2",unit,0);
  5202.  
  5203.     if (*unit) {            /* Device name has unit number. */
  5204.     /* The following loop not only searches for the various lockfile    */
  5205.     /* synonyms, but also removes all -- not just one -- stale lockfile */
  5206.     /* for the device, should there be more than one.  See ttchkpid().  */
  5207.     ttydexists = 0;
  5208.     for (i = 0; *devprefix[i]; i++) { /* For each driver... */
  5209.         /* Make device name */
  5210.         ckmakmsg(lock2,FLFNAML,"/dev/",devprefix[i],unit,NULL);
  5211.         priv_on();            /* Privs on */
  5212.         k = zchki(lock2) != -1;    /* See if device exists */
  5213.         priv_off();            /* Privs off */
  5214.         debug(F111,"TTLOCK exist",lock2,k);
  5215.             if (k) {
  5216.         if (!strcmp(devprefix[i],"ttyd")) /* ttyd device exists */
  5217.           ttydexists = 1;
  5218.         /* Make lockfile name */
  5219.         ckmakmsg(lock2,FLFNAML,lockdir,"/LCK..",devprefix[i],unit);
  5220.         debug(F110,"TTLOCK checking",lock2,0);
  5221.         priv_on();        /* Privs on */
  5222.         k = zchki(lock2) != -1;    /* See if lockfile exists */
  5223.         priv_off();        /* Privs off */
  5224.         debug(F111,"TTLOCK check for lock A",lock2,k);
  5225.         if (k) if (ttchkpid(lock2)) { /* If pid still active, fail. */
  5226.             ckstrncpy(flfnam,lock2,FLFNAML);
  5227.             return(-2);
  5228.         }
  5229.         }
  5230.     }
  5231.     } else {                /* Some other device-name format */
  5232.     /* This takes care of symbolic links, etc... */
  5233.     /* But does not chase them down! */
  5234.     ckmakmsg(lock2,FLFNAML,lockdir,"/LCK..",device,NULL);
  5235.     priv_on();
  5236.     k = zchki(lock2) != -1;        /* Check for existing lockfile */
  5237.     priv_off();
  5238.     debug(F111,"TTLOCK check for lock B",lock2,k);
  5239.     if (k) if (ttchkpid(lock2)) {    /* Check pid from lockfile */
  5240.         ckstrncpy(flfnam,lock2,FLFNAML);
  5241.         debug(F110,"TTLOCK in use",device,0);
  5242.         debug(F101,"TTLOCK returns","",-2);
  5243.         return(-2);
  5244.     }
  5245.     }
  5246. /*
  5247.   Get here only if there is no (more) lockfile, so now we make one (or two)...
  5248.   flfnam = full lockfile pathname, e.g. "/usr/spool/uucp/LCK..cul0p0".
  5249.   tmpnam = unique temporary filname, e.g. "/usr/spool/uucp/LTMP..pid".
  5250. */
  5251.     ckmakmsg(flfnam,FLFNAML,lockdir,"/",lockfil,NULL); /* SET LINE device */
  5252.  
  5253.     /* If dialout device, also make one for corresponding dialin device */
  5254.     lock2[0] = '\0';
  5255.     if (!strncmp(device,"cu",2) && *unit && ttydexists)
  5256.       ckmakmsg(lock2,FLFNAML,lockdir,"/LCK..ttyd",unit,NULL);
  5257.  
  5258.     if ((int)strlen(lockdir)+12 < LFNAML)
  5259.       sprintf(tmpnam,"%s/LTMP.%05d",lockdir,(int) pid); /* Make temp name */
  5260. #ifdef DEBUG
  5261.     if (deblog) {
  5262.     debug(F110,"TTLOCK flfnam",flfnam,0);
  5263.     debug(F110,"TTLOCK lock2",lock2,0);
  5264.     debug(F110,"TTLOCK tmpnam",tmpnam,0);
  5265.     }
  5266. #endif /* DEBUG */
  5267. /*
  5268.    Lockfile permissions...
  5269.    444 is standard, HP-UX 10.00 uses 664.  It doesn't matter.
  5270.    Kermit uses 444; the difference lets us tell whether Kermit created
  5271.    the lock file.
  5272. */
  5273.     priv_on();                /* Turn on privileges. */
  5274.     lockfd = creat(tmpnam, 0444);    /* Try to create temporary file. */
  5275.     if (lockfd < 0) {            /* Create failed. */
  5276.     debug(F111,"TTLOCK creat failed",tmpnam,errno);
  5277.     if (errno == ENOENT) {
  5278.         perror(lockdir);
  5279.         printf("UUCP not installed or Kermit misconfigured\n");
  5280.     } else {
  5281.         if (!quiet)
  5282.           perror(lockdir);
  5283.         unlink(tmpnam);        /* Get rid of the temporary file. */
  5284.     }
  5285.     priv_off();            /* Turn off privileges!!! */
  5286.     debug(F101,"TTLOCK returns","",-1);
  5287.     return(-1);            /* Return failure code. */
  5288.     }
  5289.     debug(F110,"TTLOCK temp ok",tmpnam,0);
  5290.  
  5291. /* Now write our pid into the temp lockfile in integer format. */
  5292.  
  5293.     i = write(lockfd, (char *)&pid, sizeof(pid));
  5294.  
  5295. #ifdef DEBUG
  5296.     if (deblog) {
  5297.     debug(F101,"TTLOCK pid","",pid);
  5298.     debug(F101,"TTLOCK sizeof pid","",sizeof(pid));
  5299.     debug(F101,"TTLOCK write pid returns","",i);
  5300.     }
  5301. #endif /* DEBUG */
  5302.  
  5303. /*
  5304.   Now try to rename the temporary file to the real lockfile name.
  5305.   This will fail if a lock file of that name already exists, which
  5306.   will catch race conditions with other users.
  5307. */
  5308.     close(lockfd);            /* Close the temp lockfile. */
  5309.     chmod(tmpnam,0444);
  5310.  
  5311.     tries = 0;
  5312.     while (!haslock && tries++ < 2) {
  5313.     haslock = (link(tmpnam,flfnam) == 0); /* Create a link to it. */
  5314.     debug(F101,"TTLOCK link","",haslock);
  5315.     if (haslock) {            /* If we made the lockfile... */
  5316.  
  5317. #ifdef COMMENT
  5318. /* We can't do this any more because we don't have a file descriptor yet. */
  5319. #ifdef LOCKF                /* Can be canceled with -DNOLOCKF */
  5320. /*
  5321.   Create an advisory lock on the device through its file descriptor.
  5322.   This code actually seems to work.  If it is executed, and then another
  5323.   process tries to open the same device under a different name to circumvent
  5324.   the lockfile, they get a "device busy" error.
  5325. */
  5326.         debug(F100,"TTLOCK LOCKF code...","",0);
  5327.             while ( lockf(ttyfd, F_TLOCK, 0L) != 0 ) {
  5328.                 debug(F111, "TTLOCK lockf error", "", errno);
  5329.                 if ((++tries >= 3) || (errno != EAGAIN)) {
  5330.                     x = unlink(flfnam); /* Remove the lockfile */
  5331.             if (errno == EACCES && !quiet)
  5332.               printf("Device already locked by another process\n");
  5333.                     haslock = 0;
  5334.             break;
  5335.         }
  5336.                 sleep(2);
  5337.         }
  5338. #endif /* LOCKF */
  5339. #endif /* COMMENT */
  5340.  
  5341.         if (haslock) {        /* If we made the lockfile ... */
  5342.         if (lock2[0]) {        /* if there is to be a 2nd lockfile */
  5343.             lockfd = creat(lock2, 0444); /* Create it */
  5344.             debug(F111,"TTLOCK lock2 creat", lock2, lockfd);
  5345.             if (lockfd > -1) {    /* Created OK, write pid. */
  5346.             write(lockfd, (char *)&pid, sizeof(pid) );
  5347.             close(lockfd);    /* Close and */
  5348.             chmod(lock2, 0444); /* set permissions. */
  5349.             } else {         /* Not OK, but don't fail. */
  5350.             lock2[0] = '\0'; /* Just remember it's not there. */
  5351.             }
  5352.         }
  5353.         break;            /* and we're done. */
  5354.         }
  5355.     }
  5356.     }
  5357.     unlink(tmpnam);            /* Unlink (remove) the temp file. */
  5358.     priv_off();                /* Turn off privs */
  5359.     i = haslock ? 0 : -1;        /* Our return value */
  5360.     debug(F101,"TTLOCK returns","",i);
  5361.     return(i);
  5362. #endif /* HPUX */
  5363. #endif /* USETTYLOCK */
  5364. #endif /* !NOUUCP */
  5365. }
  5366.  
  5367. /*  T T U N L O C K  */
  5368.  
  5369. static int
  5370. ttunlck() {                             /* Remove UUCP lockfile(s). */
  5371. #ifndef NOUUCP
  5372.     int x;
  5373.  
  5374.     debug(F111,"ttunlck",flfnam,haslock);
  5375.  
  5376. #ifdef USETTYLOCK
  5377.  
  5378.     if (haslock && *flfnam) {
  5379.     int x;
  5380.     priv_on();            /* Turn on privs */
  5381. #ifdef USE_UU_LOCK
  5382.     x = uu_unlock(lockname);
  5383. #else  /* USE_UU_LOCK */
  5384.     x = ttyunlock(lockname);    /* Try to unlock */
  5385. #endif /* USE_UU_LOCK */
  5386.     priv_off();            /* Turn off privs */
  5387.     if (x < 0 && !quiet)
  5388.       printf("Warning - Can't remove lockfile: %s\n", flfnam);
  5389.  
  5390.     *flfnam = '\0';            /* Erase the name. */
  5391.     haslock = 0;
  5392.     return(0);
  5393.     }
  5394.  
  5395. #else  /* No ttylock()... */
  5396.  
  5397.     if (haslock && *flfnam) {
  5398.     /* Don't remove lockfile if we didn't make it ourselves */
  5399.     if ((x = ttrpid(flfnam)) != (int)getpid()) {
  5400.         debug(F111,"ttunlck lockfile seized",flfnam,x);
  5401.         printf("Warning - Lockfile %s seized by pid %d\n",
  5402.            flfnam,
  5403.            x
  5404.            );
  5405.         return(0);
  5406.     }
  5407.     priv_on();            /* Turn privileges on.  */
  5408.     errno = 0;
  5409.     x = unlink(flfnam);        /* Remove the lockfile. */
  5410.     debug(F111,"ttunlck unlink",flfnam,x);
  5411.     if (x < 0) {
  5412.         if (errno && !quiet)
  5413.           perror(ttnmsv);
  5414.         printf("Warning - Can't remove lockfile: %s\n", flfnam);
  5415.     }
  5416.     haslock = 0;
  5417.     *flfnam = '\0';            /* Erase the name. */
  5418.  
  5419. #ifdef RTAIX
  5420.     errno = 0;
  5421.     x = unlink(lkflfn);        /* Remove link to lockfile */
  5422.     debug(F111,"ttunlck AIX link unlink",lkflfn,x);
  5423.     if (x < 0) {
  5424.         if (errno && !quiet)
  5425.           perror(ttnmsv);
  5426.         printf("Warning - Can't remove link to lockfile: %s\n", lkflfn);
  5427.     }
  5428.     *lkflfn = '\0';
  5429. #else
  5430.     if (lock2[0]) {            /* If there is a second lockfile, */
  5431.         errno = 0;
  5432.         x = unlink(lock2);        /*  remove it too. */
  5433.         debug(F111,"ttunlck lock2 unlink",lock2,x);
  5434.         if (x < 0) {
  5435.         if (errno && !quiet)
  5436.           perror(ttnmsv);
  5437.         printf("Warning - Can't remove secondary lockfile: %s\n",
  5438.                lock2
  5439.                );
  5440.         }
  5441.         lock2[0] = '\0';        /* Forget its name. */
  5442.     }
  5443. #endif /* RTAIX */
  5444.  
  5445. #ifdef COMMENT
  5446. #ifdef LOCKF
  5447.         (VOID) lockf(ttyfd, F_ULOCK, 0L); /* Remove advisory lock */
  5448. #endif /* LOCKF */
  5449. #endif /* COMMENT */
  5450.  
  5451.     priv_off();            /* Turn privileges off. */
  5452.     }
  5453. #endif /* USETTYLOCK */
  5454. #endif /* !NOUUCP */
  5455.     return(0);
  5456. }
  5457.  
  5458. /*
  5459.   4.3BSD-style UUCP line direction control.
  5460.   (Stan Barber, Rice U, 1980-something...)
  5461. */
  5462. #ifndef NOUUCP
  5463. #ifdef ACUCNTRL
  5464. VOID
  5465. acucntrl(flag,ttname) char *flag, *ttname; {
  5466.     char x[DEVNAMLEN+32], *device, *devname;
  5467.  
  5468.     if (strcmp(ttname,CTTNAM) == 0 || xlocal == 0) /* If not local, */
  5469.       return;                /* just return. */
  5470.     device = ((devname = xxlast(ttname,'/')) != NULL ? devname+1 : ttname);
  5471.     if (strncmp(device,"LCK..",4) == 0) device += 5;
  5472.     ckmakmsg(x,DEVNAMLEN+32,"/usr/lib/uucp/acucntrl ",flag," ",device);
  5473.     debug(F110,"called ",x,0);
  5474.     zsyscmd(x);
  5475. }
  5476. #endif /* ACUCNTRL */
  5477. #endif /* NOUUCP */
  5478.  
  5479. /*
  5480.   T T H F L O W  --  Set or Reset hardware flow control.
  5481.  
  5482.   This is an attempt to collect all hardware-flow-control related code
  5483.   into a single module.  Thanks to Rick Sladkey and John Kohl for lots of
  5484.   help here.  Overview:
  5485.  
  5486.   Hardware flow control is not supported in many UNIX implementions.  Even
  5487.   when it is supported, there is no (ha ha) "standard" for the programming
  5488.   interface.  In general, 4.3BSD and earlier (sometimes), 4.4BSD, System V,
  5489.   SunOS, AIX, etc, have totally different methods.  (And, not strictly
  5490.   relevant here, the programming interface often brings one only to a no-op
  5491.   in the device driver!)
  5492.  
  5493.   Among all these, we have two major types of APIs: those in which hardware
  5494.   flow control is determined by bits in the same termio/termios/sgtty mode
  5495.   word(s) that are used for controlling such items as CBREAK vs RAW mode, and
  5496.   which are also used by the ttvt(), ttpkt(), conbin(), and concb() routines
  5497.   for changing terminal modes.  And those that use entirely different
  5498.   mechanisms.
  5499.  
  5500.   In the first category, it is important that any change in the mode bits be
  5501.   reflected in the relevant termio(s)/sgtty structure, so that subsequent
  5502.   changes to that structure do not wipe out the effects of this routine.  That
  5503.   is why a pointer, attrs, to the appropriate structure is passed as a
  5504.   parameter to this routine.
  5505.  
  5506.   The second category should give us no worries, since any changes to hardware
  5507.   flow control accomplished by this routine should not affect the termio(s)/
  5508.   sgtty structures, and therefore will not be undone by later changes to them.
  5509.  
  5510.   The second argument, status, means to turn on hardware flow control if
  5511.   nonzero, and to turn it off if zero.
  5512.  
  5513.   Returns: 0 on apparent success, -1 on probable failure.
  5514. */
  5515.  
  5516. /*
  5517.   The following business is for BSDI, where it was discovered that two
  5518.   separate bits, CCTS_OFLOW and CRTS_IFLOW, are used in hardware flow control,
  5519.   but CTRSCTS is defined (in <termios.h>) to be just CCTS_OFLOW rather both
  5520.   bits, so hwfc only works in one direction if you use CRTSCTS to control it.
  5521.   Other 4.4BSD-based Unixes such as FreeBSD 4.1, which use these two bits,
  5522.   define CRTSCTS correctly.
  5523. */
  5524. #ifdef FIXCRTSCTS
  5525. #ifdef CRTSCTS
  5526. #ifdef CCTS_OFLOW
  5527. #ifdef CRTS_IFLOW
  5528. #undef CRTSCTS
  5529. #define CRTSCTS (CRTS_IFLOW|CCTS_OFLOW)
  5530. #endif /* CRTS_IFLOW */
  5531. #endif /* CCTS_OFLOW */
  5532. #endif /* CRTSCTS */
  5533. #endif /* FIXCRTSCTS */
  5534.  
  5535. static int
  5536. tthflow(flow, status, attrs)
  5537.     int flow,                /* Type of flow control (ckcdeb.h) */
  5538.     status;                /* Nonzero = turn it on */
  5539.                     /* Zero = turn it off */
  5540. #ifdef BSD44ORPOSIX            /* POSIX or BSD44 */
  5541.     struct termios *attrs;
  5542. #else                    /* System V */
  5543. #ifdef ATTSV
  5544. #ifdef ATT7300
  5545. #ifdef UNIX351M
  5546. /* AT&T UNIX 3.51m can set but not test for hardware flow control */
  5547. #define RTSFLOW CTSCD
  5548. #define CTSFLOW CTSCD
  5549. #endif /* ATT7300 */
  5550. #endif /* UNIX351M */
  5551.     struct termio *attrs;
  5552. #else                    /* BSD, V7, etc */
  5553.     struct sgttyb *attrs;        /* sgtty info... */
  5554. #endif /* ATTSV */
  5555. #endif /* BSD44ORPOSIX */
  5556. /* tthflow */ {
  5557.  
  5558.     int x = 0;                /* tthflow() return code */
  5559.  
  5560. #ifdef Plan9
  5561.     return p9tthflow(flow, status);
  5562. #else
  5563.  
  5564. #ifndef OXOS                /* NOT Olivetti X/OS... */
  5565. /*
  5566.   For SunOS 4.0 and later in the BSD environment ...
  5567.  
  5568.   The declarations are copied and interpreted from the System V header files,
  5569.   so we don't actually have to pull in all the System V junk when building
  5570.   C-Kermit for SunOS in the BSD environment, which would be dangerous because
  5571.   having those symbols defined would cause us to take the wrong paths through
  5572.   the code.  The code in this section is used in both the BSD and Sys V SunOS
  5573.   versions.
  5574. */
  5575. #ifdef SUNOS41
  5576. /*
  5577.   In SunOS 4.1 and later, we use the POSIX calls rather than ioctl calls
  5578.   because GNU CC uses different formats for the _IOxxx macros than regular CC;
  5579.   the POSIX forms work for both.  But the POSIX calls are not available in
  5580.   SunOS 4.0.
  5581. */
  5582. #define CRTSCTS 0x80000000        /* RTS/CTS flow control */
  5583. #define TCSANOW 0            /* Do it now */
  5584.  
  5585.     struct termios {
  5586.     unsigned long c_iflag;        /* Input modes */
  5587.     unsigned long c_oflag;        /* Output modes */
  5588.     unsigned long c_cflag;        /* Control modes */
  5589.     unsigned long c_lflag;        /* Line discipline modes */
  5590.     char c_line;
  5591.     CHAR c_cc[17];
  5592.     };
  5593.     struct termios temp;
  5594.  
  5595. _PROTOTYP( int tcgetattr, (int, struct termios *) );
  5596. _PROTOTYP( int tcsetattr, (int, int, struct termios *) );
  5597. /*
  5598.   When CRTSCTS is set, SunOS won't do output unless both CTS and CD are
  5599.   asserted.  So we don't set CRTSCTS unless CD is up.  This should be OK,
  5600.   since we don't need RTS/CTS during dialing, and after dialing is complete,
  5601.   we should have CD.  If not, we still communicate, but without RTS/CTS.
  5602. */
  5603.     int mflags;                /* Modem signal flags */
  5604.  
  5605. #ifdef NETCMD
  5606.     if (ttpipe) return(0);
  5607. #endif /* NETCMD */
  5608. #ifdef NETPTY
  5609.     if (ttpty) return(0);
  5610. #endif /* NETPTY */
  5611.  
  5612.     debug(F101,"tthflow SUNOS41 entry status","",status);
  5613.     if (!status) {            /* Turn hard flow off */
  5614.     if (tcgetattr(ttyfd, &temp) > -1 && /* Get device attributes */
  5615.         (temp.c_cflag & CRTSCTS)) { /* Check for RTS/CTS */
  5616.         temp.c_cflag &= ~CRTSCTS;    /* It's there, remove it */
  5617.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5618.     }
  5619.     } else {                /* Turn hard flow on */
  5620.     if (ioctl(ttyfd,TIOCMGET,&mflags) > -1 && /* Get modem signals */
  5621.         (mflags & TIOCM_CAR)) {        /* Check for CD */
  5622.         debug(F100,"tthflow SunOS has CD","",0);
  5623.         if (tcgetattr(ttyfd, &temp) > -1 && /* Get device attributes */
  5624.         !(temp.c_cflag & CRTSCTS)) { /* Check for RTS/CTS */
  5625.         temp.c_cflag |= CRTSCTS;    /* Not there, add it */
  5626.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5627.         }
  5628.     } else {
  5629.         x = -1;
  5630.         debug(F100,"tthflow SunOS no CD","",0);
  5631.     }
  5632.     }
  5633. #else
  5634. #ifdef QNX
  5635.     struct termios temp;
  5636. #ifdef NETCMD
  5637.     if (ttpipe) return(0);
  5638. #endif /* NETCMD */
  5639. #ifdef NETPTY
  5640.     if (ttpty) return(0);
  5641. #endif /* NETPTY */
  5642.     debug(F101,"tthflow QNX entry status","",status);
  5643.     if (tcgetattr(ttyfd, &temp) > -1) {    /* Get device attributes */
  5644.     if (!status) {            /* Turn hard flow off */
  5645.         if ((temp.c_cflag & (IHFLOW|OHFLOW)) == (IHFLOW|OHFLOW)) {
  5646.         temp.c_cflag &= ~(IHFLOW|OHFLOW); /* It's there, remove it */
  5647.         attrs->c_cflag &= ~(IHFLOW|OHFLOW);
  5648.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5649.         }
  5650.     } else {            /* Turn hard flow on */
  5651.         if ((temp.c_cflag & (IHFLOW|OHFLOW)) != (IHFLOW|OHFLOW)) {
  5652.         temp.c_cflag |= (IHFLOW|OHFLOW); /* Not there, add it */
  5653.         temp.c_iflag &= ~(IXON|IXOFF);   /* Bye to IXON/IXOFF */
  5654.         ttraw.c_lflag |= IEXTEN;         /* Must be on */
  5655.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5656.         attrs->c_cflag |= (IHFLOW|OHFLOW);
  5657.         attrs->c_iflag &= ~(IXON|IXOFF);
  5658.         }
  5659.     }
  5660.     } else {
  5661.     x = -1;
  5662.     debug(F100, "tthflow QNX getattr fails", "", 0);
  5663.     }
  5664. #else
  5665. #ifdef POSIX_CRTSCTS
  5666. /*
  5667.   POSIX_CRTSCTS is defined in ckcdeb.h or on CC command line.
  5668.   Note: Do not assume CRTSCTS is a one-bit field!
  5669. */
  5670.     struct termios temp;
  5671. #ifdef NETCMD
  5672.     if (ttpipe) return(0);
  5673. #endif /* NETCMD */
  5674. #ifdef NETPTY
  5675.     if (ttpty) return(0);
  5676. #endif /* NETPTY */
  5677.     debug(F101,"tthflow POSIX_CRTSCTS entry status","",status);
  5678.     errno = 0;
  5679.     x = tcgetattr(ttyfd, &temp);
  5680.     debug(F111,"tthflow POSIX_CRTSCTS tcgetattr",ckitoa(x),errno);
  5681.     errno = 0;
  5682.     if (x < 0) {
  5683.     x = -1;
  5684.     } else {
  5685.     if (!status) {            /* Turn hard flow off */
  5686.         if (
  5687. #ifdef COMMENT
  5688.         /* This can fail because of sign extension */
  5689.         /* e.g. in Linux where it's Bit 31 */
  5690.         (temp.c_cflag & CRTSCTS) == CRTSCTS
  5691. #else
  5692.         (temp.c_cflag & CRTSCTS) != 0
  5693. #endif /* COMMENT */
  5694.         ) {
  5695.         temp.c_cflag &= ~CRTSCTS; /* It's there, remove it */
  5696.         attrs->c_cflag &= ~CRTSCTS;
  5697.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5698.         debug(F111,"tthflow POSIX_CRTSCTS OFF tcsetattr",
  5699.               ckitoa(x),errno);
  5700.         }
  5701.     } else {            /* Turn hard flow on */
  5702.         if (
  5703. #ifdef COMMENT
  5704.         /* This can fail because of sign extension */
  5705.         (temp.c_cflag & CRTSCTS) != CRTSCTS
  5706. #else
  5707.         (temp.c_cflag & CRTSCTS) == 0
  5708. #endif /* COMMENT */
  5709.         ) {
  5710.         temp.c_cflag |= CRTSCTS; /* Not there, add it */
  5711.         temp.c_iflag &= ~(IXON|IXOFF|IXANY); /* Bye to IXON/IXOFF */
  5712.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5713.         debug(F111,"tthflow POSIX_CRTSCTS ON tcsetattr",
  5714.               ckitoa(x),errno);
  5715.         attrs->c_cflag |= CRTSCTS;
  5716.         attrs->c_iflag &= ~(IXON|IXOFF|IXANY);
  5717.         }
  5718.     }
  5719.     }
  5720. #else
  5721. #ifdef SUNOS4
  5722. /*
  5723.   SunOS 4.0 (and maybe earlier?).  This code is dangerous because it
  5724.   prevents compilation with GNU gcc, which uses different formats for the
  5725.   _IORxxx macros than regular cc.  SunOS 4.1 and later can use the POSIX
  5726.   routines above, which work for both cc and gcc.
  5727. */
  5728. #define TCGETS _IOR(T, 8, struct termios) /* Get modes into termios struct */
  5729. #define TCSETS _IOW(T, 9, struct termios) /* Set modes from termios struct */
  5730. #define CRTSCTS 0x80000000          /* RTS/CTS flow control */
  5731.  
  5732.     struct termios {
  5733.     unsigned long c_iflag;        /* Input modes */
  5734.     unsigned long c_oflag;        /* Output modes */
  5735.     unsigned long c_cflag;        /* Control modes */
  5736.     unsigned long c_lflag;        /* Line discipline modes */
  5737.     char c_line;
  5738.     CHAR c_cc[17];
  5739.     };
  5740.     struct termios temp;
  5741. #ifdef NETCMD
  5742.     if (ttpipe) return(0);
  5743. #endif /* NETCMD */
  5744. #ifdef NETPTY
  5745.     if (ttpty) return(0);
  5746. #endif /* NETPTY */
  5747.     debug(F101,"tthflow entry status","",status);
  5748.     if (ioctl(ttyfd,TCGETS,&temp) > -1) { /* Get terminal modes. */
  5749.     if (status) {            /* Turn hard flow on */
  5750.         temp.c_cflag |= CRTSCTS;    /* Add RTS/CTS to them. */
  5751.         x = ioctl(ttyfd,TCSETS,&temp); /* Set them again. */
  5752.         attrs->c_cflag |= CRTSCTS;    /* Add to global info. */
  5753.     } else {            /* Turn hard flow off */
  5754.         temp.c_cflag &= ~CRTSCTS;
  5755.         x = ioctl(ttyfd,TCSETS,&temp);
  5756.         attrs->c_cflag &= ~CRTSCTS;
  5757.     }
  5758.     }
  5759. #else                    /* Not SunOS 4.0 or later */
  5760. #ifdef AIXRS                /* IBM AIX RS/6000 */
  5761. #ifndef AIX41                /* But only pre-4.x == SVR4 */
  5762. #ifdef NETCMD
  5763.     if (ttpipe) return(0);
  5764. #endif /* NETCMD */
  5765. #ifdef NETPTY
  5766.     if (ttpty) return(0);
  5767. #endif /* NETPTY */
  5768.     if (status) {
  5769.     if ((x = ioctl(ttyfd, TXADDCD, "rts")) < 0 && errno != EBUSY)
  5770.       debug(F100,"hardflow TXADDCD (rts) error", "", 0);
  5771.     } else {
  5772.     if ((x = ioctl(ttyfd, TXDELCD, "rts")) < 0 && errno != EINVAL)
  5773.       debug(F100,"hardflow TXDELCD (rts) error", "", 0);
  5774.     }
  5775. #endif /* AIX41 */
  5776. #else                    /* Not AIX RS/6000 */
  5777.  
  5778. #ifdef ATTSV                /* System V... */
  5779.  
  5780. #ifdef CK_SCOV5                /* SCO Open Server 5.0 */
  5781. #define CK_SCOUNIX
  5782. #else
  5783. #ifdef M_UNIX                /* SCO UNIX 3.2v4.x or earlier */
  5784. #define CK_SCOUNIX
  5785. #endif /* M_UNIX */
  5786. #endif /* CK_SCOV5 */
  5787.  
  5788. #ifdef SCO_FORCE_RTSXOFF
  5789. #ifdef CK_SCOUNIX            /* But not SCO OpenServer 5.0.4 */
  5790. #ifdef SCO_OSR504            /* or later... */
  5791. #undef CK_SCOUNIX
  5792. #endif /* SCO_OSR504 */
  5793. #endif /* CK_SCOUNIX */
  5794. #endif /* SCO_FORCE_RTSXOFF */
  5795.  
  5796. #ifdef CK_SCOUNIX
  5797. #ifdef POSIX
  5798.     struct termios temp;
  5799. #ifdef NETCMD
  5800.     if (ttpipe) return(0);
  5801. #endif /* NETCMD */
  5802. #ifdef NETPTY
  5803.     if (ttpty) return(0);
  5804. #endif /* NETPTY */
  5805.     debug(F101,"tthflow SCOUNIX POSIX entry status","",status);
  5806.     errno = 0;
  5807.     x = tcgetattr(ttyfd, &temp);
  5808.     debug(F111,"tthflow SCO UNIX POSIX tcgetattr",ckitoa(x),errno);
  5809. #else /* POSIX */
  5810.     struct termio temp;
  5811. #ifdef NETCMD
  5812.     if (ttpipe) return(0);
  5813. #endif /* NETCMD */
  5814. #ifdef NETPTY
  5815.     if (ttpty) return(0);
  5816. #endif /* NETPTY */
  5817.     debug(F101,"tthflow SCOUNIX non-POSIX entry status","",status);
  5818.     x = ioctl(ttyfd, TCGETA, &temp);
  5819.     debug(F111,"tthflow SCO UNIX non-POSIX TCGETA",ckitoa(x),errno);
  5820. #endif /* POSIX */
  5821. /*
  5822.   This is not really POSIX, since POSIX does not deal with hardware flow
  5823.   control, but we are using the POSIX APIs.  In fact, RTSFLOW and CTSFLOW
  5824.   are defined in termio.h, but within #ifndef _POSIX_SOURCE..#endif.  So
  5825.   let's try forcing their definitions here.
  5826. */
  5827. #ifndef CTSFLOW
  5828. #define CTSFLOW 0020000
  5829.     debug(F101,"tthflow SCO defining CTSFLOW","",CTSFLOW);
  5830. #else
  5831.     debug(F101,"tthflow SCO CTSFLOW","",CTSFLOW);
  5832. #endif /* CTSFLOW */
  5833. #ifndef RTSFLOW
  5834. #define RTSFLOW 0040000
  5835.     debug(F101,"tthflow SCO defining RTSFLOW","",RTSFLOW);
  5836. #else
  5837.     debug(F101,"tthflow SCO RTSFLOW","",RTSFLOW);
  5838. #endif /* RTSFLOW */
  5839. #ifndef ORTSFL
  5840. #define ORTSFL 0100000
  5841.     debug(F101,"tthflow SCO defining ORTSFL","",ORTSFL);
  5842. #else
  5843.     debug(F101,"tthflow SCO ORTSFL","",ORTSFL);
  5844. #endif /* ORTSFL */
  5845.  
  5846.     if (x != -1) {
  5847.     if (status) {            /* Turn it ON */
  5848.         temp.c_cflag |= RTSFLOW|CTSFLOW;
  5849.         attrs->c_cflag |= RTSFLOW|CTSFLOW;
  5850. #ifdef ORTSFL
  5851.         temp.c_cflag &= ~ORTSFL;
  5852.         attrs->c_cflag &= ~ORTSFL;
  5853. #endif /* ORTSFL */
  5854.         temp.c_iflag &= ~(IXON|IXOFF|IXANY);
  5855.         attrs->c_iflag &= ~(IXON|IXOFF|IXANY);
  5856.     } else {            /* Turn it OFF */
  5857. #ifdef ORTSFL
  5858.         temp.c_cflag &= ~(RTSFLOW|CTSFLOW|ORTSFL);
  5859.         attrs->c_cflag &= ~(RTSFLOW|CTSFLOW|ORTSFL);
  5860. #else  /* ORTSFL */
  5861.         temp.c_cflag &= ~(RTSFLOW|CTSFLOW);
  5862.         attrs->c_cflag &= ~(RTSFLOW|CTSFLOW);
  5863. #endif /* ORTSFL */
  5864.     }
  5865. #ifdef POSIX
  5866.     x = tcsetattr(ttyfd, TCSADRAIN, &temp);
  5867. #else
  5868.     x = ioctl(ttyfd, TCSETA, &temp);
  5869. #endif /* POSIX */
  5870.     debug(F101,"tthflow SCO set modes","",x);
  5871.     }
  5872. #else /* Not SCO UNIX */
  5873. #ifdef NETCMD
  5874.     if (ttpipe) return(0);
  5875. #endif /* NETCMD */
  5876. #ifdef NETPTY
  5877.     if (ttpty) return(0);
  5878. #endif /* NETPTY */
  5879.     if (!status) {            /* Turn it OFF */
  5880. #ifdef RTSXOFF
  5881.     debug(F100,"tthflow ATTSV RTS/CTS OFF","",0);
  5882.     rctsx.x_hflag &= ~(RTSXOFF|CTSXON);
  5883. #ifdef TCSETX
  5884.     x = ioctl(ttyfd,TCSETX,&rctsx);
  5885.     debug(F101,"tthflow ATTSV TCSETX OFF","",x);
  5886. #else
  5887.     x = -1
  5888.     debug(F100,"tthflow TCSETX not defined","",0);
  5889. #endif /* TCSETX */
  5890. #else
  5891.     debug(F100,"tthflow ATTSV RTSXOFF not defined","",0);
  5892. #endif /* RTSXOFF */
  5893. #ifdef DTRXOFF
  5894.     debug(F100,"tthflow ATTSV DTR/CD OFF","",0);
  5895.     rctsx.x_hflag &= ~(DTRXOFF|CDXON);
  5896.     x = ioctl(ttyfd,TCSETX,&rctsx);
  5897.     debug(F101,"tthflow ATTSV DTRXOFF OFF","",x);
  5898. #else
  5899.     debug(F100,"tthflow ATTSV DTRXOFF not defined","",0);
  5900. #endif /* DTRXOFF */
  5901.     } else {                /* Turn it ON. */
  5902.     if (flow == FLO_RTSC) {    /* RTS/CTS Flow control... */
  5903.         debug(F100,"tthflow ATTSV RTS/CTS ON","",0);
  5904. #ifdef RTSXOFF
  5905.         /* This is the preferred way, according to SVID3 */
  5906. #ifdef TCGETX
  5907.         x = ioctl(ttyfd,TCGETX,&rctsx);
  5908.         debug(F101,"tthflow TCGETX","",x);
  5909.         if (x > -1) {
  5910.         rctsx.x_hflag |= RTSXOFF | CTSXON;
  5911.         x = ioctl(ttyfd,TCSETX,&rctsx);
  5912.         debug(F100,"tthflow ATTSV ioctl","",x);
  5913.         }
  5914. #else
  5915.         debug(F100,"tthflow TCGETX not defined","",0);
  5916.         x = -1
  5917. #endif /* TCGETX */
  5918. #else
  5919.         debug(F100,"tthflow RTSXOFF not defined","",0);
  5920.         x = -1;
  5921. #endif /* RTSXOFF */
  5922.     } else if (flow == FLO_DTRC) {    /* DTR/CD Flow control... */
  5923.         debug(F100,"tthflow ATTSV DTR/CD ON","",0);
  5924. #ifdef DTRXOFF
  5925.         /* This is straight out of SVID R4 */
  5926.         if (ioctl(ttyfd,TCGETX,&rctsx) > -1) {
  5927.         rctsx.x_hflag &= ~(DTRXOFF|CDXON);
  5928.         x = ioctl(ttyfd,TCSETX,&rctsx);
  5929.         }
  5930. #else
  5931.         debug(F100,"tthflow ATTSV DTRXOFF not defined","",0);
  5932.         x = -1;
  5933. #endif /* DTRXOFF */
  5934.     }
  5935.     }
  5936. #endif /* CK_SCOUNIX */
  5937.  
  5938. #else /* not System V... */
  5939.  
  5940. #ifdef CK_DTRCTS
  5941. #ifdef LDODTR
  5942. #ifdef LDOCTS
  5943. #ifdef NETCMD
  5944.     if (ttpipe) return(0);
  5945. #endif /* NETCMD */
  5946. #ifdef NETPTY
  5947.     if (ttpty) return(0);
  5948. #endif /* NETPTY */
  5949.     x = LDODTR | LDOCTS;        /* Found only on UTEK? */
  5950.     if (flow == FLO_DTRT && status) {    /* Use hardware flow control */
  5951.     if (lmodef) {
  5952.         x = ioctl(ttyfd,TIOCLBIS,&x);
  5953.         if (x < 0) {
  5954.             debug(F100,"hardflow TIOCLBIS error","",0);
  5955.         } else {
  5956.         lmodef++;
  5957.         debug(F100,"hardflow TIOCLBIS ok","",0);
  5958.         }
  5959.     }
  5960.     } else {
  5961.     if (lmodef) {
  5962.         x = ioctl(ttyfd,TIOCLBIC,&x);
  5963.         if (x < 0) {
  5964.             debug(F100,"hardflow TIOCLBIC error","",0);
  5965.         } else {
  5966.         lmodef++;
  5967.         debug(F100,"hardflow TIOCLBIC ok","",0);
  5968.         }
  5969.     }
  5970.     }
  5971. #endif /* LDODTR */
  5972. #endif /* LDOCTS */
  5973. #endif /* CK_DTRCTS */
  5974. #endif /* ATTSV */
  5975. #endif /* AIXRS */
  5976. #endif /* SUNOS4 */
  5977. #endif /* QNX */
  5978. #endif /* POSIX_CRTSCTS */
  5979. #endif /* SUNOS41 */
  5980.  
  5981. #else /* OXOS */
  5982.  
  5983.     struct termios temp;        /* Olivetti X/OS ... */
  5984.  
  5985. #ifdef NETCMD
  5986.     if (ttpipe) return(0);
  5987. #endif /* NETCMD */
  5988. #ifdef NETPTY
  5989.     if (ttpty) return(0);
  5990. #endif /* NETPTY */
  5991.     x = ioctl(ttyfd,TCGETS,&temp);
  5992.     if (x == 0) {
  5993.     temp.c_cflag &= ~(CRTSCTS|CDTRCTS|CBRKFLOW|CDTRDSR|CRTSDSR);
  5994.     if (status) {
  5995.         switch (flow) {
  5996.           case FLO_RTSC: temp.c_cflag |= CRTSCTS; /* RTS/CTS (hard) */
  5997.         break;
  5998.           case FLO_DTRT: temp.c_cflag |= CDTRCTS; /* DTR/CTS (hard) */
  5999.         break;
  6000.         }
  6001.     }
  6002.     x = ioctl(ttyfd,TCSETS,&temp);
  6003.     }
  6004. #endif /* OXOS */
  6005.     return(x);
  6006.  
  6007. #endif /* Plan9 */
  6008. }
  6009.  
  6010. /*  T T P K T  --  Condition the communication line for packets */
  6011. /*                 or for modem dialing */
  6012.  
  6013. /*
  6014.   If called with speed > -1, also set the speed.
  6015.   Returns 0 on success, -1 on failure.
  6016.  
  6017.   NOTE: the "xflow" parameter is supposed to be the currently selected
  6018.   type of flow control, but for historical reasons, this parameter is also
  6019.   used to indicate that we are dialing.  Therefore, when the true flow
  6020.   control setting is needed, we access the external variable "flow", rather
  6021.   than trusting our "xflow" argument.
  6022. */
  6023. int
  6024. #ifdef CK_ANSIC
  6025. ttpkt(long speed, int xflow, int parity)
  6026. #else
  6027. ttpkt(speed,xflow,parity) long speed; int xflow, parity;
  6028. #endif /* CK_ANSIC */
  6029. /* ttpkt */ {
  6030. #ifndef NOLOCAL
  6031.     int s2;
  6032.     int s = -1;
  6033. #endif /* NOLOCAL */
  6034.     int x;
  6035.     extern int flow;            /* REAL flow-control setting */
  6036.  
  6037.     if (ttyfd < 0) return(-1);          /* Not open. */
  6038.  
  6039.     debug(F101,"ttpkt parity","",parity);
  6040.     debug(F101,"ttpkt xflow","",xflow);
  6041.     debug(F101,"ttpkt speed","",(int) speed);
  6042.  
  6043.     ttprty = parity;                    /* Let other tt functions see these. */
  6044.     ttspeed = speed;            /* Make global copy for this module */
  6045.     ttpmsk = ttprty ? 0177 : 0377;    /* Parity stripping mask */
  6046. #ifdef PARSENSE
  6047.     needpchk = ttprty ? 0 : 1;        /* Parity check needed? */
  6048. #else
  6049.     needpchk = 0;
  6050. #endif /* PARSENSE */
  6051.  
  6052.     debug(F101,"ttpkt ttpmsk","",ttpmsk);
  6053.     debug(F101,"ttpkt netconn","",netconn);
  6054.  
  6055. #ifdef NETCONN                /* No mode-changing for telnet */
  6056.     if (netconn) {
  6057. #ifdef TCPSOCKET
  6058. #ifdef TCP_NODELAY
  6059.         if (ttnet == NET_TCPB) {    /* But turn off Nagle */
  6060.             extern int tcp_nodelay;
  6061.             nodelay_sav = tcp_nodelay;
  6062.             no_delay(1);
  6063.         }
  6064. #endif /* TCP_NODELAY */
  6065. #ifdef TN_COMPORT
  6066.         if (istncomport()) {
  6067.             int rc = -1;
  6068.             if (tvtflg == 0 && speed == ttspeed && flow == ttflow
  6069.                  /* && ttcarr == curcarr */ ) {
  6070.                 debug(F100,"ttpkt modes already set, skipping...","",0);
  6071.                 return(0);        /* Already been called. */
  6072.             }
  6073.             if (flow != ttflow) {
  6074.                 if ((rc = tnsetflow(flow)) < 0)
  6075.           return(rc);
  6076.                 ttflow = flow;
  6077.             }
  6078.             if (speed != ttspeed) {
  6079.                 if (speed <= 0) 
  6080.           speed = tnc_get_baud();
  6081.                 else if ((rc = tnc_set_baud(speed)) < 0)
  6082.           return(rc);
  6083.                 ttspeed = speed;
  6084.             }
  6085.             tnc_set_datasize(8);
  6086.         tnc_set_stopsize(stopbits);
  6087.  
  6088. #ifdef HWPARITY
  6089.             if (hwparity) {
  6090.                 switch (hwparity) {
  6091.           case 'e':            /* Even */
  6092.                     debug(F100,"ttres 8 bits + even parity","",0);
  6093.                     tnc_set_parity(3);
  6094.                     break;
  6095.           case 'o':            /* Odd */
  6096.                     debug(F100,"ttres 8 bits + odd parity","",0);
  6097.                     tnc_set_parity(2);
  6098.                     break;
  6099.           case 'm':            /* Mark */
  6100.                     debug(F100,"ttres 8 bits + invalid parity: mark","",0);
  6101.                     tnc_set_parity(4);
  6102.                     break;
  6103.           case 's':            /* Space */
  6104.                     debug(F100,"ttres 8 bits + invalid parity: space","",0);
  6105.                     tnc_set_parity(5);
  6106.                     break;
  6107.                 }
  6108.             } else 
  6109. #endif /* HWPARITY */
  6110.         {
  6111.                 tnc_set_parity(0);              /* None */
  6112.             }
  6113.             tvtflg = 0;
  6114.             return(0);
  6115.         }
  6116. #endif /* TN_COMPORT */
  6117. #endif /* TCPSOCKET */
  6118.         tvtflg = 0;
  6119.         return(0);
  6120.     }
  6121. #endif /* NETCONN */
  6122. #ifdef NETCMD
  6123.     if (ttpipe) return(0);
  6124. #endif /* NETCMD */
  6125. #ifdef NETPTY
  6126.     if (ttpty) return(0);
  6127. #endif /* NETPTY */
  6128.  
  6129. #ifndef Plan9
  6130.     if (ttfdflg && !isatty(ttyfd)) return(0);
  6131. #endif /* Plan9 */
  6132.  
  6133. #ifdef COHERENT
  6134. #define SVORPOSIX
  6135. #endif /* COHERENT */
  6136.  
  6137. #ifndef SVORPOSIX            /* Berkeley, V7, etc. */
  6138. #ifdef LPASS8
  6139. /*
  6140.  For some reason, with BSD terminal drivers, you can't set FLOW to XON/XOFF
  6141.  after having previously set it to NONE without closing and reopening the
  6142.  device.  Unless there's something I overlooked below...
  6143. */
  6144.     if (ttflow == FLO_NONE && flow == FLO_XONX && xlocal == 0) {
  6145.     debug(F101,"ttpkt executing horrible flow kludge","",0);
  6146.     ttclos(0);            /* Close it */
  6147.     x = 0;
  6148.     ttopen(ttnmsv,&x,ttmdm,0);    /* Open it again */
  6149.     }
  6150. #endif /* LPASS8 */
  6151. #endif /* SVORPOSIX */
  6152.  
  6153. #ifdef COHERENT                /* This must be vestigial since we */
  6154. #undef SVORPOSIX            /* reverse it a few lines below... */
  6155. #endif /* COHERENT */
  6156.  
  6157.     if (xflow != FLO_DIAL && xflow != FLO_DIAX)
  6158.       ttflow = xflow;            /* Now make this available too. */
  6159.  
  6160. #ifndef NOLOCAL
  6161.     if (xlocal) {
  6162.     s2 = (int) (speed / 10L);    /* Convert bps to cps */
  6163.     debug(F101,"ttpkt calling ttsspd","",s2);
  6164.     s = ttsspd(s2);            /* Check and set the speed */
  6165.     debug(F101,"ttpkt ttsspd result","",s);
  6166.      carrctl(&ttraw, xflow != FLO_DIAL /* Carrier control */
  6167.         && (ttcarr == CAR_ON || (ttcarr == CAR_AUT && ttmdm != 0)));
  6168.     tvtflg = 0;            /* So ttvt() will work next time */
  6169.     }
  6170. #endif /* NOLOCAL */
  6171.  
  6172. #ifdef COHERENT
  6173. #define SVORPOSIX
  6174. #endif /* COHERENT */
  6175.  
  6176. #ifndef SVORPOSIX            /* BSD section */
  6177.     if (flow == FLO_RTSC ||        /* Hardware flow control */
  6178.     flow == FLO_DTRC ||
  6179.     flow == FLO_DTRT) {
  6180.     tthflow(flow, 1, &ttraw);
  6181.     debug(F100,"ttpkt hard flow, TANDEM off, RAW on","",0);
  6182.     ttraw.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6183.     ttraw.sg_flags |= RAW;        /* Enter raw mode */
  6184.     } else if (flow == FLO_NONE) {    /* No flow control */
  6185.     debug(F100,"ttpkt no flow, TANDEM off, RAW on","",0);
  6186.     ttraw.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6187.     tthflow(flow, 0, &ttraw);    /* Turn off any hardware f/c too */
  6188.     ttraw.sg_flags |= RAW;        /* Enter raw mode */
  6189.     } else if (flow == FLO_KEEP) {    /* Keep device's original setting */
  6190.     debug(F100,"ttpkt keeping original TANDEM","",0);
  6191.     ttraw.sg_flags &= ~TANDEM;
  6192.     ttraw.sg_flags |= (ttold.sg_flags & TANDEM);
  6193.     /* NOTE: We should also handle hardware flow control here! */
  6194.     }
  6195.  
  6196. /* SET FLOW XON/XOFF is in effect, or SET FLOW KEEP resulted in Xon/Xoff */
  6197.  
  6198.     if ((flow == FLO_XONX) || (ttraw.sg_flags & TANDEM)) {
  6199.     debug(F100,"ttpkt turning on TANDEM","",0);
  6200.     ttraw.sg_flags |= TANDEM;    /* So ask for it. */
  6201.  
  6202. #ifdef LPASS8                /* Can pass 8-bit data through? */
  6203. /* If the LPASS8 local mode is available, then flow control can always  */
  6204. /* be used, even if parity is none and we are transferring 8-bit data.  */
  6205. /* But we only need to do all this if Xon/Xoff is requested. */
  6206. /* BUT... this tends not to work through IP or LAT connections, terminal */
  6207. /* servers, telnet, rlogin, etc, so it is currently disabled. */
  6208.     x = LPASS8;            /* If LPASS8 defined, then */
  6209.     debug(F100,"ttpkt executing LPASS8 code","",0);
  6210.     if (lmodef) {            /* TIOCLBIS must be too. */
  6211.         x = ioctl(ttyfd,TIOCLBIS,&x); /* Try to set LPASS8. */
  6212.         if (x < 0) {
  6213.         debug(F100,"ttpkt TIOCLBIS error","",0);
  6214.         } else {
  6215.         lmodef++;
  6216.         debug(F100,"ttpkt TIOCLBIS ok","",0);
  6217.         }
  6218.     }
  6219. /*
  6220.  But if we use LPASS8 mode, we must explicitly turn off
  6221.  terminal interrupts of all kinds.
  6222. */
  6223. #ifdef TIOCGETC                /* Not rawmode, */
  6224.     if (tcharf && (xlocal == 0)) {    /* must turn off */
  6225.         tchnoi.t_intrc = -1;    /* interrupt character */
  6226.         tchnoi.t_quitc = -1;    /* and quit character. */
  6227.         tchnoi.t_startc = 17;    /* Make sure xon */
  6228.         tchnoi.t_stopc = 19;    /* and xoff not ignored. */
  6229. #ifndef NOBRKC
  6230.         tchnoi.t_eofc = -1;        /* eof character. */
  6231.         tchnoi.t_brkc = -1;        /* brk character. */
  6232. #endif /* NOBRKC */
  6233.         if (ioctl(ttyfd,TIOCSETC,&tchnoi) < 0) {
  6234.         debug(F100,"ttpkt TIOCSETC failed","",0);
  6235.         } else {
  6236.         tcharf = 1;
  6237.         debug(F100,"ttpkt TIOCSETC ok","",0);
  6238.         }
  6239. #ifdef COMMENT
  6240. /* only for paranoid debugging */
  6241.         if (tcharf) {
  6242.         struct tchars foo;
  6243.         char tchbuf[100];
  6244.         ioctl(0,TIOCGETC,&foo);
  6245.         sprintf(tchbuf,
  6246.             "intr=%d,quit=%d, start=%d, stop=%d, eof=%d, brk=%d",
  6247.             foo.t_intrc, foo.t_quitc, foo.t_startc,
  6248.             foo.t_stopc, foo.t_eofc,  foo.t_brkc);
  6249.         debug(F110,"ttpkt chars",tchbuf,0);
  6250.         }
  6251. #endif /* COMMENT */
  6252.     }
  6253.     ttraw.sg_flags |= CBREAK;    /* Needed for unknown reason */
  6254. #endif /* TIOCGETC */
  6255.  
  6256. /* Prevent suspend during packet mode */
  6257. #ifdef TIOCGLTC                /* Not rawmode, */
  6258.     if (ltcharf && (xlocal == 0)) {    /* must turn off */
  6259.         ltchnoi.t_suspc = -1;    /* suspend character */
  6260.         ltchnoi.t_dsuspc = -1;    /* and delayed suspend character */
  6261.         if (ioctl(ttyfd,TIOCSLTC,&tchnoi) < 0) {
  6262.         debug(F100,"ttpkt TIOCSLTC failed","",0);
  6263.         } else {
  6264.         ltcharf = 1;
  6265.         debug(F100,"ttpkt TIOCSLTC ok","",0);
  6266.         }
  6267.     }
  6268. #endif /* TIOCGLTC */
  6269.  
  6270. #else /* LPASS8 not defined */
  6271.  
  6272. /* Previously, BSD-based implementations always */
  6273. /* used rawmode for packets.  Now, we use rawmode only if parity is NONE. */
  6274. /* This allows the flow control requested above to actually work, but only */
  6275. /* if the user asks for parity (which also means they get 8th-bit quoting). */
  6276.  
  6277.     if (parity) {            /* If parity, */
  6278.         ttraw.sg_flags &= ~RAW;    /* use cooked mode */
  6279. #ifdef COMMENT
  6280. /* WHY??? */
  6281.         if (xlocal)
  6282. #endif /* COMMENT */
  6283.           ttraw.sg_flags |= CBREAK;
  6284.         debug(F101,"ttpkt cooked, cbreak, parity","",parity);
  6285. #ifdef TIOCGETC                /* Not rawmode, */
  6286.         if (tcharf && (xlocal == 0)) { /* must turn off */
  6287.         tchnoi.t_intrc = -1;    /* interrupt character */
  6288.         tchnoi.t_quitc = -1;    /* and quit character. */
  6289.         tchnoi.t_startc = 17;    /* Make sure xon */
  6290.         tchnoi.t_stopc = 19;    /* and xoff not ignored. */
  6291. #ifndef NOBRKC
  6292.         tchnoi.t_eofc = -1;    /* eof character. */
  6293.         tchnoi.t_brkc = -1;    /* brk character. */
  6294. #endif /* NOBRKC */
  6295.         if (ioctl(ttyfd,TIOCSETC,&tchnoi) < 0) {
  6296.             debug(F100,"ttpkt TIOCSETC failed","",0);
  6297.         } else {
  6298.             tcharf = 1;
  6299.             debug(F100,"ttpkt TIOCSETC ok","",0);
  6300.         }
  6301.         }
  6302. #endif /* TIOCGETC */
  6303. #ifdef TIOCGLTC                /* Not rawmode, */
  6304. /* Prevent suspend during packet mode */
  6305.         if (ltcharf && (xlocal == 0)) { /* must turn off */
  6306.         ltchnoi.t_suspc = -1;    /* suspend character */
  6307.         ltchnoi.t_dsuspc = -1;    /* and delayed suspend character */
  6308.         if (ioctl(ttyfd,TIOCSLTC,&tchnoi) < 0) {
  6309.             debug(F100,"ttpkt TIOCSLTC failed","",0);
  6310.         } else {
  6311.             ltcharf = 1;
  6312.             debug(F100,"ttpkt TIOCSLTC ok","",0);
  6313.         }
  6314.         }
  6315. #endif /* TIOCGLTC */
  6316.     } else {            /* If no parity, */
  6317.         ttraw.sg_flags |= RAW;    /* must use 8-bit raw mode. */
  6318.         debug(F101,"ttpkt setting rawmode, parity","",parity);
  6319.     }
  6320. #endif /* LPASS8 */
  6321.     } /* End of Xon/Xoff section */
  6322.  
  6323.     /* Don't echo, don't map CR to CRLF on output, don't fool with case */
  6324. #ifdef LCASE
  6325.     ttraw.sg_flags &= ~(ECHO|CRMOD|LCASE);
  6326. #else
  6327.     ttraw.sg_flags &= ~(ECHO|CRMOD);
  6328. #endif /* LCASE */
  6329.  
  6330. #ifdef TOWER1
  6331.     ttraw.sg_flags &= ~ANYP;            /* Must set this on old Towers */
  6332. #endif /* TOWER1 */
  6333.  
  6334. #ifdef BELLV10
  6335.     if (ioctl(ttyfd,TIOCSETP,&ttraw) < 0) /* Set the new modes. */
  6336.       return(-1);
  6337. #else
  6338.     errno = 0;
  6339.     if (stty(ttyfd,&ttraw) < 0) {       /* Set the new modes. */
  6340.         debug(F101,"ttpkt stty failed","",errno);
  6341.         return(-1);
  6342.     }
  6343. #endif /* BELLV10 */
  6344.     debug(F100,"ttpkt stty ok","",0);
  6345.  
  6346. #ifdef sony_news
  6347.     x = xlocal ? km_ext : km_con;    /* Put line in ASCII mode. */
  6348.     if (x != -1) {            /* Make sure we know original modes. */
  6349.     x &= ~KM_TTYPE;
  6350.     x |= KM_ASCII;
  6351.     if (ioctl(ttyfd,TIOCKSET, &x) < 0) {
  6352.         perror("ttpkt can't set ASCII mode");
  6353.         debug(F101,"ttpkt error setting ASCII mode","",x);
  6354.         return(-1);
  6355.     }
  6356.     }
  6357.     debug(F100,"ttpkt set ASCII mode ok","",0);
  6358. #endif /* sony_news */
  6359.  
  6360.     if (xlocal == 0) {            /* Turn this off so we can read */
  6361.     signal(SIGINT,SIG_IGN);        /* Ctrl-C chars typed at console */
  6362.     sigint_ign = 1;
  6363.     }
  6364.     tvtflg = 0;                /* So ttvt() will work next time */
  6365.     debug(F100,"ttpkt success","",0);
  6366.     return(0);
  6367.  
  6368. #endif /* Not ATTSV or POSIX */
  6369.  
  6370. /* AT&T UNIX and POSIX */
  6371.  
  6372. #ifdef COHERENT
  6373. #define SVORPOSIX
  6374. #endif /* COHERENT */
  6375.  
  6376. #ifdef SVORPOSIX
  6377.     if (flow == FLO_XONX) {        /* Xon/Xoff */
  6378.     ttraw.c_iflag |= (IXON|IXOFF);
  6379.     tthflow(flow, 0, &ttraw);
  6380.     } else if (flow == FLO_NONE) {    /* None */
  6381.     /* NOTE: We should also turn off hardware flow control here! */
  6382.     ttraw.c_iflag &= ~(IXON|IXOFF);
  6383.     tthflow(flow, 0, &ttraw);
  6384.     } else if (flow == FLO_KEEP) {    /* Keep */
  6385.     ttraw.c_iflag &= ~(IXON|IXOFF);    /* Turn off Xon/Xoff flags */
  6386.     ttraw.c_iflag |= (ttold.c_iflag & (IXON|IXOFF)); /* OR in old ones */
  6387.     /* NOTE: We should also handle hardware flow control here! */
  6388. #ifdef POSIX_CRTSCTS
  6389. /* In Linux case, we do this, which is unlikely to be portable */
  6390.         ttraw.c_cflag &= ~CRTSCTS;    /* Turn off RTS/CTS flag */
  6391.         ttraw.c_cflag |= (ttold.c_cflag & CRTSCTS); /* OR in old one */
  6392. #endif /* POSIX_CRTSCTS */
  6393.     } else if (flow == FLO_RTSC ||    /* Hardware */
  6394.            flow == FLO_DTRC ||
  6395.            flow == FLO_DTRT) {
  6396.     ttraw.c_iflag &= ~(IXON|IXOFF);    /* (190) */
  6397.     tthflow(flow, 1, &ttraw);
  6398.     }
  6399.     ttraw.c_lflag &= ~(ICANON|ECHO);
  6400.     ttraw.c_lflag &= ~ISIG;        /* Do NOT check for interrupt chars */
  6401.  
  6402. #ifndef OXOS
  6403. #ifdef QNX
  6404.     if (flow != FLO_RTSC && flow != FLO_DTRC && flow != FLO_DTRT)
  6405. #endif /* QNX */
  6406. #ifndef COHERENT
  6407.       ttraw.c_lflag &= ~IEXTEN;        /* Turn off ^O/^V processing */
  6408. #endif /* COHERENT */
  6409. #else /* OXOS */
  6410.     ttraw.c_cc[VDISCARD] = ttraw.c_cc[VLNEXT] = CDISABLE;
  6411. #endif /* OXOS */
  6412.     ttraw.c_lflag |= NOFLSH;        /* Don't flush */
  6413.     ttraw.c_iflag |= IGNPAR;        /* Ignore parity errors */
  6414. #ifdef ATTSV
  6415. #ifdef BSD44
  6416.     ttraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|INPCK|ISTRIP|IXANY);
  6417. #else
  6418.     ttraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IUCLC|INPCK|ISTRIP|IXANY);
  6419. #endif /* BSD44 */
  6420. #else /* POSIX */
  6421.     ttraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|INPCK|ISTRIP);
  6422. #endif /* ATTSV */
  6423.     ttraw.c_oflag &= ~OPOST;
  6424.     ttraw.c_cflag &= ~(CSIZE);
  6425.     ttraw.c_cflag |= (CS8|CREAD|HUPCL);
  6426.  
  6427. #ifdef CSTOPB
  6428.     if (xlocal) {
  6429.     if (stopbits == 2) {
  6430.         ttraw.c_cflag |= CSTOPB;    /* 2 stop bits */
  6431.         debug(F100,"ttpkt 2 stopbits","",0);
  6432.     } else if (stopbits == 1) {
  6433.         ttraw.c_cflag &= ~(CSTOPB);    /* 1 stop bit */
  6434.         debug(F100,"ttpkt 1 stopbit","",0);
  6435.     }
  6436.     }
  6437. #endif /* CSTOPB */
  6438.  
  6439. #ifdef HWPARITY
  6440.     if (hwparity && xlocal) {        /* Hardware parity */
  6441.     ttraw.c_cflag |= PARENB;    /* Enable parity */
  6442. #ifdef IGNPAR
  6443.     ttraw.c_cflag &= ~(IGNPAR);    /* Don't discard incoming bytes */
  6444.     debug(F100,"ttpkt IGNPAR","",0); /* that have parity errors */
  6445. #endif /* IGNPAR */
  6446.     switch (hwparity) {
  6447.       case 'e':            /* Even */
  6448.         ttraw.c_cflag &= ~(PARODD);
  6449.         debug(F100,"ttpkt 8 bits + even parity","",0);
  6450.         break;
  6451.       case 'o':            /* Odd */
  6452.         ttraw.c_cflag |= PARODD;
  6453.         debug(F100,"ttpkt 8 bits + odd parity","",0);
  6454.         break;
  6455.       case 'm':            /* Mark */
  6456.       case 's':            /* Space */
  6457.         /* PAREXT is mentioned in SVID but the details are not given. */
  6458.         /* PAREXT is not included in POSIX ISO/IEC 9945-1. */
  6459.         debug(F100,"ttpkt 8 bits + invalid parity","",0);
  6460.         break;
  6461.     }
  6462.     } else {                /* We handle parity ourselves */
  6463. #endif /* HWPARITY */
  6464.     ttraw.c_cflag &= ~(PARENB);    /* Don't enable parity */
  6465. #ifdef HWPARITY
  6466.     }
  6467. #endif /* HWPARITY */
  6468.  
  6469. #ifdef IX370
  6470.     ttraw.c_cc[4] = 48;  /* So Series/1 doesn't interrupt on every char */
  6471.     ttraw.c_cc[5] = 1;
  6472. #else
  6473. #ifndef VEOF                /* for DGUX this is VEOF, not VMIN */
  6474.     ttraw.c_cc[4] = 1;   /* [VMIN]  return max of this many characters or */
  6475. #else
  6476. #ifndef OXOS
  6477. #ifdef VMIN
  6478.     ttraw.c_cc[VMIN] = 1;
  6479. #endif /* VMIN */
  6480. #else /* OXOS */
  6481.     ttraw.c_min = 1;
  6482. #endif /* OXOS */
  6483. #endif /* VEOF */
  6484. #ifndef VEOL                /* for DGUX this is VEOL, not VTIME */
  6485.     ttraw.c_cc[5] = 0;     /* [VTIME] when this many secs/10 expire w/no input */
  6486. #else
  6487. #ifndef OXOS
  6488. #ifdef VTIME
  6489.     ttraw.c_cc[VTIME] = 0;
  6490. #endif /* VTIME */
  6491. #else /* OXOS */
  6492.     ttraw.c_time = 0;
  6493. #endif /* OXOS */
  6494. #endif /* VEOL */
  6495. #endif /* IX370 */
  6496.  
  6497. #ifdef VINTR                /* Turn off interrupt character */
  6498.     if (xlocal == 0)            /* so ^C^C can break us out of */
  6499.       ttraw.c_cc[VINTR] = 0;        /* packet mode. */
  6500. #endif /* VINTR */
  6501.  
  6502. #ifdef Plan9
  6503.     if (p9ttyparity('n') < 0)
  6504.     return -1;
  6505. #else
  6506. #ifdef BSD44ORPOSIX
  6507.     errno = 0;
  6508. #ifdef BEOSORBEBOX
  6509.     ttraw.c_cc[VMIN] = 0;        /* DR7 can only poll. */
  6510. #endif /* BEOSORBEBOX */
  6511.     debug(F100,"ttpkt calling tcsetattr(TCSETAW)","",0);
  6512.     x = tcsetattr(ttyfd,TCSADRAIN,&ttraw);
  6513.     debug(F101,"ttpkt BSD44ORPOSIX tcsetattr","",x);
  6514.     if (x < 0) {
  6515.     debug(F101,"ttpkt BSD44ORPOSIX tcsetattr errno","",errno);
  6516.         return(-1);
  6517.     }
  6518. #else /* BSD44ORPOSIX */
  6519.     x = ioctl(ttyfd,TCSETAW,&ttraw);
  6520.     debug(F101,"ttpkt ATTSV ioctl TCSETAW","",x);
  6521.     if (x < 0) {  /* set new modes . */
  6522.     debug(F101,"ttpkt ATTSV ioctl TCSETAW errno","",errno);
  6523.         return(-1);
  6524.     }
  6525. #endif /* BSD44ORPOSIX */
  6526. #endif /* Plan9 */
  6527.     tvtflg = 0;
  6528.     debug(F100,"ttpkt ok","",0);
  6529.     return(0);
  6530. #endif /* ATTSV */
  6531.  
  6532. #ifdef COHERENT
  6533. #undef SVORPOSIX
  6534. #endif /* COHERENT */
  6535.  
  6536. }
  6537.  
  6538. /*  T T S E T F L O W  --  Set flow control immediately.  */
  6539.  
  6540. #ifdef COHERENT
  6541. #define SVORPOSIX
  6542. #endif /* COHERENT */
  6543.  
  6544. int
  6545. ttsetflow(flow) int flow; {
  6546.     if (ttyfd < 0)            /* A channel must be open */
  6547.       return(-1);
  6548.  
  6549.     debug(F101,"ttsetflow flow","",flow);
  6550.  
  6551. #ifdef TN_COMPORT
  6552.     if (netconn && istncomport()) {
  6553.     debug(F101,"ttsetflow net modem","",ttmdm);
  6554.     return(tnsetflow(flow));
  6555.     }
  6556. #endif /* TN_COMPORT */
  6557. #ifdef NETCMD
  6558.     if (ttpipe) return(0);
  6559. #endif /* NETCMD */
  6560. #ifdef NETPTY
  6561.     if (ttpty) return(0);
  6562. #endif /* NETPTY */
  6563.  
  6564. #ifdef COMMENT
  6565.     /* This seems to hurt... */
  6566.     if (flow == FLO_KEEP)
  6567.       return(0);
  6568. #endif /* COMMENT */
  6569.  
  6570.     if (flow == FLO_RTSC ||        /* Hardware flow control... */
  6571.     flow == FLO_DTRC ||
  6572.     flow == FLO_DTRT) {
  6573.     tthflow(flow, 1, &ttraw);
  6574. #ifndef SVORPOSIX
  6575.     ttraw.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6576. #else
  6577.     ttraw.c_iflag &= ~(IXON|IXOFF);
  6578. #endif /* SVORPOSIX */
  6579.  
  6580.     } else if (flow == FLO_XONX) {    /* Xon/Xoff... */
  6581.  
  6582. #ifndef SVORPOSIX
  6583.     ttraw.sg_flags |= TANDEM;
  6584. #else
  6585.     ttraw.c_iflag |= (IXON|IXOFF);
  6586. #endif /* SVORPOSIX */
  6587.     tthflow(FLO_RTSC, 0, &ttraw);    /* Turn off hardware flow control */
  6588.  
  6589.     } else if (flow == FLO_NONE) {    /* No flow control */
  6590.  
  6591. #ifndef SVORPOSIX
  6592.     ttraw.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6593. #else
  6594.     ttraw.c_iflag &= ~(IXON|IXOFF);
  6595. #endif /* SVORPOSIX */
  6596.     tthflow(FLO_RTSC, 0, &ttraw);    /* Turn off any hardware f/c too */
  6597.     }
  6598.  
  6599. /* Set the new modes... */
  6600.  
  6601. #ifndef SVORPOSIX            /* BSD and friends */
  6602. #ifdef BELLV10
  6603.     if (ioctl(ttyfd,TIOCSETP,&ttraw) < 0)
  6604.       return(-1);
  6605. #else
  6606. #ifndef MINIX2
  6607.     if (stty(ttyfd,&ttraw) < 0)
  6608.       return(-1);
  6609. #endif /* MINIX2 */
  6610. #endif /* BELLV10 */
  6611. #else
  6612. #ifdef BSD44ORPOSIX            /* POSIX */
  6613.     if (tcsetattr(ttyfd,TCSADRAIN,&ttraw) < 0)
  6614.       return(-1);
  6615. #else                    /* System V */
  6616.     if (ioctl(ttyfd,TCSETAW,&ttraw) < 0)
  6617.       return(-1);
  6618. #endif /* BSD44ORPOSIX */
  6619. #endif /* SVORPOSIX */
  6620.     return(0);
  6621. }
  6622. #ifdef COHERENT
  6623. #undef SVORPOSIX
  6624. #endif /* COHERENT */
  6625.  
  6626. /*  T T V T -- Condition communication device for use as virtual terminal. */
  6627.  
  6628. int
  6629. #ifdef CK_ANSIC
  6630. ttvt(long speed, int flow)
  6631. #else
  6632. ttvt(speed,flow) long speed; int flow;
  6633. #endif /* CK_ANSIC */
  6634. /* ttvt */ {
  6635.     int s, s2, x;
  6636.  
  6637.     debug(F101,"ttvt ttyfd","",ttyfd);
  6638.     debug(F101,"ttvt tvtflg","",tvtflg);
  6639.     debug(F111,"ttvt speed",ckitoa(ttspeed),speed);
  6640.     debug(F111,"ttvt flow",ckitoa(ttflow),flow);
  6641.     debug(F111,"ttvt curcarr",ckitoa(ttcarr),curcarr);
  6642.  
  6643. /* Note: NetBSD and maybe other BSD44s have cfmakeraw() */
  6644. /* Maybe it would be simpler to use it... */
  6645.  
  6646.     ttpmsk = 0xff;
  6647. #ifdef NOLOCAL
  6648.     return(conbin((char)escchr));
  6649. #else
  6650.     if (ttyfd < 0) {            /* Not open. */
  6651.     if (ttchk() < 0)
  6652.       return(-1);
  6653.     else                /* But maybe something buffered. */
  6654.       return(0);
  6655.     }
  6656. #ifdef NETCMD
  6657.     if (ttpipe) return(0);
  6658. #endif /* NETCMD */
  6659. #ifdef NETPTY
  6660.     if (ttpty) return(0);
  6661. #endif /* NETPTY */
  6662. #ifdef NETCONN
  6663.     if (netconn) {
  6664. #ifdef TCPSOCKET
  6665. #ifdef TCP_NODELAY
  6666.         {
  6667.         extern int tcp_nodelay;
  6668.         if (ttnet == NET_TCPB) {
  6669.         if (nodelay_sav > -1) {
  6670.             no_delay(nodelay_sav);
  6671.             nodelay_sav = -1;
  6672.         }
  6673.         }
  6674.         }
  6675. #endif /* TCP_NODELAY */
  6676. #ifdef TN_COMPORT
  6677.         if (istncomport()) {
  6678.             int rc = -1;
  6679.             if (tvtflg != 0 && speed == ttspeed && flow == ttflow
  6680.                  /* && ttcarr == curcarr */ ) {
  6681.                 debug(F100,"ttvt modes already set, skipping...","",0);
  6682.                 return(0);            /* Already been called. */
  6683.             }
  6684.             if (flow != ttflow) {
  6685.                 if ((rc = tnsetflow(flow)) < 0)
  6686.           return(rc);
  6687.                 ttflow = flow;
  6688.             }
  6689.             if (speed != ttspeed) {
  6690.                 if (speed <= 0) 
  6691.           speed = tnc_get_baud();
  6692.                 else if ((rc = tnc_set_baud(speed)) < 0)
  6693.           return(rc);
  6694.                 ttspeed = speed;
  6695.             }
  6696.             tnc_set_datasize(8);
  6697.         tnc_set_stopsize(stopbits);
  6698.  
  6699. #ifdef HWPARITY
  6700.             if (hwparity) {
  6701.                 switch (hwparity) {
  6702.           case 'e':        /* Even */
  6703.                     debug(F100,"ttres 8 bits + even parity","",0);
  6704.                     tnc_set_parity(3);
  6705.                     break;
  6706.           case 'o':        /* Odd */
  6707.                     debug(F100,"ttres 8 bits + odd parity","",0);
  6708.                     tnc_set_parity(2);
  6709.                     break;
  6710.           case 'm':        /* Mark */
  6711.                     debug(F100,"ttres 8 bits + invalid parity: mark","",0);
  6712.                     tnc_set_parity(4);
  6713.                     break;
  6714.           case 's':        /* Space */
  6715.                     debug(F100,"ttres 8 bits + invalid parity: space","",0);
  6716.                     tnc_set_parity(5);
  6717.                     break;
  6718.                 }
  6719.             } else
  6720. #endif /* HWPARITY */
  6721.             {
  6722.                 tnc_set_parity(0);    /* None */
  6723.             }
  6724.             tvtflg = 1;
  6725.             return(0);
  6726.         }
  6727. #endif /* TN_COMPORT */
  6728. #endif /* TCPSOCKET */
  6729.     tvtflg = 1;            /* Network connections... */
  6730.     debug(F100,"ttvt network connection, skipping...","",0);
  6731.     return(0);            /* ... require no special setup */
  6732.     }
  6733. #endif /* NETCONN */
  6734.  
  6735.     if (tvtflg != 0 && speed == ttspeed && flow == ttflow
  6736.     /* && ttcarr == curcarr */ )
  6737.       {
  6738.       debug(F100,"ttvt modes already set, skipping...","",0);
  6739.       return(0);            /* Already been called. */
  6740.       }
  6741.  
  6742.     if (ttfdflg
  6743. #ifndef Plan9
  6744.     && !isatty(ttyfd)
  6745. #endif /* Plan9 */
  6746.     ) {
  6747.     debug(F100,"ttvt using external fd, skipping...","",0);
  6748.     return(0);
  6749.     }
  6750.  
  6751.     debug(F100,"ttvt setting modes...","",0);
  6752.  
  6753.     if (xlocal) {            /* For external lines... */
  6754.     s2 = (int) (speed / 10L);
  6755.     s = ttsspd(s2);            /* Check/set the speed */
  6756.     carrctl(&tttvt, flow != FLO_DIAL /* Do carrier control */
  6757.         && (ttcarr == CAR_ON || (ttcarr == CAR_AUT && ttmdm != 0)));
  6758.     } else
  6759.       s = s2 = -1;
  6760.  
  6761. #ifdef COHERENT
  6762. #define SVORPOSIX
  6763. #endif /* COHERENT */
  6764.  
  6765. #ifndef SVORPOSIX
  6766.     /* Berkeley, V7, etc */
  6767.     if (flow == FLO_RTSC ||        /* Hardware flow control */
  6768.     flow == FLO_DTRC ||
  6769.     flow == FLO_DTRT) {
  6770.     tthflow(flow, 1, &tttvt);
  6771.     debug(F100,"ttvt hard flow, TANDEM off","",0);
  6772.     tttvt.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6773.     } else if (flow == FLO_XONX) {    /* Xon/Xoff flow control */
  6774.     debug(F100,"ttvt TANDEM on","",0);
  6775.     tttvt.sg_flags |= TANDEM;    /* Ask for it. */
  6776.     tthflow(flow, 0, &tttvt);    /* Turn off hardware f/c */
  6777.     } else if (flow == FLO_NONE) {
  6778.     debug(F100,"ttvt no flow, TANDEM off, RAW on","",0);
  6779.     tttvt.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6780.     tthflow(flow, 0, &tttvt);    /* Turn off any hardware f/c too */
  6781.     tttvt.sg_flags |= RAW;        /* Enter raw mode */
  6782.     } else if (flow == FLO_KEEP) {    /* Keep device's original setting */
  6783.     debug(F100,"ttvt keeping original TANDEM","",0);
  6784.     tttvt.sg_flags &= ~TANDEM;
  6785.     tttvt.sg_flags |= (ttold.sg_flags & TANDEM);
  6786.     /* NOTE: We should also handle hardware flow control here! */
  6787.     }
  6788.     tttvt.sg_flags |= RAW;              /* Raw mode in all cases */
  6789. #ifdef TOWER1
  6790.     tttvt.sg_flags &= ~(ECHO|ANYP);     /* No echo or parity */
  6791. #else
  6792.     tttvt.sg_flags &= ~ECHO;            /* No echo */
  6793. #endif /* TOWER1 */
  6794.  
  6795. #ifdef BELLV10
  6796.     if (ioctl(ttyfd,TIOCSETP,&tttvt) < 0) /* Set the new modes */
  6797.       return(-1);
  6798. #else
  6799.     if (stty(ttyfd,&tttvt) < 0)        /* Set the new modes */
  6800.       return(-1);
  6801. #endif /* BELLV10 */
  6802.  
  6803. #else /* It is ATTSV or POSIX */
  6804.  
  6805.     if (flow == FLO_XONX) {        /* Software flow control */
  6806.     tttvt.c_iflag |= (IXON|IXOFF);    /* On if requested. */
  6807.     tthflow(flow, 0, &tttvt);    /* Turn off hardware f/c */
  6808.     debug(F100,"ttvt SVORPOSIX flow XON/XOFF","",0);
  6809.     } else if (flow == FLO_NONE) {    /* NONE */
  6810.     tttvt.c_iflag &= ~(IXON|IXOFF);    /* Turn off Xon/Xoff */
  6811.     tthflow(flow, 0, &tttvt);    /* Turn off hardware f/c */
  6812.     debug(F100,"ttvt SVORPOSIX flow NONE","",0);
  6813.     } else if (flow == FLO_KEEP) {
  6814.     tttvt.c_iflag &= ~(IXON|IXOFF);    /* Turn off Xon/Xoff flags */
  6815.     tttvt.c_iflag |= (ttold.c_iflag & (IXON|IXOFF)); /* OR in old ones */
  6816. #ifdef POSIX_CRTSCTS
  6817.         tttvt.c_cflag &= ~CRTSCTS;    /* Turn off RTS/CTS flag */
  6818.         tttvt.c_cflag |= (ttold.c_cflag & CRTSCTS); /* OR in old one */
  6819. #endif /* POSIX_CRTSCTS */
  6820.     debug(F100,"ttvt SVORPOSIX flow KEEP","",0);
  6821.     } else if (flow == FLO_RTSC ||    /* Hardware flow control */
  6822.            flow == FLO_DTRC ||
  6823.            flow == FLO_DTRT) {
  6824.     tttvt.c_iflag &= ~(IXON|IXOFF);    /* (196) */
  6825.     tthflow(flow, 1, &tttvt);
  6826.     debug(F100,"ttvt SVORPOSIX flow HARD","",0);
  6827.     }
  6828. #ifndef OXOS
  6829. #ifdef COHERENT
  6830.     tttvt.c_lflag &= ~(ISIG|ICANON|ECHO);
  6831. #else
  6832.     tttvt.c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
  6833. #endif /* COHERENT */
  6834. #ifdef QNX
  6835.     /* Needed for hwfc */
  6836.     if (flow == FLO_RTSC || flow == FLO_DTRC || flow == FLO_DTRT)
  6837.       tttvt.c_lflag |= IEXTEN;
  6838. #endif /* QNX */
  6839. #else /* OXOS */
  6840.     tttvt.c_lflag &= ~(ISIG|ICANON|ECHO);
  6841.     tttvt.c_cc[VDISCARD] = tttvt.c_cc[VLNEXT] = CDISABLE;
  6842. #endif /* OXOS */
  6843.  
  6844.     tttvt.c_iflag |= (IGNBRK|IGNPAR);
  6845.  
  6846. /* Stop bits */
  6847.  
  6848. #ifdef CSTOPB
  6849.     if (xlocal) {
  6850.     if (stopbits == 2) {
  6851.         tttvt.c_cflag |= CSTOPB;    /* 2 stop bits */
  6852.         debug(F100,"ttvt 2 stopbits","",0);
  6853.     } else if (stopbits == 1) {
  6854.         tttvt.c_cflag &= ~(CSTOPB);    /* 1 stop bit */
  6855.         debug(F100,"ttvt 1 stopbit","",0);
  6856.     }
  6857.     }
  6858. #endif /* CSTOPB */
  6859.  
  6860. /* Parity */
  6861.  
  6862. #ifdef HWPARITY
  6863.     if (hwparity && xlocal) {        /* Hardware parity */
  6864. #ifdef IGNPAR
  6865.     tttvt.c_cflag &= ~(IGNPAR);    /* Don't discard incoming bytes */
  6866.     debug(F100,"ttvt IGNPAR","",0); /* that have parity errors */
  6867. #endif /* IGNPAR */
  6868.     tttvt.c_cflag |= PARENB;    /* Enable parity */
  6869.     switch (hwparity) {
  6870.       case 'e':            /* Even */
  6871.         tttvt.c_cflag &= ~(PARODD);
  6872.         debug(F100,"ttvt 8 bits + even parity","",0);
  6873.         break;
  6874.       case 'o':            /* Odd */
  6875.         tttvt.c_cflag |= PARODD;
  6876.         debug(F100,"ttvt 8 bits + odd parity","",0);
  6877.         break;
  6878.       case 'm':            /* Mark */
  6879.       case 's':            /* Space */
  6880.         /* PAREXT is mentioned in SVID but the details are not given. */
  6881.         /* PAREXT is not included in POSIX ISO/IEC 9945-1. */
  6882.         debug(F100,"ttvt 8 bits + invalid parity","",0);
  6883.         break;
  6884.     }
  6885.     } else {                /* We handle parity ourselves */
  6886. #endif /* HWPARITY */
  6887.     tttvt.c_cflag &= ~(PARENB);    /* Don't enable parity */
  6888. #ifdef HWPARITY
  6889.     }
  6890. #endif /* HWPARITY */
  6891.  
  6892. #ifdef ATTSV
  6893. #ifdef BSD44
  6894.     /* Things not to do... */
  6895.     tttvt.c_iflag &= ~(INLCR|IGNCR|ICRNL|INPCK|ISTRIP|IXANY);
  6896. #else
  6897.     tttvt.c_iflag &= ~(INLCR|IGNCR|ICRNL|IUCLC|INPCK|ISTRIP|IXANY);
  6898. #endif /* BSD44 */
  6899. #else /* POSIX */
  6900.     tttvt.c_iflag &= ~(INLCR|IGNCR|ICRNL|INPCK|ISTRIP);
  6901. #endif /* ATTSV */
  6902.     tttvt.c_cflag &= ~(CSIZE);        /* Zero out the char size field */
  6903.     tttvt.c_cflag |= (CS8|CREAD|HUPCL);    /* Char size 8, enable receiver, hup */
  6904.     tttvt.c_oflag &= ~OPOST;        /* Don't postprocess output */
  6905. #ifndef VEOF /* DGUX termio has VEOF at entry 4, see comment above */
  6906.     tttvt.c_cc[4] = 1;
  6907. #else
  6908. #ifndef OXOS
  6909. #ifdef VMIN
  6910.     tttvt.c_cc[VMIN] = 1;
  6911. #endif /* VMIN */
  6912. #else /* OXOS */
  6913.     tttvt.c_min = 1;
  6914. #endif /* OXOS */
  6915. #endif /* VEOF */
  6916. #ifndef VEOL    /* DGUX termio has VEOL at entry 5, see comment above */
  6917.     tttvt.c_cc[5] = 0;
  6918. #else
  6919. #ifndef OXOS
  6920. #ifdef VTIME
  6921.     tttvt.c_cc[VTIME] = 0;
  6922. #endif /* VTIME */
  6923. #else /* OXOS */
  6924.     tttvt.c_time = 0;
  6925. #endif /* OXOS */
  6926. #endif /* VEOL */
  6927.  
  6928. #ifdef Plan9
  6929.     if (p9ttyparity('n') < 0)
  6930.       return -1;
  6931. #else
  6932. #ifdef BSD44ORPOSIX
  6933.     errno = 0;
  6934. #ifdef BEOSORBEBOX
  6935.     tttvt.c_cc[VMIN] = 0;        /* DR7 can only poll. */
  6936. #endif /* BEOSORBEBOX */
  6937.     x = tcsetattr(ttyfd,TCSADRAIN,&tttvt);
  6938.     debug(F101,"ttvt BSD44ORPOSIX tcsetattr","",x);
  6939.     if (x < 0) {
  6940.     debug(F101,"ttvt BSD44ORPOSIX tcsetattr errno","",errno);
  6941.     return(-1);
  6942.     }
  6943. #else /* ATTSV */
  6944.     x = ioctl(ttyfd,TCSETAW,&tttvt);
  6945.     debug(F101,"ttvt ATTSV ioctl TCSETAW","",x);
  6946.     if (x < 0) {            /* set new modes . */
  6947.     debug(F101,"ttvt ATTSV ioctl TCSETAW errno","",errno);
  6948.     return(-1);    
  6949.     }
  6950. #endif /* BSD44ORPOSIX */
  6951. #endif /* Plan9 */
  6952. #endif /* ATTSV */
  6953.  
  6954.     ttspeed = speed;            /* Done, remember how we were */
  6955.     ttflow = flow;            /* called, so we can decide how to */
  6956.     tvtflg = 1;                /* respond next time. */
  6957.     debug(F100,"ttvt ok","",0);
  6958.     return(0);
  6959.  
  6960. #ifdef COHERENT
  6961. #undef SVORPOSIX
  6962. #endif /* COHERENT */
  6963.  
  6964. #endif /* NOLOCAL */
  6965. }
  6966.  
  6967. #ifndef NOLOCAL
  6968.  
  6969. /* Serial speed department . . . */
  6970.  
  6971. /*
  6972.   SCO OSR5.0.x might or might not support high speeds.  Sometimes they are not
  6973.   defined in the header files but they are supported (e.g. when building with
  6974.   UDK compiler rather than /bin/cc), sometimes vice versa.  Even though 5.0.4
  6975.   was the first release that came with high serial speeds standard, releases
  6976.   back to 5.0.0 could use them if certain patches (or "supplements") were
  6977.   applied to the SIO driver.  Plus a lot of SCO installations run third-party
  6978.   drivers.
  6979. */
  6980. #ifdef CK_SCOV5
  6981. #ifndef B38400
  6982. #define    B38400    0000017
  6983. #endif /* B38400 */
  6984. #ifndef B57600
  6985. #define    B57600    0000021
  6986. #endif /* B57600 */
  6987. #ifndef B76800
  6988. #define    B76800    0000022
  6989. #endif /* B76800 */
  6990. #ifndef B115200
  6991. #define    B115200    0000023
  6992. #endif /* B115200 */
  6993. #ifndef B230400
  6994. #define    B230400    0000024
  6995. #endif /* B230400 */
  6996. #ifndef B460800
  6997. #define    B460800    0000025
  6998. #endif /* B460800 */
  6999. #ifndef B921600
  7000. #define    B921600    0000026
  7001. #endif /* B921600 */
  7002. #endif /* CK_SCOV5 */
  7003. /*
  7004.   Plan 9's native speed setting interface lets you set anything you like,
  7005.   but will fail if the hardware doesn't like it, so we allow all the common
  7006.   speeds.
  7007. */
  7008. #ifdef Plan9
  7009. #ifndef B50
  7010. #define B50 50
  7011. #endif /* B50 */
  7012. #ifndef B75
  7013. #define B75 75
  7014. #endif /* B75 */
  7015. #ifndef B110
  7016. #define B110 110
  7017. #endif /* B110 */
  7018. #ifndef B134
  7019. #define B134 134
  7020. #endif /* B134 */
  7021. #ifndef B200
  7022. #define B200 200
  7023. #endif /* B200 */
  7024. #ifndef B300
  7025. #define B300 300
  7026. #endif /* B300 */
  7027. #ifndef B1200
  7028. #define B1200 1200
  7029. #endif /* B1200 */
  7030. #ifndef B1800
  7031. #define B1800 1800
  7032. #endif /* B1800 */
  7033. #ifndef B2400
  7034. #define B2400 2400
  7035. #endif /* B2400 */
  7036. #ifndef B4800
  7037. #define B4800 4800
  7038. #endif /* B4800 */
  7039. #ifndef B9600
  7040. #define B9600 9600
  7041. #endif /* B9600 */
  7042. #ifndef B14400
  7043. #define B14400 14400
  7044. #endif /* B14400 */
  7045. #ifndef B19200
  7046. #define B19200 19200
  7047. #endif /* B19200 */
  7048. #ifndef B28800
  7049. #define B28800 28800
  7050. #endif /* B28800 */
  7051. #ifndef B38400
  7052. #define B38400 38400
  7053. #endif /* B38400 */
  7054. #ifndef B57600
  7055. #define B57600 57600
  7056. #endif /* B57600 */
  7057. #ifndef B76800
  7058. #define B76800 76800
  7059. #endif /* B76800 */
  7060. #ifndef B115200
  7061. #define B115200 115200
  7062. #endif /* B115200 */
  7063. #ifndef B230400
  7064. #define B230400 230400
  7065. #endif /* B230400 */
  7066. #ifndef B460800
  7067. #define B460800 460800
  7068. #endif /* B460800 */
  7069. #ifndef B921600
  7070. #define B921600 921600
  7071. #endif /* B921600 */
  7072. #endif /* Plan9 */
  7073.  
  7074. /*  T T S S P D  --  Checks and sets transmission rate.  */
  7075.  
  7076. /*  Call with speed in characters (not bits!) per second. */
  7077. /*  Returns -1 on failure, 0 if it did nothing, 1 if it changed the speed. */
  7078.  
  7079. #ifdef USETCSETSPEED
  7080. /*
  7081.   The tcsetspeed() / tcgetspeed() interface lets you pass any number at all
  7082.   to be used as a speed to be set, rather than forcing a choice from a
  7083.   predefined list.  It seems to be peculiar to UnixWare 7.
  7084.  
  7085.   These are the function codes to be passed to tc[gs]etspeed(),
  7086.   but for some reason they don't seem to be picked up from termios.h.
  7087. */
  7088. #ifndef TCS_ALL
  7089. #define TCS_ALL 0
  7090. #endif /* TCS_ALL */
  7091. #ifndef TCS_IN
  7092. #define TCS_IN 1
  7093. #endif /* TCS_IN */
  7094. #ifndef TCS_OUT
  7095. #define TCS_OUT 2
  7096. #endif /* TCS_OUT */
  7097. #endif /* USETCSETSPEED */
  7098.  
  7099. int
  7100. ttsspd(cps) int cps; {
  7101.     int x;
  7102. #ifdef POSIX
  7103. /* Watch out, speed_t should be unsigned, so don't compare with -1, etc... */
  7104.     speed_t
  7105. #else
  7106.     int
  7107. #endif /* POSIX */
  7108.       s, s2;
  7109.     int ok = 1;                /* Speed check result, assume ok */
  7110.  
  7111. #ifdef OLINUXHISPEED
  7112.     unsigned int spd_flags = 0;
  7113.     struct serial_struct serinfo;
  7114. #endif /* OLINUXHISPEED */
  7115.  
  7116.     debug(F101,"ttsspd cps","",cps);
  7117.     debug(F101,"ttsspd ttyfd","",ttyfd);
  7118.     debug(F101,"ttsspd xlocal","",xlocal);
  7119.  
  7120.     if (ttyfd < 0 || xlocal == 0)    /* Don't set speed on console */
  7121.       return(0);
  7122.  
  7123. #ifdef    NETCONN
  7124.     if (netconn) {
  7125. #ifdef TN_COMPORT
  7126.         if (istncomport())
  7127.       return(tnc_set_baud(cps * 10));
  7128.         else
  7129. #endif /* TN_COMPORT */
  7130.     return(0);
  7131.   }
  7132. #endif    /* NETCONN */
  7133. #ifdef NETCMD
  7134.     if (ttpipe) return(0);
  7135. #endif /* NETCMD */
  7136. #ifdef NETPTY
  7137.     if (ttpty) return(0);
  7138. #endif /* NETPTY */
  7139.  
  7140.     if (cps < 0) return(-1);
  7141.     s = s2 = 0;                /* NB: s and s2 might be unsigned */
  7142.  
  7143. #ifdef USETCSETSPEED
  7144.  
  7145.     s = cps * 10L;
  7146.  
  7147.     x = tcgetattr(ttyfd,&ttcur);    /* Get current speed */
  7148.     debug(F101,"ttsspd tcgetattr","",x);
  7149.     if (x < 0)
  7150.       return(-1);
  7151.     debug(F101,"ttsspd TCSETSPEED speed","",s);
  7152.  
  7153.     errno = 0;
  7154.     if (s == 8880L) {            /* 75/1200 split speed requested */
  7155.     tcsetspeed(TCS_IN, &ttcur, 1200L);
  7156.     tcsetspeed(TCS_OUT, &ttcur, 75L);
  7157.     } else
  7158.       tcsetspeed(TCS_ALL, &ttcur, s);    /* Put new speed in structs */
  7159. #ifdef DEBUG
  7160.     if (errno & deblog) {
  7161.     debug(F101,"ttsspd TCSETSPEED errno","",errno);
  7162.     }
  7163. #endif /* DEBUG */
  7164.  
  7165. #ifdef COMMENT
  7166.     tcsetspeed(TCS_ALL, &ttraw, s);
  7167.     tcsetspeed(TCS_ALL, &tttvt, s);
  7168.     tcsetspeed(TCS_ALL, &ttold, s);
  7169. #else
  7170.     if (s == 8880L) {            /* 75/1200 split speed requested */
  7171.     tcsetspeed(TCS_IN, &ttraw, 1200L);
  7172.     tcsetspeed(TCS_OUT, &ttraw, 75L);
  7173.     tcsetspeed(TCS_IN, &tttvt, 1200L);
  7174.     tcsetspeed(TCS_OUT, &tttvt, 75L);
  7175.     tcsetspeed(TCS_IN, &ttold, 1200L);
  7176.     tcsetspeed(TCS_OUT, &ttold, 75L);
  7177.     } else {
  7178.     tcsetspeed(TCS_ALL, &ttraw, s);
  7179.     tcsetspeed(TCS_ALL, &tttvt, s);
  7180.     tcsetspeed(TCS_ALL, &ttold, s);
  7181.     }
  7182. #endif /* COMMENT */
  7183.  
  7184.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur); /* Set the speed */
  7185.     debug(F101,"ttsspd tcsetattr","",x);
  7186.     if (x < 0)
  7187.       return(-1);
  7188.  
  7189. #else  /* Not USETCSETSPEED */
  7190.  
  7191. #ifdef MINIX2        /* Hack alert */
  7192. #define MINIX        /* Use pre-2.0 speed selection for Minix 2.0 as well */
  7193. #endif /* MINIX2 */
  7194.  
  7195.     /* First check that the given speed is valid. */
  7196.  
  7197.     switch (cps) {
  7198. #ifndef MINIX
  7199.       case 0:   s = B0;    break;
  7200.       case 5:   s = B50;   break;
  7201.       case 7:   s = B75;   break;
  7202. #endif /* MINIX */
  7203.       case 11:  s = B110;  break;
  7204. #ifndef MINIX
  7205.       case 13:  s = B134;  break;
  7206.       case 15:  s = B150;  break;
  7207.       case 20:  s = B200;  break;
  7208. #endif /* MINIX */
  7209.       case 30:  s = B300;  break;
  7210. #ifndef MINIX
  7211.       case 60:  s = B600;  break;
  7212. #endif /* MINIX */
  7213.       case 120: s = B1200; break;
  7214. #ifndef MINIX
  7215.       case 180: s = B1800; break;
  7216. #endif /* MINIX */
  7217.       case 240: s = B2400; break;
  7218.       case 480: s = B4800; break;
  7219. #ifndef MINIX
  7220.       case 888: s = B75; s2 = B1200; break; /* 888 = 75/1200 split speed */
  7221. #endif /* MINIX */
  7222. #ifdef B7200
  7223.       case 720: s = B7200; break;
  7224. #endif /* B7200 */
  7225.       case 960: s = B9600; break;
  7226. #ifdef B14400
  7227.       case 1440: s = B14400; break;
  7228. #endif /* B14400 */
  7229. #ifdef B19200
  7230.       case 1920: s = B19200; break;
  7231. #else
  7232. #ifdef EXTA
  7233.       case 1920: s = EXTA; break;
  7234. #endif /* EXTA */
  7235. #endif /* B19200 */
  7236. #ifdef B28800
  7237.       case 2880: s = B28800; break;
  7238. #endif /* B28800 */
  7239. #ifdef B38400
  7240.       case 3840: s = B38400;
  7241. #ifdef OLINUXHISPEED
  7242.         spd_flags = ~ASYNC_SPD_MASK;    /* Nonzero, but zero flags */
  7243. #endif /* OLINUXHISPEED */
  7244.     break;
  7245. #else /* B38400 not defined... */
  7246. #ifdef EXTB
  7247.       case 3840: s = EXTB; break;
  7248. #endif /* EXTB */
  7249. #endif /* B38400 */
  7250.  
  7251. #ifdef HPUX
  7252. #ifdef _B57600
  7253.       case 5760: s = _B57600; break;
  7254. #endif /* _B57600 */
  7255. #ifdef _B115200
  7256.       case 11520: s = _B115200; break;
  7257. #endif /* _B115200 */
  7258. #else
  7259. #ifdef OLINUXHISPEED
  7260. /*
  7261.   This bit from <carlo@sg.tn.tudelft.nl>:
  7262.   "Only note to make is maybe this: When the ASYNC_SPD_CUST flags are set then
  7263.   setting the speed to 38400 will set the custom speed (and ttgspd returns
  7264.   38400), but speeds 57600 and 115200 won't work any more because I didn't
  7265.   want to mess up the speed flags when someone is doing sophisticated stuff
  7266.   like custom speeds..."
  7267. */
  7268.       case 5760: s = B38400; spd_flags = ASYNC_SPD_HI; break;
  7269.       case 11520: s = B38400; spd_flags = ASYNC_SPD_VHI; break;
  7270. #else
  7271. #ifdef B57600
  7272.       case 5760: s = B57600; break;
  7273. #endif /* B57600 */
  7274. #ifdef B76800
  7275.       case 7680: s = B76800; break;
  7276. #endif /* B76800 */
  7277. #ifdef B115200
  7278.       case 11520: s = B115200; break;
  7279. #endif /* B115200 */
  7280. #endif /* OLINUXHISPEED */
  7281. #ifdef B153600
  7282.       case 15360: s = B153600; break;
  7283. #endif /* B153600 */
  7284. #ifdef B230400
  7285.       case 23040: s = B230400; break;
  7286. #endif /* B230400 */
  7287. #ifdef B307200
  7288.       case 30720: s = B307200; break;
  7289. #endif /* B307200 */
  7290. #ifdef B460800
  7291.       case 46080: s = B460800; break;
  7292. #endif /* 460800 */
  7293. #ifdef B921600
  7294.       case 92160: s = B921600; break;
  7295. #endif /* B921600 */
  7296. #endif /* HPUX */
  7297.       default:
  7298.     ok = 0;                /* Good speed not found, so not ok */
  7299.     break;
  7300.     }
  7301.     debug(F101,"ttsspd ok","",ok);
  7302.     debug(F101,"ttsspd s","",s);
  7303.  
  7304.     if (!ok) {
  7305.     debug(F100,"ttsspd fails","",0);
  7306.     return(-1);
  7307.     } else {
  7308.     if (!s2) s2 = s;        /* Set input speed */
  7309. #ifdef Plan9
  7310.     if (p9ttsspd(cps) < 0)
  7311.       return(-1);
  7312. #else
  7313. #ifdef BSD44ORPOSIX
  7314.     x = tcgetattr(ttyfd,&ttcur);    /* Get current speed */
  7315.     debug(F101,"ttsspd tcgetattr","",x);
  7316.     if (x < 0)
  7317.       return(-1);
  7318. #ifdef OLINUXHISPEED
  7319.     debug(F101,"ttsspd spd_flags","",spd_flags);
  7320.     if (spd_flags && spd_flags != ASYNC_SPD_CUST) {
  7321.         if (ioctl(ttyfd, TIOCGSERIAL, &serinfo) < 0) {
  7322.         debug(F100,"ttsspd: TIOCGSERIAL failed","",0);
  7323.         return(-1);
  7324.         } else debug(F100,"ttsspd: TIOCGSERIAL ok","",0);
  7325.         serinfo.flags &= ~ASYNC_SPD_MASK;
  7326.         serinfo.flags |= (spd_flags & ASYNC_SPD_MASK);
  7327.         if (ioctl(ttyfd, TIOCSSERIAL, &serinfo) < 0)
  7328.           return(-1);
  7329.     }
  7330. #endif /* OLINUXHISPEED */
  7331.     cfsetospeed(&ttcur,s);
  7332.     cfsetispeed(&ttcur,s2);
  7333.     cfsetospeed(&ttraw,s);
  7334.     cfsetispeed(&ttraw,s2);
  7335.     cfsetospeed(&tttvt,s);
  7336.     cfsetispeed(&tttvt,s2);
  7337.     cfsetospeed(&ttold,s);
  7338.     cfsetispeed(&ttold,s2);
  7339.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  7340.     debug(F101,"ttsspd tcsetattr","",x);
  7341.     if (x < 0) return(-1);
  7342. #else
  7343. #ifdef ATTSV
  7344.     if (cps == 888) return(-1);    /* No split speeds, sorry. */
  7345.     x = ioctl(ttyfd,TCGETA,&ttcur);
  7346.     debug(F101,"ttsspd TCGETA ioctl","",x);
  7347.     if (x < 0) return(-1);
  7348.     ttcur.c_cflag &= ~CBAUD;
  7349.     ttcur.c_cflag |= s;
  7350.     tttvt.c_cflag &= ~CBAUD;
  7351.     tttvt.c_cflag |= s;
  7352.     ttraw.c_cflag &= ~CBAUD;
  7353.     ttraw.c_cflag |= s;
  7354.     ttold.c_cflag &= ~CBAUD;
  7355.     ttold.c_cflag |= s;
  7356.     x = ioctl(ttyfd,TCSETAW,&ttcur);
  7357.     debug(F101,"ttsspd TCSETAW ioctl","",x);
  7358.     if (x < 0) return(-1);
  7359. #else
  7360. #ifdef BELLV10
  7361.     x = ioctl(ttyfd,TIOCGDEV,&tdcur);
  7362.     debug(F101,"ttsspd TIOCGDEV ioctl","",x);
  7363.     if (x < 0) return(-1);
  7364.     tdcur.ispeed = s2;
  7365.     tdcur.ospeed = s;
  7366.     errno = 0;
  7367.     ok = ioctl(ttyfd,TIOCSDEV,&tdcur);
  7368.     debug(F101,"ttsspd BELLV10 ioctl","",ok);
  7369.     if (ok < 0) {
  7370.         perror(ttnmsv);
  7371.         debug(F101,"ttsspd BELLV10 errno","",ok);
  7372.         return(-1);
  7373.     }
  7374. #else
  7375.     x = gtty(ttyfd,&ttcur);
  7376.     debug(F101,"ttsspd gtty","",x);
  7377.     if (x < 0) return(-1);
  7378.     ttcur.sg_ospeed = s; ttcur.sg_ispeed = s2;
  7379.     tttvt.sg_ospeed = s; tttvt.sg_ispeed = s2;
  7380.     ttraw.sg_ospeed = s; ttraw.sg_ispeed = s2;
  7381.     ttold.sg_ospeed = s; ttold.sg_ispeed = s2;
  7382.     x = stty(ttyfd,&ttcur);
  7383.     debug(F101,"ttsspd stty","",x);
  7384.     if (x < 0) return(-1);
  7385. #endif /* BELLV10 */
  7386. #endif /* ATTSV */
  7387. #endif /* BSD44ORPOSIX */
  7388. #endif /* Plan9 */
  7389.     }
  7390.     return(1);                /* Return 1 = success. */
  7391. #endif /* USETCSETSPEED */
  7392. }
  7393.  
  7394. #endif /* NOLOCAL */
  7395.  
  7396. /* C O N G S P D  -  Get speed of console terminal  */
  7397.  
  7398. long
  7399. congspd() {
  7400. /*
  7401.   This is a disgusting hack.  The right way to do this would be to pass an
  7402.   argument to ttgspd(), but then we'd need to change the Kermit API and
  7403.   all of the ck?tio.c modules.  (Currently used only for rlogin.)
  7404. */
  7405.     int t1;
  7406.     long spd;
  7407. #ifdef NETCONN
  7408.     int t2 = netconn;
  7409.     netconn = 0;
  7410. #endif /* NETCONN */
  7411.     t1 = ttyfd;
  7412.     ttyfd = -1;
  7413.     spd = ttgspd();
  7414.     debug(F101,"congspd","",spd);
  7415. #ifdef NETCONN
  7416.     netconn = t2;
  7417. #endif /* NETCONN */
  7418.     ttyfd = t1;
  7419.     return(spd);
  7420. }
  7421.  
  7422. /*  T T S P D L I S T  -- Get list of serial speeds allowed on this platform */
  7423.  
  7424. #define NSPDLIST 64
  7425. static long spdlist[NSPDLIST];
  7426. /*
  7427.   As written, this picks up the speeds known at compile time, and thus
  7428.   apply to the system where C-Kermit was built, rather than to the one where
  7429.   it is running.  Suggestions for improvement are always welcome.
  7430. */
  7431. long *
  7432. ttspdlist() {
  7433.     int i;
  7434.     for (i = 0; i < NSPDLIST; i++)    /* Initialize the list */
  7435.       spdlist[i] = -1L;
  7436.     i = 1;
  7437.  
  7438. #ifdef USETCSETSPEED            /* No way to find out what's legal */
  7439.     debug(F100,"ttspdlist USETCSETSPEED","",0);
  7440.     spdlist[i++] = 50L;
  7441. #ifndef UW7
  7442.     spdlist[i++] = 75L;
  7443. #endif /* UW7 */
  7444.     spdlist[i++] = 110L;
  7445. #ifndef UW7
  7446.     spdlist[i++] = 134L;
  7447. #endif /* UW7 */
  7448.     spdlist[i++] = 150L;
  7449.     spdlist[i++] = 200L;
  7450.     spdlist[i++] = 300L;
  7451.     spdlist[i++] = 600L;
  7452.     spdlist[i++] = 1200L;
  7453.     spdlist[i++] = 1800L;
  7454.     spdlist[i++] = 2400L;
  7455.     spdlist[i++] = 4800L;
  7456.     spdlist[i++] = 8880L;
  7457.     spdlist[i++] = 9600L;
  7458.     spdlist[i++] = 14400L;
  7459.     spdlist[i++] = 19200L;
  7460.     spdlist[i++] = 28800L;
  7461. #ifndef UW7
  7462.     spdlist[i++] = 33600L;
  7463. #endif /* UW7 */
  7464.     spdlist[i++] = 38400L;
  7465.     spdlist[i++] = 57600L;
  7466.     spdlist[i++] = 76800L;
  7467.     spdlist[i++] = 115200L;
  7468. #ifndef UW7
  7469.     spdlist[i++] = 153600L;
  7470.     spdlist[i++] = 230400L;
  7471.     spdlist[i++] = 307200L;
  7472.     spdlist[i++] = 460800L;
  7473.     spdlist[i++] = 921600L;
  7474. #endif /* UW7 */
  7475.  
  7476. #else  /* USETCSETSPEED */
  7477.  
  7478.     debug(F100,"ttspdlist no USETCSETSPEED","",0);
  7479.  
  7480. #ifdef B50
  7481.     debug(F101,"ttspdlist B50","",B50);
  7482.     spdlist[i++] = 50L;
  7483. #endif /* B50 */
  7484. #ifdef B75
  7485.     debug(F101,"ttspdlist B75","",B75);
  7486.     spdlist[i++] = 75L;
  7487. #endif /* B75 */
  7488. #ifdef B110
  7489.     debug(F101,"ttspdlist B110","",B110);
  7490.     spdlist[i++] = 110L;
  7491. #endif /* B110 */
  7492. #ifdef B134
  7493.     debug(F101,"ttspdlist B134","",B134);
  7494.     spdlist[i++] = 134L;
  7495. #endif /* B134 */
  7496. #ifdef B150
  7497.     debug(F101,"ttspdlist B150","",B150);
  7498.     spdlist[i++] = 150L;
  7499. #endif /* B150 */
  7500. #ifdef B200
  7501.     debug(F101,"ttspdlist B200","",B200);
  7502.     spdlist[i++] = 200L;
  7503. #endif /* B200 */
  7504. #ifdef B300
  7505.     debug(F101,"ttspdlist B300","",B300);
  7506.     spdlist[i++] = 300L;
  7507. #endif /* B300 */
  7508. #ifdef B600
  7509.     debug(F101,"ttspdlist B600","",B600);
  7510.     spdlist[i++] = 600L;
  7511. #endif /* B600 */
  7512. #ifdef B1200
  7513.     debug(F101,"ttspdlist B1200","",B1200);
  7514.     spdlist[i++] = 1200L;
  7515. #endif /* B1200 */
  7516. #ifdef B1800
  7517.     debug(F101,"ttspdlist B1800","",B1800);
  7518.     spdlist[i++] = 1800L;
  7519. #endif /* B1800 */
  7520. #ifdef B2400
  7521.     debug(F101,"ttspdlist B2400","",B2400);
  7522.     spdlist[i++] = 2400L;
  7523. #endif /* B2400 */
  7524. #ifdef B4800
  7525.     debug(F101,"ttspdlist B4800","",B4800);
  7526.     spdlist[i++] = 4800L;
  7527. #endif /* B4800 */
  7528. #ifdef B9600
  7529.     debug(F101,"ttspdlist B9600","",B9600);
  7530.     spdlist[i++] = 9600L;
  7531. #endif /* B9600 */
  7532. #ifdef B14400
  7533.     debug(F101,"ttspdlist B14400","",B14400);
  7534.     spdlist[i++] = 14400L;
  7535. #endif /* B14400 */
  7536. #ifdef B19200
  7537.     debug(F101,"ttspdlist B19200","",B19200);
  7538.     spdlist[i++] = 19200L;
  7539. #else
  7540. #ifdef EXTA
  7541.     debug(F101,"ttspdlist EXTA","",EXTA);
  7542.     spdlist[i++] = 19200L;
  7543. #endif /* EXTA */
  7544. #endif /* B19200 */
  7545. #ifdef B28800
  7546.     debug(F101,"ttspdlist B28800","",B28800);
  7547.     spdlist[i++] = 28800L;
  7548. #endif /* B28800 */
  7549. #ifdef B33600
  7550.     debug(F101,"ttspdlist B33600","",B33600);
  7551.     spdlist[i++] = 33600L;
  7552. #endif /* B33600 */
  7553. #ifdef B38400
  7554.     debug(F101,"ttspdlist B38400","",B38400);
  7555.     spdlist[i++] = 38400L;
  7556. #else
  7557. #ifdef EXTB
  7558.     debug(F101,"ttspdlist EXTB","",EXTB);
  7559.     spdlist[i++] = 38400L;
  7560. #endif /* EXTB */
  7561. #endif /* B38400 */
  7562. #ifdef _B57600
  7563.     debug(F101,"ttspdlist _B57600","",_B57600);
  7564.     spdlist[i++] = 57600L;
  7565. #else
  7566. #ifdef B57600
  7567.     debug(F101,"ttspdlist B57600","",B57600);
  7568.     spdlist[i++] = 57600L;
  7569. #endif /* B57600 */
  7570. #endif /* _B57600 */
  7571. #ifdef B76800
  7572.     debug(F101,"ttspdlist B76800","",B76800);
  7573.     spdlist[i++] = 76800L;
  7574. #endif /* B76800 */
  7575. #ifdef _B115200
  7576.     debug(F101,"ttspdlist _B115200","",_B115200);
  7577.     spdlist[i++] = 115200L;
  7578. #else
  7579. #ifdef B115200
  7580.     debug(F101,"ttspdlist B115200","",B115200);
  7581.     spdlist[i++] = 115200L;
  7582. #endif /* B115200 */
  7583. #endif /* _B115200 */
  7584. #ifdef B153600
  7585.     debug(F101,"ttspdlist B153600","",B153600);
  7586.     spdlist[i++] = 153600L;
  7587. #endif /* B153600 */
  7588. #ifdef B230400
  7589.     debug(F101,"ttspdlist B230400","",B230400);
  7590.     spdlist[i++] = 230400L;
  7591. #endif /* B230400 */
  7592. #ifdef B307200
  7593.     debug(F101,"ttspdlist B307200","",B307200);
  7594.     spdlist[i++] = 307200L;
  7595. #endif /* B307200 */
  7596. #ifdef B460800
  7597.     debug(F101,"ttspdlist B460800","",B460800);
  7598.     spdlist[i++] = 460800L;
  7599. #endif /* B460800 */
  7600. #ifdef B921600
  7601.     debug(F101,"ttspdlist B921600","",B921600);
  7602.     spdlist[i++] = 921600L;
  7603. #endif /* B921600 */
  7604. #endif /* USETCSETSPEED */
  7605.     spdlist[0] = i - 1;            /* Return count in 0th element */
  7606.     debug(F111,"ttspdlist spdlist","0",spdlist[0]);
  7607.     return((long *)spdlist);
  7608. }
  7609.  
  7610. /* T T G S P D  -  Get speed of currently selected tty line  */
  7611.  
  7612. /*
  7613.   Unreliable.  After SET LINE, it returns an actual speed, but not necessarily
  7614.   the real speed.  On some systems, it returns the line's nominal speed, from
  7615.   /etc/ttytab.  Even if you SET SPEED to something else, this function might
  7616.   not notice.
  7617. */
  7618. long
  7619. ttgspd() {                /* Get current serial device speed */
  7620. #ifdef NOLOCAL
  7621.     return(-1L);
  7622. #else
  7623. #ifdef POSIX
  7624.     speed_t                /* Should be unsigned */
  7625. #else
  7626.     int                    /* Isn't unsigned */
  7627. #endif /* POSIX */
  7628.       s;
  7629.     int x;
  7630.     long ss;
  7631. #ifdef OLINUXHISPEED
  7632.     unsigned int spd_flags = 0;
  7633.     struct serial_struct serinfo;
  7634. #endif /* OLINUXHISPEED */
  7635.  
  7636. #ifdef NETCONN
  7637.     if (netconn) {
  7638. #ifdef TN_COMPORT
  7639.     if (istncomport())
  7640.       return(tnc_get_baud());
  7641.     else
  7642. #endif /* TN_COMPORT */
  7643.       return(-1);            /* -1 if network connection */
  7644.     }
  7645. #endif /* NETCONN */
  7646. #ifdef NETCMD
  7647.     if (ttpipe) return(-1);
  7648. #endif /* NETCMD */
  7649. #ifdef NETPTY
  7650.     if (ttpty) return(-1);
  7651. #endif /* NETPTY */
  7652.  
  7653.     debug(F101,"ttgspd ttyfd","",ttyfd);
  7654.  
  7655. #ifdef USETCSETSPEED
  7656.  
  7657.     x = tcgetattr(ttyfd,&ttcur);    /* Get current speed */
  7658.     debug(F101,"ttgspd tcgetattr","",x);
  7659.     if (x < 0)
  7660.       return(-1);
  7661.     errno = 0;
  7662.     s = tcgetspeed(TCS_ALL, &ttcur);
  7663.     debug(F101,"ttsspd TCGETSPEED speed","",s);
  7664.     if (s == 0) {
  7665.     long s1, s2;
  7666.     s1 = tcgetspeed(TCS_IN, &ttcur);
  7667.     s2 = tcgetspeed(TCS_OUT, &ttcur);
  7668.     if (s1 == 1200L && s2 == 75L)
  7669.       return(8880L);
  7670.     }
  7671. #ifdef DEBUG
  7672.     if (errno & deblog) {
  7673.     debug(F101,"ttsspd TCGETSPEED errno","",errno);
  7674.     }
  7675. #endif /* DEBUG */
  7676.     return(s);
  7677.  
  7678. #else  /* Not USETCSETSPEED */
  7679.  
  7680. #ifdef Plan9
  7681.     if (ttyfd < 0)
  7682.       ss = -1;
  7683.     else
  7684.       ss = ttylastspeed;
  7685. #else
  7686. #ifdef OLINUXHISPEED
  7687.     debug(F100,"ttgspd Linux OLINUXHISPEED","",0);
  7688. #endif /* OLINUXHISPEED */
  7689.  
  7690.     if (ttyfd < 0) {
  7691. #ifdef BSD44ORPOSIX
  7692.     s = cfgetospeed(&ccold);
  7693.     debug(F101,"ttgspd cfgetospeed 1 POSIX","",s);
  7694. #else
  7695. #ifdef ATTSV
  7696.     s = ccold.c_cflag & CBAUD;
  7697.     debug(F101,"ttgspd c_cflag CBAUD 1 ATTSV","",s);
  7698. #else
  7699.     s = ccold.sg_ospeed;        /* (obtained by congm()) */
  7700.     debug(F101,"ttgspd sg_ospeed 1","",s);
  7701. #endif /* ATTSV */
  7702. #endif /* BSD44POSIX */
  7703.  
  7704.     } else {
  7705. #ifdef BSD44ORPOSIX
  7706.     if (tcgetattr(ttyfd,&ttcur) < 0) return(-1);
  7707.     s = cfgetospeed(&ttcur);
  7708.     debug(F101,"ttgspd cfgetospeed 2 BSDORPOSIX","",s);
  7709. #ifdef OLINUXHISPEED
  7710.     if (ioctl(ttyfd,TIOCGSERIAL,&serinfo) > -1)
  7711.       spd_flags = serinfo.flags & ASYNC_SPD_MASK;
  7712.     debug(F101,"ttgspd spd_flags","",spd_flags);
  7713. #endif /* OLINUXHISPEED */
  7714. #else
  7715. #ifdef ATTSV
  7716.     x = ioctl(ttyfd,TCGETA,&ttcur);
  7717.     debug(F101,"ttgspd ioctl 2 ATTSV x","",x);
  7718.     debug(F101,"ttgspd ioctl 2 ATTSV errno","",errno);
  7719.     if (x < 0) return(-1);
  7720.     s = ttcur.c_cflag & CBAUD;
  7721.     debug(F101,"ttgspd ioctl 2 ATTSV speed","",s);
  7722. #else
  7723. #ifdef BELLV10
  7724.     x = ioctl(ttyfd,TIOCGDEV,&tdcur);
  7725.     debug(F101,"ttgspd ioctl 2 BELLV10 x","",x);
  7726.     if (x < 0) return(-1);
  7727.     s = tdcur.ospeed;
  7728.     debug(F101,"ttgspd ioctl 2 BELLV10 speed","",s);
  7729. #else
  7730.     x = gtty(ttyfd,&ttcur);
  7731.     debug(F101,"ttgspd gtty 2 x","",x);
  7732.     debug(F101,"ttgspd gtty 2 errno","",errno);
  7733.     if (x < 0) return(-1);
  7734.     s = ttcur.sg_ospeed;
  7735.     debug(F101,"ttgspd gtty 2 speed","",s);
  7736. #endif /* BELLV10 */
  7737. #endif /* ATTSV */
  7738. #endif /* BSD44ORPOSIX */
  7739.     }
  7740.     debug(F101,"ttgspd code","",s);
  7741. #ifdef OLINUXHISPEED
  7742.     debug(F101,"ttgspd spd_flags","",spd_flags);
  7743. #endif /* OLINUXHISPEED */
  7744.     switch (s) {
  7745. #ifdef B0
  7746.       case B0:    ss = 0L; break;
  7747. #endif /* B0 */
  7748.  
  7749. #ifndef MINIX
  7750. /*
  7751.  MINIX defines the Bxx symbols to be bps/100, so B50==B75, B110==B134==B150,
  7752.  etc, making for many "duplicate case in switch" errors, which are fatal.
  7753. */
  7754. #ifdef B50
  7755.       case B50:   ss = 50L; break;
  7756. #endif /* B50 */
  7757. #ifdef B75
  7758.       case B75:   ss = 75L; break;
  7759. #endif /* B75 */
  7760. #endif /* MINIX */
  7761.  
  7762. #ifdef B110
  7763.       case B110:  ss = 110L; break;
  7764. #endif /* B110 */
  7765.  
  7766. #ifndef MINIX
  7767. #ifdef B134
  7768.       case B134:  ss = 134L; break;
  7769. #endif /* B134 */
  7770. #ifdef B150
  7771.       case B150:  ss = 150L; break;
  7772. #endif /* B150 */
  7773. #endif /* MINIX */
  7774.  
  7775. #ifdef B200
  7776.       case B200:  ss = 200L; break;
  7777. #endif /* B200 */
  7778.  
  7779. #ifdef B300
  7780.       case B300:  ss = 300L; break;
  7781. #endif /* B300 */
  7782.  
  7783. #ifdef B600
  7784.       case B600:  ss = 600L; break;
  7785. #endif /* B600 */
  7786.  
  7787. #ifdef B1200
  7788.       case B1200: ss = 1200L; break;
  7789. #endif /* B1200 */
  7790.  
  7791. #ifdef B1800
  7792.       case B1800: ss = 1800L; break;
  7793. #endif /* B1800 */
  7794.  
  7795. #ifdef B2400
  7796.       case B2400: ss = 2400L; break;
  7797. #endif /* B2400 */
  7798.  
  7799. #ifdef B4800
  7800.       case B4800: ss = 4800L; break;
  7801. #endif /* B4800 */
  7802.  
  7803. #ifdef B7200
  7804.       case B7200: ss = 7200L; break;
  7805. #endif /* B7200 */
  7806.  
  7807. #ifdef B9600
  7808.       case B9600: ss = 9600L; break;
  7809. #endif /* B9600 */
  7810.  
  7811. #ifdef B19200
  7812.       case B19200: ss = 19200L; break;
  7813. #else
  7814. #ifdef EXTA
  7815.       case EXTA: ss = 19200L; break;
  7816. #endif /* EXTA */
  7817. #endif /* B19200 */
  7818.  
  7819. #ifdef MINIX2
  7820. /* End of hack to make MINIX2 use MINIX1 speed setting */
  7821. #undef MINIX
  7822. #endif /* MINIX2 */
  7823.  
  7824. #ifndef MINIX
  7825. #ifdef B38400
  7826.       case B38400:
  7827.         ss = 38400L;
  7828. #ifdef OLINUXHISPEED
  7829.         switch(spd_flags) {
  7830.           case ASYNC_SPD_HI:  ss =  57600L; break;
  7831.           case ASYNC_SPD_VHI: ss = 115200L; break;
  7832.     }
  7833. #endif /* OLINUXHISPEED */
  7834.         break;
  7835. #else
  7836. #ifdef EXTB
  7837.       case EXTB: ss = 38400L; break;
  7838. #endif /* EXTB */
  7839. #endif /* B38400 */
  7840. #endif /* MINIX */
  7841.  
  7842. #ifdef HPUX
  7843. #ifdef _B57600
  7844.       case _B57600: ss = 57600L; break;
  7845. #endif /* _B57600 */
  7846. #ifdef _B115200
  7847.       case _B115200: ss = 115200L; break;
  7848. #endif /* _B115200 */
  7849. #else
  7850. #ifdef B57600
  7851.       case B57600: ss = 57600L; break;
  7852. #endif /* B57600 */
  7853. #ifdef B76800
  7854.       case B76800: ss = 76800L; break;
  7855. #endif /* B76800 */
  7856. #ifdef B115200
  7857.       case B115200: ss = 115200L; break;
  7858. #endif /* B115200 */
  7859. #ifdef B153600
  7860.       case B153600: ss = 153600L; break;
  7861. #endif /* B153600 */
  7862. #ifdef B230400
  7863.       case B230400: ss = 230400L; break;
  7864. #endif /* B230400 */
  7865. #ifdef B307200
  7866.       case B307200: ss = 307200L; break;
  7867. #endif /* B307200 */
  7868. #ifdef B460800
  7869.       case B460800: ss = 460800L; break;
  7870. #endif /* B460800 */
  7871. #endif /* HPUX */
  7872. #ifdef B921600
  7873.       case 92160: ss = 921600L; break;
  7874. #endif /* B921600 */
  7875.       default:
  7876.     ss = -1; break;
  7877.     }
  7878. #endif /* Plan9 */
  7879.     debug(F101,"ttgspd speed","",ss);
  7880.     return(ss);
  7881.  
  7882. #endif /* USETCSETSPEED */
  7883. #endif /* NOLOCAL */
  7884. }
  7885. #ifdef MINIX2                /* Another hack alert */
  7886. #define MINIX
  7887. #endif /* MINIX2 */
  7888.  
  7889. /*
  7890.   FIONREAD data type...  This has been defined as "long" for many, many
  7891.   years, and it worked OK until 64-bit platforms appeared.  Thus we use
  7892.   int for 64-bit platforms, but keep long for the others.  If we changed
  7893.   the default PEEKTYPE to int, this would probably break 16-bit builds
  7894.   (note that sizeof(long) == sizeof(int) on most 32-bit platforms), many
  7895.   of which we have no way of testing any more.  Therefore, do not change
  7896.   the default definition of PEEKTYPE -- only add exceptions to it as needed.
  7897. */
  7898. #ifdef COHERENT
  7899. #ifdef FIONREAD
  7900. #undef FIONREAD
  7901. #endif /* FIONREAD */
  7902. /* #define FIONREAD TIOCQUERY */
  7903. /* #define PEEKTYPE int */
  7904. #else  /* Not COHERENT... */
  7905.  
  7906. #ifdef OSF32                /* Digital UNIX 3.2 or higher */
  7907. #define PEEKTYPE int
  7908. #else
  7909. #define PEEKTYPE long            /* Elsewhere (see notes above) */
  7910. #endif /* OSF32 */
  7911. #endif /* COHERENT */
  7912.  
  7913. /* ckumyr.c by Kristoffer Eriksson, ske@pkmab.se, 15 Mar 1990. */
  7914.  
  7915. #ifdef MYREAD
  7916.  
  7917. /* Private buffer for myread() and its companions.  Not for use by anything
  7918.  * else.  ttflui() is allowed to reset them to initial values.  ttchk() is
  7919.  * allowed to read my_count.
  7920.  *
  7921.  * my_item is an index into mybuf[].  Increment it *before* reading mybuf[].
  7922.  *
  7923.  * A global parity mask variable could be useful too.  We could use it to
  7924.  * let myread() strip the parity on its own, instead of stripping sign
  7925.  * bits as it does now.
  7926.  */
  7927. #ifdef BIGBUFOK
  7928. #define MYBUFLEN 32768
  7929. #else
  7930. #ifdef pdp11
  7931. #define MYBUFLEN 256
  7932. #else
  7933. #define MYBUFLEN 1024
  7934. #endif /* pdp11 */
  7935. #endif /* BIGBUFOK */
  7936.  
  7937. #ifdef ANYX25
  7938. #undef MYBUFLEN
  7939. #define MYBUFLEN 256
  7940. /*
  7941.   On X.25 connections, there is an extra control byte at the beginning.
  7942. */
  7943. static CHAR x25buf[MYBUFLEN+1];        /* Communication device input buffer */
  7944. static CHAR  *mybuf = x25buf+1;
  7945. #else
  7946. static CHAR mybuf[MYBUFLEN];
  7947. #endif /* ANYX25 */
  7948.  
  7949. static int my_count = 0;        /* Number of chars still in mybuf */
  7950. static int my_item = -1;        /* Last index read from mybuf[]   */
  7951.  
  7952. /*  T T P E E K  --  Peek into our internal communications input buffers. */
  7953.  
  7954. /*
  7955.   NOTE: This routine is peculiar to UNIX, and is used only by the
  7956.   select()-based CONNECT module, ckucns.c.  It need not be replicated in
  7957.   the ck?tio.c of other platforms.
  7958. */
  7959. int
  7960. ttpeek() {
  7961. #ifdef TTLEBUF
  7962.     int rc = 0;
  7963.     if (ttpush >= 0)
  7964.       rc++;
  7965.     rc += le_inbuf();
  7966.     if (rc > 0)
  7967.       return(rc);
  7968.     else
  7969. #endif /* TTLEBUF */
  7970.  
  7971. #ifdef MYREAD
  7972.     return(my_count);
  7973. #else
  7974.     return(0);
  7975. #endif /* MYREAD */
  7976. }
  7977.  
  7978. /* myread() -- Efficient read of one character from communications line.
  7979.  *
  7980.  * Uses a private buffer to minimize the number of expensive read() system
  7981.  * calls.  Essentially performs the equivalent of read() of 1 character, which
  7982.  * is then returned.  By reading all available input from the system buffers
  7983.  * to the private buffer in one chunk, and then working from this buffer, the
  7984.  * number of system calls is reduced in any case where more than one character
  7985.  * arrives during the processing of the previous chunk, for instance high
  7986.  * baud rates or network type connections where input arrives in packets.
  7987.  * If the time needed for a read() system call approaches the time for more
  7988.  * than one character to arrive, then this mechanism automatically compensates
  7989.  * for that by performing bigger read()s less frequently.  If the system load
  7990.  * is high, the same mechanism compensates for that too.
  7991.  *
  7992.  * myread() is a macro that returns the next character from the buffer.  If the
  7993.  * buffer is empty, mygetbuf() is called.  See mygetbuf() for possible error
  7994.  * returns.
  7995.  *
  7996.  * This should be efficient enough for any one-character-at-a-time loops.
  7997.  * For even better efficiency you might use memcpy()/bcopy() or such between
  7998.  * buffers (since they are often better optimized for copying), but it may not
  7999.  * be worth it if you have to take an extra pass over the buffer to strip
  8000.  * parity and check for CTRL-C anyway.
  8001.  *
  8002.  * Note that if you have been using myread() from another program module, you
  8003.  * may have some trouble accessing this macro version and the private variables
  8004.  * it uses.  In that case, just add a function in this module, that invokes the
  8005.  * macro.
  8006.  */
  8007. #define myread() (--my_count < 0 ? mygetbuf() : 255 & (int)mybuf[++my_item])
  8008.  
  8009. /* Specification: Push back up to one character onto myread()'s queue.
  8010.  *
  8011.  * This implementation: Push back characters into mybuf. At least one character
  8012.  * must have been read through myread() before myunrd() may be used.  After
  8013.  * EOF or read error, again, myunrd() can not be used.  Sometimes more than
  8014.  * one character can be pushed back, but only one character is guaranteed.
  8015.  * Since a previous myread() must have read its character out of mybuf[],
  8016.  * that guarantees that there is space for at least one character.  If push
  8017.  * back was really needed after EOF, a small addition could provide that.
  8018.  *
  8019.  * myunrd() is currently not called from anywhere inside kermit...
  8020.  */
  8021. #ifdef COMMENT /* not used */
  8022. myunrd(ch) CHAR ch; {
  8023.     if (my_item >= 0) {
  8024.     mybuf[my_item--] = ch;
  8025.     ++my_count;
  8026.     }
  8027. }
  8028. #endif /* COMMENT */
  8029.  
  8030. /*  T T P U S H B A C K  --  Put n bytes back into the myread buffer */
  8031.  
  8032. static CHAR * pushbuf = NULL;
  8033. /* static int pushed = 0; */
  8034.  
  8035. int
  8036. ttpushback(s,n) CHAR * s; int n; {
  8037.     debug(F101,"ttpushback n","",n);
  8038.     if (pushbuf || n > MYBUFLEN || n < 1)
  8039.       return(-1);
  8040.     debug(F101,"ttpushback my_count","",my_count);
  8041.     if (my_count > 0) {
  8042.     if (!(pushbuf = (CHAR *)malloc(n+1)))
  8043.       return(-1);
  8044.     memcpy(pushbuf,mybuf,my_count);
  8045.     /* pushed = my_count; */ /* (set but never used) */
  8046.     }
  8047.     memcpy(mybuf,s,n);
  8048.     my_count = n;
  8049.     my_item = -1;
  8050.     return(0);
  8051. }
  8052.  
  8053. /* mygetbuf() -- Fill buffer for myread() and return first character.
  8054.  *
  8055.  * This function is what myread() uses when it can't get the next character
  8056.  * directly from its buffer.  First, it calls a system dependent myfillbuf()
  8057.  * to read at least one new character into the buffer, and then it returns
  8058.  * the first character just as myread() would have done.  This function also
  8059.  * is responsible for all error conditions that myread() can indicate.
  8060.  *
  8061.  * Returns: When OK    => a positive character, 0 or greater.
  8062.  *        When EOF    => -2.
  8063.  *        When error    => -3, error code in errno.
  8064.  *
  8065.  * Older myread()s additionally returned -1 to indicate that there was nothing
  8066.  * to read, upon which the caller would call myread() again until it got
  8067.  * something.  The new myread()/mygetbuf() always gets something.  If it
  8068.  * doesn't, then make it do so!  Any program that actually depends on the old
  8069.  * behaviour will break.
  8070.  *
  8071.  * The older version also used to return -2 both for EOF and other errors,
  8072.  * and used to set errno to 9999 on EOF.  The errno stuff is gone, EOF and
  8073.  * other errors now return different results, although Kermit currently never
  8074.  * checks to see which it was.  It just disconnects in both cases.
  8075.  *
  8076.  * Kermit lets the user use the quit key to perform some special commands
  8077.  * during file transfer.  This causes read(), and thus also mygetbuf(), to
  8078.  * finish without reading anything and return the EINTR error.  This should
  8079.  * be checked by the caller.  Mygetbuf() could retry the read() on EINTR,
  8080.  * but if there is nothing to read, this could delay Kermit's reaction to
  8081.  * the command, and make Kermit appear unresponsive.
  8082.  *
  8083.  * The debug() call should be removed for optimum performance.
  8084.  */
  8085. int
  8086. mygetbuf() {
  8087.     int x;
  8088.     errno = 0;
  8089. #ifdef DEBUG
  8090.     if (deblog && my_count > 0)
  8091.       debug(F101,"mygetbuf IMPROPERLY CALLED with my_count","",my_count);
  8092. #endif /* DEBUG */
  8093.     if (my_count <= 0)
  8094.       my_count = myfillbuf();
  8095.  
  8096. #ifdef DEBUG
  8097. #ifdef COMMENT
  8098.     if (deblog) debug(F101, "mygetbuf read", "", my_count);
  8099. #else /* COMMENT */
  8100.     if (deblog) hexdump("mygetbuf read", mybuf, my_count);
  8101. #endif /* COMMENT */
  8102. #endif /* DEBUG */
  8103.     x = my_count;
  8104.     if (my_count <= 0) {
  8105.     my_count = 0;
  8106.     my_item = -1;
  8107.     debug(F101,"mygetbuf errno","",errno);
  8108. #ifdef TCPSOCKET
  8109.     if (netconn && ttnet == NET_TCPB && errno != 0) {
  8110.         if (errno != EINTR) {
  8111.         debug(F101,"mygetbuf TCP error","",errno);
  8112.         ttclos(0);        /* Close the connection. */
  8113.         }
  8114.         return(-3);
  8115.     }
  8116. #endif /* TCPSOCKET */
  8117.     if (!netconn && xlocal && errno) {
  8118.         if (errno != EINTR) {
  8119.         debug(F101,"mygetbuf SERIAL error","",errno);
  8120.         x = -3;
  8121.         ttclos(0);        /* Close the connection. */
  8122.         }
  8123.     }
  8124.     return((x < 0) ? -3 : -2);
  8125.     }
  8126.     --my_count;
  8127.     return((unsigned)(0xff & mybuf[my_item = 0]));
  8128. }
  8129.  
  8130. /* myfillbuf():
  8131.  * System-dependent read() into mybuf[], as many characters as possible.
  8132.  *
  8133.  * Returns: OK => number of characters read, always more than zero.
  8134.  *          EOF => 0
  8135.  *          Error => -1, error code in errno.
  8136.  *
  8137.  * If there is input available in the system's buffers, all of it should be
  8138.  * read into mybuf[] and the function return immediately.  If no input is
  8139.  * available, it should wait for a character to arrive, and return with that
  8140.  * one in mybuf[] as soon as possible.  It may wait somewhat past the first
  8141.  * character, but be aware that any such delay lengthens the packet turnaround
  8142.  * time during kermit file transfers.  Should never return with zero characters
  8143.  * unless EOF or irrecoverable read error.
  8144.  *
  8145.  * Correct functioning depends on the correct tty parameters being used.
  8146.  * Better control of current parameters is required than may have been the
  8147.  * case in older Kermit releases.  For instance, O_NDELAY (or equivalent) can
  8148.  * no longer be sometimes off and sometimes on like it used to, unless a
  8149.  * special myfillbuf() is written to handle that.  Otherwise the ordinary
  8150.  * myfillbuf()s may think they have come to EOF.
  8151.  *
  8152.  * If your system has a facility to directly perform the functioning of
  8153.  * myfillbuf(), then use it.  If the system can tell you how many characters
  8154.  * are available in its buffers, then read that amount (but not less than 1).
  8155.  * If the system can return a special indication when you try to read without
  8156.  * anything to read, while allowing you to read all there is when there is
  8157.  * something, you may loop until there is something to read, but probably that
  8158.  * is not good for the system load.
  8159.  */
  8160.  
  8161. #ifdef SVORPOSIX
  8162.     /* This is for System III/V with VMIN>0, VTIME=0 and O_NDELAY off,
  8163.      * and CLOCAL set any way you like.  This way, read() will do exactly
  8164.      * what is required by myfillbuf(): If there is data in the buffers
  8165.      * of the O.S., all available data is read into mybuf, up to the size
  8166.      * of mybuf.  If there is none, the first character to arrive is
  8167.      * awaited and returned.
  8168.      */
  8169. int
  8170. myfillbuf() {
  8171.     int fd, n;
  8172. #ifdef NETCMD
  8173.     if (ttpipe)
  8174.       fd = fdin;
  8175.     else
  8176. #endif /* NETCMD */
  8177.       fd = ttyfd;
  8178.  
  8179. #ifdef sxaE50
  8180.     /* From S. Dezawa at Fujifilm in Japan.  I don't know why this is */
  8181.     /* necessary for the sxa E50, but it is. */
  8182.     return read(fd, mybuf, 255);
  8183. #else
  8184. #ifdef BEOSORBEBOX
  8185.     while (1) {
  8186. #ifdef NETCONN
  8187.         if (netconn) {
  8188.             n = netxin(sizeof(mybuf), (char *)mybuf);
  8189.             debug(F101,"BEBOX SVORPOSIX network myfillbuf","",n);
  8190.     }
  8191.         else
  8192. #endif /* NETCONN */
  8193.       n = read(fd, mybuf, sizeof(mybuf));
  8194.     debug(F101,"BEBOX SVORPOSIX notnet myfillbuf","",n);
  8195.         if (n > 0)
  8196.       return(n);
  8197.         snooze(1000.0);
  8198.     }
  8199. #else /* BEOSORBEBOX */
  8200.     errno = 0;
  8201.     debug(F100,"SVORPOSIX myfillbuf calling read()","",0);
  8202. #ifdef IBMX25
  8203.     if (netconn && (nettype == NET_IX25)) {
  8204.     /* can't use sizeof because mybuf is a pointer, and not an array! */
  8205.     n = x25xin( MYBUFLEN, mybuf );
  8206.     } else
  8207. #endif /* IBMX25 */
  8208.  
  8209. #ifdef CK_SSL
  8210.       if (ssl_active_flag || tls_active_flag) {
  8211.       int error, n = 0;
  8212.       while (n == 0) {
  8213.           if (ssl_active_flag)
  8214.                 n = SSL_read(ssl_con, (char *)mybuf, sizeof(mybuf));
  8215.           else if (tls_active_flag)
  8216.                 n = SSL_read(tls_con, (char *)mybuf, sizeof(mybuf));
  8217.               else
  8218.         break;
  8219.           switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,n)) {
  8220.         case SSL_ERROR_NONE:
  8221.           if (n > 0)
  8222.                     return(n);
  8223.           if (n < 0)
  8224.                     return(-2);
  8225.           msleep(50);
  8226.           break;
  8227.         case SSL_ERROR_WANT_WRITE:
  8228.         case SSL_ERROR_WANT_READ:
  8229.           return(-1);
  8230.         case SSL_ERROR_SYSCALL:
  8231.           if (n != 0)
  8232.             return(-1);
  8233.         case SSL_ERROR_WANT_X509_LOOKUP:
  8234.         case SSL_ERROR_SSL:
  8235.         case SSL_ERROR_ZERO_RETURN:
  8236.         default:
  8237.           ttclos(0);
  8238.           return(-3);
  8239.             }
  8240.         }
  8241.     }
  8242. #endif /* CK_SSL */
  8243. #ifdef CK_KERBEROS
  8244. #ifdef KRB4
  8245. #ifdef RLOGCODE
  8246.     if (ttnproto == NP_EK4LOGIN) {
  8247.         if ((n = krb4_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8248.       return(-3);
  8249.         else
  8250.       return(n);
  8251.     }
  8252. #endif /* RLOGCODE */
  8253. #endif /* KRB4 */
  8254. #ifdef KRB5
  8255. #ifdef RLOGCODE
  8256.     if (ttnproto == NP_EK5LOGIN) {
  8257.         if ((n = krb5_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8258.       return(-3);
  8259.         else
  8260.       return(n);
  8261.     }
  8262. #endif /* RLOGCODE */
  8263. #ifdef KRB5_U2U
  8264.     if (ttnproto == NP_K5U2U) {
  8265.         if ((n = krb5_u2u_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8266.       return(-3);
  8267.         else
  8268.       return(n);
  8269.     }
  8270. #endif /* KRB5_U2U */
  8271. #endif /* KRB5 */
  8272. #endif /* CK_KERBEROS */
  8273.  
  8274. #ifdef NETPTY
  8275. #ifdef HAVE_PTYTRAP
  8276.     /* Special handling for HP-UX pty i/o */
  8277.   ptyread:
  8278.     if (ttpty && pty_trap_pending(ttyfd) > 0) {
  8279.         if (pty_trap_handler(ttyfd) > 0) {
  8280.             ttclos(0);
  8281.             return(-3);
  8282.         }
  8283.     }
  8284. #endif /* HAVE_PTYTRAP */
  8285. #endif /* NETPTY */
  8286.     n = read(fd, mybuf, sizeof(mybuf));
  8287.     debug(F101,"SVORPOSIX myfillbuf","",n);
  8288.     debug(F101,"SVORPOSIX myfillbuf ttcarr","",ttcarr);
  8289.     debug(F101,"SVORPOSIX myfillbuf errno","",errno);
  8290.     if (n < 1) {
  8291. #ifdef NETPTY
  8292. #ifdef HAVE_PTYTRAP
  8293.         /* When we have a PTY trap in place the connection cannot */
  8294.         /* be closed until the trap receives a close indication.  */
  8295.         if (n == 0 && ttpty)
  8296.             goto ptyread;
  8297. #endif /* HAVE_PTYTRAP */
  8298. #endif /* NETPTY */
  8299.         return(-3);
  8300.     }
  8301.     return(n);
  8302. #endif /* BEOSORBEBOX */
  8303. #endif /* sxaE50 */
  8304. }
  8305.  
  8306. #else /* not AT&T or POSIX */
  8307.  
  8308. #ifdef aegis
  8309.     /* This is quoted from the old myread().  The semantics seem to be
  8310.      * alright, but maybe errno would not need to be set even when
  8311.      * there is no error?  I don't know aegis.
  8312.      */
  8313. int
  8314. myfillbuf() {
  8315.     int count;
  8316. #ifdef NETCMD
  8317.     if (ttpipe)
  8318.       fd = fdin;
  8319.     else
  8320. #endif /* NETCMD */
  8321.       fd = ttyfd;
  8322.  
  8323.     count = ios_$get((short)fd, ios_$cond_opt, mybuf, 256L, st);
  8324.     errno = EIO;
  8325.     if (st.all == ios_$get_conditional_failed) /* get at least one */
  8326.       count = ios_$get((short)fd, 0, mybuf, 1L, st);
  8327.     if (st.all == ios_$end_of_file)
  8328.       return(-3);
  8329.     else if (st.all != status_$ok) {
  8330.     errno = EIO;
  8331.     return(-1);
  8332.     }
  8333.     return(count > 0 ? count : -3);
  8334. }
  8335. #else /* !aegis */
  8336.  
  8337. #ifdef FIONREAD
  8338.     /* This is for systems with FIONREAD.  FIONREAD returns the number
  8339.      * of characters available for reading. If none are available, wait
  8340.      * until something arrives, otherwise return all there is.
  8341.      */
  8342. int
  8343. myfillbuf() {
  8344.     PEEKTYPE avail = 0;
  8345.     int x, fd;
  8346. #ifdef NETCMD
  8347.     if (ttpipe)
  8348.       fd = fdin;
  8349.     else
  8350. #endif /* NETCMD */
  8351.       fd = ttyfd;
  8352.  
  8353. #ifdef SUNX25
  8354. /*
  8355.   SunLink X.25 support in this routine from Stefaan A. Eeckels, Eurostat (CEC).
  8356.   Depends on SunOS having FIONREAD, not because we use it, but just so this
  8357.   code is grouped correctly within the #ifdefs.  Let's hope Solaris keeps it.
  8358.  
  8359.   We call x25xin() instead of read() so that Q-Bit packets, which contain
  8360.   X.25 service-level information (e.g. PAD parameter changes), can be processed
  8361.   transparently to the upper-level code.  This is a blocking read, and so
  8362.   we depend on higher-level code (such as ttinc()) to set any necessary alarms.
  8363. */
  8364.     extern int nettype;
  8365.     if (netconn && nettype == NET_SX25) {
  8366.     while ((x = x25xin(sizeof(x25buf), x25buf)) < 1) ;
  8367.     return(x - 1);            /* "-1" compensates for extra status byte */
  8368.     }
  8369. #endif /* SUNX25 */
  8370.  
  8371. #ifdef CK_SSL
  8372.     if (ssl_active_flag || tls_active_flag) {
  8373.         int error, n = 0;
  8374.         while (n == 0) {
  8375.             if (ssl_active_flag)
  8376.           n = SSL_read(ssl_con, (char *)mybuf, sizeof(mybuf));
  8377.             else
  8378.           n = SSL_read(tls_con, (char *)mybuf, sizeof(mybuf));
  8379.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,n)) {
  8380.           case SSL_ERROR_NONE:
  8381.                 if (n > 0)
  8382.           return(n);
  8383.                 if (n < 0)
  8384.           return(-2);
  8385.                 msleep(50);
  8386.                 break;
  8387.           case SSL_ERROR_WANT_WRITE:
  8388.           case SSL_ERROR_WANT_READ:
  8389.                 return(-1);
  8390.           case SSL_ERROR_SYSCALL:
  8391.         if (n != 0)
  8392.           return(-1);
  8393.           case SSL_ERROR_WANT_X509_LOOKUP:
  8394.           case SSL_ERROR_SSL:
  8395.           case SSL_ERROR_ZERO_RETURN:
  8396.           default:
  8397.                 ttclos(0);
  8398.                 return(-2);
  8399.             }
  8400.         }
  8401.     }
  8402. #endif /* CK_SSL */
  8403. #ifdef CK_KERBEROS
  8404. #ifdef KRB4
  8405. #ifdef RLOGCODE
  8406.     if (ttnproto == NP_EK4LOGIN) {
  8407.         if ((x = krb4_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8408.       return(-1);
  8409.         else
  8410.       return(x);
  8411.     }
  8412. #endif /* RLOGCODE */
  8413. #endif /* KRB4 */
  8414. #ifdef KRB5
  8415. #ifdef RLOGCODE
  8416.     if (ttnproto == NP_EK5LOGIN) {
  8417.         if ((x = krb5_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8418.       return(-1);
  8419.         else
  8420.       return(x);
  8421.     }
  8422. #endif /* RLOGCODE */
  8423. #ifdef KRB5_U2U
  8424.     if (ttnproto == NP_K5U2U) {
  8425.         if ((x = krb5_u2u_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8426.       return(-1);
  8427.         else
  8428.       return(x);
  8429.     }
  8430. #endif /* KRB5_U2U */
  8431. #endif /* KRB5 */
  8432. #endif /* CK_KERBEROS */
  8433.  
  8434.     errno = 0;
  8435.     debug(F101,"myfillbuf calling FIONREAD ioctl","",xlocal);
  8436.     x = ioctl(fd, FIONREAD, &avail);
  8437. #ifdef DEBUG
  8438.     if (deblog) {
  8439.     debug(F101,"myfillbuf FIONREAD","",x);
  8440.     debug(F101,"myfillbuf FIONREAD avail","",avail);
  8441.     debug(F101,"myfillbuf FIONREAD errno","",errno);
  8442.     }
  8443. #endif /* DEBUG */
  8444.     if (x < 0 || avail == 0)
  8445.       avail = 1;
  8446.  
  8447.     if (avail > MYBUFLEN)
  8448.       avail = MYBUFLEN;
  8449.  
  8450.     errno = 0;
  8451.  
  8452.     x = read(fd, mybuf, (int) avail);
  8453. #ifdef DEBUG
  8454.     if (deblog) {
  8455.     debug(F101,"myfillbuf avail","",avail);
  8456.     debug(F101,"myfillbuf read","",x);
  8457.     debug(F101,"myfillbuf read errno","",errno);
  8458.         if (x > 0)
  8459.       hexdump("myfillbuf mybuf",mybuf,x);
  8460.     }
  8461. #endif /* DEBUG */
  8462.     if (x < 1) x = -3;            /* read 0 == connection loss */
  8463.     return(x);
  8464. }
  8465.  
  8466. #else /* !FIONREAD */
  8467. /* Add other systems here, between #ifdef and #else, e.g. NETCONN. */
  8468. /* When there is no other possibility, read 1 character at a time. */
  8469. int
  8470. myfillbuf() {
  8471.     int x;
  8472.  
  8473. #ifdef CK_SSL
  8474.     if (ssl_active_flag || tls_active_flag) {
  8475.         int error, n = 0;
  8476.         while (n == 0) {
  8477.             if (ssl_active_flag)
  8478.           n = SSL_read(ssl_con, (char *)mybuf, sizeof(mybuf));
  8479.             else
  8480.           count = SSL_read(tls_con, (char *)mybuf, sizeof(mybuf));
  8481.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,n)) {
  8482.           case SSL_ERROR_NONE:
  8483.                 if (n > 0)
  8484.           return(n);
  8485.                 if (n < 0)
  8486.           return(-2);
  8487.                 msleep(50);
  8488.                 break;
  8489.           case SSL_ERROR_WANT_WRITE:
  8490.           case SSL_ERROR_WANT_READ:
  8491.                 return(-1);
  8492.           case SSL_ERROR_SYSCALL:
  8493.         if (n != 0)
  8494.           return(-1);
  8495.           case SSL_ERROR_WANT_X509_LOOKUP:
  8496.           case SSL_ERROR_SSL:
  8497.           case SSL_ERROR_ZERO_RETURN:
  8498.           default:
  8499.                 ttclos(0);
  8500.                 return(-2);
  8501.             }
  8502.         }
  8503.     }
  8504. #endif /* CK_SSL */
  8505. #ifdef CK_KERBEROS
  8506. #ifdef KRB4
  8507. #ifdef RLOGCODE
  8508.     if (ttnproto == NP_EK4LOGIN) {
  8509.         if ((len = krb4_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8510.       return(-1);
  8511.         else
  8512.       return(len);
  8513.     }
  8514. #endif /* RLOGCODE */
  8515. #endif /* KRB4 */
  8516. #ifdef KRB5
  8517. #ifdef RLOGCODE
  8518.     if (ttnproto == NP_EK5LOGIN) {
  8519.         if ((len = krb5_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8520.       return(-1);
  8521.         else
  8522.       return(len);
  8523.     }
  8524. #endif /* RLOGCODE */
  8525. #ifdef KRB5_U2U
  8526.     if (ttnproto == NP_K5U2U) {
  8527.         if ((len = krb5_u2u_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8528.       return(-1);
  8529.         else
  8530.       return(len);
  8531.     }
  8532. #endif /* KRB5_U2U */
  8533. #endif /* KRB5 */
  8534. #endif /* CK_KERBEROS */
  8535.  
  8536. #ifdef NETCMD
  8537.     if (ttpipe)
  8538.       fd = fdin;
  8539.     else
  8540. #endif /* NETCMD */
  8541.       fd = ttyfd;
  8542.     x = read(fd, mybuf, 1);
  8543.     return(x > 0 ? x : -3);
  8544. }
  8545.  
  8546. #endif /* !FIONREAD */
  8547. #endif /* !aegis */
  8548. #endif /* !ATTSV */
  8549.  
  8550. #endif /* MYREAD */
  8551.  
  8552. #ifdef MINIX2
  8553. #undef MINIX
  8554. #endif /* MINIX2 */
  8555.  
  8556. /*  T T _ T N O P T  --  Handle Telnet negotions in incoming data */
  8557. /*
  8558.   Call with the IAC that was encountered.
  8559.   Returns:
  8560.    -3: If connection has dropped or gone bad.
  8561.    -2: On Telnet protocol error resulting in inconsistent states.
  8562.     0: If negotiation OK and caller has nothing to do.
  8563.     1: If packet start character has changed (new value is in global stchr).
  8564.   255: If there was a quoted IAC as data.
  8565.    or: Not at all if we got a legitimate Telnet Logout request.
  8566. */
  8567. #ifdef TCPSOCKET
  8568. static int
  8569. tt_tnopt(n) int n; {            /* Handle Telnet options */
  8570.     /* In case caller did not already check these conditions...  */
  8571.     if (n == IAC &&
  8572.     ((xlocal && netconn && (ttnproto == NP_TELNET)) ||
  8573.      (!xlocal && sstelnet))) {
  8574.     extern int duplex;
  8575.     extern int server;
  8576.     int tx = 0;
  8577.     debug(F100,"ttinl calling tn_doop()","",0);
  8578.     tx = tn_doop((CHAR)(n & 0xff),duplex,ttinc);
  8579.     debug(F111,"ttinl tn_doop() returned","tx",tx);
  8580.     switch (tx) {
  8581.       case 0:
  8582.         return(0);
  8583.       case -1:            /* I/O error */
  8584.         ttimoff();            /* Turn off timer */
  8585.         return(-3);
  8586.           case -2:            /* Connection failed. */
  8587.           case -3:
  8588.         ttimoff();            /* Turn off timer */
  8589.         ttclos(0);
  8590.         return(-3);
  8591.       case 1:            /* ECHO change */
  8592.         duplex = 1;
  8593.         return(0);
  8594.       case 2:            /* ECHO change */
  8595.         duplex = 0;
  8596.         return(0);
  8597.       case 3:            /* Quoted IAC */
  8598.         n = 255;
  8599.         return((unsigned)255);
  8600. #ifdef IKS_OPTION
  8601.       case 4: {
  8602.           if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start && server
  8603. #ifdef IKSD
  8604.           && !inserver
  8605. #endif /* IKSD */
  8606.           ) {            /* Remote in Server mode */
  8607.           ttimoff();        /* Turn off timer */
  8608.           debug(F100,"u_start and !inserver","",0);
  8609.           return(-2);        /* End server mode */
  8610.           } else if (!TELOPT_SB(TELOPT_KERMIT).kermit.me_start &&
  8611.              server
  8612.              ) {        /* I'm no longer in Server Mode */
  8613.           debug(F100,"me_start and server","",0);
  8614.           ttimoff();
  8615.           return(-2);
  8616.           }
  8617.           return(0);
  8618.       }
  8619.       case 5: {            /* Start character change */
  8620.           /* extern CHAR stchr; */
  8621.           /* start = stchr; */
  8622.           return(1);
  8623.       }
  8624. #endif /* IKS_OPTION */
  8625.       case 6:            /* Remote Logout */
  8626.         ttimoff();
  8627.         ttclos(0);
  8628. #ifdef IKSD
  8629.         if (inserver && !local)
  8630.           doexit(GOOD_EXIT,0);
  8631.         else
  8632. #endif /* IKSD */
  8633.           return(-2);
  8634.       default:
  8635.         return(0);
  8636.     }
  8637.     } else
  8638.       return(0);
  8639. }
  8640. #endif /* TCPSOCKET */
  8641.  
  8642. /*  T T F L U I  --  Flush tty input buffer */
  8643.  
  8644. void
  8645. ttflux() {                /* But first... */
  8646. #ifdef MYREAD
  8647. /*
  8648.   Flush internal MYREAD buffer.
  8649. */
  8650. #ifdef TCPSOCKET
  8651.     int dotnopts, x;
  8652.     dotnopts = (((xlocal && netconn && (ttnproto == NP_TELNET)) ||
  8653.          (!xlocal && sstelnet)));
  8654. #endif /* TCPSOCKET */
  8655.     debug(F101,"ttflux my_count","",my_count);
  8656. #ifdef TCPSOCKET
  8657.     if (dotnopts) {
  8658.     CHAR ch = '\0';
  8659.         while (my_count > 0) {
  8660.         ch = myread();
  8661. #ifdef CK_ENCRYPTION
  8662.             if (TELOPT_U(TELOPT_ENCRYPTION))
  8663.           ck_tn_decrypt(&ch,1);
  8664. #endif /* CK_ENCRYPTION */
  8665.             if (ch == IAC)
  8666.           x = tt_tnopt(ch);
  8667.         }
  8668.     } else
  8669. #endif /* TCPSOCKET */
  8670. #ifdef COMMENT
  8671. #ifdef CK_ENCRYPTION
  8672.     if (TELOPT_U(TELOPT_ENCRYPTION) && my_count > 0)
  8673.       ck_tn_decrypt(&mybuf[my_item+1],my_count);
  8674. #endif /* CK_ENCRYPTION */
  8675. #endif /* COMMENT */
  8676.     my_count = 0;            /* Reset count to zero */
  8677.     my_item = -1;            /* And buffer index to -1 */
  8678. #endif /* MYREAD */
  8679. }
  8680.  
  8681. int
  8682. ttflui() {
  8683.     int n, fd;
  8684. #ifdef TCPSOCKET
  8685.     int dotnopts;
  8686.     dotnopts = (((xlocal && netconn && (ttnproto == NP_TELNET)) ||
  8687.          (!xlocal && sstelnet)));
  8688. #endif /* TCPSOCKET */
  8689.  
  8690. #ifdef NETCMD
  8691.     if (ttpipe)
  8692.       fd = fdin;
  8693.     else
  8694. #endif /* NETCMD */
  8695.       fd = ttyfd;
  8696.  
  8697. #ifdef TTLEBUF
  8698.     ttpush = -1;            /* Clear the peek-ahead char */
  8699.     while (le_data && (le_inbuf() > 0)) {
  8700.         CHAR ch = '\0';
  8701.         if (le_getchar(&ch) > 0) {    /* Clear any more... */
  8702.             debug(F101,"ttflui le_inbuf ch","",ch);
  8703.         }
  8704.     }
  8705. #endif /* TTLEBUF */
  8706.     debug(F101,"ttflui ttpipe","",ttpipe);
  8707.  
  8708. #ifdef MYREAD
  8709. /*
  8710.   Flush internal MYREAD buffer *NEXT*, in all cases.
  8711. */
  8712.     ttflux();
  8713. #endif /* MYREAD */
  8714.  
  8715. #ifdef NETCONN
  8716. /* Network flush is done specially, in the network support module. */
  8717.     if ((netconn || sstelnet) && !ttpipe && !ttpty) {
  8718.     debug(F100,"ttflui netflui","",0);
  8719. #ifdef TN_COMPORT
  8720.     if (istncomport())
  8721.       tnc_send_purge_data(TNC_PURGE_RECEIVE);
  8722. #endif /* TN_COMPORT */
  8723.     return(netflui());
  8724.     }
  8725. #endif /* NETCONN */
  8726.  
  8727.     debug(F101,"ttflui ttyfd","",ttyfd); /* Not network */
  8728.     if (ttyfd < 0)
  8729.       return(-1);
  8730.  
  8731. #ifdef aegis
  8732.     sio_$control((short)yfd, sio_$flush_in, true, st);
  8733.     if (st.all != status_$ok) {
  8734.     fprintf(stderr, "flush failed: "); error_$print(st);
  8735.     } else {      /* sometimes the flush doesn't work */
  8736.         for (;;) {
  8737.         char buf[256];
  8738.             /* eat all the characters that shouldn't be available */
  8739.             ios_$get((short)fd, ios_$cond_opt, buf, 256L, st); /* (void) */
  8740.             if (st.all == ios_$get_conditional_failed) break;
  8741.             fprintf(stderr, "flush failed(2): "); error_$print(st);
  8742.         }
  8743.     }
  8744. #else
  8745. #ifdef BSD44                /* 4.4 BSD */
  8746.     n = FREAD;                          /* Specify read queue */
  8747.     debug(F100,"ttflui BSD44","",0);
  8748.     ioctl(fd,TIOCFLUSH,&n);
  8749. #else
  8750. #ifdef Plan9
  8751. #undef POSIX                /* Uh oh... */
  8752. #endif /* Plan9 */
  8753. #ifdef POSIX                /* POSIX */
  8754.     debug(F100,"ttflui POSIX","",0);
  8755.     tcflush(fd,TCIFLUSH);
  8756. #else
  8757. #ifdef ATTSV                /* System V */
  8758. #ifndef VXVE
  8759.     debug(F100,"ttflui ATTSV","",0);
  8760.     ioctl(fd,TCFLSH,0);
  8761. #endif /* VXVE */
  8762. #else                    /* Not BSD44, POSIX, or Sys V */
  8763. #ifdef TIOCFLUSH            /* Those with TIOCFLUSH defined */
  8764. #ifdef ANYBSD                /* Berkeley */
  8765.     n = FREAD;                          /* Specify read queue */
  8766.     debug(F100,"ttflui TIOCFLUSH ANYBSD","",0);
  8767.     ioctl(fd,TIOCFLUSH,&n);
  8768. #else                    /* Others (V7, etc) */
  8769.     debug(F100,"ttflui TIOCFLUSH","",0);
  8770.     ioctl(fd,TIOCFLUSH,0);
  8771. #endif /* ANYBSD */
  8772. #else                    /* All others... */
  8773. /*
  8774.   No system call (that we know about) for input buffer flushing.
  8775.   So see how many there are and read them in a loop, using ttinc().
  8776.   ttinc() is buffered, so we're not getting charged with a system call
  8777.   per character, just a function call.
  8778. */
  8779.     if ((n = ttchk()) > 0) {
  8780.     debug(F101,"ttflui read loop","",n);
  8781.     while ((n--) && ttinc(0) > 0) ;
  8782.     }
  8783. #endif /* TIOCFLUSH */
  8784. #endif /* ATTSV */
  8785. #endif /* POSIX */
  8786. #ifdef Plan9
  8787. #define POSIX
  8788. #endif /* Plan9 */
  8789. #endif /* BSD44 */
  8790. #endif /* aegis */
  8791.     return(0);
  8792. }
  8793.  
  8794. int
  8795. ttfluo() {                /* Flush output buffer */
  8796.     int fd;
  8797. #ifdef NETCMD
  8798.     if (ttpipe)
  8799.       fd = fdout;
  8800.     else
  8801. #endif /* NETCMD */
  8802.       fd = ttyfd;
  8803.  
  8804. #ifdef Plan9
  8805.     return 0;
  8806. #else
  8807. #ifdef POSIX
  8808.     return(tcflush(fd,TCOFLUSH));
  8809. #else
  8810. #ifdef OXOS
  8811.     return(ioctl(fd,TCFLSH,1));
  8812. #else
  8813.     return(0);                /* All others, nothing */
  8814. #endif /* OXOS */
  8815. #endif /* POSIX */
  8816. #endif /* Plan9 */
  8817. }
  8818.  
  8819. /* Interrupt Functions */
  8820.  
  8821. /* Set up terminal interrupts on console terminal */
  8822.  
  8823. #ifndef FIONREAD            /* We don't need esctrp() */
  8824. #ifndef SELECT                /* if we have any of these... */
  8825. #ifndef CK_POLL
  8826. #ifndef RDCHK
  8827.  
  8828. #ifndef OXOS
  8829. #ifdef SVORPOSIX
  8830. SIGTYP
  8831. esctrp(foo) int foo; {            /* trap console escapes (^\) */
  8832.     signal(SIGQUIT,SIG_IGN);            /* ignore until trapped */
  8833.     conesc = 1;
  8834.     debug(F101,"esctrp caught SIGQUIT","",conesc);
  8835. }
  8836. #endif /* SVORPOSIX */
  8837. #endif /* OXOS */
  8838.  
  8839. #ifdef V7
  8840. #ifndef MINIX2
  8841. SIGTYP
  8842. esctrp(foo) int foo; {            /* trap console escapes (^\) */
  8843.     signal(SIGQUIT,SIG_IGN);            /* ignore until trapped */
  8844.     conesc = 1;
  8845.     debug(F101,"esctrp caught SIGQUIT","",conesc);
  8846. }
  8847. #endif /* MINIX2 */
  8848. #endif /* V7 */
  8849.  
  8850. #ifdef C70
  8851. SIGTYP
  8852. esctrp(foo) int foo; {            /* trap console escapes (^\) */
  8853.     conesc = 1;
  8854.     signal(SIGQUIT,SIG_IGN);            /* ignore until trapped */
  8855. }
  8856. #endif /* C70 */
  8857.  
  8858. #endif /* RDCHK */
  8859. #endif /* CK_POLL */
  8860. #endif /* SELECT */
  8861. #endif /* FIONREAD */
  8862.  
  8863. /*  C O N B G T  --  Background Test  */
  8864.  
  8865. static int jc = 0;            /* 0 = no job control */
  8866.  
  8867. /*
  8868.   Call with flag == 1 to prevent signal test, which can not be expected
  8869.   to work during file transfer, when SIGINT probably *is* set to SIG_IGN.
  8870.  
  8871.   Call with flag == 0 to use the signal test, but only if the process-group
  8872.   test fails, as it does on some UNIX systems, where getpgrp() is buggy,
  8873.   requires an argument when the man page says it doesn't, or vice versa.
  8874.  
  8875.   If flag == 0 and the process-group test fails, then we determine background
  8876.   status simply (but not necessarily reliably) from isatty().
  8877.  
  8878.   conbgt() sets the global backgrd = 1 if we appear to be in the background,
  8879.   and to 0 if we seem to be in the foreground.  conbgt() is highly prone to
  8880.   misbehavior.
  8881. */
  8882. VOID
  8883. conbgt(flag) int flag; {
  8884.     int x = -1,                /* process group or SIGINT test */
  8885.         y = 0;                /* isatty() test */
  8886. /*
  8887.   Check for background operation, even if not running on real tty, so that
  8888.   background flag can be set correctly.  If background status is detected,
  8889.   then Kermit will not issue its interactive prompt or most messages.
  8890.   If your prompt goes away, you can blame (and fix?) this function.
  8891. */
  8892.  
  8893. /* Use process-group test if possible. */
  8894.  
  8895. #ifdef POSIX                /* We can do it in POSIX */
  8896. #define PGROUP_T
  8897. #else
  8898. #ifdef BSD4                /* and in BSD 4.x. */
  8899. #define PGROUP_T
  8900. #else
  8901. #ifdef HPUXJOBCTL            /* and in most HP-UX's */
  8902. #define PGROUP_T
  8903. #else
  8904. #ifdef TIOCGPGRP            /* and anyplace that has this ioctl. */
  8905. #define PGROUP_T
  8906. #endif /* TIOCGPGRP */
  8907. #endif /* HPUXJOBCTL */
  8908. #endif /* BSD4 */
  8909. #endif /* POSIX */
  8910.  
  8911. #ifdef MIPS                /* Except if it doesn't work... */
  8912. #undef PGROUP_T
  8913. #endif /* MIPS */
  8914.  
  8915. #ifdef PGROUP_T
  8916. /*
  8917.   Semi-reliable process-group test.  Check whether this process's group is
  8918.   the same as the controlling terminal's process group.  This works if the
  8919.   getpgrp() call doesn't lie (as it does in the SUNOS System V environment).
  8920. */
  8921.     PID_T mypgrp = (PID_T)0;        /* Kermit's process group */
  8922.     PID_T ctpgrp = (PID_T)0;        /* The terminal's process group */
  8923. #ifndef _POSIX_SOURCE
  8924. /*
  8925.   The getpgrp() prototype is obtained from system header files for POSIX
  8926.   and Sys V R4 compilations.  Other systems, who knows.  Some complain about
  8927.   a duplicate declaration here, others don't, so it's safer to leave it in
  8928.   if we don't know for certain.
  8929. */
  8930. #ifndef SVR4
  8931. #ifndef PS2AIX10
  8932. #ifndef HPUX9
  8933.     extern PID_T getpgrp();
  8934. #endif /* HPUX9 */
  8935. #endif /* PS2AIX10 */
  8936. #endif /* SVR4 */
  8937. #endif /* _POSIX_SOURCE */
  8938.  
  8939. /* Get my process group. */
  8940.  
  8941. #ifdef SVR3 /* Maybe this should be ATTSV? */
  8942. /* This function is not described in SVID R2 */
  8943.     mypgrp = getpgrp();
  8944.     /* debug(F101,"ATTSV conbgt process group","",(int) mypgrp); */
  8945. #else
  8946. #ifdef POSIX
  8947.     mypgrp = getpgrp();
  8948.     /* debug(F101,"POSIX conbgt process group","",(int) mypgrp); */
  8949. #else
  8950. #ifdef OSFPC
  8951.     mypgrp = getpgrp();
  8952.     /* debug(F101,"OSF conbgt process group","",(int) mypgrp); */
  8953. #else
  8954. #ifdef QNX
  8955.     mypgrp = getpgrp();
  8956.     /* debug(F101,"QNX conbgt process group","",(int) mypgrp); */
  8957. #else
  8958. #ifdef OSF32                /* (was OSF40) */
  8959.     mypgrp = getpgrp();
  8960.     /* debug(F101,"Digital UNIX conbgt process group","",(int) mypgrp); */
  8961. #else /* BSD, V7, etc */
  8962. #ifdef MINIX2
  8963.     mypgrp = getpgrp();
  8964. #else
  8965.     mypgrp = getpgrp(0);
  8966. #endif /* MINIX2 */
  8967.     /* debug(F101,"BSD conbgt process group","",(int) mypgrp); */
  8968. #endif /* OSF32 */
  8969. #endif /* QNX */
  8970. #endif /* OSFPC */
  8971. #endif /* POSIX */
  8972. #endif /* SVR3 */
  8973.  
  8974. #ifdef MINIX2
  8975. #undef BSD44ORPOSIX
  8976. #endif /* MINIX2 */
  8977.  
  8978. /* Now get controlling tty's process group */
  8979. #ifdef BSD44ORPOSIX
  8980.     ctpgrp = tcgetpgrp(1);        /* The POSIX way */
  8981.     /* debug(F101,"POSIX conbgt terminal process group","",(int) ctpgrp); */
  8982. #else
  8983.     ioctl(1, TIOCGPGRP, &ctpgrp);    /* Or the BSD way */
  8984.    /* debug(F101,"non-POSIX conbgt terminal process group","",(int) ctpgrp); */
  8985. #endif /* BSD44ORPOSIX */
  8986.  
  8987. #ifdef MINIX2
  8988. #define BSD44ORPOSIX
  8989. #endif /* MINIX2 */
  8990.  
  8991.     if ((mypgrp > (PID_T) 0) && (ctpgrp > (PID_T) 0))
  8992.       x = (mypgrp == ctpgrp) ? 0 : 1;    /* If they differ, then background. */
  8993.     else x = -1;            /* If error, remember. */
  8994.     debug(F101,"conbgt process group test","",x);
  8995. #endif /* PGROUP_T */
  8996.  
  8997. /* Try to see if job control is available */
  8998.  
  8999. #ifdef NOJC                /* User override */
  9000.     jc = 0;                /* No job control allowed */
  9001.     debug(F111,"NOJC","jc",jc);
  9002. #else
  9003. #ifdef BSD44
  9004.     jc = 1;
  9005. #else
  9006. #ifdef SVR4ORPOSIX            /* POSIX actually tells us */
  9007.     debug(F100,"SVR4ORPOSIX jc test...","",0);
  9008. #ifdef _SC_JOB_CONTROL
  9009. #ifdef __bsdi__
  9010.     jc = 1;
  9011. #else
  9012. #ifdef __386BSD__
  9013.     jc = 1;
  9014. #else
  9015.     jc = sysconf(_SC_JOB_CONTROL);    /* Whatever system says */
  9016.     if (jc < 0) {
  9017.     debug(F101,"sysconf fails, jcshell","",jcshell);
  9018.     jc = (jchdlr == SIG_DFL) ? 1 : 0;
  9019.     } else
  9020.       debug(F111,"sysconf(_SC_JOB_CONTROL)","jc",jc);
  9021. #endif /* __386BSD__ */
  9022. #endif /* __bsdi__ */
  9023. #else
  9024. #ifdef _POSIX_JOB_CONTROL
  9025.     jc = 1;                /* By definition */
  9026.     debug(F111,"_POSIX_JOB_CONTROL is defined","jc",jc);
  9027. #else
  9028.     jc = 0;                /* Assume job control not allowed */
  9029.     debug(F111,"SVR4ORPOSIX _SC/POSIX_JOB_CONTROL not defined","jc",jc);
  9030. #endif /* _POSIX_JOB_CONTROL */
  9031. #endif /* _SC_JOB_CONTROL */
  9032. #else
  9033. #ifdef BSD4
  9034.     jc = 1;                /* Job control allowed */
  9035.     debug(F111,"BSD job control","jc",jc);
  9036. #else
  9037. #ifdef SVR3JC
  9038.     jc = 1;                /* JC allowed */
  9039.     debug(F111,"SVR3 job control","jc",jc);
  9040. #else
  9041. #ifdef OXOS
  9042.     jc = 1;                /* JC allowed */
  9043.     debug(F111,"X/OS job control","jc",jc);
  9044. #else
  9045. #ifdef HPUX9
  9046.     jc = 1;                /* JC allowed */
  9047.     debug(F111,"HP-UX 9.0 job control","jc",jc);
  9048. #else
  9049. #ifdef HPUX10
  9050.     jc = 1;                /* JC allowed */
  9051.     debug(F111,"HP-UX 10.0 job control","jc",jc);
  9052. #else
  9053.     jc = 0;                /* JC not allowed */
  9054.     debug(F111,"job control catch-all","jc",jc);
  9055. #endif /* HPUX10 */
  9056. #endif /* HPUX9 */
  9057. #endif /* OXOS */
  9058. #endif /* SVR3JC */
  9059. #endif /* BSD4 */
  9060. #endif /* SVR4ORPOSIX */
  9061. #endif /* BSD44 */
  9062. #endif /* NOJC */
  9063.     debug(F101,"conbgt jc","",jc);
  9064. #ifndef NOJC
  9065.     debug(F101,"conbgt jcshell","",jcshell);
  9066. /*
  9067.   At this point, if jc == 1 but jcshell == 0, it means that the OS supports
  9068.   job control, but the shell or other process we are running under does not
  9069.   (jcshell is set in sysinit()) and so if we suspend ourselves, nothing good
  9070.   will come of it.  So...
  9071. */
  9072.     if (jc < 0) jc = 0;
  9073.     if (jc > 0 && jcshell == 0) jc = 0;
  9074. #endif /* NOJC */
  9075.  
  9076. /*
  9077.   Another background test.
  9078.   Test if SIGINT (terminal interrupt) is set to SIG_IGN (ignore),
  9079.   which is done by the shell (sh) if the program is started with '&'.
  9080.   Unfortunately, this is NOT done by csh or ksh so watch out!
  9081.   Note, it's safe to set SIGINT to SIG_IGN here, because further down
  9082.   we always set it to something else.
  9083.   Note: as of 16 Jul 1999, we also skip this test if we set SIGINT to
  9084.   SIG_IGN ourselves.
  9085. */
  9086.     if (x < 0 && !flag && !sigint_ign) { /* Didn't get good results above... */
  9087.  
  9088.     SIGTYP (*osigint)();
  9089.  
  9090.     osigint = signal(SIGINT,SIG_IGN);    /* What is SIGINT set to? */
  9091.     sigint_ign = 1;
  9092.     x = (osigint == SIG_IGN) ? 1 : 0;    /* SIG_IGN? */
  9093.     debug(F101,"conbgt osigint","",osigint);
  9094.     debug(F101,"conbgt signal test","",x);
  9095.     }
  9096.  
  9097. /* Also check to see if we're running with redirected stdio. */
  9098. /* This is not really background operation, but we want to act as though */
  9099. /* it were. */
  9100.  
  9101. #ifdef IKSD
  9102.     if (inserver) {            /* Internet Kermit Server */
  9103.     backgrd = 0;            /* is not in the background */
  9104.     return;
  9105.     }
  9106. #endif /* IKSD */
  9107.  
  9108.     y = (isatty(0) && isatty(1)) ? 1 : 0;
  9109.     debug(F101,"conbgt isatty test","",y);
  9110.  
  9111. #ifdef BSD29
  9112. /* The process group and/or signal test doesn't work under these... */
  9113.     backgrd = !y;
  9114. #else
  9115. #ifdef sxaE50
  9116.     backgrd = !y;
  9117. #else
  9118. #ifdef MINIX
  9119.     backgrd = !y;
  9120. #else
  9121. #ifdef MINIX2
  9122.     backgrd = !y;
  9123. #else
  9124.     if (x > -1)
  9125.       backgrd = (x || !y) ? 1 : 0;
  9126.     else backgrd = !y;
  9127. #endif /* BSD29 */
  9128. #endif /* sxaE50 */
  9129. #endif /* MINIX */
  9130. #endif /* MINIX2 */
  9131.     debug(F101,"conbgt backgrd","",backgrd);
  9132. }
  9133.  
  9134. /*  C O N I N T  --  Console Interrupt setter  */
  9135.  
  9136. /*
  9137.   First arg is pointer to function to handle SIGTERM & SIGINT (like Ctrl-C).
  9138.   Second arg is pointer to function to handle SIGTSTP (suspend).
  9139. */
  9140.  
  9141. VOID                    /* Set terminal interrupt traps. */
  9142. #ifdef CK_ANSIC
  9143. #ifdef apollo
  9144. conint(f,s) SIGTYP (*f)(), (*s)();
  9145. #else
  9146. conint(SIGTYP (*f)(int), SIGTYP (*s)(int))
  9147. #endif /* apollo */
  9148. #else
  9149. conint(f,s) SIGTYP (*f)(), (*s)();
  9150. #endif /* CK_ANSIC */
  9151. /* conint */ {
  9152.  
  9153.     debug(F101,"conint conistate","",conistate);
  9154.  
  9155.     conbgt(0);                /* Do background test. */
  9156.  
  9157. /* Set the desired handlers for hangup and software termination. */
  9158.  
  9159. #ifdef SIGTERM
  9160.     signal(SIGTERM,f);                  /* Software termination */
  9161. #endif /* SIGTERM */
  9162.  
  9163. /*
  9164.   Prior to July 1999 we used to call sighup() here but now it's called in
  9165.   sysinit() so SIGHUP can be caught during execution of the init file or
  9166.   a kerbang script.
  9167. */
  9168.  
  9169. /* Now handle keyboard stop, quit, and interrupt signals. */
  9170. /* Check if invoked in background -- if so signals set to be ignored. */
  9171. /* However, if running under a job control shell, don't ignore them. */
  9172. /* We won't be getting any, as we aren't in the terminal's process group. */
  9173.  
  9174.     debug(F101,"conint backgrd","",backgrd);
  9175.     debug(F101,"conint jc","",jc);
  9176.  
  9177.     if (backgrd && !jc) {        /* In background, ignore signals */
  9178.     debug(F101,"conint background ignoring signals, jc","",jc);
  9179. #ifdef SIGTSTP
  9180.         signal(SIGTSTP,SIG_IGN);        /* Keyboard stop */
  9181. #endif /* SIGTSTP */
  9182.         signal(SIGQUIT,SIG_IGN);        /* Keyboard quit */
  9183.         signal(SIGINT,SIG_IGN);         /* Keyboard interrupt */
  9184.     sigint_ign = 1;
  9185.     conistate = CONI_NOI;
  9186.     } else {                /* Else in foreground or suspended */
  9187.     debug(F101,"conint foreground catching signals, jc","",jc);
  9188.         signal(SIGINT,f);               /* Catch terminal interrupt */
  9189.     sigint_ign = (f == SIG_IGN) ? 1 : 0;
  9190.  
  9191. #ifdef SIGTSTP                /* Keyboard stop (suspend) */
  9192.     /* debug(F101,"conint SIGSTSTP","",s); */
  9193.     if (s == NULL) s = SIG_DFL;
  9194. #ifdef NOJC                /* No job control allowed. */
  9195.     signal(SIGTSTP,SIG_IGN);
  9196. #else                    /* Job control allowed */
  9197.     if (jc)                /* if available. */
  9198.       signal(SIGTSTP,s);
  9199.     else
  9200.       signal(SIGTSTP,SIG_IGN);
  9201. #endif /* NOJC */
  9202. #endif /* SIGTSTP */
  9203.  
  9204. #ifndef OXOS
  9205. #ifdef SVORPOSIX
  9206. #ifndef FIONREAD            /* Watch out, we don't know this... */
  9207. #ifndef SELECT
  9208. #ifndef CK_POLL
  9209. #ifndef RDCHK
  9210.         signal(SIGQUIT,esctrp);         /* Quit signal, Sys III/V. */
  9211. #endif /* RDCHK */
  9212. #endif /* CK_POLL */
  9213. #endif /* SELECT */
  9214. #endif /* FIONREAD */
  9215.         if (conesc) conesc = 0;         /* Clear out pending escapes */
  9216. #else
  9217. #ifdef V7
  9218.         signal(SIGQUIT,esctrp);         /* V7 like Sys III/V */
  9219.         if (conesc) conesc = 0;
  9220. #else
  9221. #ifdef aegis
  9222.         signal(SIGQUIT,f);              /* Apollo, catch it like others. */
  9223. #else
  9224.         signal(SIGQUIT,SIG_IGN);        /* Others, ignore like 4D & earlier. */
  9225. #endif /* aegis */
  9226. #endif /* V7 */
  9227. #endif /* SVORPOSIX */
  9228. #endif /* OXOS */
  9229.     conistate = CONI_INT;
  9230.     }
  9231. }
  9232.  
  9233.  
  9234. /*  C O N N O I  --  Reset console terminal interrupts */
  9235.  
  9236. VOID
  9237. connoi() {                              /* Console-no-interrupts */
  9238.  
  9239.     debug(F101,"connoi conistate","",conistate);
  9240. #ifdef SIGTSTP
  9241.     signal(SIGTSTP,SIG_IGN);        /* Suspend */
  9242. #endif /* SIGTSTP */
  9243.     conint(SIG_IGN,SIG_IGN);        /* Interrupt */
  9244.     sigint_ign = 1;            /* Remember we did this ourselves */
  9245. #ifdef SIGQUIT
  9246.     signal(SIGQUIT,SIG_IGN);        /* Quit */
  9247. #endif /* SIGQUIT */
  9248. #ifdef SIGTERM
  9249.     signal(SIGTERM,SIG_IGN);        /* Term */
  9250. #endif /* SIGTERM */
  9251.     conistate = CONI_NOI;
  9252. }
  9253.  
  9254. /*  I N I T R A W Q  --  Set up to read /dev/kmem for character count.  */
  9255.  
  9256. #ifdef  V7
  9257. /*
  9258.  Used in Version 7 to simulate Berkeley's FIONREAD ioctl call.  This
  9259.  eliminates blocking on a read, because we can read /dev/kmem to get the
  9260.  number of characters available for raw input.  If your system can't
  9261.  or you won't let the world read /dev/kmem then you must figure out a
  9262.  different way to do the counting of characters available, or else replace
  9263.  this by a dummy function that always returns 0.
  9264. */
  9265. /*
  9266.  * Call this routine as: initrawq(tty)
  9267.  * where tty is the file descriptor of a terminal.  It will return
  9268.  * (as a char *) the kernel-mode memory address of the rawq character
  9269.  * count, which may then be read.  It has the side-effect of flushing
  9270.  * input on the terminal.
  9271.  */
  9272. /*
  9273.  * John Mackin, Physiology Dept., University of Sydney (Australia)
  9274.  * ...!decvax!mulga!physiol.su.oz!john
  9275.  *
  9276.  * Permission is hereby granted to do anything with this code, as
  9277.  * long as this comment is retained unmodified and no commercial
  9278.  * advantage is gained.
  9279.  */
  9280. #ifndef MINIX
  9281. #ifndef MINIX2
  9282. #ifndef COHERENT
  9283. #include <a.out.h>
  9284. #include <sys/proc.h>
  9285. #endif /* COHERENT */
  9286. #endif /* MINIX2 */
  9287. #endif /* MINIX */
  9288.  
  9289. #ifdef COHERENT
  9290. #include <l.out.h>
  9291. #include <sys/proc.h>
  9292. #endif /* COHERENT */
  9293.  
  9294. char *
  9295. initrawq(tty) int tty; {
  9296. #ifdef MINIX
  9297.     return(0);
  9298. #else
  9299. #ifdef MINIX2
  9300.     return(0);
  9301. #else
  9302. #ifdef UTS24
  9303.     return(0);
  9304. #else
  9305. #ifdef BSD29
  9306.     return(0);
  9307. #else
  9308.     long lseek();
  9309.     static struct nlist nl[] = {
  9310.         {PROCNAME},
  9311.         {NPROCNAME},
  9312.         {""}
  9313.     };
  9314.     static struct proc *pp;
  9315.     char *qaddr, *p, c;
  9316.     int m;
  9317.     PID_T pid, me;
  9318.     NPTYPE xproc;                       /* Its type is defined in makefile. */
  9319.     int catch();
  9320.  
  9321.     me = getpid();
  9322.     if ((m = open("/dev/kmem", 0)) < 0) err("kmem");
  9323.     nlist(BOOTNAME, nl);
  9324.     if (nl[0].n_type == 0) err("proc array");
  9325.  
  9326.     if (nl[1].n_type == 0) err("nproc");
  9327.  
  9328.     lseek(m, (long)(nl[1].n_value), 0);
  9329.     read (m, &xproc, sizeof(xproc));
  9330.     saval = signal(SIGALRM, catch);
  9331.     if ((pid = fork()) == 0) {
  9332.         while(1)
  9333.             read(tty, &c, 1);
  9334.     }
  9335.     alarm(2);
  9336.  
  9337.     if(setjmp(jjbuf) == 0) {
  9338.         while(1)
  9339.       read(tty, &c, 1);
  9340.     }
  9341.     signal(SIGALRM, SIG_DFL);
  9342.  
  9343. #ifdef DIRECT
  9344.     pp = (struct proc *) nl[0].n_value;
  9345. #else
  9346.     if (lseek(m, (long)(nl[0].n_value), 0) < 0L) err("seek");
  9347.     if (read(m, &pp, sizeof(pp)) != sizeof(pp))  err("no read of proc ptr");
  9348. #endif
  9349.     lseek(m, (long)(nl[1].n_value), 0);
  9350.     read(m, &xproc, sizeof(xproc));
  9351.  
  9352.     if (lseek(m, (long)pp, 0) < 0L) err("Can't seek to proc");
  9353.     if ((p = malloc(xproc * sizeof(struct proc))) == NULL) err("malloc");
  9354.     if (read(m,p,xproc * sizeof(struct proc)) != xproc*sizeof(struct proc))
  9355.         err("read proc table");
  9356.     for (pp = (struct proc *)p; xproc > 0; --xproc, ++pp) {
  9357.         if (pp -> p_pid == (short) pid) goto iout;
  9358.     }
  9359.     err("no such proc");
  9360.  
  9361. iout:
  9362.     close(m);
  9363.     qaddr = (char *)(pp -> p_wchan);
  9364.     free (p);
  9365.     kill(pid, SIGKILL);
  9366.     wait((WAIT_T *)0);
  9367.     return (qaddr);
  9368. #endif
  9369. #endif
  9370. #endif
  9371. #endif
  9372. }
  9373.  
  9374. /*  More V7-support functions...  */
  9375.  
  9376. static VOID
  9377. err(s) char *s; {
  9378.     char buf[200];
  9379.  
  9380.     ckmakmsg(buf,200,"fatal error in initrawq: ", s, NULL, NULL);
  9381.     perror(buf);
  9382.     doexit(1,-1);
  9383. }
  9384.  
  9385. static VOID
  9386. catch(foo) int foo; {
  9387.     longjmp(jjbuf, -1);
  9388. }
  9389.  
  9390.  
  9391. /*  G E N B R K  --  Simulate a modem break.  */
  9392.  
  9393. #ifdef MINIX
  9394. #define BSPEED B110
  9395. #else
  9396. #ifdef MINIX2
  9397. #define BSPEED B110
  9398. #else
  9399. #define BSPEED B150
  9400. #endif /* MINIX2 */
  9401. #endif /* MINIX */
  9402.  
  9403. #ifndef MINIX2
  9404. VOID
  9405. genbrk(fn,msec) int fn, msec; {
  9406.     struct sgttyb ttbuf;
  9407.     int ret, sospeed, x, y;
  9408.  
  9409.     ret = ioctl(fn, TIOCGETP, &ttbuf);
  9410.     sospeed = ttbuf.sg_ospeed;
  9411.     ttbuf.sg_ospeed = BSPEED;
  9412.     ret = ioctl(fn, TIOCSETP, &ttbuf);
  9413.     y = (int)strlen(brnuls);
  9414.     x = ( BSPEED * 100 ) / msec;
  9415.     if (x > y) x = y;
  9416.     ret = write(fn, brnuls, (( BSPEED * 100 ) / msec ));
  9417.     ttbuf.sg_ospeed = sospeed;
  9418.     ret = ioctl(fn, TIOCSETP, &ttbuf);
  9419.     ret = write(fn, "@", 1);
  9420.     return;
  9421. }
  9422. #endif /* MINIX2 */
  9423.  
  9424. #ifdef MINIX2
  9425. int
  9426. genbrk(fn,msec) int fn, msec; {
  9427.     struct termios ttbuf;
  9428.     int ret, x, y;
  9429.     speed_t sospeed;
  9430.  
  9431.     ret = tcgetattr(fn, &ttbuf);
  9432.     sospeed = ttbuf.c_ospeed;
  9433.     ttbuf.c_ospeed = BSPEED;
  9434.     ret = tcsetattr(fn,TCSADRAIN, &ttbuf);
  9435.     y = (int)strlen(brnuls);
  9436.     x = ( BSPEED * 100 ) / msec;
  9437.     if (x > y) x = y;
  9438.     ret = write(fn, brnuls, (( BSPEED * 100 ) / msec ));
  9439.     ttbuf.c_ospeed = sospeed;
  9440.     ret = tcsetattr(fn, TCSADRAIN, &ttbuf);
  9441.     ret = write(fn, "@", 1);
  9442.     return ret;
  9443. }
  9444. #endif /* MINIX2 */
  9445. #endif /* V7 */
  9446.  
  9447. /*
  9448.   I N C H K  --  Check if chars waiting to be read on given file descriptor.
  9449.  
  9450.   This routine is a merger of ttchk() and conchk().
  9451.   Call with:
  9452.     channel == 0 to check console.
  9453.     channel == 1 to check communications connection.
  9454.   and:
  9455.     fd = file descriptor.
  9456.   Returns:
  9457.    >= 0: number of characters waiting, 0 or greater,
  9458.      -1: on any kind of error,
  9459.      -2: if there is (definitely) no connection.
  9460.   Note: In UNIX we don't have to call nettchk() because a socket
  9461.   file descriptor works just like in serial i/o, ioctls and all.
  9462.   (But this will change if we add non-file-descriptor channels,
  9463.   such as IBM X.25 for AIX...)
  9464. */
  9465. static int
  9466. in_chk(channel, fd) int channel, fd; {
  9467.     int x, n = 0;            /* Workers, n = return value */
  9468.     extern int clsondisc;        /* Close on disconnect */
  9469. /*
  9470.   The first section checks to make sure we have a connection,
  9471.   but only if we're in local mode.
  9472. */
  9473. #ifdef DEBUG
  9474.     if (deblog) {
  9475.     debug(F111,"in_chk entry",ckitoa(fd),channel);
  9476.     debug(F101,"in_chk ttyfd","",ttyfd);
  9477.     debug(F101,"in_chk ttpty","",ttpty);
  9478.     }
  9479. #endif /* DEBUG */
  9480. /*
  9481.   But don't say connection is gone if we have any buffered-stuff.
  9482. */
  9483. #ifdef TTLEBUF
  9484.     debug(F101,"in_chk ttpush","",ttpush);
  9485.     if (channel == 1) {
  9486.     if (ttpush >= 0)
  9487.       n++;
  9488.     n += le_inbuf();
  9489.     if (n > 0)
  9490.       return(n);
  9491.     }
  9492. #endif /* TTLEBUF */
  9493.  
  9494. #ifdef NETPTY
  9495. #ifdef HAVE_PTYTRAP
  9496.     /* Special handling for HP-UX pty i/o */
  9497.     if (ttpty && pty_trap_pending(ttyfd) > 0) {
  9498.         if (pty_trap_handler(ttyfd) > 0) {
  9499.             ttclos(0);
  9500.             return(-2);
  9501.         }
  9502.     }
  9503. #endif /* HAVE_PTYTRAP */
  9504. #endif /* NETPTY */
  9505.  
  9506.     if (channel) {            /* Checking communications channel */
  9507.     if (ttyfd < 0) {        /* No connection */
  9508.       return(-2);            /* That's what this means */
  9509.     } else if (xlocal &&        /* In local mode */
  9510.            !netconn &&        /* Serial connection */
  9511.            ttcarr != CAR_OFF    /* with CARRIER WATCH ON (or AUTO) */
  9512. #ifdef COMMENT
  9513. #ifdef MYREAD
  9514. /*
  9515.   Seems like this would be a good idea but it prevents C-Kermit from
  9516.   popping back to the prompt automatically when carrier drops.  However,
  9517.   commenting this out prevents us from seeing the NO CARRIER message.
  9518.   Needs more work...
  9519. */
  9520.            && my_count < 1    /* Nothing in our internal buffer */
  9521. #endif /* MYREAD */
  9522. #endif /* COMMENT */
  9523.            ) {
  9524.         int x;
  9525.         x = ttgmdm();        /* So get modem signals */
  9526.         debug(F101,"in_chk close-on-disconnect","",clsondisc);
  9527.         if (x > -1) {        /* Check for carrier */
  9528.         if (!(x & BM_DCD)) {    /* No carrier */
  9529.             debug(F101,"in_chk carrier lost","",x);
  9530.             if (clsondisc)    /* If "close-on-disconnect" */
  9531.               ttclos(0);    /* close device & release lock. */
  9532.             return(-2);        /* This means "disconnected" */
  9533.         }
  9534.         /* In case I/O to device after CD dropped always fails */
  9535.         /* as in Debian Linux 2.1 and Unixware 2.1... */
  9536.         } else {
  9537.             debug(F101,"in_chk ttgmdm I/O error","",errno);
  9538.             debug(F101,"in_chk ttgmdm gotsigs","",gotsigs);
  9539.             if (gotsigs) {        /* If we got signals before... */
  9540.             if (errno == 5 || errno == 6) { /* I/O error etc */
  9541.                 if (clsondisc)    /* like when modem hangs up */
  9542.               ttclos(0);
  9543.             return(-2);
  9544.             }
  9545.         }
  9546.         /* If we never got modem signals successfully on this */
  9547.         /* connection before, we can't conclude that THIS failure */
  9548.         /* means the connection was lost. */
  9549.         return(0);
  9550.         }
  9551.     }
  9552.     }
  9553.  
  9554. /* We seem to have a connection so now see if any bytes are waiting on it */
  9555.  
  9556. #ifdef CK_SSL
  9557.     if (ssl_active_flag || tls_active_flag) {
  9558.         n += SSL_pending(ssl_active_flag?ssl_con:tls_con);
  9559.         debug(F101,"in_chk SSL_pending","",n);
  9560.         if (n < 0) {
  9561.             ttclos(0);
  9562.             return(-1);
  9563.         } else if (n > 0) {
  9564.             return(n);
  9565.         }
  9566.     }
  9567. #endif /* CK_SSL */
  9568. #ifdef RLOGCODE
  9569. #ifdef CK_KERBEROS
  9570.     /* It is not safe to read any data when using encrypted Klogin */
  9571.     if (ttnproto == NP_EK4LOGIN || ttnproto == NP_EK5LOGIN) {
  9572. #ifdef KRB4
  9573.         if (ttnproto == NP_EK4LOGIN) {
  9574.             n += krb4_des_avail(ttyfd);
  9575.             debug(F101,"in_chk krb4_des_avail","",n);
  9576.         }
  9577. #endif /* KRB4 */
  9578. #ifdef KRB5
  9579.         if (ttnproto == NP_EK5LOGIN) {
  9580.             n += krb5_des_avail(ttyfd);
  9581.             debug(F101,"in_chk krb5_des_avail","",n);
  9582.         }
  9583. #ifdef KRB5_U2U
  9584.         if (ttnproto == NP_K5U2U) {
  9585.             n += krb5_u2u_avail(ttyfd);
  9586.             debug(F101,"in_chk krb5_des_avail","",n);
  9587.         }
  9588. #endif /* KRB5_U2U */
  9589. #endif /* KRB5 */
  9590.         if (n < 0)            /* Is this right? */
  9591.       return(-1);
  9592.         else
  9593.       return(n);
  9594.     }
  9595. #endif /* CK_KERBEROS */
  9596. #endif /* RLOGCODE */
  9597.  
  9598.     errno = 0;                /* Reset this so we log good info */
  9599. #ifdef FIONREAD
  9600.     x = ioctl(fd, FIONREAD, &n);    /* BSD and lots of others */
  9601. #ifdef DEBUG                /* (the more the better) */
  9602.     if (deblog) {
  9603.     debug(F101,"in_chk FIONREAD return code","",x);
  9604.     debug(F101,"in_chk FIONREAD count","",n);
  9605.     debug(F101,"in_chk FIONREAD errno","",errno);
  9606.     }
  9607. #endif /* DEBUG */
  9608. #else /* FIONREAD not defined */
  9609. /*
  9610.   Here, if (netconn && ttnet == NET_TCPB), we might try calling recvmsg()
  9611.   with flags MSG_PEEK|MSG_DONTWAIT on the socket (ttyfd), except this is not
  9612.   portable (MSG_DONTWAIT isn't defined in any of the <sys/socket.h> files
  9613.   that I looked at, but it is needed to prevent the call from blocking), and
  9614.   the msghdr struct differs from place to place, so we would need another
  9615.   avalanche of ifdefs.  Still, when FIONREAD is not available, this is the
  9616.   only other known method of asking the OS for the *number* of characters
  9617.   available for reading.
  9618. */
  9619. #ifdef V7                /* UNIX V7: look in kernel memory */
  9620. #ifdef MINIX
  9621.     n = 0;                /* But not in MINIX */
  9622. #else
  9623. #ifdef MINIX2
  9624.     n = 0;
  9625. #else
  9626.     lseek(kmem[TTY], (long) qaddr[TTY], 0); /* 7th Edition Unix */
  9627.     x = read(kmem[TTY], &n, sizeof(int));
  9628.     if (x != sizeof(int))
  9629.       n = 0;
  9630. #endif /* MINIX2 */
  9631. #endif /* MINIX */
  9632. #else /* Not V7 */
  9633. #ifdef PROVX1
  9634.     x = ioctl(fd, TIOCQCNT, &ttbuf);    /* DEC Pro/3xx Venix V.1 */
  9635.     n = ttbuf.sg_ispeed & 0377;        /* Circa 1984 */
  9636.     if (x < 0) n = 0;
  9637. #else
  9638. #ifdef MYREAD
  9639. /*
  9640.   Here we skip all the undependable and expensive calls below if we
  9641.   already have something in our internal buffer.  This tends to work quite
  9642.   nicely, so the only really bad case remaining is the one in which neither
  9643.   FIONREAD or MYREAD are defined, which is increasingly rare these days.
  9644. */
  9645.     if (channel != 0 && my_count > 0) {
  9646.     debug(F101,"in_chk buf my_count","",my_count);
  9647.     n = my_count;            /* n was 0 before we got here */
  9648.     return(n);
  9649.     }
  9650. #endif /* MYREAD */
  9651. /*
  9652.   rdchk(), select(), and poll() tell us *if* data is available to be read, but
  9653.   not how much, so these should be used only as a final resort.  Especially
  9654.   since these calls tend to add a lot overhead.
  9655. */
  9656. #ifdef RDCHK                /* This mostly SCO-specific */
  9657.     n = rdchk(fd);
  9658.     debug(F101,"in_chk rdchk","",n);
  9659. #else /* No RDCHK */
  9660. #ifdef SELECT
  9661. #ifdef Plan9
  9662.     /* Only allows select on the console ... don't ask */
  9663.     if (channel == 0)
  9664. #endif /* Plan9 */
  9665.       {
  9666.     fd_set rfds;            /* Read file descriptors */
  9667. #ifdef BELLV10
  9668.     FD_ZERO(rfds);            /* Initialize them */
  9669.     FD_SET(fd,rfds);        /* We want to look at this fd */
  9670. #else
  9671.     FD_ZERO(&rfds);            /* Initialize them */
  9672.     FD_SET(fd,&rfds);        /* We want to look at this fd */
  9673.     tv.tv_sec = tv.tv_usec = 0L;    /* A 0-valued timeval structure */
  9674. #endif /* BELLV10 */
  9675. #ifdef Plan9
  9676.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9677.     debug(F101,"in_chk Plan 9 select","",n);
  9678. #else
  9679. #ifdef BELLV10
  9680.     n = select( 128, rfds, (fd_set *)0, (fd_set *)0, 0 );
  9681.     debug(F101,"in_chk BELLV10 select","",n);
  9682. #else
  9683. #ifdef BSD44
  9684.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9685.     debug(F101,"in_chk BSD44 select","",n);
  9686. #else
  9687. #ifdef BSD43
  9688.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9689.     debug(F101,"in_chk BSD43 select","",n);
  9690. #else
  9691. #ifdef SOLARIS
  9692.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9693.     debug(F101,"in_chk SOLARIS select","",n);
  9694. #else
  9695. #ifdef QNX6
  9696.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9697.     debug(F101,"in_chk QNX6 select","",n);
  9698. #else
  9699. #ifdef QNX
  9700.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9701.     debug(F101,"in_chk QNX select","",n);
  9702. #else
  9703. #ifdef COHERENT
  9704.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9705.     debug(F101,"in_chk COHERENT select","",n);
  9706. #else
  9707. #ifdef SVR4
  9708.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9709.     debug(F101,"in_chk SVR4 select","",n);
  9710. #else
  9711. #ifdef __linux__
  9712.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9713.     debug(F101,"in_chk LINUX select","",n);
  9714. #ifdef OSF
  9715.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9716.     debug(F101,"in_chk OSF select","",n);
  9717. #else
  9718.     n = select( FD_SETSIZE, &rfds, (int *)0, (int *)0, &tv );
  9719.     debug(F101,"in_chk catchall select","",n);
  9720. #endif /* OSF */
  9721. #endif /* __linux__ */
  9722. #endif /* SVR4 */
  9723. #endif /* COHERENT */
  9724. #endif /* QNX */
  9725. #endif /* QNX6 */
  9726. #endif /* SOLARIS */
  9727. #endif /* BSD43 */
  9728. #endif /* BSD44 */
  9729. #endif /* BELLV10 */
  9730. #endif /* Plan9 */
  9731.     }
  9732. #else  /* Not SELECT */
  9733. #ifdef CK_POLL
  9734.     {
  9735.       struct pollfd pfd;
  9736.  
  9737.       pfd.fd = fd;
  9738.       pfd.events = POLLIN;
  9739.       pfd.revents = 0;
  9740.       n = poll(&pfd, 1, 0);
  9741.       debug(F101,"in_chk poll","",n);
  9742.       if ((n > 0) && (pfd.revents & POLLIN))
  9743.     n = 1;
  9744.     }
  9745. #endif /* CK_POLL */
  9746. #endif /* SELECT */
  9747. #endif /* RDCHK */
  9748. #endif /* PROVX1 */
  9749. #endif /* V7 */
  9750. #endif /* FIONREAD */
  9751.  
  9752. /* From here down, treat console and communication device differently... */
  9753.  
  9754.     if (channel == 0) {            /* Console */
  9755.  
  9756. #ifdef SVORPOSIX
  9757. #ifndef FIONREAD
  9758. #ifndef SELECT
  9759. #ifndef CK_POLL
  9760. #ifndef RDCHK
  9761. /*
  9762.   This is the hideous hack used in System V and POSIX systems that don't
  9763.   support FIONREAD, rdchk(), select(), poll(), etc, in which the user's
  9764.   CONNECT-mode escape character is attached to SIGQUIT.  Used, obviously,
  9765.   only on the console.
  9766. */
  9767.     if (conesc) {            /* Escape character typed == SIGQUIT */
  9768.         debug(F100,"in_chk conesc","",conesc);
  9769.         conesc = 0;
  9770.         signal(SIGQUIT,esctrp);    /* Restore signal */
  9771.         n += 1;
  9772.     }
  9773. #endif /* RDCHK */
  9774. #endif /* CK_POLL */
  9775. #endif /* SELECT */
  9776. #endif /* FIONREAD */
  9777. #endif /* SVORPOSIX */
  9778.  
  9779.     return(n);            /* Done with console */
  9780.     }
  9781.  
  9782.     if (channel != 0) {            /* Communications connection */
  9783.  
  9784. #ifdef MYREAD
  9785. #ifndef FIONREAD
  9786. /*
  9787.   select() or rdchk(), etc, has told us that something is waiting, but we
  9788.   don't know how much.  So we do a read to get it and then we know.  Note:
  9789.   This read is NOT nonblocking if nothing is there (because of VMIN=1), but
  9790.   it should be safe in this case since the OS tells us at least one byte is
  9791.   waiting to be read, and MYREAD reads return as much as is there without
  9792.   waiting for any more.  Controlled tests on Solaris and Unixware (with
  9793.   FIONREAD deliberately undefined) show this to be true.
  9794. */
  9795.     debug(F101,"in_chk read my_count","",my_count);
  9796.     debug(F101,"in_chk read n","",n);
  9797.     if (n > 0 && my_count == 0) {
  9798.         /* This also catches disconnects etc */
  9799.         /* Do what mygetbuf does except don't grab a character */
  9800.         my_count = myfillbuf();
  9801.         my_item = -1;        /* ^^^ */
  9802.         debug(F101,"in_chk myfillbuf my_count","",my_count);
  9803.         if (my_count < 0)
  9804.           return(-1);
  9805.         else
  9806.           n = 0;            /* NB: n is replaced by my_count */
  9807.     }
  9808. #endif /* FIONREAD */
  9809. /*
  9810.   Here we add whatever we think is unread to what is still in our
  9811.   our internal buffer.  Thus the importance of setting n to 0 just above.
  9812. */
  9813.     debug(F101,"in_chk my_count","",my_count);
  9814.     debug(F101,"in_chk n","",n);
  9815.     if (my_count > 0)
  9816.       n += my_count;
  9817. #endif /* MYREAD */
  9818.     }
  9819.     debug(F101,"in_chk result","",n);
  9820.  
  9821.     /* Errors here don't prove the connection has dropped so just say 0 */
  9822.  
  9823.     return(n < 0 ? 0 : n);
  9824. }
  9825.  
  9826.  
  9827. /*  T T C H K  --  Tell how many characters are waiting in tty input buffer  */
  9828.  
  9829. int
  9830. ttchk() {
  9831.     int fd;
  9832. #ifdef NETCMD
  9833.     if (ttpipe)
  9834.       fd = fdin;
  9835.     else
  9836. #endif /* NETCMD */
  9837.       fd = ttyfd;
  9838.     return(in_chk(1,fd));
  9839. }
  9840.  
  9841. /*  T T X I N  --  Get n characters from tty input buffer  */
  9842.  
  9843. /*  Returns number of characters actually gotten, or -1 on failure  */
  9844.  
  9845. /*  Intended for use only when it is known that n characters are actually */
  9846. /*  Available in the input buffer.  */
  9847.  
  9848. int
  9849. ttxin(n,buf) int n; CHAR *buf; {
  9850.     register int x = 0, c = -2;
  9851. #ifdef TTLEBUF
  9852.     register int i = 0;
  9853. #endif /* TTLEBUF */
  9854.     int fd;
  9855.  
  9856.     if (n < 1)                /* Nothing to do */
  9857.       return(0);
  9858.  
  9859. #ifdef TTLEBUF
  9860.     if (ttpush >= 0) {
  9861.         buf[0] = ttpush;        /* Put pushed char in buffer*/
  9862.         ttpush = -1;            /* Clear the push buffer */
  9863.         if (ttchk() > 0)
  9864.       return(ttxin(n-1, &buf[1]) + 1);
  9865.         else
  9866.       return(1);
  9867.     }
  9868.     if (le_data) {
  9869.         while (le_inbuf() > 0) {
  9870.         if (le_getchar(&buf[i])) {
  9871.                 i++;
  9872.                 n--;
  9873.             }
  9874.         }
  9875.         if (ttchk() > 0)
  9876.       return(ttxin(n,&buf[i])+i);
  9877.         else
  9878.       return(i);
  9879.     }
  9880. #endif /* TTLEBUF */
  9881.  
  9882. #ifdef NETCMD
  9883.     if (ttpipe)
  9884.       fd = fdin;
  9885.     else
  9886. #endif /* NETCMD */
  9887.       fd = ttyfd;
  9888.  
  9889. #ifdef SUNX25
  9890.     if (netconn && (ttnet == NET_SX25))    /* X.25 connection */
  9891.       return(x25xin(n,buf));
  9892. #endif /* SUNX25 */
  9893.  
  9894. #ifdef IBMX25
  9895.     /* riehm: possibly not needed. Test worked with normal reads and writes */
  9896.     if (netconn && (ttnet == NET_IX25))    { /* X.25 connection */
  9897.     x = x25xin(n,buf);
  9898.     if (x > 0) buf[x] = '\0';
  9899.     return(x);
  9900.     }
  9901. #endif /* IBMX25 */
  9902.  
  9903. #ifdef MYREAD
  9904.     debug(F101,"ttxin MYREAD","",n);
  9905.     while (x < n) {
  9906.     c = myread();
  9907.     if (c < 0) {
  9908.         debug(F101,"ttxin myread returns","",c);
  9909.         if (c == -3) x = -1;
  9910.         break;
  9911.         }
  9912.     buf[x++] = c & ttpmsk;
  9913. #ifdef RLOGCODE
  9914. #ifdef CK_KERBEROS
  9915.         /* It is impossible to know how many characters are waiting */
  9916.         /* to be read when you are using Encrypted Rlogin or SSL    */
  9917.         /* as the transport since the number of real data bytes     */
  9918.         /* can be greater or less than the number of bytes on the   */
  9919.         /* wire which is what ttchk() returns.                      */
  9920.         if (netconn && (ttnproto == NP_EK4LOGIN || ttnproto == NP_EK5LOGIN))
  9921.       if (ttchk() <= 0)
  9922.         break;
  9923. #endif /* CK_KERBEROS */
  9924. #endif /* RLOGCODE */
  9925. #ifdef CK_SSL
  9926.         if (ssl_active_flag || tls_active_flag)
  9927.       if (ttchk() <= 0)
  9928.         break;
  9929. #endif /* CK_SSL */
  9930.     }
  9931. #else
  9932.     debug(F101,"ttxin READ","",n);
  9933.     x = read(fd,buf,n);
  9934.     for (c = 0; c < n; c++)        /* Strip any parity */
  9935.       buf[c] &= ttpmsk;
  9936. #endif /* MYREAD */
  9937.  
  9938.     debug(F101,"ttxin x","",x);        /* Done */
  9939.     if (x > 0) buf[x] = '\0';
  9940.     if (x < 0) x = -1;
  9941.     return(x);
  9942. }
  9943.  
  9944. /*  T T O L  --  Write string s, length n, to communication device.  */
  9945. /*
  9946.   Returns:
  9947.    >= 0 on success, number of characters actually written.
  9948.    -1 on failure.
  9949. */
  9950. #ifdef CK_ENCRYPTION
  9951. CHAR * xpacket = NULL;
  9952. int nxpacket = 0;
  9953. #endif /* CK_ENCRYPTION */
  9954.  
  9955. #define TTOLMAXT 5
  9956. int
  9957. ttol(s,n) int n; CHAR *s; {
  9958.     int x, len, tries, fd;
  9959. #ifdef CKXXCHAR
  9960.     extern int dblflag;            /* For SET SEND DOUBLE-CHARACTER */
  9961.     extern short dblt[];
  9962.     CHAR *p = NULL, *p2, *s2, c;
  9963.     int n2 = 0;
  9964. #endif /* CKXXCHAR */
  9965.  
  9966.     if (ttyfd < 0)            /* Not open? */
  9967.       return(-3);
  9968. #ifdef DEBUG
  9969.     if (deblog) hexdump("ttol s",s,n);
  9970. #endif /* DEBUG */
  9971.  
  9972. #ifdef NETCMD
  9973.     if (ttpipe)
  9974.       fd = fdout;
  9975.     else
  9976. #endif /* NETCMD */
  9977.       fd = ttyfd;
  9978.  
  9979. #ifdef CKXXCHAR
  9980. /*  Double any characters that must be doubled.  */
  9981.     debug(F101,"ttol dblflag","",dblflag);
  9982.     if (dblflag) {
  9983.     p = (CHAR *) malloc(n + n + 1);
  9984.     if (p) {
  9985.         s2 = s;
  9986.         p2 = p;
  9987.         n2 = 0;
  9988.         while (*s2) {
  9989.         c = *s2++;
  9990.         *p2++ = c;
  9991.         n2++;
  9992.         if (dblt[(unsigned) c] & 2) {
  9993.             *p2++ = c;
  9994.             n2++;
  9995.         }
  9996.         }
  9997.         s = p;
  9998.         n = n2;
  9999.         s[n] = '\0';
  10000.     }
  10001. #ifdef DEBUG
  10002.         if (deblog) hexdump("ttol doubled s",s,n);
  10003. #endif /* DEBUG */
  10004.     }
  10005. #endif /* CKXXCHAR */
  10006.  
  10007.     tries = TTOLMAXT;            /* Allow up to this many tries */
  10008.     len = n;                /* Remember original length */
  10009.  
  10010. #ifdef CK_ENCRYPTION
  10011. /*
  10012.   This is to avoid encrypting a packet that is already encrypted, e.g.
  10013.   when we resend a packet directly out of the packet buffer, and also to
  10014.   avoid encrypting a constant (literal) string, which can cause a memory
  10015.   fault.
  10016. */
  10017.     if (TELOPT_ME(TELOPT_ENCRYPTION)) {
  10018.     int x;
  10019.     if (nxpacket < n) {
  10020.         if (xpacket) {
  10021.         free(xpacket);
  10022.         xpacket = NULL;
  10023.         nxpacket = 0;
  10024.         }
  10025.         x = n > 10240 ? n : 10240;
  10026.         xpacket = (CHAR *)malloc(x);
  10027.         if (!xpacket) {
  10028.         fprintf(stderr,"ttol malloc failure\n");
  10029.         return(-1);
  10030.         } else
  10031.           nxpacket = x;
  10032.     }
  10033.     memcpy((char *)xpacket,(char *)s,n);
  10034.     s = xpacket;
  10035.     ck_tn_encrypt((char *)s,n);
  10036.     }
  10037. #endif /* CK_ENCRYPTION */
  10038.  
  10039.     while (n > 0 &&
  10040.        (tries-- > 0
  10041. #ifdef CK_ENCRYPTION
  10042.         /* keep trying if we are encrypting */
  10043.         || TELOPT_ME(TELOPT_ENCRYPTION)
  10044. #endif /* CK_ENCRYPTION */
  10045.             )) {            /* Be persistent */
  10046.     debug(F101,"ttol try","",TTOLMAXT - tries);
  10047. #ifdef BEOSORBEBOX
  10048.         if (netconn && !ttpipe && !ttpty)
  10049.       x = nettol((char *)s,n);    /* Write string to device */
  10050.         else
  10051. #endif /* BEOSORBEBOX */
  10052. #ifdef IBMX25
  10053.       if (ttnet == NET_IX25)
  10054.         /*
  10055.          * this is a more controlled way of writing to X25
  10056.          * STREAMS, however write should also work!
  10057.          */
  10058.         x = x25write(ttyfd, s, n);
  10059.       else
  10060. #endif /* IBMX25 */
  10061. #ifdef CK_SSL
  10062.         if (ssl_active_flag || tls_active_flag) {
  10063.         int error;
  10064.         /* Write using SSL */
  10065.                 ssl_retry:
  10066.         if (ssl_active_flag)
  10067.                   x = SSL_write(ssl_con, s, n);
  10068.         else
  10069.                   x = SSL_write(tls_con, s, n);
  10070.         switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,x)) {
  10071.                 case SSL_ERROR_NONE:
  10072.                     if (x == n)
  10073.               return(len);
  10074.                     s += x;
  10075.                     n -= x;
  10076.                     goto ssl_retry;
  10077.           case SSL_ERROR_WANT_WRITE:
  10078.           case SSL_ERROR_WANT_READ:
  10079.             x = 0;
  10080.             break;
  10081.           case SSL_ERROR_SYSCALL:
  10082.                     if (x != 0)
  10083.               return(-1);
  10084.           case SSL_ERROR_WANT_X509_LOOKUP:
  10085.           case SSL_ERROR_SSL:
  10086.           case SSL_ERROR_ZERO_RETURN:
  10087.           default:
  10088.             ttclos(0);
  10089.             return(-3);
  10090.         }
  10091.         } else
  10092. #endif /* CK_SSL */
  10093. #ifdef CK_KERBEROS
  10094. #ifdef KRB4
  10095. #ifdef RLOGCODE
  10096.         if (ttnproto == NP_EK4LOGIN) {
  10097.         return(krb4_des_write(ttyfd,s,n));
  10098.         } else
  10099. #endif /* RLOGCODE */
  10100. #endif /* KRB4 */
  10101. #ifdef KRB5
  10102. #ifdef RLOGCODE
  10103.             if (ttnproto == NP_EK5LOGIN) {
  10104.                 return(krb5_des_write(ttyfd,s,n));
  10105.             } else
  10106. #endif /* RLOGCODE */
  10107. #ifdef KRB5_U2U
  10108.             if (ttnproto == NP_K5U2U) {
  10109.                 return(krb5_u2u_write(ttyfd,s,n));
  10110.             } else
  10111. #endif /* KRB5_U2U */
  10112. #endif /* KRB5 */
  10113. #endif /* CK_KERBEROS */
  10114.           x = write(fd,s,n);    /* Write string to device */
  10115.  
  10116.     if (x == n) {            /* Worked? */
  10117.         debug(F101,"ttol ok","",x);    /* OK */
  10118. #ifdef CKXXCHAR
  10119.         if (p) free(p);
  10120. #endif /* CKXXCHAR */
  10121.         return(len);        /* Done */
  10122.     } else if (x < 0) {        /* No, got error? */
  10123.         debug(F101,"ttol write error","",errno);
  10124. #ifdef EWOULDBLOCK
  10125.         if (errno == EWOULDBLOCK) {
  10126.         msleep(10);
  10127.         continue;
  10128.         } else
  10129. #endif /* EWOULDBLOCK */
  10130. #ifdef TCPSOCKET
  10131.         if (netconn && ttnet == NET_TCPB) {
  10132.         debug(F101,"ttol TCP error","",errno);
  10133.         ttclos(0);        /* Close the connection. */
  10134.         x = -3;
  10135.         }
  10136. #endif /* TCPSOCKET */
  10137. #ifdef CKXXCHAR
  10138.         if (p) free(p);
  10139. #endif /* CKXXCHAR */
  10140.         return(x);
  10141.     } else {            /* No error, so partial success */
  10142.         debug(F101,"ttol partial","",x); /* This never happens */
  10143.         s += x;            /* Point to part not written yet */
  10144.         n -= x;            /* Adjust length */
  10145.         if (x > 0) msleep(10);    /* Wait 10 msec */
  10146.     }                /* Go back and try again */
  10147.     }
  10148. #ifdef CKXXCHAR
  10149.     if (p) free(p);
  10150. #endif /* CKXXCHAR */
  10151.     return(n < 1 ? len : -1);        /* Return the results */
  10152. }
  10153.  
  10154. /*  T T O C  --  Output a character to the communication line  */
  10155.  
  10156. /*
  10157.  This function should only be used for interactive, character-mode operations,
  10158.  like terminal connection, script execution, dialer i/o, where the overhead
  10159.  of the signals and alarms does not create a bottleneck.
  10160. */
  10161. int
  10162. #ifdef CK_ANSIC
  10163. ttoc(char c)
  10164. #else
  10165. ttoc(c) char c;
  10166. #endif /* CK_ANSIC */
  10167. /* ttoc */ {
  10168. #define TTOC_TMO 15            /* Timeout in case we get stuck */
  10169.     int xx, fd;
  10170.  
  10171.     if (ttyfd < 0)            /* Check for not open. */
  10172.       return(-1);
  10173.  
  10174. #ifdef NETCMD
  10175.     if (ttpipe)
  10176.       fd = fdout;
  10177.     else
  10178. #endif /* NETCMD */
  10179.       fd = ttyfd;
  10180.  
  10181.     c &= 0xff;
  10182.     /* debug(F101,"ttoc","",(CHAR) c); */
  10183.     saval = signal(SIGALRM,timerh);    /* Enable timer interrupt */
  10184.     xx = alarm(TTOC_TMO);        /* for this many seconds. */
  10185.     if (xx < 0) xx = 0;            /* Save old alarm value. */
  10186.     /* debug(F101,"ttoc alarm","",xx); */
  10187.     if (
  10188. #ifdef CK_POSIX_SIG
  10189.     sigsetjmp(sjbuf,1)
  10190. #else
  10191.     setjmp(sjbuf)
  10192. #endif /* CK_POSIX_SIG */
  10193.     ) {        /* Timer went off? */
  10194.     ttimoff();            /* Yes, cancel this alarm. */
  10195.     if (xx - TTOC_TMO > 0) alarm(xx - TTOC_TMO); /* Restore previous one */
  10196.         /* debug(F100,"ttoc timeout","",0); */
  10197. #ifdef NETCONN
  10198.     if (!netconn) {
  10199. #endif /* NETCONN */
  10200.         debug(F101,"ttoc timeout","",c);
  10201.         if (ttflow == FLO_XONX) {
  10202.         debug(F101,"ttoc flow","",ttflow); /* Maybe we're xoff'd */
  10203. #ifndef Plan9
  10204. #ifdef POSIX
  10205.         /* POSIX way to unstick. */
  10206.         debug(F100,"ttoc tcflow","",tcflow(ttyfd,TCOON));
  10207. #else
  10208. #ifdef BSD4                /* Berkeley way to do it. */
  10209. #ifdef TIOCSTART
  10210. /* .... Used to be "ioctl(ttyfd, TIOCSTART, 0);".  Who knows? */
  10211.         {
  10212.           int x = 0;
  10213.           debug(F101,"ttoc TIOCSTART","",ioctl(ttyfd, TIOCSTART, &x));
  10214.         }
  10215. #endif /* TIOCSTART */
  10216. #endif /* BSD4 */
  10217.                     /* Is there a Sys V way to do this? */
  10218. #endif /* POSIX */
  10219. #endif /* Plan9 */
  10220.         }
  10221. #ifdef NETCONN
  10222.         }
  10223. #endif /* NETCONN */
  10224.     return(-1);            /* Return failure code. */
  10225.     } else {
  10226.         int rc;
  10227. #ifdef BEOSORBEBOX
  10228. #ifdef NETCONN
  10229.         if (netconn && !ttpipe && !ttpty)
  10230.       rc = nettoc(c);
  10231.         else
  10232. #endif /*  BEOSORBEBOX */
  10233. #endif /* NETCONN */
  10234. #ifdef CK_ENCRYPTION
  10235.       if (TELOPT_ME(TELOPT_ENCRYPTION))
  10236.         ck_tn_encrypt(&c,1);
  10237. #endif /* CK_ENCRYPTION */
  10238. #ifdef IBMX25
  10239.     /* riehm: maybe this isn't necessary after all. Test program
  10240.      * worked fine with data being sent and retrieved with normal
  10241.      * read's and writes!
  10242.      */
  10243.     if (ttnet == NET_IX25)
  10244.       rc = x25write(ttyfd,&c,1); /* as above for X25 streams */
  10245.     else
  10246. #endif /* IBMX25 */
  10247. #ifdef CK_SSL
  10248.       if (ssl_active_flag || tls_active_flag) {
  10249.           int error;
  10250.           /* Write using SSL */
  10251.           if (ssl_active_flag)
  10252.                 rc = SSL_write(ssl_con, &c, 1);
  10253.           else
  10254.                 rc = SSL_write(tls_con, &c, 1);
  10255.           switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,rc)){
  10256.         case SSL_ERROR_NONE:
  10257.           break;
  10258.         case SSL_ERROR_WANT_WRITE:
  10259.         case SSL_ERROR_WANT_READ:
  10260.           rc = 0;
  10261.           break;
  10262.         case SSL_ERROR_SYSCALL:
  10263.           if (rc != 0)
  10264.             return(-1);
  10265.         case SSL_ERROR_WANT_X509_LOOKUP:
  10266.         case SSL_ERROR_SSL:
  10267.         case SSL_ERROR_ZERO_RETURN:
  10268.         default:
  10269.           ttclos(0);
  10270.           return(-1);
  10271.           }
  10272.       } else
  10273. #endif /* CK_SSL */
  10274. #ifdef CK_KERBEROS
  10275. #ifdef KRB4
  10276. #ifdef RLOGCODE
  10277.       if (ttnproto == NP_EK4LOGIN) {
  10278.           rc = (krb4_des_write(ttyfd,&c,1) == 1);
  10279.       } else
  10280. #endif /* RLOGCODE */
  10281. #endif /* KRB4 */
  10282. #ifdef KRB5
  10283. #ifdef RLOGCODE
  10284.           if (ttnproto == NP_EK5LOGIN) {
  10285.               rc = (krb5_des_write(ttyfd,&c,1) == 1);
  10286.           } else
  10287. #endif /* RLOGCODE */
  10288. #ifdef KRB5_U2U
  10289.           if (ttnproto == NP_K5U2U) {
  10290.               rc = (krb5_u2u_write(ttyfd,&c,1) == 1);
  10291.           } else
  10292. #endif /* KRB5_U2U */
  10293. #endif /* KRB5 */
  10294. #endif /* CK_KERBEROS */
  10295.         rc = write(fd,&c,1);    /* Try to write the character. */
  10296.     if (rc < 1) {            /* Failed */
  10297.         ttimoff();            /* Turn off the alarm. */
  10298.         alarm(xx);            /* Restore previous alarm. */
  10299.         debug(F101,"ttoc errno","",errno); /* Log the error, */
  10300.         return(-1);            /* and return the error code. */
  10301.     }
  10302.     }
  10303.     ttimoff();                /* Success, turn off the alarm. */
  10304.     alarm(xx);                /* Restore previous alarm. */
  10305.     return(0);                /* Return good code. */
  10306. }
  10307.  
  10308. /*  T T I N L  --  Read a record (up to break character) from comm line.  */
  10309. /*
  10310.   Reads up to "max" characters from the communication line, terminating on:
  10311.     (a) the packet length field if the "turn" argument is zero, or
  10312.     (b) on the packet-end character (eol) if the "turn" argument is nonzero
  10313.     (c) a certain number of Ctrl-C's in a row
  10314.  
  10315.   Returns:
  10316.     >= 0, the number of characters read upon success;
  10317.     -1 if "max" exceeded, timeout, or other correctable error;
  10318.     -2 on user interruption (c);
  10319.     -3 on fatal error like connection lost.
  10320.  
  10321.   The characters that were input are copied into "dest" with their parity bits
  10322.   stripped if parity was selected.  Returns the number of characters read.
  10323.   Characters after the eol are available upon the next call to this function.
  10324.  
  10325.   The idea is to minimize the number of system calls per packet, and also to
  10326.   minimize timeouts.  This function is the inner loop of the protocol and must
  10327.   be as efficient as possible.  The current strategy is to use myread().
  10328.  
  10329.   WARNING: This function calls parchk(), which is defined in another module.
  10330.   Normally, ckutio.c does not depend on code from any other module, but there
  10331.   is an exception in this case because all the other ck?tio.c modules also
  10332.   need to call parchk(), so it's better to have it defined in a common place.
  10333. */
  10334. #ifdef CTRLC
  10335. #undef CTRLC
  10336. #endif /* CTRLC */
  10337. #define CTRLC '\03'
  10338. /*
  10339.   We have four different declarations here because:
  10340.   (a) to allow Kermit to be built without the automatic parity sensing feature
  10341.   (b) one of each type for ANSI C, one for non-ANSI.
  10342. */
  10343.  
  10344. static int csave = -1;
  10345.  
  10346. #ifndef NOXFER
  10347. int
  10348. #ifdef PARSENSE
  10349. #ifdef CK_ANSIC
  10350. ttinl(CHAR *dest, int max,int timo, CHAR eol, CHAR start, int turn)
  10351. #else
  10352. ttinl(dest,max,timo,eol,start,turn) int max,timo,turn; CHAR *dest, eol, start;
  10353. #endif /* CK_ANSIC */
  10354. #else /* not PARSENSE */
  10355. #ifdef CK_ANSIC
  10356. ttinl(CHAR *dest, int max,int timo, CHAR eol)
  10357. #else
  10358. ttinl(dest,max,timo,eol) int max,timo; CHAR *dest, eol;
  10359. #endif /* __SDTC__ */
  10360. #endif /* PARSENSE */
  10361. /* ttinl */ {
  10362.  
  10363. #ifndef MYREAD
  10364.     CHAR ch, dum;
  10365. #endif /* MYREAD */
  10366. #ifdef PARSENSE
  10367.     int pktlen = -1;
  10368.     int lplen = 0;
  10369.     int havelen = 0;
  10370. #endif /* PARSENSE */
  10371.     int fd;
  10372.     int sopmask = 0xff;            /* Start-Of-Packet mask */
  10373. #ifdef CKXXCHAR
  10374.     extern short dblt[];        /* Ignore-character table */
  10375.     extern int ignflag;
  10376. #endif /* CKXXCHAR */
  10377. #ifdef TCPSOCKET
  10378.     extern CHAR stchr;
  10379. #endif /* TCPSOCKET */
  10380.     int x;
  10381. #ifdef STREAMING
  10382.     extern int streaming;
  10383.     extern int sndtyp;
  10384. #endif /* STREAMING */
  10385.  
  10386.     if (ttyfd < 0) return(-3);          /* Not open. */
  10387.  
  10388.     debug(F101,"ttinl max","",max);
  10389.     debug(F101,"ttinl timo","",timo);
  10390.  
  10391. #ifdef NETCMD
  10392.     if (ttpipe)
  10393.       fd = fdin;
  10394.     else
  10395. #endif /* NETCMD */
  10396.       fd = ttyfd;
  10397.  
  10398. #ifdef COMMENT
  10399.     if (xlocal && conchk() > 0)        /* Allow for console interruptions */
  10400.       return(-1);
  10401. #endif /* COMMENT */
  10402.  
  10403.     *dest = '\0';                       /* Clear destination buffer */
  10404.     if (timo < 0) timo = 0;        /* Safety */
  10405.     if (timo) {                /* Don't time out if timo == 0 */
  10406.     int xx;
  10407.     saval = signal(SIGALRM,timerh);    /* Enable timer interrupt */
  10408.     xx = alarm(timo);        /* Set it. */
  10409.     debug(F101,"ttinl alarm","",xx);
  10410.     }
  10411.     if (
  10412. #ifdef CK_POSIX_SIG
  10413.     sigsetjmp(sjbuf,1)
  10414. #else
  10415.     setjmp(sjbuf)
  10416. #endif /* CK_POSIX_SIG */
  10417.     ) {                /* Timer went off? */
  10418.     debug(F100,"ttinl timout","",0); /* Get here on timeout. */
  10419.     /* debug(F110," with",(char *) dest,0); */
  10420.     ttimoff();            /* Turn off timer */
  10421.     return(-1);            /* and return error code. */
  10422.     } else {
  10423.     register int i, n = -1;        /* local variables */
  10424.     int ccn = 0;
  10425. #ifdef PARSENSE
  10426.     register int flag = 0;
  10427.     debug(F000,"ttinl start","",start);
  10428. #endif /* PARSENSE */
  10429.  
  10430.     ttpmsk = ttprty ? 0177 : 0377;    /* Set parity stripping mask. */
  10431.     sopmask = needpchk ? 0177 : ttpmsk; /* And SOP matching mask. */
  10432.  
  10433. /* Now read into destination, stripping parity and looking for the */
  10434. /* the packet terminator, and also for several Ctrl-C's typed in a row. */
  10435.  
  10436.     i = 0;                /* Destination index */
  10437.     debug(F101,"ttinl eol","",eol);
  10438.  
  10439.     while (i < max-1) {
  10440. #ifdef MYREAD
  10441.         /* debug(F101,"ttinl i","",i); */
  10442.         errno = 0;
  10443.         if (csave > -1) {
  10444.             n = csave;
  10445.         debug(F101,"ttinl unsaving","",n);
  10446.         } else
  10447. #ifdef COMMENT
  10448.           if (xlocal && conchk() > 0) {
  10449.           /* Here we could catch keyboard interruptions. */
  10450.           /* But this would be VERY expensive. */
  10451.           /* We could also do it in myread() but it would be */
  10452.           /* expensive there too -- even if done with select()... */
  10453.           }
  10454. #endif /* COMMENT */
  10455.           if ((n = myread()) < 0) {    /* Timeout or i/o error? */
  10456. #ifdef DEBUG
  10457.         if (deblog) {
  10458.             debug(F101,"ttinl myread failure, n","",n);
  10459.             debug(F101,"ttinl myread errno","",errno);
  10460.         }
  10461. #endif /* DEBUG */
  10462.         /* Don't let EINTR break packets. */
  10463.         if (n == -3) {
  10464.             if (errno == EINTR && i > 0) {
  10465.             debug(F111,"ttinl EINTR myread i","continuing",i);
  10466.             continue;
  10467.             } else {
  10468.             debug(F110,"ttinl non-EINTR -3","closing",0);
  10469.             wasclosed = 1;
  10470.             ttimoff();    /* Turn off timer */
  10471.             ttclos(0);
  10472.             return(n);
  10473.             }
  10474.         } else if (n == -2 && netconn /* && timo == 0 */ ) {
  10475.             /* Here we try to catch broken network connections */
  10476.             /* even when ioctl() and read() do not catch them */
  10477.             debug(F111,"ttinl network myread failure","closing",n);
  10478.             wasclosed = 1;
  10479.             ttimoff();
  10480.             ttclos(0);
  10481.             return(-3);
  10482.         }
  10483. #ifdef STREAMING
  10484.         /* Streaming and no data to read */
  10485.         else if (n == 0 && streaming && sndtyp == 'D')
  10486.           return(0);
  10487. #endif /* STREAMING */
  10488.         break;            /* Break out of while loop */
  10489.         }
  10490.  
  10491. #else /* not MYREAD (is this code used anywhere any more?) */
  10492.  
  10493.         if (csave > -1)        /* Char saved from last time */
  10494.           ch = csave;
  10495.         else if ((n = read(fd, &ch, 1)) < 1)
  10496.           break;            /* Error - break out of while loop */
  10497.         n = ch;
  10498.  
  10499. #endif /* MYREAD */
  10500.  
  10501.         /* Get here with char in n */
  10502.  
  10503. #ifdef CK_ENCRYPTION
  10504.         /* If csave > -1 we already decrypted this character */
  10505.         /* So don't decrypt it again */
  10506.         if (TELOPT_U(TELOPT_ENCRYPTION) && csave == -1) {
  10507.         CHAR ch = n;
  10508.         ck_tn_decrypt(&ch,1);
  10509.         n = ch;
  10510.         }
  10511. #endif /* CK_ENCRYPTION */
  10512.  
  10513.         csave = -1;            /* Unflag that we unsaved a char */
  10514.  
  10515. #ifdef TCPSOCKET
  10516.         if (n == IAC &&        /* Handle Telnet options */
  10517.         ((xlocal && netconn && (ttnproto == NP_TELNET)) ||
  10518.         (!xlocal && sstelnet))) {
  10519.         n = tt_tnopt(n);
  10520.         if (n < 0)
  10521.           return(n);
  10522. #ifndef NOPARSEN
  10523.         else if (n == 1)
  10524.           start = stchr;
  10525. #endif /* NOPARSEN */
  10526.         if (n != 255)        /* No data - go back for next char */
  10527.           continue;
  10528.         }                /* Quoted IAC - keep going */
  10529. #endif /* TCPSOCKET */
  10530. #ifdef CKXXCHAR
  10531.         if (ignflag)
  10532.           if (dblt[(unsigned) n] & 1) /* Character to ignore? */
  10533.         continue;
  10534. #endif /* CKXXCHAR */
  10535.  
  10536. /*
  10537.   Use parity mask, rather than always stripping parity, to check for
  10538.   cancellation.  Otherwise, runs like \x03\x83\x03 in a packet could cancel
  10539.   the transfer when parity is NONE.  (Note that \x03\x03\x03 is extremely
  10540.   unlikely due to run-length encoding.)
  10541. */
  10542.         /* Check cancellation */
  10543.         if (!xlocal && xfrcan && ((n & ttpmsk) == xfrchr)) {
  10544.         if (++ccn >= xfrnum) {    /* If xfrnum in a row, bail out. */
  10545.             if (timo) {        /* Clear timer. */
  10546.             ttimoff();
  10547.             }
  10548.             if (xfrchr < 32)
  10549.               printf("^%c...\r\n",(char)(xfrchr+64));
  10550.             else
  10551.               printf("Canceled...\r\n");
  10552.             return(-2);
  10553.         }
  10554.         } else ccn = 0;        /* No cancellation, reset counter, */
  10555.  
  10556. #ifdef PARSENSE
  10557.         if (flag == 0) {        /* Find the Start-Of-Packet. */
  10558.         if ((n & sopmask) == start) { /* Got it */
  10559.             flag = 1;
  10560.         } else {        /* Keep looking... */
  10561.             debug(F000,"ttinl skipping","",n);
  10562.             continue;
  10563.         }
  10564.         }
  10565.         dest[i++] = n & ttpmsk;
  10566. /*
  10567.   If we have not been instructed to wait for a turnaround character, we
  10568.   can go by the packet length field.  If turn != 0, we must wait for the
  10569.   end of line (eol) character before returning.  This is an egregious
  10570.   violation of all principles of layering...
  10571. */
  10572.         if (!havelen) {
  10573.         if (i == 2) {
  10574.             pktlen = xunchar(dest[1] & 0x7f);
  10575.             if (pktlen > 1) {
  10576.             havelen = 1;
  10577.             debug(F101,"ttinl length","",pktlen);
  10578.             }
  10579.         } else if (i == 5 && pktlen == 0) {
  10580.             lplen = xunchar(dest[4] & 0x7f);
  10581.         } else if (i == 6 && pktlen == 0) {
  10582.             pktlen = lplen * 95 + xunchar(dest[5] & 0x7f) + 5;
  10583.             havelen = 1;
  10584.             debug(F101,"ttinl extended length","",pktlen);
  10585.         }
  10586.         }
  10587.  
  10588. /*
  10589.   Suppose we looked at the sequence number here and found it was out of
  10590.   range?  This would mean either (a) incoming packets had SOP unprefixed
  10591.   and we are out of sync, or (b) the packet is damaged.  Since (a) is bad
  10592.   practice, let's ignore it.  So what should we do here if we know the
  10593.   packet is damaged?
  10594.  
  10595.    1. Nothing -- keep trying to read the packet till we find what we think
  10596.       is the end, or we time out, and let the upper layer decide what to
  10597.       do.  But since either the packet is corrupt or we are out of sync,
  10598.       our criterion for finding the end does not apply and we are likely
  10599.       to time out (or swallow a piece of the next packet) if our assumed
  10600.       length is too long.  (This was the behavior prior to version 7.0.)
  10601.  
  10602.    2. set flag = 0 and continue?  This would force us to wait for the
  10603.       next packet to come in, and therefore (in the nonwindowing case),
  10604.       would force a timeout in the other Kermit.
  10605.  
  10606.    3. set flag = 0 and continue, but only if the window size is > 1 and
  10607.       the window is not blocked?  Talk about cheating!
  10608.  
  10609.    4. Return a failure code and let the upper layer decide what to do.
  10610.       This should be equivalent to 3, but without the cheating.  So let's
  10611.       do it that way...  But note that we must ignore the parity bit
  10612.       in case this is the first packet and we have not yet run parchk().
  10613. */
  10614.         if (i == 3) {        /* Peek at sequence number */
  10615.         x = xunchar((dest[i-1] & 0x7f)); /* If it's not in range... */
  10616.         if (x < 0 || x > 63) {
  10617.             debug(F111,"ttinl bad seq",dest,x);
  10618.             if (timo) ttimoff();
  10619.             return(-1);        /* return a nonfatal error */
  10620.         }
  10621.         }
  10622.  
  10623. #else /* PARSENSE */
  10624.         dest[i++] = n & ttpmsk;
  10625. #endif /* PARSENSE */
  10626.  
  10627.     /* Check for end of packet */
  10628.  
  10629.         if (
  10630. #ifdef PARSENSE
  10631. /*
  10632.   Purely length-driven if SET HANDSHAKE NONE (i.e. turn == 0).
  10633.   This allows packet terminators and handshake characters to appear
  10634.   literally inside a packet data field.
  10635. */
  10636.         (havelen && (i > pktlen+1) &&
  10637.          (!turn || (turn && (n & 0x7f) == turn))) /* (turn, not eol) */
  10638. #else /* !PARSENSE */
  10639. /*
  10640.   Built without PARSENSE, so just look for packet terminator.
  10641. */
  10642.         ((n & 0x7f) == eol)
  10643. #endif /* PARSENSE */
  10644.         ) {
  10645. #ifndef PARSENSE
  10646.         debug(F101,"ttinl got eol","",eol); /* (or turn) */
  10647.         dest[i] = '\0';        /* Yes, terminate the string, */
  10648.         /* debug(F101,"ttinl i","",i); */
  10649. #else
  10650. #ifdef DEBUG
  10651.         if (deblog) {
  10652.             if ((n & 0x7f) != eol) {
  10653.             debug(F101,"ttinl EOP length","",pktlen);
  10654.             debug(F101,"ttinl i","",i);
  10655. #ifdef MYREAD
  10656. #ifdef PARSENSE
  10657. /*
  10658.   We read a packet based on its length.  This leaves the EOP character still
  10659.   unread, and so ttchk() will always return at least 1 because of this.  But
  10660.   if we know it is there, we can safely get rid of it.  So...
  10661. */
  10662.             {
  10663.                 int x;
  10664.                 while (my_count > 0) {
  10665.                 x = ttinc(0);
  10666.                 /* Start of next packet */
  10667.                 if (x == start) { /* Save for next time */
  10668.                     csave = (unsigned)((unsigned)x & 0xff);
  10669.                     debug(F000,"ttinl csaved","",x);
  10670.                     break;
  10671.                 }
  10672.                 debug(F000,"ttinl removed","",x);
  10673.                 }
  10674.             }
  10675. #endif /* PARSENSE */
  10676. #endif /* MYREAD */
  10677.  
  10678.             } else debug(F101,"ttinl got eol","",eol); /* (or turn) */
  10679.         }
  10680. #endif /* DEBUG */
  10681.         dest[i] = '\0';        /* Terminate the string, */
  10682.             if (needpchk) {        /* Parity checked yet? */
  10683.             if (ttprty == 0) {    /* No, check. */
  10684.             if ((ttprty = parchk(dest,start,i)) > 0) {
  10685.                 int j;
  10686.                 debug(F101,"ttinl senses parity","",ttprty);
  10687.                 debug(F110,"ttinl packet before",dest,0);
  10688.                 ttpmsk = 0x7f;
  10689.                 for (j = 0; j < i; j++)
  10690.                   dest[j] &= 0x7f;    /* Strip parity from packet */
  10691.                 debug(F110,"ttinl packet after ",dest,0);
  10692.             } else ttprty = 0; /* Restore if parchk error */
  10693.             }
  10694.             sopmask = ttprty;
  10695.             needpchk = 0;
  10696.         }
  10697. #endif /* PARSENSE */
  10698.         if (timo) {        /* Turn off timer. */
  10699.             ttimoff();
  10700.         }
  10701. #ifdef COMMENT
  10702.         debug(F011,"ttinl got", dest, (i < 60) ? i : -60);
  10703. #else /* COMMENT */
  10704.                 hexdump("ttinl got",dest,i);
  10705. #endif /* COMMENT */
  10706. #ifdef STREAMING
  10707.         /* ttinl() was called because there was non-packet */
  10708.         /* data sitting int the channel.  Ignore it.       */
  10709.         if (streaming && sndtyp == 'D')
  10710.           return(-1);
  10711. #endif /* STREAMING */
  10712.         return(i);
  10713.         }
  10714.     } /* End of while() */
  10715.     ttimoff();
  10716.     return(n);
  10717.     }
  10718. }
  10719. #endif /* NOXFER */
  10720.  
  10721. /*  T T I N C --  Read a character from the communication line  */
  10722. /*
  10723.  On success, returns the character that was read, >= 0.
  10724.  On failure, returns -1 or other negative myread error code,
  10725.    or -2 if connection is broken or ttyfd < 0.
  10726.    or -3 if session limit has expired,
  10727.    or -4 if something or other...
  10728.  NOTE: The API does not provide for ttinc() returning a special code
  10729.  upon timeout, but we need it.  So for this we have a global variable,
  10730.  ttinctimo.
  10731. */
  10732. static int ttinctimo = 0;        /* Yuk */
  10733.  
  10734. int
  10735. ttinc(timo) int timo; {
  10736.  
  10737.     int n = 0, fd;
  10738.     int is_tn = 0;
  10739.     CHAR ch = 0;
  10740.  
  10741.     ttinctimo = 0;
  10742.  
  10743.     if (ttyfd < 0) return(-2);          /* Not open. */
  10744.  
  10745.     is_tn = (xlocal && netconn && ttnproto == NP_TELNET) ||
  10746.         (!xlocal && sstelnet);
  10747.  
  10748. #ifdef TTLEBUF
  10749.     if (ttpush >= 0) {
  10750.         debug(F111,"ttinc","ttpush",ttpush);
  10751.         ch = ttpush;
  10752.         ttpush = -1;
  10753.         return(ch);
  10754.     }
  10755.     if (le_data) {
  10756.         if (le_getchar(&ch) > 0) {
  10757.             debug(F111,"ttinc le_getchar","ch",ch);
  10758.             return(ch);
  10759.         }
  10760.     }
  10761. #endif /* TTLEBUF */
  10762.  
  10763. #ifdef NETCMD
  10764.     if (ttpipe)
  10765.       fd = fdin;
  10766.     else
  10767. #endif /* NETCMD */
  10768.       fd = ttyfd;
  10769.  
  10770.     if ((timo <= 0)            /* Untimed. */
  10771. #ifdef MYREAD
  10772.     || (my_count > 0)        /* Buffered char already waiting. */
  10773. #endif /* MYREAD */
  10774.     ) {
  10775. #ifdef MYREAD
  10776.         /* Comm line failure returns -1 thru myread, so no &= 0377 */
  10777.     n = myread();            /* Wait for a character... */
  10778.     /* debug(F000,"ttinc MYREAD n","",n); */
  10779. #ifdef CK_ENCRYPTION
  10780.     /* debug(F101,"ttinc u_encrypt","",TELOPT_U(TELOPT_ENCRYPTION)); */
  10781.     if (TELOPT_U(TELOPT_ENCRYPTION) && n >= 0) {
  10782.         ch = n;
  10783.         ck_tn_decrypt(&ch,1);
  10784.         n = ch;
  10785.     }
  10786. #endif /* CK_ENCRYPTION */
  10787.  
  10788. #ifdef NETPTY
  10789.     if (ttpty && n < 0) {
  10790.         debug(F101,"ttinc error on pty","",n);
  10791.         ttclos(0);
  10792.         return(n);
  10793.     }
  10794. #endif /* NETPTY */
  10795.  
  10796. #ifdef TNCODE
  10797.     if ((n > -1) && is_tn)
  10798.       return((unsigned)(n & 0xff));
  10799.     else
  10800. #endif /* TNCODE */
  10801.       return(n < 0 ? n : (unsigned)(n & ttpmsk));
  10802.  
  10803. #else  /* MYREAD */
  10804.  
  10805.         while ((n = read(fd,&ch,1)) == 0) /* Wait for a character. */
  10806.         /* Shouldn't have to loop in ver 5A. */
  10807. #ifdef NETCONN
  10808.       if (netconn) {        /* Special handling for net */
  10809.           netclos();        /* If read() returns 0 it means */
  10810.           netconn = 0;        /* the connection has dropped. */
  10811.           errno = ENOTCONN;
  10812.           return(-2);
  10813.       }
  10814. #endif /* NETCONN */
  10815.       ;
  10816.     /* debug(F101,"ttinc","",ch); */
  10817. #ifdef TNCODE
  10818.     if ((n > 0) && is_tn) {
  10819. #ifdef CK_ENCRYPTION
  10820.         if (TELOPT_U(TELOPT_ENCRYPTION)) {
  10821.         ck_tn_decrypt(&ch,1);
  10822.         n = ch;
  10823.         }
  10824. #endif /* CK_ENCRYPTION */
  10825.         return((unsigned)(ch & 0xff));
  10826.     } else
  10827. #endif /* TNCODE */
  10828.         return((n < 0) ? -4 : ((n == 0) ? -1 : (unsigned)(ch & ttpmsk)));
  10829. #endif /* MYREAD */
  10830.  
  10831.     } else {                /* Timed read */
  10832.  
  10833.     int oldalarm;
  10834.     saval = signal(SIGALRM,timerh);    /* Set up handler, save old one. */
  10835.     oldalarm = alarm(timo);        /* Set alarm, save old one. */
  10836.     if (
  10837. #ifdef CK_POSIX_SIG
  10838.         sigsetjmp(sjbuf,1)
  10839. #else
  10840.         setjmp(sjbuf)
  10841. #endif /* CK_POSIX_SIG */
  10842.         ) {                /* Timer expired */
  10843.         ttinctimo = 1;
  10844.         n = -1;            /* set flag */
  10845.     } else {
  10846. #ifdef MYREAD
  10847.         n = myread();        /* If managing own buffer... */
  10848.         debug(F101,"ttinc myread","",n);
  10849.         ch = n;
  10850. #else
  10851.         n = read(fd,&ch,1);        /* Otherwise call the system. */
  10852.         if (n == 0) n = -1;
  10853.         debug(F101,"ttinc read","",n);
  10854. #endif /* MYREAD */
  10855.  
  10856. #ifdef CK_ENCRYPTION
  10857.         if (TELOPT_U(TELOPT_ENCRYPTION) && n >= 0) {
  10858.         ck_tn_decrypt(&ch,1);
  10859.         }
  10860. #endif /* CK_ENCRYPTION */
  10861.         if (n >= 0)
  10862.           n = (unsigned) (ch & 0xff);
  10863.         else
  10864.           n = (n < 0) ? -4 : -2;    /* Special return codes. */
  10865.     }
  10866.     ttimoff();            /* Turn off the timer */
  10867.     if (oldalarm > 0) {
  10868.         if (n == -1)        /* and restore any previous alarm */
  10869.           oldalarm -= timo;
  10870.         if (oldalarm < 0)        /* adjusted by our timeout interval */
  10871.           oldalarm = 0;
  10872.         if (oldalarm) {
  10873.             debug(F101,"ttinc restoring oldalarm","",oldalarm);
  10874.         alarm(oldalarm);
  10875.         }
  10876.     }
  10877. #ifdef NETCONN
  10878.     if (netconn) {
  10879.         if (n == -2) {        /* read() returns 0 */
  10880.         netclos();        /* on network read failure */
  10881.         netconn = 0;
  10882.         errno = ENOTCONN;
  10883.         }
  10884.     }
  10885. #endif    /* NETCONN */
  10886. #ifdef TNCODE
  10887.     if ((n > -1) && is_tn)
  10888.       return((unsigned)(n & 0xff));
  10889.     else
  10890. #endif /* TNCODE */
  10891.       /* Return masked char or neg. */
  10892.       return( (n < 0) ? n : (unsigned)(n & ttpmsk) );
  10893.     }
  10894. }
  10895.  
  10896. /*  S N D B R K  --  Send a BREAK signal of the given duration  */
  10897.  
  10898. static int
  10899. #ifdef CK_ANSIC
  10900. sndbrk(int msec) {            /* Argument is milliseconds */
  10901. #else
  10902. sndbrk(msec) int msec; {
  10903. #endif /* CK_ANSIC */
  10904. #ifndef POSIX
  10905.     int x, n;
  10906. #endif /* POSIX */
  10907.  
  10908. #ifdef OXOS
  10909. #define BSDBREAK
  10910. #endif /* OXOS */
  10911.  
  10912. #ifdef ANYBSD
  10913. #define BSDBREAK
  10914. #endif /* ANYBSD */
  10915.  
  10916. #ifdef BSD44
  10917. #define BSDBREAK
  10918. #endif /* BSD44 */
  10919.  
  10920. #ifdef COHERENT
  10921. #ifdef BSDBREAK
  10922. #undef BSDBREAK
  10923. #endif /* BSDBREAK */
  10924. #endif /* COHERENT */
  10925.  
  10926. #ifdef BELLV10
  10927. #ifdef BSDBREAK
  10928. #undef BSDBREAK
  10929. #endif /* BSDBREAK */
  10930. #endif /* BELLV10 */
  10931.  
  10932. #ifdef PROVX1
  10933.     char spd;
  10934. #endif /* PROVX1 */
  10935.  
  10936.     debug(F101,"ttsndb ttyfd","",ttyfd);
  10937.     if (ttyfd < 0) return(-1);          /* Not open. */
  10938.  
  10939. #ifdef Plan9
  10940.     return p9sndbrk(msec);
  10941. #else
  10942. #ifdef NETCONN
  10943. #ifdef NETCMD
  10944.     if (ttpipe)                /* Pipe */
  10945.       return(ttoc('\0'));
  10946. #endif /* NETCMD */
  10947. #ifdef NETPTY
  10948.     if (ttpty)
  10949.       return(ttoc('\0'));
  10950. #endif /* NETPTY */
  10951.     if (netconn)             /* Send network BREAK */
  10952.       return(netbreak());
  10953. #endif /* NETCONN */
  10954.  
  10955.     if (msec < 1 || msec > 5000) return(-1); /* Bad argument */
  10956.  
  10957. #ifdef POSIX                /* Easy in POSIX */
  10958.     {
  10959.     int x;
  10960.     debug(F111,"sndbrk POSIX",ckitoa(msec),(msec/375));
  10961.     errno = 0;
  10962.     x = tcsendbreak(ttyfd,msec / 375);
  10963.     debug(F111,"sndbrk tcsendbreak",ckitoa(errno),x);
  10964.     return(x);
  10965.     }
  10966. #else
  10967. #ifdef PROVX1
  10968.     gtty(ttyfd,&ttbuf);                 /* Get current tty flags */
  10969.     spd = ttbuf.sg_ospeed;              /* Save speed */
  10970.     ttbuf.sg_ospeed = B50;              /* Change to 50 baud */
  10971.     stty(ttyfd,&ttbuf);                 /*  ... */
  10972.     n = (int)strlen(brnuls);        /* Send the right number of nulls */
  10973.     x = msec / 91;
  10974.     if (x > n) x = n;
  10975.     write(ttyfd,brnuls,n);
  10976.     ttbuf.sg_ospeed = spd;              /* Restore speed */
  10977.     stty(ttyfd,&ttbuf);                 /*  ... */
  10978.     return(0);
  10979. #else
  10980. #ifdef aegis
  10981.     sio_$control((short)ttyfd, sio_$send_break, msec, st);
  10982.     return(0);
  10983. #else
  10984. #ifdef BSDBREAK
  10985.     n = FWRITE;                         /* Flush output queue. */
  10986. /* Watch out for int vs long problems in &n arg! */
  10987.     debug(F101,"sndbrk BSDBREAK","",msec);
  10988.     ioctl(ttyfd,TIOCFLUSH,&n);          /* Ignore any errors.. */
  10989.     if (ioctl(ttyfd,TIOCSBRK,(char *)0) < 0) {  /* Turn on BREAK */
  10990.         perror("Can't send BREAK");
  10991.         return(-1);
  10992.     }
  10993.     x = msleep(msec);                    /* Sleep for so many milliseconds */
  10994.     if (ioctl(ttyfd,TIOCCBRK,(char *)0) < 0) {  /* Turn off BREAK */
  10995.         perror("BREAK stuck!!!");
  10996.         doexit(BAD_EXIT,-1);        /* Get out, closing the line. */
  10997.                                         /*   with bad exit status */
  10998.     }
  10999.     return(x);
  11000. #else
  11001. #ifdef ATTSV
  11002. /*
  11003.   No way to send a long BREAK in Sys V, so send a bunch of regular ones.
  11004.   (Actually, Sys V R4 is *supposed* to have the POSIX tcsendbreak() function,
  11005.   but there's no way for this code to know for sure.)
  11006. */
  11007.     debug(F101,"sndbrk ATTSV","",msec);
  11008.     x = msec / 275;
  11009.     for (n = 0; n < x; n++) {
  11010.     /* Reportedly the cast breaks this function on some systems */
  11011.     /* But then why was it here in the first place? */
  11012.     if (ioctl(ttyfd,TCSBRK, /* (char *) */ 0) < 0) {
  11013.         perror("Can't send BREAK");
  11014.         return(-1);
  11015.     }
  11016.     }
  11017.     return(0);
  11018. #else
  11019. #ifdef  V7
  11020.     debug(F101,"sndbrk V7","",msec);
  11021.     return(genbrk(ttyfd,250));        /* Simulate a BREAK */
  11022. #else
  11023.     debug(F101,"sndbrk catchall","",msec);
  11024.     ttoc(0);ttoc(0);ttoc(0);ttoc(0);
  11025.     return(0);
  11026. #endif /* V7 */
  11027. #endif /* BSDBREAK */
  11028. #endif /* ATTSV */
  11029. #endif /* aegis */
  11030. #endif /* PROVX1 */
  11031. #endif /* POSIX */
  11032. #endif /* Plan9 */
  11033. }
  11034.  
  11035. /*  T T S N D B  --  Send a BREAK signal  */
  11036.  
  11037. int
  11038. ttsndb() {
  11039. #ifdef TN_COMPORT
  11040.     if (netconn && istncomport())
  11041.       return((tnsndb(275L) >= 0) ? 0 : -1);
  11042.     else
  11043. #endif /* TN_COMPORT */
  11044.       return(sndbrk(275));
  11045. }
  11046.  
  11047. /*  T T S N D L B  --  Send a Long BREAK signal  */
  11048.  
  11049. int
  11050. ttsndlb() {
  11051. #ifdef TN_COMPORT
  11052.     if (netconn && istncomport())
  11053.       return((tnsndb(1800L) >= 0) ? 0 : -1);
  11054.     else
  11055. #endif /* TN_COMPORT */
  11056.     return(sndbrk(1500));
  11057. }
  11058.  
  11059. /*  M S L E E P  --  Millisecond version of sleep().  */
  11060.  
  11061. /*
  11062.   Call with number of milliseconds (thousandths of seconds) to sleep.
  11063.   Intended only for small intervals.  For big ones, just use sleep().
  11064.   Highly system-dependent.
  11065.   Returns 0 always, even if it didn't work.
  11066. */
  11067.  
  11068. /* Define MSLFTIME for systems that must use an ftime() loop. */
  11069. #ifdef ANYBSD                /* For pre-4.2 BSD versions */
  11070. #ifndef BSD4
  11071. #define MSLFTIME
  11072. #endif /* BSD4 */
  11073. #endif /* ANYBSD */
  11074.  
  11075. #ifdef TOWER1                /* NCR Tower OS 1.0 */
  11076. #define MSLFTIME
  11077. #endif /* TOWER1 */
  11078.  
  11079. #ifdef COHERENT         /* Coherent... */
  11080. #ifndef _I386           /* Maybe Coherent/386 should get this, too */
  11081. #define MSLFTIME        /* Opinions are divided */
  11082. #endif /* _I386 */
  11083. #endif /* COHERENT */
  11084.  
  11085. #ifdef COMMENT
  11086. #ifdef GETMSEC
  11087.  
  11088. /* Millisecond timer */
  11089.  
  11090. static long msecbase = 0L;        /* Unsigned long not portable */
  11091.  
  11092. long
  11093. getmsec() {                /* Milliseconds since base time */
  11094.     struct timeval xv;
  11095.     struct timezone xz;
  11096.     long secs, msecs;
  11097.     if (
  11098. #ifdef GTODONEARG
  11099.     gettimeofday(&tv)
  11100. #else
  11101. #ifdef PTX
  11102.     gettimeofday(&tv, NULL)
  11103. #else
  11104.     gettimeofday(&tv, &tz)
  11105. #endif /* PTX */
  11106. #endif /* GTODONEARG */
  11107.     < 0)
  11108.       return(-1);
  11109.     if (msecbase == 0L) {        /* First call, set base time. */
  11110.     msecbase = tv.tv_sec;
  11111.     debug(F101,"getmsec base","",msecbase);
  11112.     }
  11113.     return(((tv.tv_sec - msecbase) * 1000L) + (tv.tv_usec / 1000L));
  11114. }
  11115. #endif /* GETMSEC */
  11116. #endif /* COMMENT */
  11117.  
  11118. #ifdef SELECT
  11119. int
  11120. ttwait(fd, secs) int fd, secs; {
  11121.     int x;
  11122.     fd_set rfds;
  11123.     FD_ZERO(&rfds);
  11124.     FD_SET(fd,&rfds);
  11125.     tv.tv_sec = secs;
  11126.     tv.tv_usec = 0L;
  11127.     errno = 0;
  11128.     if ((x = select(FD_SETSIZE,
  11129. #ifdef HPUX9
  11130.             (int *)
  11131. #else
  11132. #ifdef HPUX1000
  11133.             (int *)
  11134. #endif /* HPUX1000 */
  11135. #endif /* HPUX9 */
  11136.             &rfds,
  11137.             0, 0, &tv)) < 0) {
  11138.     debug(F101,"ttwait select errno","",errno);
  11139.     return(0);
  11140.     } else {
  11141.     debug(F101,"ttwait OK","",errno);
  11142.     x = FD_ISSET(fd, &rfds);
  11143.     debug(F101,"ttwait select x","",x);
  11144.     return(x ? 1 : 0);
  11145.     }
  11146. }
  11147. #endif /* SELECT */
  11148.  
  11149. int
  11150. msleep(m) int m; {
  11151. /*
  11152.   Other possibilities here are:
  11153.    nanosleep(), reportedly defined in POSIX.4.
  11154.    sginap(), IRIX only (back to what IRIX version I don't know).
  11155. */
  11156. #ifdef Plan9
  11157.     return _SLEEP(m);
  11158. #else
  11159. #ifdef BEOSORBEBOX
  11160.     snooze(m*1000);
  11161. #else /* BEOSORBEBOX */
  11162. #ifdef SELECT
  11163.     int t1, x;
  11164.     debug(F101,"msleep SELECT 1","",m);
  11165.     if (m <= 0) return(0);
  11166.     if (m >= 1000) {            /* Catch big arguments. */
  11167.     sleep(m/1000);
  11168.     m = m % 1000;
  11169.     if (m < 10) return(0);
  11170.     }
  11171.     debug(F101,"msleep SELECT 2","",m);
  11172. #ifdef BELLV10
  11173.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, m );
  11174.     debug(F101,"msleep BELLV10 select","",x);
  11175. #else /* BELLV10 */
  11176. #ifdef HPUX9
  11177.     gettimeofday(&tv, &tz);
  11178. #else
  11179.  
  11180. #ifndef COHERENT
  11181. #ifdef GTODONEARG
  11182.     if (gettimeofday(&tv) < 0)
  11183. #else
  11184. #ifdef PTX
  11185.     if (gettimeofday(&tv,NULL) < 0)
  11186. #else
  11187. #ifdef NOTIMEZONE
  11188.     if (gettimeofday(&tv, NULL) < 0)    /* wonder what this does... */
  11189. #else
  11190.     if (gettimeofday(&tv, &tz) < 0)
  11191. #endif /* NOTIMEZONE */
  11192. #endif /* PTX */
  11193. #endif /* GTODONEARG */
  11194.       return(-1);
  11195.     t1 = tv.tv_sec;                     /* Seconds */
  11196. #endif /* COHERENT */
  11197. #endif /* HPUX9 */
  11198.     tv.tv_sec = 0;                      /* Use select() */
  11199.     tv.tv_usec = m * 1000L;
  11200. #ifdef BSD44
  11201.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11202.     debug(F101,"msleep BSD44 select","",x);
  11203. #else /* BSD44 */
  11204. #ifdef __linux__
  11205.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11206.     debug(F101,"msleep __linux__ select","",x);
  11207. #else /* __linux__ */
  11208. #ifdef BSD43
  11209.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11210.     debug(F101,"msleep BSD43 select","",x);
  11211. #else /* BSD43 */
  11212. #ifdef QNX6
  11213.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11214.     debug(F101,"msleep QNX6 select","",x);
  11215. #else /* QNX6 */
  11216. #ifdef QNX
  11217.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11218.     debug(F101,"msleep QNX select","",x);
  11219. #else /* QNX */
  11220. #ifdef COHERENT
  11221.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11222.     debug(F101,"msleep COHERENT select","",x);
  11223. #else /* COHERENT */
  11224. #ifdef HPUX1000                /* 10.00 only, not 10.10 or later */
  11225.     x = select( 0, (int *)0, (int *)0, (int *)0, &tv );
  11226.     debug(F101,"msleep HP-UX 10.00 select","",x);
  11227. #else /* HPUX1000 */
  11228. #ifdef SVR4
  11229.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11230.     debug(F101,"msleep SVR4 select","",x);
  11231. #else /* SVR4 */
  11232. #ifdef OSF40
  11233.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11234.     debug(F101,"msleep OSF40 select","",x);
  11235. #else /* OSF40 */
  11236. #ifdef PTX
  11237.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11238.     debug(F101,"msleep OSF40 select","",x);
  11239. #else
  11240.     x = select( 0, (int *)0, (int *)0, (int *)0, &tv );
  11241.     debug(F101,"msleep catch-all select","",x);
  11242. #endif /* PTX */
  11243. #endif /* OSF40 */
  11244. #endif /* HP1000 */
  11245. #endif /* SVR4 */
  11246. #endif /* COHERENT */
  11247. #endif /* QNX */
  11248. #endif /* QNX6 */
  11249. #endif /* BSD43 */
  11250. #endif /* __linux__ */
  11251. #endif /* BSD44 */
  11252. #endif /* BELLV10 */
  11253.     return(0);
  11254.  
  11255. #else                    /* Not SELECT */
  11256. #ifdef CK_POLL                /* We have poll() */
  11257.     struct pollfd pfd;            /* Supply a valid address for poll() */
  11258.  
  11259. #ifdef ODT30                /* But in SCO ODT 3.0 */
  11260. #ifdef NAP                /* we should use nap() instead */
  11261.     debug(F101,"msleep ODT 3.0 NAP","",m); /* because using poll() here */
  11262.     nap((long)m);               /* seems to break dialing. */
  11263.     return(0);
  11264. #else
  11265.     debug(F101,"msleep ODT 3.0 POLL","",m);
  11266.     poll(&pfd, 0, m);
  11267.     return(0);
  11268. #endif /* NAP */
  11269. #else
  11270.     debug(F101,"msleep POLL","",m);
  11271.     poll(&pfd, 0, m);
  11272.     return(0);
  11273. #endif /* ODT30 */
  11274.  
  11275. /*
  11276.   We could handle the above more cleanly by just letting nap() always
  11277.   take precedence over poll() in this routine, but there is no way to know
  11278.   whether that would break something else.
  11279. */
  11280.  
  11281. #else                    /* Not POLL */
  11282. #ifdef USLEEP
  11283. /*
  11284.   "This routine is implemented using setitimer(2); it requires eight
  11285.   system calls...".  In other words, it might take 5 minutes to sleep
  11286.   10 milliseconds...
  11287. */
  11288.     debug(F101,"msleep USLEEP","",m);
  11289.     if (m >= 1000) {            /* Catch big arguments. */
  11290.     sleep(m/1000);
  11291.     m = m % 1000;
  11292.     if (m < 10) return(0);
  11293.     }
  11294.     usleep((unsigned int)(m * 1000));
  11295.     return(0);
  11296. #else
  11297. #ifdef aegis
  11298.     time_$clock_t dur;
  11299.     debug(F101,"msleep aegis","",m);
  11300.     dur.c2.high16 = 0;
  11301.     dur.c2.low32  = 250 * m; /* one millisecond = 250 four microsecond ticks */
  11302.     time_$wait(time_$relative, dur, st);
  11303.     return(0);
  11304. #else
  11305. #ifdef PROVX1
  11306.     debug(F101,"msleep Venix","",m);
  11307.     if (m <= 0) return(0);
  11308.     sleep(-((m * 60 + 500) / 1000));
  11309.     return(0);
  11310. #else
  11311. #ifdef NAP
  11312.     debug(F101,"msleep NAP","",m);
  11313.     nap((long)m);
  11314.     return(0);
  11315. #else
  11316. #ifdef ATTSV
  11317. #ifndef BSD44
  11318.     extern long times();        /* Or #include <times.h> ? */
  11319. #endif /* BSD44 */
  11320.     long t1, t2, tarray[4];
  11321.     int t3;
  11322.     char *cp = getenv("HZ");
  11323.     int CLOCK_TICK;
  11324.     int hertz;
  11325.  
  11326.     if (cp && (hertz = atoi(cp))) {
  11327.         CLOCK_TICK  = 1000 / hertz;
  11328.     } else {                /* probably single user mode */
  11329. #ifdef HZ
  11330.         CLOCK_TICK  = 1000 / HZ;
  11331. #else
  11332.     static warned = 0;
  11333.     /* HZ always exists in, for instance, SCO Xenix, so you don't have to
  11334.      * make special #ifdefs for XENIX here, like in ver 4F. Also, if you
  11335.      * have Xenix, you have should have nap(), so the best is to use -DNAP
  11336.      * in the makefile. Most systems have HZ.
  11337.      */
  11338.     CLOCK_TICK = 17;        /* 1/60 sec */
  11339.     if (!warned) {
  11340.           printf("warning: environment variable HZ bad... using HZ=%d\r\n",
  11341.          1000 / CLOCK_TICK);
  11342.           warned = 1;
  11343.     }
  11344. #endif /* !HZ */
  11345.     }
  11346.     debug(F101,"msleep ATTSV","",m);
  11347.     if (m <= 0) return(0);
  11348.     if (m >= 1000) {            /* Catch big arguments. */
  11349.     sleep(m/1000);
  11350.     m = m % 1000;
  11351.     if (m < 10) return(0);
  11352.     }
  11353.     if ((t1 = times(tarray)) < 0) return(-1);
  11354.     while (1) {
  11355.         if ((t2 = times(tarray)) < 0) return(-1);
  11356.         t3 = ((int)(t2 - t1)) * CLOCK_TICK;
  11357.         if (t3 > m) return(t3);
  11358.     }
  11359. #else /* Not ATTSV */
  11360. #ifdef MSLFTIME                /* Use ftime() loop... */
  11361.     int t1, t3 = 0;
  11362.     debug(F101,"msleep MSLFTIME","",m);
  11363.     if (m <= 0) return(0);
  11364.     if (m >= 1000) {            /* Catch big arguments. */
  11365.     sleep(m/1000);
  11366.     m = m % 1000;
  11367.     if (m < 10) return(0);
  11368.     }
  11369. #ifdef QNX
  11370.     ftime(&ftp);            /* void ftime() in QNX */
  11371. #else
  11372.     if (ftime(&ftp) < 0) return(-1);    /* Get base time. */
  11373. #endif /* QNX */
  11374.     t1 = ((ftp.time & 0xff) * 1000) + ftp.millitm;
  11375.     while (1) {
  11376.         ftime(&ftp);            /* Get current time and compare. */
  11377.         t3 = (((ftp.time & 0xff) * 1000) + ftp.millitm) - t1;
  11378.         if (t3 > m) return(0);
  11379.     }
  11380. #else
  11381. /* This includes true POSIX, which has no way to do this. */
  11382.     debug(F101,"msleep busy loop","",m);
  11383.     if (m >= 1000) {            /* Catch big arguments. */
  11384.     sleep(m/1000);
  11385.     m = m % 1000;
  11386.     if (m < 10) return(0);
  11387.     }
  11388.     if (m > 0) while (m > 0) m--;    /* Just a dumb busy loop */
  11389.     return(0);
  11390. #endif /* MSLFTIME */
  11391. #endif /* ATTSV */
  11392. #endif /* NAP */
  11393. #endif /* PROVX1 */
  11394. #endif /* aegis */
  11395. #endif /* CK_POLL */
  11396. #endif /* SELECT */
  11397. #endif /* BEOSORBEBOX */
  11398. #endif /* USLEEP */
  11399. #endif /* Plan9 */
  11400. }
  11401.  
  11402. /*  R T I M E R --  Reset elapsed time counter  */
  11403.  
  11404. VOID
  11405. rtimer() {
  11406.     tcount = time( (time_t *) 0 );
  11407. }
  11408.  
  11409.  
  11410. /*  G T I M E R --  Get current value of elapsed time counter in seconds  */
  11411.  
  11412. int
  11413. gtimer() {
  11414.     int x;
  11415.     x = (int) (time( (time_t *) 0 ) - tcount);
  11416.     debug(F101,"gtimer","",x);
  11417.     return( (x < 0) ? 0 : x );
  11418. }
  11419.  
  11420. #ifdef GFTIMER
  11421. /*
  11422.   Floating-point timers.  Require not only floating point support, but
  11423.   also gettimeofday().
  11424. */
  11425. static struct timeval tzero;
  11426.  
  11427. VOID
  11428. rftimer() {
  11429. #ifdef GTODONEARG    /* Account for Mot's definition */
  11430.     (VOID) gettimeofday(&tzero);
  11431. #else
  11432.     (VOID) gettimeofday(&tzero, (struct timezone *)0);
  11433. #endif /* GTODONEARG */
  11434. }
  11435.  
  11436. CKFLOAT
  11437. gftimer() {
  11438.     struct timeval tnow, tdelta;
  11439.     CKFLOAT s;
  11440. #ifdef DEBUG
  11441.     char fpbuf[64];
  11442. #endif /* DEBUG */
  11443. #ifdef GTODONEARG    /* Acount for Mot's definition */
  11444.     (VOID) gettimeofday(&tnow);
  11445. #else
  11446.     (VOID) gettimeofday(&tnow, (struct timezone *)0);
  11447. #endif /* GTODONEARG */
  11448.     tdelta.tv_sec = tnow.tv_sec - tzero.tv_sec;
  11449.     tdelta.tv_usec = tnow.tv_usec - tzero.tv_usec;
  11450.     if (tdelta.tv_usec < 0) {
  11451.     tdelta.tv_sec--;
  11452.     tdelta.tv_usec += 1000000;
  11453.     }
  11454.     s = (CKFLOAT) tdelta.tv_sec + ((CKFLOAT) tdelta.tv_usec / 1000000.0);
  11455.     if (s < GFMINTIME)
  11456.       s = GFMINTIME;
  11457. #ifdef DEBUG
  11458.     if (deblog) {
  11459.     sprintf(fpbuf,"%f",s);
  11460.     debug(F110,"gftimer",fpbuf,0);
  11461.     }
  11462. #endif /* DEBUG */
  11463.     return(s);
  11464. }
  11465. #endif /* GFTIMER */
  11466.  
  11467. /*  Z T I M E  --  Return asctime()-format date/time string  */
  11468. /*
  11469.   NOTE: as a side effect of calling this routine, we can also set the
  11470.   following two variables, giving the micro- and milliseconds (fractions of
  11471.   seconds) of the clock time.  Currently this is done only in BSD-based builds
  11472.   that use gettimeofday().  When these variables are not filled in, they are
  11473.   left with a value of -1L.
  11474. */
  11475. VOID
  11476. ztime(s) char **s; {
  11477.  
  11478. #ifdef GFTIMER
  11479. /*
  11480.   The gettimeofday() method, which also sets ztmsec and ztusec, works for
  11481.   all GFTIMER builds.  NOTE: ztmsec and ztusec are defined in ckcmai.c,
  11482.   and extern declarations for them are in ckcdeb.h; thus they are
  11483.   declared in this file by inclusion of ckcdeb.h.
  11484. */
  11485.     char *asctime();
  11486.     struct tm *localtime();
  11487.     struct tm *tp;
  11488.     ztmsec = -1L;
  11489.     ztusec = -1L;
  11490.  
  11491.     if (!s)
  11492.       debug(F100,"ztime s==NULL","",0);
  11493.  
  11494. #ifdef GTODONEARG
  11495.     /* No 2nd arg in Motorola SV88 and some others */
  11496.     if (gettimeofday(&tv) > -1)
  11497. #else
  11498. #ifndef COHERENT
  11499. #ifdef PTX
  11500.     if (gettimeofday(&tv,NULL) > -1)
  11501. #else
  11502. #ifdef NOTIMEZONE
  11503.     if (gettimeofday(&tv, NULL) > -1)    /* wonder what this does... */
  11504. #else
  11505.     if (gettimeofday(&tv, &tz) > -1)
  11506. #endif /* NOTIMEZONE */
  11507. #endif /* PTX */
  11508. #endif /* COHERENT */
  11509. #endif /* GTODONEARG */
  11510.       {                    /* Fill in tm struct */
  11511.     ztusec = tv.tv_usec;        /* Microseconds */
  11512.     ztmsec = ztusec / 1000L;    /* Milliseconds */
  11513. #ifdef HPUX9
  11514.     {
  11515.         time_t zz;
  11516.         zz = tv.tv_sec;
  11517.         tp = localtime(&zz);    /* Convert to local time */
  11518.     }
  11519. #else
  11520. #ifdef HPUX1000
  11521.     {
  11522.         time_t zz;
  11523.         zz = tv.tv_sec;
  11524.         tp = localtime(&zz);
  11525.     }
  11526. #else
  11527. #ifdef LINUX
  11528.     {   /* avoid unaligned access trap on 64-bit platforms */
  11529.         time_t zz;
  11530.         zz = tv.tv_sec;
  11531.         tp = localtime(&zz);
  11532.     }
  11533. #else
  11534. #ifdef MACOSX
  11535.     tp = localtime((time_t *)&tv.tv_sec); /* Convert to local time */
  11536. #else
  11537.     tp = localtime(&tv.tv_sec);
  11538. #endif /* MACOSX */
  11539. #endif /* LINUX */
  11540. #endif /* HPUX1000 */
  11541. #endif /* HPUX9 */
  11542.     if (s) {
  11543.         *s = asctime(tp);        /* Convert result to ASCII string */
  11544.         debug(F111,"ztime GFTIMER gettimeofday",*s,ztusec);
  11545.     }
  11546.     }
  11547. #else  /* Not GFTIMER */
  11548.  
  11549. #undef ZTIMEV7                /* Which systems need to use */
  11550. #ifdef COHERENT                /* old UNIX Version 7 way... */
  11551. #define ZTIMEV7
  11552. #endif /* COHERENT */
  11553. #ifdef TOWER1
  11554. #define ZTIMEV7
  11555. #endif /* TOWER1 */
  11556. #ifdef ANYBSD
  11557. #ifndef BSD42
  11558. #define ZTIMEV7
  11559. #endif /* BSD42 */
  11560. #endif /* ANYBSD */
  11561. #ifdef V7
  11562. #ifndef MINIX
  11563. #define ZTIMEV7
  11564. #endif /* MINIX */
  11565. #endif /* V7 */
  11566. #ifdef POSIX
  11567. #define ZTIMEV7
  11568. #endif /* POSIX */
  11569.  
  11570. #ifdef HPUX1020
  11571. /*
  11572.   Prototypes are in <time.h>, included above.
  11573. */
  11574.     time_t clock_storage;
  11575.     clock_storage = time((void *) 0);
  11576.     if (s) {
  11577.     *s = ctime(&clock_storage);
  11578.     debug(F110,"ztime: HPUX 10.20",*s,0);
  11579.     }
  11580. #else
  11581. #ifdef ATTSV                /* AT&T way */
  11582. /*  extern long time(); */        /* Theoretically these should */
  11583.     char *ctime();            /* already been dcl'd in <time.h> */
  11584.     time_t clock_storage;
  11585.     clock_storage = time(
  11586. #ifdef IRIX60
  11587.              (time_t *)
  11588. #else
  11589. #ifdef BSD44
  11590.              (time_t *)
  11591. #else
  11592.              (long *)
  11593. #endif /* BSD44 */
  11594. #endif /* IRIX60 */
  11595.              0 );
  11596.     if (s) {
  11597.     *s = ctime( &clock_storage );
  11598.     debug(F110,"ztime: ATTSV",*s,0);
  11599.     }
  11600. #else
  11601. #ifdef PROVX1                /* Venix 1.0 way */
  11602.     int utime[2];
  11603.     time(utime);
  11604.     if (s) {
  11605.     *s = ctime(utime);
  11606.     debug(F110,"ztime: PROVX1",*s,0);
  11607.     }
  11608. #else
  11609. #ifdef BSD42                /* 4.2BSD way */
  11610.     char *asctime();
  11611.     struct tm *localtime();
  11612.     struct tm *tp;
  11613.     gettimeofday(&tv, &tz);
  11614.     ztusec = tv.tv_usec;
  11615.     ztmsec = tv.tv_usec / 1000L;
  11616.     tp = localtime(&tv.tv_sec);
  11617.     if (s) {
  11618.     *s = asctime(tp);
  11619.     debug(F111,"ztime: BSD42",*s,ztusec);
  11620.     }
  11621. #else
  11622. #ifdef MINIX                /* MINIX way */
  11623. #ifdef COMMENT
  11624.     extern long time();            /* Already got these from <time.h> */
  11625.     extern char *ctime();
  11626. #endif /* COMMENT */
  11627.     time_t utime[2];
  11628.     time(utime);
  11629.     if (s) {
  11630.     *s = ctime(utime);
  11631.     debug(F110,"ztime: MINIX",*s,0);
  11632.     }
  11633. #else
  11634. #ifdef ZTIMEV7                /* The regular way */
  11635.     char *asctime();
  11636.     struct tm *localtime();
  11637.     struct tm *tp;
  11638.     long xclock;            /* or unsigned long for BeBox? */
  11639.     time(&xclock);
  11640.     tp = localtime(&xclock);
  11641.     if (s) {
  11642.     *s = asctime(tp);
  11643.     debug(F110,"ztime: ZTIMEV7",*s,0);
  11644.     }
  11645. #else                    /* Catch-all for others... */
  11646.     if (s) {
  11647.     *s = "Day Mon 00 00:00:00 0000\n"; /* Dummy in asctime() format */
  11648.     debug(F110,"ztime: catch-all",*s,0);
  11649.     }
  11650. #endif /* ZTIMEV7 */
  11651. #endif /* MINIX */
  11652. #endif /* BSD42 */
  11653. #endif /* PROVX1 */
  11654. #endif /* ATTSV */
  11655. #endif /* HPUX1020 */
  11656. #endif /* GFTIMER */
  11657. }
  11658.  
  11659. /*  C O N G M  --  Get console terminal modes.  */
  11660.  
  11661. /*
  11662.   Saves initial console mode, and establishes variables for switching
  11663.   between current (presumably normal) mode and other modes.
  11664.   Should be called when program starts, but only after establishing
  11665.   whether program is in the foreground or background.
  11666.   Returns 1 if it got the modes OK, 0 if it did nothing, -1 on error.
  11667. */
  11668. int
  11669. congm() {
  11670.     int fd;
  11671.     if (backgrd || !isatty(0)) {    /* If in background. */
  11672.     cgmf = -1;            /* Don't bother, modes are garbage. */
  11673.     return(-1);
  11674.     }
  11675.     if (cgmf > 0) return(0);        /* Already did this. */
  11676.     debug(F100,"congm getting modes","",0); /* Need to do it. */
  11677. #ifdef aegis
  11678.     ios_$inq_type_uid(ios_$stdin, conuid, st);
  11679.     if (st.all != status_$ok) {
  11680.     fprintf(stderr, "problem getting stdin objtype: ");
  11681.     error_$print(st);
  11682.     }
  11683.     concrp = (conuid == mbx_$uid);
  11684.     conbufn = 0;
  11685. #endif /* aegis */
  11686.  
  11687. #ifndef BEBOX
  11688.     if ((fd = open(CTTNAM,2)) < 0) {    /* Open controlling terminal */
  11689. #ifdef COMMENT
  11690.     fprintf(stderr,"Error opening %s\n", CTTNAM);
  11691.     perror("congm");
  11692.     return(-1);
  11693. #else
  11694.     fd = 0;
  11695. #endif /* COMMENT */
  11696.     }
  11697. #else
  11698.     fd = 0;
  11699. #endif /* !BEBOX */
  11700. #ifdef BSD44ORPOSIX
  11701.     if (tcgetattr(fd,&ccold) < 0) return(-1);
  11702.     if (tcgetattr(fd,&cccbrk) < 0) return(-1);
  11703.     if (tcgetattr(fd,&ccraw) < 0) return(-1);
  11704. #else
  11705. #ifdef ATTSV
  11706.     if (ioctl(fd,TCGETA,&ccold)  < 0) return(-1);
  11707.     if (ioctl(fd,TCGETA,&cccbrk) < 0) return(-1);
  11708.     if (ioctl(fd,TCGETA,&ccraw)  < 0) return(-1);
  11709. #ifdef VXVE
  11710.     cccbrk.c_line = 0;            /* STTY line 0 for CDC VX/VE */
  11711.     if (ioctl(fd,TCSETA,&cccbrk) < 0) return(-1);
  11712.     ccraw.c_line = 0;            /* STTY line 0 for CDC VX/VE */
  11713.     if (ioctl(fd,TCSETA,&ccraw) < 0) return(-1);
  11714. #endif /* VXVE */
  11715. #else
  11716. #ifdef BELLV10
  11717.     if (ioctl(fd,TIOCGETP,&ccold) < 0) return(-1);
  11718.     if (ioctl(fd,TIOCGETP,&cccbrk) < 0) return(-1);
  11719.     if (ioctl(fd,TIOCGETP,&ccraw) < 0) return(-1);
  11720.     debug(F101,"cccbrk.sg_flags orig","", cccbrk.sg_flags);
  11721. #else
  11722.     if (gtty(fd,&ccold) < 0) return(-1);
  11723.     if (gtty(fd,&cccbrk) < 0) return(-1);
  11724.     if (gtty(fd,&ccraw) < 0) return(-1);
  11725. #endif /* BELLV10 */
  11726. #endif /* ATTSV */
  11727. #endif /* BSD44ORPOSIX */
  11728. #ifdef sony_news            /* Sony NEWS */
  11729.     if (ioctl(fd,TIOCKGET,&km_con) < 0) { /* Get console Kanji mode */
  11730.     perror("congm error getting Kanji mode");
  11731.     debug(F101,"congm error getting Kanji mode","",0);
  11732.     km_con = -1;            /* Make sure this stays undefined. */
  11733.     return(-1);
  11734.     }
  11735. #endif /* sony_news */
  11736.     if (fd > 0)
  11737.       close(fd);
  11738.     cgmf = 1;                /* Flag that we got them. */
  11739.     return(1);
  11740. }
  11741.  
  11742.  
  11743. static VOID
  11744. congetbuf(x) int x; {
  11745.     int n;
  11746.     n = CONBUFSIZ - (conbufp - conbuf);    /* How much room left in buffer? */
  11747.     if (x > n) {
  11748.     debug(F101,"congetbuf char loss","",x-n);
  11749.     x = n;
  11750.     }
  11751.     x = read(0,conbufp,x);
  11752.     conbufn += x;
  11753.     debug(F111,"congetbuf readahead",conbuf,x);
  11754. }
  11755.  
  11756.  
  11757. /*  C O N C B --  Put console in cbreak mode.  */
  11758.  
  11759. /*  Returns 0 if ok, -1 if not  */
  11760.  
  11761. int
  11762. #ifdef CK_ANSIC
  11763. concb(char esc)
  11764. #else
  11765. concb(esc) char esc;
  11766. #endif /* CK_ANSIC */
  11767. /* concb */ {
  11768.     int x;
  11769.     debug(F101,"concb constate","",constate);
  11770.     debug(F101,"concb cgmf","",cgmf);
  11771.     debug(F101,"concb backgrd","",backgrd);
  11772.  
  11773.     if (constate == CON_CB)
  11774.       return(0);
  11775.  
  11776.     if (cgmf < 1)            /* Did we get console modes yet? */
  11777.       if (!backgrd)            /* No, in background? */
  11778.     congm();            /* No, try to get them now. */
  11779.     if (cgmf < 1)            /* Still don't have them? */
  11780.       return(0);            /* Give up. */
  11781.     debug(F101,"concb ttyfd","",ttyfd);
  11782.     debug(F101,"concb ttfdflg","",ttfdflg);
  11783. #ifdef COMMENT
  11784.     /* This breaks returning to prompt after protocol with "-l 0" */
  11785.     /* Commented out July 1998 */
  11786.     if (ttfdflg && ttyfd >= 0 && ttyfd < 3)
  11787.       return(0);
  11788. #endif /* COMMENT */
  11789.     x = isatty(0);
  11790.     debug(F101,"concb isatty","",x);
  11791.     if (!x) return(0);            /* Only when running on real ttys */
  11792.     debug(F101,"concb suspend","",suspend);
  11793.     if (backgrd)            /* Do nothing if in background. */
  11794.       return(0);
  11795.     escchr = esc;                       /* Make this available to other fns */
  11796.     ckxech = 1;                         /* Program can echo characters */
  11797. #ifdef aegis
  11798.     conbufn = 0;
  11799.     if (concrp) return(write(1, "\035\002", 2));
  11800.     if (conuid == input_pad_$uid) {pad_$raw(ios_$stdin, st); return(0);}
  11801. #endif /* aegis */
  11802.  
  11803. #ifdef COHERENT
  11804. #define SVORPOSIX
  11805. #endif /* COHERENT */
  11806.  
  11807. #ifdef Plan9
  11808.     x = p9concb();
  11809. #else
  11810. #ifndef SVORPOSIX            /* BSD, V7, etc */
  11811.     debug(F101,"cccbrk.sg_flags concb 1","", cccbrk.sg_flags);
  11812.     debug(F101,"concb stty CBREAK","",0);
  11813.     cccbrk.sg_flags |= (CBREAK|CRMOD);    /* Set to character wakeup, */
  11814.     cccbrk.sg_flags &= ~ECHO;           /* no echo. */
  11815.     debug(F101,"cccbrk.sg_flags concb 2","", cccbrk.sg_flags);
  11816.     errno = 0;
  11817. /*
  11818.   BSD stty() clears the console buffer.  So if anything is waiting in it,
  11819.   we have to read it now to avoid losing it.
  11820. */
  11821.     x = conchk();
  11822.     if (x > 0)
  11823.       congetbuf(x);
  11824.  
  11825. #ifdef BELLV10
  11826.     x = ioctl(0,TIOCSETP,&cccbrk);
  11827. #else
  11828.     x = stty(0,&cccbrk);
  11829.     debug(F101,"cccbrk.sg_flags concb x","", x);
  11830. #endif /* BELLV10 */
  11831. #else                    /* Sys V and POSIX */
  11832. #ifndef OXOS
  11833.     debug(F101,"concb cccbrk.c_flag","",cccbrk.c_lflag);
  11834. #ifdef QNX
  11835.     /* Don't mess with IEXTEN */
  11836.     cccbrk.c_lflag &= ~(ICANON|ECHO);
  11837. #else
  11838. #ifdef COHERENT
  11839.     cccbrk.c_lflag &= ~(ICANON|ECHO);
  11840. #else
  11841.     cccbrk.c_lflag &= ~(ICANON|ECHO|IEXTEN);
  11842. #endif /* COHERENT */
  11843. #endif /* QNX */
  11844.     cccbrk.c_lflag |= ISIG;        /* Allow signals in command mode. */
  11845.     cccbrk.c_iflag |= IGNBRK;        /* But ignore BREAK signal */
  11846.     cccbrk.c_iflag &= ~BRKINT;
  11847.  
  11848. #else /* OXOS */
  11849.     debug(F100,"concb OXOS is defined","",0);
  11850.     cccbrk.c_lflag &= ~(ICANON|ECHO);
  11851.     cccbrk.c_cc[VDISCARD] = cccbrk.c_cc[VLNEXT] = CDISABLE;
  11852. #endif /* OXOS */
  11853. #ifdef COMMENT
  11854. /*
  11855.   Believe it or not, in SCO UNIX, VSUSP is greater than NCC, and so this
  11856.   array reference is out of bounds.  It's only a debug() call so who needs it.
  11857. */
  11858. #ifdef VSUSP
  11859.     debug(F101,"concb c_cc[VSUSP]","",cccbrk.c_cc[VSUSP]);
  11860. #endif /* VSUSP */
  11861. #endif /* COMMENT */
  11862. #ifndef VINTR
  11863.     debug(F101,"concb c_cc[0]","",cccbrk.c_cc[0]);
  11864.     cccbrk.c_cc[0] = 003;               /* Interrupt char is Control-C */
  11865. #else
  11866.     debug(F101,"concb c_cc[VINTR]","",cccbrk.c_cc[0]);
  11867.     cccbrk.c_cc[VINTR] = 003;
  11868. #endif /* VINTR */
  11869. #ifndef VQUIT
  11870.     cccbrk.c_cc[1] = escchr;            /* escape during packet modes */
  11871. #else
  11872.     cccbrk.c_cc[VQUIT] = escchr;
  11873. #endif /* VQUIT */
  11874. #ifndef VEOF
  11875.     cccbrk.c_cc[4] = 1;
  11876. #else
  11877. #ifndef OXOS
  11878. #ifdef VMIN
  11879.     cccbrk.c_cc[VMIN] = 1;
  11880. #endif /* VMIN */
  11881. #else /* OXOS */
  11882.     cccbrk.c_min = 1;
  11883. #endif /* OXOS */
  11884. #endif /* VEOF */
  11885. #ifdef ZILOG
  11886.     cccbrk.c_cc[5] = 0;
  11887. #else
  11888. #ifndef VEOL
  11889.     cccbrk.c_cc[5] = 1;
  11890. #else
  11891. #ifndef OXOS
  11892. #ifdef VTIME
  11893.     cccbrk.c_cc[VTIME] = 1;
  11894. #endif /* VTIME */
  11895. #else /* OXOS */
  11896.     cccbrk.c_time = 1;
  11897. #endif /* OXOS */
  11898. #endif /* VEOL */
  11899. #endif /* ZILOG */
  11900.     errno = 0;
  11901. #ifdef BSD44ORPOSIX            /* Set new modes */
  11902.     x = tcsetattr(0,TCSADRAIN,&cccbrk);
  11903. #else /* ATTSV */                  /* or the POSIX way */
  11904.     x = ioctl(0,TCSETAW,&cccbrk);    /* the Sys V way */
  11905. #endif /* BSD44ORPOSIX */
  11906. #endif /* SVORPOSIX */
  11907.  
  11908. #ifdef COHERENT
  11909. #undef SVORPOSIX
  11910. #endif /* COHERENT */
  11911.     debug(F101,"concb x","",x);
  11912.     debug(F101,"concb errno","",errno);
  11913. #ifdef NONOSETBUF
  11914.     if (x > -1) {
  11915.     setbuf(stdout,NULL);    /* Make console unbuffered. */
  11916.     debug(F100,"concb setbuf A","",0);
  11917.     }
  11918. #else
  11919. #ifndef aegis
  11920. #ifndef NOSETBUF
  11921.     if (x > -1) {
  11922.     setbuf(stdout,NULL);    /* Make console unbuffered. */
  11923.     debug(F100,"concb setbuf B","",0);
  11924.     }
  11925. #endif /* NOSETBUF */
  11926. #endif /* aegis */
  11927. #endif /* NONOSETBUF */
  11928.  
  11929. #ifdef  V7
  11930. #ifndef MINIX
  11931.     if (kmem[CON] < 0) {
  11932.         qaddr[CON] = initrawq(0);
  11933.         if((kmem[CON] = open("/dev/kmem", 0)) < 0) {
  11934.             fprintf(stderr, "Can't read /dev/kmem in concb.\n");
  11935.             perror("/dev/kmem");
  11936.             exit(1);
  11937.         }
  11938.     }
  11939. #endif /* MINIX */
  11940. #endif /* V7 */
  11941. #endif /* Plan9 */
  11942.  
  11943.     if (x > -1)
  11944.       constate = CON_CB;
  11945.  
  11946.     debug(F101,"concb returns","",x);
  11947.     return(x);
  11948. }
  11949.  
  11950. /*  C O N B I N  --  Put console in binary mode  */
  11951.  
  11952. /*  Returns 0 if ok, -1 if not  */
  11953.  
  11954. int
  11955. #ifdef CK_ANSIC
  11956. conbin(char esc)
  11957. #else
  11958. conbin(esc) char esc;
  11959. #endif /* CK_ANSIC */
  11960. /* conbin */  {
  11961.  
  11962.     int x;
  11963.  
  11964.     debug(F101,"conbin constate","",constate);
  11965.  
  11966.     if (constate == CON_BIN)
  11967.       return(0);
  11968.  
  11969.     if (!isatty(0)) return(0);          /* only for real ttys */
  11970.     congm();                /* Get modes if necessary. */
  11971.     debug(F100,"conbin","",0);
  11972.     escchr = esc;                       /* Make this available to other fns */
  11973.     ckxech = 1;                         /* Program can echo characters */
  11974. #ifdef aegis
  11975.     conbufn = 0;
  11976.     if (concrp) return(write(1, "\035\002", 2));
  11977.     if (conuid == input_pad_$uid) {
  11978.     pad_$raw(ios_$stdin, st);
  11979.     return(0);
  11980.       }
  11981. #endif /* aegis */
  11982.  
  11983. #ifdef COHERENT
  11984. #define SVORPOSIX
  11985. #endif /* COHERENT */
  11986.  
  11987. #ifdef Plan9
  11988.     return p9conbin();
  11989. #else
  11990. #ifdef SVORPOSIX
  11991. #ifndef OXOS
  11992. #ifdef QNX
  11993.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO);
  11994. #else
  11995. #ifdef COHERENT
  11996.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO);
  11997. #else
  11998.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
  11999. #endif /* COHERENT */
  12000. #endif /* QNX */
  12001. #else /* OXOS */
  12002.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO);
  12003.     ccraw.c_cc[VDISCARD] = ccraw.c_cc[VLNEXT] = CDISABLE;
  12004. #endif /* OXOS */
  12005.     ccraw.c_iflag |= IGNPAR;
  12006. /*
  12007.   Note that for terminal sessions we disable Xon/Xoff flow control to allow
  12008.   the passage ^Q and ^S as data characters for EMACS, and to allow XMODEM
  12009.   transfers to work when C-Kermit is in the middle, etc.  Hardware flow
  12010.   control, if in use, is not affected.
  12011. */
  12012. #ifdef ATTSV
  12013. #ifdef BSD44
  12014.     ccraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IXON|IXANY|IXOFF
  12015.                         |INPCK|ISTRIP);
  12016. #else
  12017.     ccraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IUCLC|IXON|IXANY|IXOFF
  12018.                         |INPCK|ISTRIP);
  12019. #endif /* BSD44 */
  12020. #else /* POSIX */
  12021.     ccraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IXON|IXOFF|INPCK|ISTRIP);
  12022. #endif /* ATTSV */
  12023.     ccraw.c_oflag &= ~OPOST;
  12024. #ifdef COMMENT
  12025. /*
  12026.   WHAT THE HECK WAS THIS FOR?
  12027.   The B9600 setting (obviously) prevents CONNECT from working at any
  12028.   speed other than 9600 when you are logged in to the 7300 on a serial
  12029.   line.  Maybe some of the other flags are necessary -- if so, put back
  12030.   the ones that are needed.  This code is supposed to work the same, no
  12031.   matter whether you are logged in to the 7300 on the real console device,
  12032.   or through a serial port.
  12033. */
  12034. #ifdef ATT7300
  12035.     ccraw.c_cflag = CLOCAL | B9600 | CS8 | CREAD | HUPCL;
  12036. #endif /* ATT7300 */
  12037. #endif /* COMMENT */
  12038.  
  12039. /*** Kermit used to put the console in 8-bit raw mode, but some users have
  12040.  *** pointed out that this should not be done, since some sites actually
  12041.  *** use terminals with parity settings on their Unix systems, and if we
  12042.  *** override the current settings and stop doing parity, then their terminals
  12043.  *** will display blotches for characters whose parity is wrong.  Therefore,
  12044.  *** the following two lines are commented out (Larry Afrin, Clemson U):
  12045.  ***
  12046.  ***   ccraw.c_cflag &= ~(PARENB|CSIZE);
  12047.  ***   ccraw.c_cflag |= (CS8|CREAD);
  12048.  ***
  12049.  *** Sys III/V sites that have trouble with this can restore these lines.
  12050.  ***/
  12051. #ifndef VINTR
  12052.     ccraw.c_cc[0] = 003;        /* Interrupt char is Ctrl-C */
  12053. #else
  12054.     ccraw.c_cc[VINTR] = 003;
  12055. #endif /* VINTR */
  12056. #ifndef VQUIT
  12057.     ccraw.c_cc[1] = escchr;        /* Escape during packet mode */
  12058. #else
  12059.     ccraw.c_cc[VQUIT] = escchr;
  12060. #endif /* VQUIT */
  12061. #ifndef VEOF
  12062.     ccraw.c_cc[4] = 1;
  12063. #else
  12064. #ifndef OXOS
  12065. #ifdef VMIN
  12066.     ccraw.c_cc[VMIN] = 1;
  12067. #endif /* VMIN */
  12068. #else /* OXOS */
  12069.     ccraw.c_min = 1;
  12070. #endif /* OXOS */
  12071. #endif /* VEOF */
  12072.  
  12073. #ifdef ZILOG
  12074.     ccraw.c_cc[5] = 0;
  12075. #else
  12076. #ifndef VEOL
  12077.     ccraw.c_cc[5] = 1;
  12078. #else
  12079. #ifndef OXOS
  12080. #ifdef VTIME
  12081.     ccraw.c_cc[VTIME] = 1;
  12082. #endif /* VTIME */
  12083. #else /* OXOS */
  12084.     ccraw.c_time = 1;
  12085. #endif /* OXOS */
  12086. #endif /* VEOL */
  12087. #endif /* ZILOG */
  12088.  
  12089. #ifdef BSD44ORPOSIX
  12090.     x = tcsetattr(0,TCSADRAIN,&ccraw);    /* Set new modes. */
  12091. #else
  12092.     x = ioctl(0,TCSETAW,&ccraw);
  12093. #endif /* BSD44ORPOSIX */
  12094. #else /* Berkeley, etc. */
  12095.     x = conchk();            /* Because stty() is destructive */
  12096.     if (x > 0)
  12097.       congetbuf(x);
  12098.     ccraw.sg_flags |= (RAW|TANDEM);     /* Set rawmode, XON/XOFF (ha) */
  12099.     ccraw.sg_flags &= ~(ECHO|CRMOD);    /* Set char wakeup, no echo */
  12100. #ifdef BELLV10
  12101.     x = ioctl(0,TIOCSETP,&ccraw);
  12102. #else
  12103.     x = stty(0,&ccraw);
  12104. #endif /* BELLV10 */
  12105. #endif /* SVORPOSIX */
  12106. #endif /* Plan9 */
  12107.  
  12108.     if (x > -1)
  12109.       constate = CON_BIN;
  12110.  
  12111.     debug(F101,"conbin returns","",x);
  12112.     return(x);
  12113.  
  12114. #ifdef COHERENT
  12115. #undef SVORPOSIX
  12116. #endif /* COHERENT */
  12117.  
  12118. }
  12119.  
  12120.  
  12121. /*  C O N R E S  --  Restore the console terminal  */
  12122.  
  12123. int
  12124. conres() {
  12125.     int x;
  12126.     debug(F101,"conres cgmf","",cgmf);
  12127.     debug(F101,"conres constate","",constate);
  12128.  
  12129.     if (cgmf < 1)            /* Do nothing if modes unchanged */
  12130.       return(0);
  12131.     if (constate == CON_RES)
  12132.       return(0);
  12133.  
  12134.     if (!isatty(0)) return(0);          /* only for real ttys */
  12135.     debug(F100,"conres isatty ok","",0);
  12136.     ckxech = 0;                         /* System should echo chars */
  12137.  
  12138. #ifdef aegis
  12139.     conbufn = 0;
  12140.     if (concrp) return(write(1, "\035\001", 2));
  12141.     if (conuid == input_pad_$uid) {
  12142.     pad_$cooked(ios_$stdin, st);
  12143.     constate = CON_RES;
  12144.     return(0);
  12145.     }
  12146. #endif /* aegis */
  12147.  
  12148. #ifdef Plan9
  12149.     p9conres();
  12150. #else
  12151. #ifdef BSD44ORPOSIX
  12152.     debug(F100,"conres restoring tcsetattr","",0);
  12153.     x = tcsetattr(0,TCSADRAIN,&ccold);
  12154. #else
  12155. #ifdef ATTSV
  12156.     debug(F100,"conres restoring ioctl","",0);
  12157.     x = ioctl(0,TCSETAW,&ccold);
  12158. #else /* BSD, V7, and friends */
  12159. #ifdef sony_news            /* Sony NEWS */
  12160.     if (km_con != -1)
  12161.       ioctl(0,TIOCKSET,&km_con);    /* Restore console Kanji mode */
  12162. #endif /* sony_news */
  12163.     msleep(100);
  12164.     debug(F100,"conres restoring stty","",0);
  12165.     x = conchk();            /* Because stty() is destructive */
  12166.     if (x > 0)
  12167.       congetbuf(x);
  12168. #ifdef BELLV10
  12169.     x = ioctl(0,TIOCSETP,&ccold);
  12170. #else
  12171.     x = stty(0,&ccold);
  12172. #endif /* BELLV10 */
  12173. #endif /* ATTSV */
  12174. #endif /* BSD44ORPOSIX */
  12175. #endif /* Plan9 */
  12176.     if (x > -1)
  12177.       constate = CON_RES;
  12178.  
  12179.     debug(F101,"conres returns","",x);
  12180.     return(x);
  12181. }
  12182.  
  12183. /*  C O N O C  --  Output a character to the console terminal  */
  12184.  
  12185. int
  12186. #ifdef CK_ANSIC
  12187. conoc(char c)
  12188. #else
  12189. conoc(c) char c;
  12190. #endif /* CK_ANSIC */
  12191. /* conoc */ {
  12192.  
  12193. #ifdef IKSD
  12194.     if (inserver && !local)
  12195.       return(ttoc(c));
  12196.  
  12197. #ifdef CK_ENCRYPTION
  12198.     if (inserver && TELOPT_ME(TELOPT_ENCRYPTION))
  12199.         ck_tn_encrypt(&c,1);
  12200. #endif /* CK_ENCRYPTION */
  12201. #endif /* IKSD */
  12202.  
  12203. #ifdef Plan9
  12204.     return conwrite(&c,1);
  12205. #else
  12206.     return(write(1,&c,1));
  12207. #endif /* Plan9 */
  12208. }
  12209.  
  12210. /*  C O N X O  --  Write x characters to the console terminal  */
  12211.  
  12212. int
  12213. conxo(x,s) int x; char *s; {
  12214.  
  12215. #ifdef IKSD
  12216.     if (inserver && !local)
  12217.       return(ttol((CHAR *)s,x));
  12218.  
  12219. #ifdef CK_ENCRYPTION
  12220.     if (inserver && TELOPT_ME(TELOPT_ENCRYPTION))
  12221.         ck_tn_encrypt(s,x);
  12222. #endif /* CK_ENCRYPTION */
  12223. #endif /* IKSD */
  12224.  
  12225. #ifdef Plan9
  12226.     return(conwrite(s,x));
  12227. #else
  12228.     return(write(1,s,x));
  12229. #endif /* Plan9 */
  12230. }
  12231.  
  12232. /*  C O N O L  --  Write a line to the console terminal  */
  12233.  
  12234. int
  12235. conol(s) char *s; {
  12236.     int len;
  12237.     if (!s) s = "";            /* Always do this! */
  12238.     len = strlen(s);
  12239.  
  12240. #ifdef IKSD
  12241.     if (inserver && !local)
  12242.       return(ttol((CHAR *)s,len));
  12243.  
  12244. #ifdef CK_ENCRYPTION
  12245.     if (inserver && TELOPT_ME(TELOPT_ENCRYPTION)) {
  12246.     if (nxpacket < len) {
  12247.         if (xpacket) {
  12248.         free(xpacket);
  12249.         xpacket = NULL;
  12250.         nxpacket = 0;
  12251.         }
  12252.         len = len > 10240 ? len : 10240;
  12253.         xpacket = (char *)malloc(len);
  12254.         if (!xpacket) {
  12255.         fprintf(stderr,"ttol malloc failure\n");
  12256.         return(-1);
  12257.         } else
  12258.           nxpacket = len;
  12259.     }
  12260.     memcpy(xpacket,s,len);
  12261.     s = xpacket;
  12262.     ck_tn_encrypt(s,len);
  12263.     }
  12264. #endif /* CK_ENCRYPTION */
  12265. #endif /* IKSD */
  12266.  
  12267. #ifdef Plan9
  12268.     return(conwrite(s,len));
  12269. #else
  12270.     return(write(1,s,len));
  12271. #endif /* Plan9 */
  12272. }
  12273.  
  12274. /*  C O N O L A  --  Write an array of lines to the console terminal */
  12275.  
  12276. int
  12277. conola(s) char *s[]; {
  12278.     char * p;
  12279.     int i, x;
  12280.  
  12281.  
  12282.     if (!s) return(0);
  12283.     for (i = 0; ; i++) {
  12284.     p = s[i];
  12285.     if (!p) p = "";            /* Let's not dump core shall we? */
  12286.     if (!*p)
  12287.       break;
  12288. #ifdef IKSD
  12289.     if (inserver && !local)
  12290.       x = ttol((CHAR *)p,(int)strlen(p));
  12291.     else
  12292. #endif /* IKSD */
  12293.       x = conol(p);
  12294.     if (x < 0)
  12295.       return(-1);
  12296.     }
  12297.     return(0);
  12298. }
  12299.  
  12300. /*  C O N O L L  --  Output a string followed by CRLF  */
  12301.  
  12302. int
  12303. conoll(s) char *s; {
  12304.     CHAR buf[3];
  12305.     buf[0] = '\r';
  12306.     buf[1] = '\n';
  12307.     buf[2] = '\0';
  12308.     if (!s) s = "";
  12309.  
  12310. #ifdef IKSD
  12311.     if (inserver && !local) {
  12312.     ttol((CHAR *)s,(int)strlen(s));
  12313.     return(ttol(buf,2));
  12314.     }
  12315. #endif /* IKSD */
  12316.  
  12317.     conol(s);
  12318. #ifdef IKSD
  12319. #ifdef CK_ENCRYPTION
  12320.     if (inserver && TELOPT_ME(TELOPT_ENCRYPTION))
  12321.       ck_tn_encrypt(buf,2);
  12322. #endif /* CK_ENCRYPTION */
  12323. #endif /* IKSD */
  12324.  
  12325. #ifdef Plan9
  12326.     return(conwrite(buf, 2));
  12327. #else
  12328.     return(write(1,buf,2));
  12329. #endif /* Plan9 */
  12330. }
  12331.  
  12332. /*  C O N C H K  --  Return how many characters available at console  */
  12333. /*
  12334.   We could also use select() here to cover a few more systems that are not
  12335.   covered by any of the following, e.g. HP-UX 9.0x on the model 800.
  12336. */
  12337. int
  12338. conchk() {
  12339.     static int contyp = 0;        /* +1 for isatty, -1 otherwise */
  12340.  
  12341.     if (contyp == 0)            /* This prevents unnecessary */
  12342.       contyp = (isatty(0) ? 1 : -1);    /* duplicated calls to isatty() */
  12343.     debug(F101,"conchk contyp","",contyp);
  12344.     if (backgrd || (contyp < 0))
  12345.       return(0);
  12346.  
  12347. #ifdef aegis
  12348.     if (conbufn > 0) return(conbufn);   /* use old count if nonzero */
  12349.  
  12350.     /* read in more characters */
  12351.     conbufn = ios_$get(ios_$stdin,
  12352.               ios_$cond_opt, conbuf, (long)sizeof(conbuf), st);
  12353.     if (st.all != status_$ok) conbufn = 0;
  12354.     conbufp = conbuf;
  12355.     return(conbufn);
  12356. #else
  12357. #ifdef IKSD
  12358.     if (inserver && !local)
  12359.       return(in_chk(1,ttyfd));
  12360.     else
  12361. #endif /* IKSD */
  12362.       return(in_chk(0,0));
  12363. #endif /* aegis */
  12364. }
  12365.  
  12366. /*  C O N I N C  --  Get a character from the console  */
  12367. /*
  12368.   Call with timo > 0 to do a timed read, timo == 0 to do an untimed blocking
  12369.   read.  Upon success, returns the character.  Upon failure, returns -1.
  12370.   A timed read that does not complete within the timeout period returns -2.
  12371. */
  12372. int
  12373. coninc(timo) int timo; {
  12374.     int n = 0; CHAR ch;
  12375.     int xx;
  12376.  
  12377.     if (conbufn > 0) {            /* If something already buffered */
  12378.     --conbufn;
  12379.     return((unsigned)(*conbufp++ & 0xff));
  12380.     }
  12381.  
  12382.     errno = 0;                /* Clear this */
  12383. #ifdef IKSD
  12384.     if (inserver && !local) {
  12385.     xx = ttinc(timo);
  12386.     if (xx < 0)
  12387.       return(ttinctimo ? -2 : -1);
  12388.     else
  12389.       return(xx);
  12390.     }
  12391. #endif /* IKSD */
  12392.  
  12393. #ifdef aegis                /* Apollo Aegis only... */
  12394.     debug(F101,"coninc timo","",timo);
  12395.     fflush(stdout);
  12396.     if (conchk() > 0) {
  12397.     --conbufn;
  12398.     return((unsigned)(*conbufp++ & 0xff));
  12399.     }
  12400. #endif /* aegis */
  12401.  
  12402. #ifdef TTLEBUF
  12403.     if (
  12404. #ifdef IKSD
  12405.     inserver &&
  12406. #endif /* IKSD */
  12407.     !xlocal
  12408.     ) {
  12409.     if (ttpush >= 0) {
  12410.         debug(F111,"ttinc","ttpush",ttpush);
  12411.         ch = ttpush;
  12412.         ttpush = -1;
  12413.         return(ch);
  12414.     }
  12415.     if (le_data) {
  12416.         if (le_getchar(&ch) > 0) {
  12417.         debug(F111,"ttinc LocalEchoInBuf","ch",ch);
  12418.         return(ch);
  12419.         }
  12420.     }
  12421.     }
  12422. #endif /* TTLEBUF */
  12423.  
  12424.     if (timo <= 0) {            /* Untimed, blocking read. */
  12425.     while (1) {            /* Keep trying till we get one. */
  12426.         n = read(0, &ch, 1);    /* Read a character. */
  12427.         if (n == 0) continue;    /* Shouldn't happen. */
  12428.         if (n > 0) {        /* If read was successful, */
  12429. #ifdef IKSD
  12430. #ifdef CK_ENCRYPTION
  12431.                 debug(F100,"coninc decrypt 1","",0);
  12432.                 if (inserver && !local && TELOPT_U(TELOPT_ENCRYPTION))
  12433.           ck_tn_decrypt(&ch,1);
  12434. #endif /* CK_ENCRYPTION */
  12435. #endif /* IKSD */
  12436.         return((unsigned)(ch & 0xff)); /* return the character. */
  12437.             }
  12438.  
  12439. /* Come here if read() returned an error. */
  12440.  
  12441.         debug(F101, "coninc(0) errno","",errno); /* Log the error. */
  12442. #ifndef OXOS
  12443. #ifdef SVORPOSIX
  12444. #ifdef CIE                             /* CIE Regulus has no EINTR symbol? */
  12445. #ifndef EINTR
  12446. #define EINTR 4
  12447. #endif /* EINTR */
  12448. #endif /* CIE */
  12449. /*
  12450.   This routine is used for several different purposes.  In CONNECT mode, it is
  12451.   used to do an untimed, blocking read from the keyboard in the lower CONNECT
  12452.   fork.  During local-mode file transfer, it reads a character from the
  12453.   console to interrupt the file transfer (like A for a status report, X to
  12454.   cancel a file, etc).  Obviously, we don't want the reads in the latter case
  12455.   to be blocking, or the file transfer would stop until the user typed
  12456.   something.  Unfortunately, System V does not allow the console device input
  12457.   buffer to be sampled nondestructively (e.g. by conchk()), so a kludge is
  12458.   used instead.  During local-mode file transfer, the SIGQUIT signal is armed
  12459.   and trapped by esctrp(), and this routine pretends to have read the quit
  12460.   character from the keyboard normally.  But, kludge or no kludge, the read()
  12461.   issued by this command, under System V only, can fail if a signal -- ANY
  12462.   signal -- is caught while the read is pending.  This can occur not only when
  12463.   the user types the quit character, but also during telnet negotiations, when
  12464.   the lower CONNECT fork signals the upper one about an echoing mode change.
  12465.   When this happens, we have to post the read() again.  This is apparently not
  12466.   a problem in BSD-based UNIX versions.
  12467. */
  12468.         if (errno == EINTR)        /* Read interrupted. */
  12469.           if (conesc)  {        /* If by SIGQUIT, */
  12470.           conesc = 0;        /* the conesc variable is set, */
  12471.           return(escchr);    /* so return the escape character. */
  12472.          } else continue;        /* By other signal, try again. */
  12473. #else
  12474. /*
  12475.   This might be dangerous, but let's do this on non-System V versions too,
  12476.   since at least one SunOS 4.1.2 user complains of immediate disconnections
  12477.   upon first making a TELNET connection.
  12478. */
  12479.         if (errno == EINTR)        /* Read interrupted. */
  12480.           continue;
  12481. #endif /* SVORPOSIX */
  12482. #else /* OXOS */
  12483.         if (errno == EINTR)        /* Read interrupted. */
  12484.           continue;
  12485. #endif /* OXOS */
  12486.         return(-1);            /* Error */
  12487.     }
  12488.     }
  12489. #ifdef DEBUG
  12490.     if (deblog && timo <= 0) {
  12491.     debug(F100,"coninc timeout logic error","",0);
  12492.     timo = 1;
  12493.     }
  12494. #endif /* DEBUG */
  12495.  
  12496. /* Timed read... */
  12497.  
  12498.     saval = signal(SIGALRM,timerh);    /* Set up timeout handler. */
  12499.     xx = alarm(timo);            /* Set the alarm. */
  12500.     debug(F101,"coninc alarm set","",timo);
  12501.     if (
  12502. #ifdef CK_POSIX_SIG
  12503.     sigsetjmp(sjbuf,1)
  12504. #else
  12505.     setjmp(sjbuf)
  12506. #endif /* CK_POSIX_SIG */
  12507.     )                /* The read() timed out. */
  12508.       n = -2;                /* Code for timeout. */
  12509.     else
  12510.       n = read(0, &ch, 1);
  12511.     ttimoff();                /* Turn off timer */
  12512.     if (n > 0) {            /* Got character OK. */
  12513. #ifdef IKSD
  12514. #ifdef CK_ENCRYPTION
  12515.         debug(F100,"coninc decrypt 2","",0);
  12516.         if (inserver && !local && TELOPT_U(TELOPT_ENCRYPTION))
  12517.       ck_tn_decrypt(&ch,1);
  12518. #endif /* CK_ENCRYPTION */
  12519. #endif /* IKSD */
  12520.     return((unsigned)(ch & 0xff));    /* Return it. */
  12521.     }
  12522. /*
  12523.   read() returned an error.  Same deal as above, but without the loop.
  12524. */
  12525.     debug(F101, "coninc(timo) n","",n);
  12526.     debug(F101, "coninc(timo) errno","",errno);
  12527. #ifndef OXOS
  12528. #ifdef SVORPOSIX
  12529.     if (n == -1 && errno == EINTR && conesc != 0) {
  12530.     conesc = 0;
  12531.     return(escchr);            /* User entered escape character. */
  12532.     }
  12533. #endif /* SVORPOSIX */
  12534.     if (n == 0 && errno > 0) {        /* It's an error */
  12535.     return(-1);
  12536.     }
  12537. #endif /* ! OXOS */
  12538.     return(n);
  12539. }
  12540.  
  12541. /*  C O N G K S  --  Console Get Keyboard Scancode  */
  12542.  
  12543. #ifndef congks
  12544. /*
  12545.   This function needs to be filled in with the various system-dependent
  12546.   system calls used by SUNOS, NeXT OS, Xenix, Aviion, etc, to read a full
  12547.   keyboard scan code.  Unfortunately there aren't any.
  12548. */
  12549. int
  12550. congks(timo) int timo; {
  12551.  
  12552. #ifdef IKSD
  12553.     if (inserver && !local)
  12554.       return(ttinc(timo));
  12555. #endif /* IKSD */
  12556.  
  12557.     return(coninc(timo));
  12558. }
  12559. #endif /* congks */
  12560.  
  12561. #ifdef ATT7300
  12562.  
  12563. /*  A T T D I A L  --  Dial up the remote system using internal modem
  12564.  * Purpose: to open and dial a number on the internal modem available on the
  12565.  * ATT7300 UNIX PC.  Written by Joe Doupnik. Superceeds version written by
  12566.  * Richard E. Hill, Dickinson, TX. which employed dial(3c).
  12567.  * Uses information in <sys/phone.h> and our status int attmodem.
  12568.  */
  12569. attdial(ttname,speed,telnbr) char *ttname,*telnbr; long speed; {
  12570.     char *telnum;
  12571.  
  12572.     attmodem &= ~ISMODEM;                       /* modem not in use yet */
  12573.                     /* Ensure O_NDELAY is set, else i/o traffic hangs */
  12574.                     /* We turn this flag off once the dial is complete */
  12575.     fcntl(ttyfd, F_SETFL, fcntl(ttyfd, F_GETFL, 0) | O_NDELAY);
  12576.  
  12577.     /* Condition line, check availability & DATA mode, turn on speaker */
  12578.     if (ioctl(ttyfd,PIOCOFFHOOK, &dialer) == -1) {
  12579.         printf("cannot access phone\n");
  12580.         ttclos(0);
  12581.         return (-2);
  12582.     }
  12583.     ioctl(ttyfd,PIOCGETP,&dialer);      /* get phone dialer parameters */
  12584.  
  12585.     if (dialer.c_lineparam & VOICE) {    /* phone must be in DATA mode */
  12586.         printf(" Should not dial with modem in VOICE mode.\n");
  12587.         printf(" Exit Kermit, switch to DATA and retry call.\n");
  12588.         ttclos(0);
  12589.         return (-2);
  12590.     }
  12591. #ifdef ATTTONED                /* Old way, tone dialing only. */
  12592.     dialer.c_lineparam = DATA | DTMF;    /* Dial with tones, */
  12593.     dialer.c_lineparam &= ~PULSE;    /* not with pulses. */
  12594. #else
  12595.     /* Leave current pulse/tone state alone. */
  12596.     /* But what about DATA?  Add it back if you have trouble. */
  12597.     /* sys/phone says you get DATA automatically by opening device RDWR */
  12598. #endif
  12599.     dialer.c_waitdialtone = 5;                  /* wait 5 sec for dialtone */
  12600. #ifdef COMMENT
  12601.     dialer.c_feedback = SPEAKERON|NORMSPK|RINGON;  /* control speaker */
  12602. #else
  12603.     /* sys/phone says RINGON used only for incoming voice calls */
  12604.     dialer.c_feedback &= ~(SOFTSPK|LOUDSPK);
  12605.     dialer.c_feedback |= SPEAKERON|NORMSPK;
  12606. #endif
  12607.     dialer.c_waitflash = 500;                   /* 0.5 sec flash hook */
  12608.     if(ioctl(ttyfd,PIOCSETP,&dialer) == -1) {   /* set phone parameters */
  12609.         printf("Cannot set modem characteristics\n");
  12610.         ttclos(0);
  12611.         return (-2);
  12612.     }
  12613.     ioctl(ttyfd,PIOCRECONN,0);        /* Turns on speaker for pulse */
  12614.  
  12615. #ifdef COMMENT
  12616.     fprintf(stderr,"Phone line status. line_par:%o dialtone_wait:%o \
  12617. line_status:%o feedback:%o\n",
  12618.     dialer.c_lineparam, dialer.c_waitdialtone,
  12619.     dialer.c_linestatus, dialer.c_feedback);
  12620. #endif
  12621.  
  12622.     attmodem |= ISMODEM;                        /* modem is now in-use */
  12623.     sleep(1);
  12624.     for (telnum = telnbr; *telnum != '\0'; telnum++)    /* dial number */
  12625. #ifdef ATTTONED
  12626.       /* Tone dialing only */
  12627.       if (ioctl(ttyfd,PIOCDIAL,telnum) != 0) {
  12628.       perror("Error in dialing");
  12629.       ttclos(0);
  12630.       return(-2);
  12631.       }
  12632. #else /* Allow Pulse or Tone dialing */
  12633.     switch (*telnum) {
  12634.       case 't': case 'T': case '%':    /* Tone dialing requested */
  12635.     dialer.c_lineparam |= DTMF;
  12636.     dialer.c_lineparam &= ~PULSE;
  12637.     if (ioctl(ttyfd,PIOCSETP,&dialer) == -1) {
  12638.         printf("Cannot set modem to tone dialing\n");
  12639.         ttclos(0);
  12640.         return(-2);
  12641.     }
  12642.     break;
  12643.       case 'd': case 'D': case 'p': case 'P': case '^':
  12644.     dialer.c_lineparam |= PULSE;
  12645.     dialer.c_lineparam &= ~DTMF;
  12646.     if (ioctl(ttyfd,PIOCSETP,&dialer) == -1) {
  12647.         printf("Cannot set modem to pulse dialing\n");
  12648.         ttclos(0);
  12649.         return(-2);
  12650.     }
  12651.     break;
  12652.       default:
  12653.         if (ioctl(ttyfd,PIOCDIAL,telnum) != 0) {
  12654.         perror("Dialing error");
  12655.         ttclos(0);
  12656.         return(-2);
  12657.     }
  12658.     break;
  12659.     }
  12660. #endif
  12661.  
  12662.     ioctl(ttyfd,PIOCDIAL,"@");        /* terminator for data call */
  12663.     do {                /* wait for modems to Connect */
  12664.         if (ioctl(ttyfd,PIOCGETP,&dialer) != 0)    { /* get params */
  12665.         perror("Cannot get modems to connect");
  12666.         ttclos(0);
  12667.         return(-2);
  12668.     }
  12669.     } while ((dialer.c_linestatus & MODEMCONNECTED) == 0);
  12670.     /* Turn off O_NDELAY flag now. */
  12671.     fcntl(ttyfd, F_SETFL, fcntl(ttyfd, F_GETFL, 0) & ~O_NDELAY);
  12672.     signal(SIGHUP, sighup);             /* hangup on loss of carrier */
  12673.     return(0);                          /* return success */
  12674. }
  12675.  
  12676. /*
  12677.   Offgetty, ongetty functions. These function get the 'getty(1m)' off
  12678.   and restore it to the indicated line.  Shell's return codes are:
  12679.     0: Can't do it.  Probably a user logged on.
  12680.     1: No need.  No getty on that line.
  12681.     2: Done, you should restore the getty when you're done.
  12682.   DOGETY System(3), however, returns them as 0, 256, 512, respectively.
  12683.   Thanks to Kevin O'Gorman, Anarm Software Systems.
  12684.  
  12685.    getoff.sh looks like:   geton.sh looks like:
  12686.      setgetty $1 0           setgetty $1 1
  12687.      err=$?                  exit $?
  12688.      sleep 2
  12689.      exit $err
  12690. */
  12691.  
  12692. /*  O F F G E T T Y  --  Turn off getty(1m) for the communications tty line
  12693.  * and get status so it can be restarted after the line is hung up.
  12694.  */
  12695. int
  12696. offgetty(ttname) char *ttname; {
  12697.     char temp[30];
  12698.     while (*ttname != '\0') ttname++;       /* seek terminator of path */
  12699.     ttname -= 3;                            /* get last 3 chars of name */
  12700.     sprintf(temp,"/usr/bin/getoff.sh %s",ttname);
  12701.     return(zsyscmd(temp));
  12702. }
  12703.  
  12704. /*  O N G E T T Y  --  Turn on getty(1m) for the communications tty line */
  12705.  
  12706. int
  12707. ongetty(ttname) char *ttname; {
  12708.     char temp[30];
  12709.     while (*ttname != '\0') ttname++;       /* comms tty path name */
  12710.     ttname -= 3;
  12711.     sprintf(temp,"/usr/bin/geton.sh %s",ttname);
  12712.     return(zsyscmd(temp));
  12713. }
  12714. #endif /* ATT7300 */
  12715.  
  12716. /*  T T S C A R R  --  Set ttcarr variable, controlling carrier handling.
  12717.  *
  12718.  *  0 = Off: Always ignore carrier. E.g. you can connect without carrier.
  12719.  *  1 = On: Heed carrier, except during dialing. Carrier loss gives disconnect.
  12720.  *  2 = Auto: For "modem direct": The same as "Off".
  12721.  *            For real modem types: Heed carrier during connect, but ignore
  12722.  *                it anytime else.  Compatible with pre-5A C-Kermit versions.
  12723.  *
  12724.  * As you can see, this setting does not affect dialing, which always ignores
  12725.  * carrier (unless there is some special exception for some modem type).  It
  12726.  * does affect ttopen() if it is set before ttopen() is used.  This setting
  12727.  * takes effect on the next call to ttopen()/ttpkt()/ttvt().  And they are
  12728.  * (or should be) always called before any communications is tried, which
  12729.  * means that, practically speaking, the effect is immediate.
  12730.  *
  12731.  * Of course, nothing of this applies to remote mode (xlocal = 0).
  12732.  *
  12733.  * Someone has yet to uncover how to manipulate the carrier in the BSD
  12734.  * environment (or any non-termio using environment).  Until that time, this
  12735.  * will simply be a no-op for BSD.
  12736.  *
  12737.  * Note that in previous versions, the carrier was most often left unchanged
  12738.  * in ttpkt()/ttvt() unless they were called with FLO_DIAL or FLO_DIAX.  This
  12739.  * has changed.  Now it is controlled by ttcarr in conjunction with these
  12740.  * modes.
  12741.  */
  12742. int
  12743. ttscarr(carrier) int carrier; {
  12744.     ttcarr = carrier;
  12745.     debug(F101, "ttscarr","",ttcarr);
  12746.     return(ttcarr);
  12747. }
  12748.  
  12749. /* C A R R C T L  --  Set tty modes for carrier treatment.
  12750.  *
  12751.  * Sets the appropriate bits in a termio or sgttyb struct for carrier control
  12752.  * (actually, there are no bits in sgttyb for that), or performs any other
  12753.  * operations needed to control this on the current system.  The function does
  12754.  * not do the actual TCSETA or stty, since often we want to set other bits too
  12755.  * first.  Don't call this function when xlocal is 0, or the tty is not opened.
  12756.  *
  12757.  * We don't know how to do anything like carrier control on non-ATTSV systems,
  12758.  * except, apparently, ultrix.  See above.  It is also known that this doesn't
  12759.  * have much effect on a Xenix system.  For Xenix, one should switch back and
  12760.  * forth between the upper and lower case device files.  Maybe later.
  12761.  * Presently, Xenix will stick to the mode it was opened with.
  12762.  *
  12763.  * carrier: 0 = ignore carrier, 1 = require carrier.
  12764.  * The current state is saved in curcarr, and checked to save labour.
  12765.  */
  12766. #ifdef SVORPOSIX
  12767. int
  12768. #ifdef BSD44ORPOSIX
  12769. carrctl(ttpar, carrier)    struct termios *ttpar; int carrier;
  12770. #else /* ATTSV */
  12771. carrctl(ttpar, carrier)    struct termio *ttpar; int carrier;
  12772. #endif /* BSD44ORPOSIX */
  12773. /* carrctl */ {
  12774.     debug(F101, "carrctl","",carrier);
  12775.     if (carrier)
  12776.       ttpar->c_cflag &= ~CLOCAL;
  12777.     else
  12778.       ttpar->c_cflag |= CLOCAL;
  12779.     return(0);
  12780. }
  12781. #else /* Berkeley, V7, et al... */
  12782. int
  12783. carrctl(ttpar, carrier) struct sgttyb *ttpar; int carrier; {
  12784.     debug(F101, "carrctl","",carrier);
  12785.     if (carrier == curcarr)
  12786.       return(0);
  12787.     curcarr = carrier;
  12788. #ifdef ultrix
  12789. #ifdef COMMENT
  12790. /*
  12791.   Old code from somebody at DEC that tends to get stuck, time out, etc.
  12792. */
  12793.     if (carrier) {
  12794.     ioctl(ttyfd, TIOCMODEM, &temp);
  12795.     ioctl(ttyfd, TIOCHPCL, 0);
  12796.     } else {
  12797.     /* (According to the manuals, TIOCNCAR should be preferred */
  12798.     /* over TIOCNMODEM...) */
  12799.     ioctl(ttyfd, TIOCNMODEM, &temp);
  12800.     }
  12801. #else
  12802. /*
  12803.   New code from Jamie Watson that, he says, eliminates the problems.
  12804. */
  12805.     if (carrier) {
  12806.     ioctl(ttyfd, TIOCCAR);
  12807.     ioctl(ttyfd, TIOCHPCL);
  12808.     } else {
  12809.     ioctl(ttyfd, TIOCNCAR);
  12810.     }
  12811. #endif /* COMMENT */
  12812. #endif /* ultrix */
  12813.     return(0);
  12814. }
  12815. #endif /* SVORPOSIX */
  12816.  
  12817.  
  12818. /*  T T G M D M  --  Get modem signals  */
  12819. /*
  12820.  Looks for RS-232 modem signals, and returns those that are on in as its
  12821.  return value, in a bit mask composed of the BM_xxx values defined in ckcdeb.h.
  12822.  Returns:
  12823.  -3 Not implemented
  12824.  -2 if the communication device does not have modem control (e.g. telnet)
  12825.  -1 on error.
  12826.  >= 0 on success, with a bit mask containing the modem signals that are on.
  12827. */
  12828.  
  12829. /*
  12830.   Define the symbol K_MDMCTL if we have Sys V R3 / 4.3 BSD style
  12831.   modem control, namely the TIOCMGET ioctl.
  12832. */
  12833.  
  12834. #ifdef BSD43
  12835. #define K_MDMCTL
  12836. #endif /* BSD43 */
  12837.  
  12838. #ifdef SUNOS4
  12839. #define K_MDMCTL
  12840. #endif /* SUNOS4 */
  12841.  
  12842. /*
  12843.   SCO OpenServer R5.0.4.  The TIOCMGET definition is hardwired in because it
  12844.   is skipped in termio.h when _POSIX_SOURCE is defined.  But _POSIX_SOURCE
  12845.   must be defined in order to get the high serial speeds that are new to
  12846.   5.0.4.  However, the regular SCO drivers do not implement TIOCMGET, so the
  12847.   ioctl() returns -1 with errno 22 (invalid function).  But third-party
  12848.   drivers, e.g. for Digiboard, do implement it, and so it should work on ports
  12849.   driven by those drivers.
  12850. */
  12851. #ifdef SCO_OSR504
  12852. #ifndef TIOCMGET
  12853. #define TIOCMGET (('t'<<8)|29)
  12854. #endif /* TIOCMGET */
  12855. #endif /* SCO_OSR504 */
  12856.  
  12857. #ifdef CK_SCOV5
  12858. /* Because POSIX strictness in <sys/termio.h> won't let us see these. */
  12859. #ifndef TIOCM_DTR
  12860. #define TIOCM_DTR    0x0002        /* data terminal ready */
  12861. #define TIOCM_RTS    0x0004        /* request to send */
  12862. #define TIOCM_CTS    0x0020        /* clear to send */
  12863. #define TIOCM_CAR    0x0040        /* carrier detect */
  12864. #define TIOCM_RNG    0x0080        /* ring */
  12865. #define TIOCM_DSR    0x0100        /* data set ready */
  12866. #define TIOCM_CD    TIOCM_CAR
  12867. #define TIOCM_RI    TIOCM_RNG
  12868. #endif /* TIOCM_DTR */
  12869. #endif /* CK_SCOV5 */
  12870.  
  12871. #ifdef QNX
  12872. #define K_MDMCTL
  12873. #else
  12874. #ifdef TIOCMGET
  12875. #define K_MDMCTL
  12876. #endif /* TIOCMGET */
  12877. #endif /* QNX */
  12878. /*
  12879.   "A serial communication program that can't read modem signals
  12880.    is like a car without windows."
  12881. */
  12882. int
  12883. ttgmdm() {
  12884.  
  12885. #ifdef QNX
  12886. #include <sys/qioctl.h>
  12887.  
  12888.     unsigned long y, mdmbits[2];
  12889.     int x, z = 0;
  12890.  
  12891.     if (xlocal && ttyfd < 0)
  12892.       return(-1);
  12893.  
  12894. #ifdef NETCMD
  12895.     if (ttpipe) return(-2);
  12896. #endif /* NETCMD */
  12897. #ifdef NETPTY
  12898.     if (ttpty) return(-2);
  12899. #endif /* NETPTY */
  12900.  
  12901.     mdmbits[0] = 0L;
  12902.     mdmbits[1] = 0L;
  12903. /*
  12904.  * From <sys/qioctl.h>:
  12905.  *
  12906.  * SERIAL devices   (all Dev.ser versions)
  12907.  * 0 : DTR           8 = Data Bits 0  16 - reserved     24 - reserved
  12908.  * 1 : RTS           9 = Data Bits 1  17 - reserved     25 - reserved
  12909.  * 2 = Out 1        10 = Stop Bits    18 - reserved     26 - reserved
  12910.  * 3 = Int Enable   11 = Par Enable   19 - reserved     27 - reserved
  12911.  * 4 = Loop         12 = Par Even     20 = CTS          28 - reserved
  12912.  * 5 - reserved     13 = Par Stick    21 = DSR          29 - reserved
  12913.  * 6 - reserved     14 : Break        22 = RI           30 - reserved
  12914.  * 7 - reserved     15 = 0            23 = CD           31 - reserved
  12915.  */
  12916.     errno = 0;
  12917.     x = qnx_ioctl(ttyfd, QCTL_DEV_CTL, &mdmbits[0], 8, &mdmbits[0], 4);
  12918.     debug(F101,"ttgmdm qnx_ioctl","",x);
  12919.     debug(F101,"ttgmdm qnx_ioctl errno","",errno);
  12920.     if (!x) {
  12921.     debug(F101,"ttgmdm qnx_ioctl mdmbits[0]","",mdmbits[0]);
  12922.     debug(F101,"ttgmdm qnx_ioctl mdmbits[1]","",mdmbits[1]);
  12923.     y = mdmbits[0];
  12924.     if (y & 0x000001L) z |= BM_DTR;    /* Bit  0 */
  12925.     if (y & 0x000002L) z |= BM_RTS;    /* Bit  1 */
  12926.     if (y & 0x100000L) z |= BM_CTS;    /* Bit 20 */
  12927.     if (y & 0x200000L) z |= BM_DSR;    /* Bit 21 */
  12928.     if (y & 0x400000L) z |= BM_RNG;    /* Bit 22 */
  12929.     if (y & 0x800000L) z |= BM_DCD;    /* Bit 23 */
  12930.     debug(F101,"ttgmdm qnx result","",z);
  12931.     debug(F110,"ttgmdm qnx CD = ",(z & BM_DCD) ? "On" : "Off", 0);
  12932.     gotsigs = 1;
  12933.     return(z);
  12934.     } else return(-1);
  12935. #else
  12936. #ifdef HPUX                /* HPUX has its own way */
  12937.     int x, z;
  12938.  
  12939. #ifdef HPUX10                /* Modem flag word */
  12940.     mflag y;                /* mflag typedef'd in <sys/modem.h> */
  12941. #else
  12942. #ifdef HPUX9
  12943.     mflag y;
  12944. #else
  12945. #ifdef HPUX8
  12946.     mflag y;
  12947. #else
  12948.     unsigned long y;            /* Not sure about pre-8.0... */
  12949. #endif /* HPUX8 */
  12950. #endif /* HPUX9 */
  12951. #endif /* HPUX10 */
  12952.  
  12953.     if (xlocal && ttyfd < 0)
  12954.       return(-1);
  12955.  
  12956. #ifdef NETCONN
  12957.     if (netconn) {            /* Network connection */
  12958. #ifdef TN_COMPORT
  12959.         if (istncomport()) {
  12960.         gotsigs = 1;
  12961.         return(tngmdm());
  12962.     } else
  12963. #endif /* TN_COMPORT */
  12964.       return(-2);            /* No modem signals */
  12965.     }
  12966. #endif /* NETCONN */
  12967.  
  12968. #ifdef NETCMD
  12969.     if (ttpipe) return(-2);
  12970. #endif /* NETCMD */
  12971. #ifdef NETPTY
  12972.     if (ttpty) return(-2);
  12973. #endif /* NETPTY */
  12974.  
  12975.     if (xlocal)                /* Get modem signals */
  12976.       x = ioctl(ttyfd,MCGETA,&y);
  12977.     else
  12978.       x = ioctl(0,MCGETA,&y);
  12979.     if (x < 0) return(-1);
  12980.     debug(F101,"ttgmdm","",y);
  12981.  
  12982.     z = 0;                /* Initialize return value */
  12983.  
  12984. /* Now set bits for each modem signal that is reported to be on. */
  12985.  
  12986. #ifdef MCTS
  12987.     /* Clear To Send */
  12988.     debug(F101,"ttgmdm HPUX CTS","",y & MCTS);
  12989.     if (y & MCTS) z |= BM_CTS;
  12990. #endif
  12991. #ifdef MDSR
  12992.     /* Data Set Ready */
  12993.     debug(F101,"ttgmdm HPUX DSR","",y & MDSR);
  12994.     if (y & MDSR) z |= BM_DSR;
  12995. #endif
  12996. #ifdef MDCD
  12997.     /* Carrier */
  12998.     debug(F101,"ttgmdm HPUX DCD","",y & MDCD);
  12999.     if (y & MDCD) z |= BM_DCD;
  13000. #endif
  13001. #ifdef MRI
  13002.     /* Ring Indicate */
  13003.     debug(F101,"ttgmdm HPUX RI","",y & MRI);
  13004.     if (y & MRI) z |= BM_RNG;
  13005. #endif
  13006. #ifdef MDTR
  13007.     /* Data Terminal Ready */
  13008.     debug(F101,"ttgmdm HPUX DTR","",y & MDTR);
  13009.     if (y & MDTR) z |= BM_DTR;
  13010. #endif
  13011. #ifdef MRTS
  13012.     /* Request To Send */
  13013.     debug(F101,"ttgmdm HPUX RTS","",y & MRTS);
  13014.     if (y & MRTS) z |= BM_RTS;
  13015. #endif
  13016.     gotsigs = 1;
  13017.     return(z);
  13018.  
  13019. #else /* ! HPUX */
  13020.  
  13021. #ifdef K_MDMCTL
  13022. /*
  13023.   Note, TIOCMGET might already have been defined in <sys/ioctl.h> or elsewhere.
  13024.   If not, we try including <sys/ttycom.h> -- if this blows up then more ifdefs
  13025.   are needed.
  13026. */
  13027. #ifndef TIOCMGET
  13028. #include <sys/ttycom.h>
  13029. #endif /* TIOCMGET */
  13030.  
  13031.     int x, y, z;
  13032.  
  13033.     debug(F100,"ttgmdm K_MDMCTL defined","",0);
  13034.  
  13035.     if (netconn)            /* Network connection */
  13036.       return(-2);            /* No modem signals */
  13037.  
  13038. #ifdef NETCMD
  13039.     if (ttpipe) return(-2);
  13040. #endif /* NETCMD */
  13041. #ifdef NETPTY
  13042.     if (ttpty) return(-2);
  13043. #endif /* NETPTY */
  13044.  
  13045.     if (xlocal && ttyfd < 0)
  13046.       return(-1);
  13047.  
  13048.     if (xlocal)
  13049.       x = ioctl(ttyfd,TIOCMGET,&y);    /* Get modem signals. */
  13050.     else
  13051.       x = ioctl(0,TIOCMGET,&y);
  13052.     debug(F101,"ttgmdm TIOCMGET ioctl","",x);
  13053.     if (x < 0) {
  13054.     debug(F101,"ttgmdm errno","",errno);
  13055.     return(-1);
  13056.     }
  13057.     debug(F101,"ttgmdm bits","",y);
  13058.  
  13059.     z = 0;                /* Initialize return value. */
  13060. #ifdef TIOCM_CTS
  13061.     /* Clear To Send */
  13062.     if (y & TIOCM_CTS) z |= BM_CTS;
  13063.     debug(F101,"ttgmdm TIOCM_CTS defined","",TIOCM_CTS); 
  13064. #else
  13065.     debug(F100,"ttgmdm TIOCM_CTS not defined","",0);
  13066. #endif
  13067. #ifdef TIOCM_DSR
  13068.     /* Data Set Ready */
  13069.     if (y & TIOCM_DSR) z |= BM_DSR;
  13070.     debug(F101,"ttgmdm TIOCM_DSR defined","",TIOCM_DSR); 
  13071. #else
  13072.     debug(F100,"ttgmdm TIOCM_DSR not defined","",0);
  13073. #endif
  13074. #ifdef TIOCM_CAR
  13075.     /* Carrier */
  13076.     if (y & TIOCM_CAR) z |= BM_DCD;
  13077.     debug(F101,"ttgmdm TIOCM_CAR defined","",TIOCM_CAR); 
  13078. #else
  13079.     debug(F100,"ttgmdm TIOCM_CAR not defined","",0);
  13080. #endif
  13081. #ifdef TIOCM_RNG
  13082.     /* Ring Indicate */
  13083.     if (y & TIOCM_RNG) z |= BM_RNG;
  13084.     debug(F101,"ttgmdm TIOCM_RNG defined","",TIOCM_RNG); 
  13085. #else
  13086.     debug(F100,"ttgmdm TIOCM_RNG not defined","",0);
  13087. #endif
  13088. #ifdef TIOCM_DTR
  13089.     /* Data Terminal Ready */
  13090.     if (y & TIOCM_DTR) z |= BM_DTR;
  13091.     debug(F101,"ttgmdm TIOCM_DTR defined","",TIOCM_DTR); 
  13092. #else
  13093.     debug(F100,"ttgmdm TIOCM_DTR not defined","",0);
  13094. #endif
  13095. #ifdef TIOCM_RTS
  13096.     /* Request To Send */
  13097.     if (y & TIOCM_RTS) z |= BM_RTS;
  13098.     debug(F101,"ttgmdm TIOCM_RTS defined","",TIOCM_RTS); 
  13099. #else
  13100.     debug(F100,"ttgmdm TIOCM_RTS not defined","",0);
  13101. #endif
  13102.     gotsigs = 1;
  13103.     return(z);
  13104.  
  13105. #else /* !K_MDMCTL catch-All */
  13106.  
  13107.     debug(F100,"ttgmdm K_MDMCTL not defined","",0);
  13108. #ifdef TIOCMGET
  13109.     debug(F100,"ttgmdm TIOCMGET defined","",0);
  13110. #else
  13111.     debug(F100,"ttgmdm TIOCMGET not defined","",0);
  13112. #endif /* TIOCMGET */
  13113. #ifdef _SVID3
  13114.     debug(F100,"ttgmdm _SVID3 defined","",0);
  13115. #else
  13116.     debug(F100,"ttgmdm _SVID3 not defined","",0);
  13117. #endif /* _SVID3 */
  13118.  
  13119.     if (netconn)            /* Network connection */
  13120.       return(-2);            /* No modem signals */
  13121.  
  13122. #ifdef NETCMD
  13123.     if (ttpipe) return(-2);
  13124. #endif /* NETCMD */
  13125. #ifdef NETPTY
  13126.     if (ttpty) return(-2);
  13127. #endif /* NETPTY */
  13128.  
  13129.     return(-3);                /* Sorry, I don't know how... */
  13130.  
  13131. #endif /* K_MDMCTL */
  13132. #endif /* HPUX */
  13133. #endif /* QNX */
  13134. }
  13135.  
  13136. /*  P S U S P E N D  --  Put this process in the background.  */
  13137.  
  13138. /*
  13139.   Call with flag nonzero if suspending is allowed, zero if not allowed.
  13140.   Returns 0 on apparent success, -1 on failure (flag was zero, or
  13141.   kill() returned an error code.
  13142. */
  13143. int
  13144. psuspend(flag) int flag; {
  13145.  
  13146. #ifdef RTU
  13147.     extern int rtu_bug;
  13148. #endif /* RTU */
  13149.  
  13150.     if (flag == 0) return(-1);
  13151.  
  13152. #ifdef NOJC
  13153.     return(-1);
  13154. #else
  13155. #ifdef SIGTSTP
  13156. /*
  13157.   The big question here is whether job control is *really* supported.
  13158.   There's no way Kermit can know for sure.  The fact that SIGTSTP is
  13159.   defined does not guarantee the Unix kernel supports it, and the fact
  13160.   that the Unix kernel supports it doesn't guarantee that the user's
  13161.   shell (or other process that invoked Kermit) supports it.
  13162. */
  13163. #ifdef RTU
  13164.     rtu_bug = 1;
  13165. #endif /* RTU */
  13166.     if (kill(0,SIGSTOP) < 0
  13167. #ifdef MIPS
  13168. /* Let's try this for MIPS too. */
  13169.     && kill(getpid(),SIGSTOP) < 0
  13170. #endif /* MIPS */
  13171.     ) {                /* If job control, suspend the job */
  13172.     perror("suspend");
  13173.     debug(F101,"psuspend error","",errno);
  13174.     return(-1);
  13175.     }
  13176.     debug(F100,"psuspend ok","",0);
  13177.     return(0);
  13178. #else
  13179.     return(-1);
  13180. #endif /* SIGTSTP */
  13181. #endif /* NOJC */
  13182. }
  13183.  
  13184. /*
  13185.   setuid package, by Kristoffer Eriksson, with contributions from Dean
  13186.   Long and fdc.
  13187. */
  13188.  
  13189. /* The following is for SCO when CK_ANSILIBS is defined... */
  13190. #ifdef M_UNIX
  13191. #ifdef CK_ANSILIBS
  13192. #ifndef NOGETID_PROTOS
  13193. #define NOGETID_PROTOS
  13194. #endif /* NOGETID_PROTOS */
  13195. #endif /* CK_ANSILIBS */
  13196. #endif /* M_UNIX */
  13197.  
  13198. #ifndef _POSIX_SOURCE
  13199. #ifndef SUNOS4
  13200. #ifndef NEXT
  13201. #ifndef PS2AIX10
  13202. #ifndef sequent
  13203. #ifndef HPUX9
  13204. #ifndef COHERENT
  13205. #ifndef NOGETID_PROTOS
  13206. extern UID_T getuid(), geteuid(), getreuid();
  13207. extern GID_T getgid(), getegid(), getregid();
  13208. #endif /* NOGETID_PROTOS */
  13209. #else
  13210. extern UID_T getreuid();
  13211. extern GID_T getregid();
  13212. #endif /* COHERENT */
  13213. #endif /* HPUX9 */
  13214. #endif /* sequent */
  13215. #endif /* PS2AIX10 */
  13216. #endif /* NEXT */
  13217. #endif /* SUNOS4 */
  13218. #endif /* _POSIX_SOURCE */
  13219.  
  13220. /*
  13221. Subject: Set-user-id
  13222. To: fdc@watsun.cc.columbia.edu (Frank da Cruz)
  13223. Date: Sat, 21 Apr 90 4:48:25 MES
  13224. From: Kristoffer Eriksson <ske@pkmab.se>
  13225.  
  13226. This is a set of functions to be used in programs that may be run set-user-id
  13227. and/or set-group-id. They handle both the case where the program is not run
  13228. with such privileges (nothing special happens then), and the case where one
  13229. or both of these set-id modes are used.  The program is made to run with the
  13230. user's real user and group ids most of the time, except for when more
  13231. privileges are needed.  Don't set-user-id to "root".
  13232.  
  13233. This works on System V and POSIX.  In BSD, it depends on the
  13234. "saved-set-user-id" feature.
  13235. */
  13236.  
  13237. #define UID_ROOT 0            /* Root user and group ids */
  13238. #define GID_ROOT 0
  13239.  
  13240. /*
  13241.   The following code defines the symbol SETEUID for UNIX systems based
  13242.   on BSD4.4 (either -Encumbered or -Lite).  This program will then use
  13243.   seteuid() and setegid() instead of setuid() and setgid(), which still
  13244.   don't allow arbitrary switching.  It also avoids setreuid() and
  13245.   setregid(), which are included in BSD4.4 for compatibility only, are
  13246.   insecure, and print warnings to stderr under at least one system (NetBSD
  13247.   1.0).  Note that POSIX systems should still use setuid() and setgid();
  13248.   the seteuid() and setegid() functions are BSD4.4 extensions to the
  13249.   POSIX model.  Mike Long <mike.long@analog.com>, 8/94.
  13250. */
  13251. #ifdef BSD44
  13252. #define SETEUID
  13253. #endif /* BSD44 */
  13254.  
  13255. /*
  13256.   The following construction automatically defines the symbol SETREUID for
  13257.   UNIX versions based on Berkeley Unix 4.2 and 4.3.  If this symbol is
  13258.   defined, then this program will use getreuid() and getregid() calls in
  13259.   preference to getuid() and getgid(), which in Berkeley-based Unixes do
  13260.   not allow arbitrary switching back and forth of real & effective uid.
  13261.   This construction also allows -DSETREUID to be put on the cc command line
  13262.   for any system that has and wants to use setre[ug]id().  It also prevents
  13263.   automatic definition of SETREUID if -DNOSETREU is included on the cc
  13264.   command line (or otherwise defined).
  13265. */
  13266. #ifdef FT18                /* None of this for Fortune. */
  13267. #define NOSETREU
  13268. #endif /* FT18 */
  13269.  
  13270. #ifdef ANYBSD
  13271. #ifndef BSD29
  13272. #ifndef BSD41
  13273. #ifndef SETREUID
  13274. #ifndef NOSETREU
  13275. #ifndef SETEUID
  13276. #define SETREUID
  13277. #endif /* SETEUID */
  13278. #endif /* NOSETREU */
  13279. #endif /* SETREUID */
  13280. #endif /* !BSD41 */
  13281. #endif /* !BSD29 */
  13282. #endif /* ANYBSD */
  13283.  
  13284. /* Variables for user and group IDs. */
  13285.  
  13286. static UID_T realuid = (UID_T) -1, privuid = (UID_T) -1;
  13287. static GID_T realgid = (GID_T) -1, privgid = (GID_T) -1;
  13288.  
  13289.  
  13290. /* P R I V _ I N I  --  Initialize privileges package  */
  13291.  
  13292. /* Called as early as possible in a set-uid or set-gid program to store the
  13293.  * set-to uid and/or gid and step down to the users real uid and gid. The
  13294.  * stored id's can be temporarily restored (allowed in System V) during
  13295.  * operations that require the privilege.  Most of the time, the program
  13296.  * should execute in unpriviliged state, to not impose any security threat.
  13297.  *
  13298.  * Note: Don't forget that access() always uses the real id:s to determine
  13299.  * file access, even with privileges restored.
  13300.  *
  13301.  * Returns an error mask, with error values or:ed together:
  13302.  *   1 if setuid() fails,
  13303.  *   2 if setgid() fails, and
  13304.  *   4 if the program is set-user-id to "root", which can't be handled.
  13305.  *
  13306.  * Only the return value 0 indicates real success. In case of failure,
  13307.  * those privileges that could be reduced have been, at least, but the
  13308.  * program should be aborted none-the-less.
  13309.  *
  13310.  * Also note that these functions do not expect the uid or gid to change
  13311.  * without their knowing. It may work if it is only done temporarily, but
  13312.  * you're on your own.
  13313.  */
  13314. int
  13315. priv_ini() {
  13316.     int err = 0;
  13317.  
  13318.     /* Save real ID:s. */
  13319.     realuid = getuid();
  13320.     realgid = getgid();
  13321.  
  13322.     /* Save current effective ID:s, those set to at program exec. */
  13323.     privuid = geteuid();
  13324.     privgid = getegid();
  13325.  
  13326.     /* If running set-uid, go down to real uid, otherwise remember that
  13327.      * no privileged uid is available.
  13328.      *
  13329.      * Exceptions:
  13330.      *
  13331.      * 1) If the real uid is already "root" and the set-uid uid (the
  13332.      * initial effective uid) is not "root", then we would have trouble
  13333.      * if we went "down" to "root" here, and then temporarily back to the
  13334.      * set-uid uid (not "root") and then again tried to become "root". I
  13335.      * think the "saved set-uid" is lost when changing uid from effective
  13336.      * uid "root", which changes all uid, not only the effective uid. But
  13337.      * in this situation, we can simply go to "root" and stay there all
  13338.      * the time. That should give sufficient privilege (understatement!),
  13339.      * and give the right uids for subprocesses.
  13340.      *
  13341.      * 2) If the set-uid (the initial effective uid) is "root", and we
  13342.      * change uid to the real uid, we can't change it back to "root" when
  13343.      * we need the privilege, for the same reason as in 1). Thus, we can't
  13344.      * handle programs that are set-user-id to "root" at all. The program
  13345.      * should be stopped.  Use some other uid.  "root" is probably too
  13346.      * privileged for such things, anyway. (The uid is reverted to the
  13347.      * real uid until termination.)
  13348.      *
  13349.      * These two exceptions have the effect that the "root" uid will never
  13350.      * be one of the two uids that are being switched between, which also
  13351.      * means we don't have to check for such cases in the switching
  13352.      * functions.
  13353.      *
  13354.      * Note that exception 1) is handled by these routines (by constantly
  13355.      * running with uid "root", while exception 2) is a serious error, and
  13356.      * is not provided for at all in the switching functions.
  13357.      */
  13358.     if (realuid == privuid)
  13359.     privuid = (UID_T) -1;        /* Not running set-user-id. */
  13360.  
  13361.     /* If running set-gid, go down to real gid, otherwise remember that
  13362.      * no privileged gid is available.
  13363.      *
  13364.      * There are no exception like there is for the user id, since there
  13365.      * is no group id that is privileged in the manner of uid "root".
  13366.      * There could be equivalent problems for group changing if the
  13367.      * program sometimes ran with uid "root" and sometimes not, but
  13368.      * that is already avoided as explained above.
  13369.      *
  13370.      * Thus we can expect always to be able to switch to the "saved set-
  13371.      * gid" when we want, and back to the real gid again. You may also
  13372.      * draw the conclusion that set-gid provides for fewer hassles than
  13373.      * set-uid.
  13374.      */
  13375.  
  13376. #ifdef SUIDDEBUG
  13377.     fprintf(stderr,"UID_ROOT=%d\n",UID_ROOT);
  13378.     fprintf(stderr,"realuid=%d\n",realuid);
  13379.     fprintf(stderr,"privuid=%d\n",privuid);
  13380. #endif /* SUIDDEBUG */
  13381.  
  13382.     if (realgid == privgid)        /* If not running set-user-id, */
  13383.       privgid = (GID_T) -1;        /*  remember it this way. */
  13384.  
  13385.     err = priv_off();            /* Turn off setuid privilege. */
  13386.  
  13387.     if (privuid == UID_ROOT)        /* If setuid to root, */
  13388.       err |= 4;                /* return this error. */
  13389.  
  13390.     if (realuid == UID_ROOT) {        /* If real id is root, */
  13391.     privuid = (UID_T) -1;        /* stay root at all times. */
  13392. #ifdef ATT7300
  13393.     /* If Kermit installed SUID uucp and user is running as root */
  13394.     err &= ~1;            /* System V R0 does not save UID */
  13395. #endif /* ATT7300 */
  13396.     }
  13397.     return(err);
  13398. }
  13399.  
  13400.  
  13401. /* Macros for hiding the differences in UID/GID setting between various Unix
  13402.  * systems. These macros should always be called with both the privileged ID
  13403.  * and the non-privileged ID. The one in the second argument, will become the
  13404.  * effective ID. The one in the first argument will be retained for later
  13405.  * retrieval.
  13406.  */
  13407. #ifdef SETREUID
  13408. #ifdef SAVEDUID
  13409. /* On BSD systems with the saved-UID feature, we just juggle the effective
  13410.  * UID back and forth, and leave the real UID at its true value.  The kernel
  13411.  * allows switching to both the current real UID, the effective UID, and the
  13412.  * UID which the program is set-UID to.  The saved set-UID always holds the
  13413.  * privileged UID for us, and the real UID will always be the non-privileged,
  13414.  * and we can freely choose one of them for the effective UID at any time.
  13415.  */
  13416. #define switchuid(hidden,active) setreuid( (UID_T) -1, active)
  13417. #define switchgid(hidden,active) setregid( (GID_T) -1, active)
  13418.  
  13419. #else   /* SETREUID,!SAVEDUID */
  13420.  
  13421. /* On systems with setreXid() but without the saved-UID feature, notably
  13422.  * BSD 4.2, we swap the real and effective UIDs each time.  It's
  13423.  * the effective UID that we are interested in, but we have to retain the
  13424.  * unused UID somewhere to enable us to restore it later, and we do this
  13425.  * in the real UID.  The kernel only allows switching to either the current
  13426.  * real or the effective UID, unless you're "root".
  13427.  */
  13428. #define switchuid(hidden,active)    setreuid(hidden,active)
  13429. #define switchgid(hidden,active)    setregid(hidden,active)
  13430. #endif
  13431.  
  13432. #else /* !SETREUID, !SAVEDUID */
  13433.  
  13434. #ifdef SETEUID
  13435. /*
  13436.   BSD 4.4 works similarly to System V and POSIX (see below), but uses
  13437.   seteXid() instead of setXid() to change effective IDs.  In addition, the
  13438.   seteXid() functions work the same for "root" as for other users.
  13439. */
  13440. #define switchuid(hidden,active)    seteuid(active)
  13441. #define switchgid(hidden,active)    setegid(active)
  13442.  
  13443. #else /* !SETEUID */
  13444.  
  13445. /* On System V and POSIX, the only thing we can change is the effective UID
  13446.  * (unless the current effective UID is "root", but initsuid() avoids that for
  13447.  * us).  The kernel allows switching to the current real UID or to the saved
  13448.  * set-UID.  These are always set to the non-privileged UID and the privileged
  13449.  * UID, respectively, and we only change the effective UID.  This breaks if
  13450.  * the current effective UID is "root", though, because for "root" setuid/gid
  13451.  * becomes more powerful, which is why initsuid() treats "root" specially.
  13452.  * Note: That special treatment maybe could be ignored for BSD?  Note: For
  13453.  * systems that don't fit any of these four cases, we simply can't support
  13454.  * set-UID.
  13455.  */
  13456. #define switchuid(hidden,active)    setuid(active)
  13457. #define switchgid(hidden,active)    setgid(active)
  13458.  
  13459. #endif /* SETEUID */
  13460. #endif /* SETREUID */
  13461.  
  13462.  
  13463. /* P R I V _ O N  --  Turn on the setuid and/or setgid */
  13464.  
  13465. /* Go to the privileged uid (gid) that the program is set-user-id
  13466.  * (set-group-id) to, unless the program is running unprivileged.
  13467.  * If setuid() fails, return value will be 1. If getuid() fails it
  13468.  * will be 2.  Return immediately after first failure, and the function
  13469.  * tries to restore any partial work done.  Returns 0 on success.
  13470.  * Group id is changed first, since it is less serious than user id.
  13471.  */
  13472. int
  13473. priv_on() {
  13474.     if (privgid != (GID_T) -1)
  13475.       if (switchgid(realgid,privgid))
  13476.         return(2);
  13477.  
  13478.     if (privuid != (UID_T) -1)
  13479.       if (switchuid(realuid,privuid)) {
  13480.       if (privgid != (GID_T) -1)
  13481.         switchgid(privgid,realgid);
  13482.       return(1);
  13483.       }
  13484.     return(0);
  13485. }
  13486.  
  13487. /* P R I V _ O F F  --  Turn on the real uid and gid */
  13488.  
  13489. /* Return to the unprivileged uid (gid) after an temporary visit to
  13490.  * privileged status, unless the program is running without set-user-id
  13491.  * (set-group-id). Returns 1 for failure in setuid() and 2 for failure
  13492.  * in setgid() or:ed together. The functions tries to return both uid
  13493.  * and gid to unprivileged state, regardless of errors. Returns 0 on
  13494.  * success.
  13495.  */
  13496. int
  13497. priv_off() {
  13498.     int err = 0;
  13499.  
  13500.     if (privuid != (UID_T) -1)
  13501.        if (switchuid(privuid,realuid))
  13502.       err |= 1;
  13503.  
  13504.     if (privgid != (GID_T) -1)
  13505.        if (switchgid(privgid,realgid))
  13506.     err |= 2;
  13507.  
  13508.     return(err);
  13509. }
  13510.  
  13511. /* Turn off privilege permanently.  No going back.  This is necessary before
  13512.  * a fork() on BSD43 machines that don't save the setUID or setGID, because
  13513.  * we swap the real and effective ids, and we don't want to let the forked
  13514.  * process swap them again and get the privilege back. It will work on other
  13515.  * machines too, such that you can rely on its effect always being the same,
  13516.  * for instance, even when you're in priv_on() state when this is called.
  13517.  * (Well, that part about "permanent" is on System V only true if you follow
  13518.  * this with a call to exec(), but that's what we want it for anyway.)
  13519.  * Added by Dean Long -- dlong@midgard.ucsc.edu
  13520.  */
  13521. int
  13522. priv_can() {
  13523.  
  13524. #ifdef SETREUID
  13525.     int err = 0;
  13526.     if (privuid != (UID_T) -1)
  13527.        if (setreuid(realuid,realuid))
  13528.       err |= 1;
  13529.  
  13530.     if (privgid != (GID_T) -1)
  13531.         if (setregid(realgid,realgid))
  13532.        err |= 2;
  13533.  
  13534.     return(err);
  13535.  
  13536. #else
  13537. #ifdef SETEUID
  13538.     int err = 0;
  13539.     if (privuid != (UID_T) -1)
  13540.     if (setuid(realuid)) {
  13541.         debug(F101,"setuid failed","",errno);
  13542.         err |= 1;
  13543.         debug(F101,"ruid","",getuid());
  13544.         debug(F101,"euid","",geteuid());
  13545.     }
  13546.     debug(F101,"setuid","",realuid);
  13547.     if (privgid != (GID_T) -1)
  13548.         if (setgid(realgid)) {
  13549.         debug(F101,"setgid failed","",errno);
  13550.         err |= 2;
  13551.         debug(F101,"rgid","",getgid());
  13552.         debug(F101,"egid","",getegid());
  13553.     }
  13554.     debug(F101,"setgid","",realgid);
  13555.     return(err);
  13556. #else
  13557.     /* Easy way of using setuid()/setgid() instead of setreuid()/setregid().*/
  13558.     return(priv_off());
  13559. #endif /* SETEUID */
  13560. #endif /* SETREUID */
  13561. }
  13562.  
  13563. /* P R I V _ O P N  --  For opening protected files or devices. */
  13564.  
  13565. int
  13566. priv_opn(name, modes) char *name; int modes; {
  13567.     int x;
  13568.     priv_on();                /* Turn privileges on */
  13569.     debug(F111,"priv_opn",name,modes);
  13570.     errno = 0;
  13571.     x = open(name, modes);        /* Try to open the device */
  13572.     debug(F101,"priv_opn result","",x);
  13573.     debug(F101,"priv_opn errno","",errno);
  13574.     priv_off();                /* Turn privileges off */
  13575.     return(x);                /* Return open's return code */
  13576. }
  13577.  
  13578. /*  P R I V _ C H K  --  Check privileges.  */
  13579.  
  13580. /*  Try to turn them off.  If turning them off did not succeed, cancel them */
  13581.  
  13582. int
  13583. priv_chk() {
  13584.     int x, y = 0;
  13585.     x = priv_off();            /* Turn off privs. */
  13586.     if (x != 0 || getuid() == privuid || geteuid() == privuid)
  13587.       y = priv_can();
  13588.     if (x != 0 || getgid() == privgid || getegid() == privgid)
  13589.       y = y | priv_can();
  13590.     return(y);
  13591. }
  13592.  
  13593. UID_T
  13594. real_uid() {
  13595.     return(realuid);
  13596. }
  13597.  
  13598. VOID
  13599. ttimoff() {                /* Turn off any timer interrupts */
  13600.     /* int xx; */
  13601. /*
  13602.   As of 5A(183), we set SIGALRM to SIG_IGN (to ignore alarms) rather than to
  13603.   SIG_DFL (to catch alarms, or if there is no handler, to exit).  This is to
  13604.   cure (mask, really) a deeper problem with stray alarms that occurs on some
  13605.   systems, possibly having to do with sleep(), that caused core dumps.  It
  13606.   should be OK to do this, because no code in this module uses nested alarms.
  13607.   (But we still have to watch out for SCRIPT and DIAL...)
  13608. */
  13609.     /* xx = */ alarm(0);
  13610.     /* debug(F101,"ttimoff alarm","",xx); */
  13611.     if (saval) {            /* Restore any previous */
  13612.     signal(SIGALRM,saval);        /* alarm handler. */
  13613.     /* debug(F101,"ttimoff alarm restoring saval","",saval); */
  13614.     saval = NULL;
  13615.     } else {
  13616.     signal(SIGALRM,SIG_IGN);    /* Used to be SIG_DFL */
  13617.     /* debug(F100,"ttimoff alarm SIG_IGN","",0); */
  13618.     }
  13619. }
  13620.  
  13621. /* T T R U N C M D  --  Redirect an external command over the connection. */
  13622.  
  13623. #ifdef CK_REDIR
  13624. int
  13625. ttruncmd(s) char *s; {
  13626.     PID_T pid;                /* pid of lower fork */
  13627.     int wstat;                /* for wait() */
  13628.     int x;
  13629.     int statusp;
  13630.  
  13631.     if (ttyfd == -1) {
  13632.     printf("?Sorry, device is not open\n");
  13633.     return(0);
  13634.     }
  13635.     if (nopush) {
  13636.     debug(F100,"ttruncmd fail: nopush","",0);
  13637.     return(0);
  13638.     }
  13639.     conres();                /* Make console normal  */
  13640.     pexitstat = -4;
  13641.     if ((pid = fork()) == 0) {        /* Make a child fork */
  13642.     if (priv_can())            /* Child: turn off privs. */
  13643.       exit(1);
  13644.     dup2(ttyfd, 0);            /* Give stdin/out to the line */
  13645.     dup2(ttyfd, 1);
  13646.     x = system(s);
  13647.     debug(F101,"ttruncmd system",s,x);
  13648.     _exit(x ? BAD_EXIT : 0);
  13649.     } else {
  13650.     SIGTYP (*istat)(), (*qstat)();
  13651.     if (pid == (PID_T) -1)        /* fork() failed? */
  13652.       return(0);
  13653.     istat = signal(SIGINT,SIG_IGN); /* Let the fork handle keyboard */
  13654.     qstat = signal(SIGQUIT,SIG_IGN); /* interrupts itself... */
  13655.  
  13656. #ifdef COMMENT
  13657.         while (((wstat = wait(&statusp)) != pid) && (wstat != -1)) ;
  13658. #else  /* Not COMMENT */
  13659.         while (1) {
  13660.         wstat = wait(&statusp);
  13661.         debug(F101,"ttruncmd wait","",wstat);
  13662.         if (wstat == pid || wstat == -1)
  13663.           break;
  13664.     }
  13665. #endif /* COMMENT */
  13666.  
  13667.     pexitstat = (statusp & 0xff) ? statusp : statusp >> 8;
  13668.     debug(F101,"ttruncmd wait statusp","",statusp);
  13669.     debug(F101,"ttruncmd wait pexitstat","",pexitstat);
  13670.     signal(SIGINT,istat);        /* Restore interrupts */
  13671.     signal(SIGQUIT,qstat);
  13672.     }
  13673.     concb((char)escchr);        /* Restore console to CBREAK mode */
  13674.     return(statusp == 0 ? 1 : 0);
  13675. }
  13676. #endif /* CK_REDIR */
  13677.  
  13678. struct tm *
  13679. #ifdef CK_ANSIC
  13680. cmdate2tm(char * date, int gmt)         /* date as "yyyymmdd hh:mm:ss" */
  13681. #else
  13682. cmdate2tm(date,gmt) char * date; int gmt;
  13683. #endif
  13684. {
  13685.     /* date as "yyyymmdd hh:mm:ss" */
  13686.     static struct tm _tm;
  13687.     time_t now;
  13688.  
  13689.     if (strlen(date) != 17 ||
  13690.     date[8] != ' ' ||
  13691.     date[11] != ':' ||
  13692.     date[14] != ':')
  13693.       return(NULL);
  13694.  
  13695.     time(&now);
  13696.     if (gmt)
  13697.       _tm = *gmtime(&now);
  13698.     else
  13699.       _tm = *localtime(&now);
  13700.     _tm.tm_year = (date[0]-'0')*1000 + (date[1]-'0')*100 +
  13701.                   (date[2]-'0')*10   + (date[3]-'0')-1900;
  13702.     _tm.tm_mon  = (date[4]-'0')*10   + (date[5]-'0')-1;
  13703.     _tm.tm_mday = (date[6]-'0')*10   + (date[7]-'0');
  13704.     _tm.tm_hour = (date[9]-'0')*10   + (date[10]-'0');
  13705.     _tm.tm_min  = (date[12]-'0')*10  + (date[13]-'0');
  13706.     _tm.tm_sec  = (date[15]-'0')*10  + (date[16]-'0');
  13707.  
  13708.     /* Should we set _tm.tm_isdst to -1 here? */
  13709.  
  13710.     _tm.tm_wday = 0;
  13711.     _tm.tm_yday = 0;
  13712.  
  13713.     return(&_tm);
  13714. }
  13715.  
  13716. #ifdef OXOS
  13717. #undef kill
  13718. #endif /* OXOS */
  13719.  
  13720. #ifdef OXOS
  13721. int
  13722. priv_kill(pid, sig) int pid, sig; {
  13723.     int    i;
  13724.  
  13725.     if (priv_on())
  13726.     debug(F100,"priv_kill priv_on failed","",0);
  13727.     i = kill(pid, sig);
  13728.     if (priv_off())
  13729.     debug(F100,"priv_kill priv_off failed","",0);
  13730.     return(i);
  13731. }
  13732. #endif /* OXOS */
  13733.  
  13734. #ifdef BEOSORBEBOX
  13735. /* #ifdef BE_DR_7 */
  13736. /*
  13737.   alarm() function not supplied with Be OS DR7 - this one contributed by
  13738.   Neal P. Murphy.
  13739. */
  13740.  
  13741. /*
  13742.   This should mimic the UNIX/POSIX alarm() function well enough, with the
  13743.   caveat that one's SIGALRM handler must call alarm_expired() to clean up vars
  13744.   and wait for the alarm thread to finish.
  13745. */
  13746. unsigned int
  13747. alarm(unsigned int seconds) {
  13748.     long time_left = 0;
  13749.  
  13750. /* If an alarm is active, turn it off, saving the unused time */
  13751.     if (alarm_thread != -1) {
  13752.         /* We'll be generous and count partial seconds as whole seconds. */
  13753.         time_left = alarm_struct.time -
  13754.       ((system_time() - time_started) / 1000000.0);
  13755.  
  13756.         /* Kill the alarm thread */
  13757.         kill_thread (alarm_thread);
  13758.  
  13759.         /* We need to clean up as though the alarm occured. */
  13760.         time_started = 0;
  13761.         alarm_struct.thread = -1;
  13762.         alarm_struct.time = 0;
  13763.         alarm_expired();
  13764.     }
  13765.  
  13766. /* Set a new alarm clock, if requested. */
  13767.     if (seconds > 0) {
  13768.         alarm_struct.thread = find_thread(NULL);
  13769.         alarm_struct.time = seconds;
  13770.         time_started = system_time();
  13771.         alarm_thread = spawn_thread (do_alarm,
  13772.                                      "alarm_thread",
  13773.                                      B_NORMAL_PRIORITY,
  13774.                                      (void *) &alarm_struct
  13775.                      );
  13776.         resume_thread (alarm_thread);
  13777.     }
  13778.  
  13779. /* Now return [unused time | 0] */
  13780.     return ((unsigned int) time_left);
  13781. }
  13782.  
  13783. /*
  13784.   This function is the departure from UNIX/POSIX alarm handling. In the case
  13785.   of Be's missing alarm() function, this stuff needs to be done in the SIGALRM
  13786.   handler. When Be implements alarm(), this function call can be eliminated
  13787.   from user's SIGALRM signal handlers.
  13788. */
  13789.  
  13790. void
  13791. alarm_expired(void) {
  13792.     long ret_val;
  13793.  
  13794.     if (alarm_thread != -1) {
  13795.         wait_for_thread (alarm_thread, &ret_val);
  13796.         alarm_thread = -1;
  13797.     }
  13798. }
  13799.  
  13800. /*
  13801.   This is the function that snoozes the requisite number of seconds and then
  13802.   SIGALRMs the calling thread. Note that kill() wants a pid_t arg, whilst Be
  13803.   uses thread_id; currently they are both typdef'ed as long, but I'll do the
  13804.   cast anyway. This function is run in a separate thread.
  13805. */
  13806.  
  13807. long
  13808. do_alarm (void *alarm_struct) {
  13809.     snooze ((double) ((struct ALARM_STRUCT *) alarm_struct)->time * 1000000.0);
  13810.     kill ((pid_t)((struct ALARM_STRUCT *) alarm_struct)->thread, SIGALRM);
  13811.     time_started = 0;
  13812.     ((struct ALARM_STRUCT *) alarm_struct)->thread = -1;
  13813.     ((struct ALARM_STRUCT *) alarm_struct)->time = 0;
  13814. }
  13815. /* #endif */ /* BE_DR_7 */
  13816. #endif /* BEOSORBEBOX */
  13817.  
  13818. #ifdef Plan9
  13819.  
  13820. int
  13821. p9ttyctl(char letter, int num, int param) {
  13822.     char cmd[20];
  13823.     int len;
  13824.  
  13825.     if (ttyctlfd < 0)
  13826.       return -1;
  13827.  
  13828.     cmd[0] = letter;
  13829.     if (num)
  13830.       len = sprintf(cmd + 1, "%d", param) + 1;
  13831.     else {
  13832.     cmd[1] = param;
  13833.     len = 2;
  13834.     }
  13835.     if (write(ttyctlfd, cmd, len) == len) {
  13836.     cmd[len] = 0;
  13837.     /* fprintf(stdout, "wrote '%s'\n", cmd); */
  13838.     return 0;
  13839.     }
  13840.     return -1;
  13841. }
  13842.  
  13843. int
  13844. p9ttyparity(char l) {
  13845.     return p9ttyctl('p', 0, l);
  13846. }
  13847.  
  13848. int
  13849. p9tthflow(int flow, int status) {
  13850.     return p9ttyctl('m', 1, status);
  13851. }
  13852.  
  13853. int
  13854. p9ttsspd(int cps) {
  13855.     if (p9ttyctl('b', 1, cps * 10) < 0)
  13856.       return -1;
  13857.     ttylastspeed = cps * 10;
  13858.     return 0;
  13859. }
  13860.  
  13861. int
  13862. p9openttyctl(char *ttname) {
  13863.     char name[100];
  13864.  
  13865.     if (ttyctlfd >= 0) {
  13866.     close(ttyctlfd);
  13867.     ttyctlfd = -1;
  13868.     ttylastspeed = -1;
  13869.     }
  13870.     sprintf(name, "%sctl", ttname);
  13871.     ttyctlfd = open(name, 1);
  13872.     return ttyctlfd;
  13873. }
  13874.  
  13875. int
  13876. p9concb() {
  13877.     if (consctlfd >= 0) {
  13878.     if (write(consctlfd, "rawon", 5) == 5)
  13879.       return 0;
  13880.     }
  13881.     return -1;
  13882. }
  13883.  
  13884. int
  13885. p9conbin() {
  13886.     return p9concb();
  13887. }
  13888.  
  13889. int
  13890. p9conres() {
  13891.     if (consctlfd >= 0) {
  13892.     if (write(consctlfd, "rawoff", 6) == 6)
  13893.       return 0;
  13894.     }
  13895.     return -1;
  13896. }
  13897.  
  13898. int
  13899. p9sndbrk(int msec) {
  13900.     if (ttyctlfd >= 0) {
  13901.     char cmd[20];
  13902.     int i = sprintf(cmd, "k%d", msec);
  13903.     if (write(ttyctlfd, cmd, i) == i)
  13904.       return 0;
  13905.     }
  13906.     return -1;
  13907. }
  13908.  
  13909. int
  13910. conwrite(char *buf, int n) {
  13911.     int x;
  13912.     static int length = 0;
  13913.     static int holdingcr = 0;
  13914.     int normal = 0;
  13915.     for (x = 0; x < n; x++) {
  13916.     char c = buf[x];
  13917.     if (c == 007) {
  13918.         if (normal) {
  13919.         write(1, buf + (x - normal), normal);
  13920.         length += normal;
  13921.         normal = 0;
  13922.         }
  13923.         /* write(noisefd, "1000 300", 8); */
  13924.         holdingcr = 0;
  13925.     } else if (c == '\r') {
  13926.         if (normal) {
  13927.         write(1, buf + (x - normal), normal);
  13928.         length += normal;
  13929.         normal = 0;
  13930.         }
  13931.         holdingcr = 1;
  13932.     } else if (c == '\n') {
  13933.         write(1, buf + (x - normal), normal + 1);
  13934.         normal = 0;
  13935.         length = 0;
  13936.         holdingcr = 0;
  13937.     } else if (c == '\b') {
  13938.         if (normal) {
  13939.         write(1, buf + (x - normal), normal);
  13940.         length += normal;
  13941.         normal = 0;
  13942.         }
  13943.         if (length) {
  13944.         write(1, &c, 1);
  13945.         length--;
  13946.         }
  13947.         holdingcr = 0;
  13948.     } else {
  13949.         if (holdingcr) {
  13950.         char b = '\b';
  13951.         while (length-- > 0)
  13952.           write(1, &b, 1);
  13953.         length = 0;    /* compiler bug */
  13954.         }
  13955.         holdingcr = 0;
  13956.         normal++;
  13957.     }
  13958.     }
  13959.     if (normal) {
  13960.     write(1, buf + (x - normal), normal);
  13961.     length += normal;
  13962.     }
  13963.     return n;
  13964. }
  13965.  
  13966. void
  13967. conprint(char *fmt, ...) {
  13968.     static char buf[1000];        /* not safe if on the stack */
  13969.  
  13970.     va_list ap;
  13971.     int i;
  13972.  
  13973.     va_start(ap, fmt);
  13974.     i = vsprintf(buf, fmt, ap);
  13975.     conwrite(buf, i);
  13976. }
  13977. #endif /* Plan9 */
  13978.  
  13979. /* fprintf, printf, perror replacements... */
  13980.  
  13981. /* f p r i n t f */
  13982.  
  13983. #ifdef UNIX
  13984. #ifdef CK_ANSIC
  13985. #include <stdarg.h>
  13986. #else /* CK_ANSIC */
  13987. #include <varargs.h>
  13988. #endif /* CK_ANSIC */
  13989. #ifdef fprintf
  13990. #undef fprintf
  13991. static char str1[4096];
  13992. static char str2[4096];
  13993. int
  13994. #ifdef CK_ANSIC
  13995. ckxfprintf(FILE * file, const char * format, ...)
  13996. #else /* CK_ANSIC */
  13997. ckxfprintf(va_alist) va_dcl
  13998. #endif /* CK_ANSIC */
  13999. /* ckxfprintf */ {
  14000.     int i, j, len, got_cr;
  14001.     va_list args;
  14002.     int rc = 0;
  14003.  
  14004. #ifdef CK_ANSIC
  14005.     va_start(args, format);
  14006. #else /* CK_ANSIC */
  14007.     char * format;
  14008.     FILE * file;
  14009.     va_start(args);
  14010.     file = va_arg(args,FILE *);
  14011.     format = va_arg(args,char *);
  14012. #endif /* CK_ANSIC */
  14013.  
  14014.     if (!inserver || (file != stdout && file != stderr && file != stdin)) {
  14015.     rc = vfprintf(file,format,args);
  14016.     } else {
  14017.     unsigned int c;
  14018.         rc = vsprintf(str1, format, args);
  14019.         len = strlen(str1);
  14020.         if (len >= sizeof(str1)) {
  14021.             debug(F101,"ckxfprintf() buffer overflow","",len);
  14022.             doexit(BAD_EXIT,1);
  14023.         }
  14024.         for (i = 0, j = 0, got_cr = 0;
  14025.          i < len && j < sizeof(str1)-2;
  14026.          i++, j++ ) {
  14027.         /* We can't use 255 as a case label because of signed chars */
  14028.         c = (unsigned)(str1[i] & 0xff);
  14029. #ifdef TNCODE
  14030.         if (c == 255) {
  14031.         if (got_cr && !TELOPT_ME(TELOPT_BINARY))
  14032.           str2[j++] = '\0';
  14033.         str2[j++] = IAC;
  14034.         str2[j] = IAC;
  14035.         got_cr = 0;
  14036.         } else
  14037. #endif /* TNCODE */
  14038.         switch (c) {
  14039.           case '\r':
  14040.                 if (got_cr
  14041. #ifdef TNCODE
  14042.             && !TELOPT_ME(TELOPT_BINARY)
  14043. #endif /* TNCODE */
  14044.             )
  14045.           str2[j++] = '\0';
  14046.                 str2[j] = str1[i];
  14047.                 got_cr = 1;
  14048.                 break;
  14049.           case '\n':
  14050.                 if (!got_cr)
  14051.           str2[j++] = '\r';
  14052.                 str2[j] = str1[i];
  14053.                 got_cr = 0;
  14054.                 break;
  14055.           default:
  14056.                 if (got_cr
  14057. #ifdef TNCODE
  14058.             && !TELOPT_ME(TELOPT_BINARY)
  14059. #endif /* TNCODE */
  14060.             )
  14061.           str2[j++] = '\0';
  14062.                 str2[j] = str1[i];
  14063.                 got_cr = 0;
  14064.             }
  14065.         }
  14066.         if (got_cr
  14067. #ifdef TNCODE
  14068.              && !TELOPT_ME(TELOPT_BINARY)
  14069. #endif /* TNCODE */
  14070.              )
  14071.             str2[j++] = '\0';
  14072. #ifdef CK_ENCRYPTION
  14073. #ifdef TNCODE
  14074.         if (TELOPT_ME(TELOPT_ENCRYPTION))
  14075.       ck_tn_encrypt(str2,j);
  14076. #endif /* TNCODE */
  14077. #endif /* CK_ENCRYPTION */
  14078. #ifdef CK_SSL
  14079.     if (inserver && (ssl_active_flag || tls_active_flag)) {
  14080.         /* Write using SSL */
  14081.             char * p = str2;
  14082.           ssl_retry:
  14083.             if (ssl_active_flag)
  14084.           rc = SSL_write(ssl_con, p, j);
  14085.             else
  14086.           rc = SSL_write(tls_con, p, j);
  14087.         debug(F111,"ckxfprintf","SSL_write",rc);
  14088.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,rc)) {
  14089.           case SSL_ERROR_NONE:
  14090.                 if (rc == j)
  14091.           break;
  14092.                 p += rc;
  14093.                 j -= rc;
  14094.                 goto ssl_retry;
  14095.           case SSL_ERROR_WANT_WRITE:
  14096.           case SSL_ERROR_WANT_READ:
  14097.           case SSL_ERROR_SYSCALL:
  14098.                 if (rc != 0)
  14099.           return(-1);
  14100.           case SSL_ERROR_WANT_X509_LOOKUP:
  14101.           case SSL_ERROR_SSL:
  14102.           case SSL_ERROR_ZERO_RETURN:
  14103.           default:
  14104.                 rc = 0;
  14105.             }
  14106.     } else
  14107. #endif /* CK_SSL */
  14108.         fwrite(str2,sizeof(char),j,stdout);
  14109.     }
  14110.     va_end(args);
  14111.     return(rc);
  14112. }
  14113. #endif /* fprintf */
  14114.  
  14115. /* p r i n t f */
  14116.  
  14117. #ifdef printf
  14118. #undef printf
  14119. int
  14120. #ifdef CK_ANSIC
  14121. ckxprintf(const char * format, ...)
  14122. #else /* CK_ANSIC */
  14123. ckxprintf(va_alist) va_dcl
  14124. #endif /* CK_ANSIC */
  14125. /* ckxprintf */ {
  14126.     int i, j, len, got_cr;
  14127.     va_list args;
  14128.     int rc = 0;
  14129.  
  14130. #ifdef CK_ANSIC
  14131.     va_start(args, format);
  14132. #else /* CK_ANSIC */
  14133.     char * format;
  14134.     va_start(args);
  14135.     format = va_arg(args,char *);
  14136. #endif /* CK_ANSIC */
  14137.  
  14138.     if (!inserver) {
  14139.     rc = vprintf(format, args);
  14140.     } else {
  14141.     unsigned int c;
  14142.         rc = vsprintf(str1, format, args);
  14143.         len = strlen(str1);
  14144.         if (len >= sizeof(str1)) {
  14145.             debug(F101,"ckxprintf() buffer overflow","",len);
  14146.             doexit(BAD_EXIT,1);
  14147.         }
  14148.         for (i = 0, j = 0, got_cr=0;
  14149.          i < len && j < sizeof(str1)-2;
  14150.          i++, j++ ) {
  14151.         c = (unsigned)(str1[i] & 0xff);
  14152. #ifdef TNCODE
  14153.         if (c == 255) {
  14154.         if (got_cr && !TELOPT_ME(TELOPT_BINARY))
  14155.           str2[j++] = '\0';
  14156.         str2[j++] = IAC;
  14157.         str2[j] = IAC;
  14158.         got_cr = 0;
  14159.         } else
  14160. #endif /* TNCODE */
  14161.         switch (c) {
  14162.           case '\r':
  14163.                 if (got_cr
  14164. #ifdef TNCODE
  14165.             && !TELOPT_ME(TELOPT_BINARY)
  14166. #endif /* TNCODE */
  14167.             )
  14168.           str2[j++] = '\0';
  14169.                 str2[j] = str1[i];
  14170.                 got_cr = 1;
  14171.                 break;
  14172.           case '\n':
  14173.                 if (!got_cr)
  14174.           str2[j++] = '\r';
  14175.                 str2[j] = str1[i];
  14176.                 got_cr = 0;
  14177.                 break;
  14178.           default:
  14179.                 if (got_cr
  14180. #ifdef TNCODE
  14181.             && !TELOPT_ME(TELOPT_BINARY)
  14182. #endif /* TNCODE */
  14183.             )
  14184.           str2[j++] = '\0';
  14185.                 str2[j] = str1[i];
  14186.                 got_cr = 0;
  14187.                 break;
  14188.         }
  14189.         }
  14190.         if (got_cr
  14191. #ifdef TNCODE
  14192.              && !TELOPT_ME(TELOPT_BINARY)
  14193. #endif /* TNCODE */
  14194.              )
  14195.             str2[j++] = '\0';
  14196. #ifdef CK_ENCRYPTION
  14197. #ifdef TNCODE
  14198.         if (TELOPT_ME(TELOPT_ENCRYPTION))
  14199.       ck_tn_encrypt(str2,j);
  14200. #endif /* TNCODE */
  14201. #endif /* CK_ENCRYPTION */
  14202. #ifdef CK_SSL
  14203.     if (inserver && (ssl_active_flag || tls_active_flag)) {
  14204.             char * p = str2;
  14205.         /* Write using SSL */
  14206.           ssl_retry:
  14207.             if (ssl_active_flag)
  14208.           rc = SSL_write(ssl_con, p, j);
  14209.             else
  14210.           rc = SSL_write(tls_con, p, j);
  14211.         debug(F111,"ckxprintf","SSL_write",rc);
  14212.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,rc)) {
  14213.           case SSL_ERROR_NONE:
  14214.                 if (rc == j)
  14215.           break;
  14216.                 p += rc;
  14217.                 j -= rc;
  14218.                 goto ssl_retry;
  14219.           case SSL_ERROR_WANT_WRITE:
  14220.           case SSL_ERROR_WANT_READ:
  14221.           case SSL_ERROR_SYSCALL:
  14222.                 if (rc != 0)
  14223.           return(-1);
  14224.           case SSL_ERROR_WANT_X509_LOOKUP:
  14225.           case SSL_ERROR_SSL:
  14226.           case SSL_ERROR_ZERO_RETURN:
  14227.           default:
  14228.                 rc = 0;
  14229.             }
  14230.     } else
  14231. #endif /* CK_SSL */
  14232.       rc = fwrite(str2,sizeof(char),j,stdout);
  14233.     }
  14234.     va_end(args);
  14235.     return(rc);
  14236. }
  14237. #endif /* printf */
  14238.  
  14239. /*  p e r r o r  */
  14240.  
  14241. #ifdef perror
  14242. #undef perror
  14243. _PROTOTYP(char * ck_errstr,(VOID));
  14244. #ifdef NEXT
  14245. void
  14246. #else
  14247. #ifdef CK_SCOV5
  14248. void
  14249. #else
  14250. int
  14251. #endif /* CK_SCOV5 */
  14252. #endif /* NEXT */
  14253. #ifdef CK_ANSIC
  14254. ckxperror(const char * str)
  14255. #else /* CK_ANSIC */
  14256. ckxperror(str) char * str;
  14257. #endif /* CK_ANSIC */
  14258. /* ckxperror */ {
  14259.     char * errstr = ck_errstr();
  14260. #ifndef NEXT
  14261. #ifndef CK_SCOV5
  14262.     return
  14263. #endif /* CK_SCOV5 */
  14264. #endif /* NEXT */
  14265.       ckxprintf("%s%s %s\n",str,*errstr?":":"",errstr);
  14266. }
  14267. #endif /* perror */
  14268. #endif /* UNIX */
  14269.  
  14270. #ifdef MINIX2
  14271.  
  14272. /* Minix doesn't have a gettimeofday call. We fake one here using time(2) */
  14273.  
  14274. int
  14275. gettimeofday(struct timeval *tp, struct timezone *tzp) {
  14276.     tp->tv_usec = 0L;            /* Close enough for horseshoes */
  14277.     if(time(&(tp->tv_sec))==-1)
  14278.       return(-1);
  14279.     return(0);
  14280. }
  14281.  
  14282. /* Minix does not support symbolic links. We implement a version of
  14283.    readlink that always fails */
  14284.  
  14285. int
  14286. readlink(const char *path, void *buf, size_t bufsiz) {
  14287.     errno = ENOSYS;
  14288.     return(-1);
  14289. }
  14290. #endif /* MINIX2 */
  14291.