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

  1. #ifdef aegis
  2. char *ckxv = "Aegis Communications support, 8.0.280, 9 Jan 2002";
  3. #else
  4. #ifdef Plan9
  5. char *ckxv = "Plan 9 Communications support, 8.0.280, 9 Jan 2002";
  6. #else
  7. char *ckxv = "UNIX Communications support, 8.0.280, 9 Jan 2002";
  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, 2002,
  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. #ifdef TNCODE
  2037.     extern char tn_env_acct[], tn_env_disp[], tn_env_job[],
  2038.     tn_env_prnt[], tn_env_sys[];
  2039. #endif /* TNCODE */
  2040.     extern char uidbuf[];
  2041.         extern char * whoami();
  2042.     char *p;
  2043. #ifdef CKSENDUID
  2044.         uidbuf[0] = '\0';
  2045. #ifdef IKSD
  2046.         if (!inserver) {
  2047. #endif /* IKSD */
  2048.             p = getenv("USER");
  2049.             debug(F110,"sysinit uidbuf from USER",uidbuf,0);
  2050.         if (!p) p = "";
  2051.             if (!*p) {
  2052.                 p = getenv("LOGNAME");
  2053.                 debug(F110,"sysinit uidbuf from LOGNAME",uidbuf,0);
  2054.             }
  2055.         if (!p) p = "";
  2056.             if (!*p) {
  2057.                 p = whoami();
  2058.                 debug(F110,"sysinit uidbuf from whoami()",uidbuf,0);
  2059.             }
  2060.         if (!p) p = "";
  2061.         ckstrncpy(uidbuf, *p ? p : "UNKNOWN", UIDBUFLEN);
  2062. #ifdef IKSD
  2063.         }
  2064. #endif /* IKSD */
  2065.     debug(F110,"sysinit final uidbuf",uidbuf,0);
  2066. #endif /* CKSENDUID */
  2067.  
  2068. #ifdef TNCODE
  2069.     if ((p = getenv("JOB"))) ckstrncpy(tn_env_job,p,63);
  2070.     if ((p = getenv("ACCT"))) ckstrncpy(tn_env_acct,p,63);
  2071.     if ((p = getenv("PRINTER"))) ckstrncpy(tn_env_prnt,p,63);
  2072.     if ((p = getenv("DISPLAY"))) ckstrncpy(tn_env_disp,p,63);
  2073. #ifdef aegis
  2074.     ckstrncpy(tn_env_sys,"Aegis",64);
  2075. #else
  2076. #ifdef Plan9
  2077.     ckstrncpy(tn_env_sys,"Plan9",64);
  2078. #else
  2079.     ckstrncpy(tn_env_sys,"UNIX",64);
  2080. #endif /* Plan9 */
  2081. #endif /* aegis */
  2082. #endif /* TNCODE */
  2083.     }
  2084. #endif /* CK_ENVIRONMENT */
  2085. #ifdef CK_SNDLOC
  2086.     {
  2087.     extern char * tn_loc;
  2088.     char *p;
  2089.     if (p = getenv("LOCATION"))
  2090.       if (tn_loc = (char *)malloc((int)strlen(p)+1))
  2091.         strcpy(tn_loc,p);        /* safe */
  2092.     }
  2093. #endif /* CK_SNDLOC */
  2094.  
  2095.     ckstrncpy(startupdir, zgtdir(), CKMAXPATH);
  2096.     startupdir[CKMAXPATH] = '\0';
  2097.     x = strlen(startupdir);
  2098.     if (x <= 0) {
  2099.     startupdir[0] = '/';
  2100.     startupdir[1] = '\0';
  2101.     } else if (startupdir[x-1] != '/') {
  2102.     startupdir[x] = '/';
  2103.     startupdir[x+1] = '\0';
  2104.     }
  2105.     debug(F110,"sysinit startupdir",startupdir,0);
  2106. #ifdef TTLEBUF
  2107.     le_init();
  2108. #endif /* TTLEBUF */
  2109. #ifdef BSD44ORPOSIX
  2110.     /* This should catch the ncurses platforms */
  2111.     /* Some platforms don't have putenv(), like NeXTSTEP */
  2112.     putenv("NCURSES_NO_SETBUF=1");
  2113. #endif /* BSD44ORPOSIX */
  2114.     return(0);
  2115. }
  2116.  
  2117. /*  S Y S C L E A N U P  --  System-dependent program cleanup.  */
  2118.  
  2119. int
  2120. syscleanup() {
  2121. #ifdef F_SETFL
  2122.     if (iniflags > -1)
  2123.       fcntl(0,F_SETFL,iniflags);    /* Restore stdin flags */
  2124. #endif /* F_SETFL */
  2125. #ifdef ultrix
  2126.     stty(0,&vanilla);                   /* Get sgtty info */
  2127. #endif /* ultrix */
  2128. #ifdef NETCMD
  2129.     if (ttpid) kill(ttpid,9);
  2130. #endif /* NETCMD */
  2131.     return(0);
  2132. }
  2133.  
  2134. /*  T T O P E N  --  Open a tty for exclusive access.  */
  2135.  
  2136. /*
  2137.   Call with:
  2138.     ttname: character string - device name or network host name.
  2139.     lcl:
  2140.   If called with lcl < 0, sets value of lcl as follows:
  2141.   0: the terminal named by ttname is the job's controlling terminal.
  2142.   1: the terminal named by ttname is not the job's controlling terminal.
  2143.   But watch out: if a line is already open, or if requested line can't
  2144.   be opened, then lcl remains (and is returned as) -1.
  2145.     modem:
  2146.   Less than zero: ttname is a network host name.
  2147.   Zero or greater: ttname is a terminal device name.
  2148.   Zero means a local connection (don't use modem signals).
  2149.   Positive means use modem signals.
  2150.    timo:
  2151.   0 = no timer.
  2152.   nonzero = number of seconds to wait for open() to return before timing out.
  2153.  
  2154.   Returns:
  2155.     0 on success
  2156.    -5 if device is in use
  2157.    -4 if access to device is denied
  2158.    -3 if access to lock directory denied
  2159.    -2 upon timeout waiting for device to open
  2160.    -1 on other error
  2161. */
  2162. static int ttotmo = 0;            /* Timeout flag */
  2163. /* Flag kept here to avoid being clobbered by longjmp.  */
  2164.  
  2165. int
  2166. ttopen(ttname,lcl,modem,timo) char *ttname; int *lcl, modem, timo; {
  2167.  
  2168. #ifdef BSD44
  2169. #define ctermid(x) strcpy(x,"")
  2170. #else
  2171. #ifdef SVORPOSIX
  2172. #ifndef CIE
  2173.     extern char *ctermid();        /* Wish they all had this! */
  2174. #else                    /* CIE Regulus */
  2175. #define ctermid(x) strcpy(x,"")
  2176. #endif /* CIE */
  2177. #endif /* SVORPOSIX */
  2178. #endif /* BSD44 */
  2179.  
  2180. #ifdef ultrix
  2181.     int temp = 0;
  2182. #endif /* ultrix */
  2183.  
  2184. #ifndef OPENFIRST
  2185.     char fullname[DEVNAMLEN+1];
  2186. #endif /* OPENFIRST */
  2187.  
  2188.     char * fnam;            /* Full name after expansion */
  2189.  
  2190.     int y;
  2191.  
  2192. #ifndef pdp11
  2193. #define NAMEFD     /* Feature to allow name to be an open file descriptor */
  2194. #endif /* pdp11 */
  2195.  
  2196. #ifdef NAMEFD
  2197.     char *p;
  2198.     debug(F101,"ttopen telnetfd","",telnetfd);
  2199. #endif /* NAMEFD */
  2200.  
  2201.     debug(F110,"ttopen ttname",ttname,0);
  2202.     debug(F110,"ttopen ttnmsv",ttnmsv,0);
  2203.     debug(F101,"ttopen modem","",modem);
  2204.     debug(F101,"ttopen netconn","",netconn);
  2205.     debug(F101,"ttopen ttyfd","",ttyfd);
  2206.     debug(F101,"ttopen *lcl","",*lcl);
  2207.     debug(F101,"ttopen ttmdm","",ttmdm);
  2208.     debug(F101,"ttopen ttnet","",ttnet);
  2209.  
  2210.     ttpmsk = 0xff;
  2211.     lockpid[0] = '\0';
  2212.  
  2213.     if (ttyfd > -1) {            /* If device already opened */
  2214.         if (!strncmp(ttname,ttnmsv,DEVNAMLEN)) /* are new & old names equal? */
  2215.       return(0);            /* Yes, nothing to do - just return */
  2216.     ttnmsv[0] = '\0';        /* No, clear out old name */
  2217.     ttclos(ttyfd);            /* close old connection.  */
  2218.     }
  2219.     wasclosed = 0;            /* New connection, not closed yet. */
  2220.     ttpipe = 0;                /* Assume it's not a pipe */
  2221.     ttpty = 0;                /* or a pty... */
  2222.  
  2223. #ifdef NETCONN
  2224. /*
  2225.   This is a bit tricky...  Suppose that previously Kermit had dialed a telnet
  2226.   modem server ("set host xxx:2001, set modem type usr, dial ...").  Then the
  2227.   connection was closed (ttyfd = -1), and then a REDIAL command was given.  At
  2228.   this point we've obliterated the negative modem type hack, and so would
  2229.   treat the IP hostname as a device name, and would then fail because of "No
  2230.   such device or directory".  But the previous connection has left behind some
  2231.   clues, so let's use them...
  2232. */
  2233.     if (ttyfd < 0) {            /* Connection is not open */
  2234.     if (!strcmp(ttname,ttnmsv)) {    /* Old and new names the same? */
  2235.         if (((netconn > 0) && (ttmdm < 0)) ||
  2236.         ((ttnet > 0) &&
  2237.          (!ckstrchr(ttname,'/')) && (ckstrchr(ttname,':')))
  2238.         ) {
  2239.         int x, rc;
  2240.         x = (ttmdm < 0) ? -ttmdm : ttnet;
  2241.         rc = netopen(ttname, lcl, x);
  2242.         debug(F111,"ttopen REOPEN netopen",ttname,rc);
  2243.         if (rc > -1) {
  2244.             netconn = 1;
  2245.             xlocal = *lcl = 1;
  2246.         } else {
  2247.             netconn = 0;
  2248.         }
  2249.         gotsigs = 0;
  2250.         return(rc);
  2251.         }
  2252.     }
  2253.     }
  2254. #endif /* NETCONN */
  2255.  
  2256. #ifdef MAXNAMLEN
  2257.     debug(F100,"ttopen MAXNAMLEN defined","",0);
  2258. #else
  2259.     debug(F100,"ttopen MAXNAMLEN *NOT* defined","",0);
  2260. #endif
  2261.  
  2262. #ifdef BSD4
  2263.     debug(F100,"ttopen BSD4 defined","",0);
  2264. #else
  2265.     debug(F100,"ttopen BSD4 *NOT* defined","",0);
  2266. #endif /* BSD4 */
  2267.  
  2268. #ifdef BSD42
  2269.     debug(F100,"ttopen BSD42 defined","",0);
  2270. #else
  2271.     debug(F100,"ttopen BSD42 *NOT* defined","",0);
  2272. #endif /* BSD42 */
  2273.  
  2274. #ifdef MYREAD
  2275.     debug(F100,"ttopen MYREAD defined","",0);
  2276. #else
  2277.     debug(F100,"ttopen MYREAD *NOT* defined","",0);
  2278. #endif /* MYREAD */
  2279.  
  2280. #ifdef    NETCONN
  2281.     if (modem < 0) {            /* modem < 0 = code for network */
  2282.     int x;
  2283.     ttmdm = modem;
  2284.     modem = -modem;            /* Positive network type number */
  2285.     fdflag = 0;            /* Stdio not redirected. */
  2286.     netconn = 1;            /* And it's a network connection */
  2287.     debug(F111,"ttopen net",ttname,modem);
  2288. #ifdef NAMEFD
  2289.     for (p = ttname; isdigit(*p); p++) ; /* Check for all digits */
  2290.      if (*p == '\0' && (telnetfd || x25fd)) { /* Avoid X.121 addresses */
  2291.         ttyfd = atoi(ttname);    /* Is there a way to test it's open? */
  2292.         ttfdflg = 1;        /* We got an open file descriptor */
  2293.         debug(F111,"ttopen got open network fd",ttname,ttyfd);
  2294.         ckstrncpy(ttnmsv,ttname,DEVNAMLEN); /* Remember the "name". */
  2295.         x = 1;            /* Return code is "good". */
  2296.         if (telnetfd) {
  2297.         ttnet = NET_TCPB;
  2298.         if (ttnproto != NP_TCPRAW)
  2299.           ttnproto = NP_TELNET;
  2300. #ifdef SUNX25
  2301.         } else if (x25fd) {
  2302.         ttnet = NET_SX25;
  2303.         ttnproto = NP_NONE;
  2304. #endif /* SUNX25 */
  2305.         }
  2306.     } else {            /* Host name or address given */
  2307. #ifdef NETPTY
  2308.         if (modem == NET_PTY) {
  2309.         int x;
  2310.         if (nopush) {
  2311.             debug(F100,"ttopen PTY: nopush","",0);
  2312.             return(-1);
  2313.         }
  2314.                 ttnet = NET_PTY;
  2315.         ttnproto = NP_NONE;
  2316.                 netconn = 1;            /* but we don't use network i/o */
  2317.                 ttpty = 1;
  2318.                 debug(F110,"ttopen PTY",ttname,0);
  2319.         x = do_pty(ttname);
  2320.         if (x > -1) {
  2321.             ckstrncpy(ttnmsv,ttname,DEVNAMLEN);
  2322.             xlocal = *lcl = 1;    /* It's local */
  2323.         } else {
  2324.             ttpty = 0;
  2325.             netconn = 0;
  2326.         }
  2327.         gotsigs = 0;
  2328.         return(x);
  2329.         }
  2330. #endif /* NETPTY */
  2331. #ifdef NETCMD
  2332. /*
  2333.   dup2() is not available on older System V platforms like AT&T 3Bx.  For
  2334.   those systems we punt by not defining NETCMD, but we might be able to do
  2335.   better -- see workarounds for this problem in ckufio.c (search for dup2).
  2336. */
  2337.         if (modem == NET_CMD) {
  2338.         if (nopush) {
  2339.             debug(F100,"ttopen pipe: nopush","",0);
  2340.             return(-1);
  2341.         }
  2342.         if (pipe(pipe0) || pipe(pipe1)) {
  2343.             perror("Pipe error");
  2344.             return(-1);
  2345.         }
  2346.         ttpid = fork();        /* Make a fork */
  2347.  
  2348.         switch (ttpid) {
  2349.           case -1:        /* Error making fork */
  2350.             close(pipe0[0]);
  2351.             close(pipe0[1]);
  2352.             close(pipe1[0]);
  2353.             close(pipe1[1]);
  2354.             perror("Fork error");
  2355.             return(-1);
  2356.           case 0:        /* Child. */
  2357.             close(pipe0[0]);
  2358.             close(pipe1[1]);
  2359.             dup2(pipe0[1], 1);
  2360.             close(pipe0[1]);
  2361.             dup2(pipe1[0], 0);
  2362.             close(pipe1[0]);
  2363.             system(ttname);
  2364.             _exit(0);
  2365.           default:        /* Parent */
  2366.             close(pipe0[1]);
  2367.             close(pipe1[0]);
  2368.             fdin = pipe0[0];    /* Read from pipe */
  2369.             fdout = pipe1[1];    /* Write to pipe */
  2370.             ttout = fdopen(fdout,"w"); /* Get stream so we can */
  2371.             if (!ttout) {    /* make it unbuffered. */
  2372.             perror("fdopen failure");
  2373.             return(-1);
  2374.             }
  2375.             setbuf(ttout,NULL);
  2376.             ckstrncpy(ttnmsv,ttname,DEVNAMLEN);
  2377.             xlocal = *lcl = 1;    /* It's local */
  2378.             netconn = 1;    /* Call it a network connection */
  2379.             ttmdm = modem;    /* Remember network type */
  2380.             ttyfd = fdin;
  2381.             ttpipe = 1;
  2382.             gotsigs = 0;
  2383.             return(0);
  2384.         }
  2385.         }
  2386. #endif /* NETCMD */
  2387. #endif /* NAMEFD */
  2388.         x = netopen(ttname, lcl, modem); /* (see ckcnet.h) */
  2389.         if (x > -1) {
  2390.         ckstrncpy(ttnmsv,ttname,DEVNAMLEN);
  2391.         } else netconn = 0;
  2392. #ifdef NAMEFD
  2393.     }
  2394. #endif /* NAMEFD */
  2395.  
  2396. #ifdef sony_news            /* Sony NEWS */
  2397.     if (ioctl(ttyfd,TIOCKGET,&km_ext) < 0) { /* Get Kanji mode */
  2398.         perror("ttopen error getting Kanji mode (network)");
  2399.         debug(F111,"ttopen error getting Kanji mode","network",0);
  2400.         km_ext = -1;        /* Make sure this stays undefined. */
  2401.     }
  2402. #endif /* sony_news */
  2403.  
  2404.     xlocal = *lcl = 1;        /* Network connections are local. */
  2405.     debug(F101,"ttopen net x","",x);
  2406. #ifdef COMMENT
  2407. /* Let netopen() do this */
  2408.     if (x > -1 && !x25fd)
  2409.       x = tn_ini();            /* Initialize TELNET protocol */
  2410. #endif /* COMMENT */
  2411.     gotsigs = 0;
  2412.     return(x);
  2413.     } else {                /* Terminal device */
  2414. #endif    /* NETCONN */
  2415.  
  2416. #ifdef NAMEFD
  2417. /*
  2418.   This code lets you give Kermit an open file descriptor for a serial
  2419.   communication device, rather than a device name.  Kermit assumes that the
  2420.   line is already open, locked, conditioned with the right parameters, etc.
  2421. */
  2422.     for (p = ttname; isdigit(*p); p++) ; /* Check for all digits */
  2423.     if (*p == '\0') {
  2424.         ttyfd = atoi(ttname);    /* Is there a way to test it's open? */
  2425.         debug(F111,"ttopen got open fd",ttname,ttyfd);
  2426.         ckstrncpy(ttnmsv,ttname,DEVNAMLEN); /* Remember the "name". */
  2427.         if (ttyfd == 0)        /* If it's stdio... */
  2428.           xlocal = *lcl = 0;    /* we're in remote mode */
  2429.         else            /* otherwise */
  2430.           xlocal = *lcl = 1;    /* local mode. */
  2431.         netconn = 0;        /* Assume it's not a network. */
  2432.         tvtflg = 0;            /* Might need to initialize modes. */
  2433.         ttmdm = modem;        /* Remember modem type. */
  2434.         fdflag = 0;            /* Stdio not redirected. */
  2435.         ttfdflg = 1;        /* Flag we were opened this way. */
  2436.  
  2437. #ifdef sony_news            /* Sony NEWS */
  2438.         /* Get device Kanji mode */
  2439.         if (ioctl(ttyfd,TIOCKGET,&km_ext) < 0) {
  2440.         perror("ttopen error getting Kanji mode");
  2441.         debug(F101,"ttopen error getting Kanji mode","",0);
  2442.         km_ext = -1;        /* Make sure this stays undefined. */
  2443.         }
  2444. #endif /* sony_news */
  2445.         gotsigs = 0;
  2446.         return(0);            /* Return success */
  2447.     }
  2448. #endif /* NAMEFD */
  2449. #ifdef NETCONN
  2450.     }
  2451. #endif /* NETCONN */
  2452.  
  2453. /* Here we have to open a serial device of the given name. */
  2454.  
  2455.     netconn = 0;            /* So it's not a network connection */
  2456.     occt = signal(SIGINT, cctrap);    /* Set Control-C trap, save old one */
  2457.     sigint_ign = 0;
  2458.  
  2459.     tvtflg = 0;            /* Flag for use by ttvt(). */
  2460.                 /* 0 = ttvt not called yet for this device */
  2461.  
  2462.     fdflag = (!isatty(0) || !isatty(1)); /* Flag for stdio redirected */
  2463.     debug(F101,"ttopen fdflag","",fdflag);
  2464.  
  2465.     ttmdm = modem;                      /* Make this available to other fns */
  2466.     xlocal = *lcl;                      /* Make this available to other fns */
  2467.  
  2468. /* Code for handling bidirectional tty lines goes here. */
  2469. /* Use specified method for turning off logins and suppressing getty. */
  2470.  
  2471. #ifdef ACUCNTRL
  2472.     /* Should put call to priv_on() here, but that would be very risky! */
  2473.     acucntrl("disable",ttname);         /* acucntrl() program. */
  2474.     /* and priv_off() here... */
  2475. #else
  2476. #ifdef ATT7300
  2477.     if ((attmodem & DOGETY) == 0)       /* offgetty() program. */
  2478.       attmodem |= offgetty(ttname);    /* Remember response.  */
  2479. #endif /* ATT7300 */
  2480. #endif /* ACUCNTRL */
  2481.  
  2482. #ifdef OPENFIRST
  2483. /*
  2484.  1985-2001: opens device first then gets lock; reason:
  2485.  Kermit usually has to run setuid or setgid in order to create a lockfile.
  2486.  If you give a SET LINE command for a device that happens to be your job's
  2487.  controlling terminal, Kermit doesn't have to create a lockfile, and in fact
  2488.  should not create one, and would fail if it tried to if it did not have the
  2489.  required privileges.  But you can't find out if two tty device names are
  2490.  equivalent until you have a file descriptor that you can give to ttyname().
  2491.  But this can cause a race condition between Kermit and [m]getty.  So see
  2492.  the [#]else part...
  2493. */ 
  2494.  
  2495. /*
  2496.  In the following section, we open the tty device for read/write.
  2497.  If a modem has been specified via "set modem" prior to "set line"
  2498.  then the O_NDELAY parameter is used in the open, provided this symbol
  2499.  is defined (e.g. in fcntl.h), so that the program does not hang waiting
  2500.  for carrier (which in most cases won't be present because a connection
  2501.  has not been dialed yet).  O_NDELAY is removed later on in ttopen().  It
  2502.  would make more sense to first determine if the line is local before
  2503.  doing this, but because ttyname() requires a file descriptor, we have
  2504.  to open it first.  See do_open().
  2505.  
  2506.  Now open the device using the desired treatment of carrier.
  2507.  If carrier is REQUIRED, then open could hang forever, so an optional
  2508.  timer is provided.  If carrier is not required, the timer should never
  2509.  go off, and should do no harm...
  2510. */
  2511.     ttotmo = 0;                /* Flag no timeout */
  2512.     debug(F101,"ttopen timo","",timo);
  2513.     debug(F101,"ttopen xlocal","",xlocal);
  2514.     if (timo > 0) {
  2515.     int xx;
  2516.     saval = signal(SIGALRM,timerh);    /* Timed, set up timer. */
  2517.     xx = alarm(timo);        /* Timed open() */
  2518.     debug(F101,"ttopen alarm","",xx);
  2519.     if (
  2520. #ifdef CK_POSIX_SIG
  2521.         sigsetjmp(sjbuf,1)
  2522. #else
  2523.         setjmp(sjbuf)
  2524. #endif /* CK_POSIX_SIG */
  2525.         ) {
  2526.         ttotmo = 1;            /* Flag timeout. */
  2527.     } else ttyfd = do_open(ttname);
  2528.     ttimoff();
  2529.     debug(F111,"ttopen","modem",modem);
  2530.     debug(F101,"ttopen ttyfd","",ttyfd);
  2531.     debug(F101,"ttopen alarm return","",ttotmo);
  2532.     } else {
  2533.     errno = 0;
  2534.     ttyfd = do_open(ttname);
  2535.     }
  2536.     debug(F111,"ttopen ttyfd",ttname,ttyfd);
  2537.     if (ttyfd < 0) {            /* If couldn't open, fail. */
  2538.     debug(F101,"ttopen errno","",errno);
  2539.     if (errno > 0 && !quiet)
  2540.       perror(ttname);        /* Print message */
  2541.  
  2542. #ifdef ATT7300
  2543.     if (attmodem & DOGETY)        /* was getty(1m) running before us? */
  2544.       ongetty(ttnmsv);        /* yes, restart on tty line */
  2545.     attmodem &= ~DOGETY;        /* no phone in use, getty restored */
  2546. #else
  2547. #ifdef ACUCNTRL
  2548.         /* Should put call to priv_on() here, but that would be risky! */
  2549.     acucntrl("enable",ttname);    /* acucntrl() program. */
  2550.     /* and priv_off() here... */
  2551. #endif /* ACUNTRL */
  2552. #endif /* ATT7300 */
  2553.  
  2554.     signal(SIGINT,occt);        /* Put old Ctrl-C trap back. */
  2555.     if (errno == EACCES) {        /* Device is protected against user */
  2556.         debug(F110,"ttopen EACCESS",ttname,0); /* Return -4 */
  2557.         return(-4);
  2558.     } else return(ttotmo ? -2 : -1); /* Otherwise -2 if timeout, or -1 */
  2559.     }
  2560.  
  2561. #ifdef QNX
  2562.     {
  2563.     extern int qnxportlock;
  2564.     x = qnxopencount();
  2565.     debug(F101,"ttopen qnxopencount","",x);
  2566.     debug(F101,"ttopen qnxportlock","",qnxportlock);
  2567.     if (x < 0 && qnxportlock) {
  2568.         ttclos(0);
  2569.         printf("?Can't get port open count\n");
  2570.         printf("(Try again with SET QNX-PORT-LOCK OFF)\n");
  2571.         return(-1);            /* Indicate device is in use */
  2572.     }
  2573.     if (x > 1) {            /* 1 == me */
  2574.         if (qnxportlock)
  2575.           ttclos(0);
  2576.           return(-2);        /* Indicate device is in use */
  2577.         else if (!quiet)
  2578.           printf("WARNING: \"%s\" looks busy...\n",ttdev);
  2579.     }
  2580.     }
  2581. #endif /* QNX */
  2582.  
  2583. #ifdef Plan9
  2584.     /* take this opportunity to open the control channel */
  2585.     if (p9openttyctl(ttname) < 0)
  2586. #else
  2587.     /* Make sure it's a real tty. */
  2588.     if (!isatty(ttyfd) && strcmp(ttname,"/dev/null"))
  2589. #endif /* Plan9 */
  2590.       {
  2591.     fprintf(stderr,"%s is not a terminal device\n",ttname);
  2592.     debug(F111,"ttopen not a tty",ttname,errno);
  2593.     close(ttyfd);
  2594.     ttyfd = -1;
  2595.     wasclosed = 1;
  2596.     signal(SIGINT,occt);
  2597.     return(-1);
  2598.     }
  2599.  
  2600. #ifdef aegis
  2601.     /* Apollo C runtime claims that console pads are tty devices, which
  2602.      * is reasonable, but they aren't any good for packet transfer. */
  2603.     ios_$inq_type_uid((short)ttyfd, ttyuid, st);
  2604.     if (st.all != status_$ok) {
  2605.         fprintf(stderr, "problem getting tty object type: ");
  2606.         error_$print(st);
  2607.     } else if (ttyuid != sio_$uid) { /* reject non-SIO lines */
  2608.         close(ttyfd); ttyfd = -1;
  2609.         wasclosed = 1;
  2610.         errno = ENOTTY; perror(ttname);
  2611.         signal(SIGINT,occt);
  2612.         return(-1);
  2613.     }
  2614. #endif /* aegis */
  2615.  
  2616.     sigint_ign = (occt == SIG_IGN) ? 1 : 0;
  2617.  
  2618.     ckstrncpy(ttnmsv,ttname,DEVNAMLEN);    /* Keep copy of name locally. */
  2619.  
  2620. /* Caller wants us to figure out if line is controlling tty */
  2621.  
  2622.     if (*lcl < 0) {
  2623.         if (strcmp(ttname,CTTNAM) == 0) { /* "/dev/tty" always remote */
  2624.             xlocal = 0;
  2625.         debug(F111,"ttopen ttname=CTTNAM",ttname,xlocal);
  2626.         } else if (strcmp(ttname,cttnam) == 0) {
  2627.             xlocal = 0;
  2628.         debug(F111,"ttopen ttname=cttnam",ttname,xlocal);
  2629.     } else if (cttnam[0]) {
  2630. #ifdef BEBOX_DR7
  2631.             x = ttnmsv;            /* ttyname() is broken */
  2632. #else
  2633.             x = ttyname(ttyfd);         /* Get real name of ttname. */
  2634. #endif /* BEBOX_DR7 */
  2635.         if (!x) x = "";
  2636.         if (*x)
  2637.           xlocal = ((strncmp(x,cttnam,DEVNAMLEN) == 0) ? 0 : 1);
  2638.         else
  2639.           xlocal = 1;
  2640.             debug(F111,"ttopen ttyname(ttyfd) xlocal",x,xlocal);
  2641.         }
  2642.     }
  2643.  
  2644. #ifndef NOFDZERO
  2645. /* Note, the following code was added so that Unix "idle-line" snoopers */
  2646. /* would not think Kermit was idle when it was transferring files, and */
  2647. /* maybe log people out. */
  2648.     if (xlocal == 0) {            /* Remote mode */
  2649.     if (fdflag == 0) {        /* Standard i/o is not redirected */
  2650.         debug(F100,"ttopen setting ttyfd = 0","",0);
  2651. #ifdef LYNXOS
  2652.         /* On Lynx OS, fd 0 is open for read only. */
  2653.         dup2(ttyfd,0);
  2654. #endif /* LYNXOS */
  2655.         close(ttyfd);        /* Use file descriptor 0 */
  2656.         ttyfd = 0;
  2657.     } else {            /* Standard i/o is redirected */
  2658.         debug(F101,"ttopen stdio redirected","",ttyfd);
  2659.     }
  2660.     }
  2661. #endif /* NOFDZERO */
  2662.  
  2663. /* Now check if line is locked -- if so fail, else lock for ourselves */
  2664. /* Note: After having done this, don't forget to delete the lock if you */
  2665. /* leave ttopen() with an error condition. */
  2666.  
  2667.     lkf = 0;                            /* Check lock */
  2668.     if (xlocal > 0) {
  2669.     int xx; int xpid;
  2670.         if ((xx = ttlock(ttname)) < 0) { /* Can't lock it. */
  2671.             debug(F111,"ttopen ttlock fails",ttname,xx);
  2672.         /* WARNING - This close() can hang if tty is an empty socket... */
  2673.             close(ttyfd);        /* Close the device. */
  2674.         ttyfd = -1;            /* Erase its file descriptor. */
  2675.         wasclosed = 1;
  2676.         signal(SIGINT,occt);    /* Put old SIGINT back. */
  2677.         sigint_ign = (occt == SIG_IGN) ? 1 : 0;
  2678.         if (xx == -2) {        /* If lockfile says device in use, */
  2679. #ifndef NOUUCP
  2680.         debug(F111,"ttopen reading lockfile pid",flfnam,xx);
  2681.         xpid = ttrpid(flfnam);    /* Try to read pid from lockfile */
  2682.         if (xpid > -1) {    /* If we got a pid */
  2683.                     if (!quiet)
  2684.               printf("Locked by process %d\n",xpid); /* tell them. */
  2685.             sprintf(lockpid,"%d",xpid);    /* Record it too */
  2686.             debug(F110,"ttopen lockpid",lockpid,0);
  2687.         } else if (*flfnam) {
  2688.             extern char *DIRCMD;
  2689.             char *p = NULL;
  2690.             int x;
  2691.             x = (int)strlen(flfnam) + (int)strlen(DIRCMD) + 2;
  2692.             p = malloc(x);    /* Print a directory listing. */
  2693. /*
  2694.   Note: priv_on() won't help here, because we do not pass privs along to
  2695.   to inferior processes, in this case ls.  So if the real user does not have
  2696.   directory-listing access to the lockfile directory, this will result in
  2697.   something like "not found".  That's why we try this only as a last resort.
  2698. */
  2699.             if (p) {        /* If we got the space... */
  2700.             ckmakmsg(p,x,DIRCMD," ",flfnam,NULL);
  2701.             zsyscmd(p);    /* Get listing. */
  2702.             if (p) {    /* free the space */
  2703.                 free(p);
  2704.                 p = NULL;
  2705.             }
  2706.             }
  2707.         }
  2708. #endif /* NOUUCP */
  2709.         return(-5);        /* Code for device in use */
  2710.         } else return(-3);        /* Access denied */
  2711.         } else lkf = 1;
  2712.     }
  2713. #else  /* OPENFIRST */
  2714.  
  2715. /*
  2716.   27 Oct 2001: New simpler code that gets the lock first and then opens the
  2717.   device, which eliminates the race condition.  The downside is you can no
  2718.   longer say "set line /dev/ttyp0" or whatever, where /dev/ttyp0 is your login
  2719.   terminal, without trying to create a lockfile, which fails if C-Kermit lacks
  2720.   privs, and if it succeeds, it has created a lockfile where it didn't create
  2721.   one before.
  2722. */
  2723.     xlocal = *lcl;            /* Is the device my login terminal? */
  2724.     debug(F111,"ttopen xlocal","A",xlocal);
  2725.     fnam = ttname;
  2726.     if (strcmp(ttname,CTTNAM) && netconn == 0) {
  2727.     if (zfnqfp(ttname,DEVNAMLEN+1,fullname)) {
  2728.         if ((int)strlen(fullname) > 0)
  2729.           fnam = fullname;
  2730.     }
  2731.     }
  2732.     debug(F110,"ttopen fnam",fnam,0);
  2733.     if (xlocal < 0) {
  2734.     xlocal = (strcmp(fnam,CTTNAM) != 0);
  2735.     }
  2736.     debug(F111,"ttopen xlocal","B",xlocal);
  2737.  
  2738.     lkf = 0;                            /* No lock yet */
  2739.     if (xlocal > 0) {            /* If not... */
  2740.     int xx; int xpid;
  2741.     xx = ttlock(fnam);        /* Try to lock it. */
  2742.     debug(F101,"ttopen ttlock","",xx);
  2743.         if (xx < 0) {            /* Can't lock it. */
  2744.             debug(F111,"ttopen ttlock fails",fnam,xx);
  2745.         if (xx == -2) {        /* If lockfile says device in use, */
  2746. #ifndef NOUUCP
  2747.         debug(F111,"ttopen reading lockfile pid",flfnam,xx);
  2748.         xpid = ttrpid(flfnam);    /* Try to read pid from lockfile */
  2749.         if (xpid > -1) {    /* If we got a pid */
  2750.                     if (!quiet)
  2751.               printf("Locked by process %d\n",xpid); /* tell them. */
  2752.             ckstrncpy(lockpid,ckitoa(xpid),16);
  2753.             debug(F110,"ttopen lockpid",lockpid,0);
  2754. #ifndef NOPUSH
  2755.         } else if (flfnam[0] && !nopush) {
  2756.             extern char *DIRCMD;
  2757.             char *p = NULL;
  2758.             int x;
  2759.             x = (int)strlen(flfnam) + (int)strlen(DIRCMD) + 2;
  2760.             p = malloc(x);    /* Print a directory listing. */
  2761. /*
  2762.   Note: priv_on() won't help here, because we do not pass privs along to
  2763.   to inferior processes, in this case ls.  So if the real user does not have
  2764.   directory-listing access to the lockfile directory, this will result in
  2765.   something like "not found".  That's why we try this only as a last resort.
  2766. */
  2767.             if (p) {        /* If we got the space... */
  2768.             ckmakmsg(p,x,DIRCMD," ",flfnam,NULL);
  2769.             zsyscmd(p);    /* Get listing. */
  2770.             if (p) {    /* free the space */
  2771.                 free(p);
  2772.                 p = NULL;
  2773.             }
  2774.             }
  2775. #endif /* NOPUSH */
  2776.         }
  2777. #endif /* NOUUCP */
  2778.         return(-5);        /* Code for device in use */
  2779.         } else return(-3);        /* Access denied */
  2780.         } else lkf = 1;
  2781.     }
  2782.     /* Have lock -- now it's safe to open the device */
  2783.  
  2784.     debug(F101,"ttopen lkf","",lkf);
  2785.     debug(F101,"ttopen timo","",timo);
  2786.  
  2787.     ttotmo = 0;                /* Flag no timeout */
  2788.     if (timo > 0) {
  2789.     int xx;
  2790.     saval = signal(SIGALRM,timerh);    /* Timed, set up timer. */
  2791.     xx = alarm(timo);        /* Timed open() */
  2792.     debug(F101,"ttopen alarm","",xx);
  2793.     if (
  2794. #ifdef CK_POSIX_SIG
  2795.         sigsetjmp(sjbuf,1)
  2796. #else
  2797.         setjmp(sjbuf)
  2798. #endif /* CK_POSIX_SIG */
  2799.         ) {
  2800.         ttotmo = 1;            /* Flag timeout. */
  2801.     } else {
  2802.         ttyfd = do_open(fnam);
  2803.     }
  2804.     ttimoff();
  2805.     debug(F111,"ttopen timed ttyfd",fnam,ttyfd);
  2806.     } else {
  2807.     errno = 0;
  2808.     ttyfd = do_open(fnam);
  2809.     debug(F111,"ttopen untimed ttyfd",fnam,ttyfd);
  2810.     }
  2811.     if (ttyfd < 0) {            /* If couldn't open, fail. */
  2812.     debug(F111,"ttopen errno",fnam,errno);
  2813.     debug(F111,"ttopen xlocal","C",xlocal);
  2814.     if (xlocal == 0) {
  2815.         debug(F100,"ttopen substituting 0","",0);
  2816.         ttyfd = 0;
  2817.     } else {
  2818.         if (errno > 0 && !quiet) {
  2819.             debug(F111,"ttopen perror",fnam,errno);
  2820.         perror(fnam);        /* Print message */
  2821.         }
  2822.         if (ttunlck())                  /* Release the lock file */
  2823.           fprintf(stderr,"Warning, problem releasing lock\r\n");
  2824.     }
  2825.     }
  2826.  
  2827.     if (ttyfd < 0) {            /* ttyfd is still < 0? */
  2828. #ifdef ATT7300
  2829.     if (attmodem & DOGETY)        /* was getty(1m) running before us? */
  2830.       ongetty(ttnmsv);        /* yes, restart on tty line */
  2831.     attmodem &= ~DOGETY;        /* no phone in use, getty restored */
  2832. #else
  2833. #ifdef ACUCNTRL
  2834.         /* Should put call to priv_on() here, but that would be risky! */
  2835.     acucntrl("enable",fnam);    /* acucntrl() program. */
  2836.     /* and priv_off() here... */
  2837. #endif /* ACUNTRL */
  2838. #endif /* ATT7300 */
  2839.  
  2840.     signal(SIGINT,occt);        /* Put old Ctrl-C trap back. */
  2841.     if (errno == EACCES) {        /* Device is protected against user */
  2842.         debug(F110,"ttopen EACCESS",fnam,0); /* Return -4 */
  2843.         return(-4);
  2844.     } else return(ttotmo ? -2 : -1); /* Otherwise -2 if timeout, or -1 */
  2845.     }
  2846.  
  2847. /* Make sure it's a real tty. */
  2848.  
  2849. #ifdef Plan9
  2850.     /* take this opportunity to open the control channel */
  2851.     if (p9openttyctl(fnam) < 0)       
  2852. #else
  2853.       if (!isatty(ttyfd) && strcmp(fnam,"/dev/null"))
  2854. #endif /* Plan9 */
  2855.     {
  2856.         fprintf(stderr,"%s is not a terminal device\n",fnam);
  2857.         debug(F111,"ttopen not a tty",fnam,errno);
  2858.         if (ttunlck())        /* Release the lock file */
  2859.           fprintf(stderr,"Warning, problem releasing lock\r\n");
  2860.         close(ttyfd);
  2861.         ttyfd = -1;
  2862.         wasclosed = 1;
  2863.         signal(SIGINT,occt);
  2864.         return(-1);
  2865.     }
  2866.  
  2867. #ifdef aegis
  2868.     /*
  2869.       Apollo C runtime claims that console pads are tty devices, which
  2870.       is reasonable, but they aren't any good for packet transfer.
  2871.     */
  2872.     ios_$inq_type_uid((short)ttyfd, ttyuid, st);
  2873.     if (st.all != status_$ok) {
  2874.     fprintf(stderr, "problem getting tty object type: ");
  2875.     error_$print(st);
  2876.     } else if (ttyuid != sio_$uid) {    /* Reject non-SIO lines */
  2877.     close(ttyfd); ttyfd = -1;
  2878.     wasclosed = 1;
  2879.     errno = ENOTTY; perror(fnam);
  2880.     signal(SIGINT,occt);
  2881.     return(-1);
  2882.     }
  2883. #endif /* aegis */
  2884.  
  2885.     sigint_ign = (occt == SIG_IGN) ? 1 : 0;
  2886.  
  2887.     ckstrncpy(ttnmsv,ttname,DEVNAMLEN);    /* Keep copy of name locally. */
  2888.  
  2889. /* Caller wants us to figure out if line is controlling tty */
  2890.  
  2891.     if (*lcl < 0) {
  2892.     char * s;
  2893.         if (strcmp(fnam,CTTNAM) == 0) { /* "/dev/tty" always remote */
  2894.             xlocal = 0;
  2895.         debug(F111,"ttopen fnam=CTTNAM",fnam,xlocal);
  2896.         } else if (strcmp(fnam,cttnam) == 0) {
  2897.             xlocal = 0;
  2898.         debug(F111,"ttopen fnam=cttnam",fnam,xlocal);
  2899.     } else if (cttnam[0]) {
  2900. #ifdef BEBOX_DR7
  2901.             s = ttnmsv;            /* ttyname() is broken */
  2902. #else
  2903.             s = ttyname(ttyfd);         /* Get real name of ttname. */
  2904. #endif /* BEBOX_DR7 */
  2905.         if (!s) s = "";
  2906.         if (*s)
  2907.           xlocal = ((strncmp(s,cttnam,DEVNAMLEN) == 0) ? 0 : 1);
  2908.         else
  2909.           xlocal = 1;
  2910.             debug(F111,"ttopen ttyname(ttyfd) xlocal",s,xlocal);
  2911.         }
  2912.     }
  2913.  
  2914. #ifndef NOFDZERO
  2915. /* Note, the following code was added so that Unix "idle-line" snoopers */
  2916. /* would not think Kermit was idle when it was transferring files, and */
  2917. /* maybe log people out. */
  2918.     if (xlocal == 0) {            /* Remote mode */
  2919.     if (fdflag == 0) {        /* Standard i/o is not redirected */
  2920.         debug(F100,"ttopen setting ttyfd = 0","",0);
  2921. #ifdef LYNXOS
  2922.         /* On Lynx OS, fd 0 is open for read only. */
  2923.         dup2(ttyfd,0);
  2924. #endif /* LYNXOS */
  2925.         close(ttyfd);        /* Use file descriptor 0 */
  2926.         ttyfd = 0;
  2927.     } else {            /* Standard i/o is redirected */
  2928.         debug(F101,"ttopen stdio redirected","",ttyfd);
  2929.     }
  2930.     }
  2931. #endif /* NOFDZERO */
  2932. #endif /* OPENFIRST */
  2933.  
  2934. /* Got the line, now set the desired value for local. */
  2935.  
  2936.     if (*lcl != 0) *lcl = xlocal;
  2937.  
  2938. /* Some special stuff for v7... */
  2939.  
  2940. #ifdef  V7
  2941. #ifndef MINIX
  2942.     if (kmem[TTY] < 0) {        /*  If open, then skip this.  */
  2943.     qaddr[TTY] = initrawq(ttyfd);   /* Init the queue. */
  2944.     if ((kmem[TTY] = open("/dev/kmem", 0)) < 0) {
  2945.         fprintf(stderr, "Can't read /dev/kmem in ttopen.\n");
  2946.         perror("/dev/kmem");
  2947.         exit(1);
  2948.     }
  2949.     }
  2950. #endif /* !MINIX */
  2951. #endif /* V7 */
  2952.  
  2953. /* No failure returns after this point */
  2954.  
  2955. #ifdef ultrix
  2956.     ioctl(ttyfd, TIOCMODEM, &temp);
  2957. #ifdef TIOCSINUSE
  2958.     if (xlocal && ioctl(ttyfd, TIOCSINUSE, NULL) < 0) {
  2959.     if (!quiet)
  2960.       perror(fnam);
  2961.     }
  2962. #endif /* TIOCSINUSE */
  2963. #endif /* ultrix */
  2964.  
  2965. /* Get tty device settings  */
  2966.  
  2967. #ifdef BSD44ORPOSIX            /* POSIX */
  2968.     tcgetattr(ttyfd,&ttold);
  2969.     debug(F101,"ttopen tcgetattr ttold.c_lflag","",ttold.c_lflag);
  2970.     tcgetattr(ttyfd,&ttraw);
  2971.     debug(F101,"ttopen tcgetattr ttraw.c_lflag","",ttraw.c_lflag);
  2972.     tcgetattr(ttyfd,&tttvt);
  2973.     debug(F101,"ttopen tcgetattr tttvt.c_lflag","",tttvt.c_lflag);
  2974. #else                    /* BSD, V7, and all others */
  2975. #ifdef ATTSV                /* AT&T UNIX */
  2976.     ioctl(ttyfd,TCGETA,&ttold);
  2977.     debug(F101,"ttopen ioctl TCGETA ttold.c_lflag","",ttold.c_lflag);
  2978.     ioctl(ttyfd,TCGETA,&ttraw);
  2979.     ioctl(ttyfd,TCGETA,&tttvt);
  2980. #else
  2981. #ifdef BELLV10
  2982.     ioctl(ttyfd,TIOCGETP,&ttold);
  2983.     debug(F101,"ttopen BELLV10 ttold.sg_flags","",ttold.sg_flags);
  2984.     ioctl(ttyfd,TIOCGDEV,&tdold);
  2985.     debug(F101,"ttopen BELLV10 tdold.flags","",tdold.flags);
  2986. #else
  2987.     gtty(ttyfd,&ttold);
  2988.     debug(F101,"ttopen gtty ttold.sg_flags","",ttold.sg_flags);
  2989. #endif /* BELLV10 */
  2990.  
  2991. #ifdef sony_news            /* Sony NEWS */
  2992.     if (ioctl(ttyfd,TIOCKGET,&km_ext) < 0) { /* Get console Kanji mode */
  2993.     perror("ttopen error getting Kanji mode");
  2994.     debug(F101,"ttopen error getting Kanji mode","",0);
  2995.     km_ext = -1;            /* Make sure this stays undefined. */
  2996.     }
  2997. #endif /* sony_news */
  2998.  
  2999. #ifdef TIOCGETC
  3000.     debug(F100,"ttopen TIOCGETC","",0);
  3001.     tcharf = 0;                /* In remote mode, also get */
  3002.     if (xlocal == 0) {            /* special characters */
  3003.     if (ioctl(ttyfd,TIOCGETC,&tchold) < 0) {
  3004.         debug(F100,"ttopen TIOCGETC failed","",0);
  3005.     } else {
  3006.         tcharf = 1;            /* It worked. */
  3007.         ioctl(ttyfd,TIOCGETC,&tchnoi); /* Get another copy */
  3008.         debug(F100,"ttopen TIOCGETC ok","",0);
  3009.     }
  3010.     }
  3011. #else
  3012.     debug(F100,"ttopen TIOCGETC not defined","",0);
  3013. #endif /* TIOCGETC */
  3014.  
  3015. #ifdef TIOCGLTC
  3016.     debug(F100,"ttopen TIOCGLTC","",0);
  3017.     ltcharf = 0;            /* In remote mode, also get */
  3018.     if (xlocal == 0) {            /* local special characters */
  3019.     if (ioctl(ttyfd,TIOCGLTC,<chold) < 0) {
  3020.         debug(F100,"ttopen TIOCGLTC failed","",0);
  3021.     } else {
  3022.         ltcharf = 1;        /* It worked. */
  3023.         ioctl(ttyfd,TIOCGLTC,<chnoi); /* Get another copy */
  3024.         debug(F100,"ttopen TIOCGLTC ok","",0);
  3025.     }
  3026.     }
  3027. #else
  3028.     debug(F100,"ttopen TIOCGLTC not defined","",0);
  3029. #endif /* TIOCGLTC */
  3030.  
  3031. #ifdef TIOCLGET
  3032.     debug(F100,"ttopen TIOCLGET","",0);
  3033.     lmodef = 0;
  3034.     if (ioctl(ttyfd,TIOCLGET,&lmode) < 0) {
  3035.     debug(F100,"ttopen TIOCLGET failed","",0);
  3036.     } else {
  3037.     lmodef = 1;
  3038.     debug(F100,"ttopen TIOCLGET ok","",0);
  3039.     }
  3040. #endif /* TIOCLGET */
  3041.  
  3042. #ifdef BELLV10
  3043.     ioctl(ttyfd,TIOCGETP,&ttraw);
  3044.     ioctl(ttyfd,TIOCGETP,&tttvt);
  3045. #else
  3046.     gtty(ttyfd,&ttraw);                 /* And a copy of it for packets*/
  3047.     gtty(ttyfd,&tttvt);                 /* And one for virtual tty service */
  3048. #endif /* BELLV10 */
  3049.  
  3050. #endif /* ATTSV */
  3051. #endif /* BSD44ORPOSIX */
  3052.  
  3053. /* Section for changing line discipline.  It's restored in ttres(). */
  3054.  
  3055. #ifdef AIXRS
  3056. #ifndef AIX41
  3057.     { union txname ld_name; int ld_idx = 0;
  3058.       ttld = 0;
  3059.         do {
  3060.         ld_name.tx_which = ld_idx++;
  3061.         ioctl(ttyfd, TXGETCD, &ld_name);
  3062.       if (!strncmp(ld_name.tx_name, "rts", 3))
  3063.           ttld |= 1;
  3064.         } while (*ld_name.tx_name);
  3065.         debug(F101,"AIX line discipline","",ttld);
  3066.       }
  3067. #endif /* AIX41 */
  3068. #endif /* AIXRS */
  3069.  
  3070. #ifdef BSD41
  3071. /* For 4.1BSD only, force "old" tty driver, new one botches TANDEM. */
  3072.     { int k;
  3073.       ioctl(ttyfd, TIOCGETD, &ttld);    /* Get and save line discipline */
  3074.       debug(F101,"4.1bsd line discipline","",ttld);
  3075.       k = OTTYDISC;            /* Switch to "old" discipline */
  3076.       k = ioctl(ttyfd, TIOCSETD, &k);
  3077.       debug(F101,"4.1bsd tiocsetd","",k);
  3078.     }
  3079. #endif /* BSD41 */
  3080.  
  3081. #ifdef aegis
  3082.     /* This was previously done before the last two TCGETA or gtty above,
  3083.      * in both the ATTSV and not-ATTSV case.  If it is not okay to have only
  3084.      * one copy if it here instead, give us a shout!
  3085.      */
  3086.     sio_$control((short)ttyfd, sio_$raw_nl, false, st);
  3087.     if (xlocal) {       /* ignore breaks from local line */
  3088.         sio_$control((short)ttyfd, sio_$int_enable, false, st);
  3089.         sio_$control((short)ttyfd, sio_$quit_enable, false, st);
  3090.     }
  3091. #endif /* aegis */
  3092.  
  3093. #ifdef VXVE
  3094.     ttraw.c_line = 0;                   /* STTY line 0 for VX/VE */
  3095.     tttvt.c_line = 0;                   /* STTY line 0 for VX/VE */
  3096.     ioctl(ttyfd,TCSETA,&ttraw);
  3097. #endif /* vxve */
  3098.  
  3099. /* If O_NDELAY was used during open(), then remove it now. */
  3100.  
  3101. #ifdef O_NDELAY
  3102.     debug(F100,"ttopen O_NDELAY","",0);
  3103.     if (xlocal > 0) {
  3104.       if (fcntl(ttyfd, F_GETFL, 0) & O_NDELAY) {
  3105.     debug(F100,"ttopen fcntl O_NDELAY","",0);
  3106. #ifndef aegis
  3107.     if (fcntl(ttyfd,F_SETFL, fcntl(ttyfd, F_GETFL, 0) & ~O_NDELAY) < 0) {
  3108.         debug(F100,"ttopen fcntl failure to unset O_NDELAY","",0);
  3109.         perror("Can't unset O_NDELAY");
  3110.     }
  3111. #endif /* aegis */
  3112.     /* Some systems, notably Xenix (don't know how common this is in
  3113.      * other systems), need special treatment to get rid of the O_NDELAY
  3114.      * behaviour on read() with respect to carrier presence (i.e. read()
  3115.      * returning 0 when carrier absent), even though the above fcntl()
  3116.      * is enough to make read() wait for input when carrier is present.
  3117.      * This magic, in turn, requires CLOCAL for working when the carrier
  3118.      * is absent. But if xlocal == 0, presumably you already have CLOCAL
  3119.      * or you have a carrier, otherwise you wouldn't be running this.
  3120.      */
  3121.     debug(F101,"ttopen xlocal","",xlocal);
  3122. #ifdef ATTSV
  3123. #ifdef BSD44ORPOSIX
  3124. #ifdef COMMENT                /* 12 Aug 1997 */
  3125. #ifdef __bsdi__
  3126.     if (xlocal)
  3127.       ttraw.c_cflag |= CLOCAL;
  3128. #else
  3129. #ifdef __FreeBSD__
  3130.     if (xlocal)
  3131.       ttraw.c_cflag |= CLOCAL;
  3132. #endif /* __FreeBSD__ */
  3133. #endif /* __bsdi__ */
  3134. #else /* Not COMMENT */
  3135. #ifdef CLOCAL
  3136.     if (xlocal)            /* Unset this if it's defined. */
  3137.       ttraw.c_cflag |= CLOCAL;
  3138. #endif /* CLOCAL */
  3139. #endif /* COMMENT */
  3140.     debug(F101,"ttopen BSD44ORPOSIX calling tcsetattr","",TCSADRAIN);
  3141.     if (tcsetattr(ttyfd, TCSADRAIN, &ttraw) < 0) {
  3142.         debug(F100,"ttopen POSIX tcseattr fails","",0);
  3143.         perror("tcsetattr");
  3144.     }
  3145. #else /* !BSD44ORPOSIX */
  3146.     if (xlocal) {
  3147.         ttraw.c_cflag |= CLOCAL;
  3148.         debug(F100,"ttopen calling ioctl(TCSETA)","",0);
  3149.         errno = 0;
  3150.         if (ioctl(ttyfd, TCSETA, &ttraw) < 0) {
  3151.                 debug(F101,"ttopen ioctl(TCSETA) fails","",errno);
  3152.                 perror("ioctl(TCSETA)");
  3153.             }
  3154.     }
  3155. #endif /* BSD44ORPOSIX */
  3156. #endif /* ATTSV */
  3157. #ifndef NOCOTFMC /* = NO Close(Open()) To Force Mode Change */
  3158. /* Reportedly lets uugetty grab the device in SCO UNIX 3.2 / XENIX 2.3 */
  3159.     debug(F100,"ttopen executing close/open","",0);
  3160.     close( priv_opn(fnam, O_RDWR) ); /* Magic to force change. */
  3161. #endif /* NOCOTFMC */
  3162.       }
  3163.     }
  3164. #endif /* O_NDELAY */
  3165.  
  3166. /* Instruct the system how to treat the carrier, and set a few other tty
  3167.  * parameters.
  3168.  *
  3169.  * This also undoes the temporary setting of CLOCAL that may have been done
  3170.  * for the close(open()) above (except in Xenix).  Also throw in ~ECHO, to
  3171.  * prevent the other end of the line from sitting there talking to itself,
  3172.  * producing garbage when the user performs a connect.
  3173.  *
  3174.  * SCO Xenix unfortunately seems to ignore the actual state of CLOCAL.
  3175.  * Now it thinks CLOCAL is always on. It seems the only real solution for
  3176.  * Xenix is to switch between the lower and upper case device names.
  3177.  *
  3178.  * This section may at some future time expand into setting a complete
  3179.  * collection of tty parameters, or call a function shared with ttpkt()/
  3180.  * ttvt() that does so.  On the other hand, the initial parameters are not
  3181.  * that important, since ttpkt() or ttvt() should always fix that before
  3182.  * any communication is done.  Well, we'll see...
  3183.  */
  3184.     if (xlocal) {
  3185.         curcarr = -2;
  3186.     debug(F100,"ttopen calling carrctl","",0);
  3187.     carrctl(&ttraw, ttcarr == CAR_ON);
  3188.     debug(F100,"ttopen carrctl ok","",0);
  3189.  
  3190. #ifdef COHERENT
  3191. #define SVORPOSIX
  3192. #endif /* COHERENT */
  3193.  
  3194. #ifdef SVORPOSIX
  3195.     ttraw.c_lflag &= ~ECHO;
  3196.     ttold.c_lflag &= ~ECHO;
  3197. #ifdef BSD44ORPOSIX
  3198.     y = tcsetattr(ttyfd, TCSADRAIN, &ttraw);
  3199.     debug(F101,"ttopen tcsetattr","",y);
  3200. #else
  3201.     y = ioctl(ttyfd, TCSETA, &ttraw);
  3202.     debug(F100,"ttopen ioctl","",y);
  3203. #endif /* BSD44ORPOSIX */
  3204.  
  3205. #else /* BSD, etc */
  3206.     ttraw.sg_flags &= ~ECHO;
  3207.     ttold.sg_flags &= ~ECHO;
  3208. #ifdef BELLV10
  3209.     y = ioctl(ttyfd,TIOCSETP,&ttraw);
  3210.     debug(F100,"ttopen ioctl","",y);
  3211. #else
  3212.     y = stty(ttyfd,&ttraw);
  3213.     debug(F100,"ttopen stty","",y);
  3214. #endif /* BELLV10 */
  3215. #endif /* SVORPOSIX */
  3216.  
  3217. #ifdef COHERENT
  3218. #undef SVORPOSIX
  3219. #endif /* COHERENT */
  3220.  
  3221.     /* ttflui(); */  /*  This fails for some reason.  */
  3222.     }
  3223.  
  3224.     /* Get current speed */
  3225.  
  3226. #ifndef BEBOX
  3227.     ttspeed = ttgspd();
  3228. #else
  3229.     ttspeed = 19200;
  3230. #endif /* !BEBOX */
  3231.     debug(F101,"ttopen ttspeed","",ttspeed);
  3232.  
  3233.     /* Done, make entries in debug log, restore Ctrl-C trap, and return. */
  3234.  
  3235.     debug(F101,"ttopen ttyfd","",ttyfd);
  3236.     debug(F101,"ttopen *lcl","",*lcl);
  3237.     debug(F111,"ttopen lock file",flfnam,lkf);
  3238.     signal(SIGINT,occt);
  3239.     sigint_ign = (occt == SIG_IGN) ? 1 : 0;
  3240.     gotsigs = 0;
  3241.     return(0);
  3242. }
  3243.  
  3244.  
  3245. /*  D O _ O P E N  --  Do the right kind of open() call for the tty. */
  3246.  
  3247. int
  3248. do_open(ttname) char *ttname; {
  3249.     int flags;
  3250.  
  3251. #ifdef QNX6
  3252.     /* O_NONBLOCK on /dev/tty makes open() fail */
  3253.     return(priv_opn(ttname, O_RDWR |
  3254.             (
  3255.              ((int)strcmp(ttname,"/dev/tty") == 0) ?
  3256.              0 :
  3257.              (ttcarr != CAR_ON) ? O_NONBLOCK : 0)
  3258.             )
  3259.        ); 
  3260. #else  /* !QNX6 */
  3261.  
  3262. #ifndef    O_NDELAY            /* O_NDELAY not defined */
  3263.     return(priv_opn(ttname,2));
  3264. #else                    /* O_NDELAY defined */
  3265.  
  3266. #ifdef ATT7300
  3267. /*
  3268.  Open comms line without waiting for carrier so initial call does not hang
  3269.  because state of "modem" is likely unknown at the initial call  -jrd.
  3270.  If this is needed for the getty stuff to work, and the open would not work
  3271.  without O_NDELAY when getty is still on, then this special case is ok.
  3272.  Otherwise, get rid of it. -ske
  3273. */
  3274.     return(priv_opn(ttname, O_RDWR | O_NDELAY));
  3275.  
  3276. #else    /* !ATT7300 */
  3277.  
  3278.     /* Normal case. Use O_NDELAY according to SET CARRIER. See ttscarr(). */
  3279.     flags = O_RDWR;
  3280.     debug(F101,"do_open xlocal","",xlocal);
  3281.     debug(F111,"do_open flags A",ttname,flags);
  3282.     if (xlocal && (ttcarr != CAR_ON))
  3283.       flags |= O_NDELAY;
  3284.     debug(F111,"do_open flags B",ttname,flags);
  3285.     return(priv_opn(ttname, flags));
  3286. #endif /* !ATT7300 */
  3287. #endif /* O_NDELAY */
  3288. #endif /* QNX6 */
  3289. }
  3290.  
  3291. /*  T T C L O S  --  Close the TTY, releasing any lock.  */
  3292.  
  3293. static int ttc_state = 0;        /* ttclose() state */
  3294. static char * ttc_nam[] = { "setup", "hangup", "reset", "close" };
  3295.  
  3296. int
  3297. ttclos(foo) int foo; {            /* Arg req'd for signal() prototype */
  3298.     int xx, x = 0;
  3299.     extern int exithangup;
  3300.  
  3301.     debug(F101,"ttclos ttyfd","",ttyfd);
  3302.     debug(F101,"ttclos netconn","",netconn);
  3303.     debug(F101,"ttclos xlocal","",xlocal);
  3304. #ifdef NOFDZERO
  3305.     debug(F100,"ttclos NOFDZERO","",0);
  3306. #endif /* NOFDZERO */
  3307.  
  3308. #ifdef COMMENT
  3309. #ifdef TTLEBUF
  3310.     le_init();                /* No need for any of this */
  3311. #endif /* TTLEBUF */
  3312. #endif /* COMMENT */
  3313.  
  3314.     if (ttyfd < 0)            /* Wasn't open. */
  3315.       return(0);
  3316.  
  3317.     if (ttfdflg)            /* If we inherited ttyfd from */
  3318.       return(0);            /* another process, don't close it. */
  3319.  
  3320.     tvtflg = 0;                /* (some day get rid of this...) */
  3321.     gotsigs = 0;
  3322.  
  3323. #ifdef IKSD
  3324.     if (inserver) {
  3325. #ifdef TNCODE
  3326.           tn_push();                    /* Place any waiting data into input*/
  3327.           tn_sopt(DO,TELOPT_LOGOUT);    /* Send LOGOUT option before close */
  3328.           TELOPT_UNANSWERED_DO(TELOPT_LOGOUT) = 1;
  3329.           tn_reset();                   /* The Reset Telnet Option table.  */
  3330. #endif /* TNCODE */
  3331. #ifdef CK_SSL
  3332.       if (ssl_active_flag) {
  3333.           if (ssl_debug_flag)
  3334.         BIO_printf(bio_err,"calling SSL_shutdown(ssl)\n");
  3335.           SSL_shutdown(ssl_con);
  3336.           SSL_free(ssl_con);
  3337.           ssl_con = NULL;
  3338.           ssl_active_flag = 0;
  3339.       }
  3340.       if (tls_active_flag) {
  3341.           if (ssl_debug_flag)
  3342.         BIO_printf(bio_err,"calling SSL_shutdown(tls)\n");
  3343.           SSL_shutdown(tls_con);
  3344.           SSL_free(tls_con);
  3345.           tls_con = NULL;
  3346.           tls_active_flag = 0;
  3347.       }
  3348. #endif /* CK_SSL */
  3349.     }
  3350. #endif /* IKSD */
  3351. #ifdef NETCMD
  3352.     if (ttpipe) {            /* We've been using a pipe */
  3353.     /* ttpipe = 0; */
  3354.     if (ttpid > 0) {
  3355.         int wstat;
  3356.         int statusp;
  3357.         close(fdin);        /* Close these. */
  3358.         close(fdout);
  3359.         fdin = fdout = -1;
  3360.         kill(ttpid,1);        /* Kill fork with SIGHUP */
  3361.         while (1) {
  3362.         wstat = wait(&statusp);
  3363.         if (wstat == ttpid || wstat == -1)
  3364.           break;
  3365.         pexitstat = (statusp & 0xff) ? statusp : statusp >> 8;
  3366.         }
  3367.         ttpid = 0;
  3368.     }
  3369.     netconn = 0;
  3370.     wasclosed = 1;
  3371.     ttyfd = -1;
  3372.     return(0);
  3373.     }
  3374. #endif /* NETCMD */
  3375. #ifdef NETPTY
  3376.     if (ttpty) {
  3377. #ifndef NODOPTY
  3378.         end_pty();
  3379. #endif /* NODOPTY */
  3380.         close(ttyfd);
  3381.     netconn = 0;
  3382.     wasclosed = 1;
  3383.         ttpty = 0;
  3384.         ttyfd = -1;
  3385.         return(0);
  3386.     }
  3387. #endif /* NETPTY */
  3388.  
  3389. #ifdef    NETCONN
  3390.     if (netconn) {            /* If it's a network connection. */
  3391.     debug(F100,"ttclos closing net","",0);
  3392.     netclos();            /* Let the network module close it. */
  3393.     netconn = 0;            /* No more network connection. */
  3394.     debug(F101,"ttclos ttyfd after netclos","",ttyfd); /* Should be -1 */
  3395.     return(0);
  3396.     }
  3397. #endif    /* NETCONN */
  3398.  
  3399.     if (xlocal) {            /* We're closing a SET LINE device */
  3400. #ifdef FT21                /* Fortune 2.1-specific items ... */
  3401.     ioctl(ttyfd,TIOCHPCL, NULL);
  3402. #endif /* FT21 */
  3403. #ifdef ultrix                /* Ultrix-specific items ... */
  3404. #ifdef TIOCSINUSE
  3405.     /* Unset the INUSE flag that we set in ttopen() */
  3406.     ioctl(ttyfd, TIOCSINUSE, NULL);
  3407. #endif /* TIOCSINUSE */
  3408.     ioctl(ttyfd, TIOCNMODEM, &x);
  3409. #ifdef COMMENT
  3410.     /* What was this? */
  3411.     ioctl(ttyfd, TIOCNCAR, NULL);
  3412. #endif /* COMMENT */
  3413. #endif /* ultrix */
  3414.     }
  3415.  
  3416.     /* This is to prevent us from sticking in tthang() or close(). */
  3417.  
  3418. #ifdef O_NDELAY
  3419. #ifndef aegis
  3420.     if (ttyfd > 0) {            /* But skip it on stdin. */
  3421.     debug(F100,"ttclos setting O_NDELAY","",0);
  3422.     x = fcntl(ttyfd,F_SETFL,fcntl(ttyfd,F_GETFL, 0)|O_NDELAY);
  3423. #ifdef DEBUG
  3424.     if (deblog && x == -1) {
  3425.         perror("Warning - Can't set O_NDELAY");
  3426.         debug(F101,"ttclos fcntl failure to set O_NDELAY","",x);
  3427.     }
  3428. #endif /* DEBUG */
  3429.     }
  3430. #endif /* aegis */
  3431. #endif /* O_NDELAY */
  3432.  
  3433.     x = 0;
  3434.     ttc_state = 0;
  3435.     if (xlocal
  3436. #ifdef NOFDZERO
  3437.     || ttyfd > 0
  3438. #endif /* NOFDZERO */
  3439.     ) {
  3440.     saval = signal(SIGALRM,xtimerh); /* Enable timer interrupt. */
  3441.     xx = alarm(8);            /* Allow 8 seconds. */
  3442.     debug(F101,"ttclos alarm","",xx);
  3443.     if (
  3444. #ifdef CK_POSIX_SIG
  3445.         sigsetjmp(sjbuf,1)
  3446. #else
  3447.         setjmp(sjbuf)
  3448. #endif /* CK_POSIX_SIG */
  3449.         ) {                /* Timer went off? */
  3450.         x = -1;
  3451. #ifdef DEBUG
  3452.         debug(F111,"ttclos ALARM TRAP errno",ckitoa(ttc_state),errno);
  3453.         printf("ttclos() timeout: %s\n", ttc_nam[ttc_state]);
  3454. #endif /* DEBUG */
  3455.     }
  3456.     errno = 0;
  3457.     debug(F101,"ttclos A","",ttc_state);
  3458.     if (ttc_state < 1) {
  3459.         ttc_state = 1;
  3460.         debug(F101,"ttclos exithangup","",exithangup);
  3461.         if (exithangup) {
  3462.         alarm(8);        /* Re-arm the timer */
  3463.         debug(F101,"ttclos calling tthang()","",x);
  3464.         x = tthang();        /* Hang up first, then... */
  3465.         debug(F101,"ttclos tthang()","",x);
  3466.         }
  3467.     }
  3468.     debug(F101,"ttclos B","",ttc_state);
  3469.     if (ttc_state < 2) {
  3470.         ttc_state = 2;
  3471.         debug(F101,"ttclos calling ttres()","",x);
  3472.         alarm(8);            /* Re-arm the timer */
  3473.         x = ttres();        /* Reset device modes. */
  3474.         debug(F101,"ttclos ttres()","",x);
  3475.     }
  3476.     debug(F101,"ttclos C","",ttc_state);
  3477.     if (ttc_state < 3) {
  3478.         ttc_state = 3;
  3479.         errno = 0;
  3480.         debug(F101,"ttclos calling close","",x);
  3481.         alarm(8);            /* Re-arm the timer */
  3482.         x = close(ttyfd);        /* Close the device. */
  3483.         debug(F101,"ttclos close()","",x);
  3484.         if (x > -1)
  3485.           ttc_state = 4;
  3486.     }
  3487.     debug(F101,"ttclos D","",ttc_state);
  3488.     ttimoff();            /* Turn off timer. */
  3489.     if (x < 0) {
  3490.         printf("?WARNING - close failed: %s\n",ttnmsv);
  3491. #ifdef DEBUG
  3492.         if (deblog) {
  3493.         printf("errno = %d\n", errno);
  3494.         debug(F101,"ttclos failed","",errno);
  3495.         }
  3496. #endif /* DEBUG */
  3497.     }
  3498.     /* Unlock after closing but before any getty mumbo jumbo */
  3499.  
  3500.     debug(F100,"ttclos about to call ttunlck","",0);
  3501.         if (ttunlck())                  /* Release uucp-style lock */
  3502.       fprintf(stderr,"Warning, problem releasing lock\r\n");
  3503.     }
  3504.  
  3505. /* For bidirectional lines, restore getty if it was there before. */
  3506.  
  3507. #ifdef ACUCNTRL                /* 4.3BSD acucntrl() method. */
  3508.     if (xlocal) {
  3509.     debug(F100,"ttclos ACUCNTRL","",0);
  3510.     acucntrl("enable",ttnmsv);    /* Enable getty on the device. */
  3511.     }
  3512. #else
  3513. #ifdef ATT7300                /* ATT UNIX PC (3B1, 7300) method. */
  3514.     if (xlocal) {
  3515.     debug(F100,"ttclos ATT7300 ongetty","",0);
  3516.     if (attmodem & DOGETY)        /* Was getty(1m) running before us? */
  3517.       ongetty(ttnmsv);        /* Yes, restart getty on tty line */
  3518.     attmodem &= ~DOGETY;        /* No phone in use, getty restored */
  3519.     }
  3520. #endif /* ATT7300 */
  3521. #endif /* System-dependent getty-restoring methods */
  3522.  
  3523. #ifdef sony_news
  3524.     km_ext = -1;            /* Invalidate device's Kanji-mode */
  3525. #endif /* sony_news */
  3526.  
  3527.     ttyfd = -1;                         /* Invalidate the file descriptor. */
  3528.     wasclosed = 1;
  3529.     debug(F100,"ttclos done","",0);
  3530.     return(0);
  3531. }
  3532.  
  3533. /*  T T H A N G  --  Hangup phone line or network connection.  */
  3534. /*
  3535.   Returns:
  3536.   0 if it does nothing.
  3537.   1 if it believes that it hung up successfully.
  3538.  -1 if it believes that the hangup attempt failed.
  3539. */
  3540.  
  3541. #define HUPTIME 500            /* Milliseconds for hangup */
  3542.  
  3543. #ifdef COMMENT
  3544. /* The following didn't work but TIOCSDTR does work */
  3545. #ifdef UNIXWARE
  3546. /* Define HUP_POSIX to force non-POSIX builds to use the POSIX hangup method */
  3547. #ifndef POSIX                /* Such as Unixware 1.x, 2.x */
  3548. #ifndef HUP_POSIX
  3549. #define HUP_POSIX
  3550. #endif /* HUP_POSIX */
  3551. #endif /* POSIX */
  3552. #endif /* UNIXWARE */
  3553. #endif /* COMMENT */
  3554.  
  3555. #ifndef USE_TIOCSDTR
  3556. #ifdef __NetBSD__
  3557. /* Because the POSIX method (set output speed to 0) doesn't work in NetBSD */
  3558. #ifdef TIOCSDTR
  3559. #ifdef TIOCCDTR
  3560. #define USE_TIOCSDTR
  3561. #endif /* TIOCCDTR */
  3562. #endif /* TIOCSDTR */
  3563. #endif /* __NetBSD__ */
  3564. #endif /* USE_TIOCSDTR */
  3565.  
  3566. #ifndef HUP_CLOSE_POSIX
  3567. #ifdef OU8
  3568. #define HUP_CLOSE_POSIX
  3569. #else
  3570. #ifdef CK_SCOV5
  3571. #define HUP_CLOSE_POSIX
  3572. #endif /* CK_SCOV5 */
  3573. #endif /* OU8 */
  3574. #endif /* HUP_CLOSE_POSIX */
  3575.  
  3576. #ifdef NO_HUP_CLOSE_POSIX
  3577. #ifdef HUP_CLOSE_POSIX
  3578. #undef HUP_CLOSE_POSIX
  3579. #endif /* HUP_CLOSE_POSIX */
  3580. #endif /* NO_HUP_CLOSE_POSIX */
  3581.  
  3582. int
  3583. tthang() {
  3584. #ifdef NOLOCAL
  3585.     return(0);
  3586. #else
  3587.     int x = 0;                /* Sometimes used as return code. */
  3588. #ifndef POSIX
  3589.     int z;                /* worker */
  3590. #endif /* POSIX */
  3591.  
  3592. #ifdef COHERENT
  3593. #define SVORPOSIX
  3594. #endif /* COHERENT */
  3595.  
  3596. #ifdef SVORPOSIX            /* AT&T, POSIX, HPUX declarations. */
  3597.     int spdsav;                /* for saving speed */
  3598. #ifdef HUP_POSIX
  3599.     int spdsavi;
  3600. #else
  3601. #ifdef BSD44ORPOSIX
  3602.     int spdsavi;
  3603. #endif /* BSD44ORPOSIX */
  3604. #endif /* HUP_POSIX */
  3605. #ifdef HPUX
  3606. /*
  3607.   Early versions of HP-UX omitted the mflag typedef.  If you get complaints
  3608.   about it, just change it to long (or better still, unsigned long).
  3609. */
  3610.     mflag
  3611.       dtr_down = 00000000000,
  3612.       modem_rtn,
  3613.       modem_sav;
  3614.     char modem_state[64];
  3615. #endif /* HPUX */
  3616.     int flags;                /* fcntl flags */
  3617.     unsigned short ttc_save;
  3618. #endif /* SVORPOSIX */
  3619.  
  3620.     if (ttyfd < 0) return(0);           /* Don't do this if not open  */
  3621.     if (xlocal < 1) return(0);        /* Don't do this if not local */
  3622.  
  3623. #ifdef NETCMD
  3624.     if (ttpipe)
  3625.       return((ttclos(0) < 0) ? -1 : 1);
  3626. #endif /* NETCMD */
  3627. #ifdef NETPTY
  3628.     if (ttpty)
  3629.       return((ttclos(0) < 0) ? -1 : 1);
  3630. #endif /* NETPTY */
  3631. #ifdef NETCONN
  3632.     if (netconn) {            /* Network connection. */
  3633. #ifdef TN_COMPORT
  3634.         if (istncomport()) {
  3635.             int rc = tnc_set_dtr_state(0);
  3636.             if (rc >= 0) {
  3637.                 msleep(HUPTIME);
  3638.                 rc = tnc_set_dtr_state(1);
  3639.             }
  3640.             return(rc >= 0 ? 0 : -1);
  3641.         } else
  3642. #endif /* TN_COMPORT */
  3643.       return((netclos() < 0) ? -1 : 1); /* Just close it. */
  3644.   }
  3645. #endif /* NETCONN */
  3646.  
  3647. /* From here down, we handle real tty devices. */
  3648. #ifdef HUP_POSIX
  3649. /*
  3650.   e.g. for Unixware 2, where we don't have a full POSIX build, we
  3651.   still have to use POSIX-style hangup.  Thus the duplication of this
  3652.   and the next case, the only difference being we use a local termios
  3653.   struct here, since a different model is used elsewhere.
  3654.  
  3655.   NO LONGER USED as of C-Kermit 8.0 -- it turns out that this method,
  3656.   even though it compiles and executes without error, doesn't actually
  3657.   work (i.e. DTR does not drop), whereas the TIOCSDTR method works just fine,
  3658. */
  3659.     {
  3660.     struct termios ttcur;
  3661.     int x;
  3662.     debug(F100,"tthang HUP_POSIX style","",0);
  3663.     x = tcgetattr(ttyfd, &ttcur);    /* Get current attributes */
  3664.     debug(F111,"tthang tcgetattr",ckitoa(errno),x);
  3665.     if (x < 0) return(-1);
  3666.     spdsav = cfgetospeed(&ttcur);    /* Get current speed */
  3667.     debug(F111,"tthang cfgetospeed",ckitoa(errno),spdsav);
  3668.     spdsavi = cfgetispeed(&ttcur);    /* Get current speed */
  3669.     debug(F111,"tthang cfgetispeed",ckitoa(errno),spdsavi);
  3670.     x = cfsetospeed(&ttcur,B0);    /* Replace by 0 */
  3671.     debug(F111,"tthang cfsetospeed",ckitoa(errno),x);
  3672.     if (x < 0) return(-1);
  3673.     x = cfsetispeed(&ttcur,B0);
  3674.     debug(F111,"tthang cfsetispeed",ckitoa(errno),x);
  3675.     if (x < 0) return(-1);
  3676.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3677.     debug(F111,"tthang tcsetattr B0",ckitoa(errno),x);
  3678.     if (x < 0) return(-1);
  3679.     msleep(HUPTIME);        /* Sleep 0.5 sec */
  3680.     x = cfsetospeed(&ttcur,spdsav); /* Restore prev speed */
  3681.     if (x < 0) return(-1);
  3682.     debug(F111,"tthang cfsetospeed prev",ckitoa(errno),x);
  3683.     x = cfsetispeed(&ttcur,spdsavi);
  3684.     debug(F111,"tthang cfsetispeed prev",ckitoa(errno),x);
  3685.     if (x < 0) return(-1);
  3686.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3687.     debug(F111,"tthang tcsetattr restore",ckitoa(errno),x);
  3688.     if (x < 0) return(-1);
  3689.     return(1);
  3690.     }
  3691. #else
  3692. #ifdef BSD44ORPOSIX
  3693. #ifdef QNX
  3694.     {
  3695.     int x;
  3696.     x = tcdropline(ttyfd,500);
  3697.     debug(F101,"tthang QNX tcdropline","",x);
  3698.     ttcur.c_cflag |= CLOCAL;
  3699.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3700.     debug(F101,"tthang QNX tcsetattr restore","",x);
  3701.     if (x < 0) {
  3702.         debug(F101,"tthang QNX tcsetattr restore errno","",errno);
  3703.         return(-1);
  3704.     }
  3705.     /* Fix flags - ensure O_NONBLOCK is off */
  3706.  
  3707.     errno = 0;
  3708.     debug(F101,"tthang QNX iniflags","",iniflags);
  3709.     if (fcntl(ttyfd, F_SETFL, iniflags) == -1) {
  3710.         debug(F101,"tthang QNX F_SETFL errno","",errno);
  3711.         return(-1);
  3712.     }
  3713.     return(x);
  3714.     }
  3715. #else  /* QNX */
  3716.     {
  3717.     int x;
  3718. #ifdef USE_TIOCSDTR
  3719.     debug(F100,"tthang BSD44ORPOSIX USE_TIOCSDTR","",0);
  3720.     errno = 0;
  3721.     x = ioctl(ttyfd, TIOCCDTR, NULL);
  3722.     debug(F111,"tthang BSD44ORPOSIX ioctl TIOCCDTR",ckitoa(errno),x);
  3723.     if (x < 0) return(-1);
  3724.     msleep(HUPTIME);        /* Sleep 0.5 sec */
  3725.     errno = 0;
  3726.     x = ioctl(ttyfd, TIOCSDTR, NULL);
  3727.     debug(F111,"tthang BSD44ORPOSIX ioctl TIOCSDTR",ckitoa(errno),x);
  3728.     if (x < 0) return(-1);
  3729. #else  /* USE_TIOCSDTR */
  3730.  
  3731. #ifdef HUP_CLOSE_POSIX
  3732. /*
  3733.   In OSR5 versions where TIOCSDTR is not defined (up to and including at
  3734.   least 5.0.6a) the POSIX APIs in the "#else" part below are available but
  3735.   don't work, and no other APIs are available that do work.  In this case
  3736.   we have to drop DTR by brute force: close and reopen the port.  This
  3737.   code actually works, but all the steps are crucial: setting CLOCAL, the
  3738.   O_NDELAY manipulations, etc.
  3739. */
  3740.     debug(F100,"tthang HUP_CLOSE_POSIX close/open","",0);
  3741.     debug(F101,"tthang HUP_CLOSE_POSIX O_NONBLOCK","",O_NONBLOCK);
  3742.     debug(F101,"tthang HUP_CLOSE_POSIX O_NDELAY","",O_NDELAY);
  3743.     errno = 0;
  3744.     x = tcgetattr(ttyfd, &ttcur);    /* Get current attributes */
  3745.     debug(F101,"tthang HUP_CLOSE_POSIX tcgetattr","",x);
  3746.     if (x < 0) {
  3747.         debug(F101,"tthang HUP_CLOSE_POSIX tcgetattr errno","",errno);
  3748.         return(-1);
  3749.     }
  3750.     errno = 0;
  3751.  
  3752.     x = close(ttyfd);        /* Close without releasing lock */
  3753.     if (x < 0) {
  3754.         debug(F101,"tthang HUP_CLOSE_POSIX close errno","",errno);
  3755.         return(-1);
  3756.     }
  3757.     errno = 0;
  3758.     x = msleep(500);        /* Pause half a second */
  3759.     if (x < 0) {            /* Or if that doesn't work, 1 sec */
  3760.         debug(F101,"tthang HUP_CLOSE_POSIX msleep errno","",errno);
  3761.         sleep(1);
  3762.     }
  3763.     errno = 0;
  3764.     ttyfd = priv_opn(ttnmsv, (O_RDWR|O_NDELAY)); /* Reopen the device */
  3765.     debug(F111,"tthang HUP_CLOSE_POSIX reopen",ttnmsv,ttyfd);
  3766.     if (ttyfd < 0) {
  3767.         debug(F101,"tthang HUP_CLOSE_POSIX reopen errno","",errno);
  3768.         return(-1);
  3769.     }
  3770.     debug(F101,"tthang HUP_CLOSE_POSIX re-ttopen ttyfd","",ttyfd);
  3771.  
  3772.     /* Restore previous attributes */
  3773.  
  3774.     errno = 0;
  3775.     tvtflg = 0;
  3776.     ttcur.c_cflag |= CLOCAL;
  3777.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3778.     debug(F101,"tthang HUP_CLOSE_POSIX tcsetattr restore","",x);
  3779.     if (x < 0) {
  3780.         debug(F101,"tthang HUP_CLOSE_POSIX tcsetattr restore errno",
  3781.           "",errno);
  3782.         return(-1);
  3783.     }
  3784.     /* Fix flags - ensure O_NDELAY and O_NONBLOCK are off */
  3785.  
  3786.     errno = 0;
  3787.         if ((x = fcntl(ttyfd, F_GETFL, 0)) == -1) {
  3788.         debug(F101,"tthang HUP_CLOSE_POSIX F_GETFL errno","",errno);
  3789.         return(-1);
  3790.     }
  3791.     debug(F101,"tthang HUP_CLOSE_POSIX flags","",x);
  3792.     errno = 0;
  3793.         x &= ~(O_NONBLOCK|O_NDELAY);
  3794.     debug(F101,"tthang HUP_CLOSE_POSIX flags to set","",x);
  3795.     debug(F101,"tthang HUP_CLOSE_POSIX iniflags","",iniflags);
  3796.     if (fcntl(ttyfd, F_SETFL, x) == -1) {
  3797.         debug(F101,"tthang HUP_CLOSE_POSIX F_SETFL errno","",errno);
  3798.         return(-1);
  3799.     }
  3800. #ifdef DEBUG
  3801.     if (deblog) {
  3802.         if ((x = fcntl(ttyfd, F_GETFL, 0)) > -1) {
  3803.         debug(F101,"tthang HUP_CLOSE_POSIX flags","",x);
  3804.         debug(F101,"tthang HUP_CLOSE_POSIX flags & O_NONBLOCK",
  3805.               "",x&O_NONBLOCK);
  3806.         debug(F101,"tthang HUP_CLOSE_POSIX flags & O_NDELAY",
  3807.               "",x&O_NDELAY);
  3808.         }
  3809.     }
  3810. #endif /* DEBUG */
  3811.  
  3812. #else  /* HUP_CLOSE_POSIX */
  3813.     
  3814.     /* General BSD44ORPOSIX case (Linux, BSDI, FreeBSD, etc) */
  3815.  
  3816.     debug(F100,"tthang BSD44ORPOSIX B0","",0);
  3817.     x = tcgetattr(ttyfd, &ttcur);    /* Get current attributes */
  3818.     debug(F111,"tthang BSD44ORPOSIX tcgetattr",ckitoa(errno),x);
  3819.     if (x < 0) return(-1);
  3820.     spdsav = cfgetospeed(&ttcur);    /* Get current speed */
  3821.     debug(F111,"tthang BSD44ORPOSIX cfgetospeed",ckitoa(errno),spdsav);
  3822.     spdsavi = cfgetispeed(&ttcur);    /* Get current speed */
  3823.     debug(F111,"tthang BSD44ORPOSIX cfgetispeed",ckitoa(errno),spdsavi);
  3824.     x = cfsetospeed(&ttcur,B0);    /* Replace by 0 */
  3825.     debug(F111,"tthang BSD44ORPOSIX cfsetospeed",ckitoa(errno),x);
  3826.     if (x < 0) return(-1);
  3827.     x = cfsetispeed(&ttcur,B0);
  3828.     debug(F111,"tthang BSD44ORPOSIX cfsetispeed",ckitoa(errno),x);
  3829.     if (x < 0) return(-1);
  3830.     /* This gets EINVAL on NetBSD 1.4.1 because of B0... */
  3831.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3832.     debug(F111,"tthang BSD44ORPOSIX tcsetattr B0",ckitoa(errno),x);
  3833.     if (x < 0) return(-1);
  3834.     msleep(HUPTIME);        /* Sleep 0.5 sec */
  3835.     debug(F101,"tthang BSD44ORPOSIX restore output speed","",spdsav);
  3836.     x = cfsetospeed(&ttcur,spdsav); /* Restore prev speed */
  3837.     debug(F111,"tthang BSD44ORPOSIX cfsetospeed prev",ckitoa(errno),x);
  3838.     if (x < 0) return(-1);
  3839.     debug(F101,"tthang BSD44ORPOSIX restore input speed","",spdsavi);
  3840.     x = cfsetispeed(&ttcur,spdsavi);
  3841.     debug(F111,"tthang BSD44ORPOSIX cfsetispeed prev",ckitoa(errno),x);
  3842.     if (x < 0) return(-1);
  3843.     ttcur.c_cflag |= CLOCAL;    /* Don't expect CD after hangup */
  3844.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3845.     debug(F111,"tthang BSD44ORPOSIX tcsetattr restore",ckitoa(errno),x);
  3846.     if (x < 0) return(-1);
  3847.  
  3848. #endif /* HUP_CLOSE_POSIX */
  3849. #endif /* USE_TIOCSDTR */
  3850.  
  3851.     return(1);
  3852.     }
  3853.  
  3854. #endif /* QNX */
  3855. #else /* BSD44ORPOSIX */
  3856.  
  3857. #ifdef aegis                /* Apollo Aegis */
  3858.     sio_$control((short)ttyfd, sio_$dtr, false, st);    /* DTR down */
  3859.     msleep(HUPTIME);                    /* pause */
  3860.     sio_$control((short)ttyfd, sio_$dtr, true,  st);    /* DTR up */
  3861.     return(1);
  3862. #endif /* aegis */
  3863.  
  3864. #ifdef ANYBSD                /* Any BSD version. */
  3865. #ifdef TIOCCDTR                /* Except those that don't have this */
  3866.     debug(F100,"tthang BSD style","",0);
  3867.     if (ioctl(ttyfd,TIOCCDTR,0) < 0) {    /* Clear DTR. */
  3868.     debug(F101,"tthang TIOCCDTR fails","",errno);
  3869.     return(-1);
  3870.     }
  3871.     msleep(HUPTIME);            /* For about 1/2 sec */
  3872.     errno = 0;
  3873.     x = ioctl(ttyfd,TIOCSDTR,0);    /* Restore DTR */
  3874.     if (x < 0) {
  3875.     /*
  3876.       For some reason, this tends to fail with "no such device or address"
  3877.       but the operation still works, probably because of the close/open
  3878.       later on.  So let's not scare the user unnecessarily here.
  3879.     */
  3880.     debug(F101,"tthang TIOCSDTR errno","",errno); /* Log the error */
  3881.     x = 1;                /* Pretend we succeeded */
  3882.     } else if (x == 0) x = 1;        /* Success */
  3883. #ifdef COMMENT
  3884. #ifdef FT21
  3885.     ioctl(ttyfd, TIOCSAVEMODES, 0);
  3886.     ioctl(ttyfd, TIOCHPCL, 0);
  3887.     close(ttyfd);            /* Yes, must do this twice */
  3888.     if ((ttyfd = open(ttnmsv,2)) < 0)    /* on Fortune computers... */
  3889.       return(-1);            /* (but why?) */
  3890.     else x = 1;
  3891. #endif /* FT21 */
  3892. #endif /* COMMENT */
  3893. #endif /* TIOCCDTR */
  3894.     close(do_open(ttnmsv));        /* Clear i/o error condition */
  3895.     errno = 0;
  3896. #ifdef COMMENT
  3897. /* This is definitely dangerous.  Why was it here? */
  3898.     z = ttvt(ttspeed,ttflow);        /* Restore modes. */
  3899.     debug(F101,"tthang ttvt returns","",z);
  3900.     return(z < 0 ? -1 : 1);
  3901. #else
  3902.     return(x);
  3903. #endif /* COMMENT */
  3904. #endif /* ANYBSD */
  3905.  
  3906. #ifdef ATTSV
  3907. /* AT&T UNIX section, includes HP-UX and generic AT&T System III/V... */
  3908.  
  3909. #ifdef HPUX
  3910. /* Hewlett Packard allows explicit manipulation of modem signals. */
  3911.  
  3912. #ifdef COMMENT
  3913. /* Old way... */
  3914.     debug(F100,"tthang HP-UX style","",0);
  3915.     if (ioctl(ttyfd,MCSETAF,&dtr_down) < 0)        /* lower DTR */
  3916.       return(-1);                           /* oops, can't. */
  3917.     msleep(HUPTIME);                       /* Pause half a second. */
  3918.     x = 1;                           /* Set return code */
  3919.     if (ioctl(ttyfd,MCGETA,&modem_rtn) > -1) {     /* Get line status. */
  3920.     if ((modem_rtn & MDCD) != 0)             /* Check if CD is low. */
  3921.       x = -1;                                  /* CD didn't drop, fail. */
  3922.     } else x = -1;
  3923.  
  3924.     /* Even if above calls fail, RTS & DTR should be turned back on. */
  3925.     modem_rtn = MRTS | MDTR;
  3926.     if (ioctl(ttyfd,MCSETAF,&modem_rtn) < 0) x = -1;
  3927.     return(x);
  3928. #else
  3929. /* New way, from Hellmuth Michaelis */
  3930.     debug(F100,"tthang HP-UX style, HPUXDEBUG","",0);
  3931.     if (ioctl(ttyfd,MCGETA,&modem_rtn) == -1) { /* Get current status. */
  3932.     debug(F100,"tthang HP-UX: can't get modem lines, NO HANGUP!","",0);
  3933.     return(-1);
  3934.     }
  3935.     sprintf(modem_state,"%#lx",modem_rtn);
  3936.     debug(F110,"tthang HP-UX: modem lines = ",modem_state,0);
  3937.     modem_sav = modem_rtn;        /* Save current modem signals */
  3938.     modem_rtn &= ~MDTR;            /* Turn DTR bit off */
  3939.     sprintf(modem_state,"%#lx",modem_rtn);
  3940.     debug(F110,"tthang HP-UX: DTR down = ",modem_state,0);
  3941.     if (ioctl(ttyfd,MCSETAF,&modem_rtn) < 0) { /* lower DTR */
  3942.     debug(F100,"tthang HP-UX: can't lower DTR!","",0);
  3943.     return(-1);            /* oops, can't. */
  3944.     }
  3945.     msleep(HUPTIME);            /* Pause half a second. */
  3946.     x = 1;                /* Set return code */
  3947.     if (ioctl(ttyfd,MCGETA,&modem_rtn) > -1) { /* Get line status. */
  3948.     sprintf(modem_state,"%#lx",modem_rtn);
  3949.     debug(F110,"tthang HP-UX: modem lines got = ",modem_state,0);
  3950.     if ((modem_rtn & MDCD) != 0) {    /* Check if CD is low. */
  3951.         debug(F100,"tthang HP-UX: DCD not down","",0);
  3952.         x = -1;            /* CD didn't drop, fail. */
  3953.     } else {
  3954.         debug(F100,"tthang HP-UX: DCD down","",0);
  3955.     }
  3956.     } else {
  3957.     x = -1;
  3958.     debug(F100,"tthang HP-UX: can't get DCD status !","",0);
  3959.     }
  3960.  
  3961.     /* Even if above calls fail, DTR should be turned back on. */
  3962.  
  3963.     modem_sav |= MDTR;
  3964.     if (ioctl(ttyfd,MCSETAF,&modem_sav) < 0) {
  3965.     x = -1;
  3966.     debug(F100,"tthang HP-UX: can't set saved state","",0);
  3967.     } else {
  3968.     sprintf(modem_state,"%#lx",modem_sav);
  3969.     debug(F110,"tthang HP-UX: final modem lines = ",modem_state,0);
  3970.     }
  3971.     return(x);
  3972. #endif /* COMMENT */
  3973.  
  3974. #else /* AT&T but not HP-UX */
  3975.  
  3976. /* SVID for AT&T System V R3 defines ioctl's for handling modem signals. */
  3977. /* It is not known how many, if any, systems actually implement them, */
  3978. /* so we include them here in ifdef's. */
  3979.  
  3980. /*
  3981.   Unixware has the TIOCMxxx symbols defined, but calling ioctl() with them
  3982.   gives error 22 (invalid argument).
  3983. */
  3984. #ifndef _IBMR2
  3985. /*
  3986.   No modem-signal twiddling for IBM RT PC or RS/6000.
  3987.   In AIX 3.1 and earlier, the ioctl() call is broken.
  3988.   This code could be activated for AIX 3.1 with PTF 2006 or later
  3989.   (e.g. AIX 3.2), but close/open does the job too, so why bother.
  3990. */
  3991. #ifdef TIOCMBIS                /* Bit Set */
  3992. #ifdef TIOCMBIC                /* Bit Clear */
  3993. #ifdef TIOCM_DTR            /* DTR */
  3994.  
  3995. /* Clear DTR, sleep 300 msec, turn it back on. */
  3996. /* If any of the ioctl's return failure, go on to the next section. */
  3997.  
  3998.     z = TIOCM_DTR;            /* Code for DTR. */
  3999. #ifdef COMMENT
  4000. /*
  4001.   This was the cause of the troubles with the Solaris Port Monitor.
  4002.   The problem is: RTS never comes back on.  Moral: Don't do it!
  4003.   (But why doesn't it come back on?  See the TIOCMBIS call...)
  4004. */
  4005. #ifdef TIOCM_RTS            /* Lower RTS too if symbol is known. */
  4006.     z |= TIOCM_RTS;
  4007. #endif /* TIOCM_RTS */
  4008. #endif /* COMMENT */
  4009.  
  4010.     debug(F101,"tthang TIOCM signal mask","",z);
  4011.     if (ioctl(ttyfd,TIOCMBIC,&z) > -1) {   /* Try to lower DTR. */
  4012.     debug(F100,"tthang TIOCMBIC ok","",0);
  4013.     msleep(HUPTIME);           /* Pause half a second. */
  4014.     if (ioctl(ttyfd,TIOCMBIS,&z) > -1) { /* Try to turn it back on. */
  4015.         debug(F100,"tthang TIOCMBIS ok","",0);
  4016. #ifndef CLSOPN
  4017.         return(1);            /* Success, done. */
  4018. #endif /* CLSOPN */
  4019.     } else {            /* Couldn't raise, continue. */
  4020.         debug(F101,"tthang TIOCMBIS errno","",errno);
  4021.     }
  4022.     } else {                /* Couldn't lower, continue. */
  4023.      debug(F101,"tthang TIOCMBIC errno","",errno);
  4024.     }
  4025. #endif /* TIOCM_DTR */
  4026. #endif /* TIOCMBIC */
  4027. #endif /* TIOCMBIS */
  4028. #endif /* _IBMR2 */
  4029.  
  4030. /*
  4031.   General AT&T UNIX case, not HPUX.  The following code is highly suspect.  No
  4032.   two AT&T-based systems seem to do this the same way.  The object is simply
  4033.   to turn off DTR and then turn it back on.  SVID says the universal method
  4034.   for turning off DTR is to set the speed to zero, and this does seem to do
  4035.   the trick in all cases.  But neither SVID nor any known man pages say how to
  4036.   turn DTR back on again.  Some variants, like most Xenix implementations,
  4037.   raise DTR again when the speed is restored to a nonzero value.  Others
  4038.   require the device to be closed and opened again, but this is risky because
  4039.   getty could seize the device during the instant it is closed.
  4040. */
  4041.  
  4042. /* Return code for ioctl failures... */
  4043. #ifdef ATT6300
  4044.     x = 1;                /* ATT6300 doesn't want to fail... */
  4045. #else
  4046.     x = -1;
  4047. #endif /* ATT6300 */
  4048.  
  4049.     debug(F100,"tthang get settings","",0);
  4050.     if (ioctl(ttyfd,TCGETA,&ttcur) < 0) /* Get current settings. */
  4051.       return(x);            /* Fail if this doesn't work. */
  4052.     if ((flags = fcntl(ttyfd,F_GETFL,0)) < 0) /* Get device flags. */
  4053.       return(x);
  4054.     ttc_save = ttcur.c_cflag;        /* Remember current speed. */
  4055.     spdsav = ttc_save & CBAUD;
  4056.     debug(F101,"tthang speed","",spdsav);
  4057.  
  4058. #ifdef O_NDELAY
  4059.     debug(F100,"tthang turning O_NDELAY on","",0);
  4060.     fcntl(ttyfd, F_SETFL, flags | O_NDELAY); /* Activate O_NDELAY */
  4061. #endif /* O_NDELAY */
  4062.  
  4063. #ifdef ATT7300 /* This is the way it is SUPPOSED to work */
  4064.     ttcur.c_cflag &= ~CBAUD;        /* Change the speed to zero.  */
  4065. #else
  4066. #ifdef RTAIX
  4067.     ttcur.c_cflag &= ~CBAUD;        /* Change the speed to zero.  */
  4068. #else          /* This way really works but may be dangerous */
  4069. #ifdef u3b2
  4070.     ttcur.c_cflag = ~(CBAUD|CLOCAL);    /* Special for AT&T 3B2s */
  4071.                     /* (CLOCAL must be OFF) */
  4072. #else
  4073. #ifdef SCO3R2                /* SCO UNIX 3.2 */
  4074. /*
  4075.   This is complete nonsense, but an SCO user claimed this change made
  4076.   hanging up work.  Comments from other SCO UNIX 3.2 users would be
  4077.   appreciated.
  4078. */
  4079.     ttcur.c_cflag = CBAUD|B0;
  4080. #else
  4081. #ifdef AIXRS                /* AIX on RS/6000 */
  4082. /*
  4083.   Can't set speed to zero on AIX 3.1 on RS/6000 64-port adapter,
  4084.   even though you can do it on the built-in port and the 8- and 16-port
  4085.   adapters.  (Untested on 128-port adapter.)
  4086. */
  4087.     ttcur.c_cflag = CLOCAL|HUPCL|spdsav; /* Speed 0 causes EINVAL */
  4088. #else                    /* None of the above */
  4089. /*
  4090.   Set everything, including the speed, to zero, except for the CLOCAL
  4091.   and HUPCL bits.
  4092. */
  4093.     ttcur.c_cflag = CLOCAL|HUPCL;
  4094. #endif /* AIXRS */
  4095. #endif /* SCO3R2 */
  4096. #endif /* u3b2 */
  4097. #endif /* RTAIX */
  4098. #endif /* ATT7300 */
  4099.  
  4100. #ifdef COMMENT
  4101.     /* and if none of those work, try one of these... */
  4102.     ttcur.c_cflag = 0;
  4103.     ttcur.c_cflag = CLOCAL;
  4104.     ttcur.c_cflag &= ~(CBAUD|HUPCL);
  4105.     ttcur.c_cflag &= ~(CBAUD|CREAD);
  4106.     ttcur.c_cflag &= ~(CBAUD|CREAD|HUPCL);
  4107.     /* or other combinations */
  4108. #endif /* COMMENT */
  4109.  
  4110. #ifdef TCXONC
  4111.     debug(F100,"tthang TCXONC","",0);
  4112.     if (ioctl(ttyfd, TCXONC, 1) < 0) {
  4113.     debug(F101,"tthang TCXONC failed","",errno);
  4114.     }
  4115. #endif /* TCXONC */
  4116.  
  4117. #ifdef TIOCSTART
  4118.     debug(F100,"tthang TIOCSTART","",0);
  4119.     if (ioctl(ttyfd, TIOCSTART, 0) < 0) {
  4120.     debug(F101,"tthang TIOCSTART failed","",errno);
  4121.     }
  4122. #endif /* TIOCSTART */
  4123.  
  4124.     if (ioctl(ttyfd,TCSETAF,&ttcur) < 0) { /* Fail if we can't. */
  4125.     debug(F101,"tthang TCSETAF failed","",errno);
  4126.     fcntl(ttyfd, F_SETFL, flags);    /* Restore flags */
  4127.     return(-1);            /* before returning. */
  4128.     }
  4129.     msleep(300);            /* Give modem time to notice. */
  4130.  
  4131. #ifndef NOCOTFMC
  4132.  
  4133. /* Now, even though it doesn't say this in SVID or any man page, we have */
  4134. /* to close and reopen the device.  This is not necessary for all systems, */
  4135. /* but it's impossible to predict which ones need it and which ones don't. */
  4136.  
  4137. #ifdef ATT7300
  4138. /*
  4139.   Special handling for ATT 7300 UNIX PC and 3B1, which have "phone"
  4140.   related ioctl's for their internal modems.  attmodem has getty status and
  4141.   modem-in-use bit.  Reportedly the ATT7300/3B1 PIOCDISC call is necessary,
  4142.   but also ruins the file descriptor, and no other phone(7) ioctl call can fix
  4143.   it.  Whatever it does, it seems to escape detection with PIOCGETA and TCGETA.
  4144.   The only way to undo the damage is to close the fd and then reopen it.
  4145. */
  4146.     if (attmodem & ISMODEM) {
  4147.     debug(F100,"tthang attmodem close/open","",0);
  4148.     ioctl(ttyfd,PIOCUNHOLD,&dialer); /* Return call to handset. */
  4149.     ioctl(ttyfd,PIOCDISC,&dialer);    /* Disconnect phone. */
  4150.     close(ttyfd);            /* Close and reopen the fd. */
  4151.     ttyfd = priv_opn(ttnmsv, O_RDWR | O_NDELAY);
  4152.     attmodem &= ~ISMODEM;        /* Phone no longer in use. */
  4153.     }
  4154. #else /* !ATT7300 */
  4155. /* It seems we have to close and open the device for other AT&T systems */
  4156. /* too, and this is the place to do it.  The following code does the */
  4157. /* famous close(open(...)) magic by default.  If that doesn't work for you, */
  4158. /* then try uncommenting the following statement or putting -DCLSOPN in */
  4159. /* the makefile CFLAGS. */
  4160.  
  4161. /* #define CLSOPN */
  4162.  
  4163. #ifndef SCO32 /* Not needed by, and harmful to, SCO UNIX 3.2 / Xenix 2.3 */
  4164.  
  4165. #ifdef O_NDELAY
  4166. #define OPENFLGS O_RDWR | O_NDELAY
  4167. #else
  4168. #define OPENFLGS O_RDWR
  4169. #endif
  4170.  
  4171. #ifndef CLSOPN
  4172. /* This method is used by default, i.e. unless CLSOPN is defined. */
  4173. /* It is thought to be safer because there is no window where getty */
  4174. /* can seize control of the device.  The drawback is that it might not work. */
  4175.  
  4176.     debug(F101,"tthang close(open()), OPENFLGS","",OPENFLGS);
  4177.     close(priv_opn(ttnmsv, OPENFLGS));
  4178.  
  4179. #else
  4180. /* This method is used if you #define CLSOPN.  It is more likely to work */
  4181. /* than the previous method, but it's also more dangerous. */
  4182.  
  4183.     debug(F101,"tthang close/open, OPENFLGS","",OPENFLGS);
  4184.     close(ttyfd);
  4185.     msleep(10);
  4186.     ttyfd = priv_opn(ttnmsv, OPENFLGS);    /* Open it again */
  4187. #endif /* CLSOPN */
  4188. #undef OPENFLGS
  4189.  
  4190. #endif /* SCO32 */
  4191. #endif /* ATT7300 */
  4192.  
  4193. #endif /* NOCOTFMC */
  4194.  
  4195. /* Now put all flags & modes back the way we found them. */
  4196. /* (Does the order of ioctl & fcntl matter ? ) */
  4197.  
  4198.     debug(F100,"tthang restore settings","",0);
  4199.     ttcur.c_cflag = ttc_save;        /* Get old speed back. */
  4200.     if (ioctl(ttyfd,TCSETAF,&ttcur) < 0) /* ioctl parameters. */
  4201.       return(-1);
  4202. #ifdef O_NDELAY
  4203. /*
  4204.   This is required for IBM RT and RS/6000, probably helps elsewhere too (?).
  4205.   After closing a modem line, the modem will probably not be asserting
  4206.   carrier any more, so we should not require carrier any more.  If this
  4207.   causes trouble on non-IBM UNIXes, change the #ifdef to use _IBMR2 rather
  4208.   than O_NDELAY.
  4209. */
  4210.     flags &= ~O_NDELAY;            /* Don't require carrier on reopen */
  4211. #endif /* O_NDELAY */
  4212.     if (fcntl(ttyfd,F_SETFL,flags) < 0)    /* fcntl parameters */
  4213.       return(-1);
  4214.  
  4215.     return(1);
  4216. #endif /* not HPUX */
  4217. #endif /* ATTSV */
  4218. #endif /* BSD44ORPOSIX */
  4219. #endif /* HUP_POSIX */
  4220. #endif /* NOLOCAL */
  4221. }
  4222.  
  4223. /*
  4224.   Major change in 5A(174).  We used to use LPASS8, if it was defined, to
  4225.   allow 8-bit data and Xon/Xoff flow control at the same time.  But this
  4226.   LPASS8 business seems to have been causing trouble for everybody but me!
  4227.   For example, Annex terminal servers, commonly used with Encore computers,
  4228.   do not support LPASS8 even though the Encore itself does.  Ditto for many
  4229.   other terminal servers, TELNET connections, rlogin connections, etc etc.
  4230.   Now, reportedly, even vanilla 4.3 BSD systems can't do this right on their
  4231.   serial lines, even though LPASS8 is a feature of 4.3BSD.  So let's turn it
  4232.   off for everybody.  That means we goes back to using raw mode, with no
  4233.   flow control.  Phooey.
  4234.  
  4235.   NOTE: This must be done before the first reference to LPASS8 in this file,
  4236.   and after the last #include statment.
  4237. */
  4238. #ifdef LPASS8
  4239. #undef LPASS8
  4240. #endif /* LPASS8 */
  4241.  
  4242. /*  T T R E S  --  Restore terminal to "normal" mode.  */
  4243.  
  4244. /* ske@pkmab.se: There are two choices for what this function should do.
  4245.  * (1) Restore the tty to current "normal" mode, with carrier treatment
  4246.  * according to ttcarr, to be used after every kermit command. (2) Restore
  4247.  * the tty to the state it was in before kermit opened it. These choices
  4248.  * conflict, since ttold can't hold both choices of tty parameters.  ttres()
  4249.  * is currently being called as in choice (1), but ttold basically holds
  4250.  * the initial parameters, as in (2), and the description at the beginning
  4251.  * of this file says (2).
  4252.  *
  4253.  * I don't think restoring tty parameters after all kermit commands makes
  4254.  * much of a difference.  Restoring them upon exit from kermit may be of
  4255.  * some use in some cases (when the line is not restored automatically on
  4256.  * close, by the operating system).
  4257.  *
  4258.  * I can't choose which one it should be, so I haven't changed it. It
  4259.  * probably works as it is, too. It would probably even work even with
  4260.  * ttres() entirely deleted...
  4261.  *
  4262.  * (from fdc: Actually, this function operates in remote mode too, so
  4263.  * it restores the console (command) terminal to whatever mode it was
  4264.  * in before packet operations began, so that commands work right again.)
  4265.  */
  4266. int
  4267. ttres() {                               /* Restore the tty to normal. */
  4268.     int x;
  4269.  
  4270.     if (ttyfd < 0) return(-1);          /* Not open. */
  4271.  
  4272.     if (ttfdflg) return(0);        /* Don't mess with terminal modes if */
  4273.                     /* we got ttyfd from another process */
  4274. #ifdef    NETCONN
  4275.     if (netconn) {            /* Network connection */
  4276.         tvtflg = 0;
  4277. #ifdef TCPSOCKET
  4278. #ifdef TCP_NODELAY
  4279.         {
  4280.         extern int tcp_nodelay;    /* Just put this back if necessary */
  4281.         if (ttnet == NET_TCPB) {
  4282.         if (nodelay_sav > -1) {
  4283.             no_delay(ttyfd,nodelay_sav);
  4284.             nodelay_sav = -1;
  4285.         }
  4286.         }
  4287.         }
  4288. #endif /* TCP_NODELAY */
  4289. #ifdef TN_COMPORT
  4290.         if (istncomport()) {
  4291.             int rc = -1;
  4292.             if ((rc = tnsetflow(ttflow)) < 0)
  4293.           return(rc);
  4294.             if (ttspeed <= 0) 
  4295.           ttspeed = tnc_get_baud();
  4296.             else if ((rc = tnc_set_baud(ttspeed)) < 0)
  4297.           return(rc);
  4298.             tnc_set_datasize(8);
  4299.         tnc_set_stopsize(stopbits);
  4300.  
  4301. #ifdef HWPARITY
  4302.             if (hwparity) {
  4303.                 switch (hwparity) {
  4304.           case 'e':            /* Even */
  4305.                     debug(F100,"ttres 8 bits + even parity","",0);
  4306.                     tnc_set_parity(3);
  4307.                     break;
  4308.           case 'o':            /* Odd */
  4309.                     debug(F100,"ttres 8 bits + odd parity","",0);
  4310.                     tnc_set_parity(2);
  4311.                     break;
  4312.           case 'm':            /* Mark */
  4313.                     debug(F100,"ttres 8 bits + invalid parity: mark","",0);
  4314.                     tnc_set_parity(4);
  4315.                     break;
  4316.           case 's':            /* Space */
  4317.                     debug(F100,"ttres 8 bits + invalid parity: space","",0);
  4318.                     tnc_set_parity(5);
  4319.                     break;
  4320.                 }
  4321.             } else
  4322. #endif /* HWPARITY */
  4323.         {
  4324.                 tnc_set_parity(0);              /* None */
  4325.             }
  4326.             tvtflg = 0;
  4327.             return(0);
  4328.         }
  4329. #endif /* TN_COMPORT */
  4330. #endif /* TCPSOCKET */
  4331.     return(0);
  4332.     }
  4333. #endif    /* NETCONN */
  4334. #ifdef NETCMD
  4335.     if (ttpipe) return(0);
  4336. #endif /* NETCMD */
  4337. #ifdef NETPTY
  4338.     if (ttpty) return(0);
  4339. #endif /* NETPTY */
  4340.  
  4341. /* Real terminal device, so restore its original modes */
  4342.  
  4343. #ifdef BSD44ORPOSIX            /* For POSIX like this */
  4344.     debug(F100,"ttres BSD44ORPOSIX","",0);
  4345.     x = tcsetattr(ttyfd,TCSADRAIN,&ttold);
  4346. #else                    /* For all others... */
  4347. #ifdef ATTSV                            /* For AT&T versions... */
  4348.     debug(F100,"ttres ATTSV","",0);
  4349.     x = ioctl(ttyfd,TCSETAW,&ttold);    /* Restore tty modes this way. */
  4350. #else
  4351. /* Here we restore the modes for BSD */
  4352.  
  4353. #ifdef LPASS8                /* Undo "pass8" if it were done */
  4354.     if (lmodef) {
  4355.     if (ioctl(ttyfd,TIOCLSET,&lmode) < 0)
  4356.       debug(F100,"ttres TIOCLSET failed","",0);
  4357.     else
  4358.       debug(F100,"ttres TIOCLSET ok","",0);
  4359.     }
  4360. #endif /* LPASS8 */
  4361.  
  4362. #ifdef CK_DTRCTS           /* Undo hardware flow if it were done */
  4363.     if (lmodef) {
  4364.      if (ioctl(ttyfd,TIOCLSET,&lmode) < 0)
  4365.        debug(F100,"ttres TIOCLSET failed","",0);
  4366.      else
  4367.        debug(F100,"ttres TIOCLSET ok","",0);
  4368.     }
  4369. #endif /* CK_DTRCTS */
  4370.  
  4371. #ifdef TIOCGETC                /* Put back special characters */
  4372.     if (tcharf && (xlocal == 0)) {
  4373.     if (ioctl(ttyfd,TIOCSETC,&tchold) < 0)
  4374.       debug(F100,"ttres TIOCSETC failed","",0);
  4375.     else
  4376.       debug(F100,"ttres TIOCSETC ok","",0);
  4377.     }
  4378. #endif /* TIOCGETC */
  4379.  
  4380. #ifdef TIOCGLTC                /* Put back local special characters */
  4381.     if (ltcharf && (xlocal == 0)) {
  4382.     if (ioctl(ttyfd,TIOCSLTC,<chold) < 0)
  4383.       debug(F100,"ttres TIOCSLTC failed","",0);
  4384.     else
  4385.       debug(F100,"ttres TIOCSLTC ok","",0);
  4386.     }
  4387. #endif /* TIOCGLTC */
  4388.  
  4389. #ifdef BELLV10
  4390.     debug(F100,"ttres BELLV10","",0);
  4391.     x = ioctl(ttyfd,TIOCSETP,&ttold);    /* Restore both structs */
  4392.     x = ioctl(ttyfd,TIOCSDEV,&tdold);
  4393. #else
  4394.     debug(F100,"ttres stty","",0);
  4395.     x = stty(ttyfd,&ttold);             /* Restore tty modes the old way. */
  4396. #endif /* BELLV10 */
  4397.  
  4398.     if (!xlocal)
  4399.       msleep(100);            /* This replaces sleep(1)... */
  4400.                     /* Put back sleep(1) if tty is */
  4401.                     /* messed up after close. */
  4402. #endif /* ATTSV */
  4403. #endif /* BSD44ORPOSIX */
  4404.  
  4405.     debug(F101,"ttres result","",x);
  4406. #ifndef QNX
  4407.     if (x < 0) debug(F101,"ttres errno","",errno);
  4408. #endif /* QNX */
  4409.  
  4410. #ifdef AIXRS
  4411. #ifndef AIX41
  4412.     x = ioctl(ttyfd, ttld & 1 ? TXADDCD : TXDELCD, "rts");
  4413.     debug(F101,"ttres AIX line discipline rts restore","",x);
  4414. #endif /* AIX41 */
  4415. #endif /* AIXRS */
  4416.  
  4417. #ifdef BSD41
  4418.     if (ttld > -1) {            /* Put back line discipline */
  4419.     x = ioctl(ttyfd, TIOCSETD, &ttld);
  4420.     debug(F101,"ttres BSD41 line discipline restore","",x);
  4421.     if (x < 0) debug(F101,"...ioctl errno","",errno);
  4422.     ttld = -1;
  4423.     }
  4424. #endif /* BSD41 */
  4425.  
  4426. #ifdef sony_news
  4427.     x = xlocal ? km_ext : km_con;    /* Restore Kanji mode. */
  4428.     if (x != -1) {            /* Make sure we know original modes. */
  4429.     if (ioctl(ttyfd,TIOCKSET, &x) < 0) {
  4430.         perror("ttres can't set Kanji mode");
  4431.         debug(F101,"ttres error setting Kanji mode","",x);
  4432.         return(-1);
  4433.     }
  4434.     }
  4435.     debug(F100,"ttres set Kanji mode ok","",0);
  4436. #endif /* sony_news */
  4437.  
  4438.     tvtflg = 0;                /* Invalidate terminal mode settings */
  4439.     debug(F101,"ttres return code","",x);
  4440.     return(x);
  4441. }
  4442.  
  4443. #ifndef NOUUCP
  4444.  
  4445. /*  T T C H K P I D  --  Check lockfile pid  */
  4446. /*
  4447.   Read pid from lockfile named f, check that it's still valid.
  4448.   If so, return 1.
  4449.   On failure to read pid, return 1.
  4450.   Otherwise, try to delete lockfile f and return 0 if successful, else 1.
  4451. */
  4452. static int
  4453. ttchkpid(f) char *f; {
  4454.     int pid, mypid, x;
  4455.     pid = ttrpid(f);            /* Read pid from file. */
  4456.     if (pid > -1) {            /* If we were able to read the pid.. */
  4457.     debug(F101,"ttchkpid lock pid","",pid);
  4458.     errno = 0;            /* See if process still exists. */
  4459.     mypid = (int)getpid();        /* Get my own pid. */
  4460.     debug(F101,"ttchkpid my pid","",mypid);
  4461.     if (pid == mypid) {        /* It's me! */
  4462.         x = -1;            /* So I can delete it */
  4463.         errno = ESRCH;        /* pretend it's invalid */
  4464.     } else {            /* It's not me */
  4465.         x = kill((PID_T)pid, 0);    /* See if it's a live process */
  4466.         debug(F101,"ttchkpid kill errno","",errno);
  4467.     }
  4468.     debug(F101,"ttchkpid pid test","",x);
  4469.     if (x < 0 && errno == ESRCH) { /* pid is invalid */
  4470.         debug(F111,"removing stale lock",f,pid);
  4471.         if (!backgrd)
  4472.           printf("Removing stale lock %s (pid %d terminated)\n", f, pid);
  4473.         priv_on();
  4474.         x = unlink(f);        /* Remove the lockfile. */
  4475.         priv_off();
  4476.         debug(F111,"ttchkpid unlink",f,x);
  4477.         if (x > -1)
  4478.           return(0);        /* Device is not locked after all */
  4479.         else if (!backgrd)
  4480.           perror(f);
  4481.     }
  4482.     return(1);
  4483.     }
  4484.     return(1);                /* Failure to read pid */
  4485. }
  4486.  
  4487. #ifdef HPUX
  4488.  
  4489. /* Aliases (different drivers) for HP-UX dialout devices: */
  4490.  
  4491. static char *devprefix[] = { "tty", "ttyd", "cul", "cua", "cuad", "culd", "" };
  4492. static int ttydexists = 0;
  4493.  
  4494. #endif /* HPUX */
  4495.  
  4496. /*  T T R P I D  --  Read pid from lockfile "name" */
  4497.  
  4498. static int
  4499. ttrpid(name) char *name; {
  4500.     long len;
  4501.     int x, fd, pid;
  4502.     short spid;
  4503.     char buf[32];
  4504.  
  4505.     debug(F110,"ttrpid",name,0);
  4506.     if (!name) return(-1);
  4507.     if (!*name) return(-1);
  4508.     priv_on();
  4509.     len = zchki(name);            /* Get file length */
  4510.     priv_off();
  4511.     debug(F101,"ttrpid zchki","",len);
  4512.     if (len < 0)
  4513.       return(-1);
  4514.     if (len > 31)
  4515.       return(-1);
  4516.     priv_on();
  4517.     fd = open(name,O_RDONLY);        /* Try to open lockfile. */
  4518.     priv_off();
  4519.     debug(F101,"ttrpid fd","",fd);
  4520.     if (fd <= 0)
  4521.       return(-1);
  4522. /*
  4523.   Here we try to be flexible and allow for all different binary and string
  4524.   formats at runtime, rather than a specific format for each configuration
  4525.   hardwired at compile time.
  4526. */
  4527.     pid = -1;
  4528. #ifndef COHERENT
  4529. /*
  4530.   COHERENT uses a string PID but without leading spaces or 0's, so there is
  4531.   no way to tell from the file's length whether it contains a string or binary
  4532.   pid.  So for COHERENT only, we only allow string pids.  For all others, we
  4533.   decide based on the size of the lockfile.
  4534. */
  4535.     if (len > 4) {            /* If file > 4 bytes it's a string */
  4536. #endif /* COHERENT */
  4537.     x = read(fd,buf,(int)len);
  4538.     debug(F111,"ttrpid string read",buf,x);
  4539.     if (x < 0) {
  4540.         pid = -1;
  4541.     } else {
  4542.         buf[31] = '\0';
  4543.         x = sscanf(buf,"%d",&pid);    /* Get the integer pid from it. */
  4544.     }
  4545. #ifndef COHERENT
  4546.     } else if (len == 4) {        /* 4 bytes so binary */
  4547.     x = read(fd, (char *)&pid, 4);    /* Read the bytes into an int */
  4548.     debug(F101,"ttrpid integer read","",x);
  4549.     if (x < 4)
  4550.       pid = -1;
  4551.     } else if (len == 2) {        /* 2 bytes binary */
  4552.     x = read(fd, (char *)&spid, 2);    /* Read the bytes into a short */
  4553.     debug(F101,"ttrpid short read","",x);
  4554.     if (x < 2)
  4555.       pid = -1;
  4556.     else
  4557.       pid = spid;
  4558.     } else
  4559.       pid = -1;
  4560. #endif /* COHERENT */
  4561.     close(fd);                /* Close the lockfile */
  4562.     debug(F101,"ttrpid pid","",pid);
  4563.     return(pid);
  4564. }
  4565. #endif /* NOUUCP */
  4566.  
  4567. /*  T T L O C K  */
  4568.  
  4569. /*
  4570.   This function attempts to coordinate use of the communication device with
  4571.   other copies of Kermit and any other program that follows the UUCP
  4572.   device-locking conventions, which, unfortunately, vary among different UNIX
  4573.   implementations.  The idea is to look for a file of a certain name, the
  4574.   "lockfile", in a certain directory.  If such a file is found, then the line
  4575.   is presumed to be in use, and Kermit should not use it.  If no such file is
  4576.   found, Kermit attempts to create one so that other programs will not use the
  4577.   same line at the same time.  Because the lockfile and/or the directory it's
  4578.   in might lack write permission for the person running Kermit, Kermit could
  4579.   find itself running setuid to uucp or other user that does have the
  4580.   necessary permissions.  At startup, Kermit has changed its effective uid to
  4581.   the user's real uid, and so ttlock() must switch back to the original
  4582.   effective uid in order to create the lockfile, and then back again to the
  4583.   real uid to prevent unauthorized access to other directories or files owned
  4584.   by the user the program is setuid to.
  4585.  
  4586.   Totally rewritten for C-Kermit 5A to eliminate windows of vulnerability,
  4587.   based on suggestions from Warren Tucker.  Call with pointer to name of
  4588.   tty device.  Returns:
  4589.  
  4590.    0 on success
  4591.   -1 on failure
  4592.  
  4593.   Note: Once privileges are turned on using priv_on(), it is essential that
  4594.   they are turned off again before this function returns.
  4595. */
  4596. #ifdef SVR4                /* Lockfile uses device numbers. */
  4597. /*
  4598.   Although I can't find this in writing anywhere (e.g. in SVID for SVR4),
  4599.   it is the behavior of the "reference version" of SVR4, i.e. the Intel
  4600.   port from UNIX Systems Laboratories, then called Univel UnixWare,
  4601.   then called Novell UnixWare, then called SCO Unixware, then called Caldera
  4602.   Open UNIX...  It also makes much more sense than device-name-based lockfiles
  4603.   since there can be multiple names for the same device, symlinks, etc.
  4604. */
  4605. #ifndef LFDEVNO                /* Define this for SVR4 */
  4606. #ifndef AIXRS                /* But not for RS/6000 AIX 3.2, etc. */
  4607. #ifndef BSD44                /* If anybody else needs it... */
  4608. #ifndef __386BSD__
  4609. #ifndef __FreeBSD__
  4610. #ifndef HPUX10
  4611. #ifndef IRIX51                /* SGI IRIX 5.1 or later */
  4612. #ifndef CK_SCOV5            /* SCO Open Server 5.0 */
  4613. #define LFDEVNO
  4614. #endif /* CK_SCOV5 */
  4615. #endif /* IRIX51 */
  4616. #endif /* HPUX10 */
  4617. #endif /* __FreeBSD__ */
  4618. #endif /* __386BSD__ */
  4619. #endif /* BSD44 */
  4620. #endif /* AIXRS */
  4621. #endif /* LFDEVNO */            /* ... define it here or on CC */
  4622. #endif /* SVR4 */            /* command line. */
  4623.  
  4624. #ifdef COHERENT
  4625. #define LFDEVNO
  4626. #endif /* COHERENT */
  4627.  
  4628. /*
  4629.   For platforms where the lockfile name is made from device/major/minor
  4630.   device number, as in SVR4.  Which, if we must have lockfiles at all, is
  4631.   by far the best format, since it eliminates all the confusion that stems
  4632.   from multiple names (or drivers) for the same port, not to mention
  4633.   symlinks.  It might even be a good idea to start using this form even
  4634.   on platforms where it's not supported, alongside the normal forms for those
  4635.   platforms, in order to get people used to it...
  4636. */
  4637. #ifdef LFDEVNO
  4638. #ifndef major                /* If we didn't find it */
  4639. #ifdef SVR4                /* then for Sys V R4 */
  4640. #include <sys/mkdev.h>            /* look here */
  4641. #else                    /* or for SunOS versions */
  4642. #ifdef SUNOS4                /* ... */
  4643. #include <sys/sysmacros.h>        /* look here */
  4644. #else                    /* Otherwise take a chance: */
  4645. #define    major(dev) ( (int) ( ((unsigned)(dev) >> 8) & 0xff))
  4646. #define    minor(dev) ( (int) ( (dev) & 0xff))
  4647. #endif /* SUNOS4 */
  4648. #endif /* SVR4 */
  4649. #endif /* major */
  4650. #endif /* LFDEVNO */
  4651.  
  4652. /* No advisory locks if F_TLOCK and F_ULOCK are not defined at this point */
  4653.  
  4654. #ifdef LOCKF
  4655. #ifndef F_TLOCK
  4656. #undef LOCKF
  4657. #ifndef NOLOCKF
  4658. #define NOLOCKF
  4659. #endif /* NOLOCKF */
  4660. #endif /* F_TLOCK */
  4661. #endif /* LOCKF */
  4662.  
  4663. #ifdef LOCKF
  4664. #ifndef F_ULOCK
  4665. #undef LOCKF
  4666. #ifndef NOLOCKF
  4667. #define NOLOCKF
  4668. #endif /* NOLOCKF */
  4669. #endif /* F_ULOCK */
  4670. #endif /* LOCKF */
  4671.  
  4672. static char linkto[DEVNAMLEN+1];
  4673. static char * linkdev = NULL;
  4674.  
  4675. #ifndef NOUUCP
  4676. #ifdef USETTYLOCK
  4677. #ifdef LOCK_DIR
  4678. char * uucplockdir = LOCK_DIR;
  4679. #else
  4680. char * uucplockdir = "";
  4681. #endif /* LOCK_DIR */
  4682. #else
  4683. #ifdef LOCK_DIR
  4684. char * uucplockdir = LOCK_DIR;
  4685. #else
  4686. char * uucplockdir = "";
  4687. #endif /* LOCK_DIR */
  4688. #endif /* USETTYLOCK */
  4689. #else
  4690. char * uucplockdir = "";
  4691. #endif /* NOUUCP */
  4692.  
  4693. #ifdef QNX                /* Only for QNX4 */
  4694. int                    /* Visible to outside world */
  4695. qnxopencount() {            /* Get QNX device open count */
  4696.     struct _dev_info_entry info;
  4697.     int x;
  4698.  
  4699.     x = -1;                /* Unknown */
  4700.     if (ttyfd > -1) {
  4701.     if (!dev_info(ttyfd, &info)) {
  4702.         debug(F101,"ttlock QNX open_count","",info.open_count);
  4703.         x = info.open_count;
  4704.     }
  4705.     }
  4706.     return(x);
  4707. }
  4708. #endif /* QNX */
  4709.  
  4710. char *
  4711. ttglckdir() {                /* Get Lockfile directory name */
  4712. #ifdef __OpenBSD__
  4713.     return("/var/spool/lock");
  4714. #else /* __OpenBSD__ */
  4715. #ifdef __FreeBSD__
  4716.     return("/var/spool/lock");
  4717. #else  /* __FreeBSD__ */
  4718. #ifdef LOCK_DIR
  4719.     char * s = LOCK_DIR;
  4720. #endif /* LOCK_DIR */
  4721. #ifdef NOUUCP
  4722.     return("");
  4723. #else  /* NOUUCP */
  4724. #ifdef LOCK_DIR
  4725.     return(s);
  4726. #else  /* LOCK_DIR */
  4727.     return("");
  4728. #endif /* LOCK_DIR */
  4729. #endif /* NOUUCP */
  4730. #endif /* __FreeBSD__ */
  4731. #endif /* __OpenBSD__ */
  4732. }
  4733.  
  4734. static int
  4735. ttlock(ttdev) char *ttdev; {
  4736.  
  4737.     int x, n;
  4738.     int islink = 0;
  4739.  
  4740. #ifdef NOUUCP
  4741.     debug(F100,"ttlock NOUUCP","",0);
  4742.     ckstrncpy(flfnam,"NOLOCK",FLFNAML);
  4743.     haslock = 1;
  4744.     return(0);
  4745. #else /* !NOUUCP */
  4746.  
  4747. #ifdef USETTYLOCK
  4748.     haslock = 0;                        /* Not locked yet. */
  4749.     *flfnam = '\0';            /* Lockfile name is empty. */
  4750.     if (!strncmp(ttdev,"/dev/",5) && ttdev[5])
  4751.       ckstrncpy(lockname,ttdev+5,DEVNAMLEN);
  4752.     else
  4753.       ckstrncpy(lockname,ttdev,DEVNAMLEN);
  4754. /*
  4755.   This might be overkill, but it's not clear from the man pages whether
  4756.   ttylock() can be called without calling ttylocked() first, since the doc
  4757.   says that ttylocked() removes any stale lockfiles, but it does not say this
  4758.   about ttylock().  Also the docs don't say what ttylocked() returns in the
  4759.   case when it finds and removes a stale lockfile.  So one or both calls to
  4760.   to ttylocked() might be superfluous, but they should do no harm.  Also I'm
  4761.   assuming that we have to do all the same ID swapping, etc, with these
  4762.   routines as we do without them.  Thus the priv_on/off() sandwich.
  4763. */
  4764. #ifdef USE_UU_LOCK
  4765.     priv_on();                /* Turn on privs */
  4766.     x = uu_lock(lockname);        /* Try to set the lock */
  4767.     priv_off();                /* Turn privs off */
  4768.     debug(F111,"ttlock uu_lock",lockname,x);
  4769.     switch (x) {
  4770.       case UU_LOCK_INUSE:
  4771.     return(-2);
  4772.       case UU_LOCK_OK:
  4773. #ifdef BSD44
  4774.     ckmakmsg(flfnam,FLFNAML,"/var/spool/lock/LCK..",lockname,NULL,NULL);
  4775. #endif /* BSD44 */
  4776.     haslock = 1;
  4777.     return(0);
  4778.       default:
  4779.     return(-1);
  4780.     }
  4781. #else  /* USE_UU_LOCK */
  4782.     priv_on();                /* Turn on privs */
  4783.     if (ttylocked(lockname)) {        /* This should remove any stale lock */
  4784.     if (ttylocked(lockname)) {    /* so check again. */
  4785.         priv_off();
  4786.         return(-5);            /* Still locked, fail. */
  4787.     }
  4788.     }
  4789.     x = ttylock(lockname);        /* Lock it. */
  4790.     priv_off();                /* Turn off privs */
  4791.  
  4792.     debug(F111,"ttlock lockname",lockname,x);
  4793.     if (x > -1) {
  4794.     /*
  4795.       We don't really know the name of the lockfile, but
  4796.       this is what the man page says it is.  In USETTYLOCK
  4797.           builds, it is used only for display by SHOW COMM.
  4798.     */
  4799.     ckmakmsg(flfnam,FLFNAML,"/etc/locks/LCK..",lockname,NULL,NULL);
  4800.     haslock = 1;
  4801.     }
  4802.     return(x);
  4803. #endif /* USE_UU_LOCK */
  4804. #else  /* Systems that don't have ttylock()... */
  4805.  
  4806. #ifndef HPUX
  4807.  
  4808.     int lockfd;                /* File descriptor for lock file. */
  4809.     PID_T pid;                /* Process id of this process. */
  4810.     int tries;                /* How many times we've tried... */
  4811.     struct stat devbuf;            /* For device numbers (SVR4). */
  4812.  
  4813. #ifdef PIDSTRING
  4814.     char pid_str[32];            /* My pid in string format. */
  4815. #endif /* PIDSTRING */
  4816.  
  4817.     char *device, *devname;
  4818.  
  4819. #define LFNAML 256            /* Max length for lock file name. */
  4820.     char lockfil[LFNAML];        /* Lock file name */
  4821. #ifdef RTAIX
  4822.     char lklockf[LFNAML];        /* Name for link to lock file  */
  4823. #endif /* RTAIX */
  4824. #ifdef CKSYMLINK
  4825.     char symlock[LFNAML];        /* Name for symlink lockfile name */
  4826. #endif /* CKSYMLINK */
  4827.     char tmpnam[LFNAML+30];        /* Temporary lockfile name. */
  4828.     char *lockdir = LOCK_DIR;        /* Defined near top of this file, */
  4829.                     /* or on cc command line. */
  4830.     haslock = 0;                        /* Not locked yet. */
  4831.     *flfnam = '\0';            /* Lockfile name is empty. */
  4832.     lock2[0] = '\0';            /* Clear secondary lockfile name. */
  4833.     pid = getpid();            /* Get id of this process. */
  4834.  
  4835. /*  Construct name of lockfile and temporary file */
  4836.  
  4837. /*  device  = name of tty device without the path, e.g. "ttyh8" */
  4838. /*  lockfil = name of lock file, without path, e.g. "LCK..ttyh8" */
  4839.  
  4840.     device = ((devname = xxlast(ttdev,'/')) != NULL ? devname+1 : ttdev);
  4841.  
  4842.     if (stat(ttdev,&devbuf) < 0)
  4843.       return(-1);
  4844.  
  4845. #ifdef CKSYMLINK
  4846.     islink = 1;                /* Assume it's a symlink */
  4847.     linkto[0] = '\0';            /* But we don't know to what */
  4848. #ifdef COMMENT
  4849. /*
  4850.   This is undependable.  If it worked it would save the readlink call if
  4851.   we knew the device name was not a link.
  4852. */
  4853. #ifdef S_ISLNK
  4854.     islink = S_ISLNK(devbuf.st_mode);
  4855.     debug(F101,"ttlock stat S_ISLNK","",islink);
  4856. #endif /* S_ISLNK */
  4857. #endif /* COMMENT */
  4858.     if (islink) {
  4859.     n = readlink(ttdev,linkto,DEVNAMLEN); /* See if it's a link */
  4860.     debug(F111,"ttlock readlink",ttdev,n);
  4861.     if (n > -1)            /* It is */
  4862.       linkto[n] = '\0';
  4863.     else                /* It's not */
  4864.       islink = 0;
  4865.     debug(F111,"ttlock link",linkto,islink);
  4866.     }
  4867.     if (islink) {
  4868.     linkdev = (devname = xxlast(linkto,'/')) ? devname + 1 : linkto;
  4869.     debug(F110,"ttlock linkdev",linkdev,0);
  4870.     }
  4871. #endif /* CKSYMLINK */
  4872.  
  4873. /*
  4874.   On SCO platforms, if we don't have a symlink, then let's pretend the
  4875.   name given for the device is a symlink, because later we will change
  4876.   the name if it contains any uppercase characters.
  4877. */
  4878. #ifdef CK_SCOV5                /* SCO Open Server 5.0 */
  4879.     if (!islink) {
  4880.     islink = 1;
  4881.     ckstrncpy(linkto,ttdev,DEVNAMLEN);
  4882.     linkdev = (devname = xxlast(linkto,'/')) ? devname + 1 : linkto;
  4883.     debug(F110,"ttlock linkdev",linkdev,0);
  4884.     }
  4885. #else
  4886. #ifdef M_XENIX                /* SCO Xenix or UNIX */
  4887.     if (!islink) {
  4888.     islink = 1;
  4889.     ckstrncpy(linkto,ttdev,DEVNAMLEN);
  4890.     linkdev = (devname = xxlast(linkto,'/')) ? devname + 1 : linkto;
  4891.     debug(F110,"ttlock linkdev",linkdev,0);
  4892.     }
  4893. #endif /* M_XENIX */
  4894. #endif /* CK_SCOV5 */
  4895.  
  4896. #ifdef ISIII                /* Interactive System III, PC/IX */
  4897.     ckstrncpy(lockfil, device, DEVNAMLEN);
  4898. #else  /* not ISIII */
  4899. #ifdef LFDEVNO                /* Lockfilename has device numbers. */
  4900. #ifdef COHERENT
  4901.     sprintf(lockfil,"LCK..%d.%d",    /* SAFE */
  4902.         major(devbuf.st_rdev),       /* major device number */
  4903.         0x1f & minor(devbuf.st_rdev)); /* minor device number */
  4904. #else
  4905.     /* Note: %d changed to %u in 8.0 -- %u is part of SVID for SVR4 */
  4906.     /* Lockfile name format verified to agree with Solaris cu, Dec 2001 */
  4907.     sprintf(lockfil,"LK.%03u.%03u.%03u", /* SAFE */
  4908.         major(devbuf.st_dev),    /* device */
  4909.         major(devbuf.st_rdev),    /* major device number */
  4910.         minor(devbuf.st_rdev));    /* minor device number */
  4911. #endif /* COHERENT */
  4912. #else  /* Not LFDEVNO */
  4913. #ifdef PTX                /* Dynix PTX */
  4914.     if ((device != &ttdev[5]) && (strncmp(ttdev,"/dev/",5) == 0)) {
  4915.     if ((int)strlen(device) + 8 < LFNAML)
  4916.       sprintf(lockfil,"LCK..%.3s%s", &ttdev[5], device);
  4917.     else
  4918.       ckstrncpy(lockfil,"LOCKFILE_NAME_TOO_LONG",LFNAML);
  4919.     } else
  4920. #endif /* PTX */
  4921.       if ((int)strlen(device) + 5 < LFNAML)
  4922.     sprintf(lockfil,"LCK..%s", device);
  4923.       else
  4924.     ckstrncpy(lockfil,"LOCKFILE_NAME_TOO_LONG",LFNAML);
  4925. #ifdef RTAIX
  4926.     ckstrncpy(lklockf,device,DEVNAMLEN);
  4927. #endif /* RTAIX */
  4928. #ifdef CKSYMLINK
  4929.     symlock[0] = '\0';
  4930.     if (islink)
  4931.       ckmakmsg(symlock,LFNAML, "LCK..", linkdev, NULL, NULL);
  4932. #endif /* CKSYMLINK */
  4933. #endif /* LFDEVNO */
  4934. #endif /* ISIII */
  4935.  
  4936. #ifdef CK_SCOV5                /* SCO Open Server 5.0 */
  4937.     {
  4938.     /* Lowercase the entire filename. */
  4939.         /* SCO says we must do this in V5.0 and later. */
  4940.     /* BUT... watch out for devices -- like Digiboard Portserver */
  4941.     /* That can have hundreds of ports... */
  4942.     char *p = (char *)(lockfil + 5);
  4943.     while (*p) { if (isupper(*p)) *p = (char) tolower(*p); p++; }
  4944.     }
  4945. #ifdef CKSYMLINK
  4946.     if (islink) {            /* If no change */
  4947.     if (!strcmp(lockfil,symlock)) {    /* then no second lockfile needed */
  4948.         islink = 0;
  4949.         symlock[0] = '\0';
  4950.     }
  4951.     }
  4952. #endif /* CKSYMLINK */
  4953. #else
  4954. #ifdef M_XENIX                /* SCO Xenix or UNIX */
  4955.     {
  4956.     int x; char c;
  4957.     x = (int)strlen(lockfil) - 1;    /* Get last letter of device name. */
  4958.     if (x > 0) {            /* If it's uppercase, lower it. */
  4959.         c = lockfil[x];
  4960.         if (c >= 'A' && c <= 'Z') lockfil[x] += ('a' - 'A');
  4961.     }
  4962.     }
  4963. #ifdef CKSYMLINK
  4964.     if (islink) {
  4965.     if (!strcmp(lockfil,symlock)) {    /* No change */
  4966.         islink = 0;            /* so no second lockfile */
  4967.         symlock[0] = '\0';
  4968.     }
  4969.     }
  4970. #endif /* CKSYMLINK */
  4971. #endif /* M_XENIX */
  4972. #endif /* CK_SCOV5 */
  4973.  
  4974. /*  flfnam = full lockfile pathname, e.g. "/usr/spool/uucp/LCK..ttyh8" */
  4975. /*  tmpnam = temporary unique, e.g. "/usr/spool/uucp/LTMP..pid" */
  4976.  
  4977.     ckmakmsg(flfnam,LFNAML,lockdir,"/",lockfil,NULL);
  4978.  
  4979. #ifdef RTAIX
  4980.     ckmakmsg(lkflfn,FLFNAML,lockdir,"/",lklockf,NULL);
  4981. #endif /* RTAIX */
  4982.  
  4983. #ifndef LFDEVNO
  4984. #ifdef CKSYMLINK
  4985.     /* If it's a link then also make a lockfile for the real name */
  4986.     debug(F111,"ttlock link symlock",symlock,islink);
  4987.     if (islink && symlock[0]) {
  4988.     /* But only if the lockfile names would be different. */
  4989.     /* WARNING: They won't be, e.g. for /dev/ttyd2 => /hw/ttys/ttyd2 */
  4990.     ckmakmsg(lock2,FLFNAML,lockdir,"/",symlock,NULL);
  4991.     debug(F110,"ttlock lock2",lock2,0);
  4992.     if (!strcmp(lock2,flfnam)) {    /* Are lockfile names the same? */
  4993.         debug(F100,"ttlock lock2 cleared","",0);
  4994.         lock2[0] = '\0';        /* Clear secondary lockfile name. */
  4995.     }
  4996.     }
  4997. #endif /* CKSYMLINK */
  4998. #endif /* LFDEVNO */
  4999.  
  5000.     sprintf(tmpnam,"%s/LTMP.%05d",lockdir,(int) pid); /* safe */
  5001.     debug(F110,"ttlock flfnam",flfnam,0);
  5002.     debug(F110,"ttlock tmpnam",tmpnam,0);
  5003.  
  5004.     priv_on();                /* Turn on privileges if possible. */
  5005.     lockfd = creat(tmpnam, 0444);    /* Try to create temp lock file. */
  5006.     if (lockfd < 0) {            /* Create failed. */
  5007.     debug(F111,"ttlock creat failed",tmpnam,errno);
  5008.     if (errno == ENOENT) {
  5009.         perror(lockdir);
  5010.         printf("UUCP not installed or Kermit misconfigured\n");
  5011.     } else {
  5012.         if (!quiet)
  5013.           perror(lockdir);
  5014.         unlink(tmpnam);        /* Get rid of the temporary file. */
  5015.     }
  5016.     priv_off();            /* Turn off privileges!!! */
  5017.     return(-1);            /* Return failure code. */
  5018.     }
  5019. /* Now write the pid into the temp lockfile in the appropriate format */
  5020.  
  5021. #ifdef PIDSTRING            /* For Honey DanBer UUCP, */
  5022.     sprintf(                /* write PID as decimal string */
  5023.         pid_str,
  5024. #ifdef LINUXFSSTND            /* The "Linux File System Standard" */
  5025. #ifdef FSSTND10                /* Version 1.0 calls for */
  5026.         "%010d\n",            /* leading zeros */
  5027. #else                    /* while version 1.2 calls for */
  5028.         "%10d\n",            /* leading spaces */
  5029. #endif /* FSSTND10 */
  5030. #else
  5031. #ifdef COHERENT
  5032.         "%d\n",            /* with leading nothing */
  5033. #else
  5034.         "%10d\n",            /* with leading blanks */
  5035. #endif /* COHERENT */
  5036. #endif /* LINUXFSSTND */
  5037.         (int) pid
  5038.         );                /* safe */
  5039.     write(lockfd, pid_str, 11);
  5040.     debug(F111,"ttlock hdb pid string",pid_str,(int) pid);
  5041.  
  5042. #else /* Not PIDSTRING, use integer PID */
  5043.  
  5044.     write(lockfd, (char *)&pid, sizeof(pid) );
  5045.     debug(F101,"ttlock pid","",(int) pid);
  5046.  
  5047. #endif /* PIDSTRING */
  5048.  
  5049. /* Now try to rename the temp file to the real lock file name. */
  5050. /* This will fail if a lock file of that name already exists.  */
  5051.  
  5052.     close(lockfd);            /* Close the temp lockfile. */
  5053.     chmod(tmpnam,0444);            /* Permission for a valid lock. */
  5054.     tries = 0;
  5055.     while (!haslock && tries++ < 2) {
  5056.     haslock = (link(tmpnam,flfnam) == 0); /* Create a link to it. */
  5057.     if (haslock) {                  /* If we got the lockfile */
  5058. #ifdef RTAIX
  5059.         link(flfnam,lkflfn);
  5060. #endif /* RTAIX */
  5061. #ifdef CKSYMLINK
  5062. #ifndef LFDEVNO
  5063.         if (islink && lock2[0])
  5064.           link(flfnam,lock2);
  5065. #endif /* LFDEVNO */
  5066. #endif /* CKSYMLINK */
  5067.  
  5068. #ifdef COMMENT
  5069. /* Can't do this any more because device is not open yet so no ttyfd. */
  5070. #ifdef LOCKF
  5071. /*
  5072.   Advisory file locking works on SVR4, so we use it.  In fact, it is
  5073.   necessary in some cases, e.g. when SLIP is involved.  But it still doesn't
  5074.   seem to prevent multiple users accessing the same device by different names.
  5075. */
  5076.             while (lockf(ttyfd, F_TLOCK, 0L) != 0) {
  5077.                 debug(F111, "ttlock lockf returns errno", "", errno);
  5078.                 if ((++tries >= 3) || (errno != EAGAIN)) {
  5079.                     x = unlink(flfnam); /* remove the lockfile */
  5080. #ifdef RTAIX
  5081.             unlink(lkflfn);    /* And any links to it... */
  5082. #endif /* RTAIX */
  5083. #ifdef CKSYMLINK
  5084. #ifndef LFDEVNO
  5085.             if (islink && lock2[0])
  5086.               unlink(lock2);    /* ditto... */
  5087. #endif /* LFDEVNO */
  5088. #endif /* CKSYMLINK */
  5089.                     debug(F111,"ttlock unlink",flfnam,x);
  5090.                     haslock = 0;
  5091.             break;
  5092.         }
  5093.                 sleep(2);
  5094.         }
  5095.         if (haslock)        /* If we got an advisory lock */
  5096. #endif /* LOCKF */
  5097. #endif /* COMMENT */
  5098.           break;            /* We're done. */
  5099.  
  5100.     } else {            /* We didn't create a new lockfile. */
  5101.         priv_off();
  5102.         if (ttchkpid(flfnam)) {    /* Check existing lockfile */
  5103.         priv_on();        /* cause ttchkpid turns priv_off... */
  5104.         unlink(tmpnam);        /* Delete the tempfile */
  5105.         debug(F100,"ttlock found tty locked","",0);
  5106.         priv_off();        /* Turn off privs */
  5107.         return(-2);        /* Code for device is in use. */
  5108.         }
  5109.         priv_on();
  5110.     }
  5111.     }
  5112.     unlink(tmpnam);            /* Unlink (remove) the temp file. */
  5113.     priv_off();                /* Turn off privs */
  5114.     return(haslock ? 0 : -1);        /* Return link's return code. */
  5115.  
  5116. #else /* HPUX */
  5117.  
  5118. /*
  5119.   HP-UX gets its own copy of this routine, modeled after the observed behavior
  5120.   of the HP-UX 'cu' program.  HP-UX serial device names consist of a base name
  5121.   such as "tty", "ttyd", "cua", "cul", "cuad", or "culd", followed by a unit
  5122.   designator which is a string of digits, possibly containing an imbedded
  5123.   letter "p".  Examples (for base name "tty"):
  5124.  
  5125.      /dev/tty0, /dev/tty00, dev/ttyd00, /dev/tty0p0
  5126.  
  5127.   According to the HP-UX UUCP manual of 1988, the "0p0" notation has been
  5128.   used on Series 800 since HP-UX 2.00, and the "non-p" notation was used
  5129.   on other models.  In HP-UX 10.00, "0p0" notation was adopted for all models.
  5130.   However, we make and enforce no such distinctions; either notation is
  5131.   accepted on any model or HP-UX version as a valid unit designator.
  5132.  
  5133.   If a valid unit is specified (as opposed to a designer name or symlink), we
  5134.   check for all aliases of the given unit according to the devprefix[] array.
  5135.   If no lockfiles are found for the given unit, we can have the device; we
  5136.   create a lockfile LCK..name in the lockfile directory appropriate for the
  5137.   HP-UX version (/var/spool/locks for 10.00 and later, /usr/spool/uucp for
  5138.   9.xx and earlier).  If it is a "cua" or "cul" device, a second lockfile is
  5139.   created with the "ttyd" prefix.  This is exactly what cu does.
  5140.  
  5141.   If the "set line" device does not have a valid unit designator, then it is
  5142.   used literally and no synomyms are searched for and only one lockfile is
  5143.   created.
  5144.  
  5145.   -fdc, March 1998.
  5146. */
  5147. #define LFNAML 80            /* Max length for lock file name. */
  5148.  
  5149.     int lockfd;                /* File descriptor for lock file. */
  5150.     PID_T pid;                /* Process ID of this process. */
  5151.     int fpid;                /* pid found in existing lockfile. */
  5152.     int tries;                /* How many times we've tried... */
  5153.     int i, k;                /* Workers */
  5154.  
  5155.     char *device, *devname;        /* "/dev/xxx", "xxx" */
  5156.     char *unit, *p;            /* <instance>p<port> part of xxx */
  5157.  
  5158.     char lockfil[LFNAML];        /* Lockfile name (no path) */
  5159.     char tmpnam[LFNAML];        /* Temporary lockfile name. */
  5160.  
  5161. #ifdef HPUX10                /* Lockfile directory */
  5162.     char *lockdir = "/var/spool/locks";    /* Always this for 10.00 and higher */
  5163. #else  /* HP-UX 9.xx and below */
  5164. #ifdef LOCK_DIR
  5165.     char *lockdir = LOCK_DIR;        /* Defined near top of this file */
  5166. #else
  5167.     char *lockdir = "/usr/spool/uucp";    /* or not... */
  5168. #endif /* LOCK_DIR */
  5169. #endif /* HPUX10 */
  5170.  
  5171.     haslock = 0;                        /* Not locked yet. */
  5172.     *flfnam = '\0';            /* Lockfile name is empty. */
  5173.     lock2[0] = '\0';            /* Second one too. */
  5174.     pid = getpid();            /* Get my process ID */
  5175. /*
  5176.   Construct name of lockfile and temporary file...
  5177.   device  = name of tty device without the path, e.g. "tty0p0"
  5178.   lockfil = name of lock file, without path, e.g. "LCK..tty0p0"
  5179. */
  5180.     device = ((devname = xxlast(ttdev,'/')) != NULL ? devname+1 : ttdev);
  5181.     debug(F110,"TTLOCK device",device,0);
  5182.     ckmakmsg(lockfil,LFNAML,"LCK..",device,NULL,NULL);
  5183.  
  5184.     k = 0;                /* Assume device is not locked */
  5185.     n = 0;                /* Digit counter */
  5186.     unit = device;            /* Unit = <instance>p<port> */
  5187.     while (*unit && !isdigit(*unit))    /* Search for digit... */
  5188.       unit++;
  5189.     p = unit;                /* Verify <num>p<num> format... */
  5190.     debug(F110,"TTLOCK unit 1",unit,0);
  5191. /*
  5192.   The unit number is recognized as:
  5193.   (a) any sequence of digits that runs to the end of the string.
  5194.   (b) any (a) that includes one and only one letter "p", with at least
  5195.       one digit before and after it.
  5196. */
  5197.     while (isdigit(*p)) p++, n++;    /* Get a run of digits */
  5198.     if (*p && n > 0) {            /* Have a "p"? */
  5199.     if (*p == 'p' && isdigit(*(p+1))) {
  5200.         p++;
  5201.         n = 0;
  5202.         while (isdigit(*p)) p++, n++;
  5203.     }
  5204.     }
  5205.     if (n == 0 || *p) unit = "";
  5206.     debug(F110,"TTLOCK unit 2",unit,0);
  5207.  
  5208.     if (*unit) {            /* Device name has unit number. */
  5209.     /* The following loop not only searches for the various lockfile    */
  5210.     /* synonyms, but also removes all -- not just one -- stale lockfile */
  5211.     /* for the device, should there be more than one.  See ttchkpid().  */
  5212.     ttydexists = 0;
  5213.     for (i = 0; *devprefix[i]; i++) { /* For each driver... */
  5214.         /* Make device name */
  5215.         ckmakmsg(lock2,FLFNAML,"/dev/",devprefix[i],unit,NULL);
  5216.         priv_on();            /* Privs on */
  5217.         k = zchki(lock2) != -1;    /* See if device exists */
  5218.         priv_off();            /* Privs off */
  5219.         debug(F111,"TTLOCK exist",lock2,k);
  5220.             if (k) {
  5221.         if (!strcmp(devprefix[i],"ttyd")) /* ttyd device exists */
  5222.           ttydexists = 1;
  5223.         /* Make lockfile name */
  5224.         ckmakmsg(lock2,FLFNAML,lockdir,"/LCK..",devprefix[i],unit);
  5225.         debug(F110,"TTLOCK checking",lock2,0);
  5226.         priv_on();        /* Privs on */
  5227.         k = zchki(lock2) != -1;    /* See if lockfile exists */
  5228.         priv_off();        /* Privs off */
  5229.         debug(F111,"TTLOCK check for lock A",lock2,k);
  5230.         if (k) if (ttchkpid(lock2)) { /* If pid still active, fail. */
  5231.             ckstrncpy(flfnam,lock2,FLFNAML);
  5232.             return(-2);
  5233.         }
  5234.         }
  5235.     }
  5236.     } else {                /* Some other device-name format */
  5237.     /* This takes care of symbolic links, etc... */
  5238.     /* But does not chase them down! */
  5239.     ckmakmsg(lock2,FLFNAML,lockdir,"/LCK..",device,NULL);
  5240.     priv_on();
  5241.     k = zchki(lock2) != -1;        /* Check for existing lockfile */
  5242.     priv_off();
  5243.     debug(F111,"TTLOCK check for lock B",lock2,k);
  5244.     if (k) if (ttchkpid(lock2)) {    /* Check pid from lockfile */
  5245.         ckstrncpy(flfnam,lock2,FLFNAML);
  5246.         debug(F110,"TTLOCK in use",device,0);
  5247.         debug(F101,"TTLOCK returns","",-2);
  5248.         return(-2);
  5249.     }
  5250.     }
  5251. /*
  5252.   Get here only if there is no (more) lockfile, so now we make one (or two)...
  5253.   flfnam = full lockfile pathname, e.g. "/usr/spool/uucp/LCK..cul0p0".
  5254.   tmpnam = unique temporary filname, e.g. "/usr/spool/uucp/LTMP..pid".
  5255. */
  5256.     ckmakmsg(flfnam,FLFNAML,lockdir,"/",lockfil,NULL); /* SET LINE device */
  5257.  
  5258.     /* If dialout device, also make one for corresponding dialin device */
  5259.     lock2[0] = '\0';
  5260.     if (!strncmp(device,"cu",2) && *unit && ttydexists)
  5261.       ckmakmsg(lock2,FLFNAML,lockdir,"/LCK..ttyd",unit,NULL);
  5262.  
  5263.     if ((int)strlen(lockdir)+12 < LFNAML)
  5264.       sprintf(tmpnam,"%s/LTMP.%05d",lockdir,(int) pid); /* Make temp name */
  5265. #ifdef DEBUG
  5266.     if (deblog) {
  5267.     debug(F110,"TTLOCK flfnam",flfnam,0);
  5268.     debug(F110,"TTLOCK lock2",lock2,0);
  5269.     debug(F110,"TTLOCK tmpnam",tmpnam,0);
  5270.     }
  5271. #endif /* DEBUG */
  5272. /*
  5273.    Lockfile permissions...
  5274.    444 is standard, HP-UX 10.00 uses 664.  It doesn't matter.
  5275.    Kermit uses 444; the difference lets us tell whether Kermit created
  5276.    the lock file.
  5277. */
  5278.     priv_on();                /* Turn on privileges. */
  5279.     lockfd = creat(tmpnam, 0444);    /* Try to create temporary file. */
  5280.     if (lockfd < 0) {            /* Create failed. */
  5281.     debug(F111,"TTLOCK creat failed",tmpnam,errno);
  5282.     if (errno == ENOENT) {
  5283.         perror(lockdir);
  5284.         printf("UUCP not installed or Kermit misconfigured\n");
  5285.     } else {
  5286.         if (!quiet)
  5287.           perror(lockdir);
  5288.         unlink(tmpnam);        /* Get rid of the temporary file. */
  5289.     }
  5290.     priv_off();            /* Turn off privileges!!! */
  5291.     debug(F101,"TTLOCK returns","",-1);
  5292.     return(-1);            /* Return failure code. */
  5293.     }
  5294.     debug(F110,"TTLOCK temp ok",tmpnam,0);
  5295.  
  5296. /* Now write our pid into the temp lockfile in integer format. */
  5297.  
  5298.     i = write(lockfd, (char *)&pid, sizeof(pid));
  5299.  
  5300. #ifdef DEBUG
  5301.     if (deblog) {
  5302.     debug(F101,"TTLOCK pid","",pid);
  5303.     debug(F101,"TTLOCK sizeof pid","",sizeof(pid));
  5304.     debug(F101,"TTLOCK write pid returns","",i);
  5305.     }
  5306. #endif /* DEBUG */
  5307.  
  5308. /*
  5309.   Now try to rename the temporary file to the real lockfile name.
  5310.   This will fail if a lock file of that name already exists, which
  5311.   will catch race conditions with other users.
  5312. */
  5313.     close(lockfd);            /* Close the temp lockfile. */
  5314.     chmod(tmpnam,0444);
  5315.  
  5316.     tries = 0;
  5317.     while (!haslock && tries++ < 2) {
  5318.     haslock = (link(tmpnam,flfnam) == 0); /* Create a link to it. */
  5319.     debug(F101,"TTLOCK link","",haslock);
  5320.     if (haslock) {            /* If we made the lockfile... */
  5321.  
  5322. #ifdef COMMENT
  5323. /* We can't do this any more because we don't have a file descriptor yet. */
  5324. #ifdef LOCKF                /* Can be canceled with -DNOLOCKF */
  5325. /*
  5326.   Create an advisory lock on the device through its file descriptor.
  5327.   This code actually seems to work.  If it is executed, and then another
  5328.   process tries to open the same device under a different name to circumvent
  5329.   the lockfile, they get a "device busy" error.
  5330. */
  5331.         debug(F100,"TTLOCK LOCKF code...","",0);
  5332.             while ( lockf(ttyfd, F_TLOCK, 0L) != 0 ) {
  5333.                 debug(F111, "TTLOCK lockf error", "", errno);
  5334.                 if ((++tries >= 3) || (errno != EAGAIN)) {
  5335.                     x = unlink(flfnam); /* Remove the lockfile */
  5336.             if (errno == EACCES && !quiet)
  5337.               printf("Device already locked by another process\n");
  5338.                     haslock = 0;
  5339.             break;
  5340.         }
  5341.                 sleep(2);
  5342.         }
  5343. #endif /* LOCKF */
  5344. #endif /* COMMENT */
  5345.  
  5346.         if (haslock) {        /* If we made the lockfile ... */
  5347.         if (lock2[0]) {        /* if there is to be a 2nd lockfile */
  5348.             lockfd = creat(lock2, 0444); /* Create it */
  5349.             debug(F111,"TTLOCK lock2 creat", lock2, lockfd);
  5350.             if (lockfd > -1) {    /* Created OK, write pid. */
  5351.             write(lockfd, (char *)&pid, sizeof(pid) );
  5352.             close(lockfd);    /* Close and */
  5353.             chmod(lock2, 0444); /* set permissions. */
  5354.             } else {         /* Not OK, but don't fail. */
  5355.             lock2[0] = '\0'; /* Just remember it's not there. */
  5356.             }
  5357.         }
  5358.         break;            /* and we're done. */
  5359.         }
  5360.     }
  5361.     }
  5362.     unlink(tmpnam);            /* Unlink (remove) the temp file. */
  5363.     priv_off();                /* Turn off privs */
  5364.     i = haslock ? 0 : -1;        /* Our return value */
  5365.     debug(F101,"TTLOCK returns","",i);
  5366.     return(i);
  5367. #endif /* HPUX */
  5368. #endif /* USETTYLOCK */
  5369. #endif /* !NOUUCP */
  5370. }
  5371.  
  5372. /*  T T U N L O C K  */
  5373.  
  5374. static int
  5375. ttunlck() {                             /* Remove UUCP lockfile(s). */
  5376. #ifndef NOUUCP
  5377.     int x;
  5378.  
  5379.     debug(F111,"ttunlck",flfnam,haslock);
  5380.  
  5381. #ifdef USETTYLOCK
  5382.  
  5383.     if (haslock && *flfnam) {
  5384.     int x;
  5385.     priv_on();            /* Turn on privs */
  5386. #ifdef USE_UU_LOCK
  5387.     x = uu_unlock(lockname);
  5388. #else  /* USE_UU_LOCK */
  5389.     x = ttyunlock(lockname);    /* Try to unlock */
  5390. #endif /* USE_UU_LOCK */
  5391.     priv_off();            /* Turn off privs */
  5392.     if (x < 0 && !quiet)
  5393.       printf("Warning - Can't remove lockfile: %s\n", flfnam);
  5394.  
  5395.     *flfnam = '\0';            /* Erase the name. */
  5396.     haslock = 0;
  5397.     return(0);
  5398.     }
  5399.  
  5400. #else  /* No ttylock()... */
  5401.  
  5402.     if (haslock && *flfnam) {
  5403.     /* Don't remove lockfile if we didn't make it ourselves */
  5404.     if ((x = ttrpid(flfnam)) != (int)getpid()) {
  5405.         debug(F111,"ttunlck lockfile seized",flfnam,x);
  5406.         printf("Warning - Lockfile %s seized by pid %d\n",
  5407.            flfnam,
  5408.            x
  5409.            );
  5410.         return(0);
  5411.     }
  5412.     priv_on();            /* Turn privileges on.  */
  5413.     errno = 0;
  5414.     x = unlink(flfnam);        /* Remove the lockfile. */
  5415.     debug(F111,"ttunlck unlink",flfnam,x);
  5416.     if (x < 0) {
  5417.         if (errno && !quiet)
  5418.           perror(ttnmsv);
  5419.         printf("Warning - Can't remove lockfile: %s\n", flfnam);
  5420.     }
  5421.     haslock = 0;
  5422.     *flfnam = '\0';            /* Erase the name. */
  5423.  
  5424. #ifdef RTAIX
  5425.     errno = 0;
  5426.     x = unlink(lkflfn);        /* Remove link to lockfile */
  5427.     debug(F111,"ttunlck AIX link unlink",lkflfn,x);
  5428.     if (x < 0) {
  5429.         if (errno && !quiet)
  5430.           perror(ttnmsv);
  5431.         printf("Warning - Can't remove link to lockfile: %s\n", lkflfn);
  5432.     }
  5433.     *lkflfn = '\0';
  5434. #else
  5435.     if (lock2[0]) {            /* If there is a second lockfile, */
  5436.         errno = 0;
  5437.         x = unlink(lock2);        /*  remove it too. */
  5438.         debug(F111,"ttunlck lock2 unlink",lock2,x);
  5439.         if (x < 0) {
  5440.         if (errno && !quiet)
  5441.           perror(ttnmsv);
  5442.         printf("Warning - Can't remove secondary lockfile: %s\n",
  5443.                lock2
  5444.                );
  5445.         }
  5446.         lock2[0] = '\0';        /* Forget its name. */
  5447.     }
  5448. #endif /* RTAIX */
  5449.  
  5450. #ifdef COMMENT
  5451. #ifdef LOCKF
  5452.         (VOID) lockf(ttyfd, F_ULOCK, 0L); /* Remove advisory lock */
  5453. #endif /* LOCKF */
  5454. #endif /* COMMENT */
  5455.  
  5456.     priv_off();            /* Turn privileges off. */
  5457.     }
  5458. #endif /* USETTYLOCK */
  5459. #endif /* !NOUUCP */
  5460.     return(0);
  5461. }
  5462.  
  5463. /*
  5464.   4.3BSD-style UUCP line direction control.
  5465.   (Stan Barber, Rice U, 1980-something...)
  5466. */
  5467. #ifndef NOUUCP
  5468. #ifdef ACUCNTRL
  5469. VOID
  5470. acucntrl(flag,ttname) char *flag, *ttname; {
  5471.     char x[DEVNAMLEN+32], *device, *devname;
  5472.  
  5473.     if (strcmp(ttname,CTTNAM) == 0 || xlocal == 0) /* If not local, */
  5474.       return;                /* just return. */
  5475.     device = ((devname = xxlast(ttname,'/')) != NULL ? devname+1 : ttname);
  5476.     if (strncmp(device,"LCK..",4) == 0) device += 5;
  5477.     ckmakmsg(x,DEVNAMLEN+32,"/usr/lib/uucp/acucntrl ",flag," ",device);
  5478.     debug(F110,"called ",x,0);
  5479.     zsyscmd(x);
  5480. }
  5481. #endif /* ACUCNTRL */
  5482. #endif /* NOUUCP */
  5483.  
  5484. /*
  5485.   T T H F L O W  --  Set or Reset hardware flow control.
  5486.  
  5487.   This is an attempt to collect all hardware-flow-control related code
  5488.   into a single module.  Thanks to Rick Sladkey and John Kohl for lots of
  5489.   help here.  Overview:
  5490.  
  5491.   Hardware flow control is not supported in many UNIX implementions.  Even
  5492.   when it is supported, there is no (ha ha) "standard" for the programming
  5493.   interface.  In general, 4.3BSD and earlier (sometimes), 4.4BSD, System V,
  5494.   SunOS, AIX, etc, have totally different methods.  (And, not strictly
  5495.   relevant here, the programming interface often brings one only to a no-op
  5496.   in the device driver!)
  5497.  
  5498.   Among all these, we have two major types of APIs: those in which hardware
  5499.   flow control is determined by bits in the same termio/termios/sgtty mode
  5500.   word(s) that are used for controlling such items as CBREAK vs RAW mode, and
  5501.   which are also used by the ttvt(), ttpkt(), conbin(), and concb() routines
  5502.   for changing terminal modes.  And those that use entirely different
  5503.   mechanisms.
  5504.  
  5505.   In the first category, it is important that any change in the mode bits be
  5506.   reflected in the relevant termio(s)/sgtty structure, so that subsequent
  5507.   changes to that structure do not wipe out the effects of this routine.  That
  5508.   is why a pointer, attrs, to the appropriate structure is passed as a
  5509.   parameter to this routine.
  5510.  
  5511.   The second category should give us no worries, since any changes to hardware
  5512.   flow control accomplished by this routine should not affect the termio(s)/
  5513.   sgtty structures, and therefore will not be undone by later changes to them.
  5514.  
  5515.   The second argument, status, means to turn on hardware flow control if
  5516.   nonzero, and to turn it off if zero.
  5517.  
  5518.   Returns: 0 on apparent success, -1 on probable failure.
  5519. */
  5520.  
  5521. /*
  5522.   The following business is for BSDI, where it was discovered that two
  5523.   separate bits, CCTS_OFLOW and CRTS_IFLOW, are used in hardware flow control,
  5524.   but CTRSCTS is defined (in <termios.h>) to be just CCTS_OFLOW rather both
  5525.   bits, so hwfc only works in one direction if you use CRTSCTS to control it.
  5526.   Other 4.4BSD-based Unixes such as FreeBSD 4.1, which use these two bits,
  5527.   define CRTSCTS correctly.
  5528. */
  5529. #ifdef FIXCRTSCTS
  5530. #ifdef CRTSCTS
  5531. #ifdef CCTS_OFLOW
  5532. #ifdef CRTS_IFLOW
  5533. #undef CRTSCTS
  5534. #define CRTSCTS (CRTS_IFLOW|CCTS_OFLOW)
  5535. #endif /* CRTS_IFLOW */
  5536. #endif /* CCTS_OFLOW */
  5537. #endif /* CRTSCTS */
  5538. #endif /* FIXCRTSCTS */
  5539.  
  5540. static int
  5541. tthflow(flow, status, attrs)
  5542.     int flow,                /* Type of flow control (ckcdeb.h) */
  5543.     status;                /* Nonzero = turn it on */
  5544.                     /* Zero = turn it off */
  5545. #ifdef BSD44ORPOSIX            /* POSIX or BSD44 */
  5546.     struct termios *attrs;
  5547. #else                    /* System V */
  5548. #ifdef ATTSV
  5549. #ifdef ATT7300
  5550. #ifdef UNIX351M
  5551. /* AT&T UNIX 3.51m can set but not test for hardware flow control */
  5552. #define RTSFLOW CTSCD
  5553. #define CTSFLOW CTSCD
  5554. #endif /* ATT7300 */
  5555. #endif /* UNIX351M */
  5556.     struct termio *attrs;
  5557. #else                    /* BSD, V7, etc */
  5558.     struct sgttyb *attrs;        /* sgtty info... */
  5559. #endif /* ATTSV */
  5560. #endif /* BSD44ORPOSIX */
  5561. /* tthflow */ {
  5562.  
  5563.     int x = 0;                /* tthflow() return code */
  5564.  
  5565. #ifdef Plan9
  5566.     return p9tthflow(flow, status);
  5567. #else
  5568.  
  5569. #ifndef OXOS                /* NOT Olivetti X/OS... */
  5570. /*
  5571.   For SunOS 4.0 and later in the BSD environment ...
  5572.  
  5573.   The declarations are copied and interpreted from the System V header files,
  5574.   so we don't actually have to pull in all the System V junk when building
  5575.   C-Kermit for SunOS in the BSD environment, which would be dangerous because
  5576.   having those symbols defined would cause us to take the wrong paths through
  5577.   the code.  The code in this section is used in both the BSD and Sys V SunOS
  5578.   versions.
  5579. */
  5580. #ifdef SUNOS41
  5581. /*
  5582.   In SunOS 4.1 and later, we use the POSIX calls rather than ioctl calls
  5583.   because GNU CC uses different formats for the _IOxxx macros than regular CC;
  5584.   the POSIX forms work for both.  But the POSIX calls are not available in
  5585.   SunOS 4.0.
  5586. */
  5587. #define CRTSCTS 0x80000000        /* RTS/CTS flow control */
  5588. #define TCSANOW 0            /* Do it now */
  5589.  
  5590.     struct termios {
  5591.     unsigned long c_iflag;        /* Input modes */
  5592.     unsigned long c_oflag;        /* Output modes */
  5593.     unsigned long c_cflag;        /* Control modes */
  5594.     unsigned long c_lflag;        /* Line discipline modes */
  5595.     char c_line;
  5596.     CHAR c_cc[17];
  5597.     };
  5598.     struct termios temp;
  5599.  
  5600. _PROTOTYP( int tcgetattr, (int, struct termios *) );
  5601. _PROTOTYP( int tcsetattr, (int, int, struct termios *) );
  5602. /*
  5603.   When CRTSCTS is set, SunOS won't do output unless both CTS and CD are
  5604.   asserted.  So we don't set CRTSCTS unless CD is up.  This should be OK,
  5605.   since we don't need RTS/CTS during dialing, and after dialing is complete,
  5606.   we should have CD.  If not, we still communicate, but without RTS/CTS.
  5607. */
  5608.     int mflags;                /* Modem signal flags */
  5609.  
  5610. #ifdef NETCMD
  5611.     if (ttpipe) return(0);
  5612. #endif /* NETCMD */
  5613. #ifdef NETPTY
  5614.     if (ttpty) return(0);
  5615. #endif /* NETPTY */
  5616.  
  5617.     debug(F101,"tthflow SUNOS41 entry status","",status);
  5618.     if (!status) {            /* Turn hard flow off */
  5619.     if (tcgetattr(ttyfd, &temp) > -1 && /* Get device attributes */
  5620.         (temp.c_cflag & CRTSCTS)) { /* Check for RTS/CTS */
  5621.         temp.c_cflag &= ~CRTSCTS;    /* It's there, remove it */
  5622.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5623.     }
  5624.     } else {                /* Turn hard flow on */
  5625.     if (ioctl(ttyfd,TIOCMGET,&mflags) > -1 && /* Get modem signals */
  5626.         (mflags & TIOCM_CAR)) {        /* Check for CD */
  5627.         debug(F100,"tthflow SunOS has CD","",0);
  5628.         if (tcgetattr(ttyfd, &temp) > -1 && /* Get device attributes */
  5629.         !(temp.c_cflag & CRTSCTS)) { /* Check for RTS/CTS */
  5630.         temp.c_cflag |= CRTSCTS;    /* Not there, add it */
  5631.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5632.         }
  5633.     } else {
  5634.         x = -1;
  5635.         debug(F100,"tthflow SunOS no CD","",0);
  5636.     }
  5637.     }
  5638. #else
  5639. #ifdef QNX
  5640.     struct termios temp;
  5641. #ifdef NETCMD
  5642.     if (ttpipe) return(0);
  5643. #endif /* NETCMD */
  5644. #ifdef NETPTY
  5645.     if (ttpty) return(0);
  5646. #endif /* NETPTY */
  5647.     debug(F101,"tthflow QNX entry status","",status);
  5648.     if (tcgetattr(ttyfd, &temp) > -1) {    /* Get device attributes */
  5649.     if (!status) {            /* Turn hard flow off */
  5650.         if ((temp.c_cflag & (IHFLOW|OHFLOW)) == (IHFLOW|OHFLOW)) {
  5651.         temp.c_cflag &= ~(IHFLOW|OHFLOW); /* It's there, remove it */
  5652.         attrs->c_cflag &= ~(IHFLOW|OHFLOW);
  5653.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5654.         }
  5655.     } else {            /* Turn hard flow on */
  5656.         if ((temp.c_cflag & (IHFLOW|OHFLOW)) != (IHFLOW|OHFLOW)) {
  5657.         temp.c_cflag |= (IHFLOW|OHFLOW); /* Not there, add it */
  5658.         temp.c_iflag &= ~(IXON|IXOFF);   /* Bye to IXON/IXOFF */
  5659.         ttraw.c_lflag |= IEXTEN;         /* Must be on */
  5660.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5661.         attrs->c_cflag |= (IHFLOW|OHFLOW);
  5662.         attrs->c_iflag &= ~(IXON|IXOFF);
  5663.         }
  5664.     }
  5665.     } else {
  5666.     x = -1;
  5667.     debug(F100, "tthflow QNX getattr fails", "", 0);
  5668.     }
  5669. #else
  5670. #ifdef POSIX_CRTSCTS
  5671. /*
  5672.   POSIX_CRTSCTS is defined in ckcdeb.h or on CC command line.
  5673.   Note: Do not assume CRTSCTS is a one-bit field!
  5674. */
  5675.     struct termios temp;
  5676. #ifdef NETCMD
  5677.     if (ttpipe) return(0);
  5678. #endif /* NETCMD */
  5679. #ifdef NETPTY
  5680.     if (ttpty) return(0);
  5681. #endif /* NETPTY */
  5682.     debug(F101,"tthflow POSIX_CRTSCTS entry status","",status);
  5683.     errno = 0;
  5684.     x = tcgetattr(ttyfd, &temp);
  5685.     debug(F111,"tthflow POSIX_CRTSCTS tcgetattr",ckitoa(x),errno);
  5686.     errno = 0;
  5687.     if (x < 0) {
  5688.     x = -1;
  5689.     } else {
  5690.     if (!status) {            /* Turn hard flow off */
  5691.         if (
  5692. #ifdef COMMENT
  5693.         /* This can fail because of sign extension */
  5694.         /* e.g. in Linux where it's Bit 31 */
  5695.         (temp.c_cflag & CRTSCTS) == CRTSCTS
  5696. #else
  5697.         (temp.c_cflag & CRTSCTS) != 0
  5698. #endif /* COMMENT */
  5699.         ) {
  5700.         temp.c_cflag &= ~CRTSCTS; /* It's there, remove it */
  5701.         attrs->c_cflag &= ~CRTSCTS;
  5702.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5703.         debug(F111,"tthflow POSIX_CRTSCTS OFF tcsetattr",
  5704.               ckitoa(x),errno);
  5705.         }
  5706.     } else {            /* Turn hard flow on */
  5707.         if (
  5708. #ifdef COMMENT
  5709.         /* This can fail because of sign extension */
  5710.         (temp.c_cflag & CRTSCTS) != CRTSCTS
  5711. #else
  5712.         (temp.c_cflag & CRTSCTS) == 0
  5713. #endif /* COMMENT */
  5714.         ) {
  5715.         temp.c_cflag |= CRTSCTS; /* Not there, add it */
  5716.         temp.c_iflag &= ~(IXON|IXOFF|IXANY); /* Bye to IXON/IXOFF */
  5717.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5718.         debug(F111,"tthflow POSIX_CRTSCTS ON tcsetattr",
  5719.               ckitoa(x),errno);
  5720.         attrs->c_cflag |= CRTSCTS;
  5721.         attrs->c_iflag &= ~(IXON|IXOFF|IXANY);
  5722.         }
  5723.     }
  5724.     }
  5725. #else
  5726. #ifdef SUNOS4
  5727. /*
  5728.   SunOS 4.0 (and maybe earlier?).  This code is dangerous because it
  5729.   prevents compilation with GNU gcc, which uses different formats for the
  5730.   _IORxxx macros than regular cc.  SunOS 4.1 and later can use the POSIX
  5731.   routines above, which work for both cc and gcc.
  5732. */
  5733. #define TCGETS _IOR(T, 8, struct termios) /* Get modes into termios struct */
  5734. #define TCSETS _IOW(T, 9, struct termios) /* Set modes from termios struct */
  5735. #define CRTSCTS 0x80000000          /* RTS/CTS flow control */
  5736.  
  5737.     struct termios {
  5738.     unsigned long c_iflag;        /* Input modes */
  5739.     unsigned long c_oflag;        /* Output modes */
  5740.     unsigned long c_cflag;        /* Control modes */
  5741.     unsigned long c_lflag;        /* Line discipline modes */
  5742.     char c_line;
  5743.     CHAR c_cc[17];
  5744.     };
  5745.     struct termios temp;
  5746. #ifdef NETCMD
  5747.     if (ttpipe) return(0);
  5748. #endif /* NETCMD */
  5749. #ifdef NETPTY
  5750.     if (ttpty) return(0);
  5751. #endif /* NETPTY */
  5752.     debug(F101,"tthflow entry status","",status);
  5753.     if (ioctl(ttyfd,TCGETS,&temp) > -1) { /* Get terminal modes. */
  5754.     if (status) {            /* Turn hard flow on */
  5755.         temp.c_cflag |= CRTSCTS;    /* Add RTS/CTS to them. */
  5756.         x = ioctl(ttyfd,TCSETS,&temp); /* Set them again. */
  5757.         attrs->c_cflag |= CRTSCTS;    /* Add to global info. */
  5758.     } else {            /* Turn hard flow off */
  5759.         temp.c_cflag &= ~CRTSCTS;
  5760.         x = ioctl(ttyfd,TCSETS,&temp);
  5761.         attrs->c_cflag &= ~CRTSCTS;
  5762.     }
  5763.     }
  5764. #else                    /* Not SunOS 4.0 or later */
  5765. #ifdef AIXRS                /* IBM AIX RS/6000 */
  5766. #ifndef AIX41                /* But only pre-4.x == SVR4 */
  5767. #ifdef NETCMD
  5768.     if (ttpipe) return(0);
  5769. #endif /* NETCMD */
  5770. #ifdef NETPTY
  5771.     if (ttpty) return(0);
  5772. #endif /* NETPTY */
  5773.     if (status) {
  5774.     if ((x = ioctl(ttyfd, TXADDCD, "rts")) < 0 && errno != EBUSY)
  5775.       debug(F100,"hardflow TXADDCD (rts) error", "", 0);
  5776.     } else {
  5777.     if ((x = ioctl(ttyfd, TXDELCD, "rts")) < 0 && errno != EINVAL)
  5778.       debug(F100,"hardflow TXDELCD (rts) error", "", 0);
  5779.     }
  5780. #endif /* AIX41 */
  5781. #else                    /* Not AIX RS/6000 */
  5782.  
  5783. #ifdef ATTSV                /* System V... */
  5784.  
  5785. #ifdef CK_SCOV5                /* SCO Open Server 5.0 */
  5786. #define CK_SCOUNIX
  5787. #else
  5788. #ifdef M_UNIX                /* SCO UNIX 3.2v4.x or earlier */
  5789. #define CK_SCOUNIX
  5790. #endif /* M_UNIX */
  5791. #endif /* CK_SCOV5 */
  5792.  
  5793. #ifdef SCO_FORCE_RTSXOFF
  5794. #ifdef CK_SCOUNIX            /* But not SCO OpenServer 5.0.4 */
  5795. #ifdef SCO_OSR504            /* or later... */
  5796. #undef CK_SCOUNIX
  5797. #endif /* SCO_OSR504 */
  5798. #endif /* CK_SCOUNIX */
  5799. #endif /* SCO_FORCE_RTSXOFF */
  5800.  
  5801. #ifdef CK_SCOUNIX
  5802. #ifdef POSIX
  5803.     struct termios temp;
  5804. #ifdef NETCMD
  5805.     if (ttpipe) return(0);
  5806. #endif /* NETCMD */
  5807. #ifdef NETPTY
  5808.     if (ttpty) return(0);
  5809. #endif /* NETPTY */
  5810.     debug(F101,"tthflow SCOUNIX POSIX entry status","",status);
  5811.     errno = 0;
  5812.     x = tcgetattr(ttyfd, &temp);
  5813.     debug(F111,"tthflow SCO UNIX POSIX tcgetattr",ckitoa(x),errno);
  5814. #else /* POSIX */
  5815.     struct termio temp;
  5816. #ifdef NETCMD
  5817.     if (ttpipe) return(0);
  5818. #endif /* NETCMD */
  5819. #ifdef NETPTY
  5820.     if (ttpty) return(0);
  5821. #endif /* NETPTY */
  5822.     debug(F101,"tthflow SCOUNIX non-POSIX entry status","",status);
  5823.     x = ioctl(ttyfd, TCGETA, &temp);
  5824.     debug(F111,"tthflow SCO UNIX non-POSIX TCGETA",ckitoa(x),errno);
  5825. #endif /* POSIX */
  5826. /*
  5827.   This is not really POSIX, since POSIX does not deal with hardware flow
  5828.   control, but we are using the POSIX APIs.  In fact, RTSFLOW and CTSFLOW
  5829.   are defined in termio.h, but within #ifndef _POSIX_SOURCE..#endif.  So
  5830.   let's try forcing their definitions here.
  5831. */
  5832. #ifndef CTSFLOW
  5833. #define CTSFLOW 0020000
  5834.     debug(F101,"tthflow SCO defining CTSFLOW","",CTSFLOW);
  5835. #else
  5836.     debug(F101,"tthflow SCO CTSFLOW","",CTSFLOW);
  5837. #endif /* CTSFLOW */
  5838. #ifndef RTSFLOW
  5839. #define RTSFLOW 0040000
  5840.     debug(F101,"tthflow SCO defining RTSFLOW","",RTSFLOW);
  5841. #else
  5842.     debug(F101,"tthflow SCO RTSFLOW","",RTSFLOW);
  5843. #endif /* RTSFLOW */
  5844. #ifndef ORTSFL
  5845. #define ORTSFL 0100000
  5846.     debug(F101,"tthflow SCO defining ORTSFL","",ORTSFL);
  5847. #else
  5848.     debug(F101,"tthflow SCO ORTSFL","",ORTSFL);
  5849. #endif /* ORTSFL */
  5850.  
  5851.     if (x != -1) {
  5852.     if (status) {            /* Turn it ON */
  5853.         temp.c_cflag |= RTSFLOW|CTSFLOW;
  5854.         attrs->c_cflag |= RTSFLOW|CTSFLOW;
  5855. #ifdef ORTSFL
  5856.         temp.c_cflag &= ~ORTSFL;
  5857.         attrs->c_cflag &= ~ORTSFL;
  5858. #endif /* ORTSFL */
  5859.         temp.c_iflag &= ~(IXON|IXOFF|IXANY);
  5860.         attrs->c_iflag &= ~(IXON|IXOFF|IXANY);
  5861.     } else {            /* Turn it OFF */
  5862. #ifdef ORTSFL
  5863.         temp.c_cflag &= ~(RTSFLOW|CTSFLOW|ORTSFL);
  5864.         attrs->c_cflag &= ~(RTSFLOW|CTSFLOW|ORTSFL);
  5865. #else  /* ORTSFL */
  5866.         temp.c_cflag &= ~(RTSFLOW|CTSFLOW);
  5867.         attrs->c_cflag &= ~(RTSFLOW|CTSFLOW);
  5868. #endif /* ORTSFL */
  5869.     }
  5870. #ifdef POSIX
  5871.     x = tcsetattr(ttyfd, TCSADRAIN, &temp);
  5872. #else
  5873.     x = ioctl(ttyfd, TCSETA, &temp);
  5874. #endif /* POSIX */
  5875.     debug(F101,"tthflow SCO set modes","",x);
  5876.     }
  5877. #else /* Not SCO UNIX */
  5878. #ifdef NETCMD
  5879.     if (ttpipe) return(0);
  5880. #endif /* NETCMD */
  5881. #ifdef NETPTY
  5882.     if (ttpty) return(0);
  5883. #endif /* NETPTY */
  5884.     if (!status) {            /* Turn it OFF */
  5885. #ifdef RTSXOFF
  5886.     debug(F100,"tthflow ATTSV RTS/CTS OFF","",0);
  5887.     rctsx.x_hflag &= ~(RTSXOFF|CTSXON);
  5888. #ifdef TCSETX
  5889.     x = ioctl(ttyfd,TCSETX,&rctsx);
  5890.     debug(F101,"tthflow ATTSV TCSETX OFF","",x);
  5891. #else
  5892.     x = -1
  5893.     debug(F100,"tthflow TCSETX not defined","",0);
  5894. #endif /* TCSETX */
  5895. #else
  5896.     debug(F100,"tthflow ATTSV RTSXOFF not defined","",0);
  5897. #endif /* RTSXOFF */
  5898. #ifdef DTRXOFF
  5899.     debug(F100,"tthflow ATTSV DTR/CD OFF","",0);
  5900.     rctsx.x_hflag &= ~(DTRXOFF|CDXON);
  5901.     x = ioctl(ttyfd,TCSETX,&rctsx);
  5902.     debug(F101,"tthflow ATTSV DTRXOFF OFF","",x);
  5903. #else
  5904.     debug(F100,"tthflow ATTSV DTRXOFF not defined","",0);
  5905. #endif /* DTRXOFF */
  5906.     } else {                /* Turn it ON. */
  5907.     if (flow == FLO_RTSC) {    /* RTS/CTS Flow control... */
  5908.         debug(F100,"tthflow ATTSV RTS/CTS ON","",0);
  5909. #ifdef RTSXOFF
  5910.         /* This is the preferred way, according to SVID3 */
  5911. #ifdef TCGETX
  5912.         x = ioctl(ttyfd,TCGETX,&rctsx);
  5913.         debug(F101,"tthflow TCGETX","",x);
  5914.         if (x > -1) {
  5915.         rctsx.x_hflag |= RTSXOFF | CTSXON;
  5916.         x = ioctl(ttyfd,TCSETX,&rctsx);
  5917.         debug(F100,"tthflow ATTSV ioctl","",x);
  5918.         }
  5919. #else
  5920.         debug(F100,"tthflow TCGETX not defined","",0);
  5921.         x = -1
  5922. #endif /* TCGETX */
  5923. #else
  5924.         debug(F100,"tthflow RTSXOFF not defined","",0);
  5925.         x = -1;
  5926. #endif /* RTSXOFF */
  5927.     } else if (flow == FLO_DTRC) {    /* DTR/CD Flow control... */
  5928.         debug(F100,"tthflow ATTSV DTR/CD ON","",0);
  5929. #ifdef DTRXOFF
  5930.         /* This is straight out of SVID R4 */
  5931.         if (ioctl(ttyfd,TCGETX,&rctsx) > -1) {
  5932.         rctsx.x_hflag &= ~(DTRXOFF|CDXON);
  5933.         x = ioctl(ttyfd,TCSETX,&rctsx);
  5934.         }
  5935. #else
  5936.         debug(F100,"tthflow ATTSV DTRXOFF not defined","",0);
  5937.         x = -1;
  5938. #endif /* DTRXOFF */
  5939.     }
  5940.     }
  5941. #endif /* CK_SCOUNIX */
  5942.  
  5943. #else /* not System V... */
  5944.  
  5945. #ifdef CK_DTRCTS
  5946. #ifdef LDODTR
  5947. #ifdef LDOCTS
  5948. #ifdef NETCMD
  5949.     if (ttpipe) return(0);
  5950. #endif /* NETCMD */
  5951. #ifdef NETPTY
  5952.     if (ttpty) return(0);
  5953. #endif /* NETPTY */
  5954.     x = LDODTR | LDOCTS;        /* Found only on UTEK? */
  5955.     if (flow == FLO_DTRT && status) {    /* Use hardware flow control */
  5956.     if (lmodef) {
  5957.         x = ioctl(ttyfd,TIOCLBIS,&x);
  5958.         if (x < 0) {
  5959.             debug(F100,"hardflow TIOCLBIS error","",0);
  5960.         } else {
  5961.         lmodef++;
  5962.         debug(F100,"hardflow TIOCLBIS ok","",0);
  5963.         }
  5964.     }
  5965.     } else {
  5966.     if (lmodef) {
  5967.         x = ioctl(ttyfd,TIOCLBIC,&x);
  5968.         if (x < 0) {
  5969.             debug(F100,"hardflow TIOCLBIC error","",0);
  5970.         } else {
  5971.         lmodef++;
  5972.         debug(F100,"hardflow TIOCLBIC ok","",0);
  5973.         }
  5974.     }
  5975.     }
  5976. #endif /* LDODTR */
  5977. #endif /* LDOCTS */
  5978. #endif /* CK_DTRCTS */
  5979. #endif /* ATTSV */
  5980. #endif /* AIXRS */
  5981. #endif /* SUNOS4 */
  5982. #endif /* QNX */
  5983. #endif /* POSIX_CRTSCTS */
  5984. #endif /* SUNOS41 */
  5985.  
  5986. #else /* OXOS */
  5987.  
  5988.     struct termios temp;        /* Olivetti X/OS ... */
  5989.  
  5990. #ifdef NETCMD
  5991.     if (ttpipe) return(0);
  5992. #endif /* NETCMD */
  5993. #ifdef NETPTY
  5994.     if (ttpty) return(0);
  5995. #endif /* NETPTY */
  5996.     x = ioctl(ttyfd,TCGETS,&temp);
  5997.     if (x == 0) {
  5998.     temp.c_cflag &= ~(CRTSCTS|CDTRCTS|CBRKFLOW|CDTRDSR|CRTSDSR);
  5999.     if (status) {
  6000.         switch (flow) {
  6001.           case FLO_RTSC: temp.c_cflag |= CRTSCTS; /* RTS/CTS (hard) */
  6002.         break;
  6003.           case FLO_DTRT: temp.c_cflag |= CDTRCTS; /* DTR/CTS (hard) */
  6004.         break;
  6005.         }
  6006.     }
  6007.     x = ioctl(ttyfd,TCSETS,&temp);
  6008.     }
  6009. #endif /* OXOS */
  6010.     return(x);
  6011.  
  6012. #endif /* Plan9 */
  6013. }
  6014.  
  6015. /*  T T P K T  --  Condition the communication line for packets */
  6016. /*                 or for modem dialing */
  6017.  
  6018. /*
  6019.   If called with speed > -1, also set the speed.
  6020.   Returns 0 on success, -1 on failure.
  6021.  
  6022.   NOTE: the "xflow" parameter is supposed to be the currently selected
  6023.   type of flow control, but for historical reasons, this parameter is also
  6024.   used to indicate that we are dialing.  Therefore, when the true flow
  6025.   control setting is needed, we access the external variable "flow", rather
  6026.   than trusting our "xflow" argument.
  6027. */
  6028. int
  6029. #ifdef CK_ANSIC
  6030. ttpkt(long speed, int xflow, int parity)
  6031. #else
  6032. ttpkt(speed,xflow,parity) long speed; int xflow, parity;
  6033. #endif /* CK_ANSIC */
  6034. /* ttpkt */ {
  6035. #ifndef NOLOCAL
  6036.     int s2;
  6037.     int s = -1;
  6038. #endif /* NOLOCAL */
  6039.     int x;
  6040.     extern int flow;            /* REAL flow-control setting */
  6041.  
  6042.     if (ttyfd < 0) return(-1);          /* Not open. */
  6043.  
  6044.     debug(F101,"ttpkt parity","",parity);
  6045.     debug(F101,"ttpkt xflow","",xflow);
  6046.     debug(F101,"ttpkt speed","",(int) speed);
  6047.  
  6048.     ttprty = parity;                    /* Let other tt functions see these. */
  6049.     ttspeed = speed;            /* Make global copy for this module */
  6050.     ttpmsk = ttprty ? 0177 : 0377;    /* Parity stripping mask */
  6051. #ifdef PARSENSE
  6052.     needpchk = ttprty ? 0 : 1;        /* Parity check needed? */
  6053. #else
  6054.     needpchk = 0;
  6055. #endif /* PARSENSE */
  6056.  
  6057.     debug(F101,"ttpkt ttpmsk","",ttpmsk);
  6058.     debug(F101,"ttpkt netconn","",netconn);
  6059.  
  6060. #ifdef NETCONN                /* No mode-changing for telnet */
  6061.     if (netconn) {
  6062. #ifdef TCPSOCKET
  6063. #ifdef TCP_NODELAY
  6064.         if (ttnet == NET_TCPB) {    /* But turn off Nagle */
  6065.             extern int tcp_nodelay;
  6066.             nodelay_sav = tcp_nodelay;
  6067.             no_delay(ttyfd,1);
  6068.         }
  6069. #endif /* TCP_NODELAY */
  6070. #ifdef TN_COMPORT
  6071.         if (istncomport()) {
  6072.             int rc = -1;
  6073.             if (tvtflg == 0 && speed == ttspeed && flow == ttflow
  6074.                  /* && ttcarr == curcarr */ ) {
  6075.                 debug(F100,"ttpkt modes already set, skipping...","",0);
  6076.                 return(0);        /* Already been called. */
  6077.             }
  6078.             if (flow != ttflow) {
  6079.                 if ((rc = tnsetflow(flow)) < 0)
  6080.           return(rc);
  6081.                 ttflow = flow;
  6082.             }
  6083.             if (speed != ttspeed) {
  6084.                 if (speed <= 0) 
  6085.           speed = tnc_get_baud();
  6086.                 else if ((rc = tnc_set_baud(speed)) < 0)
  6087.           return(rc);
  6088.                 ttspeed = speed;
  6089.             }
  6090.             tnc_set_datasize(8);
  6091.         tnc_set_stopsize(stopbits);
  6092.  
  6093. #ifdef HWPARITY
  6094.             if (hwparity) {
  6095.                 switch (hwparity) {
  6096.           case 'e':            /* Even */
  6097.                     debug(F100,"ttres 8 bits + even parity","",0);
  6098.                     tnc_set_parity(3);
  6099.                     break;
  6100.           case 'o':            /* Odd */
  6101.                     debug(F100,"ttres 8 bits + odd parity","",0);
  6102.                     tnc_set_parity(2);
  6103.                     break;
  6104.           case 'm':            /* Mark */
  6105.                     debug(F100,"ttres 8 bits + invalid parity: mark","",0);
  6106.                     tnc_set_parity(4);
  6107.                     break;
  6108.           case 's':            /* Space */
  6109.                     debug(F100,"ttres 8 bits + invalid parity: space","",0);
  6110.                     tnc_set_parity(5);
  6111.                     break;
  6112.                 }
  6113.             } else 
  6114. #endif /* HWPARITY */
  6115.         {
  6116.                 tnc_set_parity(0);              /* None */
  6117.             }
  6118.             tvtflg = 0;
  6119.             return(0);
  6120.         }
  6121. #endif /* TN_COMPORT */
  6122. #endif /* TCPSOCKET */
  6123.         tvtflg = 0;
  6124.         return(0);
  6125.     }
  6126. #endif /* NETCONN */
  6127. #ifdef NETCMD
  6128.     if (ttpipe) return(0);
  6129. #endif /* NETCMD */
  6130. #ifdef NETPTY
  6131.     if (ttpty) return(0);
  6132. #endif /* NETPTY */
  6133.  
  6134. #ifndef Plan9
  6135.     if (ttfdflg && !isatty(ttyfd)) return(0);
  6136. #endif /* Plan9 */
  6137.  
  6138. #ifdef COHERENT
  6139. #define SVORPOSIX
  6140. #endif /* COHERENT */
  6141.  
  6142. #ifndef SVORPOSIX            /* Berkeley, V7, etc. */
  6143. #ifdef LPASS8
  6144. /*
  6145.  For some reason, with BSD terminal drivers, you can't set FLOW to XON/XOFF
  6146.  after having previously set it to NONE without closing and reopening the
  6147.  device.  Unless there's something I overlooked below...
  6148. */
  6149.     if (ttflow == FLO_NONE && flow == FLO_XONX && xlocal == 0) {
  6150.     debug(F101,"ttpkt executing horrible flow kludge","",0);
  6151.     ttclos(0);            /* Close it */
  6152.     x = 0;
  6153.     ttopen(ttnmsv,&x,ttmdm,0);    /* Open it again */
  6154.     }
  6155. #endif /* LPASS8 */
  6156. #endif /* SVORPOSIX */
  6157.  
  6158. #ifdef COHERENT                /* This must be vestigial since we */
  6159. #undef SVORPOSIX            /* reverse it a few lines below... */
  6160. #endif /* COHERENT */
  6161.  
  6162.     if (xflow != FLO_DIAL && xflow != FLO_DIAX)
  6163.       ttflow = xflow;            /* Now make this available too. */
  6164.  
  6165. #ifndef NOLOCAL
  6166.     if (xlocal) {
  6167.     s2 = (int) (speed / 10L);    /* Convert bps to cps */
  6168.     debug(F101,"ttpkt calling ttsspd","",s2);
  6169.     s = ttsspd(s2);            /* Check and set the speed */
  6170.     debug(F101,"ttpkt ttsspd result","",s);
  6171.      carrctl(&ttraw, xflow != FLO_DIAL /* Carrier control */
  6172.         && (ttcarr == CAR_ON || (ttcarr == CAR_AUT && ttmdm != 0)));
  6173.     tvtflg = 0;            /* So ttvt() will work next time */
  6174.     }
  6175. #endif /* NOLOCAL */
  6176.  
  6177. #ifdef COHERENT
  6178. #define SVORPOSIX
  6179. #endif /* COHERENT */
  6180.  
  6181. #ifndef SVORPOSIX            /* BSD section */
  6182.     if (flow == FLO_RTSC ||        /* Hardware flow control */
  6183.     flow == FLO_DTRC ||
  6184.     flow == FLO_DTRT) {
  6185.     tthflow(flow, 1, &ttraw);
  6186.     debug(F100,"ttpkt hard flow, TANDEM off, RAW on","",0);
  6187.     ttraw.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6188.     ttraw.sg_flags |= RAW;        /* Enter raw mode */
  6189.     } else if (flow == FLO_NONE) {    /* No flow control */
  6190.     debug(F100,"ttpkt no flow, TANDEM off, RAW on","",0);
  6191.     ttraw.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6192.     tthflow(flow, 0, &ttraw);    /* Turn off any hardware f/c too */
  6193.     ttraw.sg_flags |= RAW;        /* Enter raw mode */
  6194.     } else if (flow == FLO_KEEP) {    /* Keep device's original setting */
  6195.     debug(F100,"ttpkt keeping original TANDEM","",0);
  6196.     ttraw.sg_flags &= ~TANDEM;
  6197.     ttraw.sg_flags |= (ttold.sg_flags & TANDEM);
  6198.     /* NOTE: We should also handle hardware flow control here! */
  6199.     }
  6200.  
  6201. /* SET FLOW XON/XOFF is in effect, or SET FLOW KEEP resulted in Xon/Xoff */
  6202.  
  6203.     if ((flow == FLO_XONX) || (ttraw.sg_flags & TANDEM)) {
  6204.     debug(F100,"ttpkt turning on TANDEM","",0);
  6205.     ttraw.sg_flags |= TANDEM;    /* So ask for it. */
  6206.  
  6207. #ifdef LPASS8                /* Can pass 8-bit data through? */
  6208. /* If the LPASS8 local mode is available, then flow control can always  */
  6209. /* be used, even if parity is none and we are transferring 8-bit data.  */
  6210. /* But we only need to do all this if Xon/Xoff is requested. */
  6211. /* BUT... this tends not to work through IP or LAT connections, terminal */
  6212. /* servers, telnet, rlogin, etc, so it is currently disabled. */
  6213.     x = LPASS8;            /* If LPASS8 defined, then */
  6214.     debug(F100,"ttpkt executing LPASS8 code","",0);
  6215.     if (lmodef) {            /* TIOCLBIS must be too. */
  6216.         x = ioctl(ttyfd,TIOCLBIS,&x); /* Try to set LPASS8. */
  6217.         if (x < 0) {
  6218.         debug(F100,"ttpkt TIOCLBIS error","",0);
  6219.         } else {
  6220.         lmodef++;
  6221.         debug(F100,"ttpkt TIOCLBIS ok","",0);
  6222.         }
  6223.     }
  6224. /*
  6225.  But if we use LPASS8 mode, we must explicitly turn off
  6226.  terminal interrupts of all kinds.
  6227. */
  6228. #ifdef TIOCGETC                /* Not rawmode, */
  6229.     if (tcharf && (xlocal == 0)) {    /* must turn off */
  6230.         tchnoi.t_intrc = -1;    /* interrupt character */
  6231.         tchnoi.t_quitc = -1;    /* and quit character. */
  6232.         tchnoi.t_startc = 17;    /* Make sure xon */
  6233.         tchnoi.t_stopc = 19;    /* and xoff not ignored. */
  6234. #ifndef NOBRKC
  6235.         tchnoi.t_eofc = -1;        /* eof character. */
  6236.         tchnoi.t_brkc = -1;        /* brk character. */
  6237. #endif /* NOBRKC */
  6238.         if (ioctl(ttyfd,TIOCSETC,&tchnoi) < 0) {
  6239.         debug(F100,"ttpkt TIOCSETC failed","",0);
  6240.         } else {
  6241.         tcharf = 1;
  6242.         debug(F100,"ttpkt TIOCSETC ok","",0);
  6243.         }
  6244. #ifdef COMMENT
  6245. /* only for paranoid debugging */
  6246.         if (tcharf) {
  6247.         struct tchars foo;
  6248.         char tchbuf[100];
  6249.         ioctl(0,TIOCGETC,&foo);
  6250.         sprintf(tchbuf,
  6251.             "intr=%d,quit=%d, start=%d, stop=%d, eof=%d, brk=%d",
  6252.             foo.t_intrc, foo.t_quitc, foo.t_startc,
  6253.             foo.t_stopc, foo.t_eofc,  foo.t_brkc);
  6254.         debug(F110,"ttpkt chars",tchbuf,0);
  6255.         }
  6256. #endif /* COMMENT */
  6257.     }
  6258.     ttraw.sg_flags |= CBREAK;    /* Needed for unknown reason */
  6259. #endif /* TIOCGETC */
  6260.  
  6261. /* Prevent suspend during packet mode */
  6262. #ifdef TIOCGLTC                /* Not rawmode, */
  6263.     if (ltcharf && (xlocal == 0)) {    /* must turn off */
  6264.         ltchnoi.t_suspc = -1;    /* suspend character */
  6265.         ltchnoi.t_dsuspc = -1;    /* and delayed suspend character */
  6266.         if (ioctl(ttyfd,TIOCSLTC,&tchnoi) < 0) {
  6267.         debug(F100,"ttpkt TIOCSLTC failed","",0);
  6268.         } else {
  6269.         ltcharf = 1;
  6270.         debug(F100,"ttpkt TIOCSLTC ok","",0);
  6271.         }
  6272.     }
  6273. #endif /* TIOCGLTC */
  6274.  
  6275. #else /* LPASS8 not defined */
  6276.  
  6277. /* Previously, BSD-based implementations always */
  6278. /* used rawmode for packets.  Now, we use rawmode only if parity is NONE. */
  6279. /* This allows the flow control requested above to actually work, but only */
  6280. /* if the user asks for parity (which also means they get 8th-bit quoting). */
  6281.  
  6282.     if (parity) {            /* If parity, */
  6283.         ttraw.sg_flags &= ~RAW;    /* use cooked mode */
  6284. #ifdef COMMENT
  6285. /* WHY??? */
  6286.         if (xlocal)
  6287. #endif /* COMMENT */
  6288.           ttraw.sg_flags |= CBREAK;
  6289.         debug(F101,"ttpkt cooked, cbreak, parity","",parity);
  6290. #ifdef TIOCGETC                /* Not rawmode, */
  6291.         if (tcharf && (xlocal == 0)) { /* must turn off */
  6292.         tchnoi.t_intrc = -1;    /* interrupt character */
  6293.         tchnoi.t_quitc = -1;    /* and quit character. */
  6294.         tchnoi.t_startc = 17;    /* Make sure xon */
  6295.         tchnoi.t_stopc = 19;    /* and xoff not ignored. */
  6296. #ifndef NOBRKC
  6297.         tchnoi.t_eofc = -1;    /* eof character. */
  6298.         tchnoi.t_brkc = -1;    /* brk character. */
  6299. #endif /* NOBRKC */
  6300.         if (ioctl(ttyfd,TIOCSETC,&tchnoi) < 0) {
  6301.             debug(F100,"ttpkt TIOCSETC failed","",0);
  6302.         } else {
  6303.             tcharf = 1;
  6304.             debug(F100,"ttpkt TIOCSETC ok","",0);
  6305.         }
  6306.         }
  6307. #endif /* TIOCGETC */
  6308. #ifdef TIOCGLTC                /* Not rawmode, */
  6309. /* Prevent suspend during packet mode */
  6310.         if (ltcharf && (xlocal == 0)) { /* must turn off */
  6311.         ltchnoi.t_suspc = -1;    /* suspend character */
  6312.         ltchnoi.t_dsuspc = -1;    /* and delayed suspend character */
  6313.         if (ioctl(ttyfd,TIOCSLTC,&tchnoi) < 0) {
  6314.             debug(F100,"ttpkt TIOCSLTC failed","",0);
  6315.         } else {
  6316.             ltcharf = 1;
  6317.             debug(F100,"ttpkt TIOCSLTC ok","",0);
  6318.         }
  6319.         }
  6320. #endif /* TIOCGLTC */
  6321.     } else {            /* If no parity, */
  6322.         ttraw.sg_flags |= RAW;    /* must use 8-bit raw mode. */
  6323.         debug(F101,"ttpkt setting rawmode, parity","",parity);
  6324.     }
  6325. #endif /* LPASS8 */
  6326.     } /* End of Xon/Xoff section */
  6327.  
  6328.     /* Don't echo, don't map CR to CRLF on output, don't fool with case */
  6329. #ifdef LCASE
  6330.     ttraw.sg_flags &= ~(ECHO|CRMOD|LCASE);
  6331. #else
  6332.     ttraw.sg_flags &= ~(ECHO|CRMOD);
  6333. #endif /* LCASE */
  6334.  
  6335. #ifdef TOWER1
  6336.     ttraw.sg_flags &= ~ANYP;            /* Must set this on old Towers */
  6337. #endif /* TOWER1 */
  6338.  
  6339. #ifdef BELLV10
  6340.     if (ioctl(ttyfd,TIOCSETP,&ttraw) < 0) /* Set the new modes. */
  6341.       return(-1);
  6342. #else
  6343.     errno = 0;
  6344.     if (stty(ttyfd,&ttraw) < 0) {       /* Set the new modes. */
  6345.         debug(F101,"ttpkt stty failed","",errno);
  6346.         return(-1);
  6347.     }
  6348. #endif /* BELLV10 */
  6349.     debug(F100,"ttpkt stty ok","",0);
  6350.  
  6351. #ifdef sony_news
  6352.     x = xlocal ? km_ext : km_con;    /* Put line in ASCII mode. */
  6353.     if (x != -1) {            /* Make sure we know original modes. */
  6354.     x &= ~KM_TTYPE;
  6355.     x |= KM_ASCII;
  6356.     if (ioctl(ttyfd,TIOCKSET, &x) < 0) {
  6357.         perror("ttpkt can't set ASCII mode");
  6358.         debug(F101,"ttpkt error setting ASCII mode","",x);
  6359.         return(-1);
  6360.     }
  6361.     }
  6362.     debug(F100,"ttpkt set ASCII mode ok","",0);
  6363. #endif /* sony_news */
  6364.  
  6365.     if (xlocal == 0) {            /* Turn this off so we can read */
  6366.     signal(SIGINT,SIG_IGN);        /* Ctrl-C chars typed at console */
  6367.     sigint_ign = 1;
  6368.     }
  6369.     tvtflg = 0;                /* So ttvt() will work next time */
  6370.     debug(F100,"ttpkt success","",0);
  6371.     return(0);
  6372.  
  6373. #endif /* Not ATTSV or POSIX */
  6374.  
  6375. /* AT&T UNIX and POSIX */
  6376.  
  6377. #ifdef COHERENT
  6378. #define SVORPOSIX
  6379. #endif /* COHERENT */
  6380.  
  6381. #ifdef SVORPOSIX
  6382.     if (flow == FLO_XONX) {        /* Xon/Xoff */
  6383.     ttraw.c_iflag |= (IXON|IXOFF);
  6384.     tthflow(flow, 0, &ttraw);
  6385.     } else if (flow == FLO_NONE) {    /* None */
  6386.     /* NOTE: We should also turn off hardware flow control here! */
  6387.     ttraw.c_iflag &= ~(IXON|IXOFF);
  6388.     tthflow(flow, 0, &ttraw);
  6389.     } else if (flow == FLO_KEEP) {    /* Keep */
  6390.     ttraw.c_iflag &= ~(IXON|IXOFF);    /* Turn off Xon/Xoff flags */
  6391.     ttraw.c_iflag |= (ttold.c_iflag & (IXON|IXOFF)); /* OR in old ones */
  6392.     /* NOTE: We should also handle hardware flow control here! */
  6393. #ifdef POSIX_CRTSCTS
  6394. /* In Linux case, we do this, which is unlikely to be portable */
  6395.         ttraw.c_cflag &= ~CRTSCTS;    /* Turn off RTS/CTS flag */
  6396.         ttraw.c_cflag |= (ttold.c_cflag & CRTSCTS); /* OR in old one */
  6397. #endif /* POSIX_CRTSCTS */
  6398.     } else if (flow == FLO_RTSC ||    /* Hardware */
  6399.            flow == FLO_DTRC ||
  6400.            flow == FLO_DTRT) {
  6401.     ttraw.c_iflag &= ~(IXON|IXOFF);    /* (190) */
  6402.     tthflow(flow, 1, &ttraw);
  6403.     }
  6404.     ttraw.c_lflag &= ~(ICANON|ECHO);
  6405.     ttraw.c_lflag &= ~ISIG;        /* Do NOT check for interrupt chars */
  6406.  
  6407. #ifndef OXOS
  6408. #ifdef QNX
  6409.     if (flow != FLO_RTSC && flow != FLO_DTRC && flow != FLO_DTRT)
  6410. #endif /* QNX */
  6411. #ifndef COHERENT
  6412.       ttraw.c_lflag &= ~IEXTEN;        /* Turn off ^O/^V processing */
  6413. #endif /* COHERENT */
  6414. #else /* OXOS */
  6415.     ttraw.c_cc[VDISCARD] = ttraw.c_cc[VLNEXT] = CDISABLE;
  6416. #endif /* OXOS */
  6417.     ttraw.c_lflag |= NOFLSH;        /* Don't flush */
  6418.     ttraw.c_iflag |= IGNPAR;        /* Ignore parity errors */
  6419. #ifdef ATTSV
  6420. #ifdef BSD44
  6421.     ttraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|INPCK|ISTRIP|IXANY);
  6422. #else
  6423.     ttraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IUCLC|INPCK|ISTRIP|IXANY);
  6424. #endif /* BSD44 */
  6425. #else /* POSIX */
  6426.     ttraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|INPCK|ISTRIP);
  6427. #endif /* ATTSV */
  6428.     ttraw.c_oflag &= ~OPOST;
  6429.     ttraw.c_cflag &= ~(CSIZE);
  6430.     ttraw.c_cflag |= (CS8|CREAD|HUPCL);
  6431.  
  6432. #ifdef CSTOPB
  6433.     if (xlocal) {
  6434.     if (stopbits == 2) {
  6435.         ttraw.c_cflag |= CSTOPB;    /* 2 stop bits */
  6436.         debug(F100,"ttpkt 2 stopbits","",0);
  6437.     } else if (stopbits == 1) {
  6438.         ttraw.c_cflag &= ~(CSTOPB);    /* 1 stop bit */
  6439.         debug(F100,"ttpkt 1 stopbit","",0);
  6440.     }
  6441.     }
  6442. #endif /* CSTOPB */
  6443.  
  6444. #ifdef HWPARITY
  6445.     if (hwparity && xlocal) {        /* Hardware parity */
  6446.     ttraw.c_cflag |= PARENB;    /* Enable parity */
  6447. #ifdef IGNPAR
  6448.     ttraw.c_cflag &= ~(IGNPAR);    /* Don't discard incoming bytes */
  6449.     debug(F100,"ttpkt IGNPAR","",0); /* that have parity errors */
  6450. #endif /* IGNPAR */
  6451.     switch (hwparity) {
  6452.       case 'e':            /* Even */
  6453.         ttraw.c_cflag &= ~(PARODD);
  6454.         debug(F100,"ttpkt 8 bits + even parity","",0);
  6455.         break;
  6456.       case 'o':            /* Odd */
  6457.         ttraw.c_cflag |= PARODD;
  6458.         debug(F100,"ttpkt 8 bits + odd parity","",0);
  6459.         break;
  6460.       case 'm':            /* Mark */
  6461.       case 's':            /* Space */
  6462.         /* PAREXT is mentioned in SVID but the details are not given. */
  6463.         /* PAREXT is not included in POSIX ISO/IEC 9945-1. */
  6464.         debug(F100,"ttpkt 8 bits + invalid parity","",0);
  6465.         break;
  6466.     }
  6467.     } else {                /* We handle parity ourselves */
  6468. #endif /* HWPARITY */
  6469.     ttraw.c_cflag &= ~(PARENB);    /* Don't enable parity */
  6470. #ifdef HWPARITY
  6471.     }
  6472. #endif /* HWPARITY */
  6473.  
  6474. #ifdef IX370
  6475.     ttraw.c_cc[4] = 48;  /* So Series/1 doesn't interrupt on every char */
  6476.     ttraw.c_cc[5] = 1;
  6477. #else
  6478. #ifndef VEOF                /* for DGUX this is VEOF, not VMIN */
  6479.     ttraw.c_cc[4] = 1;   /* [VMIN]  return max of this many characters or */
  6480. #else
  6481. #ifndef OXOS
  6482. #ifdef VMIN
  6483.     ttraw.c_cc[VMIN] = 1;
  6484. #endif /* VMIN */
  6485. #else /* OXOS */
  6486.     ttraw.c_min = 1;
  6487. #endif /* OXOS */
  6488. #endif /* VEOF */
  6489. #ifndef VEOL                /* for DGUX this is VEOL, not VTIME */
  6490.     ttraw.c_cc[5] = 0;     /* [VTIME] when this many secs/10 expire w/no input */
  6491. #else
  6492. #ifndef OXOS
  6493. #ifdef VTIME
  6494.     ttraw.c_cc[VTIME] = 0;
  6495. #endif /* VTIME */
  6496. #else /* OXOS */
  6497.     ttraw.c_time = 0;
  6498. #endif /* OXOS */
  6499. #endif /* VEOL */
  6500. #endif /* IX370 */
  6501.  
  6502. #ifdef VINTR                /* Turn off interrupt character */
  6503.     if (xlocal == 0)            /* so ^C^C can break us out of */
  6504.       ttraw.c_cc[VINTR] = 0;        /* packet mode. */
  6505. #endif /* VINTR */
  6506.  
  6507. #ifdef Plan9
  6508.     if (p9ttyparity('n') < 0)
  6509.     return -1;
  6510. #else
  6511. #ifdef BSD44ORPOSIX
  6512.     errno = 0;
  6513. #ifdef BEOSORBEBOX
  6514.     ttraw.c_cc[VMIN] = 0;        /* DR7 can only poll. */
  6515. #endif /* BEOSORBEBOX */
  6516.     debug(F100,"ttpkt calling tcsetattr(TCSETAW)","",0);
  6517.     x = tcsetattr(ttyfd,TCSADRAIN,&ttraw);
  6518.     debug(F101,"ttpkt BSD44ORPOSIX tcsetattr","",x);
  6519.     if (x < 0) {
  6520.     debug(F101,"ttpkt BSD44ORPOSIX tcsetattr errno","",errno);
  6521.         return(-1);
  6522.     }
  6523. #else /* BSD44ORPOSIX */
  6524.     x = ioctl(ttyfd,TCSETAW,&ttraw);
  6525.     debug(F101,"ttpkt ATTSV ioctl TCSETAW","",x);
  6526.     if (x < 0) {  /* set new modes . */
  6527.     debug(F101,"ttpkt ATTSV ioctl TCSETAW errno","",errno);
  6528.         return(-1);
  6529.     }
  6530. #endif /* BSD44ORPOSIX */
  6531. #endif /* Plan9 */
  6532.     tvtflg = 0;
  6533.     debug(F100,"ttpkt ok","",0);
  6534.     return(0);
  6535. #endif /* ATTSV */
  6536.  
  6537. #ifdef COHERENT
  6538. #undef SVORPOSIX
  6539. #endif /* COHERENT */
  6540.  
  6541. }
  6542.  
  6543. /*  T T S E T F L O W  --  Set flow control immediately.  */
  6544.  
  6545. #ifdef COHERENT
  6546. #define SVORPOSIX
  6547. #endif /* COHERENT */
  6548.  
  6549. int
  6550. ttsetflow(flow) int flow; {
  6551.     if (ttyfd < 0)            /* A channel must be open */
  6552.       return(-1);
  6553.  
  6554.     debug(F101,"ttsetflow flow","",flow);
  6555.  
  6556. #ifdef TN_COMPORT
  6557.     if (netconn && istncomport()) {
  6558.     debug(F101,"ttsetflow net modem","",ttmdm);
  6559.     return(tnsetflow(flow));
  6560.     }
  6561. #endif /* TN_COMPORT */
  6562. #ifdef NETCMD
  6563.     if (ttpipe) return(0);
  6564. #endif /* NETCMD */
  6565. #ifdef NETPTY
  6566.     if (ttpty) return(0);
  6567. #endif /* NETPTY */
  6568.  
  6569. #ifdef COMMENT
  6570.     /* This seems to hurt... */
  6571.     if (flow == FLO_KEEP)
  6572.       return(0);
  6573. #endif /* COMMENT */
  6574.  
  6575.     if (flow == FLO_RTSC ||        /* Hardware flow control... */
  6576.     flow == FLO_DTRC ||
  6577.     flow == FLO_DTRT) {
  6578.     tthflow(flow, 1, &ttraw);
  6579. #ifndef SVORPOSIX
  6580.     ttraw.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6581. #else
  6582.     ttraw.c_iflag &= ~(IXON|IXOFF);
  6583. #endif /* SVORPOSIX */
  6584.  
  6585.     } else if (flow == FLO_XONX) {    /* Xon/Xoff... */
  6586.  
  6587. #ifndef SVORPOSIX
  6588.     ttraw.sg_flags |= TANDEM;
  6589. #else
  6590.     ttraw.c_iflag |= (IXON|IXOFF);
  6591. #endif /* SVORPOSIX */
  6592.     tthflow(FLO_RTSC, 0, &ttraw);    /* Turn off hardware flow control */
  6593.  
  6594.     } else if (flow == FLO_NONE) {    /* No flow control */
  6595.  
  6596. #ifndef SVORPOSIX
  6597.     ttraw.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6598. #else
  6599.     ttraw.c_iflag &= ~(IXON|IXOFF);
  6600. #endif /* SVORPOSIX */
  6601.     tthflow(FLO_RTSC, 0, &ttraw);    /* Turn off any hardware f/c too */
  6602.     }
  6603.  
  6604. /* Set the new modes... */
  6605.  
  6606. #ifndef SVORPOSIX            /* BSD and friends */
  6607. #ifdef BELLV10
  6608.     if (ioctl(ttyfd,TIOCSETP,&ttraw) < 0)
  6609.       return(-1);
  6610. #else
  6611. #ifndef MINIX2
  6612.     if (stty(ttyfd,&ttraw) < 0)
  6613.       return(-1);
  6614. #endif /* MINIX2 */
  6615. #endif /* BELLV10 */
  6616. #else
  6617. #ifdef BSD44ORPOSIX            /* POSIX */
  6618.     if (tcsetattr(ttyfd,TCSADRAIN,&ttraw) < 0)
  6619.       return(-1);
  6620. #else                    /* System V */
  6621.     if (ioctl(ttyfd,TCSETAW,&ttraw) < 0)
  6622.       return(-1);
  6623. #endif /* BSD44ORPOSIX */
  6624. #endif /* SVORPOSIX */
  6625.     return(0);
  6626. }
  6627. #ifdef COHERENT
  6628. #undef SVORPOSIX
  6629. #endif /* COHERENT */
  6630.  
  6631. /*  T T V T -- Condition communication device for use as virtual terminal. */
  6632.  
  6633. int
  6634. #ifdef CK_ANSIC
  6635. ttvt(long speed, int flow)
  6636. #else
  6637. ttvt(speed,flow) long speed; int flow;
  6638. #endif /* CK_ANSIC */
  6639. /* ttvt */ {
  6640.     int s, s2, x;
  6641.  
  6642.     debug(F101,"ttvt ttyfd","",ttyfd);
  6643.     debug(F101,"ttvt tvtflg","",tvtflg);
  6644.     debug(F111,"ttvt speed",ckitoa(ttspeed),speed);
  6645.     debug(F111,"ttvt flow",ckitoa(ttflow),flow);
  6646.     debug(F111,"ttvt curcarr",ckitoa(ttcarr),curcarr);
  6647.  
  6648. /* Note: NetBSD and maybe other BSD44s have cfmakeraw() */
  6649. /* Maybe it would be simpler to use it... */
  6650.  
  6651.     ttpmsk = 0xff;
  6652. #ifdef NOLOCAL
  6653.     return(conbin((char)escchr));
  6654. #else
  6655.     if (ttyfd < 0) {            /* Not open. */
  6656.     if (ttchk() < 0)
  6657.       return(-1);
  6658.     else                /* But maybe something buffered. */
  6659.       return(0);
  6660.     }
  6661. #ifdef NETCMD
  6662.     if (ttpipe) return(0);
  6663. #endif /* NETCMD */
  6664. #ifdef NETPTY
  6665.     if (ttpty) return(0);
  6666. #endif /* NETPTY */
  6667. #ifdef NETCONN
  6668.     if (netconn) {
  6669. #ifdef TCPSOCKET
  6670. #ifdef TCP_NODELAY
  6671.         {
  6672.         extern int tcp_nodelay;
  6673.         if (ttnet == NET_TCPB) {
  6674.         if (nodelay_sav > -1) {
  6675.             no_delay(ttyfd,nodelay_sav);
  6676.             nodelay_sav = -1;
  6677.         }
  6678.         }
  6679.         }
  6680. #endif /* TCP_NODELAY */
  6681. #ifdef TN_COMPORT
  6682.         if (istncomport()) {
  6683.             int rc = -1;
  6684.             if (tvtflg != 0 && speed == ttspeed && flow == ttflow
  6685.                  /* && ttcarr == curcarr */ ) {
  6686.                 debug(F100,"ttvt modes already set, skipping...","",0);
  6687.                 return(0);            /* Already been called. */
  6688.             }
  6689.             if (flow != ttflow) {
  6690.                 if ((rc = tnsetflow(flow)) < 0)
  6691.           return(rc);
  6692.                 ttflow = flow;
  6693.             }
  6694.             if (speed != ttspeed) {
  6695.                 if (speed <= 0) 
  6696.           speed = tnc_get_baud();
  6697.                 else if ((rc = tnc_set_baud(speed)) < 0)
  6698.           return(rc);
  6699.                 ttspeed = speed;
  6700.             }
  6701.             tnc_set_datasize(8);
  6702.         tnc_set_stopsize(stopbits);
  6703.  
  6704. #ifdef HWPARITY
  6705.             if (hwparity) {
  6706.                 switch (hwparity) {
  6707.           case 'e':        /* Even */
  6708.                     debug(F100,"ttres 8 bits + even parity","",0);
  6709.                     tnc_set_parity(3);
  6710.                     break;
  6711.           case 'o':        /* Odd */
  6712.                     debug(F100,"ttres 8 bits + odd parity","",0);
  6713.                     tnc_set_parity(2);
  6714.                     break;
  6715.           case 'm':        /* Mark */
  6716.                     debug(F100,"ttres 8 bits + invalid parity: mark","",0);
  6717.                     tnc_set_parity(4);
  6718.                     break;
  6719.           case 's':        /* Space */
  6720.                     debug(F100,"ttres 8 bits + invalid parity: space","",0);
  6721.                     tnc_set_parity(5);
  6722.                     break;
  6723.                 }
  6724.             } else
  6725. #endif /* HWPARITY */
  6726.             {
  6727.                 tnc_set_parity(0);    /* None */
  6728.             }
  6729.             tvtflg = 1;
  6730.             return(0);
  6731.         }
  6732. #endif /* TN_COMPORT */
  6733. #endif /* TCPSOCKET */
  6734.     tvtflg = 1;            /* Network connections... */
  6735.     debug(F100,"ttvt network connection, skipping...","",0);
  6736.     return(0);            /* ... require no special setup */
  6737.     }
  6738. #endif /* NETCONN */
  6739.  
  6740.     if (tvtflg != 0 && speed == ttspeed && flow == ttflow
  6741.     /* && ttcarr == curcarr */ )
  6742.       {
  6743.       debug(F100,"ttvt modes already set, skipping...","",0);
  6744.       return(0);            /* Already been called. */
  6745.       }
  6746.  
  6747.     if (ttfdflg
  6748. #ifndef Plan9
  6749.     && !isatty(ttyfd)
  6750. #endif /* Plan9 */
  6751.     ) {
  6752.     debug(F100,"ttvt using external fd, skipping...","",0);
  6753.     return(0);
  6754.     }
  6755.  
  6756.     debug(F100,"ttvt setting modes...","",0);
  6757.  
  6758.     if (xlocal) {            /* For external lines... */
  6759.     s2 = (int) (speed / 10L);
  6760.     s = ttsspd(s2);            /* Check/set the speed */
  6761.     carrctl(&tttvt, flow != FLO_DIAL /* Do carrier control */
  6762.         && (ttcarr == CAR_ON || (ttcarr == CAR_AUT && ttmdm != 0)));
  6763.     } else
  6764.       s = s2 = -1;
  6765.  
  6766. #ifdef COHERENT
  6767. #define SVORPOSIX
  6768. #endif /* COHERENT */
  6769.  
  6770. #ifndef SVORPOSIX
  6771.     /* Berkeley, V7, etc */
  6772.     if (flow == FLO_RTSC ||        /* Hardware flow control */
  6773.     flow == FLO_DTRC ||
  6774.     flow == FLO_DTRT) {
  6775.     tthflow(flow, 1, &tttvt);
  6776.     debug(F100,"ttvt hard flow, TANDEM off","",0);
  6777.     tttvt.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6778.     } else if (flow == FLO_XONX) {    /* Xon/Xoff flow control */
  6779.     debug(F100,"ttvt TANDEM on","",0);
  6780.     tttvt.sg_flags |= TANDEM;    /* Ask for it. */
  6781.     tthflow(flow, 0, &tttvt);    /* Turn off hardware f/c */
  6782.     } else if (flow == FLO_NONE) {
  6783.     debug(F100,"ttvt no flow, TANDEM off, RAW on","",0);
  6784.     tttvt.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6785.     tthflow(flow, 0, &tttvt);    /* Turn off any hardware f/c too */
  6786.     tttvt.sg_flags |= RAW;        /* Enter raw mode */
  6787.     } else if (flow == FLO_KEEP) {    /* Keep device's original setting */
  6788.     debug(F100,"ttvt keeping original TANDEM","",0);
  6789.     tttvt.sg_flags &= ~TANDEM;
  6790.     tttvt.sg_flags |= (ttold.sg_flags & TANDEM);
  6791.     /* NOTE: We should also handle hardware flow control here! */
  6792.     }
  6793.     tttvt.sg_flags |= RAW;              /* Raw mode in all cases */
  6794. #ifdef TOWER1
  6795.     tttvt.sg_flags &= ~(ECHO|ANYP);     /* No echo or parity */
  6796. #else
  6797.     tttvt.sg_flags &= ~ECHO;            /* No echo */
  6798. #endif /* TOWER1 */
  6799.  
  6800. #ifdef BELLV10
  6801.     if (ioctl(ttyfd,TIOCSETP,&tttvt) < 0) /* Set the new modes */
  6802.       return(-1);
  6803. #else
  6804.     if (stty(ttyfd,&tttvt) < 0)        /* Set the new modes */
  6805.       return(-1);
  6806. #endif /* BELLV10 */
  6807.  
  6808. #else /* It is ATTSV or POSIX */
  6809.  
  6810.     if (flow == FLO_XONX) {        /* Software flow control */
  6811.     tttvt.c_iflag |= (IXON|IXOFF);    /* On if requested. */
  6812.     tthflow(flow, 0, &tttvt);    /* Turn off hardware f/c */
  6813.     debug(F100,"ttvt SVORPOSIX flow XON/XOFF","",0);
  6814.     } else if (flow == FLO_NONE) {    /* NONE */
  6815.     tttvt.c_iflag &= ~(IXON|IXOFF);    /* Turn off Xon/Xoff */
  6816.     tthflow(flow, 0, &tttvt);    /* Turn off hardware f/c */
  6817.     debug(F100,"ttvt SVORPOSIX flow NONE","",0);
  6818.     } else if (flow == FLO_KEEP) {
  6819.     tttvt.c_iflag &= ~(IXON|IXOFF);    /* Turn off Xon/Xoff flags */
  6820.     tttvt.c_iflag |= (ttold.c_iflag & (IXON|IXOFF)); /* OR in old ones */
  6821. #ifdef POSIX_CRTSCTS
  6822.         tttvt.c_cflag &= ~CRTSCTS;    /* Turn off RTS/CTS flag */
  6823.         tttvt.c_cflag |= (ttold.c_cflag & CRTSCTS); /* OR in old one */
  6824. #endif /* POSIX_CRTSCTS */
  6825.     debug(F100,"ttvt SVORPOSIX flow KEEP","",0);
  6826.     } else if (flow == FLO_RTSC ||    /* Hardware flow control */
  6827.            flow == FLO_DTRC ||
  6828.            flow == FLO_DTRT) {
  6829.     tttvt.c_iflag &= ~(IXON|IXOFF);    /* (196) */
  6830.     tthflow(flow, 1, &tttvt);
  6831.     debug(F100,"ttvt SVORPOSIX flow HARD","",0);
  6832.     }
  6833. #ifndef OXOS
  6834. #ifdef COHERENT
  6835.     tttvt.c_lflag &= ~(ISIG|ICANON|ECHO);
  6836. #else
  6837.     tttvt.c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
  6838. #endif /* COHERENT */
  6839. #ifdef QNX
  6840.     /* Needed for hwfc */
  6841.     if (flow == FLO_RTSC || flow == FLO_DTRC || flow == FLO_DTRT)
  6842.       tttvt.c_lflag |= IEXTEN;
  6843. #endif /* QNX */
  6844. #else /* OXOS */
  6845.     tttvt.c_lflag &= ~(ISIG|ICANON|ECHO);
  6846.     tttvt.c_cc[VDISCARD] = tttvt.c_cc[VLNEXT] = CDISABLE;
  6847. #endif /* OXOS */
  6848.  
  6849.     tttvt.c_iflag |= (IGNBRK|IGNPAR);
  6850.  
  6851. /* Stop bits */
  6852.  
  6853. #ifdef CSTOPB
  6854.     if (xlocal) {
  6855.     if (stopbits == 2) {
  6856.         tttvt.c_cflag |= CSTOPB;    /* 2 stop bits */
  6857.         debug(F100,"ttvt 2 stopbits","",0);
  6858.     } else if (stopbits == 1) {
  6859.         tttvt.c_cflag &= ~(CSTOPB);    /* 1 stop bit */
  6860.         debug(F100,"ttvt 1 stopbit","",0);
  6861.     }
  6862.     }
  6863. #endif /* CSTOPB */
  6864.  
  6865. /* Parity */
  6866.  
  6867. #ifdef HWPARITY
  6868.     if (hwparity && xlocal) {        /* Hardware parity */
  6869. #ifdef IGNPAR
  6870.     tttvt.c_cflag &= ~(IGNPAR);    /* Don't discard incoming bytes */
  6871.     debug(F100,"ttvt IGNPAR","",0); /* that have parity errors */
  6872. #endif /* IGNPAR */
  6873.     tttvt.c_cflag |= PARENB;    /* Enable parity */
  6874.     switch (hwparity) {
  6875.       case 'e':            /* Even */
  6876.         tttvt.c_cflag &= ~(PARODD);
  6877.         debug(F100,"ttvt 8 bits + even parity","",0);
  6878.         break;
  6879.       case 'o':            /* Odd */
  6880.         tttvt.c_cflag |= PARODD;
  6881.         debug(F100,"ttvt 8 bits + odd parity","",0);
  6882.         break;
  6883.       case 'm':            /* Mark */
  6884.       case 's':            /* Space */
  6885.         /* PAREXT is mentioned in SVID but the details are not given. */
  6886.         /* PAREXT is not included in POSIX ISO/IEC 9945-1. */
  6887.         debug(F100,"ttvt 8 bits + invalid parity","",0);
  6888.         break;
  6889.     }
  6890.     } else {                /* We handle parity ourselves */
  6891. #endif /* HWPARITY */
  6892.     tttvt.c_cflag &= ~(PARENB);    /* Don't enable parity */
  6893. #ifdef HWPARITY
  6894.     }
  6895. #endif /* HWPARITY */
  6896.  
  6897. #ifdef ATTSV
  6898. #ifdef BSD44
  6899.     /* Things not to do... */
  6900.     tttvt.c_iflag &= ~(INLCR|IGNCR|ICRNL|INPCK|ISTRIP|IXANY);
  6901. #else
  6902.     tttvt.c_iflag &= ~(INLCR|IGNCR|ICRNL|IUCLC|INPCK|ISTRIP|IXANY);
  6903. #endif /* BSD44 */
  6904. #else /* POSIX */
  6905.     tttvt.c_iflag &= ~(INLCR|IGNCR|ICRNL|INPCK|ISTRIP);
  6906. #endif /* ATTSV */
  6907.     tttvt.c_cflag &= ~(CSIZE);        /* Zero out the char size field */
  6908.     tttvt.c_cflag |= (CS8|CREAD|HUPCL);    /* Char size 8, enable receiver, hup */
  6909.     tttvt.c_oflag &= ~OPOST;        /* Don't postprocess output */
  6910. #ifndef VEOF /* DGUX termio has VEOF at entry 4, see comment above */
  6911.     tttvt.c_cc[4] = 1;
  6912. #else
  6913. #ifndef OXOS
  6914. #ifdef VMIN
  6915.     tttvt.c_cc[VMIN] = 1;
  6916. #endif /* VMIN */
  6917. #else /* OXOS */
  6918.     tttvt.c_min = 1;
  6919. #endif /* OXOS */
  6920. #endif /* VEOF */
  6921. #ifndef VEOL    /* DGUX termio has VEOL at entry 5, see comment above */
  6922.     tttvt.c_cc[5] = 0;
  6923. #else
  6924. #ifndef OXOS
  6925. #ifdef VTIME
  6926.     tttvt.c_cc[VTIME] = 0;
  6927. #endif /* VTIME */
  6928. #else /* OXOS */
  6929.     tttvt.c_time = 0;
  6930. #endif /* OXOS */
  6931. #endif /* VEOL */
  6932.  
  6933. #ifdef Plan9
  6934.     if (p9ttyparity('n') < 0)
  6935.       return -1;
  6936. #else
  6937. #ifdef BSD44ORPOSIX
  6938.     errno = 0;
  6939. #ifdef BEOSORBEBOX
  6940.     tttvt.c_cc[VMIN] = 0;        /* DR7 can only poll. */
  6941. #endif /* BEOSORBEBOX */
  6942.     x = tcsetattr(ttyfd,TCSADRAIN,&tttvt);
  6943.     debug(F101,"ttvt BSD44ORPOSIX tcsetattr","",x);
  6944.     if (x < 0) {
  6945.     debug(F101,"ttvt BSD44ORPOSIX tcsetattr errno","",errno);
  6946.     return(-1);
  6947.     }
  6948. #else /* ATTSV */
  6949.     x = ioctl(ttyfd,TCSETAW,&tttvt);
  6950.     debug(F101,"ttvt ATTSV ioctl TCSETAW","",x);
  6951.     if (x < 0) {            /* set new modes . */
  6952.     debug(F101,"ttvt ATTSV ioctl TCSETAW errno","",errno);
  6953.     return(-1);    
  6954.     }
  6955. #endif /* BSD44ORPOSIX */
  6956. #endif /* Plan9 */
  6957. #endif /* ATTSV */
  6958.  
  6959.     ttspeed = speed;            /* Done, remember how we were */
  6960.     ttflow = flow;            /* called, so we can decide how to */
  6961.     tvtflg = 1;                /* respond next time. */
  6962.     debug(F100,"ttvt ok","",0);
  6963.     return(0);
  6964.  
  6965. #ifdef COHERENT
  6966. #undef SVORPOSIX
  6967. #endif /* COHERENT */
  6968.  
  6969. #endif /* NOLOCAL */
  6970. }
  6971.  
  6972. #ifndef NOLOCAL
  6973.  
  6974. /* Serial speed department . . . */
  6975.  
  6976. /*
  6977.   SCO OSR5.0.x might or might not support high speeds.  Sometimes they are not
  6978.   defined in the header files but they are supported (e.g. when building with
  6979.   UDK compiler rather than /bin/cc), sometimes vice versa.  Even though 5.0.4
  6980.   was the first release that came with high serial speeds standard, releases
  6981.   back to 5.0.0 could use them if certain patches (or "supplements") were
  6982.   applied to the SIO driver.  Plus a lot of SCO installations run third-party
  6983.   drivers.
  6984. */
  6985. #ifdef CK_SCOV5
  6986. #ifndef B38400
  6987. #define    B38400    0000017
  6988. #endif /* B38400 */
  6989. #ifndef B57600
  6990. #define    B57600    0000021
  6991. #endif /* B57600 */
  6992. #ifndef B76800
  6993. #define    B76800    0000022
  6994. #endif /* B76800 */
  6995. #ifndef B115200
  6996. #define    B115200    0000023
  6997. #endif /* B115200 */
  6998. #ifndef B230400
  6999. #define    B230400    0000024
  7000. #endif /* B230400 */
  7001. #ifndef B460800
  7002. #define    B460800    0000025
  7003. #endif /* B460800 */
  7004. #ifndef B921600
  7005. #define    B921600    0000026
  7006. #endif /* B921600 */
  7007. #endif /* CK_SCOV5 */
  7008. /*
  7009.   Plan 9's native speed setting interface lets you set anything you like,
  7010.   but will fail if the hardware doesn't like it, so we allow all the common
  7011.   speeds.
  7012. */
  7013. #ifdef Plan9
  7014. #ifndef B50
  7015. #define B50 50
  7016. #endif /* B50 */
  7017. #ifndef B75
  7018. #define B75 75
  7019. #endif /* B75 */
  7020. #ifndef B110
  7021. #define B110 110
  7022. #endif /* B110 */
  7023. #ifndef B134
  7024. #define B134 134
  7025. #endif /* B134 */
  7026. #ifndef B200
  7027. #define B200 200
  7028. #endif /* B200 */
  7029. #ifndef B300
  7030. #define B300 300
  7031. #endif /* B300 */
  7032. #ifndef B1200
  7033. #define B1200 1200
  7034. #endif /* B1200 */
  7035. #ifndef B1800
  7036. #define B1800 1800
  7037. #endif /* B1800 */
  7038. #ifndef B2400
  7039. #define B2400 2400
  7040. #endif /* B2400 */
  7041. #ifndef B4800
  7042. #define B4800 4800
  7043. #endif /* B4800 */
  7044. #ifndef B9600
  7045. #define B9600 9600
  7046. #endif /* B9600 */
  7047. #ifndef B14400
  7048. #define B14400 14400
  7049. #endif /* B14400 */
  7050. #ifndef B19200
  7051. #define B19200 19200
  7052. #endif /* B19200 */
  7053. #ifndef B28800
  7054. #define B28800 28800
  7055. #endif /* B28800 */
  7056. #ifndef B38400
  7057. #define B38400 38400
  7058. #endif /* B38400 */
  7059. #ifndef B57600
  7060. #define B57600 57600
  7061. #endif /* B57600 */
  7062. #ifndef B76800
  7063. #define B76800 76800
  7064. #endif /* B76800 */
  7065. #ifndef B115200
  7066. #define B115200 115200
  7067. #endif /* B115200 */
  7068. #ifndef B230400
  7069. #define B230400 230400
  7070. #endif /* B230400 */
  7071. #ifndef B460800
  7072. #define B460800 460800
  7073. #endif /* B460800 */
  7074. #ifndef B921600
  7075. #define B921600 921600
  7076. #endif /* B921600 */
  7077. #endif /* Plan9 */
  7078.  
  7079. /*  T T S S P D  --  Checks and sets transmission rate.  */
  7080.  
  7081. /*  Call with speed in characters (not bits!) per second. */
  7082. /*  Returns -1 on failure, 0 if it did nothing, 1 if it changed the speed. */
  7083.  
  7084. #ifdef USETCSETSPEED
  7085. /*
  7086.   The tcsetspeed() / tcgetspeed() interface lets you pass any number at all
  7087.   to be used as a speed to be set, rather than forcing a choice from a
  7088.   predefined list.  It seems to be peculiar to UnixWare 7.
  7089.  
  7090.   These are the function codes to be passed to tc[gs]etspeed(),
  7091.   but for some reason they don't seem to be picked up from termios.h.
  7092. */
  7093. #ifndef TCS_ALL
  7094. #define TCS_ALL 0
  7095. #endif /* TCS_ALL */
  7096. #ifndef TCS_IN
  7097. #define TCS_IN 1
  7098. #endif /* TCS_IN */
  7099. #ifndef TCS_OUT
  7100. #define TCS_OUT 2
  7101. #endif /* TCS_OUT */
  7102. #endif /* USETCSETSPEED */
  7103.  
  7104. int
  7105. ttsspd(cps) int cps; {
  7106.     int x;
  7107. #ifdef POSIX
  7108. /* Watch out, speed_t should be unsigned, so don't compare with -1, etc... */
  7109.     speed_t
  7110. #else
  7111.     int
  7112. #endif /* POSIX */
  7113.       s, s2;
  7114.     int ok = 1;                /* Speed check result, assume ok */
  7115.  
  7116. #ifdef OLINUXHISPEED
  7117.     unsigned int spd_flags = 0;
  7118.     struct serial_struct serinfo;
  7119. #endif /* OLINUXHISPEED */
  7120.  
  7121.     debug(F101,"ttsspd cps","",cps);
  7122.     debug(F101,"ttsspd ttyfd","",ttyfd);
  7123.     debug(F101,"ttsspd xlocal","",xlocal);
  7124.  
  7125.     if (ttyfd < 0 || xlocal == 0)    /* Don't set speed on console */
  7126.       return(0);
  7127.  
  7128. #ifdef    NETCONN
  7129.     if (netconn) {
  7130. #ifdef TN_COMPORT
  7131.         if (istncomport())
  7132.       return(tnc_set_baud(cps * 10));
  7133.         else
  7134. #endif /* TN_COMPORT */
  7135.     return(0);
  7136.   }
  7137. #endif    /* NETCONN */
  7138. #ifdef NETCMD
  7139.     if (ttpipe) return(0);
  7140. #endif /* NETCMD */
  7141. #ifdef NETPTY
  7142.     if (ttpty) return(0);
  7143. #endif /* NETPTY */
  7144.  
  7145.     if (cps < 0) return(-1);
  7146.     s = s2 = 0;                /* NB: s and s2 might be unsigned */
  7147.  
  7148. #ifdef USETCSETSPEED
  7149.  
  7150.     s = cps * 10L;
  7151.  
  7152.     x = tcgetattr(ttyfd,&ttcur);    /* Get current speed */
  7153.     debug(F101,"ttsspd tcgetattr","",x);
  7154.     if (x < 0)
  7155.       return(-1);
  7156.     debug(F101,"ttsspd TCSETSPEED speed","",s);
  7157.  
  7158.     errno = 0;
  7159.     if (s == 8880L) {            /* 75/1200 split speed requested */
  7160.     tcsetspeed(TCS_IN, &ttcur, 1200L);
  7161.     tcsetspeed(TCS_OUT, &ttcur, 75L);
  7162.     } else
  7163.       tcsetspeed(TCS_ALL, &ttcur, s);    /* Put new speed in structs */
  7164. #ifdef DEBUG
  7165.     if (errno & deblog) {
  7166.     debug(F101,"ttsspd TCSETSPEED errno","",errno);
  7167.     }
  7168. #endif /* DEBUG */
  7169.  
  7170. #ifdef COMMENT
  7171.     tcsetspeed(TCS_ALL, &ttraw, s);
  7172.     tcsetspeed(TCS_ALL, &tttvt, s);
  7173.     tcsetspeed(TCS_ALL, &ttold, s);
  7174. #else
  7175.     if (s == 8880L) {            /* 75/1200 split speed requested */
  7176.     tcsetspeed(TCS_IN, &ttraw, 1200L);
  7177.     tcsetspeed(TCS_OUT, &ttraw, 75L);
  7178.     tcsetspeed(TCS_IN, &tttvt, 1200L);
  7179.     tcsetspeed(TCS_OUT, &tttvt, 75L);
  7180.     tcsetspeed(TCS_IN, &ttold, 1200L);
  7181.     tcsetspeed(TCS_OUT, &ttold, 75L);
  7182.     } else {
  7183.     tcsetspeed(TCS_ALL, &ttraw, s);
  7184.     tcsetspeed(TCS_ALL, &tttvt, s);
  7185.     tcsetspeed(TCS_ALL, &ttold, s);
  7186.     }
  7187. #endif /* COMMENT */
  7188.  
  7189.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur); /* Set the speed */
  7190.     debug(F101,"ttsspd tcsetattr","",x);
  7191.     if (x < 0)
  7192.       return(-1);
  7193.  
  7194. #else  /* Not USETCSETSPEED */
  7195.  
  7196. #ifdef MINIX2        /* Hack alert */
  7197. #define MINIX        /* Use pre-2.0 speed selection for Minix 2.0 as well */
  7198. #endif /* MINIX2 */
  7199.  
  7200.     /* First check that the given speed is valid. */
  7201.  
  7202.     switch (cps) {
  7203. #ifndef MINIX
  7204.       case 0:   s = B0;    break;
  7205.       case 5:   s = B50;   break;
  7206.       case 7:   s = B75;   break;
  7207. #endif /* MINIX */
  7208.       case 11:  s = B110;  break;
  7209. #ifndef MINIX
  7210.       case 13:  s = B134;  break;
  7211.       case 15:  s = B150;  break;
  7212.       case 20:  s = B200;  break;
  7213. #endif /* MINIX */
  7214.       case 30:  s = B300;  break;
  7215. #ifndef MINIX
  7216.       case 60:  s = B600;  break;
  7217. #endif /* MINIX */
  7218.       case 120: s = B1200; break;
  7219. #ifndef MINIX
  7220.       case 180: s = B1800; break;
  7221. #endif /* MINIX */
  7222.       case 240: s = B2400; break;
  7223.       case 480: s = B4800; break;
  7224. #ifndef MINIX
  7225.       case 888: s = B75; s2 = B1200; break; /* 888 = 75/1200 split speed */
  7226. #endif /* MINIX */
  7227. #ifdef B7200
  7228.       case 720: s = B7200; break;
  7229. #endif /* B7200 */
  7230.       case 960: s = B9600; break;
  7231. #ifdef B14400
  7232.       case 1440: s = B14400; break;
  7233. #endif /* B14400 */
  7234. #ifdef B19200
  7235.       case 1920: s = B19200; break;
  7236. #else
  7237. #ifdef EXTA
  7238.       case 1920: s = EXTA; break;
  7239. #endif /* EXTA */
  7240. #endif /* B19200 */
  7241. #ifdef B28800
  7242.       case 2880: s = B28800; break;
  7243. #endif /* B28800 */
  7244. #ifdef B38400
  7245.       case 3840: s = B38400;
  7246. #ifdef OLINUXHISPEED
  7247.         spd_flags = ~ASYNC_SPD_MASK;    /* Nonzero, but zero flags */
  7248. #endif /* OLINUXHISPEED */
  7249.     break;
  7250. #else /* B38400 not defined... */
  7251. #ifdef EXTB
  7252.       case 3840: s = EXTB; break;
  7253. #endif /* EXTB */
  7254. #endif /* B38400 */
  7255.  
  7256. #ifdef HPUX
  7257. #ifdef _B57600
  7258.       case 5760: s = _B57600; break;
  7259. #endif /* _B57600 */
  7260. #ifdef _B115200
  7261.       case 11520: s = _B115200; break;
  7262. #endif /* _B115200 */
  7263. #else
  7264. #ifdef OLINUXHISPEED
  7265. /*
  7266.   This bit from <carlo@sg.tn.tudelft.nl>:
  7267.   "Only note to make is maybe this: When the ASYNC_SPD_CUST flags are set then
  7268.   setting the speed to 38400 will set the custom speed (and ttgspd returns
  7269.   38400), but speeds 57600 and 115200 won't work any more because I didn't
  7270.   want to mess up the speed flags when someone is doing sophisticated stuff
  7271.   like custom speeds..."
  7272. */
  7273.       case 5760: s = B38400; spd_flags = ASYNC_SPD_HI; break;
  7274.       case 11520: s = B38400; spd_flags = ASYNC_SPD_VHI; break;
  7275. #else
  7276. #ifdef B57600
  7277.       case 5760: s = B57600; break;
  7278. #endif /* B57600 */
  7279. #ifdef B76800
  7280.       case 7680: s = B76800; break;
  7281. #endif /* B76800 */
  7282. #ifdef B115200
  7283.       case 11520: s = B115200; break;
  7284. #endif /* B115200 */
  7285. #endif /* OLINUXHISPEED */
  7286. #ifdef B153600
  7287.       case 15360: s = B153600; break;
  7288. #endif /* B153600 */
  7289. #ifdef B230400
  7290.       case 23040: s = B230400; break;
  7291. #endif /* B230400 */
  7292. #ifdef B307200
  7293.       case 30720: s = B307200; break;
  7294. #endif /* B307200 */
  7295. #ifdef B460800
  7296.       case 46080: s = B460800; break;
  7297. #endif /* 460800 */
  7298. #ifdef B921600
  7299.       case 92160: s = B921600; break;
  7300. #endif /* B921600 */
  7301. #endif /* HPUX */
  7302.       default:
  7303.     ok = 0;                /* Good speed not found, so not ok */
  7304.     break;
  7305.     }
  7306.     debug(F101,"ttsspd ok","",ok);
  7307.     debug(F101,"ttsspd s","",s);
  7308.  
  7309.     if (!ok) {
  7310.     debug(F100,"ttsspd fails","",0);
  7311.     return(-1);
  7312.     } else {
  7313.     if (!s2) s2 = s;        /* Set input speed */
  7314. #ifdef Plan9
  7315.     if (p9ttsspd(cps) < 0)
  7316.       return(-1);
  7317. #else
  7318. #ifdef BSD44ORPOSIX
  7319.     x = tcgetattr(ttyfd,&ttcur);    /* Get current speed */
  7320.     debug(F101,"ttsspd tcgetattr","",x);
  7321.     if (x < 0)
  7322.       return(-1);
  7323. #ifdef OLINUXHISPEED
  7324.     debug(F101,"ttsspd spd_flags","",spd_flags);
  7325.     if (spd_flags && spd_flags != ASYNC_SPD_CUST) {
  7326.         if (ioctl(ttyfd, TIOCGSERIAL, &serinfo) < 0) {
  7327.         debug(F100,"ttsspd: TIOCGSERIAL failed","",0);
  7328.         return(-1);
  7329.         } else debug(F100,"ttsspd: TIOCGSERIAL ok","",0);
  7330.         serinfo.flags &= ~ASYNC_SPD_MASK;
  7331.         serinfo.flags |= (spd_flags & ASYNC_SPD_MASK);
  7332.         if (ioctl(ttyfd, TIOCSSERIAL, &serinfo) < 0)
  7333.           return(-1);
  7334.     }
  7335. #endif /* OLINUXHISPEED */
  7336.     cfsetospeed(&ttcur,s);
  7337.     cfsetispeed(&ttcur,s2);
  7338.     cfsetospeed(&ttraw,s);
  7339.     cfsetispeed(&ttraw,s2);
  7340.     cfsetospeed(&tttvt,s);
  7341.     cfsetispeed(&tttvt,s2);
  7342.     cfsetospeed(&ttold,s);
  7343.     cfsetispeed(&ttold,s2);
  7344.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  7345.     debug(F101,"ttsspd tcsetattr","",x);
  7346.     if (x < 0) return(-1);
  7347. #else
  7348. #ifdef ATTSV
  7349.     if (cps == 888) return(-1);    /* No split speeds, sorry. */
  7350.     x = ioctl(ttyfd,TCGETA,&ttcur);
  7351.     debug(F101,"ttsspd TCGETA ioctl","",x);
  7352.     if (x < 0) return(-1);
  7353.     ttcur.c_cflag &= ~CBAUD;
  7354.     ttcur.c_cflag |= s;
  7355.     tttvt.c_cflag &= ~CBAUD;
  7356.     tttvt.c_cflag |= s;
  7357.     ttraw.c_cflag &= ~CBAUD;
  7358.     ttraw.c_cflag |= s;
  7359.     ttold.c_cflag &= ~CBAUD;
  7360.     ttold.c_cflag |= s;
  7361.     x = ioctl(ttyfd,TCSETAW,&ttcur);
  7362.     debug(F101,"ttsspd TCSETAW ioctl","",x);
  7363.     if (x < 0) return(-1);
  7364. #else
  7365. #ifdef BELLV10
  7366.     x = ioctl(ttyfd,TIOCGDEV,&tdcur);
  7367.     debug(F101,"ttsspd TIOCGDEV ioctl","",x);
  7368.     if (x < 0) return(-1);
  7369.     tdcur.ispeed = s2;
  7370.     tdcur.ospeed = s;
  7371.     errno = 0;
  7372.     ok = ioctl(ttyfd,TIOCSDEV,&tdcur);
  7373.     debug(F101,"ttsspd BELLV10 ioctl","",ok);
  7374.     if (ok < 0) {
  7375.         perror(ttnmsv);
  7376.         debug(F101,"ttsspd BELLV10 errno","",ok);
  7377.         return(-1);
  7378.     }
  7379. #else
  7380.     x = gtty(ttyfd,&ttcur);
  7381.     debug(F101,"ttsspd gtty","",x);
  7382.     if (x < 0) return(-1);
  7383.     ttcur.sg_ospeed = s; ttcur.sg_ispeed = s2;
  7384.     tttvt.sg_ospeed = s; tttvt.sg_ispeed = s2;
  7385.     ttraw.sg_ospeed = s; ttraw.sg_ispeed = s2;
  7386.     ttold.sg_ospeed = s; ttold.sg_ispeed = s2;
  7387.     x = stty(ttyfd,&ttcur);
  7388.     debug(F101,"ttsspd stty","",x);
  7389.     if (x < 0) return(-1);
  7390. #endif /* BELLV10 */
  7391. #endif /* ATTSV */
  7392. #endif /* BSD44ORPOSIX */
  7393. #endif /* Plan9 */
  7394.     }
  7395.     return(1);                /* Return 1 = success. */
  7396. #endif /* USETCSETSPEED */
  7397. }
  7398.  
  7399. #endif /* NOLOCAL */
  7400.  
  7401. /* C O N G S P D  -  Get speed of console terminal  */
  7402.  
  7403. long
  7404. congspd() {
  7405. /*
  7406.   This is a disgusting hack.  The right way to do this would be to pass an
  7407.   argument to ttgspd(), but then we'd need to change the Kermit API and
  7408.   all of the ck?tio.c modules.  (Currently used only for rlogin.)
  7409. */
  7410.     int t1;
  7411.     long spd;
  7412. #ifdef NETCONN
  7413.     int t2 = netconn;
  7414.     netconn = 0;
  7415. #endif /* NETCONN */
  7416.     t1 = ttyfd;
  7417.     ttyfd = -1;
  7418.     spd = ttgspd();
  7419.     debug(F101,"congspd","",spd);
  7420. #ifdef NETCONN
  7421.     netconn = t2;
  7422. #endif /* NETCONN */
  7423.     ttyfd = t1;
  7424.     return(spd);
  7425. }
  7426.  
  7427. /*  T T S P D L I S T  -- Get list of serial speeds allowed on this platform */
  7428.  
  7429. #define NSPDLIST 64
  7430. static long spdlist[NSPDLIST];
  7431. /*
  7432.   As written, this picks up the speeds known at compile time, and thus
  7433.   apply to the system where C-Kermit was built, rather than to the one where
  7434.   it is running.  Suggestions for improvement are always welcome.
  7435. */
  7436. long *
  7437. ttspdlist() {
  7438.     int i;
  7439.     for (i = 0; i < NSPDLIST; i++)    /* Initialize the list */
  7440.       spdlist[i] = -1L;
  7441.     i = 1;
  7442.  
  7443. #ifdef USETCSETSPEED            /* No way to find out what's legal */
  7444.     debug(F100,"ttspdlist USETCSETSPEED","",0);
  7445.     spdlist[i++] = 50L;
  7446. #ifndef UW7
  7447.     spdlist[i++] = 75L;
  7448. #endif /* UW7 */
  7449.     spdlist[i++] = 110L;
  7450. #ifndef UW7
  7451.     spdlist[i++] = 134L;
  7452. #endif /* UW7 */
  7453.     spdlist[i++] = 150L;
  7454.     spdlist[i++] = 200L;
  7455.     spdlist[i++] = 300L;
  7456.     spdlist[i++] = 600L;
  7457.     spdlist[i++] = 1200L;
  7458.     spdlist[i++] = 1800L;
  7459.     spdlist[i++] = 2400L;
  7460.     spdlist[i++] = 4800L;
  7461.     spdlist[i++] = 8880L;
  7462.     spdlist[i++] = 9600L;
  7463.     spdlist[i++] = 14400L;
  7464.     spdlist[i++] = 19200L;
  7465.     spdlist[i++] = 28800L;
  7466. #ifndef UW7
  7467.     spdlist[i++] = 33600L;
  7468. #endif /* UW7 */
  7469.     spdlist[i++] = 38400L;
  7470.     spdlist[i++] = 57600L;
  7471.     spdlist[i++] = 76800L;
  7472.     spdlist[i++] = 115200L;
  7473. #ifndef UW7
  7474.     spdlist[i++] = 153600L;
  7475.     spdlist[i++] = 230400L;
  7476.     spdlist[i++] = 307200L;
  7477.     spdlist[i++] = 460800L;
  7478.     spdlist[i++] = 921600L;
  7479. #endif /* UW7 */
  7480.  
  7481. #else  /* USETCSETSPEED */
  7482.  
  7483.     debug(F100,"ttspdlist no USETCSETSPEED","",0);
  7484.  
  7485. #ifdef B50
  7486.     debug(F101,"ttspdlist B50","",B50);
  7487.     spdlist[i++] = 50L;
  7488. #endif /* B50 */
  7489. #ifdef B75
  7490.     debug(F101,"ttspdlist B75","",B75);
  7491.     spdlist[i++] = 75L;
  7492. #endif /* B75 */
  7493. #ifdef B110
  7494.     debug(F101,"ttspdlist B110","",B110);
  7495.     spdlist[i++] = 110L;
  7496. #endif /* B110 */
  7497. #ifdef B134
  7498.     debug(F101,"ttspdlist B134","",B134);
  7499.     spdlist[i++] = 134L;
  7500. #endif /* B134 */
  7501. #ifdef B150
  7502.     debug(F101,"ttspdlist B150","",B150);
  7503.     spdlist[i++] = 150L;
  7504. #endif /* B150 */
  7505. #ifdef B200
  7506.     debug(F101,"ttspdlist B200","",B200);
  7507.     spdlist[i++] = 200L;
  7508. #endif /* B200 */
  7509. #ifdef B300
  7510.     debug(F101,"ttspdlist B300","",B300);
  7511.     spdlist[i++] = 300L;
  7512. #endif /* B300 */
  7513. #ifdef B600
  7514.     debug(F101,"ttspdlist B600","",B600);
  7515.     spdlist[i++] = 600L;
  7516. #endif /* B600 */
  7517. #ifdef B1200
  7518.     debug(F101,"ttspdlist B1200","",B1200);
  7519.     spdlist[i++] = 1200L;
  7520. #endif /* B1200 */
  7521. #ifdef B1800
  7522.     debug(F101,"ttspdlist B1800","",B1800);
  7523.     spdlist[i++] = 1800L;
  7524. #endif /* B1800 */
  7525. #ifdef B2400
  7526.     debug(F101,"ttspdlist B2400","",B2400);
  7527.     spdlist[i++] = 2400L;
  7528. #endif /* B2400 */
  7529. #ifdef B4800
  7530.     debug(F101,"ttspdlist B4800","",B4800);
  7531.     spdlist[i++] = 4800L;
  7532. #endif /* B4800 */
  7533. #ifdef B9600
  7534.     debug(F101,"ttspdlist B9600","",B9600);
  7535.     spdlist[i++] = 9600L;
  7536. #endif /* B9600 */
  7537. #ifdef B14400
  7538.     debug(F101,"ttspdlist B14400","",B14400);
  7539.     spdlist[i++] = 14400L;
  7540. #endif /* B14400 */
  7541. #ifdef B19200
  7542.     debug(F101,"ttspdlist B19200","",B19200);
  7543.     spdlist[i++] = 19200L;
  7544. #else
  7545. #ifdef EXTA
  7546.     debug(F101,"ttspdlist EXTA","",EXTA);
  7547.     spdlist[i++] = 19200L;
  7548. #endif /* EXTA */
  7549. #endif /* B19200 */
  7550. #ifdef B28800
  7551.     debug(F101,"ttspdlist B28800","",B28800);
  7552.     spdlist[i++] = 28800L;
  7553. #endif /* B28800 */
  7554. #ifdef B33600
  7555.     debug(F101,"ttspdlist B33600","",B33600);
  7556.     spdlist[i++] = 33600L;
  7557. #endif /* B33600 */
  7558. #ifdef B38400
  7559.     debug(F101,"ttspdlist B38400","",B38400);
  7560.     spdlist[i++] = 38400L;
  7561. #else
  7562. #ifdef EXTB
  7563.     debug(F101,"ttspdlist EXTB","",EXTB);
  7564.     spdlist[i++] = 38400L;
  7565. #endif /* EXTB */
  7566. #endif /* B38400 */
  7567. #ifdef _B57600
  7568.     debug(F101,"ttspdlist _B57600","",_B57600);
  7569.     spdlist[i++] = 57600L;
  7570. #else
  7571. #ifdef B57600
  7572.     debug(F101,"ttspdlist B57600","",B57600);
  7573.     spdlist[i++] = 57600L;
  7574. #endif /* B57600 */
  7575. #endif /* _B57600 */
  7576. #ifdef B76800
  7577.     debug(F101,"ttspdlist B76800","",B76800);
  7578.     spdlist[i++] = 76800L;
  7579. #endif /* B76800 */
  7580. #ifdef _B115200
  7581.     debug(F101,"ttspdlist _B115200","",_B115200);
  7582.     spdlist[i++] = 115200L;
  7583. #else
  7584. #ifdef B115200
  7585.     debug(F101,"ttspdlist B115200","",B115200);
  7586.     spdlist[i++] = 115200L;
  7587. #endif /* B115200 */
  7588. #endif /* _B115200 */
  7589. #ifdef B153600
  7590.     debug(F101,"ttspdlist B153600","",B153600);
  7591.     spdlist[i++] = 153600L;
  7592. #endif /* B153600 */
  7593. #ifdef B230400
  7594.     debug(F101,"ttspdlist B230400","",B230400);
  7595.     spdlist[i++] = 230400L;
  7596. #endif /* B230400 */
  7597. #ifdef B307200
  7598.     debug(F101,"ttspdlist B307200","",B307200);
  7599.     spdlist[i++] = 307200L;
  7600. #endif /* B307200 */
  7601. #ifdef B460800
  7602.     debug(F101,"ttspdlist B460800","",B460800);
  7603.     spdlist[i++] = 460800L;
  7604. #endif /* B460800 */
  7605. #ifdef B921600
  7606.     debug(F101,"ttspdlist B921600","",B921600);
  7607.     spdlist[i++] = 921600L;
  7608. #endif /* B921600 */
  7609. #endif /* USETCSETSPEED */
  7610.     spdlist[0] = i - 1;            /* Return count in 0th element */
  7611.     debug(F111,"ttspdlist spdlist","0",spdlist[0]);
  7612.     return((long *)spdlist);
  7613. }
  7614.  
  7615. /* T T G S P D  -  Get speed of currently selected tty line  */
  7616.  
  7617. /*
  7618.   Unreliable.  After SET LINE, it returns an actual speed, but not necessarily
  7619.   the real speed.  On some systems, it returns the line's nominal speed, from
  7620.   /etc/ttytab.  Even if you SET SPEED to something else, this function might
  7621.   not notice.
  7622. */
  7623. long
  7624. ttgspd() {                /* Get current serial device speed */
  7625. #ifdef NOLOCAL
  7626.     return(-1L);
  7627. #else
  7628. #ifdef POSIX
  7629.     speed_t                /* Should be unsigned */
  7630. #else
  7631.     int                    /* Isn't unsigned */
  7632. #endif /* POSIX */
  7633.       s;
  7634.     int x;
  7635.     long ss;
  7636. #ifdef OLINUXHISPEED
  7637.     unsigned int spd_flags = 0;
  7638.     struct serial_struct serinfo;
  7639. #endif /* OLINUXHISPEED */
  7640.  
  7641. #ifdef NETCONN
  7642.     if (netconn) {
  7643. #ifdef TN_COMPORT
  7644.     if (istncomport())
  7645.       return(tnc_get_baud());
  7646.     else
  7647. #endif /* TN_COMPORT */
  7648.       return(-1);            /* -1 if network connection */
  7649.     }
  7650. #endif /* NETCONN */
  7651. #ifdef NETCMD
  7652.     if (ttpipe) return(-1);
  7653. #endif /* NETCMD */
  7654. #ifdef NETPTY
  7655.     if (ttpty) return(-1);
  7656. #endif /* NETPTY */
  7657.  
  7658.     debug(F101,"ttgspd ttyfd","",ttyfd);
  7659.  
  7660. #ifdef USETCSETSPEED
  7661.  
  7662.     x = tcgetattr(ttyfd,&ttcur);    /* Get current speed */
  7663.     debug(F101,"ttgspd tcgetattr","",x);
  7664.     if (x < 0)
  7665.       return(-1);
  7666.     errno = 0;
  7667.     s = tcgetspeed(TCS_ALL, &ttcur);
  7668.     debug(F101,"ttsspd TCGETSPEED speed","",s);
  7669.     if (s == 0) {
  7670.     long s1, s2;
  7671.     s1 = tcgetspeed(TCS_IN, &ttcur);
  7672.     s2 = tcgetspeed(TCS_OUT, &ttcur);
  7673.     if (s1 == 1200L && s2 == 75L)
  7674.       return(8880L);
  7675.     }
  7676. #ifdef DEBUG
  7677.     if (errno & deblog) {
  7678.     debug(F101,"ttsspd TCGETSPEED errno","",errno);
  7679.     }
  7680. #endif /* DEBUG */
  7681.     return(s);
  7682.  
  7683. #else  /* Not USETCSETSPEED */
  7684.  
  7685. #ifdef Plan9
  7686.     if (ttyfd < 0)
  7687.       ss = -1;
  7688.     else
  7689.       ss = ttylastspeed;
  7690. #else
  7691. #ifdef OLINUXHISPEED
  7692.     debug(F100,"ttgspd Linux OLINUXHISPEED","",0);
  7693. #endif /* OLINUXHISPEED */
  7694.  
  7695.     if (ttyfd < 0) {
  7696. #ifdef BSD44ORPOSIX
  7697.     s = cfgetospeed(&ccold);
  7698.     debug(F101,"ttgspd cfgetospeed 1 POSIX","",s);
  7699. #else
  7700. #ifdef ATTSV
  7701.     s = ccold.c_cflag & CBAUD;
  7702.     debug(F101,"ttgspd c_cflag CBAUD 1 ATTSV","",s);
  7703. #else
  7704.     s = ccold.sg_ospeed;        /* (obtained by congm()) */
  7705.     debug(F101,"ttgspd sg_ospeed 1","",s);
  7706. #endif /* ATTSV */
  7707. #endif /* BSD44POSIX */
  7708.  
  7709.     } else {
  7710. #ifdef BSD44ORPOSIX
  7711.     if (tcgetattr(ttyfd,&ttcur) < 0) return(-1);
  7712.     s = cfgetospeed(&ttcur);
  7713.     debug(F101,"ttgspd cfgetospeed 2 BSDORPOSIX","",s);
  7714. #ifdef OLINUXHISPEED
  7715.     if (ioctl(ttyfd,TIOCGSERIAL,&serinfo) > -1)
  7716.       spd_flags = serinfo.flags & ASYNC_SPD_MASK;
  7717.     debug(F101,"ttgspd spd_flags","",spd_flags);
  7718. #endif /* OLINUXHISPEED */
  7719. #else
  7720. #ifdef ATTSV
  7721.     x = ioctl(ttyfd,TCGETA,&ttcur);
  7722.     debug(F101,"ttgspd ioctl 2 ATTSV x","",x);
  7723.     debug(F101,"ttgspd ioctl 2 ATTSV errno","",errno);
  7724.     if (x < 0) return(-1);
  7725.     s = ttcur.c_cflag & CBAUD;
  7726.     debug(F101,"ttgspd ioctl 2 ATTSV speed","",s);
  7727. #else
  7728. #ifdef BELLV10
  7729.     x = ioctl(ttyfd,TIOCGDEV,&tdcur);
  7730.     debug(F101,"ttgspd ioctl 2 BELLV10 x","",x);
  7731.     if (x < 0) return(-1);
  7732.     s = tdcur.ospeed;
  7733.     debug(F101,"ttgspd ioctl 2 BELLV10 speed","",s);
  7734. #else
  7735.     x = gtty(ttyfd,&ttcur);
  7736.     debug(F101,"ttgspd gtty 2 x","",x);
  7737.     debug(F101,"ttgspd gtty 2 errno","",errno);
  7738.     if (x < 0) return(-1);
  7739.     s = ttcur.sg_ospeed;
  7740.     debug(F101,"ttgspd gtty 2 speed","",s);
  7741. #endif /* BELLV10 */
  7742. #endif /* ATTSV */
  7743. #endif /* BSD44ORPOSIX */
  7744.     }
  7745.     debug(F101,"ttgspd code","",s);
  7746. #ifdef OLINUXHISPEED
  7747.     debug(F101,"ttgspd spd_flags","",spd_flags);
  7748. #endif /* OLINUXHISPEED */
  7749.     switch (s) {
  7750. #ifdef B0
  7751.       case B0:    ss = 0L; break;
  7752. #endif /* B0 */
  7753.  
  7754. #ifndef MINIX
  7755. /*
  7756.  MINIX defines the Bxx symbols to be bps/100, so B50==B75, B110==B134==B150,
  7757.  etc, making for many "duplicate case in switch" errors, which are fatal.
  7758. */
  7759. #ifdef B50
  7760.       case B50:   ss = 50L; break;
  7761. #endif /* B50 */
  7762. #ifdef B75
  7763.       case B75:   ss = 75L; break;
  7764. #endif /* B75 */
  7765. #endif /* MINIX */
  7766.  
  7767. #ifdef B110
  7768.       case B110:  ss = 110L; break;
  7769. #endif /* B110 */
  7770.  
  7771. #ifndef MINIX
  7772. #ifdef B134
  7773.       case B134:  ss = 134L; break;
  7774. #endif /* B134 */
  7775. #ifdef B150
  7776.       case B150:  ss = 150L; break;
  7777. #endif /* B150 */
  7778. #endif /* MINIX */
  7779.  
  7780. #ifdef B200
  7781.       case B200:  ss = 200L; break;
  7782. #endif /* B200 */
  7783.  
  7784. #ifdef B300
  7785.       case B300:  ss = 300L; break;
  7786. #endif /* B300 */
  7787.  
  7788. #ifdef B600
  7789.       case B600:  ss = 600L; break;
  7790. #endif /* B600 */
  7791.  
  7792. #ifdef B1200
  7793.       case B1200: ss = 1200L; break;
  7794. #endif /* B1200 */
  7795.  
  7796. #ifdef B1800
  7797.       case B1800: ss = 1800L; break;
  7798. #endif /* B1800 */
  7799.  
  7800. #ifdef B2400
  7801.       case B2400: ss = 2400L; break;
  7802. #endif /* B2400 */
  7803.  
  7804. #ifdef B4800
  7805.       case B4800: ss = 4800L; break;
  7806. #endif /* B4800 */
  7807.  
  7808. #ifdef B7200
  7809.       case B7200: ss = 7200L; break;
  7810. #endif /* B7200 */
  7811.  
  7812. #ifdef B9600
  7813.       case B9600: ss = 9600L; break;
  7814. #endif /* B9600 */
  7815.  
  7816. #ifdef B19200
  7817.       case B19200: ss = 19200L; break;
  7818. #else
  7819. #ifdef EXTA
  7820.       case EXTA: ss = 19200L; break;
  7821. #endif /* EXTA */
  7822. #endif /* B19200 */
  7823.  
  7824. #ifdef MINIX2
  7825. /* End of hack to make MINIX2 use MINIX1 speed setting */
  7826. #undef MINIX
  7827. #endif /* MINIX2 */
  7828.  
  7829. #ifndef MINIX
  7830. #ifdef B38400
  7831.       case B38400:
  7832.         ss = 38400L;
  7833. #ifdef OLINUXHISPEED
  7834.         switch(spd_flags) {
  7835.           case ASYNC_SPD_HI:  ss =  57600L; break;
  7836.           case ASYNC_SPD_VHI: ss = 115200L; break;
  7837.     }
  7838. #endif /* OLINUXHISPEED */
  7839.         break;
  7840. #else
  7841. #ifdef EXTB
  7842.       case EXTB: ss = 38400L; break;
  7843. #endif /* EXTB */
  7844. #endif /* B38400 */
  7845. #endif /* MINIX */
  7846.  
  7847. #ifdef HPUX
  7848. #ifdef _B57600
  7849.       case _B57600: ss = 57600L; break;
  7850. #endif /* _B57600 */
  7851. #ifdef _B115200
  7852.       case _B115200: ss = 115200L; break;
  7853. #endif /* _B115200 */
  7854. #else
  7855. #ifdef B57600
  7856.       case B57600: ss = 57600L; break;
  7857. #endif /* B57600 */
  7858. #ifdef B76800
  7859.       case B76800: ss = 76800L; break;
  7860. #endif /* B76800 */
  7861. #ifdef B115200
  7862.       case B115200: ss = 115200L; break;
  7863. #endif /* B115200 */
  7864. #ifdef B153600
  7865.       case B153600: ss = 153600L; break;
  7866. #endif /* B153600 */
  7867. #ifdef B230400
  7868.       case B230400: ss = 230400L; break;
  7869. #endif /* B230400 */
  7870. #ifdef B307200
  7871.       case B307200: ss = 307200L; break;
  7872. #endif /* B307200 */
  7873. #ifdef B460800
  7874.       case B460800: ss = 460800L; break;
  7875. #endif /* B460800 */
  7876. #endif /* HPUX */
  7877. #ifdef B921600
  7878.       case 92160: ss = 921600L; break;
  7879. #endif /* B921600 */
  7880.       default:
  7881.     ss = -1; break;
  7882.     }
  7883. #endif /* Plan9 */
  7884.     debug(F101,"ttgspd speed","",ss);
  7885.     return(ss);
  7886.  
  7887. #endif /* USETCSETSPEED */
  7888. #endif /* NOLOCAL */
  7889. }
  7890. #ifdef MINIX2                /* Another hack alert */
  7891. #define MINIX
  7892. #endif /* MINIX2 */
  7893.  
  7894. /*
  7895.   FIONREAD data type...  This has been defined as "long" for many, many
  7896.   years, and it worked OK until 64-bit platforms appeared.  Thus we use
  7897.   int for 64-bit platforms, but keep long for the others.  If we changed
  7898.   the default PEEKTYPE to int, this would probably break 16-bit builds
  7899.   (note that sizeof(long) == sizeof(int) on most 32-bit platforms), many
  7900.   of which we have no way of testing any more.  Therefore, do not change
  7901.   the default definition of PEEKTYPE -- only add exceptions to it as needed.
  7902. */
  7903. #ifdef COHERENT
  7904. #ifdef FIONREAD
  7905. #undef FIONREAD
  7906. #endif /* FIONREAD */
  7907. /* #define FIONREAD TIOCQUERY */
  7908. /* #define PEEKTYPE int */
  7909. #else  /* Not COHERENT... */
  7910.  
  7911. #ifdef OSF32                /* Digital UNIX 3.2 or higher */
  7912. #define PEEKTYPE int
  7913. #else
  7914. #define PEEKTYPE long            /* Elsewhere (see notes above) */
  7915. #endif /* OSF32 */
  7916. #endif /* COHERENT */
  7917.  
  7918. /* ckumyr.c by Kristoffer Eriksson, ske@pkmab.se, 15 Mar 1990. */
  7919.  
  7920. #ifdef MYREAD
  7921.  
  7922. /* Private buffer for myread() and its companions.  Not for use by anything
  7923.  * else.  ttflui() is allowed to reset them to initial values.  ttchk() is
  7924.  * allowed to read my_count.
  7925.  *
  7926.  * my_item is an index into mybuf[].  Increment it *before* reading mybuf[].
  7927.  *
  7928.  * A global parity mask variable could be useful too.  We could use it to
  7929.  * let myread() strip the parity on its own, instead of stripping sign
  7930.  * bits as it does now.
  7931.  */
  7932. #ifdef BIGBUFOK
  7933. #define MYBUFLEN 32768
  7934. #else
  7935. #ifdef pdp11
  7936. #define MYBUFLEN 256
  7937. #else
  7938. #define MYBUFLEN 1024
  7939. #endif /* pdp11 */
  7940. #endif /* BIGBUFOK */
  7941.  
  7942. #ifdef ANYX25
  7943. #undef MYBUFLEN
  7944. #define MYBUFLEN 256
  7945. /*
  7946.   On X.25 connections, there is an extra control byte at the beginning.
  7947. */
  7948. static CHAR x25buf[MYBUFLEN+1];        /* Communication device input buffer */
  7949. static CHAR  *mybuf = x25buf+1;
  7950. #else
  7951. static CHAR mybuf[MYBUFLEN];
  7952. #endif /* ANYX25 */
  7953.  
  7954. static int my_count = 0;        /* Number of chars still in mybuf */
  7955. static int my_item = -1;        /* Last index read from mybuf[]   */
  7956.  
  7957. /*  T T P E E K  --  Peek into our internal communications input buffers. */
  7958.  
  7959. /*
  7960.   NOTE: This routine is peculiar to UNIX, and is used only by the
  7961.   select()-based CONNECT module, ckucns.c.  It need not be replicated in
  7962.   the ck?tio.c of other platforms.
  7963. */
  7964. int
  7965. ttpeek() {
  7966. #ifdef TTLEBUF
  7967.     int rc = 0;
  7968.     if (ttpush >= 0)
  7969.       rc++;
  7970.     rc += le_inbuf();
  7971.     if (rc > 0)
  7972.       return(rc);
  7973.     else
  7974. #endif /* TTLEBUF */
  7975.  
  7976. #ifdef MYREAD
  7977.     return(my_count);
  7978. #else
  7979.     return(0);
  7980. #endif /* MYREAD */
  7981. }
  7982.  
  7983. /* myread() -- Efficient read of one character from communications line.
  7984.  *
  7985.  * Uses a private buffer to minimize the number of expensive read() system
  7986.  * calls.  Essentially performs the equivalent of read() of 1 character, which
  7987.  * is then returned.  By reading all available input from the system buffers
  7988.  * to the private buffer in one chunk, and then working from this buffer, the
  7989.  * number of system calls is reduced in any case where more than one character
  7990.  * arrives during the processing of the previous chunk, for instance high
  7991.  * baud rates or network type connections where input arrives in packets.
  7992.  * If the time needed for a read() system call approaches the time for more
  7993.  * than one character to arrive, then this mechanism automatically compensates
  7994.  * for that by performing bigger read()s less frequently.  If the system load
  7995.  * is high, the same mechanism compensates for that too.
  7996.  *
  7997.  * myread() is a macro that returns the next character from the buffer.  If the
  7998.  * buffer is empty, mygetbuf() is called.  See mygetbuf() for possible error
  7999.  * returns.
  8000.  *
  8001.  * This should be efficient enough for any one-character-at-a-time loops.
  8002.  * For even better efficiency you might use memcpy()/bcopy() or such between
  8003.  * buffers (since they are often better optimized for copying), but it may not
  8004.  * be worth it if you have to take an extra pass over the buffer to strip
  8005.  * parity and check for CTRL-C anyway.
  8006.  *
  8007.  * Note that if you have been using myread() from another program module, you
  8008.  * may have some trouble accessing this macro version and the private variables
  8009.  * it uses.  In that case, just add a function in this module, that invokes the
  8010.  * macro.
  8011.  */
  8012. #define myread() (--my_count < 0 ? mygetbuf() : 255 & (int)mybuf[++my_item])
  8013.  
  8014. /* Specification: Push back up to one character onto myread()'s queue.
  8015.  *
  8016.  * This implementation: Push back characters into mybuf. At least one character
  8017.  * must have been read through myread() before myunrd() may be used.  After
  8018.  * EOF or read error, again, myunrd() can not be used.  Sometimes more than
  8019.  * one character can be pushed back, but only one character is guaranteed.
  8020.  * Since a previous myread() must have read its character out of mybuf[],
  8021.  * that guarantees that there is space for at least one character.  If push
  8022.  * back was really needed after EOF, a small addition could provide that.
  8023.  *
  8024.  * myunrd() is currently not called from anywhere inside kermit...
  8025.  */
  8026. #ifdef COMMENT /* not used */
  8027. myunrd(ch) CHAR ch; {
  8028.     if (my_item >= 0) {
  8029.     mybuf[my_item--] = ch;
  8030.     ++my_count;
  8031.     }
  8032. }
  8033. #endif /* COMMENT */
  8034.  
  8035. /*  T T P U S H B A C K  --  Put n bytes back into the myread buffer */
  8036.  
  8037. static CHAR * pushbuf = NULL;
  8038. /* static int pushed = 0; */
  8039.  
  8040. int
  8041. ttpushback(s,n) CHAR * s; int n; {
  8042.     debug(F101,"ttpushback n","",n);
  8043.     if (pushbuf || n > MYBUFLEN || n < 1)
  8044.       return(-1);
  8045.     debug(F101,"ttpushback my_count","",my_count);
  8046.     if (my_count > 0) {
  8047.     if (!(pushbuf = (CHAR *)malloc(n+1)))
  8048.       return(-1);
  8049.     memcpy(pushbuf,mybuf,my_count);
  8050.     /* pushed = my_count; */ /* (set but never used) */
  8051.     }
  8052.     memcpy(mybuf,s,n);
  8053.     my_count = n;
  8054.     my_item = -1;
  8055.     return(0);
  8056. }
  8057.  
  8058. /* mygetbuf() -- Fill buffer for myread() and return first character.
  8059.  *
  8060.  * This function is what myread() uses when it can't get the next character
  8061.  * directly from its buffer.  First, it calls a system dependent myfillbuf()
  8062.  * to read at least one new character into the buffer, and then it returns
  8063.  * the first character just as myread() would have done.  This function also
  8064.  * is responsible for all error conditions that myread() can indicate.
  8065.  *
  8066.  * Returns: When OK    => a positive character, 0 or greater.
  8067.  *        When EOF    => -2.
  8068.  *        When error    => -3, error code in errno.
  8069.  *
  8070.  * Older myread()s additionally returned -1 to indicate that there was nothing
  8071.  * to read, upon which the caller would call myread() again until it got
  8072.  * something.  The new myread()/mygetbuf() always gets something.  If it
  8073.  * doesn't, then make it do so!  Any program that actually depends on the old
  8074.  * behaviour will break.
  8075.  *
  8076.  * The older version also used to return -2 both for EOF and other errors,
  8077.  * and used to set errno to 9999 on EOF.  The errno stuff is gone, EOF and
  8078.  * other errors now return different results, although Kermit currently never
  8079.  * checks to see which it was.  It just disconnects in both cases.
  8080.  *
  8081.  * Kermit lets the user use the quit key to perform some special commands
  8082.  * during file transfer.  This causes read(), and thus also mygetbuf(), to
  8083.  * finish without reading anything and return the EINTR error.  This should
  8084.  * be checked by the caller.  Mygetbuf() could retry the read() on EINTR,
  8085.  * but if there is nothing to read, this could delay Kermit's reaction to
  8086.  * the command, and make Kermit appear unresponsive.
  8087.  *
  8088.  * The debug() call should be removed for optimum performance.
  8089.  */
  8090. int
  8091. mygetbuf() {
  8092.     int x;
  8093.     errno = 0;
  8094. #ifdef DEBUG
  8095.     if (deblog && my_count > 0)
  8096.       debug(F101,"mygetbuf IMPROPERLY CALLED with my_count","",my_count);
  8097. #endif /* DEBUG */
  8098.     if (my_count <= 0)
  8099.       my_count = myfillbuf();
  8100.  
  8101. #ifdef DEBUG
  8102. #ifdef COMMENT
  8103.     if (deblog) debug(F101, "mygetbuf read", "", my_count);
  8104. #else /* COMMENT */
  8105.     if (deblog) hexdump("mygetbuf read", mybuf, my_count);
  8106. #endif /* COMMENT */
  8107. #endif /* DEBUG */
  8108.     x = my_count;
  8109.     if (my_count <= 0) {
  8110.     my_count = 0;
  8111.     my_item = -1;
  8112.     debug(F101,"mygetbuf errno","",errno);
  8113. #ifdef TCPSOCKET
  8114.     if (netconn && ttnet == NET_TCPB && errno != 0) {
  8115.         if (errno != EINTR) {
  8116.         debug(F101,"mygetbuf TCP error","",errno);
  8117.         ttclos(0);        /* Close the connection. */
  8118.         }
  8119.         return(-3);
  8120.     }
  8121. #endif /* TCPSOCKET */
  8122.     if (!netconn && xlocal && errno) {
  8123.         if (errno != EINTR) {
  8124.         debug(F101,"mygetbuf SERIAL error","",errno);
  8125.         x = -3;
  8126.         ttclos(0);        /* Close the connection. */
  8127.         }
  8128.     }
  8129.     return((x < 0) ? -3 : -2);
  8130.     }
  8131.     --my_count;
  8132.     return((unsigned)(0xff & mybuf[my_item = 0]));
  8133. }
  8134.  
  8135. /* myfillbuf():
  8136.  * System-dependent read() into mybuf[], as many characters as possible.
  8137.  *
  8138.  * Returns: OK => number of characters read, always more than zero.
  8139.  *          EOF => 0
  8140.  *          Error => -1, error code in errno.
  8141.  *
  8142.  * If there is input available in the system's buffers, all of it should be
  8143.  * read into mybuf[] and the function return immediately.  If no input is
  8144.  * available, it should wait for a character to arrive, and return with that
  8145.  * one in mybuf[] as soon as possible.  It may wait somewhat past the first
  8146.  * character, but be aware that any such delay lengthens the packet turnaround
  8147.  * time during kermit file transfers.  Should never return with zero characters
  8148.  * unless EOF or irrecoverable read error.
  8149.  *
  8150.  * Correct functioning depends on the correct tty parameters being used.
  8151.  * Better control of current parameters is required than may have been the
  8152.  * case in older Kermit releases.  For instance, O_NDELAY (or equivalent) can
  8153.  * no longer be sometimes off and sometimes on like it used to, unless a
  8154.  * special myfillbuf() is written to handle that.  Otherwise the ordinary
  8155.  * myfillbuf()s may think they have come to EOF.
  8156.  *
  8157.  * If your system has a facility to directly perform the functioning of
  8158.  * myfillbuf(), then use it.  If the system can tell you how many characters
  8159.  * are available in its buffers, then read that amount (but not less than 1).
  8160.  * If the system can return a special indication when you try to read without
  8161.  * anything to read, while allowing you to read all there is when there is
  8162.  * something, you may loop until there is something to read, but probably that
  8163.  * is not good for the system load.
  8164.  */
  8165.  
  8166. #ifdef SVORPOSIX
  8167.     /* This is for System III/V with VMIN>0, VTIME=0 and O_NDELAY off,
  8168.      * and CLOCAL set any way you like.  This way, read() will do exactly
  8169.      * what is required by myfillbuf(): If there is data in the buffers
  8170.      * of the O.S., all available data is read into mybuf, up to the size
  8171.      * of mybuf.  If there is none, the first character to arrive is
  8172.      * awaited and returned.
  8173.      */
  8174. int
  8175. myfillbuf() {
  8176.     int fd, n;
  8177. #ifdef NETCMD
  8178.     if (ttpipe)
  8179.       fd = fdin;
  8180.     else
  8181. #endif /* NETCMD */
  8182.       fd = ttyfd;
  8183.  
  8184. #ifdef sxaE50
  8185.     /* From S. Dezawa at Fujifilm in Japan.  I don't know why this is */
  8186.     /* necessary for the sxa E50, but it is. */
  8187.     return read(fd, mybuf, 255);
  8188. #else
  8189. #ifdef BEOSORBEBOX
  8190.     while (1) {
  8191. #ifdef NETCONN
  8192.         if (netconn) {
  8193.             n = netxin(sizeof(mybuf), (char *)mybuf);
  8194.             debug(F101,"BEBOX SVORPOSIX network myfillbuf","",n);
  8195.     }
  8196.         else
  8197. #endif /* NETCONN */
  8198.       n = read(fd, mybuf, sizeof(mybuf));
  8199.     debug(F101,"BEBOX SVORPOSIX notnet myfillbuf","",n);
  8200.         if (n > 0)
  8201.       return(n);
  8202.         snooze(1000.0);
  8203.     }
  8204. #else /* BEOSORBEBOX */
  8205.     errno = 0;
  8206.     debug(F100,"SVORPOSIX myfillbuf calling read()","",0);
  8207. #ifdef IBMX25
  8208.     if (netconn && (nettype == NET_IX25)) {
  8209.     /* can't use sizeof because mybuf is a pointer, and not an array! */
  8210.     n = x25xin( MYBUFLEN, mybuf );
  8211.     } else
  8212. #endif /* IBMX25 */
  8213.  
  8214. #ifdef CK_SSL
  8215.       if (ssl_active_flag || tls_active_flag) {
  8216.       int error, n = 0;
  8217.       while (n == 0) {
  8218.           if (ssl_active_flag)
  8219.                 n = SSL_read(ssl_con, (char *)mybuf, sizeof(mybuf));
  8220.           else if (tls_active_flag)
  8221.                 n = SSL_read(tls_con, (char *)mybuf, sizeof(mybuf));
  8222.               else
  8223.         break;
  8224.           switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,n)) {
  8225.         case SSL_ERROR_NONE:
  8226.           if (n > 0)
  8227.                     return(n);
  8228.           if (n < 0)
  8229.                     return(-2);
  8230.           msleep(50);
  8231.           break;
  8232.         case SSL_ERROR_WANT_WRITE:
  8233.         case SSL_ERROR_WANT_READ:
  8234.           return(-1);
  8235.         case SSL_ERROR_SYSCALL:
  8236.           if (n != 0)
  8237.             return(-1);
  8238.         case SSL_ERROR_WANT_X509_LOOKUP:
  8239.         case SSL_ERROR_SSL:
  8240.         case SSL_ERROR_ZERO_RETURN:
  8241.         default:
  8242.           ttclos(0);
  8243.           return(-3);
  8244.             }
  8245.         }
  8246.     }
  8247. #endif /* CK_SSL */
  8248. #ifdef CK_KERBEROS
  8249. #ifdef KRB4
  8250. #ifdef RLOGCODE
  8251.     if (ttnproto == NP_EK4LOGIN) {
  8252.         if ((n = krb4_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8253.       return(-3);
  8254.         else
  8255.       return(n);
  8256.     }
  8257. #endif /* RLOGCODE */
  8258. #endif /* KRB4 */
  8259. #ifdef KRB5
  8260. #ifdef RLOGCODE
  8261.     if (ttnproto == NP_EK5LOGIN) {
  8262.         if ((n = krb5_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8263.       return(-3);
  8264.         else
  8265.       return(n);
  8266.     }
  8267. #endif /* RLOGCODE */
  8268. #ifdef KRB5_U2U
  8269.     if (ttnproto == NP_K5U2U) {
  8270.         if ((n = krb5_u2u_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8271.       return(-3);
  8272.         else
  8273.       return(n);
  8274.     }
  8275. #endif /* KRB5_U2U */
  8276. #endif /* KRB5 */
  8277. #endif /* CK_KERBEROS */
  8278.  
  8279. #ifdef NETPTY
  8280. #ifdef HAVE_PTYTRAP
  8281.     /* Special handling for HP-UX pty i/o */
  8282.   ptyread:
  8283.     if (ttpty && pty_trap_pending(ttyfd) > 0) {
  8284.         if (pty_trap_handler(ttyfd) > 0) {
  8285.             ttclos(0);
  8286.             return(-3);
  8287.         }
  8288.     }
  8289. #endif /* HAVE_PTYTRAP */
  8290. #endif /* NETPTY */
  8291.     n = read(fd, mybuf, sizeof(mybuf));
  8292.     debug(F101,"SVORPOSIX myfillbuf","",n);
  8293.     debug(F101,"SVORPOSIX myfillbuf ttcarr","",ttcarr);
  8294.     debug(F101,"SVORPOSIX myfillbuf errno","",errno);
  8295.     if (n < 1) {
  8296. #ifdef NETPTY
  8297. #ifdef HAVE_PTYTRAP
  8298.         /* When we have a PTY trap in place the connection cannot */
  8299.         /* be closed until the trap receives a close indication.  */
  8300.         if (n == 0 && ttpty)
  8301.             goto ptyread;
  8302. #endif /* HAVE_PTYTRAP */
  8303. #endif /* NETPTY */
  8304.         return(-3);
  8305.     }
  8306.     return(n);
  8307. #endif /* BEOSORBEBOX */
  8308. #endif /* sxaE50 */
  8309. }
  8310.  
  8311. #else /* not AT&T or POSIX */
  8312.  
  8313. #ifdef aegis
  8314.     /* This is quoted from the old myread().  The semantics seem to be
  8315.      * alright, but maybe errno would not need to be set even when
  8316.      * there is no error?  I don't know aegis.
  8317.      */
  8318. int
  8319. myfillbuf() {
  8320.     int count;
  8321. #ifdef NETCMD
  8322.     if (ttpipe)
  8323.       fd = fdin;
  8324.     else
  8325. #endif /* NETCMD */
  8326.       fd = ttyfd;
  8327.  
  8328.     count = ios_$get((short)fd, ios_$cond_opt, mybuf, 256L, st);
  8329.     errno = EIO;
  8330.     if (st.all == ios_$get_conditional_failed) /* get at least one */
  8331.       count = ios_$get((short)fd, 0, mybuf, 1L, st);
  8332.     if (st.all == ios_$end_of_file)
  8333.       return(-3);
  8334.     else if (st.all != status_$ok) {
  8335.     errno = EIO;
  8336.     return(-1);
  8337.     }
  8338.     return(count > 0 ? count : -3);
  8339. }
  8340. #else /* !aegis */
  8341.  
  8342. #ifdef FIONREAD
  8343.     /* This is for systems with FIONREAD.  FIONREAD returns the number
  8344.      * of characters available for reading. If none are available, wait
  8345.      * until something arrives, otherwise return all there is.
  8346.      */
  8347. int
  8348. myfillbuf() {
  8349.     PEEKTYPE avail = 0;
  8350.     int x, fd;
  8351. #ifdef NETCMD
  8352.     if (ttpipe)
  8353.       fd = fdin;
  8354.     else
  8355. #endif /* NETCMD */
  8356.       fd = ttyfd;
  8357.  
  8358. #ifdef SUNX25
  8359. /*
  8360.   SunLink X.25 support in this routine from Stefaan A. Eeckels, Eurostat (CEC).
  8361.   Depends on SunOS having FIONREAD, not because we use it, but just so this
  8362.   code is grouped correctly within the #ifdefs.  Let's hope Solaris keeps it.
  8363.  
  8364.   We call x25xin() instead of read() so that Q-Bit packets, which contain
  8365.   X.25 service-level information (e.g. PAD parameter changes), can be processed
  8366.   transparently to the upper-level code.  This is a blocking read, and so
  8367.   we depend on higher-level code (such as ttinc()) to set any necessary alarms.
  8368. */
  8369.     extern int nettype;
  8370.     if (netconn && nettype == NET_SX25) {
  8371.     while ((x = x25xin(sizeof(x25buf), x25buf)) < 1) ;
  8372.     return(x - 1);            /* "-1" compensates for extra status byte */
  8373.     }
  8374. #endif /* SUNX25 */
  8375.  
  8376. #ifdef CK_SSL
  8377.     if (ssl_active_flag || tls_active_flag) {
  8378.         int error, n = 0;
  8379.         while (n == 0) {
  8380.             if (ssl_active_flag)
  8381.           n = SSL_read(ssl_con, (char *)mybuf, sizeof(mybuf));
  8382.             else
  8383.           n = SSL_read(tls_con, (char *)mybuf, sizeof(mybuf));
  8384.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,n)) {
  8385.           case SSL_ERROR_NONE:
  8386.                 if (n > 0)
  8387.           return(n);
  8388.                 if (n < 0)
  8389.           return(-2);
  8390.                 msleep(50);
  8391.                 break;
  8392.           case SSL_ERROR_WANT_WRITE:
  8393.           case SSL_ERROR_WANT_READ:
  8394.                 return(-1);
  8395.           case SSL_ERROR_SYSCALL:
  8396.         if (n != 0)
  8397.           return(-1);
  8398.           case SSL_ERROR_WANT_X509_LOOKUP:
  8399.           case SSL_ERROR_SSL:
  8400.           case SSL_ERROR_ZERO_RETURN:
  8401.           default:
  8402.                 ttclos(0);
  8403.                 return(-2);
  8404.             }
  8405.         }
  8406.     }
  8407. #endif /* CK_SSL */
  8408. #ifdef CK_KERBEROS
  8409. #ifdef KRB4
  8410. #ifdef RLOGCODE
  8411.     if (ttnproto == NP_EK4LOGIN) {
  8412.         if ((x = krb4_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8413.       return(-1);
  8414.         else
  8415.       return(x);
  8416.     }
  8417. #endif /* RLOGCODE */
  8418. #endif /* KRB4 */
  8419. #ifdef KRB5
  8420. #ifdef RLOGCODE
  8421.     if (ttnproto == NP_EK5LOGIN) {
  8422.         if ((x = krb5_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8423.       return(-1);
  8424.         else
  8425.       return(x);
  8426.     }
  8427. #endif /* RLOGCODE */
  8428. #ifdef KRB5_U2U
  8429.     if (ttnproto == NP_K5U2U) {
  8430.         if ((x = krb5_u2u_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8431.       return(-1);
  8432.         else
  8433.       return(x);
  8434.     }
  8435. #endif /* KRB5_U2U */
  8436. #endif /* KRB5 */
  8437. #endif /* CK_KERBEROS */
  8438.  
  8439.     errno = 0;
  8440.     debug(F101,"myfillbuf calling FIONREAD ioctl","",xlocal);
  8441.     x = ioctl(fd, FIONREAD, &avail);
  8442. #ifdef DEBUG
  8443.     if (deblog) {
  8444.     debug(F101,"myfillbuf FIONREAD","",x);
  8445.     debug(F101,"myfillbuf FIONREAD avail","",avail);
  8446.     debug(F101,"myfillbuf FIONREAD errno","",errno);
  8447.     }
  8448. #endif /* DEBUG */
  8449.     if (x < 0 || avail == 0)
  8450.       avail = 1;
  8451.  
  8452.     if (avail > MYBUFLEN)
  8453.       avail = MYBUFLEN;
  8454.  
  8455.     errno = 0;
  8456.  
  8457.     x = read(fd, mybuf, (int) avail);
  8458. #ifdef DEBUG
  8459.     if (deblog) {
  8460.     debug(F101,"myfillbuf avail","",avail);
  8461.     debug(F101,"myfillbuf read","",x);
  8462.     debug(F101,"myfillbuf read errno","",errno);
  8463.         if (x > 0)
  8464.       hexdump("myfillbuf mybuf",mybuf,x);
  8465.     }
  8466. #endif /* DEBUG */
  8467.     if (x < 1) x = -3;            /* read 0 == connection loss */
  8468.     return(x);
  8469. }
  8470.  
  8471. #else /* !FIONREAD */
  8472. /* Add other systems here, between #ifdef and #else, e.g. NETCONN. */
  8473. /* When there is no other possibility, read 1 character at a time. */
  8474. int
  8475. myfillbuf() {
  8476.     int x;
  8477.  
  8478. #ifdef CK_SSL
  8479.     if (ssl_active_flag || tls_active_flag) {
  8480.         int error, n = 0;
  8481.         while (n == 0) {
  8482.             if (ssl_active_flag)
  8483.           n = SSL_read(ssl_con, (char *)mybuf, sizeof(mybuf));
  8484.             else
  8485.           count = SSL_read(tls_con, (char *)mybuf, sizeof(mybuf));
  8486.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,n)) {
  8487.           case SSL_ERROR_NONE:
  8488.                 if (n > 0)
  8489.           return(n);
  8490.                 if (n < 0)
  8491.           return(-2);
  8492.                 msleep(50);
  8493.                 break;
  8494.           case SSL_ERROR_WANT_WRITE:
  8495.           case SSL_ERROR_WANT_READ:
  8496.                 return(-1);
  8497.           case SSL_ERROR_SYSCALL:
  8498.         if (n != 0)
  8499.           return(-1);
  8500.           case SSL_ERROR_WANT_X509_LOOKUP:
  8501.           case SSL_ERROR_SSL:
  8502.           case SSL_ERROR_ZERO_RETURN:
  8503.           default:
  8504.                 ttclos(0);
  8505.                 return(-2);
  8506.             }
  8507.         }
  8508.     }
  8509. #endif /* CK_SSL */
  8510. #ifdef CK_KERBEROS
  8511. #ifdef KRB4
  8512. #ifdef RLOGCODE
  8513.     if (ttnproto == NP_EK4LOGIN) {
  8514.         if ((len = krb4_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8515.       return(-1);
  8516.         else
  8517.       return(len);
  8518.     }
  8519. #endif /* RLOGCODE */
  8520. #endif /* KRB4 */
  8521. #ifdef KRB5
  8522. #ifdef RLOGCODE
  8523.     if (ttnproto == NP_EK5LOGIN) {
  8524.         if ((len = krb5_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8525.       return(-1);
  8526.         else
  8527.       return(len);
  8528.     }
  8529. #endif /* RLOGCODE */
  8530. #ifdef KRB5_U2U
  8531.     if (ttnproto == NP_K5U2U) {
  8532.         if ((len = krb5_u2u_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8533.       return(-1);
  8534.         else
  8535.       return(len);
  8536.     }
  8537. #endif /* KRB5_U2U */
  8538. #endif /* KRB5 */
  8539. #endif /* CK_KERBEROS */
  8540.  
  8541. #ifdef NETCMD
  8542.     if (ttpipe)
  8543.       fd = fdin;
  8544.     else
  8545. #endif /* NETCMD */
  8546.       fd = ttyfd;
  8547.     x = read(fd, mybuf, 1);
  8548.     return(x > 0 ? x : -3);
  8549. }
  8550.  
  8551. #endif /* !FIONREAD */
  8552. #endif /* !aegis */
  8553. #endif /* !ATTSV */
  8554.  
  8555. #endif /* MYREAD */
  8556.  
  8557. #ifdef MINIX2
  8558. #undef MINIX
  8559. #endif /* MINIX2 */
  8560.  
  8561. /*  T T _ T N O P T  --  Handle Telnet negotions in incoming data */
  8562. /*
  8563.   Call with the IAC that was encountered.
  8564.   Returns:
  8565.    -3: If connection has dropped or gone bad.
  8566.    -2: On Telnet protocol error resulting in inconsistent states.
  8567.     0: If negotiation OK and caller has nothing to do.
  8568.     1: If packet start character has changed (new value is in global stchr).
  8569.   255: If there was a quoted IAC as data.
  8570.    or: Not at all if we got a legitimate Telnet Logout request.
  8571. */
  8572. #ifdef TCPSOCKET
  8573. static int
  8574. tt_tnopt(n) int n; {            /* Handle Telnet options */
  8575.     /* In case caller did not already check these conditions...  */
  8576.     if (n == IAC &&
  8577.     ((xlocal && netconn && IS_TELNET()) ||
  8578.      (!xlocal && sstelnet))) {
  8579.     extern int duplex;
  8580.     extern int server;
  8581.     int tx = 0;
  8582.     debug(F100,"ttinl calling tn_doop()","",0);
  8583.     tx = tn_doop((CHAR)(n & 0xff),duplex,ttinc);
  8584.     debug(F111,"ttinl tn_doop() returned","tx",tx);
  8585.     switch (tx) {
  8586.       case 0:
  8587.         return(0);
  8588.       case -1:            /* I/O error */
  8589.         ttimoff();            /* Turn off timer */
  8590.         return(-3);
  8591.           case -2:            /* Connection failed. */
  8592.           case -3:
  8593.         ttimoff();            /* Turn off timer */
  8594.         ttclos(0);
  8595.         return(-3);
  8596.       case 1:            /* ECHO change */
  8597.         duplex = 1;
  8598.         return(0);
  8599.       case 2:            /* ECHO change */
  8600.         duplex = 0;
  8601.         return(0);
  8602.       case 3:            /* Quoted IAC */
  8603.         n = 255;
  8604.         return((unsigned)255);
  8605. #ifdef IKS_OPTION
  8606.       case 4: {
  8607.           if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start && server
  8608. #ifdef IKSD
  8609.           && !inserver
  8610. #endif /* IKSD */
  8611.           ) {            /* Remote in Server mode */
  8612.           ttimoff();        /* Turn off timer */
  8613.           debug(F100,"u_start and !inserver","",0);
  8614.           return(-2);        /* End server mode */
  8615.           } else if (!TELOPT_SB(TELOPT_KERMIT).kermit.me_start &&
  8616.              server
  8617.              ) {        /* I'm no longer in Server Mode */
  8618.           debug(F100,"me_start and server","",0);
  8619.           ttimoff();
  8620.           return(-2);
  8621.           }
  8622.           return(0);
  8623.       }
  8624.       case 5: {            /* Start character change */
  8625.           /* extern CHAR stchr; */
  8626.           /* start = stchr; */
  8627.           return(1);
  8628.       }
  8629. #endif /* IKS_OPTION */
  8630.       case 6:            /* Remote Logout */
  8631.         ttimoff();
  8632.         ttclos(0);
  8633. #ifdef IKSD
  8634.         if (inserver && !local)
  8635.           doexit(GOOD_EXIT,0);
  8636.         else
  8637. #endif /* IKSD */
  8638.           return(-2);
  8639.       default:
  8640.         return(0);
  8641.     }
  8642.     } else
  8643.       return(0);
  8644. }
  8645. #endif /* TCPSOCKET */
  8646.  
  8647. /*  T T F L U I  --  Flush tty input buffer */
  8648.  
  8649. void
  8650. ttflux() {                /* But first... */
  8651. #ifdef MYREAD
  8652. /*
  8653.   Flush internal MYREAD buffer.
  8654. */
  8655. #ifdef TCPSOCKET
  8656.     int dotnopts, x;
  8657.     dotnopts = (((xlocal && netconn && IS_TELNET()) ||
  8658.          (!xlocal && sstelnet)));
  8659. #endif /* TCPSOCKET */
  8660.     debug(F101,"ttflux my_count","",my_count);
  8661. #ifdef TCPSOCKET
  8662.     if (dotnopts) {
  8663.     CHAR ch = '\0';
  8664.         while (my_count > 0) {
  8665.         ch = myread();
  8666. #ifdef CK_ENCRYPTION
  8667.             if (TELOPT_U(TELOPT_ENCRYPTION))
  8668.           ck_tn_decrypt(&ch,1);
  8669. #endif /* CK_ENCRYPTION */
  8670.             if (ch == IAC)
  8671.           x = tt_tnopt(ch);
  8672.         }
  8673.     } else
  8674. #endif /* TCPSOCKET */
  8675. #ifdef COMMENT
  8676. #ifdef CK_ENCRYPTION
  8677.     if (TELOPT_U(TELOPT_ENCRYPTION) && my_count > 0)
  8678.       ck_tn_decrypt(&mybuf[my_item+1],my_count);
  8679. #endif /* CK_ENCRYPTION */
  8680. #endif /* COMMENT */
  8681.     my_count = 0;            /* Reset count to zero */
  8682.     my_item = -1;            /* And buffer index to -1 */
  8683. #endif /* MYREAD */
  8684. }
  8685.  
  8686. int
  8687. ttflui() {
  8688.     int n, fd;
  8689. #ifdef TCPSOCKET
  8690.     int dotnopts;
  8691.     dotnopts = (((xlocal && netconn && IS_TELNET()) ||
  8692.          (!xlocal && sstelnet)));
  8693. #endif /* TCPSOCKET */
  8694.  
  8695. #ifdef NETCMD
  8696.     if (ttpipe)
  8697.       fd = fdin;
  8698.     else
  8699. #endif /* NETCMD */
  8700.       fd = ttyfd;
  8701.  
  8702. #ifdef TTLEBUF
  8703.     ttpush = -1;            /* Clear the peek-ahead char */
  8704.     while (le_data && (le_inbuf() > 0)) {
  8705.         CHAR ch = '\0';
  8706.         if (le_getchar(&ch) > 0) {    /* Clear any more... */
  8707.             debug(F101,"ttflui le_inbuf ch","",ch);
  8708.         }
  8709.     }
  8710. #endif /* TTLEBUF */
  8711.     debug(F101,"ttflui ttpipe","",ttpipe);
  8712.  
  8713. #ifdef MYREAD
  8714. /*
  8715.   Flush internal MYREAD buffer *NEXT*, in all cases.
  8716. */
  8717.     ttflux();
  8718. #endif /* MYREAD */
  8719.  
  8720. #ifdef NETCONN
  8721. /* Network flush is done specially, in the network support module. */
  8722.     if ((netconn || sstelnet) && !ttpipe && !ttpty) {
  8723.     debug(F100,"ttflui netflui","",0);
  8724. #ifdef TN_COMPORT
  8725.     if (istncomport())
  8726.       tnc_send_purge_data(TNC_PURGE_RECEIVE);
  8727. #endif /* TN_COMPORT */
  8728.     return(netflui());
  8729.     }
  8730. #endif /* NETCONN */
  8731.  
  8732.     debug(F101,"ttflui ttyfd","",ttyfd); /* Not network */
  8733.     if (ttyfd < 0)
  8734.       return(-1);
  8735.  
  8736. #ifdef aegis
  8737.     sio_$control((short)yfd, sio_$flush_in, true, st);
  8738.     if (st.all != status_$ok) {
  8739.     fprintf(stderr, "flush failed: "); error_$print(st);
  8740.     } else {      /* sometimes the flush doesn't work */
  8741.         for (;;) {
  8742.         char buf[256];
  8743.             /* eat all the characters that shouldn't be available */
  8744.             ios_$get((short)fd, ios_$cond_opt, buf, 256L, st); /* (void) */
  8745.             if (st.all == ios_$get_conditional_failed) break;
  8746.             fprintf(stderr, "flush failed(2): "); error_$print(st);
  8747.         }
  8748.     }
  8749. #else
  8750. #ifdef BSD44                /* 4.4 BSD */
  8751.     n = FREAD;                          /* Specify read queue */
  8752.     debug(F100,"ttflui BSD44","",0);
  8753.     ioctl(fd,TIOCFLUSH,&n);
  8754. #else
  8755. #ifdef Plan9
  8756. #undef POSIX                /* Uh oh... */
  8757. #endif /* Plan9 */
  8758. #ifdef POSIX                /* POSIX */
  8759.     debug(F100,"ttflui POSIX","",0);
  8760.     tcflush(fd,TCIFLUSH);
  8761. #else
  8762. #ifdef ATTSV                /* System V */
  8763. #ifndef VXVE
  8764.     debug(F100,"ttflui ATTSV","",0);
  8765.     ioctl(fd,TCFLSH,0);
  8766. #endif /* VXVE */
  8767. #else                    /* Not BSD44, POSIX, or Sys V */
  8768. #ifdef TIOCFLUSH            /* Those with TIOCFLUSH defined */
  8769. #ifdef ANYBSD                /* Berkeley */
  8770.     n = FREAD;                          /* Specify read queue */
  8771.     debug(F100,"ttflui TIOCFLUSH ANYBSD","",0);
  8772.     ioctl(fd,TIOCFLUSH,&n);
  8773. #else                    /* Others (V7, etc) */
  8774.     debug(F100,"ttflui TIOCFLUSH","",0);
  8775.     ioctl(fd,TIOCFLUSH,0);
  8776. #endif /* ANYBSD */
  8777. #else                    /* All others... */
  8778. /*
  8779.   No system call (that we know about) for input buffer flushing.
  8780.   So see how many there are and read them in a loop, using ttinc().
  8781.   ttinc() is buffered, so we're not getting charged with a system call
  8782.   per character, just a function call.
  8783. */
  8784.     if ((n = ttchk()) > 0) {
  8785.     debug(F101,"ttflui read loop","",n);
  8786.     while ((n--) && ttinc(0) > 0) ;
  8787.     }
  8788. #endif /* TIOCFLUSH */
  8789. #endif /* ATTSV */
  8790. #endif /* POSIX */
  8791. #ifdef Plan9
  8792. #define POSIX
  8793. #endif /* Plan9 */
  8794. #endif /* BSD44 */
  8795. #endif /* aegis */
  8796.     return(0);
  8797. }
  8798.  
  8799. int
  8800. ttfluo() {                /* Flush output buffer */
  8801.     int fd;
  8802. #ifdef NETCMD
  8803.     if (ttpipe)
  8804.       fd = fdout;
  8805.     else
  8806. #endif /* NETCMD */
  8807.       fd = ttyfd;
  8808.  
  8809. #ifdef Plan9
  8810.     return 0;
  8811. #else
  8812. #ifdef POSIX
  8813.     return(tcflush(fd,TCOFLUSH));
  8814. #else
  8815. #ifdef OXOS
  8816.     return(ioctl(fd,TCFLSH,1));
  8817. #else
  8818.     return(0);                /* All others, nothing */
  8819. #endif /* OXOS */
  8820. #endif /* POSIX */
  8821. #endif /* Plan9 */
  8822. }
  8823.  
  8824. /* Interrupt Functions */
  8825.  
  8826. /* Set up terminal interrupts on console terminal */
  8827.  
  8828. #ifndef FIONREAD            /* We don't need esctrp() */
  8829. #ifndef SELECT                /* if we have any of these... */
  8830. #ifndef CK_POLL
  8831. #ifndef RDCHK
  8832.  
  8833. #ifndef OXOS
  8834. #ifdef SVORPOSIX
  8835. SIGTYP
  8836. esctrp(foo) int foo; {            /* trap console escapes (^\) */
  8837.     signal(SIGQUIT,SIG_IGN);            /* ignore until trapped */
  8838.     conesc = 1;
  8839.     debug(F101,"esctrp caught SIGQUIT","",conesc);
  8840. }
  8841. #endif /* SVORPOSIX */
  8842. #endif /* OXOS */
  8843.  
  8844. #ifdef V7
  8845. #ifndef MINIX2
  8846. SIGTYP
  8847. esctrp(foo) int foo; {            /* trap console escapes (^\) */
  8848.     signal(SIGQUIT,SIG_IGN);            /* ignore until trapped */
  8849.     conesc = 1;
  8850.     debug(F101,"esctrp caught SIGQUIT","",conesc);
  8851. }
  8852. #endif /* MINIX2 */
  8853. #endif /* V7 */
  8854.  
  8855. #ifdef C70
  8856. SIGTYP
  8857. esctrp(foo) int foo; {            /* trap console escapes (^\) */
  8858.     conesc = 1;
  8859.     signal(SIGQUIT,SIG_IGN);            /* ignore until trapped */
  8860. }
  8861. #endif /* C70 */
  8862.  
  8863. #endif /* RDCHK */
  8864. #endif /* CK_POLL */
  8865. #endif /* SELECT */
  8866. #endif /* FIONREAD */
  8867.  
  8868. /*  C O N B G T  --  Background Test  */
  8869.  
  8870. static int jc = 0;            /* 0 = no job control */
  8871.  
  8872. /*
  8873.   Call with flag == 1 to prevent signal test, which can not be expected
  8874.   to work during file transfer, when SIGINT probably *is* set to SIG_IGN.
  8875.  
  8876.   Call with flag == 0 to use the signal test, but only if the process-group
  8877.   test fails, as it does on some UNIX systems, where getpgrp() is buggy,
  8878.   requires an argument when the man page says it doesn't, or vice versa.
  8879.  
  8880.   If flag == 0 and the process-group test fails, then we determine background
  8881.   status simply (but not necessarily reliably) from isatty().
  8882.  
  8883.   conbgt() sets the global backgrd = 1 if we appear to be in the background,
  8884.   and to 0 if we seem to be in the foreground.  conbgt() is highly prone to
  8885.   misbehavior.
  8886. */
  8887. VOID
  8888. conbgt(flag) int flag; {
  8889.     int x = -1,                /* process group or SIGINT test */
  8890.         y = 0;                /* isatty() test */
  8891. /*
  8892.   Check for background operation, even if not running on real tty, so that
  8893.   background flag can be set correctly.  If background status is detected,
  8894.   then Kermit will not issue its interactive prompt or most messages.
  8895.   If your prompt goes away, you can blame (and fix?) this function.
  8896. */
  8897.  
  8898. /* Use process-group test if possible. */
  8899.  
  8900. #ifdef POSIX                /* We can do it in POSIX */
  8901. #define PGROUP_T
  8902. #else
  8903. #ifdef BSD4                /* and in BSD 4.x. */
  8904. #define PGROUP_T
  8905. #else
  8906. #ifdef HPUXJOBCTL            /* and in most HP-UX's */
  8907. #define PGROUP_T
  8908. #else
  8909. #ifdef TIOCGPGRP            /* and anyplace that has this ioctl. */
  8910. #define PGROUP_T
  8911. #endif /* TIOCGPGRP */
  8912. #endif /* HPUXJOBCTL */
  8913. #endif /* BSD4 */
  8914. #endif /* POSIX */
  8915.  
  8916. #ifdef MIPS                /* Except if it doesn't work... */
  8917. #undef PGROUP_T
  8918. #endif /* MIPS */
  8919.  
  8920. #ifdef PGROUP_T
  8921. /*
  8922.   Semi-reliable process-group test.  Check whether this process's group is
  8923.   the same as the controlling terminal's process group.  This works if the
  8924.   getpgrp() call doesn't lie (as it does in the SUNOS System V environment).
  8925. */
  8926.     PID_T mypgrp = (PID_T)0;        /* Kermit's process group */
  8927.     PID_T ctpgrp = (PID_T)0;        /* The terminal's process group */
  8928. #ifndef _POSIX_SOURCE
  8929. /*
  8930.   The getpgrp() prototype is obtained from system header files for POSIX
  8931.   and Sys V R4 compilations.  Other systems, who knows.  Some complain about
  8932.   a duplicate declaration here, others don't, so it's safer to leave it in
  8933.   if we don't know for certain.
  8934. */
  8935. #ifndef SVR4
  8936. #ifndef PS2AIX10
  8937. #ifndef HPUX9
  8938.     extern PID_T getpgrp();
  8939. #endif /* HPUX9 */
  8940. #endif /* PS2AIX10 */
  8941. #endif /* SVR4 */
  8942. #endif /* _POSIX_SOURCE */
  8943.  
  8944. /* Get my process group. */
  8945.  
  8946. #ifdef SVR3 /* Maybe this should be ATTSV? */
  8947. /* This function is not described in SVID R2 */
  8948.     mypgrp = getpgrp();
  8949.     /* debug(F101,"ATTSV conbgt process group","",(int) mypgrp); */
  8950. #else
  8951. #ifdef POSIX
  8952.     mypgrp = getpgrp();
  8953.     /* debug(F101,"POSIX conbgt process group","",(int) mypgrp); */
  8954. #else
  8955. #ifdef OSFPC
  8956.     mypgrp = getpgrp();
  8957.     /* debug(F101,"OSF conbgt process group","",(int) mypgrp); */
  8958. #else
  8959. #ifdef QNX
  8960.     mypgrp = getpgrp();
  8961.     /* debug(F101,"QNX conbgt process group","",(int) mypgrp); */
  8962. #else
  8963. #ifdef OSF32                /* (was OSF40) */
  8964.     mypgrp = getpgrp();
  8965.     /* debug(F101,"Digital UNIX conbgt process group","",(int) mypgrp); */
  8966. #else /* BSD, V7, etc */
  8967. #ifdef MINIX2
  8968.     mypgrp = getpgrp();
  8969. #else
  8970.     mypgrp = getpgrp(0);
  8971. #endif /* MINIX2 */
  8972.     /* debug(F101,"BSD conbgt process group","",(int) mypgrp); */
  8973. #endif /* OSF32 */
  8974. #endif /* QNX */
  8975. #endif /* OSFPC */
  8976. #endif /* POSIX */
  8977. #endif /* SVR3 */
  8978.  
  8979. #ifdef MINIX2
  8980. #undef BSD44ORPOSIX
  8981. #endif /* MINIX2 */
  8982.  
  8983. /* Now get controlling tty's process group */
  8984. #ifdef BSD44ORPOSIX
  8985.     ctpgrp = tcgetpgrp(1);        /* The POSIX way */
  8986.     /* debug(F101,"POSIX conbgt terminal process group","",(int) ctpgrp); */
  8987. #else
  8988.     ioctl(1, TIOCGPGRP, &ctpgrp);    /* Or the BSD way */
  8989.    /* debug(F101,"non-POSIX conbgt terminal process group","",(int) ctpgrp); */
  8990. #endif /* BSD44ORPOSIX */
  8991.  
  8992. #ifdef MINIX2
  8993. #define BSD44ORPOSIX
  8994. #endif /* MINIX2 */
  8995.  
  8996.     if ((mypgrp > (PID_T) 0) && (ctpgrp > (PID_T) 0))
  8997.       x = (mypgrp == ctpgrp) ? 0 : 1;    /* If they differ, then background. */
  8998.     else x = -1;            /* If error, remember. */
  8999.     debug(F101,"conbgt process group test","",x);
  9000. #endif /* PGROUP_T */
  9001.  
  9002. /* Try to see if job control is available */
  9003.  
  9004. #ifdef NOJC                /* User override */
  9005.     jc = 0;                /* No job control allowed */
  9006.     debug(F111,"NOJC","jc",jc);
  9007. #else
  9008. #ifdef BSD44
  9009.     jc = 1;
  9010. #else
  9011. #ifdef SVR4ORPOSIX            /* POSIX actually tells us */
  9012.     debug(F100,"SVR4ORPOSIX jc test...","",0);
  9013. #ifdef _SC_JOB_CONTROL
  9014. #ifdef __bsdi__
  9015.     jc = 1;
  9016. #else
  9017. #ifdef __386BSD__
  9018.     jc = 1;
  9019. #else
  9020.     jc = sysconf(_SC_JOB_CONTROL);    /* Whatever system says */
  9021.     if (jc < 0) {
  9022.     debug(F101,"sysconf fails, jcshell","",jcshell);
  9023.     jc = (jchdlr == SIG_DFL) ? 1 : 0;
  9024.     } else
  9025.       debug(F111,"sysconf(_SC_JOB_CONTROL)","jc",jc);
  9026. #endif /* __386BSD__ */
  9027. #endif /* __bsdi__ */
  9028. #else
  9029. #ifdef _POSIX_JOB_CONTROL
  9030.     jc = 1;                /* By definition */
  9031.     debug(F111,"_POSIX_JOB_CONTROL is defined","jc",jc);
  9032. #else
  9033.     jc = 0;                /* Assume job control not allowed */
  9034.     debug(F111,"SVR4ORPOSIX _SC/POSIX_JOB_CONTROL not defined","jc",jc);
  9035. #endif /* _POSIX_JOB_CONTROL */
  9036. #endif /* _SC_JOB_CONTROL */
  9037. #else
  9038. #ifdef BSD4
  9039.     jc = 1;                /* Job control allowed */
  9040.     debug(F111,"BSD job control","jc",jc);
  9041. #else
  9042. #ifdef SVR3JC
  9043.     jc = 1;                /* JC allowed */
  9044.     debug(F111,"SVR3 job control","jc",jc);
  9045. #else
  9046. #ifdef OXOS
  9047.     jc = 1;                /* JC allowed */
  9048.     debug(F111,"X/OS job control","jc",jc);
  9049. #else
  9050. #ifdef HPUX9
  9051.     jc = 1;                /* JC allowed */
  9052.     debug(F111,"HP-UX 9.0 job control","jc",jc);
  9053. #else
  9054. #ifdef HPUX10
  9055.     jc = 1;                /* JC allowed */
  9056.     debug(F111,"HP-UX 10.0 job control","jc",jc);
  9057. #else
  9058.     jc = 0;                /* JC not allowed */
  9059.     debug(F111,"job control catch-all","jc",jc);
  9060. #endif /* HPUX10 */
  9061. #endif /* HPUX9 */
  9062. #endif /* OXOS */
  9063. #endif /* SVR3JC */
  9064. #endif /* BSD4 */
  9065. #endif /* SVR4ORPOSIX */
  9066. #endif /* BSD44 */
  9067. #endif /* NOJC */
  9068.     debug(F101,"conbgt jc","",jc);
  9069. #ifndef NOJC
  9070.     debug(F101,"conbgt jcshell","",jcshell);
  9071. /*
  9072.   At this point, if jc == 1 but jcshell == 0, it means that the OS supports
  9073.   job control, but the shell or other process we are running under does not
  9074.   (jcshell is set in sysinit()) and so if we suspend ourselves, nothing good
  9075.   will come of it.  So...
  9076. */
  9077.     if (jc < 0) jc = 0;
  9078.     if (jc > 0 && jcshell == 0) jc = 0;
  9079. #endif /* NOJC */
  9080.  
  9081. /*
  9082.   Another background test.
  9083.   Test if SIGINT (terminal interrupt) is set to SIG_IGN (ignore),
  9084.   which is done by the shell (sh) if the program is started with '&'.
  9085.   Unfortunately, this is NOT done by csh or ksh so watch out!
  9086.   Note, it's safe to set SIGINT to SIG_IGN here, because further down
  9087.   we always set it to something else.
  9088.   Note: as of 16 Jul 1999, we also skip this test if we set SIGINT to
  9089.   SIG_IGN ourselves.
  9090. */
  9091.     if (x < 0 && !flag && !sigint_ign) { /* Didn't get good results above... */
  9092.  
  9093.     SIGTYP (*osigint)();
  9094.  
  9095.     osigint = signal(SIGINT,SIG_IGN);    /* What is SIGINT set to? */
  9096.     sigint_ign = 1;
  9097.     x = (osigint == SIG_IGN) ? 1 : 0;    /* SIG_IGN? */
  9098.     debug(F101,"conbgt osigint","",osigint);
  9099.     debug(F101,"conbgt signal test","",x);
  9100.     }
  9101.  
  9102. /* Also check to see if we're running with redirected stdio. */
  9103. /* This is not really background operation, but we want to act as though */
  9104. /* it were. */
  9105.  
  9106. #ifdef IKSD
  9107.     if (inserver) {            /* Internet Kermit Server */
  9108.     backgrd = 0;            /* is not in the background */
  9109.     return;
  9110.     }
  9111. #endif /* IKSD */
  9112.  
  9113.     y = (isatty(0) && isatty(1)) ? 1 : 0;
  9114.     debug(F101,"conbgt isatty test","",y);
  9115.  
  9116. #ifdef BSD29
  9117. /* The process group and/or signal test doesn't work under these... */
  9118.     backgrd = !y;
  9119. #else
  9120. #ifdef sxaE50
  9121.     backgrd = !y;
  9122. #else
  9123. #ifdef MINIX
  9124.     backgrd = !y;
  9125. #else
  9126. #ifdef MINIX2
  9127.     backgrd = !y;
  9128. #else
  9129.     if (x > -1)
  9130.       backgrd = (x || !y) ? 1 : 0;
  9131.     else backgrd = !y;
  9132. #endif /* BSD29 */
  9133. #endif /* sxaE50 */
  9134. #endif /* MINIX */
  9135. #endif /* MINIX2 */
  9136.     debug(F101,"conbgt backgrd","",backgrd);
  9137. }
  9138.  
  9139. /*  C O N I N T  --  Console Interrupt setter  */
  9140.  
  9141. /*
  9142.   First arg is pointer to function to handle SIGTERM & SIGINT (like Ctrl-C).
  9143.   Second arg is pointer to function to handle SIGTSTP (suspend).
  9144. */
  9145.  
  9146. VOID                    /* Set terminal interrupt traps. */
  9147. #ifdef CK_ANSIC
  9148. #ifdef apollo
  9149. conint(f,s) SIGTYP (*f)(), (*s)();
  9150. #else
  9151. conint(SIGTYP (*f)(int), SIGTYP (*s)(int))
  9152. #endif /* apollo */
  9153. #else
  9154. conint(f,s) SIGTYP (*f)(), (*s)();
  9155. #endif /* CK_ANSIC */
  9156. /* conint */ {
  9157.  
  9158.     debug(F101,"conint conistate","",conistate);
  9159.  
  9160.     conbgt(0);                /* Do background test. */
  9161.  
  9162. /* Set the desired handlers for hangup and software termination. */
  9163.  
  9164. #ifdef SIGTERM
  9165.     signal(SIGTERM,f);                  /* Software termination */
  9166. #endif /* SIGTERM */
  9167.  
  9168. /*
  9169.   Prior to July 1999 we used to call sighup() here but now it's called in
  9170.   sysinit() so SIGHUP can be caught during execution of the init file or
  9171.   a kerbang script.
  9172. */
  9173.  
  9174. /* Now handle keyboard stop, quit, and interrupt signals. */
  9175. /* Check if invoked in background -- if so signals set to be ignored. */
  9176. /* However, if running under a job control shell, don't ignore them. */
  9177. /* We won't be getting any, as we aren't in the terminal's process group. */
  9178.  
  9179.     debug(F101,"conint backgrd","",backgrd);
  9180.     debug(F101,"conint jc","",jc);
  9181.  
  9182.     if (backgrd && !jc) {        /* In background, ignore signals */
  9183.     debug(F101,"conint background ignoring signals, jc","",jc);
  9184. #ifdef SIGTSTP
  9185.         signal(SIGTSTP,SIG_IGN);        /* Keyboard stop */
  9186. #endif /* SIGTSTP */
  9187.         signal(SIGQUIT,SIG_IGN);        /* Keyboard quit */
  9188.         signal(SIGINT,SIG_IGN);         /* Keyboard interrupt */
  9189.     sigint_ign = 1;
  9190.     conistate = CONI_NOI;
  9191.     } else {                /* Else in foreground or suspended */
  9192.     debug(F101,"conint foreground catching signals, jc","",jc);
  9193.         signal(SIGINT,f);               /* Catch terminal interrupt */
  9194.     sigint_ign = (f == SIG_IGN) ? 1 : 0;
  9195.  
  9196. #ifdef SIGTSTP                /* Keyboard stop (suspend) */
  9197.     /* debug(F101,"conint SIGSTSTP","",s); */
  9198.     if (s == NULL) s = SIG_DFL;
  9199. #ifdef NOJC                /* No job control allowed. */
  9200.     signal(SIGTSTP,SIG_IGN);
  9201. #else                    /* Job control allowed */
  9202.     if (jc)                /* if available. */
  9203.       signal(SIGTSTP,s);
  9204.     else
  9205.       signal(SIGTSTP,SIG_IGN);
  9206. #endif /* NOJC */
  9207. #endif /* SIGTSTP */
  9208.  
  9209. #ifndef OXOS
  9210. #ifdef SVORPOSIX
  9211. #ifndef FIONREAD            /* Watch out, we don't know this... */
  9212. #ifndef SELECT
  9213. #ifndef CK_POLL
  9214. #ifndef RDCHK
  9215.         signal(SIGQUIT,esctrp);         /* Quit signal, Sys III/V. */
  9216. #endif /* RDCHK */
  9217. #endif /* CK_POLL */
  9218. #endif /* SELECT */
  9219. #endif /* FIONREAD */
  9220.         if (conesc) conesc = 0;         /* Clear out pending escapes */
  9221. #else
  9222. #ifdef V7
  9223.         signal(SIGQUIT,esctrp);         /* V7 like Sys III/V */
  9224.         if (conesc) conesc = 0;
  9225. #else
  9226. #ifdef aegis
  9227.         signal(SIGQUIT,f);              /* Apollo, catch it like others. */
  9228. #else
  9229.         signal(SIGQUIT,SIG_IGN);        /* Others, ignore like 4D & earlier. */
  9230. #endif /* aegis */
  9231. #endif /* V7 */
  9232. #endif /* SVORPOSIX */
  9233. #endif /* OXOS */
  9234.     conistate = CONI_INT;
  9235.     }
  9236. }
  9237.  
  9238.  
  9239. /*  C O N N O I  --  Reset console terminal interrupts */
  9240.  
  9241. VOID
  9242. connoi() {                              /* Console-no-interrupts */
  9243.  
  9244.     debug(F101,"connoi conistate","",conistate);
  9245. #ifdef SIGTSTP
  9246.     signal(SIGTSTP,SIG_IGN);        /* Suspend */
  9247. #endif /* SIGTSTP */
  9248.     conint(SIG_IGN,SIG_IGN);        /* Interrupt */
  9249.     sigint_ign = 1;            /* Remember we did this ourselves */
  9250. #ifdef SIGQUIT
  9251.     signal(SIGQUIT,SIG_IGN);        /* Quit */
  9252. #endif /* SIGQUIT */
  9253. #ifdef SIGTERM
  9254.     signal(SIGTERM,SIG_IGN);        /* Term */
  9255. #endif /* SIGTERM */
  9256.     conistate = CONI_NOI;
  9257. }
  9258.  
  9259. /*  I N I T R A W Q  --  Set up to read /dev/kmem for character count.  */
  9260.  
  9261. #ifdef  V7
  9262. /*
  9263.  Used in Version 7 to simulate Berkeley's FIONREAD ioctl call.  This
  9264.  eliminates blocking on a read, because we can read /dev/kmem to get the
  9265.  number of characters available for raw input.  If your system can't
  9266.  or you won't let the world read /dev/kmem then you must figure out a
  9267.  different way to do the counting of characters available, or else replace
  9268.  this by a dummy function that always returns 0.
  9269. */
  9270. /*
  9271.  * Call this routine as: initrawq(tty)
  9272.  * where tty is the file descriptor of a terminal.  It will return
  9273.  * (as a char *) the kernel-mode memory address of the rawq character
  9274.  * count, which may then be read.  It has the side-effect of flushing
  9275.  * input on the terminal.
  9276.  */
  9277. /*
  9278.  * John Mackin, Physiology Dept., University of Sydney (Australia)
  9279.  * ...!decvax!mulga!physiol.su.oz!john
  9280.  *
  9281.  * Permission is hereby granted to do anything with this code, as
  9282.  * long as this comment is retained unmodified and no commercial
  9283.  * advantage is gained.
  9284.  */
  9285. #ifndef MINIX
  9286. #ifndef MINIX2
  9287. #ifndef COHERENT
  9288. #include <a.out.h>
  9289. #include <sys/proc.h>
  9290. #endif /* COHERENT */
  9291. #endif /* MINIX2 */
  9292. #endif /* MINIX */
  9293.  
  9294. #ifdef COHERENT
  9295. #include <l.out.h>
  9296. #include <sys/proc.h>
  9297. #endif /* COHERENT */
  9298.  
  9299. char *
  9300. initrawq(tty) int tty; {
  9301. #ifdef MINIX
  9302.     return(0);
  9303. #else
  9304. #ifdef MINIX2
  9305.     return(0);
  9306. #else
  9307. #ifdef UTS24
  9308.     return(0);
  9309. #else
  9310. #ifdef BSD29
  9311.     return(0);
  9312. #else
  9313.     long lseek();
  9314.     static struct nlist nl[] = {
  9315.         {PROCNAME},
  9316.         {NPROCNAME},
  9317.         {""}
  9318.     };
  9319.     static struct proc *pp;
  9320.     char *qaddr, *p, c;
  9321.     int m;
  9322.     PID_T pid, me;
  9323.     NPTYPE xproc;                       /* Its type is defined in makefile. */
  9324.     int catch();
  9325.  
  9326.     me = getpid();
  9327.     if ((m = open("/dev/kmem", 0)) < 0) err("kmem");
  9328.     nlist(BOOTNAME, nl);
  9329.     if (nl[0].n_type == 0) err("proc array");
  9330.  
  9331.     if (nl[1].n_type == 0) err("nproc");
  9332.  
  9333.     lseek(m, (long)(nl[1].n_value), 0);
  9334.     read (m, &xproc, sizeof(xproc));
  9335.     saval = signal(SIGALRM, catch);
  9336.     if ((pid = fork()) == 0) {
  9337.         while(1)
  9338.             read(tty, &c, 1);
  9339.     }
  9340.     alarm(2);
  9341.  
  9342.     if(setjmp(jjbuf) == 0) {
  9343.         while(1)
  9344.       read(tty, &c, 1);
  9345.     }
  9346.     signal(SIGALRM, SIG_DFL);
  9347.  
  9348. #ifdef DIRECT
  9349.     pp = (struct proc *) nl[0].n_value;
  9350. #else
  9351.     if (lseek(m, (long)(nl[0].n_value), 0) < 0L) err("seek");
  9352.     if (read(m, &pp, sizeof(pp)) != sizeof(pp))  err("no read of proc ptr");
  9353. #endif
  9354.     lseek(m, (long)(nl[1].n_value), 0);
  9355.     read(m, &xproc, sizeof(xproc));
  9356.  
  9357.     if (lseek(m, (long)pp, 0) < 0L) err("Can't seek to proc");
  9358.     if ((p = malloc(xproc * sizeof(struct proc))) == NULL) err("malloc");
  9359.     if (read(m,p,xproc * sizeof(struct proc)) != xproc*sizeof(struct proc))
  9360.         err("read proc table");
  9361.     for (pp = (struct proc *)p; xproc > 0; --xproc, ++pp) {
  9362.         if (pp -> p_pid == (short) pid) goto iout;
  9363.     }
  9364.     err("no such proc");
  9365.  
  9366. iout:
  9367.     close(m);
  9368.     qaddr = (char *)(pp -> p_wchan);
  9369.     free (p);
  9370.     kill(pid, SIGKILL);
  9371.     wait((WAIT_T *)0);
  9372.     return (qaddr);
  9373. #endif
  9374. #endif
  9375. #endif
  9376. #endif
  9377. }
  9378.  
  9379. /*  More V7-support functions...  */
  9380.  
  9381. static VOID
  9382. err(s) char *s; {
  9383.     char buf[200];
  9384.  
  9385.     ckmakmsg(buf,200,"fatal error in initrawq: ", s, NULL, NULL);
  9386.     perror(buf);
  9387.     doexit(1,-1);
  9388. }
  9389.  
  9390. static VOID
  9391. catch(foo) int foo; {
  9392.     longjmp(jjbuf, -1);
  9393. }
  9394.  
  9395.  
  9396. /*  G E N B R K  --  Simulate a modem break.  */
  9397.  
  9398. #ifdef MINIX
  9399. #define BSPEED B110
  9400. #else
  9401. #ifdef MINIX2
  9402. #define BSPEED B110
  9403. #else
  9404. #define BSPEED B150
  9405. #endif /* MINIX2 */
  9406. #endif /* MINIX */
  9407.  
  9408. #ifndef MINIX2
  9409. VOID
  9410. genbrk(fn,msec) int fn, msec; {
  9411.     struct sgttyb ttbuf;
  9412.     int ret, sospeed, x, y;
  9413.  
  9414.     ret = ioctl(fn, TIOCGETP, &ttbuf);
  9415.     sospeed = ttbuf.sg_ospeed;
  9416.     ttbuf.sg_ospeed = BSPEED;
  9417.     ret = ioctl(fn, TIOCSETP, &ttbuf);
  9418.     y = (int)strlen(brnuls);
  9419.     x = ( BSPEED * 100 ) / msec;
  9420.     if (x > y) x = y;
  9421.     ret = write(fn, brnuls, (( BSPEED * 100 ) / msec ));
  9422.     ttbuf.sg_ospeed = sospeed;
  9423.     ret = ioctl(fn, TIOCSETP, &ttbuf);
  9424.     ret = write(fn, "@", 1);
  9425.     return;
  9426. }
  9427. #endif /* MINIX2 */
  9428.  
  9429. #ifdef MINIX2
  9430. int
  9431. genbrk(fn,msec) int fn, msec; {
  9432.     struct termios ttbuf;
  9433.     int ret, x, y;
  9434.     speed_t sospeed;
  9435.  
  9436.     ret = tcgetattr(fn, &ttbuf);
  9437.     sospeed = ttbuf.c_ospeed;
  9438.     ttbuf.c_ospeed = BSPEED;
  9439.     ret = tcsetattr(fn,TCSADRAIN, &ttbuf);
  9440.     y = (int)strlen(brnuls);
  9441.     x = ( BSPEED * 100 ) / msec;
  9442.     if (x > y) x = y;
  9443.     ret = write(fn, brnuls, (( BSPEED * 100 ) / msec ));
  9444.     ttbuf.c_ospeed = sospeed;
  9445.     ret = tcsetattr(fn, TCSADRAIN, &ttbuf);
  9446.     ret = write(fn, "@", 1);
  9447.     return ret;
  9448. }
  9449. #endif /* MINIX2 */
  9450. #endif /* V7 */
  9451.  
  9452. /*
  9453.   I N C H K  --  Check if chars waiting to be read on given file descriptor.
  9454.  
  9455.   This routine is a merger of ttchk() and conchk().
  9456.   Call with:
  9457.     channel == 0 to check console.
  9458.     channel == 1 to check communications connection.
  9459.   and:
  9460.     fd = file descriptor.
  9461.   Returns:
  9462.    >= 0: number of characters waiting, 0 or greater,
  9463.      -1: on any kind of error,
  9464.      -2: if there is (definitely) no connection.
  9465.   Note: In UNIX we don't have to call nettchk() because a socket
  9466.   file descriptor works just like in serial i/o, ioctls and all.
  9467.   (But this will change if we add non-file-descriptor channels,
  9468.   such as IBM X.25 for AIX...)
  9469. */
  9470. static int
  9471. in_chk(channel, fd) int channel, fd; {
  9472.     int x, n = 0;            /* Workers, n = return value */
  9473.     extern int clsondisc;        /* Close on disconnect */
  9474. /*
  9475.   The first section checks to make sure we have a connection,
  9476.   but only if we're in local mode.
  9477. */
  9478. #ifdef DEBUG
  9479.     if (deblog) {
  9480.     debug(F111,"in_chk entry",ckitoa(fd),channel);
  9481.     debug(F101,"in_chk ttyfd","",ttyfd);
  9482.     debug(F101,"in_chk ttpty","",ttpty);
  9483.     }
  9484. #endif /* DEBUG */
  9485. /*
  9486.   But don't say connection is gone if we have any buffered-stuff.
  9487. */
  9488. #ifdef TTLEBUF
  9489.     debug(F101,"in_chk ttpush","",ttpush);
  9490.     if (channel == 1) {
  9491.     if (ttpush >= 0)
  9492.       n++;
  9493.     n += le_inbuf();
  9494.     if (n > 0)
  9495.       return(n);
  9496.     }
  9497. #endif /* TTLEBUF */
  9498.  
  9499. #ifdef NETPTY
  9500. #ifdef HAVE_PTYTRAP
  9501.     /* Special handling for HP-UX pty i/o */
  9502.     if (ttpty && pty_trap_pending(ttyfd) > 0) {
  9503.         if (pty_trap_handler(ttyfd) > 0) {
  9504.             ttclos(0);
  9505.             return(-2);
  9506.         }
  9507.     }
  9508. #endif /* HAVE_PTYTRAP */
  9509. #endif /* NETPTY */
  9510.  
  9511.     if (channel) {            /* Checking communications channel */
  9512.     if (ttyfd < 0) {        /* No connection */
  9513.       return(-2);            /* That's what this means */
  9514.     } else if (xlocal &&        /* In local mode */
  9515.            !netconn &&        /* Serial connection */
  9516.            ttcarr != CAR_OFF    /* with CARRIER WATCH ON (or AUTO) */
  9517. #ifdef COMMENT
  9518. #ifdef MYREAD
  9519. /*
  9520.   Seems like this would be a good idea but it prevents C-Kermit from
  9521.   popping back to the prompt automatically when carrier drops.  However,
  9522.   commenting this out prevents us from seeing the NO CARRIER message.
  9523.   Needs more work...
  9524. */
  9525.            && my_count < 1    /* Nothing in our internal buffer */
  9526. #endif /* MYREAD */
  9527. #endif /* COMMENT */
  9528.            ) {
  9529.         int x;
  9530.         x = ttgmdm();        /* So get modem signals */
  9531.         debug(F101,"in_chk close-on-disconnect","",clsondisc);
  9532.         if (x > -1) {        /* Check for carrier */
  9533.         if (!(x & BM_DCD)) {    /* No carrier */
  9534.             debug(F101,"in_chk carrier lost","",x);
  9535.             if (clsondisc)    /* If "close-on-disconnect" */
  9536.               ttclos(0);    /* close device & release lock. */
  9537.             return(-2);        /* This means "disconnected" */
  9538.         }
  9539.         /* In case I/O to device after CD dropped always fails */
  9540.         /* as in Debian Linux 2.1 and Unixware 2.1... */
  9541.         } else {
  9542.             debug(F101,"in_chk ttgmdm I/O error","",errno);
  9543.             debug(F101,"in_chk ttgmdm gotsigs","",gotsigs);
  9544.             if (gotsigs) {        /* If we got signals before... */
  9545.             if (errno == 5 || errno == 6) { /* I/O error etc */
  9546.                 if (clsondisc)    /* like when modem hangs up */
  9547.               ttclos(0);
  9548.             return(-2);
  9549.             }
  9550.         }
  9551.         /* If we never got modem signals successfully on this */
  9552.         /* connection before, we can't conclude that THIS failure */
  9553.         /* means the connection was lost. */
  9554.         return(0);
  9555.         }
  9556.     }
  9557.     }
  9558.  
  9559. /* We seem to have a connection so now see if any bytes are waiting on it */
  9560.  
  9561. #ifdef CK_SSL
  9562.     if (ssl_active_flag || tls_active_flag) {
  9563.         n += SSL_pending(ssl_active_flag?ssl_con:tls_con);
  9564.         debug(F101,"in_chk SSL_pending","",n);
  9565.         if (n < 0) {
  9566.             ttclos(0);
  9567.             return(-1);
  9568.         } else if (n > 0) {
  9569.             return(n);
  9570.         }
  9571.     }
  9572. #endif /* CK_SSL */
  9573. #ifdef RLOGCODE
  9574. #ifdef CK_KERBEROS
  9575.     /* It is not safe to read any data when using encrypted Klogin */
  9576.     if (ttnproto == NP_EK4LOGIN || ttnproto == NP_EK5LOGIN) {
  9577. #ifdef KRB4
  9578.         if (ttnproto == NP_EK4LOGIN) {
  9579.             n += krb4_des_avail(ttyfd);
  9580.             debug(F101,"in_chk krb4_des_avail","",n);
  9581.         }
  9582. #endif /* KRB4 */
  9583. #ifdef KRB5
  9584.         if (ttnproto == NP_EK5LOGIN) {
  9585.             n += krb5_des_avail(ttyfd);
  9586.             debug(F101,"in_chk krb5_des_avail","",n);
  9587.         }
  9588. #ifdef KRB5_U2U
  9589.         if (ttnproto == NP_K5U2U) {
  9590.             n += krb5_u2u_avail(ttyfd);
  9591.             debug(F101,"in_chk krb5_des_avail","",n);
  9592.         }
  9593. #endif /* KRB5_U2U */
  9594. #endif /* KRB5 */
  9595.         if (n < 0)            /* Is this right? */
  9596.       return(-1);
  9597.         else
  9598.       return(n);
  9599.     }
  9600. #endif /* CK_KERBEROS */
  9601. #endif /* RLOGCODE */
  9602.  
  9603.     errno = 0;                /* Reset this so we log good info */
  9604. #ifdef FIONREAD
  9605.     x = ioctl(fd, FIONREAD, &n);    /* BSD and lots of others */
  9606. #ifdef DEBUG                /* (the more the better) */
  9607.     if (deblog) {
  9608.     debug(F101,"in_chk FIONREAD return code","",x);
  9609.     debug(F101,"in_chk FIONREAD count","",n);
  9610.     debug(F101,"in_chk FIONREAD errno","",errno);
  9611.     }
  9612. #endif /* DEBUG */
  9613. #else /* FIONREAD not defined */
  9614. /*
  9615.   Here, if (netconn && ttnet == NET_TCPB), we might try calling recvmsg()
  9616.   with flags MSG_PEEK|MSG_DONTWAIT on the socket (ttyfd), except this is not
  9617.   portable (MSG_DONTWAIT isn't defined in any of the <sys/socket.h> files
  9618.   that I looked at, but it is needed to prevent the call from blocking), and
  9619.   the msghdr struct differs from place to place, so we would need another
  9620.   avalanche of ifdefs.  Still, when FIONREAD is not available, this is the
  9621.   only other known method of asking the OS for the *number* of characters
  9622.   available for reading.
  9623. */
  9624. #ifdef V7                /* UNIX V7: look in kernel memory */
  9625. #ifdef MINIX
  9626.     n = 0;                /* But not in MINIX */
  9627. #else
  9628. #ifdef MINIX2
  9629.     n = 0;
  9630. #else
  9631.     lseek(kmem[TTY], (long) qaddr[TTY], 0); /* 7th Edition Unix */
  9632.     x = read(kmem[TTY], &n, sizeof(int));
  9633.     if (x != sizeof(int))
  9634.       n = 0;
  9635. #endif /* MINIX2 */
  9636. #endif /* MINIX */
  9637. #else /* Not V7 */
  9638. #ifdef PROVX1
  9639.     x = ioctl(fd, TIOCQCNT, &ttbuf);    /* DEC Pro/3xx Venix V.1 */
  9640.     n = ttbuf.sg_ispeed & 0377;        /* Circa 1984 */
  9641.     if (x < 0) n = 0;
  9642. #else
  9643. #ifdef MYREAD
  9644. /*
  9645.   Here we skip all the undependable and expensive calls below if we
  9646.   already have something in our internal buffer.  This tends to work quite
  9647.   nicely, so the only really bad case remaining is the one in which neither
  9648.   FIONREAD or MYREAD are defined, which is increasingly rare these days.
  9649. */
  9650.     if (channel != 0 && my_count > 0) {
  9651.     debug(F101,"in_chk buf my_count","",my_count);
  9652.     n = my_count;            /* n was 0 before we got here */
  9653.     return(n);
  9654.     }
  9655. #endif /* MYREAD */
  9656. /*
  9657.   rdchk(), select(), and poll() tell us *if* data is available to be read, but
  9658.   not how much, so these should be used only as a final resort.  Especially
  9659.   since these calls tend to add a lot overhead.
  9660. */
  9661. #ifdef RDCHK                /* This mostly SCO-specific */
  9662.     n = rdchk(fd);
  9663.     debug(F101,"in_chk rdchk","",n);
  9664. #else /* No RDCHK */
  9665. #ifdef SELECT
  9666. #ifdef Plan9
  9667.     /* Only allows select on the console ... don't ask */
  9668.     if (channel == 0)
  9669. #endif /* Plan9 */
  9670.       {
  9671.     fd_set rfds;            /* Read file descriptors */
  9672. #ifdef BELLV10
  9673.     FD_ZERO(rfds);            /* Initialize them */
  9674.     FD_SET(fd,rfds);        /* We want to look at this fd */
  9675. #else
  9676.     FD_ZERO(&rfds);            /* Initialize them */
  9677.     FD_SET(fd,&rfds);        /* We want to look at this fd */
  9678.     tv.tv_sec = tv.tv_usec = 0L;    /* A 0-valued timeval structure */
  9679. #endif /* BELLV10 */
  9680. #ifdef Plan9
  9681.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9682.     debug(F101,"in_chk Plan 9 select","",n);
  9683. #else
  9684. #ifdef BELLV10
  9685.     n = select( 128, rfds, (fd_set *)0, (fd_set *)0, 0 );
  9686.     debug(F101,"in_chk BELLV10 select","",n);
  9687. #else
  9688. #ifdef BSD44
  9689.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9690.     debug(F101,"in_chk BSD44 select","",n);
  9691. #else
  9692. #ifdef BSD43
  9693.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9694.     debug(F101,"in_chk BSD43 select","",n);
  9695. #else
  9696. #ifdef SOLARIS
  9697.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9698.     debug(F101,"in_chk SOLARIS select","",n);
  9699. #else
  9700. #ifdef QNX6
  9701.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9702.     debug(F101,"in_chk QNX6 select","",n);
  9703. #else
  9704. #ifdef QNX
  9705.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9706.     debug(F101,"in_chk QNX select","",n);
  9707. #else
  9708. #ifdef COHERENT
  9709.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9710.     debug(F101,"in_chk COHERENT select","",n);
  9711. #else
  9712. #ifdef SVR4
  9713.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9714.     debug(F101,"in_chk SVR4 select","",n);
  9715. #else
  9716. #ifdef __linux__
  9717.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9718.     debug(F101,"in_chk LINUX select","",n);
  9719. #ifdef OSF
  9720.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9721.     debug(F101,"in_chk OSF select","",n);
  9722. #else
  9723.     n = select( FD_SETSIZE, &rfds, (int *)0, (int *)0, &tv );
  9724.     debug(F101,"in_chk catchall select","",n);
  9725. #endif /* OSF */
  9726. #endif /* __linux__ */
  9727. #endif /* SVR4 */
  9728. #endif /* COHERENT */
  9729. #endif /* QNX */
  9730. #endif /* QNX6 */
  9731. #endif /* SOLARIS */
  9732. #endif /* BSD43 */
  9733. #endif /* BSD44 */
  9734. #endif /* BELLV10 */
  9735. #endif /* Plan9 */
  9736.     }
  9737. #else  /* Not SELECT */
  9738. #ifdef CK_POLL
  9739.     {
  9740.       struct pollfd pfd;
  9741.  
  9742.       pfd.fd = fd;
  9743.       pfd.events = POLLIN;
  9744.       pfd.revents = 0;
  9745.       n = poll(&pfd, 1, 0);
  9746.       debug(F101,"in_chk poll","",n);
  9747.       if ((n > 0) && (pfd.revents & POLLIN))
  9748.     n = 1;
  9749.     }
  9750. #endif /* CK_POLL */
  9751. #endif /* SELECT */
  9752. #endif /* RDCHK */
  9753. #endif /* PROVX1 */
  9754. #endif /* V7 */
  9755. #endif /* FIONREAD */
  9756.  
  9757. /* From here down, treat console and communication device differently... */
  9758.  
  9759.     if (channel == 0) {            /* Console */
  9760.  
  9761. #ifdef SVORPOSIX
  9762. #ifndef FIONREAD
  9763. #ifndef SELECT
  9764. #ifndef CK_POLL
  9765. #ifndef RDCHK
  9766. /*
  9767.   This is the hideous hack used in System V and POSIX systems that don't
  9768.   support FIONREAD, rdchk(), select(), poll(), etc, in which the user's
  9769.   CONNECT-mode escape character is attached to SIGQUIT.  Used, obviously,
  9770.   only on the console.
  9771. */
  9772.     if (conesc) {            /* Escape character typed == SIGQUIT */
  9773.         debug(F100,"in_chk conesc","",conesc);
  9774.         conesc = 0;
  9775.         signal(SIGQUIT,esctrp);    /* Restore signal */
  9776.         n += 1;
  9777.     }
  9778. #endif /* RDCHK */
  9779. #endif /* CK_POLL */
  9780. #endif /* SELECT */
  9781. #endif /* FIONREAD */
  9782. #endif /* SVORPOSIX */
  9783.  
  9784.     return(n);            /* Done with console */
  9785.     }
  9786.  
  9787.     if (channel != 0) {            /* Communications connection */
  9788.  
  9789. #ifdef MYREAD
  9790. #ifndef FIONREAD
  9791. /*
  9792.   select() or rdchk(), etc, has told us that something is waiting, but we
  9793.   don't know how much.  So we do a read to get it and then we know.  Note:
  9794.   This read is NOT nonblocking if nothing is there (because of VMIN=1), but
  9795.   it should be safe in this case since the OS tells us at least one byte is
  9796.   waiting to be read, and MYREAD reads return as much as is there without
  9797.   waiting for any more.  Controlled tests on Solaris and Unixware (with
  9798.   FIONREAD deliberately undefined) show this to be true.
  9799. */
  9800.     debug(F101,"in_chk read my_count","",my_count);
  9801.     debug(F101,"in_chk read n","",n);
  9802.     if (n > 0 && my_count == 0) {
  9803.         /* This also catches disconnects etc */
  9804.         /* Do what mygetbuf does except don't grab a character */
  9805.         my_count = myfillbuf();
  9806.         my_item = -1;        /* ^^^ */
  9807.         debug(F101,"in_chk myfillbuf my_count","",my_count);
  9808.         if (my_count < 0)
  9809.           return(-1);
  9810.         else
  9811.           n = 0;            /* NB: n is replaced by my_count */
  9812.     }
  9813. #endif /* FIONREAD */
  9814. /*
  9815.   Here we add whatever we think is unread to what is still in our
  9816.   our internal buffer.  Thus the importance of setting n to 0 just above.
  9817. */
  9818.     debug(F101,"in_chk my_count","",my_count);
  9819.     debug(F101,"in_chk n","",n);
  9820.     if (my_count > 0)
  9821.       n += my_count;
  9822. #endif /* MYREAD */
  9823.     }
  9824.     debug(F101,"in_chk result","",n);
  9825.  
  9826.     /* Errors here don't prove the connection has dropped so just say 0 */
  9827.  
  9828.     return(n < 0 ? 0 : n);
  9829. }
  9830.  
  9831.  
  9832. /*  T T C H K  --  Tell how many characters are waiting in tty input buffer  */
  9833.  
  9834. int
  9835. ttchk() {
  9836.     int fd;
  9837. #ifdef NETCMD
  9838.     if (ttpipe)
  9839.       fd = fdin;
  9840.     else
  9841. #endif /* NETCMD */
  9842.       fd = ttyfd;
  9843.     return(in_chk(1,fd));
  9844. }
  9845.  
  9846. /*  T T X I N  --  Get n characters from tty input buffer  */
  9847.  
  9848. /*  Returns number of characters actually gotten, or -1 on failure  */
  9849.  
  9850. /*  Intended for use only when it is known that n characters are actually */
  9851. /*  Available in the input buffer.  */
  9852.  
  9853. int
  9854. ttxin(n,buf) int n; CHAR *buf; {
  9855.     register int x = 0, c = -2;
  9856. #ifdef TTLEBUF
  9857.     register int i = 0;
  9858. #endif /* TTLEBUF */
  9859.     int fd;
  9860.  
  9861.     if (n < 1)                /* Nothing to do */
  9862.       return(0);
  9863.  
  9864. #ifdef TTLEBUF
  9865.     if (ttpush >= 0) {
  9866.         buf[0] = ttpush;        /* Put pushed char in buffer*/
  9867.         ttpush = -1;            /* Clear the push buffer */
  9868.         if (ttchk() > 0)
  9869.       return(ttxin(n-1, &buf[1]) + 1);
  9870.         else
  9871.       return(1);
  9872.     }
  9873.     if (le_data) {
  9874.         while (le_inbuf() > 0) {
  9875.         if (le_getchar(&buf[i])) {
  9876.                 i++;
  9877.                 n--;
  9878.             }
  9879.         }
  9880.         if (ttchk() > 0)
  9881.       return(ttxin(n,&buf[i])+i);
  9882.         else
  9883.       return(i);
  9884.     }
  9885. #endif /* TTLEBUF */
  9886.  
  9887. #ifdef NETCMD
  9888.     if (ttpipe)
  9889.       fd = fdin;
  9890.     else
  9891. #endif /* NETCMD */
  9892.       fd = ttyfd;
  9893.  
  9894. #ifdef SUNX25
  9895.     if (netconn && (ttnet == NET_SX25))    /* X.25 connection */
  9896.       return(x25xin(n,buf));
  9897. #endif /* SUNX25 */
  9898.  
  9899. #ifdef IBMX25
  9900.     /* riehm: possibly not needed. Test worked with normal reads and writes */
  9901.     if (netconn && (ttnet == NET_IX25))    { /* X.25 connection */
  9902.     x = x25xin(n,buf);
  9903.     if (x > 0) buf[x] = '\0';
  9904.     return(x);
  9905.     }
  9906. #endif /* IBMX25 */
  9907.  
  9908. #ifdef MYREAD
  9909.     debug(F101,"ttxin MYREAD","",n);
  9910.     while (x < n) {
  9911.     c = myread();
  9912.     if (c < 0) {
  9913.         debug(F101,"ttxin myread returns","",c);
  9914.         if (c == -3) x = -1;
  9915.         break;
  9916.         }
  9917.     buf[x++] = c & ttpmsk;
  9918. #ifdef RLOGCODE
  9919. #ifdef CK_KERBEROS
  9920.         /* It is impossible to know how many characters are waiting */
  9921.         /* to be read when you are using Encrypted Rlogin or SSL    */
  9922.         /* as the transport since the number of real data bytes     */
  9923.         /* can be greater or less than the number of bytes on the   */
  9924.         /* wire which is what ttchk() returns.                      */
  9925.         if (netconn && (ttnproto == NP_EK4LOGIN || ttnproto == NP_EK5LOGIN))
  9926.       if (ttchk() <= 0)
  9927.         break;
  9928. #endif /* CK_KERBEROS */
  9929. #endif /* RLOGCODE */
  9930. #ifdef CK_SSL
  9931.         if (ssl_active_flag || tls_active_flag)
  9932.       if (ttchk() <= 0)
  9933.         break;
  9934. #endif /* CK_SSL */
  9935.     }
  9936. #else
  9937.     debug(F101,"ttxin READ","",n);
  9938.     x = read(fd,buf,n);
  9939.     for (c = 0; c < n; c++)        /* Strip any parity */
  9940.       buf[c] &= ttpmsk;
  9941. #endif /* MYREAD */
  9942.  
  9943.     debug(F101,"ttxin x","",x);        /* Done */
  9944.     if (x > 0) buf[x] = '\0';
  9945.     if (x < 0) x = -1;
  9946.     return(x);
  9947. }
  9948.  
  9949. /*  T T O L  --  Write string s, length n, to communication device.  */
  9950. /*
  9951.   Returns:
  9952.    >= 0 on success, number of characters actually written.
  9953.    -1 on failure.
  9954. */
  9955. #ifdef CK_ENCRYPTION
  9956. CHAR * xpacket = NULL;
  9957. int nxpacket = 0;
  9958. #endif /* CK_ENCRYPTION */
  9959.  
  9960. #define TTOLMAXT 5
  9961. int
  9962. ttol(s,n) int n; CHAR *s; {
  9963.     int x, len, tries, fd;
  9964. #ifdef CKXXCHAR
  9965.     extern int dblflag;            /* For SET SEND DOUBLE-CHARACTER */
  9966.     extern short dblt[];
  9967.     CHAR *p = NULL, *p2, *s2, c;
  9968.     int n2 = 0;
  9969. #endif /* CKXXCHAR */
  9970.  
  9971.     if (ttyfd < 0)            /* Not open? */
  9972.       return(-3);
  9973. #ifdef DEBUG
  9974.     if (deblog) hexdump("ttol s",s,n);
  9975. #endif /* DEBUG */
  9976.  
  9977. #ifdef NETCMD
  9978.     if (ttpipe)
  9979.       fd = fdout;
  9980.     else
  9981. #endif /* NETCMD */
  9982.       fd = ttyfd;
  9983.  
  9984. #ifdef CKXXCHAR
  9985. /*  Double any characters that must be doubled.  */
  9986.     debug(F101,"ttol dblflag","",dblflag);
  9987.     if (dblflag) {
  9988.     p = (CHAR *) malloc(n + n + 1);
  9989.     if (p) {
  9990.         s2 = s;
  9991.         p2 = p;
  9992.         n2 = 0;
  9993.         while (*s2) {
  9994.         c = *s2++;
  9995.         *p2++ = c;
  9996.         n2++;
  9997.         if (dblt[(unsigned) c] & 2) {
  9998.             *p2++ = c;
  9999.             n2++;
  10000.         }
  10001.         }
  10002.         s = p;
  10003.         n = n2;
  10004.         s[n] = '\0';
  10005.     }
  10006. #ifdef DEBUG
  10007.         if (deblog) hexdump("ttol doubled s",s,n);
  10008. #endif /* DEBUG */
  10009.     }
  10010. #endif /* CKXXCHAR */
  10011.  
  10012.     tries = TTOLMAXT;            /* Allow up to this many tries */
  10013.     len = n;                /* Remember original length */
  10014.  
  10015. #ifdef CK_ENCRYPTION
  10016. /*
  10017.   This is to avoid encrypting a packet that is already encrypted, e.g.
  10018.   when we resend a packet directly out of the packet buffer, and also to
  10019.   avoid encrypting a constant (literal) string, which can cause a memory
  10020.   fault.
  10021. */
  10022.     if (TELOPT_ME(TELOPT_ENCRYPTION)) {
  10023.     int x;
  10024.     if (nxpacket < n) {
  10025.         if (xpacket) {
  10026.         free(xpacket);
  10027.         xpacket = NULL;
  10028.         nxpacket = 0;
  10029.         }
  10030.         x = n > 10240 ? n : 10240;
  10031.         xpacket = (CHAR *)malloc(x);
  10032.         if (!xpacket) {
  10033.         fprintf(stderr,"ttol malloc failure\n");
  10034.         return(-1);
  10035.         } else
  10036.           nxpacket = x;
  10037.     }
  10038.     memcpy((char *)xpacket,(char *)s,n);
  10039.     s = xpacket;
  10040.     ck_tn_encrypt((char *)s,n);
  10041.     }
  10042. #endif /* CK_ENCRYPTION */
  10043.  
  10044.     while (n > 0 &&
  10045.        (tries-- > 0
  10046. #ifdef CK_ENCRYPTION
  10047.         /* keep trying if we are encrypting */
  10048.         || TELOPT_ME(TELOPT_ENCRYPTION)
  10049. #endif /* CK_ENCRYPTION */
  10050.             )) {            /* Be persistent */
  10051.     debug(F101,"ttol try","",TTOLMAXT - tries);
  10052. #ifdef BEOSORBEBOX
  10053.         if (netconn && !ttpipe && !ttpty)
  10054.       x = nettol((char *)s,n);    /* Write string to device */
  10055.         else
  10056. #endif /* BEOSORBEBOX */
  10057. #ifdef IBMX25
  10058.       if (ttnet == NET_IX25)
  10059.         /*
  10060.          * this is a more controlled way of writing to X25
  10061.          * STREAMS, however write should also work!
  10062.          */
  10063.         x = x25write(ttyfd, s, n);
  10064.       else
  10065. #endif /* IBMX25 */
  10066. #ifdef CK_SSL
  10067.         if (ssl_active_flag || tls_active_flag) {
  10068.         int error;
  10069.         /* Write using SSL */
  10070.                 ssl_retry:
  10071.         if (ssl_active_flag)
  10072.                   x = SSL_write(ssl_con, s, n);
  10073.         else
  10074.                   x = SSL_write(tls_con, s, n);
  10075.         switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,x)) {
  10076.                 case SSL_ERROR_NONE:
  10077.                     if (x == n)
  10078.               return(len);
  10079.                     s += x;
  10080.                     n -= x;
  10081.                     goto ssl_retry;
  10082.           case SSL_ERROR_WANT_WRITE:
  10083.           case SSL_ERROR_WANT_READ:
  10084.             x = 0;
  10085.             break;
  10086.           case SSL_ERROR_SYSCALL:
  10087.                     if (x != 0)
  10088.               return(-1);
  10089.           case SSL_ERROR_WANT_X509_LOOKUP:
  10090.           case SSL_ERROR_SSL:
  10091.           case SSL_ERROR_ZERO_RETURN:
  10092.           default:
  10093.             ttclos(0);
  10094.             return(-3);
  10095.         }
  10096.         } else
  10097. #endif /* CK_SSL */
  10098. #ifdef CK_KERBEROS
  10099. #ifdef KRB4
  10100. #ifdef RLOGCODE
  10101.         if (ttnproto == NP_EK4LOGIN) {
  10102.         return(krb4_des_write(ttyfd,s,n));
  10103.         } else
  10104. #endif /* RLOGCODE */
  10105. #endif /* KRB4 */
  10106. #ifdef KRB5
  10107. #ifdef RLOGCODE
  10108.             if (ttnproto == NP_EK5LOGIN) {
  10109.                 return(krb5_des_write(ttyfd,s,n));
  10110.             } else
  10111. #endif /* RLOGCODE */
  10112. #ifdef KRB5_U2U
  10113.             if (ttnproto == NP_K5U2U) {
  10114.                 return(krb5_u2u_write(ttyfd,s,n));
  10115.             } else
  10116. #endif /* KRB5_U2U */
  10117. #endif /* KRB5 */
  10118. #endif /* CK_KERBEROS */
  10119.           x = write(fd,s,n);    /* Write string to device */
  10120.  
  10121.     if (x == n) {            /* Worked? */
  10122.         debug(F101,"ttol ok","",x);    /* OK */
  10123. #ifdef CKXXCHAR
  10124.         if (p) free(p);
  10125. #endif /* CKXXCHAR */
  10126.         return(len);        /* Done */
  10127.     } else if (x < 0) {        /* No, got error? */
  10128.         debug(F101,"ttol write error","",errno);
  10129. #ifdef EWOULDBLOCK
  10130.         if (errno == EWOULDBLOCK) {
  10131.         msleep(10);
  10132.         continue;
  10133.         } else
  10134. #endif /* EWOULDBLOCK */
  10135. #ifdef TCPSOCKET
  10136.         if (netconn && ttnet == NET_TCPB) {
  10137.         debug(F101,"ttol TCP error","",errno);
  10138.         ttclos(0);        /* Close the connection. */
  10139.         x = -3;
  10140.         }
  10141. #endif /* TCPSOCKET */
  10142. #ifdef CKXXCHAR
  10143.         if (p) free(p);
  10144. #endif /* CKXXCHAR */
  10145.         return(x);
  10146.     } else {            /* No error, so partial success */
  10147.         debug(F101,"ttol partial","",x); /* This never happens */
  10148.         s += x;            /* Point to part not written yet */
  10149.         n -= x;            /* Adjust length */
  10150.         if (x > 0) msleep(10);    /* Wait 10 msec */
  10151.     }                /* Go back and try again */
  10152.     }
  10153. #ifdef CKXXCHAR
  10154.     if (p) free(p);
  10155. #endif /* CKXXCHAR */
  10156.     return(n < 1 ? len : -1);        /* Return the results */
  10157. }
  10158.  
  10159. /*  T T O C  --  Output a character to the communication line  */
  10160.  
  10161. /*
  10162.  This function should only be used for interactive, character-mode operations,
  10163.  like terminal connection, script execution, dialer i/o, where the overhead
  10164.  of the signals and alarms does not create a bottleneck.
  10165. */
  10166. int
  10167. #ifdef CK_ANSIC
  10168. ttoc(char c)
  10169. #else
  10170. ttoc(c) char c;
  10171. #endif /* CK_ANSIC */
  10172. /* ttoc */ {
  10173. #define TTOC_TMO 15            /* Timeout in case we get stuck */
  10174.     int xx, fd;
  10175.  
  10176.     if (ttyfd < 0)            /* Check for not open. */
  10177.       return(-1);
  10178.  
  10179. #ifdef NETCMD
  10180.     if (ttpipe)
  10181.       fd = fdout;
  10182.     else
  10183. #endif /* NETCMD */
  10184.       fd = ttyfd;
  10185.  
  10186.     c &= 0xff;
  10187.     /* debug(F101,"ttoc","",(CHAR) c); */
  10188.     saval = signal(SIGALRM,timerh);    /* Enable timer interrupt */
  10189.     xx = alarm(TTOC_TMO);        /* for this many seconds. */
  10190.     if (xx < 0) xx = 0;            /* Save old alarm value. */
  10191.     /* debug(F101,"ttoc alarm","",xx); */
  10192.     if (
  10193. #ifdef CK_POSIX_SIG
  10194.     sigsetjmp(sjbuf,1)
  10195. #else
  10196.     setjmp(sjbuf)
  10197. #endif /* CK_POSIX_SIG */
  10198.     ) {        /* Timer went off? */
  10199.     ttimoff();            /* Yes, cancel this alarm. */
  10200.     if (xx - TTOC_TMO > 0) alarm(xx - TTOC_TMO); /* Restore previous one */
  10201.         /* debug(F100,"ttoc timeout","",0); */
  10202. #ifdef NETCONN
  10203.     if (!netconn) {
  10204. #endif /* NETCONN */
  10205.         debug(F101,"ttoc timeout","",c);
  10206.         if (ttflow == FLO_XONX) {
  10207.         debug(F101,"ttoc flow","",ttflow); /* Maybe we're xoff'd */
  10208. #ifndef Plan9
  10209. #ifdef POSIX
  10210.         /* POSIX way to unstick. */
  10211.         debug(F100,"ttoc tcflow","",tcflow(ttyfd,TCOON));
  10212. #else
  10213. #ifdef BSD4                /* Berkeley way to do it. */
  10214. #ifdef TIOCSTART
  10215. /* .... Used to be "ioctl(ttyfd, TIOCSTART, 0);".  Who knows? */
  10216.         {
  10217.           int x = 0;
  10218.           debug(F101,"ttoc TIOCSTART","",ioctl(ttyfd, TIOCSTART, &x));
  10219.         }
  10220. #endif /* TIOCSTART */
  10221. #endif /* BSD4 */
  10222.                     /* Is there a Sys V way to do this? */
  10223. #endif /* POSIX */
  10224. #endif /* Plan9 */
  10225.         }
  10226. #ifdef NETCONN
  10227.         }
  10228. #endif /* NETCONN */
  10229.     return(-1);            /* Return failure code. */
  10230.     } else {
  10231.         int rc;
  10232. #ifdef BEOSORBEBOX
  10233. #ifdef NETCONN
  10234.         if (netconn && !ttpipe && !ttpty)
  10235.       rc = nettoc(c);
  10236.         else
  10237. #endif /*  BEOSORBEBOX */
  10238. #endif /* NETCONN */
  10239. #ifdef CK_ENCRYPTION
  10240.       if (TELOPT_ME(TELOPT_ENCRYPTION))
  10241.         ck_tn_encrypt(&c,1);
  10242. #endif /* CK_ENCRYPTION */
  10243. #ifdef IBMX25
  10244.     /* riehm: maybe this isn't necessary after all. Test program
  10245.      * worked fine with data being sent and retrieved with normal
  10246.      * read's and writes!
  10247.      */
  10248.     if (ttnet == NET_IX25)
  10249.       rc = x25write(ttyfd,&c,1); /* as above for X25 streams */
  10250.     else
  10251. #endif /* IBMX25 */
  10252. #ifdef CK_SSL
  10253.       if (ssl_active_flag || tls_active_flag) {
  10254.           int error;
  10255.           /* Write using SSL */
  10256.           if (ssl_active_flag)
  10257.                 rc = SSL_write(ssl_con, &c, 1);
  10258.           else
  10259.                 rc = SSL_write(tls_con, &c, 1);
  10260.           switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,rc)){
  10261.         case SSL_ERROR_NONE:
  10262.           break;
  10263.         case SSL_ERROR_WANT_WRITE:
  10264.         case SSL_ERROR_WANT_READ:
  10265.           rc = 0;
  10266.           break;
  10267.         case SSL_ERROR_SYSCALL:
  10268.           if (rc != 0)
  10269.             return(-1);
  10270.         case SSL_ERROR_WANT_X509_LOOKUP:
  10271.         case SSL_ERROR_SSL:
  10272.         case SSL_ERROR_ZERO_RETURN:
  10273.         default:
  10274.           ttclos(0);
  10275.           return(-1);
  10276.           }
  10277.       } else
  10278. #endif /* CK_SSL */
  10279. #ifdef CK_KERBEROS
  10280. #ifdef KRB4
  10281. #ifdef RLOGCODE
  10282.       if (ttnproto == NP_EK4LOGIN) {
  10283.           rc = (krb4_des_write(ttyfd,&c,1) == 1);
  10284.       } else
  10285. #endif /* RLOGCODE */
  10286. #endif /* KRB4 */
  10287. #ifdef KRB5
  10288. #ifdef RLOGCODE
  10289.           if (ttnproto == NP_EK5LOGIN) {
  10290.               rc = (krb5_des_write(ttyfd,&c,1) == 1);
  10291.           } else
  10292. #endif /* RLOGCODE */
  10293. #ifdef KRB5_U2U
  10294.           if (ttnproto == NP_K5U2U) {
  10295.               rc = (krb5_u2u_write(ttyfd,&c,1) == 1);
  10296.           } else
  10297. #endif /* KRB5_U2U */
  10298. #endif /* KRB5 */
  10299. #endif /* CK_KERBEROS */
  10300.         rc = write(fd,&c,1);    /* Try to write the character. */
  10301.     if (rc < 1) {            /* Failed */
  10302.         ttimoff();            /* Turn off the alarm. */
  10303.         alarm(xx);            /* Restore previous alarm. */
  10304.         debug(F101,"ttoc errno","",errno); /* Log the error, */
  10305.         return(-1);            /* and return the error code. */
  10306.     }
  10307.     }
  10308.     ttimoff();                /* Success, turn off the alarm. */
  10309.     alarm(xx);                /* Restore previous alarm. */
  10310.     return(0);                /* Return good code. */
  10311. }
  10312.  
  10313. /*  T T I N L  --  Read a record (up to break character) from comm line.  */
  10314. /*
  10315.   Reads up to "max" characters from the communication line, terminating on:
  10316.     (a) the packet length field if the "turn" argument is zero, or
  10317.     (b) on the packet-end character (eol) if the "turn" argument is nonzero
  10318.     (c) a certain number of Ctrl-C's in a row
  10319.  
  10320.   Returns:
  10321.     >= 0, the number of characters read upon success;
  10322.     -1 if "max" exceeded, timeout, or other correctable error;
  10323.     -2 on user interruption (c);
  10324.     -3 on fatal error like connection lost.
  10325.  
  10326.   The characters that were input are copied into "dest" with their parity bits
  10327.   stripped if parity was selected.  Returns the number of characters read.
  10328.   Characters after the eol are available upon the next call to this function.
  10329.  
  10330.   The idea is to minimize the number of system calls per packet, and also to
  10331.   minimize timeouts.  This function is the inner loop of the protocol and must
  10332.   be as efficient as possible.  The current strategy is to use myread().
  10333.  
  10334.   WARNING: This function calls parchk(), which is defined in another module.
  10335.   Normally, ckutio.c does not depend on code from any other module, but there
  10336.   is an exception in this case because all the other ck?tio.c modules also
  10337.   need to call parchk(), so it's better to have it defined in a common place.
  10338. */
  10339. #ifdef CTRLC
  10340. #undef CTRLC
  10341. #endif /* CTRLC */
  10342. #define CTRLC '\03'
  10343. /*
  10344.   We have four different declarations here because:
  10345.   (a) to allow Kermit to be built without the automatic parity sensing feature
  10346.   (b) one of each type for ANSI C, one for non-ANSI.
  10347. */
  10348.  
  10349. static int csave = -1;
  10350.  
  10351. #ifndef NOXFER
  10352. int
  10353. #ifdef PARSENSE
  10354. #ifdef CK_ANSIC
  10355. ttinl(CHAR *dest, int max,int timo, CHAR eol, CHAR start, int turn)
  10356. #else
  10357. ttinl(dest,max,timo,eol,start,turn) int max,timo,turn; CHAR *dest, eol, start;
  10358. #endif /* CK_ANSIC */
  10359. #else /* not PARSENSE */
  10360. #ifdef CK_ANSIC
  10361. ttinl(CHAR *dest, int max,int timo, CHAR eol)
  10362. #else
  10363. ttinl(dest,max,timo,eol) int max,timo; CHAR *dest, eol;
  10364. #endif /* __SDTC__ */
  10365. #endif /* PARSENSE */
  10366. /* ttinl */ {
  10367.  
  10368. #ifndef MYREAD
  10369.     CHAR ch, dum;
  10370. #endif /* MYREAD */
  10371. #ifdef PARSENSE
  10372.     int pktlen = -1;
  10373.     int lplen = 0;
  10374.     int havelen = 0;
  10375. #endif /* PARSENSE */
  10376.     int fd;
  10377.     int sopmask = 0xff;            /* Start-Of-Packet mask */
  10378. #ifdef CKXXCHAR
  10379.     extern short dblt[];        /* Ignore-character table */
  10380.     extern int ignflag;
  10381. #endif /* CKXXCHAR */
  10382. #ifdef TCPSOCKET
  10383.     extern CHAR stchr;
  10384. #endif /* TCPSOCKET */
  10385.     int x;
  10386. #ifdef STREAMING
  10387.     extern int streaming;
  10388.     extern int sndtyp;
  10389. #endif /* STREAMING */
  10390.  
  10391.     if (ttyfd < 0) return(-3);          /* Not open. */
  10392.  
  10393.     debug(F101,"ttinl max","",max);
  10394.     debug(F101,"ttinl timo","",timo);
  10395.  
  10396. #ifdef NETCMD
  10397.     if (ttpipe)
  10398.       fd = fdin;
  10399.     else
  10400. #endif /* NETCMD */
  10401.       fd = ttyfd;
  10402.  
  10403. #ifdef COMMENT
  10404.     if (xlocal && conchk() > 0)        /* Allow for console interruptions */
  10405.       return(-1);
  10406. #endif /* COMMENT */
  10407.  
  10408.     *dest = '\0';                       /* Clear destination buffer */
  10409.     if (timo < 0) timo = 0;        /* Safety */
  10410.     if (timo) {                /* Don't time out if timo == 0 */
  10411.     int xx;
  10412.     saval = signal(SIGALRM,timerh);    /* Enable timer interrupt */
  10413.     xx = alarm(timo);        /* Set it. */
  10414.     debug(F101,"ttinl alarm","",xx);
  10415.     }
  10416.     if (
  10417. #ifdef CK_POSIX_SIG
  10418.     sigsetjmp(sjbuf,1)
  10419. #else
  10420.     setjmp(sjbuf)
  10421. #endif /* CK_POSIX_SIG */
  10422.     ) {                /* Timer went off? */
  10423.     debug(F100,"ttinl timout","",0); /* Get here on timeout. */
  10424.     /* debug(F110," with",(char *) dest,0); */
  10425.     ttimoff();            /* Turn off timer */
  10426.     return(-1);            /* and return error code. */
  10427.     } else {
  10428.     register int i, n = -1;        /* local variables */
  10429.     int ccn = 0;
  10430. #ifdef PARSENSE
  10431.     register int flag = 0;
  10432.     debug(F000,"ttinl start","",start);
  10433. #endif /* PARSENSE */
  10434.  
  10435.     ttpmsk = ttprty ? 0177 : 0377;    /* Set parity stripping mask. */
  10436.     sopmask = needpchk ? 0177 : ttpmsk; /* And SOP matching mask. */
  10437.  
  10438. /* Now read into destination, stripping parity and looking for the */
  10439. /* the packet terminator, and also for several Ctrl-C's typed in a row. */
  10440.  
  10441.     i = 0;                /* Destination index */
  10442.     debug(F101,"ttinl eol","",eol);
  10443.  
  10444.     while (i < max-1) {
  10445. #ifdef MYREAD
  10446.         /* debug(F101,"ttinl i","",i); */
  10447.         errno = 0;
  10448.         if (csave > -1) {
  10449.             n = csave;
  10450.         debug(F101,"ttinl unsaving","",n);
  10451.         } else
  10452. #ifdef COMMENT
  10453.           if (xlocal && conchk() > 0) {
  10454.           /* Here we could catch keyboard interruptions. */
  10455.           /* But this would be VERY expensive. */
  10456.           /* We could also do it in myread() but it would be */
  10457.           /* expensive there too -- even if done with select()... */
  10458.           }
  10459. #endif /* COMMENT */
  10460.           if ((n = myread()) < 0) {    /* Timeout or i/o error? */
  10461. #ifdef DEBUG
  10462.         if (deblog) {
  10463.             debug(F101,"ttinl myread failure, n","",n);
  10464.             debug(F101,"ttinl myread errno","",errno);
  10465.         }
  10466. #endif /* DEBUG */
  10467.         /* Don't let EINTR break packets. */
  10468.         if (n == -3) {
  10469.             if (errno == EINTR && i > 0) {
  10470.             debug(F111,"ttinl EINTR myread i","continuing",i);
  10471.             continue;
  10472.             } else {
  10473.             debug(F110,"ttinl non-EINTR -3","closing",0);
  10474.             wasclosed = 1;
  10475.             ttimoff();    /* Turn off timer */
  10476.             ttclos(0);
  10477.             return(n);
  10478.             }
  10479.         } else if (n == -2 && netconn /* && timo == 0 */ ) {
  10480.             /* Here we try to catch broken network connections */
  10481.             /* even when ioctl() and read() do not catch them */
  10482.             debug(F111,"ttinl network myread failure","closing",n);
  10483.             wasclosed = 1;
  10484.             ttimoff();
  10485.             ttclos(0);
  10486.             return(-3);
  10487.         }
  10488. #ifdef STREAMING
  10489.         /* Streaming and no data to read */
  10490.         else if (n == 0 && streaming && sndtyp == 'D')
  10491.           return(0);
  10492. #endif /* STREAMING */
  10493.         break;            /* Break out of while loop */
  10494.         }
  10495.  
  10496. #else /* not MYREAD (is this code used anywhere any more?) */
  10497.  
  10498.         if (csave > -1)        /* Char saved from last time */
  10499.           ch = csave;
  10500.         else if ((n = read(fd, &ch, 1)) < 1)
  10501.           break;            /* Error - break out of while loop */
  10502.         n = ch;
  10503.  
  10504. #endif /* MYREAD */
  10505.  
  10506.         /* Get here with char in n */
  10507.  
  10508. #ifdef CK_ENCRYPTION
  10509.         /* If csave > -1 we already decrypted this character */
  10510.         /* So don't decrypt it again */
  10511.         if (TELOPT_U(TELOPT_ENCRYPTION) && csave == -1) {
  10512.         CHAR ch = n;
  10513.         ck_tn_decrypt(&ch,1);
  10514.         n = ch;
  10515.         }
  10516. #endif /* CK_ENCRYPTION */
  10517.  
  10518.         csave = -1;            /* Unflag that we unsaved a char */
  10519.  
  10520. #ifdef TCPSOCKET
  10521.         if (n == IAC &&        /* Handle Telnet options */
  10522.         ((xlocal && netconn && IS_TELNET()) ||
  10523.         (!xlocal && sstelnet))) {
  10524.         n = tt_tnopt(n);
  10525.         if (n < 0)
  10526.           return(n);
  10527. #ifndef NOPARSEN
  10528.         else if (n == 1)
  10529.           start = stchr;
  10530. #endif /* NOPARSEN */
  10531.         if (n != 255)        /* No data - go back for next char */
  10532.           continue;
  10533.         }                /* Quoted IAC - keep going */
  10534. #endif /* TCPSOCKET */
  10535. #ifdef CKXXCHAR
  10536.         if (ignflag)
  10537.           if (dblt[(unsigned) n] & 1) /* Character to ignore? */
  10538.         continue;
  10539. #endif /* CKXXCHAR */
  10540.  
  10541. /*
  10542.   Use parity mask, rather than always stripping parity, to check for
  10543.   cancellation.  Otherwise, runs like \x03\x83\x03 in a packet could cancel
  10544.   the transfer when parity is NONE.  (Note that \x03\x03\x03 is extremely
  10545.   unlikely due to run-length encoding.)
  10546. */
  10547.         /* Check cancellation */
  10548.         if (!xlocal && xfrcan && ((n & ttpmsk) == xfrchr)) {
  10549.         if (++ccn >= xfrnum) {    /* If xfrnum in a row, bail out. */
  10550.             if (timo) {        /* Clear timer. */
  10551.             ttimoff();
  10552.             }
  10553.             if (xfrchr < 32)
  10554.               printf("^%c...\r\n",(char)(xfrchr+64));
  10555.             else
  10556.               printf("Canceled...\r\n");
  10557.             return(-2);
  10558.         }
  10559.         } else ccn = 0;        /* No cancellation, reset counter, */
  10560.  
  10561. #ifdef PARSENSE
  10562.         if (flag == 0) {        /* Find the Start-Of-Packet. */
  10563.         if ((n & sopmask) == start) { /* Got it */
  10564.             flag = 1;
  10565.         } else {        /* Keep looking... */
  10566.             debug(F000,"ttinl skipping","",n);
  10567.             continue;
  10568.         }
  10569.         }
  10570.         dest[i++] = n & ttpmsk;
  10571. /*
  10572.   If we have not been instructed to wait for a turnaround character, we
  10573.   can go by the packet length field.  If turn != 0, we must wait for the
  10574.   end of line (eol) character before returning.  This is an egregious
  10575.   violation of all principles of layering...
  10576. */
  10577.         if (!havelen) {
  10578.         if (i == 2) {
  10579.             pktlen = xunchar(dest[1] & 0x7f);
  10580.             if (pktlen > 1) {
  10581.             havelen = 1;
  10582.             debug(F101,"ttinl length","",pktlen);
  10583.             }
  10584.         } else if (i == 5 && pktlen == 0) {
  10585.             lplen = xunchar(dest[4] & 0x7f);
  10586.         } else if (i == 6 && pktlen == 0) {
  10587.             pktlen = lplen * 95 + xunchar(dest[5] & 0x7f) + 5;
  10588.             havelen = 1;
  10589.             debug(F101,"ttinl extended length","",pktlen);
  10590.         }
  10591.         }
  10592.  
  10593. /*
  10594.   Suppose we looked at the sequence number here and found it was out of
  10595.   range?  This would mean either (a) incoming packets had SOP unprefixed
  10596.   and we are out of sync, or (b) the packet is damaged.  Since (a) is bad
  10597.   practice, let's ignore it.  So what should we do here if we know the
  10598.   packet is damaged?
  10599.  
  10600.    1. Nothing -- keep trying to read the packet till we find what we think
  10601.       is the end, or we time out, and let the upper layer decide what to
  10602.       do.  But since either the packet is corrupt or we are out of sync,
  10603.       our criterion for finding the end does not apply and we are likely
  10604.       to time out (or swallow a piece of the next packet) if our assumed
  10605.       length is too long.  (This was the behavior prior to version 7.0.)
  10606.  
  10607.    2. set flag = 0 and continue?  This would force us to wait for the
  10608.       next packet to come in, and therefore (in the nonwindowing case),
  10609.       would force a timeout in the other Kermit.
  10610.  
  10611.    3. set flag = 0 and continue, but only if the window size is > 1 and
  10612.       the window is not blocked?  Talk about cheating!
  10613.  
  10614.    4. Return a failure code and let the upper layer decide what to do.
  10615.       This should be equivalent to 3, but without the cheating.  So let's
  10616.       do it that way...  But note that we must ignore the parity bit
  10617.       in case this is the first packet and we have not yet run parchk().
  10618. */
  10619.         if (i == 3) {        /* Peek at sequence number */
  10620.         x = xunchar((dest[i-1] & 0x7f)); /* If it's not in range... */
  10621.         if (x < 0 || x > 63) {
  10622.             debug(F111,"ttinl bad seq",dest,x);
  10623.             if (timo) ttimoff();
  10624.             return(-1);        /* return a nonfatal error */
  10625.         }
  10626.         }
  10627.  
  10628. #else /* PARSENSE */
  10629.         dest[i++] = n & ttpmsk;
  10630. #endif /* PARSENSE */
  10631.  
  10632.     /* Check for end of packet */
  10633.  
  10634.         if (
  10635. #ifdef PARSENSE
  10636. /*
  10637.   Purely length-driven if SET HANDSHAKE NONE (i.e. turn == 0).
  10638.   This allows packet terminators and handshake characters to appear
  10639.   literally inside a packet data field.
  10640. */
  10641.         (havelen && (i > pktlen+1) &&
  10642.          (!turn || (turn && (n & 0x7f) == turn))) /* (turn, not eol) */
  10643. #else /* !PARSENSE */
  10644. /*
  10645.   Built without PARSENSE, so just look for packet terminator.
  10646. */
  10647.         ((n & 0x7f) == eol)
  10648. #endif /* PARSENSE */
  10649.         ) {
  10650. #ifndef PARSENSE
  10651.         debug(F101,"ttinl got eol","",eol); /* (or turn) */
  10652.         dest[i] = '\0';        /* Yes, terminate the string, */
  10653.         /* debug(F101,"ttinl i","",i); */
  10654. #else
  10655. #ifdef DEBUG
  10656.         if (deblog) {
  10657.             if ((n & 0x7f) != eol) {
  10658.             debug(F101,"ttinl EOP length","",pktlen);
  10659.             debug(F101,"ttinl i","",i);
  10660. #ifdef MYREAD
  10661. #ifdef PARSENSE
  10662. /*
  10663.   We read a packet based on its length.  This leaves the EOP character still
  10664.   unread, and so ttchk() will always return at least 1 because of this.  But
  10665.   if we know it is there, we can safely get rid of it.  So...
  10666. */
  10667.             {
  10668.                 int x;
  10669.                 while (my_count > 0) {
  10670.                 x = ttinc(0);
  10671.                 /* Start of next packet */
  10672.                 if (x == start) { /* Save for next time */
  10673.                     csave = (unsigned)((unsigned)x & 0xff);
  10674.                     debug(F000,"ttinl csaved","",x);
  10675.                     break;
  10676.                 }
  10677.                 debug(F000,"ttinl removed","",x);
  10678.                 }
  10679.             }
  10680. #endif /* PARSENSE */
  10681. #endif /* MYREAD */
  10682.  
  10683.             } else debug(F101,"ttinl got eol","",eol); /* (or turn) */
  10684.         }
  10685. #endif /* DEBUG */
  10686.         dest[i] = '\0';        /* Terminate the string, */
  10687.             if (needpchk) {        /* Parity checked yet? */
  10688.             if (ttprty == 0) {    /* No, check. */
  10689.             if ((ttprty = parchk(dest,start,i)) > 0) {
  10690.                 int j;
  10691.                 debug(F101,"ttinl senses parity","",ttprty);
  10692.                 debug(F110,"ttinl packet before",dest,0);
  10693.                 ttpmsk = 0x7f;
  10694.                 for (j = 0; j < i; j++)
  10695.                   dest[j] &= 0x7f;    /* Strip parity from packet */
  10696.                 debug(F110,"ttinl packet after ",dest,0);
  10697.             } else ttprty = 0; /* Restore if parchk error */
  10698.             }
  10699.             sopmask = ttprty;
  10700.             needpchk = 0;
  10701.         }
  10702. #endif /* PARSENSE */
  10703.         if (timo) {        /* Turn off timer. */
  10704.             ttimoff();
  10705.         }
  10706. #ifdef COMMENT
  10707.         debug(F011,"ttinl got", dest, (i < 60) ? i : -60);
  10708. #else /* COMMENT */
  10709.                 hexdump("ttinl got",dest,i);
  10710. #endif /* COMMENT */
  10711. #ifdef STREAMING
  10712.         /* ttinl() was called because there was non-packet */
  10713.         /* data sitting int the channel.  Ignore it.       */
  10714.         if (streaming && sndtyp == 'D')
  10715.           return(-1);
  10716. #endif /* STREAMING */
  10717.         return(i);
  10718.         }
  10719.     } /* End of while() */
  10720.     ttimoff();
  10721.     return(n);
  10722.     }
  10723. }
  10724. #endif /* NOXFER */
  10725.  
  10726. /*  T T I N C --  Read a character from the communication line  */
  10727. /*
  10728.  On success, returns the character that was read, >= 0.
  10729.  On failure, returns -1 or other negative myread error code,
  10730.    or -2 if connection is broken or ttyfd < 0.
  10731.    or -3 if session limit has expired,
  10732.    or -4 if something or other...
  10733.  NOTE: The API does not provide for ttinc() returning a special code
  10734.  upon timeout, but we need it.  So for this we have a global variable,
  10735.  ttinctimo.
  10736. */
  10737. static int ttinctimo = 0;        /* Yuk */
  10738.  
  10739. int
  10740. ttinc(timo) int timo; {
  10741.  
  10742.     int n = 0, fd;
  10743.     int is_tn = 0;
  10744.     CHAR ch = 0;
  10745.  
  10746.     ttinctimo = 0;
  10747.  
  10748.     if (ttyfd < 0) return(-2);          /* Not open. */
  10749.  
  10750.     is_tn = (xlocal && netconn && IS_TELNET()) ||
  10751.         (!xlocal && sstelnet);
  10752.  
  10753. #ifdef TTLEBUF
  10754.     if (ttpush >= 0) {
  10755.         debug(F111,"ttinc","ttpush",ttpush);
  10756.         ch = ttpush;
  10757.         ttpush = -1;
  10758.         return(ch);
  10759.     }
  10760.     if (le_data) {
  10761.         if (le_getchar(&ch) > 0) {
  10762.             debug(F111,"ttinc le_getchar","ch",ch);
  10763.             return(ch);
  10764.         }
  10765.     }
  10766. #endif /* TTLEBUF */
  10767.  
  10768. #ifdef NETCMD
  10769.     if (ttpipe)
  10770.       fd = fdin;
  10771.     else
  10772. #endif /* NETCMD */
  10773.       fd = ttyfd;
  10774.  
  10775.     if ((timo <= 0)            /* Untimed. */
  10776. #ifdef MYREAD
  10777.     || (my_count > 0)        /* Buffered char already waiting. */
  10778. #endif /* MYREAD */
  10779.     ) {
  10780. #ifdef MYREAD
  10781.         /* Comm line failure returns -1 thru myread, so no &= 0377 */
  10782.     n = myread();            /* Wait for a character... */
  10783.     /* debug(F000,"ttinc MYREAD n","",n); */
  10784. #ifdef CK_ENCRYPTION
  10785.     /* debug(F101,"ttinc u_encrypt","",TELOPT_U(TELOPT_ENCRYPTION)); */
  10786.     if (TELOPT_U(TELOPT_ENCRYPTION) && n >= 0) {
  10787.         ch = n;
  10788.         ck_tn_decrypt(&ch,1);
  10789.         n = ch;
  10790.     }
  10791. #endif /* CK_ENCRYPTION */
  10792.  
  10793. #ifdef NETPTY
  10794.     if (ttpty && n < 0) {
  10795.         debug(F101,"ttinc error on pty","",n);
  10796.         ttclos(0);
  10797.         return(n);
  10798.     }
  10799. #endif /* NETPTY */
  10800.  
  10801. #ifdef TNCODE
  10802.     if ((n > -1) && is_tn)
  10803.       return((unsigned)(n & 0xff));
  10804.     else
  10805. #endif /* TNCODE */
  10806.       return(n < 0 ? n : (unsigned)(n & ttpmsk));
  10807.  
  10808. #else  /* MYREAD */
  10809.  
  10810.         while ((n = read(fd,&ch,1)) == 0) /* Wait for a character. */
  10811.         /* Shouldn't have to loop in ver 5A. */
  10812. #ifdef NETCONN
  10813.       if (netconn) {        /* Special handling for net */
  10814.           netclos();        /* If read() returns 0 it means */
  10815.           netconn = 0;        /* the connection has dropped. */
  10816.           errno = ENOTCONN;
  10817.           return(-2);
  10818.       }
  10819. #endif /* NETCONN */
  10820.       ;
  10821.     /* debug(F101,"ttinc","",ch); */
  10822. #ifdef TNCODE
  10823.     if ((n > 0) && is_tn) {
  10824. #ifdef CK_ENCRYPTION
  10825.         if (TELOPT_U(TELOPT_ENCRYPTION)) {
  10826.         ck_tn_decrypt(&ch,1);
  10827.         n = ch;
  10828.         }
  10829. #endif /* CK_ENCRYPTION */
  10830.         return((unsigned)(ch & 0xff));
  10831.     } else
  10832. #endif /* TNCODE */
  10833.         return((n < 0) ? -4 : ((n == 0) ? -1 : (unsigned)(ch & ttpmsk)));
  10834. #endif /* MYREAD */
  10835.  
  10836.     } else {                /* Timed read */
  10837.  
  10838.     int oldalarm;
  10839.     saval = signal(SIGALRM,timerh);    /* Set up handler, save old one. */
  10840.     oldalarm = alarm(timo);        /* Set alarm, save old one. */
  10841.     if (
  10842. #ifdef CK_POSIX_SIG
  10843.         sigsetjmp(sjbuf,1)
  10844. #else
  10845.         setjmp(sjbuf)
  10846. #endif /* CK_POSIX_SIG */
  10847.         ) {                /* Timer expired */
  10848.         ttinctimo = 1;
  10849.         n = -1;            /* set flag */
  10850.     } else {
  10851. #ifdef MYREAD
  10852.         n = myread();        /* If managing own buffer... */
  10853.         debug(F101,"ttinc myread","",n);
  10854.         ch = n;
  10855. #else
  10856.         n = read(fd,&ch,1);        /* Otherwise call the system. */
  10857.         if (n == 0) n = -1;
  10858.         debug(F101,"ttinc read","",n);
  10859. #endif /* MYREAD */
  10860.  
  10861. #ifdef CK_ENCRYPTION
  10862.         if (TELOPT_U(TELOPT_ENCRYPTION) && n >= 0) {
  10863.         ck_tn_decrypt(&ch,1);
  10864.         }
  10865. #endif /* CK_ENCRYPTION */
  10866.         if (n >= 0)
  10867.           n = (unsigned) (ch & 0xff);
  10868.         else
  10869.           n = (n < 0) ? -4 : -2;    /* Special return codes. */
  10870.     }
  10871.     ttimoff();            /* Turn off the timer */
  10872.     if (oldalarm > 0) {
  10873.         if (n == -1)        /* and restore any previous alarm */
  10874.           oldalarm -= timo;
  10875.         if (oldalarm < 0)        /* adjusted by our timeout interval */
  10876.           oldalarm = 0;
  10877.         if (oldalarm) {
  10878.             debug(F101,"ttinc restoring oldalarm","",oldalarm);
  10879.         alarm(oldalarm);
  10880.         }
  10881.     }
  10882. #ifdef NETCONN
  10883.     if (netconn) {
  10884.         if (n == -2) {        /* read() returns 0 */
  10885.         netclos();        /* on network read failure */
  10886.         netconn = 0;
  10887.         errno = ENOTCONN;
  10888.         }
  10889.     }
  10890. #endif    /* NETCONN */
  10891. #ifdef TNCODE
  10892.     if ((n > -1) && is_tn)
  10893.       return((unsigned)(n & 0xff));
  10894.     else
  10895. #endif /* TNCODE */
  10896.       /* Return masked char or neg. */
  10897.       return( (n < 0) ? n : (unsigned)(n & ttpmsk) );
  10898.     }
  10899. }
  10900.  
  10901. /*  S N D B R K  --  Send a BREAK signal of the given duration  */
  10902.  
  10903. static int
  10904. #ifdef CK_ANSIC
  10905. sndbrk(int msec) {            /* Argument is milliseconds */
  10906. #else
  10907. sndbrk(msec) int msec; {
  10908. #endif /* CK_ANSIC */
  10909. #ifndef POSIX
  10910.     int x, n;
  10911. #endif /* POSIX */
  10912.  
  10913. #ifdef OXOS
  10914. #define BSDBREAK
  10915. #endif /* OXOS */
  10916.  
  10917. #ifdef ANYBSD
  10918. #define BSDBREAK
  10919. #endif /* ANYBSD */
  10920.  
  10921. #ifdef BSD44
  10922. #define BSDBREAK
  10923. #endif /* BSD44 */
  10924.  
  10925. #ifdef COHERENT
  10926. #ifdef BSDBREAK
  10927. #undef BSDBREAK
  10928. #endif /* BSDBREAK */
  10929. #endif /* COHERENT */
  10930.  
  10931. #ifdef BELLV10
  10932. #ifdef BSDBREAK
  10933. #undef BSDBREAK
  10934. #endif /* BSDBREAK */
  10935. #endif /* BELLV10 */
  10936.  
  10937. #ifdef PROVX1
  10938.     char spd;
  10939. #endif /* PROVX1 */
  10940.  
  10941.     debug(F101,"ttsndb ttyfd","",ttyfd);
  10942.     if (ttyfd < 0) return(-1);          /* Not open. */
  10943.  
  10944. #ifdef Plan9
  10945.     return p9sndbrk(msec);
  10946. #else
  10947. #ifdef NETCONN
  10948. #ifdef NETCMD
  10949.     if (ttpipe)                /* Pipe */
  10950.       return(ttoc('\0'));
  10951. #endif /* NETCMD */
  10952. #ifdef NETPTY
  10953.     if (ttpty)
  10954.       return(ttoc('\0'));
  10955. #endif /* NETPTY */
  10956.     if (netconn)             /* Send network BREAK */
  10957.       return(netbreak());
  10958. #endif /* NETCONN */
  10959.  
  10960.     if (msec < 1 || msec > 5000) return(-1); /* Bad argument */
  10961.  
  10962. #ifdef POSIX                /* Easy in POSIX */
  10963.     {
  10964.     int x;
  10965.     debug(F111,"sndbrk POSIX",ckitoa(msec),(msec/375));
  10966.     errno = 0;
  10967.     x = tcsendbreak(ttyfd,msec / 375);
  10968.     debug(F111,"sndbrk tcsendbreak",ckitoa(errno),x);
  10969.     return(x);
  10970.     }
  10971. #else
  10972. #ifdef PROVX1
  10973.     gtty(ttyfd,&ttbuf);                 /* Get current tty flags */
  10974.     spd = ttbuf.sg_ospeed;              /* Save speed */
  10975.     ttbuf.sg_ospeed = B50;              /* Change to 50 baud */
  10976.     stty(ttyfd,&ttbuf);                 /*  ... */
  10977.     n = (int)strlen(brnuls);        /* Send the right number of nulls */
  10978.     x = msec / 91;
  10979.     if (x > n) x = n;
  10980.     write(ttyfd,brnuls,n);
  10981.     ttbuf.sg_ospeed = spd;              /* Restore speed */
  10982.     stty(ttyfd,&ttbuf);                 /*  ... */
  10983.     return(0);
  10984. #else
  10985. #ifdef aegis
  10986.     sio_$control((short)ttyfd, sio_$send_break, msec, st);
  10987.     return(0);
  10988. #else
  10989. #ifdef BSDBREAK
  10990.     n = FWRITE;                         /* Flush output queue. */
  10991. /* Watch out for int vs long problems in &n arg! */
  10992.     debug(F101,"sndbrk BSDBREAK","",msec);
  10993.     ioctl(ttyfd,TIOCFLUSH,&n);          /* Ignore any errors.. */
  10994.     if (ioctl(ttyfd,TIOCSBRK,(char *)0) < 0) {  /* Turn on BREAK */
  10995.         perror("Can't send BREAK");
  10996.         return(-1);
  10997.     }
  10998.     x = msleep(msec);                    /* Sleep for so many milliseconds */
  10999.     if (ioctl(ttyfd,TIOCCBRK,(char *)0) < 0) {  /* Turn off BREAK */
  11000.         perror("BREAK stuck!!!");
  11001.         doexit(BAD_EXIT,-1);        /* Get out, closing the line. */
  11002.                                         /*   with bad exit status */
  11003.     }
  11004.     return(x);
  11005. #else
  11006. #ifdef ATTSV
  11007. /*
  11008.   No way to send a long BREAK in Sys V, so send a bunch of regular ones.
  11009.   (Actually, Sys V R4 is *supposed* to have the POSIX tcsendbreak() function,
  11010.   but there's no way for this code to know for sure.)
  11011. */
  11012.     debug(F101,"sndbrk ATTSV","",msec);
  11013.     x = msec / 275;
  11014.     for (n = 0; n < x; n++) {
  11015.     /* Reportedly the cast breaks this function on some systems */
  11016.     /* But then why was it here in the first place? */
  11017.     if (ioctl(ttyfd,TCSBRK, /* (char *) */ 0) < 0) {
  11018.         perror("Can't send BREAK");
  11019.         return(-1);
  11020.     }
  11021.     }
  11022.     return(0);
  11023. #else
  11024. #ifdef  V7
  11025.     debug(F101,"sndbrk V7","",msec);
  11026.     return(genbrk(ttyfd,250));        /* Simulate a BREAK */
  11027. #else
  11028.     debug(F101,"sndbrk catchall","",msec);
  11029.     ttoc(0);ttoc(0);ttoc(0);ttoc(0);
  11030.     return(0);
  11031. #endif /* V7 */
  11032. #endif /* BSDBREAK */
  11033. #endif /* ATTSV */
  11034. #endif /* aegis */
  11035. #endif /* PROVX1 */
  11036. #endif /* POSIX */
  11037. #endif /* Plan9 */
  11038. }
  11039.  
  11040. /*  T T S N D B  --  Send a BREAK signal  */
  11041.  
  11042. int
  11043. ttsndb() {
  11044. #ifdef TN_COMPORT
  11045.     if (netconn && istncomport())
  11046.       return((tnsndb(275L) >= 0) ? 0 : -1);
  11047.     else
  11048. #endif /* TN_COMPORT */
  11049.       return(sndbrk(275));
  11050. }
  11051.  
  11052. /*  T T S N D L B  --  Send a Long BREAK signal  */
  11053.  
  11054. int
  11055. ttsndlb() {
  11056. #ifdef TN_COMPORT
  11057.     if (netconn && istncomport())
  11058.       return((tnsndb(1800L) >= 0) ? 0 : -1);
  11059.     else
  11060. #endif /* TN_COMPORT */
  11061.     return(sndbrk(1500));
  11062. }
  11063.  
  11064. /*  M S L E E P  --  Millisecond version of sleep().  */
  11065.  
  11066. /*
  11067.   Call with number of milliseconds (thousandths of seconds) to sleep.
  11068.   Intended only for small intervals.  For big ones, just use sleep().
  11069.   Highly system-dependent.
  11070.   Returns 0 always, even if it didn't work.
  11071. */
  11072.  
  11073. /* Define MSLFTIME for systems that must use an ftime() loop. */
  11074. #ifdef ANYBSD                /* For pre-4.2 BSD versions */
  11075. #ifndef BSD4
  11076. #define MSLFTIME
  11077. #endif /* BSD4 */
  11078. #endif /* ANYBSD */
  11079.  
  11080. #ifdef TOWER1                /* NCR Tower OS 1.0 */
  11081. #define MSLFTIME
  11082. #endif /* TOWER1 */
  11083.  
  11084. #ifdef COHERENT         /* Coherent... */
  11085. #ifndef _I386           /* Maybe Coherent/386 should get this, too */
  11086. #define MSLFTIME        /* Opinions are divided */
  11087. #endif /* _I386 */
  11088. #endif /* COHERENT */
  11089.  
  11090. #ifdef COMMENT
  11091. #ifdef GETMSEC
  11092.  
  11093. /* Millisecond timer */
  11094.  
  11095. static long msecbase = 0L;        /* Unsigned long not portable */
  11096.  
  11097. long
  11098. getmsec() {                /* Milliseconds since base time */
  11099.     struct timeval xv;
  11100.     struct timezone xz;
  11101.     long secs, msecs;
  11102.     if (
  11103. #ifdef GTODONEARG
  11104.     gettimeofday(&tv)
  11105. #else
  11106. #ifdef PTX
  11107.     gettimeofday(&tv, NULL)
  11108. #else
  11109.     gettimeofday(&tv, &tz)
  11110. #endif /* PTX */
  11111. #endif /* GTODONEARG */
  11112.     < 0)
  11113.       return(-1);
  11114.     if (msecbase == 0L) {        /* First call, set base time. */
  11115.     msecbase = tv.tv_sec;
  11116.     debug(F101,"getmsec base","",msecbase);
  11117.     }
  11118.     return(((tv.tv_sec - msecbase) * 1000L) + (tv.tv_usec / 1000L));
  11119. }
  11120. #endif /* GETMSEC */
  11121. #endif /* COMMENT */
  11122.  
  11123. #ifdef SELECT
  11124. int
  11125. ttwait(fd, secs) int fd, secs; {
  11126.     int x;
  11127.     fd_set rfds;
  11128.     FD_ZERO(&rfds);
  11129.     FD_SET(fd,&rfds);
  11130.     tv.tv_sec = secs;
  11131.     tv.tv_usec = 0L;
  11132.     errno = 0;
  11133.     if ((x = select(FD_SETSIZE,
  11134. #ifdef HPUX9
  11135.             (int *)
  11136. #else
  11137. #ifdef HPUX1000
  11138.             (int *)
  11139. #endif /* HPUX1000 */
  11140. #endif /* HPUX9 */
  11141.             &rfds,
  11142.             0, 0, &tv)) < 0) {
  11143.     debug(F101,"ttwait select errno","",errno);
  11144.     return(0);
  11145.     } else {
  11146.     debug(F101,"ttwait OK","",errno);
  11147.     x = FD_ISSET(fd, &rfds);
  11148.     debug(F101,"ttwait select x","",x);
  11149.     return(x ? 1 : 0);
  11150.     }
  11151. }
  11152. #endif /* SELECT */
  11153.  
  11154. int
  11155. msleep(m) int m; {
  11156. /*
  11157.   Other possibilities here are:
  11158.    nanosleep(), reportedly defined in POSIX.4.
  11159.    sginap(), IRIX only (back to what IRIX version I don't know).
  11160. */
  11161. #ifdef Plan9
  11162.     return _SLEEP(m);
  11163. #else
  11164. #ifdef BEOSORBEBOX
  11165.     snooze(m*1000);
  11166. #else /* BEOSORBEBOX */
  11167. #ifdef SELECT
  11168.     int t1, x;
  11169.     debug(F101,"msleep SELECT 1","",m);
  11170.     if (m <= 0) return(0);
  11171.     if (m >= 1000) {            /* Catch big arguments. */
  11172.     sleep(m/1000);
  11173.     m = m % 1000;
  11174.     if (m < 10) return(0);
  11175.     }
  11176.     debug(F101,"msleep SELECT 2","",m);
  11177. #ifdef BELLV10
  11178.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, m );
  11179.     debug(F101,"msleep BELLV10 select","",x);
  11180. #else /* BELLV10 */
  11181. #ifdef HPUX9
  11182.     gettimeofday(&tv, &tz);
  11183. #else
  11184.  
  11185. #ifndef COHERENT
  11186. #ifdef GTODONEARG
  11187.     if (gettimeofday(&tv) < 0)
  11188. #else
  11189. #ifdef PTX
  11190.     if (gettimeofday(&tv,NULL) < 0)
  11191. #else
  11192. #ifdef NOTIMEZONE
  11193.     if (gettimeofday(&tv, NULL) < 0)    /* wonder what this does... */
  11194. #else
  11195.     if (gettimeofday(&tv, &tz) < 0)
  11196. #endif /* NOTIMEZONE */
  11197. #endif /* PTX */
  11198. #endif /* GTODONEARG */
  11199.       return(-1);
  11200.     t1 = tv.tv_sec;                     /* Seconds */
  11201. #endif /* COHERENT */
  11202. #endif /* HPUX9 */
  11203.     tv.tv_sec = 0;                      /* Use select() */
  11204.     tv.tv_usec = m * 1000L;
  11205. #ifdef BSD44
  11206.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11207.     debug(F101,"msleep BSD44 select","",x);
  11208. #else /* BSD44 */
  11209. #ifdef __linux__
  11210.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11211.     debug(F101,"msleep __linux__ select","",x);
  11212. #else /* __linux__ */
  11213. #ifdef BSD43
  11214.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11215.     debug(F101,"msleep BSD43 select","",x);
  11216. #else /* BSD43 */
  11217. #ifdef QNX6
  11218.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11219.     debug(F101,"msleep QNX6 select","",x);
  11220. #else /* QNX6 */
  11221. #ifdef QNX
  11222.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11223.     debug(F101,"msleep QNX select","",x);
  11224. #else /* QNX */
  11225. #ifdef COHERENT
  11226.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11227.     debug(F101,"msleep COHERENT select","",x);
  11228. #else /* COHERENT */
  11229. #ifdef HPUX1000                /* 10.00 only, not 10.10 or later */
  11230.     x = select( 0, (int *)0, (int *)0, (int *)0, &tv );
  11231.     debug(F101,"msleep HP-UX 10.00 select","",x);
  11232. #else /* HPUX1000 */
  11233. #ifdef SVR4
  11234.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11235.     debug(F101,"msleep SVR4 select","",x);
  11236. #else /* SVR4 */
  11237. #ifdef OSF40
  11238.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11239.     debug(F101,"msleep OSF40 select","",x);
  11240. #else /* OSF40 */
  11241. #ifdef PTX
  11242.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11243.     debug(F101,"msleep OSF40 select","",x);
  11244. #else
  11245.     x = select( 0, (int *)0, (int *)0, (int *)0, &tv );
  11246.     debug(F101,"msleep catch-all select","",x);
  11247. #endif /* PTX */
  11248. #endif /* OSF40 */
  11249. #endif /* HP1000 */
  11250. #endif /* SVR4 */
  11251. #endif /* COHERENT */
  11252. #endif /* QNX */
  11253. #endif /* QNX6 */
  11254. #endif /* BSD43 */
  11255. #endif /* __linux__ */
  11256. #endif /* BSD44 */
  11257. #endif /* BELLV10 */
  11258.     return(0);
  11259.  
  11260. #else                    /* Not SELECT */
  11261. #ifdef CK_POLL                /* We have poll() */
  11262.     struct pollfd pfd;            /* Supply a valid address for poll() */
  11263.  
  11264. #ifdef ODT30                /* But in SCO ODT 3.0 */
  11265. #ifdef NAP                /* we should use nap() instead */
  11266.     debug(F101,"msleep ODT 3.0 NAP","",m); /* because using poll() here */
  11267.     nap((long)m);               /* seems to break dialing. */
  11268.     return(0);
  11269. #else
  11270.     debug(F101,"msleep ODT 3.0 POLL","",m);
  11271.     poll(&pfd, 0, m);
  11272.     return(0);
  11273. #endif /* NAP */
  11274. #else
  11275.     debug(F101,"msleep POLL","",m);
  11276.     poll(&pfd, 0, m);
  11277.     return(0);
  11278. #endif /* ODT30 */
  11279.  
  11280. /*
  11281.   We could handle the above more cleanly by just letting nap() always
  11282.   take precedence over poll() in this routine, but there is no way to know
  11283.   whether that would break something else.
  11284. */
  11285.  
  11286. #else                    /* Not POLL */
  11287. #ifdef USLEEP
  11288. /*
  11289.   "This routine is implemented using setitimer(2); it requires eight
  11290.   system calls...".  In other words, it might take 5 minutes to sleep
  11291.   10 milliseconds...
  11292. */
  11293.     debug(F101,"msleep USLEEP","",m);
  11294.     if (m >= 1000) {            /* Catch big arguments. */
  11295.     sleep(m/1000);
  11296.     m = m % 1000;
  11297.     if (m < 10) return(0);
  11298.     }
  11299.     usleep((unsigned int)(m * 1000));
  11300.     return(0);
  11301. #else
  11302. #ifdef aegis
  11303.     time_$clock_t dur;
  11304.     debug(F101,"msleep aegis","",m);
  11305.     dur.c2.high16 = 0;
  11306.     dur.c2.low32  = 250 * m; /* one millisecond = 250 four microsecond ticks */
  11307.     time_$wait(time_$relative, dur, st);
  11308.     return(0);
  11309. #else
  11310. #ifdef PROVX1
  11311.     debug(F101,"msleep Venix","",m);
  11312.     if (m <= 0) return(0);
  11313.     sleep(-((m * 60 + 500) / 1000));
  11314.     return(0);
  11315. #else
  11316. #ifdef NAP
  11317.     debug(F101,"msleep NAP","",m);
  11318.     nap((long)m);
  11319.     return(0);
  11320. #else
  11321. #ifdef ATTSV
  11322. #ifndef BSD44
  11323.     extern long times();        /* Or #include <times.h> ? */
  11324. #endif /* BSD44 */
  11325.     long t1, t2, tarray[4];
  11326.     int t3;
  11327.     char *cp = getenv("HZ");
  11328.     int CLOCK_TICK;
  11329.     int hertz;
  11330.  
  11331.     if (cp && (hertz = atoi(cp))) {
  11332.         CLOCK_TICK  = 1000 / hertz;
  11333.     } else {                /* probably single user mode */
  11334. #ifdef HZ
  11335.         CLOCK_TICK  = 1000 / HZ;
  11336. #else
  11337.     static warned = 0;
  11338.     /* HZ always exists in, for instance, SCO Xenix, so you don't have to
  11339.      * make special #ifdefs for XENIX here, like in ver 4F. Also, if you
  11340.      * have Xenix, you have should have nap(), so the best is to use -DNAP
  11341.      * in the makefile. Most systems have HZ.
  11342.      */
  11343.     CLOCK_TICK = 17;        /* 1/60 sec */
  11344.     if (!warned) {
  11345.           printf("warning: environment variable HZ bad... using HZ=%d\r\n",
  11346.          1000 / CLOCK_TICK);
  11347.           warned = 1;
  11348.     }
  11349. #endif /* !HZ */
  11350.     }
  11351.     debug(F101,"msleep ATTSV","",m);
  11352.     if (m <= 0) return(0);
  11353.     if (m >= 1000) {            /* Catch big arguments. */
  11354.     sleep(m/1000);
  11355.     m = m % 1000;
  11356.     if (m < 10) return(0);
  11357.     }
  11358.     if ((t1 = times(tarray)) < 0) return(-1);
  11359.     while (1) {
  11360.         if ((t2 = times(tarray)) < 0) return(-1);
  11361.         t3 = ((int)(t2 - t1)) * CLOCK_TICK;
  11362.         if (t3 > m) return(t3);
  11363.     }
  11364. #else /* Not ATTSV */
  11365. #ifdef MSLFTIME                /* Use ftime() loop... */
  11366.     int t1, t3 = 0;
  11367.     debug(F101,"msleep MSLFTIME","",m);
  11368.     if (m <= 0) return(0);
  11369.     if (m >= 1000) {            /* Catch big arguments. */
  11370.     sleep(m/1000);
  11371.     m = m % 1000;
  11372.     if (m < 10) return(0);
  11373.     }
  11374. #ifdef QNX
  11375.     ftime(&ftp);            /* void ftime() in QNX */
  11376. #else
  11377.     if (ftime(&ftp) < 0) return(-1);    /* Get base time. */
  11378. #endif /* QNX */
  11379.     t1 = ((ftp.time & 0xff) * 1000) + ftp.millitm;
  11380.     while (1) {
  11381.         ftime(&ftp);            /* Get current time and compare. */
  11382.         t3 = (((ftp.time & 0xff) * 1000) + ftp.millitm) - t1;
  11383.         if (t3 > m) return(0);
  11384.     }
  11385. #else
  11386. /* This includes true POSIX, which has no way to do this. */
  11387.     debug(F101,"msleep busy loop","",m);
  11388.     if (m >= 1000) {            /* Catch big arguments. */
  11389.     sleep(m/1000);
  11390.     m = m % 1000;
  11391.     if (m < 10) return(0);
  11392.     }
  11393.     if (m > 0) while (m > 0) m--;    /* Just a dumb busy loop */
  11394.     return(0);
  11395. #endif /* MSLFTIME */
  11396. #endif /* ATTSV */
  11397. #endif /* NAP */
  11398. #endif /* PROVX1 */
  11399. #endif /* aegis */
  11400. #endif /* CK_POLL */
  11401. #endif /* SELECT */
  11402. #endif /* BEOSORBEBOX */
  11403. #endif /* USLEEP */
  11404. #endif /* Plan9 */
  11405. }
  11406.  
  11407. /*  R T I M E R --  Reset elapsed time counter  */
  11408.  
  11409. VOID
  11410. rtimer() {
  11411.     tcount = time( (time_t *) 0 );
  11412. }
  11413.  
  11414.  
  11415. /*  G T I M E R --  Get current value of elapsed time counter in seconds  */
  11416.  
  11417. int
  11418. gtimer() {
  11419.     int x;
  11420.     x = (int) (time( (time_t *) 0 ) - tcount);
  11421.     debug(F101,"gtimer","",x);
  11422.     return( (x < 0) ? 0 : x );
  11423. }
  11424.  
  11425. #ifdef GFTIMER
  11426. /*
  11427.   Floating-point timers.  Require not only floating point support, but
  11428.   also gettimeofday().
  11429. */
  11430. static struct timeval tzero;
  11431.  
  11432. VOID
  11433. rftimer() {
  11434. #ifdef GTODONEARG    /* Account for Mot's definition */
  11435.     (VOID) gettimeofday(&tzero);
  11436. #else
  11437.     (VOID) gettimeofday(&tzero, (struct timezone *)0);
  11438. #endif /* GTODONEARG */
  11439. }
  11440.  
  11441. CKFLOAT
  11442. gftimer() {
  11443.     struct timeval tnow, tdelta;
  11444.     CKFLOAT s;
  11445. #ifdef DEBUG
  11446.     char fpbuf[64];
  11447. #endif /* DEBUG */
  11448. #ifdef GTODONEARG    /* Acount for Mot's definition */
  11449.     (VOID) gettimeofday(&tnow);
  11450. #else
  11451.     (VOID) gettimeofday(&tnow, (struct timezone *)0);
  11452. #endif /* GTODONEARG */
  11453.     tdelta.tv_sec = tnow.tv_sec - tzero.tv_sec;
  11454.     tdelta.tv_usec = tnow.tv_usec - tzero.tv_usec;
  11455.     if (tdelta.tv_usec < 0) {
  11456.     tdelta.tv_sec--;
  11457.     tdelta.tv_usec += 1000000;
  11458.     }
  11459.     s = (CKFLOAT) tdelta.tv_sec + ((CKFLOAT) tdelta.tv_usec / 1000000.0);
  11460.     if (s < GFMINTIME)
  11461.       s = GFMINTIME;
  11462. #ifdef DEBUG
  11463.     if (deblog) {
  11464.     sprintf(fpbuf,"%f",s);
  11465.     debug(F110,"gftimer",fpbuf,0);
  11466.     }
  11467. #endif /* DEBUG */
  11468.     return(s);
  11469. }
  11470. #endif /* GFTIMER */
  11471.  
  11472. /*  Z T I M E  --  Return asctime()-format date/time string  */
  11473. /*
  11474.   NOTE: as a side effect of calling this routine, we can also set the
  11475.   following two variables, giving the micro- and milliseconds (fractions of
  11476.   seconds) of the clock time.  Currently this is done only in BSD-based builds
  11477.   that use gettimeofday().  When these variables are not filled in, they are
  11478.   left with a value of -1L.
  11479. */
  11480. VOID
  11481. ztime(s) char **s; {
  11482.  
  11483. #ifdef GFTIMER
  11484. /*
  11485.   The gettimeofday() method, which also sets ztmsec and ztusec, works for
  11486.   all GFTIMER builds.  NOTE: ztmsec and ztusec are defined in ckcmai.c,
  11487.   and extern declarations for them are in ckcdeb.h; thus they are
  11488.   declared in this file by inclusion of ckcdeb.h.
  11489. */
  11490.     char *asctime();
  11491.     struct tm *localtime();
  11492.     struct tm *tp;
  11493.     ztmsec = -1L;
  11494.     ztusec = -1L;
  11495.  
  11496.     if (!s)
  11497.       debug(F100,"ztime s==NULL","",0);
  11498.  
  11499. #ifdef GTODONEARG
  11500.     /* No 2nd arg in Motorola SV88 and some others */
  11501.     if (gettimeofday(&tv) > -1)
  11502. #else
  11503. #ifndef COHERENT
  11504. #ifdef PTX
  11505.     if (gettimeofday(&tv,NULL) > -1)
  11506. #else
  11507. #ifdef NOTIMEZONE
  11508.     if (gettimeofday(&tv, NULL) > -1)    /* wonder what this does... */
  11509. #else
  11510.     if (gettimeofday(&tv, &tz) > -1)
  11511. #endif /* NOTIMEZONE */
  11512. #endif /* PTX */
  11513. #endif /* COHERENT */
  11514. #endif /* GTODONEARG */
  11515.       {                    /* Fill in tm struct */
  11516.     ztusec = tv.tv_usec;        /* Microseconds */
  11517.     ztmsec = ztusec / 1000L;    /* Milliseconds */
  11518. #ifdef HPUX9
  11519.     {
  11520.         time_t zz;
  11521.         zz = tv.tv_sec;
  11522.         tp = localtime(&zz);    /* Convert to local time */
  11523.     }
  11524. #else
  11525. #ifdef HPUX1000
  11526.     {
  11527.         time_t zz;
  11528.         zz = tv.tv_sec;
  11529.         tp = localtime(&zz);
  11530.     }
  11531. #else
  11532. #ifdef LINUX
  11533.     {   /* avoid unaligned access trap on 64-bit platforms */
  11534.         time_t zz;
  11535.         zz = tv.tv_sec;
  11536.         tp = localtime(&zz);
  11537.     }
  11538. #else
  11539. #ifdef MACOSX
  11540.     tp = localtime((time_t *)&tv.tv_sec); /* Convert to local time */
  11541. #else
  11542.     tp = localtime(&tv.tv_sec);
  11543. #endif /* MACOSX */
  11544. #endif /* LINUX */
  11545. #endif /* HPUX1000 */
  11546. #endif /* HPUX9 */
  11547.     if (s) {
  11548.         *s = asctime(tp);        /* Convert result to ASCII string */
  11549.         debug(F111,"ztime GFTIMER gettimeofday",*s,ztusec);
  11550.     }
  11551.     }
  11552. #else  /* Not GFTIMER */
  11553.  
  11554. #undef ZTIMEV7                /* Which systems need to use */
  11555. #ifdef COHERENT                /* old UNIX Version 7 way... */
  11556. #define ZTIMEV7
  11557. #endif /* COHERENT */
  11558. #ifdef TOWER1
  11559. #define ZTIMEV7
  11560. #endif /* TOWER1 */
  11561. #ifdef ANYBSD
  11562. #ifndef BSD42
  11563. #define ZTIMEV7
  11564. #endif /* BSD42 */
  11565. #endif /* ANYBSD */
  11566. #ifdef V7
  11567. #ifndef MINIX
  11568. #define ZTIMEV7
  11569. #endif /* MINIX */
  11570. #endif /* V7 */
  11571. #ifdef POSIX
  11572. #define ZTIMEV7
  11573. #endif /* POSIX */
  11574.  
  11575. #ifdef HPUX1020
  11576. /*
  11577.   Prototypes are in <time.h>, included above.
  11578. */
  11579.     time_t clock_storage;
  11580.     clock_storage = time((void *) 0);
  11581.     if (s) {
  11582.     *s = ctime(&clock_storage);
  11583.     debug(F110,"ztime: HPUX 10.20",*s,0);
  11584.     }
  11585. #else
  11586. #ifdef ATTSV                /* AT&T way */
  11587. /*  extern long time(); */        /* Theoretically these should */
  11588.     char *ctime();            /* already been dcl'd in <time.h> */
  11589.     time_t clock_storage;
  11590.     clock_storage = time(
  11591. #ifdef IRIX60
  11592.              (time_t *)
  11593. #else
  11594. #ifdef BSD44
  11595.              (time_t *)
  11596. #else
  11597.              (long *)
  11598. #endif /* BSD44 */
  11599. #endif /* IRIX60 */
  11600.              0 );
  11601.     if (s) {
  11602.     *s = ctime( &clock_storage );
  11603.     debug(F110,"ztime: ATTSV",*s,0);
  11604.     }
  11605. #else
  11606. #ifdef PROVX1                /* Venix 1.0 way */
  11607.     int utime[2];
  11608.     time(utime);
  11609.     if (s) {
  11610.     *s = ctime(utime);
  11611.     debug(F110,"ztime: PROVX1",*s,0);
  11612.     }
  11613. #else
  11614. #ifdef BSD42                /* 4.2BSD way */
  11615.     char *asctime();
  11616.     struct tm *localtime();
  11617.     struct tm *tp;
  11618.     gettimeofday(&tv, &tz);
  11619.     ztusec = tv.tv_usec;
  11620.     ztmsec = tv.tv_usec / 1000L;
  11621.     tp = localtime(&tv.tv_sec);
  11622.     if (s) {
  11623.     *s = asctime(tp);
  11624.     debug(F111,"ztime: BSD42",*s,ztusec);
  11625.     }
  11626. #else
  11627. #ifdef MINIX                /* MINIX way */
  11628. #ifdef COMMENT
  11629.     extern long time();            /* Already got these from <time.h> */
  11630.     extern char *ctime();
  11631. #endif /* COMMENT */
  11632.     time_t utime[2];
  11633.     time(utime);
  11634.     if (s) {
  11635.     *s = ctime(utime);
  11636.     debug(F110,"ztime: MINIX",*s,0);
  11637.     }
  11638. #else
  11639. #ifdef ZTIMEV7                /* The regular way */
  11640.     char *asctime();
  11641.     struct tm *localtime();
  11642.     struct tm *tp;
  11643.     long xclock;            /* or unsigned long for BeBox? */
  11644.     time(&xclock);
  11645.     tp = localtime(&xclock);
  11646.     if (s) {
  11647.     *s = asctime(tp);
  11648.     debug(F110,"ztime: ZTIMEV7",*s,0);
  11649.     }
  11650. #else                    /* Catch-all for others... */
  11651.     if (s) {
  11652.     *s = "Day Mon 00 00:00:00 0000\n"; /* Dummy in asctime() format */
  11653.     debug(F110,"ztime: catch-all",*s,0);
  11654.     }
  11655. #endif /* ZTIMEV7 */
  11656. #endif /* MINIX */
  11657. #endif /* BSD42 */
  11658. #endif /* PROVX1 */
  11659. #endif /* ATTSV */
  11660. #endif /* HPUX1020 */
  11661. #endif /* GFTIMER */
  11662. }
  11663.  
  11664. /*  C O N G M  --  Get console terminal modes.  */
  11665.  
  11666. /*
  11667.   Saves initial console mode, and establishes variables for switching
  11668.   between current (presumably normal) mode and other modes.
  11669.   Should be called when program starts, but only after establishing
  11670.   whether program is in the foreground or background.
  11671.   Returns 1 if it got the modes OK, 0 if it did nothing, -1 on error.
  11672. */
  11673. int
  11674. congm() {
  11675.     int fd;
  11676.     if (backgrd || !isatty(0)) {    /* If in background. */
  11677.     cgmf = -1;            /* Don't bother, modes are garbage. */
  11678.     return(-1);
  11679.     }
  11680.     if (cgmf > 0) return(0);        /* Already did this. */
  11681.     debug(F100,"congm getting modes","",0); /* Need to do it. */
  11682. #ifdef aegis
  11683.     ios_$inq_type_uid(ios_$stdin, conuid, st);
  11684.     if (st.all != status_$ok) {
  11685.     fprintf(stderr, "problem getting stdin objtype: ");
  11686.     error_$print(st);
  11687.     }
  11688.     concrp = (conuid == mbx_$uid);
  11689.     conbufn = 0;
  11690. #endif /* aegis */
  11691.  
  11692. #ifndef BEBOX
  11693.     if ((fd = open(CTTNAM,2)) < 0) {    /* Open controlling terminal */
  11694. #ifdef COMMENT
  11695.     fprintf(stderr,"Error opening %s\n", CTTNAM);
  11696.     perror("congm");
  11697.     return(-1);
  11698. #else
  11699.     fd = 0;
  11700. #endif /* COMMENT */
  11701.     }
  11702. #else
  11703.     fd = 0;
  11704. #endif /* !BEBOX */
  11705. #ifdef BSD44ORPOSIX
  11706.     if (tcgetattr(fd,&ccold) < 0) return(-1);
  11707.     if (tcgetattr(fd,&cccbrk) < 0) return(-1);
  11708.     if (tcgetattr(fd,&ccraw) < 0) return(-1);
  11709. #else
  11710. #ifdef ATTSV
  11711.     if (ioctl(fd,TCGETA,&ccold)  < 0) return(-1);
  11712.     if (ioctl(fd,TCGETA,&cccbrk) < 0) return(-1);
  11713.     if (ioctl(fd,TCGETA,&ccraw)  < 0) return(-1);
  11714. #ifdef VXVE
  11715.     cccbrk.c_line = 0;            /* STTY line 0 for CDC VX/VE */
  11716.     if (ioctl(fd,TCSETA,&cccbrk) < 0) return(-1);
  11717.     ccraw.c_line = 0;            /* STTY line 0 for CDC VX/VE */
  11718.     if (ioctl(fd,TCSETA,&ccraw) < 0) return(-1);
  11719. #endif /* VXVE */
  11720. #else
  11721. #ifdef BELLV10
  11722.     if (ioctl(fd,TIOCGETP,&ccold) < 0) return(-1);
  11723.     if (ioctl(fd,TIOCGETP,&cccbrk) < 0) return(-1);
  11724.     if (ioctl(fd,TIOCGETP,&ccraw) < 0) return(-1);
  11725.     debug(F101,"cccbrk.sg_flags orig","", cccbrk.sg_flags);
  11726. #else
  11727.     if (gtty(fd,&ccold) < 0) return(-1);
  11728.     if (gtty(fd,&cccbrk) < 0) return(-1);
  11729.     if (gtty(fd,&ccraw) < 0) return(-1);
  11730. #endif /* BELLV10 */
  11731. #endif /* ATTSV */
  11732. #endif /* BSD44ORPOSIX */
  11733. #ifdef sony_news            /* Sony NEWS */
  11734.     if (ioctl(fd,TIOCKGET,&km_con) < 0) { /* Get console Kanji mode */
  11735.     perror("congm error getting Kanji mode");
  11736.     debug(F101,"congm error getting Kanji mode","",0);
  11737.     km_con = -1;            /* Make sure this stays undefined. */
  11738.     return(-1);
  11739.     }
  11740. #endif /* sony_news */
  11741.     if (fd > 0)
  11742.       close(fd);
  11743.     cgmf = 1;                /* Flag that we got them. */
  11744.     return(1);
  11745. }
  11746.  
  11747.  
  11748. static VOID
  11749. congetbuf(x) int x; {
  11750.     int n;
  11751.     n = CONBUFSIZ - (conbufp - conbuf);    /* How much room left in buffer? */
  11752.     if (x > n) {
  11753.     debug(F101,"congetbuf char loss","",x-n);
  11754.     x = n;
  11755.     }
  11756.     x = read(0,conbufp,x);
  11757.     conbufn += x;
  11758.     debug(F111,"congetbuf readahead",conbuf,x);
  11759. }
  11760.  
  11761.  
  11762. /*  C O N C B --  Put console in cbreak mode.  */
  11763.  
  11764. /*  Returns 0 if ok, -1 if not  */
  11765.  
  11766. int
  11767. #ifdef CK_ANSIC
  11768. concb(char esc)
  11769. #else
  11770. concb(esc) char esc;
  11771. #endif /* CK_ANSIC */
  11772. /* concb */ {
  11773.     int x;
  11774.     debug(F101,"concb constate","",constate);
  11775.     debug(F101,"concb cgmf","",cgmf);
  11776.     debug(F101,"concb backgrd","",backgrd);
  11777.  
  11778.     if (constate == CON_CB)
  11779.       return(0);
  11780.  
  11781.     if (cgmf < 1)            /* Did we get console modes yet? */
  11782.       if (!backgrd)            /* No, in background? */
  11783.     congm();            /* No, try to get them now. */
  11784.     if (cgmf < 1)            /* Still don't have them? */
  11785.       return(0);            /* Give up. */
  11786.     debug(F101,"concb ttyfd","",ttyfd);
  11787.     debug(F101,"concb ttfdflg","",ttfdflg);
  11788. #ifdef COMMENT
  11789.     /* This breaks returning to prompt after protocol with "-l 0" */
  11790.     /* Commented out July 1998 */
  11791.     if (ttfdflg && ttyfd >= 0 && ttyfd < 3)
  11792.       return(0);
  11793. #endif /* COMMENT */
  11794.     x = isatty(0);
  11795.     debug(F101,"concb isatty","",x);
  11796.     if (!x) return(0);            /* Only when running on real ttys */
  11797.     debug(F101,"concb suspend","",suspend);
  11798.     if (backgrd)            /* Do nothing if in background. */
  11799.       return(0);
  11800.     escchr = esc;                       /* Make this available to other fns */
  11801.     ckxech = 1;                         /* Program can echo characters */
  11802. #ifdef aegis
  11803.     conbufn = 0;
  11804.     if (concrp) return(write(1, "\035\002", 2));
  11805.     if (conuid == input_pad_$uid) {pad_$raw(ios_$stdin, st); return(0);}
  11806. #endif /* aegis */
  11807.  
  11808. #ifdef COHERENT
  11809. #define SVORPOSIX
  11810. #endif /* COHERENT */
  11811.  
  11812. #ifdef Plan9
  11813.     x = p9concb();
  11814. #else
  11815. #ifndef SVORPOSIX            /* BSD, V7, etc */
  11816.     debug(F101,"cccbrk.sg_flags concb 1","", cccbrk.sg_flags);
  11817.     debug(F101,"concb stty CBREAK","",0);
  11818.     cccbrk.sg_flags |= (CBREAK|CRMOD);    /* Set to character wakeup, */
  11819.     cccbrk.sg_flags &= ~ECHO;           /* no echo. */
  11820.     debug(F101,"cccbrk.sg_flags concb 2","", cccbrk.sg_flags);
  11821.     errno = 0;
  11822. /*
  11823.   BSD stty() clears the console buffer.  So if anything is waiting in it,
  11824.   we have to read it now to avoid losing it.
  11825. */
  11826.     x = conchk();
  11827.     if (x > 0)
  11828.       congetbuf(x);
  11829.  
  11830. #ifdef BELLV10
  11831.     x = ioctl(0,TIOCSETP,&cccbrk);
  11832. #else
  11833.     x = stty(0,&cccbrk);
  11834.     debug(F101,"cccbrk.sg_flags concb x","", x);
  11835. #endif /* BELLV10 */
  11836. #else                    /* Sys V and POSIX */
  11837. #ifndef OXOS
  11838.     debug(F101,"concb cccbrk.c_flag","",cccbrk.c_lflag);
  11839. #ifdef QNX
  11840.     /* Don't mess with IEXTEN */
  11841.     cccbrk.c_lflag &= ~(ICANON|ECHO);
  11842. #else
  11843. #ifdef COHERENT
  11844.     cccbrk.c_lflag &= ~(ICANON|ECHO);
  11845. #else
  11846.     cccbrk.c_lflag &= ~(ICANON|ECHO|IEXTEN);
  11847. #endif /* COHERENT */
  11848. #endif /* QNX */
  11849.     cccbrk.c_lflag |= ISIG;        /* Allow signals in command mode. */
  11850.     cccbrk.c_iflag |= IGNBRK;        /* But ignore BREAK signal */
  11851.     cccbrk.c_iflag &= ~BRKINT;
  11852.  
  11853. #else /* OXOS */
  11854.     debug(F100,"concb OXOS is defined","",0);
  11855.     cccbrk.c_lflag &= ~(ICANON|ECHO);
  11856.     cccbrk.c_cc[VDISCARD] = cccbrk.c_cc[VLNEXT] = CDISABLE;
  11857. #endif /* OXOS */
  11858. #ifdef COMMENT
  11859. /*
  11860.   Believe it or not, in SCO UNIX, VSUSP is greater than NCC, and so this
  11861.   array reference is out of bounds.  It's only a debug() call so who needs it.
  11862. */
  11863. #ifdef VSUSP
  11864.     debug(F101,"concb c_cc[VSUSP]","",cccbrk.c_cc[VSUSP]);
  11865. #endif /* VSUSP */
  11866. #endif /* COMMENT */
  11867. #ifndef VINTR
  11868.     debug(F101,"concb c_cc[0]","",cccbrk.c_cc[0]);
  11869.     cccbrk.c_cc[0] = 003;               /* Interrupt char is Control-C */
  11870. #else
  11871.     debug(F101,"concb c_cc[VINTR]","",cccbrk.c_cc[0]);
  11872.     cccbrk.c_cc[VINTR] = 003;
  11873. #endif /* VINTR */
  11874. #ifndef VQUIT
  11875.     cccbrk.c_cc[1] = escchr;            /* escape during packet modes */
  11876. #else
  11877.     cccbrk.c_cc[VQUIT] = escchr;
  11878. #endif /* VQUIT */
  11879. #ifndef VEOF
  11880.     cccbrk.c_cc[4] = 1;
  11881. #else
  11882. #ifndef OXOS
  11883. #ifdef VMIN
  11884.     cccbrk.c_cc[VMIN] = 1;
  11885. #endif /* VMIN */
  11886. #else /* OXOS */
  11887.     cccbrk.c_min = 1;
  11888. #endif /* OXOS */
  11889. #endif /* VEOF */
  11890. #ifdef ZILOG
  11891.     cccbrk.c_cc[5] = 0;
  11892. #else
  11893. #ifndef VEOL
  11894.     cccbrk.c_cc[5] = 1;
  11895. #else
  11896. #ifndef OXOS
  11897. #ifdef VTIME
  11898.     cccbrk.c_cc[VTIME] = 1;
  11899. #endif /* VTIME */
  11900. #else /* OXOS */
  11901.     cccbrk.c_time = 1;
  11902. #endif /* OXOS */
  11903. #endif /* VEOL */
  11904. #endif /* ZILOG */
  11905.     errno = 0;
  11906. #ifdef BSD44ORPOSIX            /* Set new modes */
  11907.     x = tcsetattr(0,TCSADRAIN,&cccbrk);
  11908. #else /* ATTSV */                  /* or the POSIX way */
  11909.     x = ioctl(0,TCSETAW,&cccbrk);    /* the Sys V way */
  11910. #endif /* BSD44ORPOSIX */
  11911. #endif /* SVORPOSIX */
  11912.  
  11913. #ifdef COHERENT
  11914. #undef SVORPOSIX
  11915. #endif /* COHERENT */
  11916.     debug(F101,"concb x","",x);
  11917.     debug(F101,"concb errno","",errno);
  11918. #ifdef NONOSETBUF
  11919.     if (x > -1) {
  11920.     setbuf(stdout,NULL);    /* Make console unbuffered. */
  11921.     debug(F100,"concb setbuf A","",0);
  11922.     }
  11923. #else
  11924. #ifndef aegis
  11925. #ifndef NOSETBUF
  11926.     if (x > -1) {
  11927.     setbuf(stdout,NULL);    /* Make console unbuffered. */
  11928.     debug(F100,"concb setbuf B","",0);
  11929.     }
  11930. #endif /* NOSETBUF */
  11931. #endif /* aegis */
  11932. #endif /* NONOSETBUF */
  11933.  
  11934. #ifdef  V7
  11935. #ifndef MINIX
  11936.     if (kmem[CON] < 0) {
  11937.         qaddr[CON] = initrawq(0);
  11938.         if((kmem[CON] = open("/dev/kmem", 0)) < 0) {
  11939.             fprintf(stderr, "Can't read /dev/kmem in concb.\n");
  11940.             perror("/dev/kmem");
  11941.             exit(1);
  11942.         }
  11943.     }
  11944. #endif /* MINIX */
  11945. #endif /* V7 */
  11946. #endif /* Plan9 */
  11947.  
  11948.     if (x > -1)
  11949.       constate = CON_CB;
  11950.  
  11951.     debug(F101,"concb returns","",x);
  11952.     return(x);
  11953. }
  11954.  
  11955. /*  C O N B I N  --  Put console in binary mode  */
  11956.  
  11957. /*  Returns 0 if ok, -1 if not  */
  11958.  
  11959. int
  11960. #ifdef CK_ANSIC
  11961. conbin(char esc)
  11962. #else
  11963. conbin(esc) char esc;
  11964. #endif /* CK_ANSIC */
  11965. /* conbin */  {
  11966.  
  11967.     int x;
  11968.  
  11969.     debug(F101,"conbin constate","",constate);
  11970.  
  11971.     if (constate == CON_BIN)
  11972.       return(0);
  11973.  
  11974.     if (!isatty(0)) return(0);          /* only for real ttys */
  11975.     congm();                /* Get modes if necessary. */
  11976.     debug(F100,"conbin","",0);
  11977.     escchr = esc;                       /* Make this available to other fns */
  11978.     ckxech = 1;                         /* Program can echo characters */
  11979. #ifdef aegis
  11980.     conbufn = 0;
  11981.     if (concrp) return(write(1, "\035\002", 2));
  11982.     if (conuid == input_pad_$uid) {
  11983.     pad_$raw(ios_$stdin, st);
  11984.     return(0);
  11985.       }
  11986. #endif /* aegis */
  11987.  
  11988. #ifdef COHERENT
  11989. #define SVORPOSIX
  11990. #endif /* COHERENT */
  11991.  
  11992. #ifdef Plan9
  11993.     return p9conbin();
  11994. #else
  11995. #ifdef SVORPOSIX
  11996. #ifndef OXOS
  11997. #ifdef QNX
  11998.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO);
  11999. #else
  12000. #ifdef COHERENT
  12001.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO);
  12002. #else
  12003.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
  12004. #endif /* COHERENT */
  12005. #endif /* QNX */
  12006. #else /* OXOS */
  12007.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO);
  12008.     ccraw.c_cc[VDISCARD] = ccraw.c_cc[VLNEXT] = CDISABLE;
  12009. #endif /* OXOS */
  12010.     ccraw.c_iflag |= IGNPAR;
  12011. /*
  12012.   Note that for terminal sessions we disable Xon/Xoff flow control to allow
  12013.   the passage ^Q and ^S as data characters for EMACS, and to allow XMODEM
  12014.   transfers to work when C-Kermit is in the middle, etc.  Hardware flow
  12015.   control, if in use, is not affected.
  12016. */
  12017. #ifdef ATTSV
  12018. #ifdef BSD44
  12019.     ccraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IXON|IXANY|IXOFF
  12020.                         |INPCK|ISTRIP);
  12021. #else
  12022.     ccraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IUCLC|IXON|IXANY|IXOFF
  12023.                         |INPCK|ISTRIP);
  12024. #endif /* BSD44 */
  12025. #else /* POSIX */
  12026.     ccraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IXON|IXOFF|INPCK|ISTRIP);
  12027. #endif /* ATTSV */
  12028.     ccraw.c_oflag &= ~OPOST;
  12029. #ifdef COMMENT
  12030. /*
  12031.   WHAT THE HECK WAS THIS FOR?
  12032.   The B9600 setting (obviously) prevents CONNECT from working at any
  12033.   speed other than 9600 when you are logged in to the 7300 on a serial
  12034.   line.  Maybe some of the other flags are necessary -- if so, put back
  12035.   the ones that are needed.  This code is supposed to work the same, no
  12036.   matter whether you are logged in to the 7300 on the real console device,
  12037.   or through a serial port.
  12038. */
  12039. #ifdef ATT7300
  12040.     ccraw.c_cflag = CLOCAL | B9600 | CS8 | CREAD | HUPCL;
  12041. #endif /* ATT7300 */
  12042. #endif /* COMMENT */
  12043.  
  12044. /*** Kermit used to put the console in 8-bit raw mode, but some users have
  12045.  *** pointed out that this should not be done, since some sites actually
  12046.  *** use terminals with parity settings on their Unix systems, and if we
  12047.  *** override the current settings and stop doing parity, then their terminals
  12048.  *** will display blotches for characters whose parity is wrong.  Therefore,
  12049.  *** the following two lines are commented out (Larry Afrin, Clemson U):
  12050.  ***
  12051.  ***   ccraw.c_cflag &= ~(PARENB|CSIZE);
  12052.  ***   ccraw.c_cflag |= (CS8|CREAD);
  12053.  ***
  12054.  *** Sys III/V sites that have trouble with this can restore these lines.
  12055.  ***/
  12056. #ifndef VINTR
  12057.     ccraw.c_cc[0] = 003;        /* Interrupt char is Ctrl-C */
  12058. #else
  12059.     ccraw.c_cc[VINTR] = 003;
  12060. #endif /* VINTR */
  12061. #ifndef VQUIT
  12062.     ccraw.c_cc[1] = escchr;        /* Escape during packet mode */
  12063. #else
  12064.     ccraw.c_cc[VQUIT] = escchr;
  12065. #endif /* VQUIT */
  12066. #ifndef VEOF
  12067.     ccraw.c_cc[4] = 1;
  12068. #else
  12069. #ifndef OXOS
  12070. #ifdef VMIN
  12071.     ccraw.c_cc[VMIN] = 1;
  12072. #endif /* VMIN */
  12073. #else /* OXOS */
  12074.     ccraw.c_min = 1;
  12075. #endif /* OXOS */
  12076. #endif /* VEOF */
  12077.  
  12078. #ifdef ZILOG
  12079.     ccraw.c_cc[5] = 0;
  12080. #else
  12081. #ifndef VEOL
  12082.     ccraw.c_cc[5] = 1;
  12083. #else
  12084. #ifndef OXOS
  12085. #ifdef VTIME
  12086.     ccraw.c_cc[VTIME] = 1;
  12087. #endif /* VTIME */
  12088. #else /* OXOS */
  12089.     ccraw.c_time = 1;
  12090. #endif /* OXOS */
  12091. #endif /* VEOL */
  12092. #endif /* ZILOG */
  12093.  
  12094. #ifdef BSD44ORPOSIX
  12095.     x = tcsetattr(0,TCSADRAIN,&ccraw);    /* Set new modes. */
  12096. #else
  12097.     x = ioctl(0,TCSETAW,&ccraw);
  12098. #endif /* BSD44ORPOSIX */
  12099. #else /* Berkeley, etc. */
  12100.     x = conchk();            /* Because stty() is destructive */
  12101.     if (x > 0)
  12102.       congetbuf(x);
  12103.     ccraw.sg_flags |= (RAW|TANDEM);     /* Set rawmode, XON/XOFF (ha) */
  12104.     ccraw.sg_flags &= ~(ECHO|CRMOD);    /* Set char wakeup, no echo */
  12105. #ifdef BELLV10
  12106.     x = ioctl(0,TIOCSETP,&ccraw);
  12107. #else
  12108.     x = stty(0,&ccraw);
  12109. #endif /* BELLV10 */
  12110. #endif /* SVORPOSIX */
  12111. #endif /* Plan9 */
  12112.  
  12113.     if (x > -1)
  12114.       constate = CON_BIN;
  12115.  
  12116.     debug(F101,"conbin returns","",x);
  12117.     return(x);
  12118.  
  12119. #ifdef COHERENT
  12120. #undef SVORPOSIX
  12121. #endif /* COHERENT */
  12122.  
  12123. }
  12124.  
  12125.  
  12126. /*  C O N R E S  --  Restore the console terminal  */
  12127.  
  12128. int
  12129. conres() {
  12130.     int x;
  12131.     debug(F101,"conres cgmf","",cgmf);
  12132.     debug(F101,"conres constate","",constate);
  12133.  
  12134.     if (cgmf < 1)            /* Do nothing if modes unchanged */
  12135.       return(0);
  12136.     if (constate == CON_RES)
  12137.       return(0);
  12138.  
  12139.     if (!isatty(0)) return(0);          /* only for real ttys */
  12140.     debug(F100,"conres isatty ok","",0);
  12141.     ckxech = 0;                         /* System should echo chars */
  12142.  
  12143. #ifdef aegis
  12144.     conbufn = 0;
  12145.     if (concrp) return(write(1, "\035\001", 2));
  12146.     if (conuid == input_pad_$uid) {
  12147.     pad_$cooked(ios_$stdin, st);
  12148.     constate = CON_RES;
  12149.     return(0);
  12150.     }
  12151. #endif /* aegis */
  12152.  
  12153. #ifdef Plan9
  12154.     p9conres();
  12155. #else
  12156. #ifdef BSD44ORPOSIX
  12157.     debug(F100,"conres restoring tcsetattr","",0);
  12158.     x = tcsetattr(0,TCSADRAIN,&ccold);
  12159. #else
  12160. #ifdef ATTSV
  12161.     debug(F100,"conres restoring ioctl","",0);
  12162.     x = ioctl(0,TCSETAW,&ccold);
  12163. #else /* BSD, V7, and friends */
  12164. #ifdef sony_news            /* Sony NEWS */
  12165.     if (km_con != -1)
  12166.       ioctl(0,TIOCKSET,&km_con);    /* Restore console Kanji mode */
  12167. #endif /* sony_news */
  12168.     msleep(100);
  12169.     debug(F100,"conres restoring stty","",0);
  12170.     x = conchk();            /* Because stty() is destructive */
  12171.     if (x > 0)
  12172.       congetbuf(x);
  12173. #ifdef BELLV10
  12174.     x = ioctl(0,TIOCSETP,&ccold);
  12175. #else
  12176.     x = stty(0,&ccold);
  12177. #endif /* BELLV10 */
  12178. #endif /* ATTSV */
  12179. #endif /* BSD44ORPOSIX */
  12180. #endif /* Plan9 */
  12181.     if (x > -1)
  12182.       constate = CON_RES;
  12183.  
  12184.     debug(F101,"conres returns","",x);
  12185.     return(x);
  12186. }
  12187.  
  12188. /*  C O N O C  --  Output a character to the console terminal  */
  12189.  
  12190. int
  12191. #ifdef CK_ANSIC
  12192. conoc(char c)
  12193. #else
  12194. conoc(c) char c;
  12195. #endif /* CK_ANSIC */
  12196. /* conoc */ {
  12197.  
  12198. #ifdef IKSD
  12199.     if (inserver && !local)
  12200.       return(ttoc(c));
  12201.  
  12202. #ifdef CK_ENCRYPTION
  12203.     if (inserver && TELOPT_ME(TELOPT_ENCRYPTION))
  12204.         ck_tn_encrypt(&c,1);
  12205. #endif /* CK_ENCRYPTION */
  12206. #endif /* IKSD */
  12207.  
  12208. #ifdef Plan9
  12209.     return conwrite(&c,1);
  12210. #else
  12211.     return(write(1,&c,1));
  12212. #endif /* Plan9 */
  12213. }
  12214.  
  12215. /*  C O N X O  --  Write x characters to the console terminal  */
  12216.  
  12217. int
  12218. conxo(x,s) int x; char *s; {
  12219.  
  12220. #ifdef IKSD
  12221.     if (inserver && !local)
  12222.       return(ttol((CHAR *)s,x));
  12223.  
  12224. #ifdef CK_ENCRYPTION
  12225.     if (inserver && TELOPT_ME(TELOPT_ENCRYPTION))
  12226.         ck_tn_encrypt(s,x);
  12227. #endif /* CK_ENCRYPTION */
  12228. #endif /* IKSD */
  12229.  
  12230. #ifdef Plan9
  12231.     return(conwrite(s,x));
  12232. #else
  12233.     return(write(1,s,x));
  12234. #endif /* Plan9 */
  12235. }
  12236.  
  12237. /*  C O N O L  --  Write a line to the console terminal  */
  12238.  
  12239. int
  12240. conol(s) char *s; {
  12241.     int len;
  12242.     if (!s) s = "";            /* Always do this! */
  12243.     len = strlen(s);
  12244.     if (len == 0)
  12245.       return(0);
  12246.  
  12247. #ifdef IKSD
  12248.     if (inserver && !local)
  12249.       return(ttol((CHAR *)s,len));
  12250.  
  12251. #ifdef CK_ENCRYPTION
  12252.     if (inserver && TELOPT_ME(TELOPT_ENCRYPTION)) {
  12253.     if (nxpacket < len) {
  12254.         if (xpacket) {
  12255.         free(xpacket);
  12256.         xpacket = NULL;
  12257.         nxpacket = 0;
  12258.         }
  12259.         len = len > 10240 ? len : 10240;
  12260.         xpacket = (char *)malloc(len);
  12261.         if (!xpacket) {
  12262.         fprintf(stderr,"ttol malloc failure\n");
  12263.         return(-1);
  12264.         } else
  12265.           nxpacket = len;
  12266.     }
  12267.     memcpy(xpacket,s,len);
  12268.     s = xpacket;
  12269.     ck_tn_encrypt(s,len);
  12270.     }
  12271. #endif /* CK_ENCRYPTION */
  12272. #endif /* IKSD */
  12273.  
  12274. #ifdef Plan9
  12275.     return(conwrite(s,len));
  12276. #else
  12277.     return(write(1,s,len));
  12278. #endif /* Plan9 */
  12279. }
  12280.  
  12281. /*  C O N O L A  --  Write an array of lines to the console terminal */
  12282.  
  12283. int
  12284. conola(s) char *s[]; {
  12285.     char * p;
  12286.     int i, x;
  12287.  
  12288.  
  12289.     if (!s) return(0);
  12290.     for (i = 0; ; i++) {
  12291.     p = s[i];
  12292.     if (!p) p = "";            /* Let's not dump core shall we? */
  12293.     if (!*p)
  12294.       break;
  12295. #ifdef IKSD
  12296.     if (inserver && !local)
  12297.       x = ttol((CHAR *)p,(int)strlen(p));
  12298.     else
  12299. #endif /* IKSD */
  12300.       x = conol(p);
  12301.     if (x < 0)
  12302.       return(-1);
  12303.     }
  12304.     return(0);
  12305. }
  12306.  
  12307. /*  C O N O L L  --  Output a string followed by CRLF  */
  12308.  
  12309. int
  12310. conoll(s) char *s; {
  12311.     CHAR buf[3];
  12312.     buf[0] = '\r';
  12313.     buf[1] = '\n';
  12314.     buf[2] = '\0';
  12315.     if (!s) s = "";
  12316.  
  12317. #ifdef IKSD
  12318.     if (inserver && !local) {
  12319.     if (*s) ttol((CHAR *)s,(int)strlen(s));
  12320.     return(ttol(buf,2));
  12321.     }
  12322. #endif /* IKSD */
  12323.  
  12324.     if (*s) conol(s);
  12325. #ifdef IKSD
  12326. #ifdef CK_ENCRYPTION
  12327.     if (inserver && TELOPT_ME(TELOPT_ENCRYPTION))
  12328.       ck_tn_encrypt(buf,2);
  12329. #endif /* CK_ENCRYPTION */
  12330. #endif /* IKSD */
  12331.  
  12332. #ifdef Plan9
  12333.     return(conwrite(buf, 2));
  12334. #else
  12335.     return(write(1,buf,2));
  12336. #endif /* Plan9 */
  12337. }
  12338.  
  12339. /*  C O N C H K  --  Return how many characters available at console  */
  12340. /*
  12341.   We could also use select() here to cover a few more systems that are not
  12342.   covered by any of the following, e.g. HP-UX 9.0x on the model 800.
  12343. */
  12344. int
  12345. conchk() {
  12346.     static int contyp = 0;        /* +1 for isatty, -1 otherwise */
  12347.  
  12348.     if (contyp == 0)            /* This prevents unnecessary */
  12349.       contyp = (isatty(0) ? 1 : -1);    /* duplicated calls to isatty() */
  12350.     debug(F101,"conchk contyp","",contyp);
  12351.     if (backgrd || (contyp < 0))
  12352.       return(0);
  12353.  
  12354. #ifdef aegis
  12355.     if (conbufn > 0) return(conbufn);   /* use old count if nonzero */
  12356.  
  12357.     /* read in more characters */
  12358.     conbufn = ios_$get(ios_$stdin,
  12359.               ios_$cond_opt, conbuf, (long)sizeof(conbuf), st);
  12360.     if (st.all != status_$ok) conbufn = 0;
  12361.     conbufp = conbuf;
  12362.     return(conbufn);
  12363. #else
  12364. #ifdef IKSD
  12365.     if (inserver && !local)
  12366.       return(in_chk(1,ttyfd));
  12367.     else
  12368. #endif /* IKSD */
  12369.       return(in_chk(0,0));
  12370. #endif /* aegis */
  12371. }
  12372.  
  12373. /*  C O N I N C  --  Get a character from the console  */
  12374. /*
  12375.   Call with timo > 0 to do a timed read, timo == 0 to do an untimed blocking
  12376.   read.  Upon success, returns the character.  Upon failure, returns -1.
  12377.   A timed read that does not complete within the timeout period returns -2.
  12378. */
  12379. int
  12380. coninc(timo) int timo; {
  12381.     int n = 0; CHAR ch;
  12382.     int xx;
  12383.  
  12384.     if (conbufn > 0) {            /* If something already buffered */
  12385.     --conbufn;
  12386.     return((unsigned)(*conbufp++ & 0xff));
  12387.     }
  12388.  
  12389.     errno = 0;                /* Clear this */
  12390. #ifdef IKSD
  12391.     if (inserver && !local) {
  12392.     xx = ttinc(timo);
  12393.     if (xx < 0)
  12394.       return(ttinctimo ? -2 : -1);
  12395.     else
  12396.       return(xx);
  12397.     }
  12398. #endif /* IKSD */
  12399.  
  12400. #ifdef aegis                /* Apollo Aegis only... */
  12401.     debug(F101,"coninc timo","",timo);
  12402.     fflush(stdout);
  12403.     if (conchk() > 0) {
  12404.     --conbufn;
  12405.     return((unsigned)(*conbufp++ & 0xff));
  12406.     }
  12407. #endif /* aegis */
  12408.  
  12409. #ifdef TTLEBUF
  12410.     if (
  12411. #ifdef IKSD
  12412.     inserver &&
  12413. #endif /* IKSD */
  12414.     !xlocal
  12415.     ) {
  12416.     if (ttpush >= 0) {
  12417.         debug(F111,"ttinc","ttpush",ttpush);
  12418.         ch = ttpush;
  12419.         ttpush = -1;
  12420.         return(ch);
  12421.     }
  12422.     if (le_data) {
  12423.         if (le_getchar(&ch) > 0) {
  12424.         debug(F111,"ttinc LocalEchoInBuf","ch",ch);
  12425.         return(ch);
  12426.         }
  12427.     }
  12428.     }
  12429. #endif /* TTLEBUF */
  12430.  
  12431.     if (timo <= 0) {            /* Untimed, blocking read. */
  12432.     while (1) {            /* Keep trying till we get one. */
  12433.         n = read(0, &ch, 1);    /* Read a character. */
  12434.         if (n == 0) continue;    /* Shouldn't happen. */
  12435.         if (n > 0) {        /* If read was successful, */
  12436. #ifdef IKSD
  12437. #ifdef CK_ENCRYPTION
  12438.                 debug(F100,"coninc decrypt 1","",0);
  12439.                 if (inserver && !local && TELOPT_U(TELOPT_ENCRYPTION))
  12440.           ck_tn_decrypt(&ch,1);
  12441. #endif /* CK_ENCRYPTION */
  12442. #endif /* IKSD */
  12443.         return((unsigned)(ch & 0xff)); /* return the character. */
  12444.             }
  12445.  
  12446. /* Come here if read() returned an error. */
  12447.  
  12448.         debug(F101, "coninc(0) errno","",errno); /* Log the error. */
  12449. #ifndef OXOS
  12450. #ifdef SVORPOSIX
  12451. #ifdef CIE                             /* CIE Regulus has no EINTR symbol? */
  12452. #ifndef EINTR
  12453. #define EINTR 4
  12454. #endif /* EINTR */
  12455. #endif /* CIE */
  12456. /*
  12457.   This routine is used for several different purposes.  In CONNECT mode, it is
  12458.   used to do an untimed, blocking read from the keyboard in the lower CONNECT
  12459.   fork.  During local-mode file transfer, it reads a character from the
  12460.   console to interrupt the file transfer (like A for a status report, X to
  12461.   cancel a file, etc).  Obviously, we don't want the reads in the latter case
  12462.   to be blocking, or the file transfer would stop until the user typed
  12463.   something.  Unfortunately, System V does not allow the console device input
  12464.   buffer to be sampled nondestructively (e.g. by conchk()), so a kludge is
  12465.   used instead.  During local-mode file transfer, the SIGQUIT signal is armed
  12466.   and trapped by esctrp(), and this routine pretends to have read the quit
  12467.   character from the keyboard normally.  But, kludge or no kludge, the read()
  12468.   issued by this command, under System V only, can fail if a signal -- ANY
  12469.   signal -- is caught while the read is pending.  This can occur not only when
  12470.   the user types the quit character, but also during telnet negotiations, when
  12471.   the lower CONNECT fork signals the upper one about an echoing mode change.
  12472.   When this happens, we have to post the read() again.  This is apparently not
  12473.   a problem in BSD-based UNIX versions.
  12474. */
  12475.         if (errno == EINTR)        /* Read interrupted. */
  12476.           if (conesc)  {        /* If by SIGQUIT, */
  12477.           conesc = 0;        /* the conesc variable is set, */
  12478.           return(escchr);    /* so return the escape character. */
  12479.          } else continue;        /* By other signal, try again. */
  12480. #else
  12481. /*
  12482.   This might be dangerous, but let's do this on non-System V versions too,
  12483.   since at least one SunOS 4.1.2 user complains of immediate disconnections
  12484.   upon first making a TELNET connection.
  12485. */
  12486.         if (errno == EINTR)        /* Read interrupted. */
  12487.           continue;
  12488. #endif /* SVORPOSIX */
  12489. #else /* OXOS */
  12490.         if (errno == EINTR)        /* Read interrupted. */
  12491.           continue;
  12492. #endif /* OXOS */
  12493.         return(-1);            /* Error */
  12494.     }
  12495.     }
  12496. #ifdef DEBUG
  12497.     if (deblog && timo <= 0) {
  12498.     debug(F100,"coninc timeout logic error","",0);
  12499.     timo = 1;
  12500.     }
  12501. #endif /* DEBUG */
  12502.  
  12503. /* Timed read... */
  12504.  
  12505.     saval = signal(SIGALRM,timerh);    /* Set up timeout handler. */
  12506.     xx = alarm(timo);            /* Set the alarm. */
  12507.     debug(F101,"coninc alarm set","",timo);
  12508.     if (
  12509. #ifdef CK_POSIX_SIG
  12510.     sigsetjmp(sjbuf,1)
  12511. #else
  12512.     setjmp(sjbuf)
  12513. #endif /* CK_POSIX_SIG */
  12514.     )                /* The read() timed out. */
  12515.       n = -2;                /* Code for timeout. */
  12516.     else
  12517.       n = read(0, &ch, 1);
  12518.     ttimoff();                /* Turn off timer */
  12519.     if (n > 0) {            /* Got character OK. */
  12520. #ifdef IKSD
  12521. #ifdef CK_ENCRYPTION
  12522.         debug(F100,"coninc decrypt 2","",0);
  12523.         if (inserver && !local && TELOPT_U(TELOPT_ENCRYPTION))
  12524.       ck_tn_decrypt(&ch,1);
  12525. #endif /* CK_ENCRYPTION */
  12526. #endif /* IKSD */
  12527.     return((unsigned)(ch & 0xff));    /* Return it. */
  12528.     }
  12529. /*
  12530.   read() returned an error.  Same deal as above, but without the loop.
  12531. */
  12532.     debug(F101, "coninc(timo) n","",n);
  12533.     debug(F101, "coninc(timo) errno","",errno);
  12534. #ifndef OXOS
  12535. #ifdef SVORPOSIX
  12536.     if (n == -1 && errno == EINTR && conesc != 0) {
  12537.     conesc = 0;
  12538.     return(escchr);            /* User entered escape character. */
  12539.     }
  12540. #endif /* SVORPOSIX */
  12541.     if (n == 0 && errno > 0) {        /* It's an error */
  12542.     return(-1);
  12543.     }
  12544. #endif /* ! OXOS */
  12545.     return(n);
  12546. }
  12547.  
  12548. /*  C O N G K S  --  Console Get Keyboard Scancode  */
  12549.  
  12550. #ifndef congks
  12551. /*
  12552.   This function needs to be filled in with the various system-dependent
  12553.   system calls used by SUNOS, NeXT OS, Xenix, Aviion, etc, to read a full
  12554.   keyboard scan code.  Unfortunately there aren't any.
  12555. */
  12556. int
  12557. congks(timo) int timo; {
  12558.  
  12559. #ifdef IKSD
  12560.     if (inserver && !local)
  12561.       return(ttinc(timo));
  12562. #endif /* IKSD */
  12563.  
  12564.     return(coninc(timo));
  12565. }
  12566. #endif /* congks */
  12567.  
  12568. #ifdef ATT7300
  12569.  
  12570. /*  A T T D I A L  --  Dial up the remote system using internal modem
  12571.  * Purpose: to open and dial a number on the internal modem available on the
  12572.  * ATT7300 UNIX PC.  Written by Joe Doupnik. Superceeds version written by
  12573.  * Richard E. Hill, Dickinson, TX. which employed dial(3c).
  12574.  * Uses information in <sys/phone.h> and our status int attmodem.
  12575.  */
  12576. attdial(ttname,speed,telnbr) char *ttname,*telnbr; long speed; {
  12577.     char *telnum;
  12578.  
  12579.     attmodem &= ~ISMODEM;                       /* modem not in use yet */
  12580.                     /* Ensure O_NDELAY is set, else i/o traffic hangs */
  12581.                     /* We turn this flag off once the dial is complete */
  12582.     fcntl(ttyfd, F_SETFL, fcntl(ttyfd, F_GETFL, 0) | O_NDELAY);
  12583.  
  12584.     /* Condition line, check availability & DATA mode, turn on speaker */
  12585.     if (ioctl(ttyfd,PIOCOFFHOOK, &dialer) == -1) {
  12586.         printf("cannot access phone\n");
  12587.         ttclos(0);
  12588.         return (-2);
  12589.     }
  12590.     ioctl(ttyfd,PIOCGETP,&dialer);      /* get phone dialer parameters */
  12591.  
  12592.     if (dialer.c_lineparam & VOICE) {    /* phone must be in DATA mode */
  12593.         printf(" Should not dial with modem in VOICE mode.\n");
  12594.         printf(" Exit Kermit, switch to DATA and retry call.\n");
  12595.         ttclos(0);
  12596.         return (-2);
  12597.     }
  12598. #ifdef ATTTONED                /* Old way, tone dialing only. */
  12599.     dialer.c_lineparam = DATA | DTMF;    /* Dial with tones, */
  12600.     dialer.c_lineparam &= ~PULSE;    /* not with pulses. */
  12601. #else
  12602.     /* Leave current pulse/tone state alone. */
  12603.     /* But what about DATA?  Add it back if you have trouble. */
  12604.     /* sys/phone says you get DATA automatically by opening device RDWR */
  12605. #endif
  12606.     dialer.c_waitdialtone = 5;                  /* wait 5 sec for dialtone */
  12607. #ifdef COMMENT
  12608.     dialer.c_feedback = SPEAKERON|NORMSPK|RINGON;  /* control speaker */
  12609. #else
  12610.     /* sys/phone says RINGON used only for incoming voice calls */
  12611.     dialer.c_feedback &= ~(SOFTSPK|LOUDSPK);
  12612.     dialer.c_feedback |= SPEAKERON|NORMSPK;
  12613. #endif
  12614.     dialer.c_waitflash = 500;                   /* 0.5 sec flash hook */
  12615.     if(ioctl(ttyfd,PIOCSETP,&dialer) == -1) {   /* set phone parameters */
  12616.         printf("Cannot set modem characteristics\n");
  12617.         ttclos(0);
  12618.         return (-2);
  12619.     }
  12620.     ioctl(ttyfd,PIOCRECONN,0);        /* Turns on speaker for pulse */
  12621.  
  12622. #ifdef COMMENT
  12623.     fprintf(stderr,"Phone line status. line_par:%o dialtone_wait:%o \
  12624. line_status:%o feedback:%o\n",
  12625.     dialer.c_lineparam, dialer.c_waitdialtone,
  12626.     dialer.c_linestatus, dialer.c_feedback);
  12627. #endif
  12628.  
  12629.     attmodem |= ISMODEM;                        /* modem is now in-use */
  12630.     sleep(1);
  12631.     for (telnum = telnbr; *telnum != '\0'; telnum++)    /* dial number */
  12632. #ifdef ATTTONED
  12633.       /* Tone dialing only */
  12634.       if (ioctl(ttyfd,PIOCDIAL,telnum) != 0) {
  12635.       perror("Error in dialing");
  12636.       ttclos(0);
  12637.       return(-2);
  12638.       }
  12639. #else /* Allow Pulse or Tone dialing */
  12640.     switch (*telnum) {
  12641.       case 't': case 'T': case '%':    /* Tone dialing requested */
  12642.     dialer.c_lineparam |= DTMF;
  12643.     dialer.c_lineparam &= ~PULSE;
  12644.     if (ioctl(ttyfd,PIOCSETP,&dialer) == -1) {
  12645.         printf("Cannot set modem to tone dialing\n");
  12646.         ttclos(0);
  12647.         return(-2);
  12648.     }
  12649.     break;
  12650.       case 'd': case 'D': case 'p': case 'P': case '^':
  12651.     dialer.c_lineparam |= PULSE;
  12652.     dialer.c_lineparam &= ~DTMF;
  12653.     if (ioctl(ttyfd,PIOCSETP,&dialer) == -1) {
  12654.         printf("Cannot set modem to pulse dialing\n");
  12655.         ttclos(0);
  12656.         return(-2);
  12657.     }
  12658.     break;
  12659.       default:
  12660.         if (ioctl(ttyfd,PIOCDIAL,telnum) != 0) {
  12661.         perror("Dialing error");
  12662.         ttclos(0);
  12663.         return(-2);
  12664.     }
  12665.     break;
  12666.     }
  12667. #endif
  12668.  
  12669.     ioctl(ttyfd,PIOCDIAL,"@");        /* terminator for data call */
  12670.     do {                /* wait for modems to Connect */
  12671.         if (ioctl(ttyfd,PIOCGETP,&dialer) != 0)    { /* get params */
  12672.         perror("Cannot get modems to connect");
  12673.         ttclos(0);
  12674.         return(-2);
  12675.     }
  12676.     } while ((dialer.c_linestatus & MODEMCONNECTED) == 0);
  12677.     /* Turn off O_NDELAY flag now. */
  12678.     fcntl(ttyfd, F_SETFL, fcntl(ttyfd, F_GETFL, 0) & ~O_NDELAY);
  12679.     signal(SIGHUP, sighup);             /* hangup on loss of carrier */
  12680.     return(0);                          /* return success */
  12681. }
  12682.  
  12683. /*
  12684.   Offgetty, ongetty functions. These function get the 'getty(1m)' off
  12685.   and restore it to the indicated line.  Shell's return codes are:
  12686.     0: Can't do it.  Probably a user logged on.
  12687.     1: No need.  No getty on that line.
  12688.     2: Done, you should restore the getty when you're done.
  12689.   DOGETY System(3), however, returns them as 0, 256, 512, respectively.
  12690.   Thanks to Kevin O'Gorman, Anarm Software Systems.
  12691.  
  12692.    getoff.sh looks like:   geton.sh looks like:
  12693.      setgetty $1 0           setgetty $1 1
  12694.      err=$?                  exit $?
  12695.      sleep 2
  12696.      exit $err
  12697. */
  12698.  
  12699. /*  O F F G E T T Y  --  Turn off getty(1m) for the communications tty line
  12700.  * and get status so it can be restarted after the line is hung up.
  12701.  */
  12702. int
  12703. offgetty(ttname) char *ttname; {
  12704.     char temp[30];
  12705.     while (*ttname != '\0') ttname++;       /* seek terminator of path */
  12706.     ttname -= 3;                            /* get last 3 chars of name */
  12707.     sprintf(temp,"/usr/bin/getoff.sh %s",ttname);
  12708.     return(zsyscmd(temp));
  12709. }
  12710.  
  12711. /*  O N G E T T Y  --  Turn on getty(1m) for the communications tty line */
  12712.  
  12713. int
  12714. ongetty(ttname) char *ttname; {
  12715.     char temp[30];
  12716.     while (*ttname != '\0') ttname++;       /* comms tty path name */
  12717.     ttname -= 3;
  12718.     sprintf(temp,"/usr/bin/geton.sh %s",ttname);
  12719.     return(zsyscmd(temp));
  12720. }
  12721. #endif /* ATT7300 */
  12722.  
  12723. /*  T T S C A R R  --  Set ttcarr variable, controlling carrier handling.
  12724.  *
  12725.  *  0 = Off: Always ignore carrier. E.g. you can connect without carrier.
  12726.  *  1 = On: Heed carrier, except during dialing. Carrier loss gives disconnect.
  12727.  *  2 = Auto: For "modem direct": The same as "Off".
  12728.  *            For real modem types: Heed carrier during connect, but ignore
  12729.  *                it anytime else.  Compatible with pre-5A C-Kermit versions.
  12730.  *
  12731.  * As you can see, this setting does not affect dialing, which always ignores
  12732.  * carrier (unless there is some special exception for some modem type).  It
  12733.  * does affect ttopen() if it is set before ttopen() is used.  This setting
  12734.  * takes effect on the next call to ttopen()/ttpkt()/ttvt().  And they are
  12735.  * (or should be) always called before any communications is tried, which
  12736.  * means that, practically speaking, the effect is immediate.
  12737.  *
  12738.  * Of course, nothing of this applies to remote mode (xlocal = 0).
  12739.  *
  12740.  * Someone has yet to uncover how to manipulate the carrier in the BSD
  12741.  * environment (or any non-termio using environment).  Until that time, this
  12742.  * will simply be a no-op for BSD.
  12743.  *
  12744.  * Note that in previous versions, the carrier was most often left unchanged
  12745.  * in ttpkt()/ttvt() unless they were called with FLO_DIAL or FLO_DIAX.  This
  12746.  * has changed.  Now it is controlled by ttcarr in conjunction with these
  12747.  * modes.
  12748.  */
  12749. int
  12750. ttscarr(carrier) int carrier; {
  12751.     ttcarr = carrier;
  12752.     debug(F101, "ttscarr","",ttcarr);
  12753.     return(ttcarr);
  12754. }
  12755.  
  12756. /* C A R R C T L  --  Set tty modes for carrier treatment.
  12757.  *
  12758.  * Sets the appropriate bits in a termio or sgttyb struct for carrier control
  12759.  * (actually, there are no bits in sgttyb for that), or performs any other
  12760.  * operations needed to control this on the current system.  The function does
  12761.  * not do the actual TCSETA or stty, since often we want to set other bits too
  12762.  * first.  Don't call this function when xlocal is 0, or the tty is not opened.
  12763.  *
  12764.  * We don't know how to do anything like carrier control on non-ATTSV systems,
  12765.  * except, apparently, ultrix.  See above.  It is also known that this doesn't
  12766.  * have much effect on a Xenix system.  For Xenix, one should switch back and
  12767.  * forth between the upper and lower case device files.  Maybe later.
  12768.  * Presently, Xenix will stick to the mode it was opened with.
  12769.  *
  12770.  * carrier: 0 = ignore carrier, 1 = require carrier.
  12771.  * The current state is saved in curcarr, and checked to save labour.
  12772.  */
  12773. #ifdef SVORPOSIX
  12774. int
  12775. #ifdef BSD44ORPOSIX
  12776. carrctl(ttpar, carrier)    struct termios *ttpar; int carrier;
  12777. #else /* ATTSV */
  12778. carrctl(ttpar, carrier)    struct termio *ttpar; int carrier;
  12779. #endif /* BSD44ORPOSIX */
  12780. /* carrctl */ {
  12781.     debug(F101, "carrctl","",carrier);
  12782.     if (carrier)
  12783.       ttpar->c_cflag &= ~CLOCAL;
  12784.     else
  12785.       ttpar->c_cflag |= CLOCAL;
  12786.     return(0);
  12787. }
  12788. #else /* Berkeley, V7, et al... */
  12789. int
  12790. carrctl(ttpar, carrier) struct sgttyb *ttpar; int carrier; {
  12791.     debug(F101, "carrctl","",carrier);
  12792.     if (carrier == curcarr)
  12793.       return(0);
  12794.     curcarr = carrier;
  12795. #ifdef ultrix
  12796. #ifdef COMMENT
  12797. /*
  12798.   Old code from somebody at DEC that tends to get stuck, time out, etc.
  12799. */
  12800.     if (carrier) {
  12801.     ioctl(ttyfd, TIOCMODEM, &temp);
  12802.     ioctl(ttyfd, TIOCHPCL, 0);
  12803.     } else {
  12804.     /* (According to the manuals, TIOCNCAR should be preferred */
  12805.     /* over TIOCNMODEM...) */
  12806.     ioctl(ttyfd, TIOCNMODEM, &temp);
  12807.     }
  12808. #else
  12809. /*
  12810.   New code from Jamie Watson that, he says, eliminates the problems.
  12811. */
  12812.     if (carrier) {
  12813.     ioctl(ttyfd, TIOCCAR);
  12814.     ioctl(ttyfd, TIOCHPCL);
  12815.     } else {
  12816.     ioctl(ttyfd, TIOCNCAR);
  12817.     }
  12818. #endif /* COMMENT */
  12819. #endif /* ultrix */
  12820.     return(0);
  12821. }
  12822. #endif /* SVORPOSIX */
  12823.  
  12824.  
  12825. /*  T T G M D M  --  Get modem signals  */
  12826. /*
  12827.  Looks for RS-232 modem signals, and returns those that are on in as its
  12828.  return value, in a bit mask composed of the BM_xxx values defined in ckcdeb.h.
  12829.  Returns:
  12830.  -3 Not implemented
  12831.  -2 if the communication device does not have modem control (e.g. telnet)
  12832.  -1 on error.
  12833.  >= 0 on success, with a bit mask containing the modem signals that are on.
  12834. */
  12835.  
  12836. /*
  12837.   Define the symbol K_MDMCTL if we have Sys V R3 / 4.3 BSD style
  12838.   modem control, namely the TIOCMGET ioctl.
  12839. */
  12840.  
  12841. #ifdef BSD43
  12842. #define K_MDMCTL
  12843. #endif /* BSD43 */
  12844.  
  12845. #ifdef SUNOS4
  12846. #define K_MDMCTL
  12847. #endif /* SUNOS4 */
  12848.  
  12849. /*
  12850.   SCO OpenServer R5.0.4.  The TIOCMGET definition is hardwired in because it
  12851.   is skipped in termio.h when _POSIX_SOURCE is defined.  But _POSIX_SOURCE
  12852.   must be defined in order to get the high serial speeds that are new to
  12853.   5.0.4.  However, the regular SCO drivers do not implement TIOCMGET, so the
  12854.   ioctl() returns -1 with errno 22 (invalid function).  But third-party
  12855.   drivers, e.g. for Digiboard, do implement it, and so it should work on ports
  12856.   driven by those drivers.
  12857. */
  12858. #ifdef SCO_OSR504
  12859. #ifndef TIOCMGET
  12860. #define TIOCMGET (('t'<<8)|29)
  12861. #endif /* TIOCMGET */
  12862. #endif /* SCO_OSR504 */
  12863.  
  12864. #ifdef CK_SCOV5
  12865. /* Because POSIX strictness in <sys/termio.h> won't let us see these. */
  12866. #ifndef TIOCM_DTR
  12867. #define TIOCM_DTR    0x0002        /* data terminal ready */
  12868. #define TIOCM_RTS    0x0004        /* request to send */
  12869. #define TIOCM_CTS    0x0020        /* clear to send */
  12870. #define TIOCM_CAR    0x0040        /* carrier detect */
  12871. #define TIOCM_RNG    0x0080        /* ring */
  12872. #define TIOCM_DSR    0x0100        /* data set ready */
  12873. #define TIOCM_CD    TIOCM_CAR
  12874. #define TIOCM_RI    TIOCM_RNG
  12875. #endif /* TIOCM_DTR */
  12876. #endif /* CK_SCOV5 */
  12877.  
  12878. #ifdef QNX
  12879. #define K_MDMCTL
  12880. #else
  12881. #ifdef TIOCMGET
  12882. #define K_MDMCTL
  12883. #endif /* TIOCMGET */
  12884. #endif /* QNX */
  12885. /*
  12886.   "A serial communication program that can't read modem signals
  12887.    is like a car without windows."
  12888. */
  12889. int
  12890. ttgmdm() {
  12891.  
  12892. #ifdef QNX
  12893. #include <sys/qioctl.h>
  12894.  
  12895.     unsigned long y, mdmbits[2];
  12896.     int x, z = 0;
  12897.  
  12898.     if (xlocal && ttyfd < 0)
  12899.       return(-1);
  12900.  
  12901. #ifdef NETCMD
  12902.     if (ttpipe) return(-2);
  12903. #endif /* NETCMD */
  12904. #ifdef NETPTY
  12905.     if (ttpty) return(-2);
  12906. #endif /* NETPTY */
  12907.  
  12908.     mdmbits[0] = 0L;
  12909.     mdmbits[1] = 0L;
  12910. /*
  12911.  * From <sys/qioctl.h>:
  12912.  *
  12913.  * SERIAL devices   (all Dev.ser versions)
  12914.  * 0 : DTR           8 = Data Bits 0  16 - reserved     24 - reserved
  12915.  * 1 : RTS           9 = Data Bits 1  17 - reserved     25 - reserved
  12916.  * 2 = Out 1        10 = Stop Bits    18 - reserved     26 - reserved
  12917.  * 3 = Int Enable   11 = Par Enable   19 - reserved     27 - reserved
  12918.  * 4 = Loop         12 = Par Even     20 = CTS          28 - reserved
  12919.  * 5 - reserved     13 = Par Stick    21 = DSR          29 - reserved
  12920.  * 6 - reserved     14 : Break        22 = RI           30 - reserved
  12921.  * 7 - reserved     15 = 0            23 = CD           31 - reserved
  12922.  */
  12923.     errno = 0;
  12924.     x = qnx_ioctl(ttyfd, QCTL_DEV_CTL, &mdmbits[0], 8, &mdmbits[0], 4);
  12925.     debug(F101,"ttgmdm qnx_ioctl","",x);
  12926.     debug(F101,"ttgmdm qnx_ioctl errno","",errno);
  12927.     if (!x) {
  12928.     debug(F101,"ttgmdm qnx_ioctl mdmbits[0]","",mdmbits[0]);
  12929.     debug(F101,"ttgmdm qnx_ioctl mdmbits[1]","",mdmbits[1]);
  12930.     y = mdmbits[0];
  12931.     if (y & 0x000001L) z |= BM_DTR;    /* Bit  0 */
  12932.     if (y & 0x000002L) z |= BM_RTS;    /* Bit  1 */
  12933.     if (y & 0x100000L) z |= BM_CTS;    /* Bit 20 */
  12934.     if (y & 0x200000L) z |= BM_DSR;    /* Bit 21 */
  12935.     if (y & 0x400000L) z |= BM_RNG;    /* Bit 22 */
  12936.     if (y & 0x800000L) z |= BM_DCD;    /* Bit 23 */
  12937.     debug(F101,"ttgmdm qnx result","",z);
  12938.     debug(F110,"ttgmdm qnx CD = ",(z & BM_DCD) ? "On" : "Off", 0);
  12939.     gotsigs = 1;
  12940.     return(z);
  12941.     } else return(-1);
  12942. #else
  12943. #ifdef HPUX                /* HPUX has its own way */
  12944.     int x, z;
  12945.  
  12946. #ifdef HPUX10                /* Modem flag word */
  12947.     mflag y;                /* mflag typedef'd in <sys/modem.h> */
  12948. #else
  12949. #ifdef HPUX9
  12950.     mflag y;
  12951. #else
  12952. #ifdef HPUX8
  12953.     mflag y;
  12954. #else
  12955.     unsigned long y;            /* Not sure about pre-8.0... */
  12956. #endif /* HPUX8 */
  12957. #endif /* HPUX9 */
  12958. #endif /* HPUX10 */
  12959.  
  12960.     if (xlocal && ttyfd < 0)
  12961.       return(-1);
  12962.  
  12963. #ifdef NETCONN
  12964.     if (netconn) {            /* Network connection */
  12965. #ifdef TN_COMPORT
  12966.         if (istncomport()) {
  12967.         gotsigs = 1;
  12968.         return(tngmdm());
  12969.     } else
  12970. #endif /* TN_COMPORT */
  12971.       return(-2);            /* No modem signals */
  12972.     }
  12973. #endif /* NETCONN */
  12974.  
  12975. #ifdef NETCMD
  12976.     if (ttpipe) return(-2);
  12977. #endif /* NETCMD */
  12978. #ifdef NETPTY
  12979.     if (ttpty) return(-2);
  12980. #endif /* NETPTY */
  12981.  
  12982.     if (xlocal)                /* Get modem signals */
  12983.       x = ioctl(ttyfd,MCGETA,&y);
  12984.     else
  12985.       x = ioctl(0,MCGETA,&y);
  12986.     if (x < 0) return(-1);
  12987.     debug(F101,"ttgmdm","",y);
  12988.  
  12989.     z = 0;                /* Initialize return value */
  12990.  
  12991. /* Now set bits for each modem signal that is reported to be on. */
  12992.  
  12993. #ifdef MCTS
  12994.     /* Clear To Send */
  12995.     debug(F101,"ttgmdm HPUX CTS","",y & MCTS);
  12996.     if (y & MCTS) z |= BM_CTS;
  12997. #endif
  12998. #ifdef MDSR
  12999.     /* Data Set Ready */
  13000.     debug(F101,"ttgmdm HPUX DSR","",y & MDSR);
  13001.     if (y & MDSR) z |= BM_DSR;
  13002. #endif
  13003. #ifdef MDCD
  13004.     /* Carrier */
  13005.     debug(F101,"ttgmdm HPUX DCD","",y & MDCD);
  13006.     if (y & MDCD) z |= BM_DCD;
  13007. #endif
  13008. #ifdef MRI
  13009.     /* Ring Indicate */
  13010.     debug(F101,"ttgmdm HPUX RI","",y & MRI);
  13011.     if (y & MRI) z |= BM_RNG;
  13012. #endif
  13013. #ifdef MDTR
  13014.     /* Data Terminal Ready */
  13015.     debug(F101,"ttgmdm HPUX DTR","",y & MDTR);
  13016.     if (y & MDTR) z |= BM_DTR;
  13017. #endif
  13018. #ifdef MRTS
  13019.     /* Request To Send */
  13020.     debug(F101,"ttgmdm HPUX RTS","",y & MRTS);
  13021.     if (y & MRTS) z |= BM_RTS;
  13022. #endif
  13023.     gotsigs = 1;
  13024.     return(z);
  13025.  
  13026. #else /* ! HPUX */
  13027.  
  13028. #ifdef K_MDMCTL
  13029. /*
  13030.   Note, TIOCMGET might already have been defined in <sys/ioctl.h> or elsewhere.
  13031.   If not, we try including <sys/ttycom.h> -- if this blows up then more ifdefs
  13032.   are needed.
  13033. */
  13034. #ifndef TIOCMGET
  13035. #include <sys/ttycom.h>
  13036. #endif /* TIOCMGET */
  13037.  
  13038.     int x, y, z;
  13039.  
  13040.     debug(F100,"ttgmdm K_MDMCTL defined","",0);
  13041.  
  13042.     if (netconn)            /* Network connection */
  13043.       return(-2);            /* No modem signals */
  13044.  
  13045. #ifdef NETCMD
  13046.     if (ttpipe) return(-2);
  13047. #endif /* NETCMD */
  13048. #ifdef NETPTY
  13049.     if (ttpty) return(-2);
  13050. #endif /* NETPTY */
  13051.  
  13052.     if (xlocal && ttyfd < 0)
  13053.       return(-1);
  13054.  
  13055.     if (xlocal)
  13056.       x = ioctl(ttyfd,TIOCMGET,&y);    /* Get modem signals. */
  13057.     else
  13058.       x = ioctl(0,TIOCMGET,&y);
  13059.     debug(F101,"ttgmdm TIOCMGET ioctl","",x);
  13060.     if (x < 0) {
  13061.     debug(F101,"ttgmdm errno","",errno);
  13062.     return(-1);
  13063.     }
  13064.     debug(F101,"ttgmdm bits","",y);
  13065.  
  13066.     z = 0;                /* Initialize return value. */
  13067. #ifdef TIOCM_CTS
  13068.     /* Clear To Send */
  13069.     if (y & TIOCM_CTS) z |= BM_CTS;
  13070.     debug(F101,"ttgmdm TIOCM_CTS defined","",TIOCM_CTS); 
  13071. #else
  13072.     debug(F100,"ttgmdm TIOCM_CTS not defined","",0);
  13073. #endif
  13074. #ifdef TIOCM_DSR
  13075.     /* Data Set Ready */
  13076.     if (y & TIOCM_DSR) z |= BM_DSR;
  13077.     debug(F101,"ttgmdm TIOCM_DSR defined","",TIOCM_DSR); 
  13078. #else
  13079.     debug(F100,"ttgmdm TIOCM_DSR not defined","",0);
  13080. #endif
  13081. #ifdef TIOCM_CAR
  13082.     /* Carrier */
  13083.     if (y & TIOCM_CAR) z |= BM_DCD;
  13084.     debug(F101,"ttgmdm TIOCM_CAR defined","",TIOCM_CAR); 
  13085. #else
  13086.     debug(F100,"ttgmdm TIOCM_CAR not defined","",0);
  13087. #endif
  13088. #ifdef TIOCM_RNG
  13089.     /* Ring Indicate */
  13090.     if (y & TIOCM_RNG) z |= BM_RNG;
  13091.     debug(F101,"ttgmdm TIOCM_RNG defined","",TIOCM_RNG); 
  13092. #else
  13093.     debug(F100,"ttgmdm TIOCM_RNG not defined","",0);
  13094. #endif
  13095. #ifdef TIOCM_DTR
  13096.     /* Data Terminal Ready */
  13097.     if (y & TIOCM_DTR) z |= BM_DTR;
  13098.     debug(F101,"ttgmdm TIOCM_DTR defined","",TIOCM_DTR); 
  13099. #else
  13100.     debug(F100,"ttgmdm TIOCM_DTR not defined","",0);
  13101. #endif
  13102. #ifdef TIOCM_RTS
  13103.     /* Request To Send */
  13104.     if (y & TIOCM_RTS) z |= BM_RTS;
  13105.     debug(F101,"ttgmdm TIOCM_RTS defined","",TIOCM_RTS); 
  13106. #else
  13107.     debug(F100,"ttgmdm TIOCM_RTS not defined","",0);
  13108. #endif
  13109.     gotsigs = 1;
  13110.     return(z);
  13111.  
  13112. #else /* !K_MDMCTL catch-All */
  13113.  
  13114.     debug(F100,"ttgmdm K_MDMCTL not defined","",0);
  13115. #ifdef TIOCMGET
  13116.     debug(F100,"ttgmdm TIOCMGET defined","",0);
  13117. #else
  13118.     debug(F100,"ttgmdm TIOCMGET not defined","",0);
  13119. #endif /* TIOCMGET */
  13120. #ifdef _SVID3
  13121.     debug(F100,"ttgmdm _SVID3 defined","",0);
  13122. #else
  13123.     debug(F100,"ttgmdm _SVID3 not defined","",0);
  13124. #endif /* _SVID3 */
  13125.  
  13126.     if (netconn)            /* Network connection */
  13127.       return(-2);            /* No modem signals */
  13128.  
  13129. #ifdef NETCMD
  13130.     if (ttpipe) return(-2);
  13131. #endif /* NETCMD */
  13132. #ifdef NETPTY
  13133.     if (ttpty) return(-2);
  13134. #endif /* NETPTY */
  13135.  
  13136.     return(-3);                /* Sorry, I don't know how... */
  13137.  
  13138. #endif /* K_MDMCTL */
  13139. #endif /* HPUX */
  13140. #endif /* QNX */
  13141. }
  13142.  
  13143. /*  P S U S P E N D  --  Put this process in the background.  */
  13144.  
  13145. /*
  13146.   Call with flag nonzero if suspending is allowed, zero if not allowed.
  13147.   Returns 0 on apparent success, -1 on failure (flag was zero, or
  13148.   kill() returned an error code.
  13149. */
  13150. int
  13151. psuspend(flag) int flag; {
  13152.  
  13153. #ifdef RTU
  13154.     extern int rtu_bug;
  13155. #endif /* RTU */
  13156.  
  13157.     if (flag == 0) return(-1);
  13158.  
  13159. #ifdef NOJC
  13160.     return(-1);
  13161. #else
  13162. #ifdef SIGTSTP
  13163. /*
  13164.   The big question here is whether job control is *really* supported.
  13165.   There's no way Kermit can know for sure.  The fact that SIGTSTP is
  13166.   defined does not guarantee the Unix kernel supports it, and the fact
  13167.   that the Unix kernel supports it doesn't guarantee that the user's
  13168.   shell (or other process that invoked Kermit) supports it.
  13169. */
  13170. #ifdef RTU
  13171.     rtu_bug = 1;
  13172. #endif /* RTU */
  13173.     if (kill(0,SIGSTOP) < 0
  13174. #ifdef MIPS
  13175. /* Let's try this for MIPS too. */
  13176.     && kill(getpid(),SIGSTOP) < 0
  13177. #endif /* MIPS */
  13178.     ) {                /* If job control, suspend the job */
  13179.     perror("suspend");
  13180.     debug(F101,"psuspend error","",errno);
  13181.     return(-1);
  13182.     }
  13183.     debug(F100,"psuspend ok","",0);
  13184.     return(0);
  13185. #else
  13186.     return(-1);
  13187. #endif /* SIGTSTP */
  13188. #endif /* NOJC */
  13189. }
  13190.  
  13191. /*
  13192.   setuid package, by Kristoffer Eriksson, with contributions from Dean
  13193.   Long and fdc.
  13194. */
  13195.  
  13196. /* The following is for SCO when CK_ANSILIBS is defined... */
  13197. #ifdef M_UNIX
  13198. #ifdef CK_ANSILIBS
  13199. #ifndef NOGETID_PROTOS
  13200. #define NOGETID_PROTOS
  13201. #endif /* NOGETID_PROTOS */
  13202. #endif /* CK_ANSILIBS */
  13203. #endif /* M_UNIX */
  13204.  
  13205. #ifndef _POSIX_SOURCE
  13206. #ifndef SUNOS4
  13207. #ifndef NEXT
  13208. #ifndef PS2AIX10
  13209. #ifndef sequent
  13210. #ifndef HPUX9
  13211. #ifndef COHERENT
  13212. #ifndef NOGETID_PROTOS
  13213. extern UID_T getuid(), geteuid(), getreuid();
  13214. extern GID_T getgid(), getegid(), getregid();
  13215. #endif /* NOGETID_PROTOS */
  13216. #else
  13217. extern UID_T getreuid();
  13218. extern GID_T getregid();
  13219. #endif /* COHERENT */
  13220. #endif /* HPUX9 */
  13221. #endif /* sequent */
  13222. #endif /* PS2AIX10 */
  13223. #endif /* NEXT */
  13224. #endif /* SUNOS4 */
  13225. #endif /* _POSIX_SOURCE */
  13226.  
  13227. /*
  13228. Subject: Set-user-id
  13229. To: fdc@watsun.cc.columbia.edu (Frank da Cruz)
  13230. Date: Sat, 21 Apr 90 4:48:25 MES
  13231. From: Kristoffer Eriksson <ske@pkmab.se>
  13232.  
  13233. This is a set of functions to be used in programs that may be run set-user-id
  13234. and/or set-group-id. They handle both the case where the program is not run
  13235. with such privileges (nothing special happens then), and the case where one
  13236. or both of these set-id modes are used.  The program is made to run with the
  13237. user's real user and group ids most of the time, except for when more
  13238. privileges are needed.  Don't set-user-id to "root".
  13239.  
  13240. This works on System V and POSIX.  In BSD, it depends on the
  13241. "saved-set-user-id" feature.
  13242. */
  13243.  
  13244. #define UID_ROOT 0            /* Root user and group ids */
  13245. #define GID_ROOT 0
  13246.  
  13247. /*
  13248.   The following code defines the symbol SETEUID for UNIX systems based
  13249.   on BSD4.4 (either -Encumbered or -Lite).  This program will then use
  13250.   seteuid() and setegid() instead of setuid() and setgid(), which still
  13251.   don't allow arbitrary switching.  It also avoids setreuid() and
  13252.   setregid(), which are included in BSD4.4 for compatibility only, are
  13253.   insecure, and print warnings to stderr under at least one system (NetBSD
  13254.   1.0).  Note that POSIX systems should still use setuid() and setgid();
  13255.   the seteuid() and setegid() functions are BSD4.4 extensions to the
  13256.   POSIX model.  Mike Long <mike.long@analog.com>, 8/94.
  13257. */
  13258. #ifdef BSD44
  13259. #define SETEUID
  13260. #endif /* BSD44 */
  13261.  
  13262. /*
  13263.   The following construction automatically defines the symbol SETREUID for
  13264.   UNIX versions based on Berkeley Unix 4.2 and 4.3.  If this symbol is
  13265.   defined, then this program will use getreuid() and getregid() calls in
  13266.   preference to getuid() and getgid(), which in Berkeley-based Unixes do
  13267.   not allow arbitrary switching back and forth of real & effective uid.
  13268.   This construction also allows -DSETREUID to be put on the cc command line
  13269.   for any system that has and wants to use setre[ug]id().  It also prevents
  13270.   automatic definition of SETREUID if -DNOSETREU is included on the cc
  13271.   command line (or otherwise defined).
  13272. */
  13273. #ifdef FT18                /* None of this for Fortune. */
  13274. #define NOSETREU
  13275. #endif /* FT18 */
  13276.  
  13277. #ifdef ANYBSD
  13278. #ifndef BSD29
  13279. #ifndef BSD41
  13280. #ifndef SETREUID
  13281. #ifndef NOSETREU
  13282. #ifndef SETEUID
  13283. #define SETREUID
  13284. #endif /* SETEUID */
  13285. #endif /* NOSETREU */
  13286. #endif /* SETREUID */
  13287. #endif /* !BSD41 */
  13288. #endif /* !BSD29 */
  13289. #endif /* ANYBSD */
  13290.  
  13291. /* Variables for user and group IDs. */
  13292.  
  13293. static UID_T realuid = (UID_T) -1, privuid = (UID_T) -1;
  13294. static GID_T realgid = (GID_T) -1, privgid = (GID_T) -1;
  13295.  
  13296.  
  13297. /* P R I V _ I N I  --  Initialize privileges package  */
  13298.  
  13299. /* Called as early as possible in a set-uid or set-gid program to store the
  13300.  * set-to uid and/or gid and step down to the users real uid and gid. The
  13301.  * stored id's can be temporarily restored (allowed in System V) during
  13302.  * operations that require the privilege.  Most of the time, the program
  13303.  * should execute in unpriviliged state, to not impose any security threat.
  13304.  *
  13305.  * Note: Don't forget that access() always uses the real id:s to determine
  13306.  * file access, even with privileges restored.
  13307.  *
  13308.  * Returns an error mask, with error values or:ed together:
  13309.  *   1 if setuid() fails,
  13310.  *   2 if setgid() fails, and
  13311.  *   4 if the program is set-user-id to "root", which can't be handled.
  13312.  *
  13313.  * Only the return value 0 indicates real success. In case of failure,
  13314.  * those privileges that could be reduced have been, at least, but the
  13315.  * program should be aborted none-the-less.
  13316.  *
  13317.  * Also note that these functions do not expect the uid or gid to change
  13318.  * without their knowing. It may work if it is only done temporarily, but
  13319.  * you're on your own.
  13320.  */
  13321. int
  13322. priv_ini() {
  13323.     int err = 0;
  13324.  
  13325.     /* Save real ID:s. */
  13326.     realuid = getuid();
  13327.     realgid = getgid();
  13328.  
  13329.     /* Save current effective ID:s, those set to at program exec. */
  13330.     privuid = geteuid();
  13331.     privgid = getegid();
  13332.  
  13333.     /* If running set-uid, go down to real uid, otherwise remember that
  13334.      * no privileged uid is available.
  13335.      *
  13336.      * Exceptions:
  13337.      *
  13338.      * 1) If the real uid is already "root" and the set-uid uid (the
  13339.      * initial effective uid) is not "root", then we would have trouble
  13340.      * if we went "down" to "root" here, and then temporarily back to the
  13341.      * set-uid uid (not "root") and then again tried to become "root". I
  13342.      * think the "saved set-uid" is lost when changing uid from effective
  13343.      * uid "root", which changes all uid, not only the effective uid. But
  13344.      * in this situation, we can simply go to "root" and stay there all
  13345.      * the time. That should give sufficient privilege (understatement!),
  13346.      * and give the right uids for subprocesses.
  13347.      *
  13348.      * 2) If the set-uid (the initial effective uid) is "root", and we
  13349.      * change uid to the real uid, we can't change it back to "root" when
  13350.      * we need the privilege, for the same reason as in 1). Thus, we can't
  13351.      * handle programs that are set-user-id to "root" at all. The program
  13352.      * should be stopped.  Use some other uid.  "root" is probably too
  13353.      * privileged for such things, anyway. (The uid is reverted to the
  13354.      * real uid until termination.)
  13355.      *
  13356.      * These two exceptions have the effect that the "root" uid will never
  13357.      * be one of the two uids that are being switched between, which also
  13358.      * means we don't have to check for such cases in the switching
  13359.      * functions.
  13360.      *
  13361.      * Note that exception 1) is handled by these routines (by constantly
  13362.      * running with uid "root", while exception 2) is a serious error, and
  13363.      * is not provided for at all in the switching functions.
  13364.      */
  13365.     if (realuid == privuid)
  13366.     privuid = (UID_T) -1;        /* Not running set-user-id. */
  13367.  
  13368.     /* If running set-gid, go down to real gid, otherwise remember that
  13369.      * no privileged gid is available.
  13370.      *
  13371.      * There are no exception like there is for the user id, since there
  13372.      * is no group id that is privileged in the manner of uid "root".
  13373.      * There could be equivalent problems for group changing if the
  13374.      * program sometimes ran with uid "root" and sometimes not, but
  13375.      * that is already avoided as explained above.
  13376.      *
  13377.      * Thus we can expect always to be able to switch to the "saved set-
  13378.      * gid" when we want, and back to the real gid again. You may also
  13379.      * draw the conclusion that set-gid provides for fewer hassles than
  13380.      * set-uid.
  13381.      */
  13382.  
  13383. #ifdef SUIDDEBUG
  13384.     fprintf(stderr,"UID_ROOT=%d\n",UID_ROOT);
  13385.     fprintf(stderr,"realuid=%d\n",realuid);
  13386.     fprintf(stderr,"privuid=%d\n",privuid);
  13387. #endif /* SUIDDEBUG */
  13388.  
  13389.     if (realgid == privgid)        /* If not running set-user-id, */
  13390.       privgid = (GID_T) -1;        /*  remember it this way. */
  13391.  
  13392.     err = priv_off();            /* Turn off setuid privilege. */
  13393.  
  13394.     if (privuid == UID_ROOT)        /* If setuid to root, */
  13395.       err |= 4;                /* return this error. */
  13396.  
  13397.     if (realuid == UID_ROOT) {        /* If real id is root, */
  13398.     privuid = (UID_T) -1;        /* stay root at all times. */
  13399. #ifdef ATT7300
  13400.     /* If Kermit installed SUID uucp and user is running as root */
  13401.     err &= ~1;            /* System V R0 does not save UID */
  13402. #endif /* ATT7300 */
  13403.     }
  13404.     return(err);
  13405. }
  13406.  
  13407.  
  13408. /* Macros for hiding the differences in UID/GID setting between various Unix
  13409.  * systems. These macros should always be called with both the privileged ID
  13410.  * and the non-privileged ID. The one in the second argument, will become the
  13411.  * effective ID. The one in the first argument will be retained for later
  13412.  * retrieval.
  13413.  */
  13414. #ifdef SETREUID
  13415. #ifdef SAVEDUID
  13416. /* On BSD systems with the saved-UID feature, we just juggle the effective
  13417.  * UID back and forth, and leave the real UID at its true value.  The kernel
  13418.  * allows switching to both the current real UID, the effective UID, and the
  13419.  * UID which the program is set-UID to.  The saved set-UID always holds the
  13420.  * privileged UID for us, and the real UID will always be the non-privileged,
  13421.  * and we can freely choose one of them for the effective UID at any time.
  13422.  */
  13423. #define switchuid(hidden,active) setreuid( (UID_T) -1, active)
  13424. #define switchgid(hidden,active) setregid( (GID_T) -1, active)
  13425.  
  13426. #else   /* SETREUID,!SAVEDUID */
  13427.  
  13428. /* On systems with setreXid() but without the saved-UID feature, notably
  13429.  * BSD 4.2, we swap the real and effective UIDs each time.  It's
  13430.  * the effective UID that we are interested in, but we have to retain the
  13431.  * unused UID somewhere to enable us to restore it later, and we do this
  13432.  * in the real UID.  The kernel only allows switching to either the current
  13433.  * real or the effective UID, unless you're "root".
  13434.  */
  13435. #define switchuid(hidden,active)    setreuid(hidden,active)
  13436. #define switchgid(hidden,active)    setregid(hidden,active)
  13437. #endif
  13438.  
  13439. #else /* !SETREUID, !SAVEDUID */
  13440.  
  13441. #ifdef SETEUID
  13442. /*
  13443.   BSD 4.4 works similarly to System V and POSIX (see below), but uses
  13444.   seteXid() instead of setXid() to change effective IDs.  In addition, the
  13445.   seteXid() functions work the same for "root" as for other users.
  13446. */
  13447. #define switchuid(hidden,active)    seteuid(active)
  13448. #define switchgid(hidden,active)    setegid(active)
  13449.  
  13450. #else /* !SETEUID */
  13451.  
  13452. /* On System V and POSIX, the only thing we can change is the effective UID
  13453.  * (unless the current effective UID is "root", but initsuid() avoids that for
  13454.  * us).  The kernel allows switching to the current real UID or to the saved
  13455.  * set-UID.  These are always set to the non-privileged UID and the privileged
  13456.  * UID, respectively, and we only change the effective UID.  This breaks if
  13457.  * the current effective UID is "root", though, because for "root" setuid/gid
  13458.  * becomes more powerful, which is why initsuid() treats "root" specially.
  13459.  * Note: That special treatment maybe could be ignored for BSD?  Note: For
  13460.  * systems that don't fit any of these four cases, we simply can't support
  13461.  * set-UID.
  13462.  */
  13463. #define switchuid(hidden,active)    setuid(active)
  13464. #define switchgid(hidden,active)    setgid(active)
  13465.  
  13466. #endif /* SETEUID */
  13467. #endif /* SETREUID */
  13468.  
  13469.  
  13470. /* P R I V _ O N  --  Turn on the setuid and/or setgid */
  13471.  
  13472. /* Go to the privileged uid (gid) that the program is set-user-id
  13473.  * (set-group-id) to, unless the program is running unprivileged.
  13474.  * If setuid() fails, return value will be 1. If getuid() fails it
  13475.  * will be 2.  Return immediately after first failure, and the function
  13476.  * tries to restore any partial work done.  Returns 0 on success.
  13477.  * Group id is changed first, since it is less serious than user id.
  13478.  */
  13479. int
  13480. priv_on() {
  13481.     if (privgid != (GID_T) -1)
  13482.       if (switchgid(realgid,privgid))
  13483.         return(2);
  13484.  
  13485.     if (privuid != (UID_T) -1)
  13486.       if (switchuid(realuid,privuid)) {
  13487.       if (privgid != (GID_T) -1)
  13488.         switchgid(privgid,realgid);
  13489.       return(1);
  13490.       }
  13491.     return(0);
  13492. }
  13493.  
  13494. /* P R I V _ O F F  --  Turn on the real uid and gid */
  13495.  
  13496. /* Return to the unprivileged uid (gid) after an temporary visit to
  13497.  * privileged status, unless the program is running without set-user-id
  13498.  * (set-group-id). Returns 1 for failure in setuid() and 2 for failure
  13499.  * in setgid() or:ed together. The functions tries to return both uid
  13500.  * and gid to unprivileged state, regardless of errors. Returns 0 on
  13501.  * success.
  13502.  */
  13503. int
  13504. priv_off() {
  13505.     int err = 0;
  13506.  
  13507.     if (privuid != (UID_T) -1)
  13508.        if (switchuid(privuid,realuid))
  13509.       err |= 1;
  13510.  
  13511.     if (privgid != (GID_T) -1)
  13512.        if (switchgid(privgid,realgid))
  13513.     err |= 2;
  13514.  
  13515.     return(err);
  13516. }
  13517.  
  13518. /* Turn off privilege permanently.  No going back.  This is necessary before
  13519.  * a fork() on BSD43 machines that don't save the setUID or setGID, because
  13520.  * we swap the real and effective ids, and we don't want to let the forked
  13521.  * process swap them again and get the privilege back. It will work on other
  13522.  * machines too, such that you can rely on its effect always being the same,
  13523.  * for instance, even when you're in priv_on() state when this is called.
  13524.  * (Well, that part about "permanent" is on System V only true if you follow
  13525.  * this with a call to exec(), but that's what we want it for anyway.)
  13526.  * Added by Dean Long -- dlong@midgard.ucsc.edu
  13527.  */
  13528. int
  13529. priv_can() {
  13530.  
  13531. #ifdef SETREUID
  13532.     int err = 0;
  13533.     if (privuid != (UID_T) -1)
  13534.        if (setreuid(realuid,realuid))
  13535.       err |= 1;
  13536.  
  13537.     if (privgid != (GID_T) -1)
  13538.         if (setregid(realgid,realgid))
  13539.        err |= 2;
  13540.  
  13541.     return(err);
  13542.  
  13543. #else
  13544. #ifdef SETEUID
  13545.     int err = 0;
  13546.     if (privuid != (UID_T) -1)
  13547.     if (setuid(realuid)) {
  13548.         debug(F101,"setuid failed","",errno);
  13549.         err |= 1;
  13550.         debug(F101,"ruid","",getuid());
  13551.         debug(F101,"euid","",geteuid());
  13552.     }
  13553.     debug(F101,"setuid","",realuid);
  13554.     if (privgid != (GID_T) -1)
  13555.         if (setgid(realgid)) {
  13556.         debug(F101,"setgid failed","",errno);
  13557.         err |= 2;
  13558.         debug(F101,"rgid","",getgid());
  13559.         debug(F101,"egid","",getegid());
  13560.     }
  13561.     debug(F101,"setgid","",realgid);
  13562.     return(err);
  13563. #else
  13564.     /* Easy way of using setuid()/setgid() instead of setreuid()/setregid().*/
  13565.     return(priv_off());
  13566. #endif /* SETEUID */
  13567. #endif /* SETREUID */
  13568. }
  13569.  
  13570. /* P R I V _ O P N  --  For opening protected files or devices. */
  13571.  
  13572. int
  13573. priv_opn(name, modes) char *name; int modes; {
  13574.     int x;
  13575.     priv_on();                /* Turn privileges on */
  13576.     debug(F111,"priv_opn",name,modes);
  13577.     errno = 0;
  13578.     x = open(name, modes);        /* Try to open the device */
  13579.     debug(F101,"priv_opn result","",x);
  13580.     debug(F101,"priv_opn errno","",errno);
  13581.     priv_off();                /* Turn privileges off */
  13582.     return(x);                /* Return open's return code */
  13583. }
  13584.  
  13585. /*  P R I V _ C H K  --  Check privileges.  */
  13586.  
  13587. /*  Try to turn them off.  If turning them off did not succeed, cancel them */
  13588.  
  13589. int
  13590. priv_chk() {
  13591.     int x, y = 0;
  13592.     x = priv_off();            /* Turn off privs. */
  13593.     if (x != 0 || getuid() == privuid || geteuid() == privuid)
  13594.       y = priv_can();
  13595.     if (x != 0 || getgid() == privgid || getegid() == privgid)
  13596.       y = y | priv_can();
  13597.     return(y);
  13598. }
  13599.  
  13600. UID_T
  13601. real_uid() {
  13602.     return(realuid);
  13603. }
  13604.  
  13605. VOID
  13606. ttimoff() {                /* Turn off any timer interrupts */
  13607.     /* int xx; */
  13608. /*
  13609.   As of 5A(183), we set SIGALRM to SIG_IGN (to ignore alarms) rather than to
  13610.   SIG_DFL (to catch alarms, or if there is no handler, to exit).  This is to
  13611.   cure (mask, really) a deeper problem with stray alarms that occurs on some
  13612.   systems, possibly having to do with sleep(), that caused core dumps.  It
  13613.   should be OK to do this, because no code in this module uses nested alarms.
  13614.   (But we still have to watch out for SCRIPT and DIAL...)
  13615. */
  13616.     /* xx = */ alarm(0);
  13617.     /* debug(F101,"ttimoff alarm","",xx); */
  13618.     if (saval) {            /* Restore any previous */
  13619.     signal(SIGALRM,saval);        /* alarm handler. */
  13620.     /* debug(F101,"ttimoff alarm restoring saval","",saval); */
  13621.     saval = NULL;
  13622.     } else {
  13623.     signal(SIGALRM,SIG_IGN);    /* Used to be SIG_DFL */
  13624.     /* debug(F100,"ttimoff alarm SIG_IGN","",0); */
  13625.     }
  13626. }
  13627.  
  13628. /* T T R U N C M D  --  Redirect an external command over the connection. */
  13629.  
  13630. #ifdef CK_REDIR
  13631. int
  13632. ttruncmd(s) char *s; {
  13633.     PID_T pid;                /* pid of lower fork */
  13634.     int wstat;                /* for wait() */
  13635.     int x;
  13636.     int statusp;
  13637.  
  13638.     if (ttyfd == -1) {
  13639.     printf("?Sorry, device is not open\n");
  13640.     return(0);
  13641.     }
  13642.     if (nopush) {
  13643.     debug(F100,"ttruncmd fail: nopush","",0);
  13644.     return(0);
  13645.     }
  13646.     conres();                /* Make console normal  */
  13647.     pexitstat = -4;
  13648.     if ((pid = fork()) == 0) {        /* Make a child fork */
  13649.     if (priv_can())            /* Child: turn off privs. */
  13650.       exit(1);
  13651.     dup2(ttyfd, 0);            /* Give stdin/out to the line */
  13652.     dup2(ttyfd, 1);
  13653.     x = system(s);
  13654.     debug(F101,"ttruncmd system",s,x);
  13655.     _exit(x ? BAD_EXIT : 0);
  13656.     } else {
  13657.     SIGTYP (*istat)(), (*qstat)();
  13658.     if (pid == (PID_T) -1)        /* fork() failed? */
  13659.       return(0);
  13660.     istat = signal(SIGINT,SIG_IGN); /* Let the fork handle keyboard */
  13661.     qstat = signal(SIGQUIT,SIG_IGN); /* interrupts itself... */
  13662.  
  13663. #ifdef COMMENT
  13664.         while (((wstat = wait(&statusp)) != pid) && (wstat != -1)) ;
  13665. #else  /* Not COMMENT */
  13666.         while (1) {
  13667.         wstat = wait(&statusp);
  13668.         debug(F101,"ttruncmd wait","",wstat);
  13669.         if (wstat == pid || wstat == -1)
  13670.           break;
  13671.     }
  13672. #endif /* COMMENT */
  13673.  
  13674.     pexitstat = (statusp & 0xff) ? statusp : statusp >> 8;
  13675.     debug(F101,"ttruncmd wait statusp","",statusp);
  13676.     debug(F101,"ttruncmd wait pexitstat","",pexitstat);
  13677.     signal(SIGINT,istat);        /* Restore interrupts */
  13678.     signal(SIGQUIT,qstat);
  13679.     }
  13680.     concb((char)escchr);        /* Restore console to CBREAK mode */
  13681.     return(statusp == 0 ? 1 : 0);
  13682. }
  13683. #endif /* CK_REDIR */
  13684.  
  13685. struct tm *
  13686. #ifdef CK_ANSIC
  13687. cmdate2tm(char * date, int gmt)         /* date as "yyyymmdd hh:mm:ss" */
  13688. #else
  13689. cmdate2tm(date,gmt) char * date; int gmt;
  13690. #endif
  13691. {
  13692.     /* date as "yyyymmdd hh:mm:ss" */
  13693.     static struct tm _tm;
  13694.     time_t now;
  13695.  
  13696.     if (strlen(date) != 17 ||
  13697.     date[8] != ' ' ||
  13698.     date[11] != ':' ||
  13699.     date[14] != ':')
  13700.       return(NULL);
  13701.  
  13702.     time(&now);
  13703.     if (gmt)
  13704.       _tm = *gmtime(&now);
  13705.     else
  13706.       _tm = *localtime(&now);
  13707.     _tm.tm_year = (date[0]-'0')*1000 + (date[1]-'0')*100 +
  13708.                   (date[2]-'0')*10   + (date[3]-'0')-1900;
  13709.     _tm.tm_mon  = (date[4]-'0')*10   + (date[5]-'0')-1;
  13710.     _tm.tm_mday = (date[6]-'0')*10   + (date[7]-'0');
  13711.     _tm.tm_hour = (date[9]-'0')*10   + (date[10]-'0');
  13712.     _tm.tm_min  = (date[12]-'0')*10  + (date[13]-'0');
  13713.     _tm.tm_sec  = (date[15]-'0')*10  + (date[16]-'0');
  13714.  
  13715.     /* Should we set _tm.tm_isdst to -1 here? */
  13716.  
  13717.     _tm.tm_wday = 0;
  13718.     _tm.tm_yday = 0;
  13719.  
  13720.     return(&_tm);
  13721. }
  13722.  
  13723. #ifdef OXOS
  13724. #undef kill
  13725. #endif /* OXOS */
  13726.  
  13727. #ifdef OXOS
  13728. int
  13729. priv_kill(pid, sig) int pid, sig; {
  13730.     int    i;
  13731.  
  13732.     if (priv_on())
  13733.     debug(F100,"priv_kill priv_on failed","",0);
  13734.     i = kill(pid, sig);
  13735.     if (priv_off())
  13736.     debug(F100,"priv_kill priv_off failed","",0);
  13737.     return(i);
  13738. }
  13739. #endif /* OXOS */
  13740.  
  13741. #ifdef BEOSORBEBOX
  13742. /* #ifdef BE_DR_7 */
  13743. /*
  13744.   alarm() function not supplied with Be OS DR7 - this one contributed by
  13745.   Neal P. Murphy.
  13746. */
  13747.  
  13748. /*
  13749.   This should mimic the UNIX/POSIX alarm() function well enough, with the
  13750.   caveat that one's SIGALRM handler must call alarm_expired() to clean up vars
  13751.   and wait for the alarm thread to finish.
  13752. */
  13753. unsigned int
  13754. alarm(unsigned int seconds) {
  13755.     long time_left = 0;
  13756.  
  13757. /* If an alarm is active, turn it off, saving the unused time */
  13758.     if (alarm_thread != -1) {
  13759.         /* We'll be generous and count partial seconds as whole seconds. */
  13760.         time_left = alarm_struct.time -
  13761.       ((system_time() - time_started) / 1000000.0);
  13762.  
  13763.         /* Kill the alarm thread */
  13764.         kill_thread (alarm_thread);
  13765.  
  13766.         /* We need to clean up as though the alarm occured. */
  13767.         time_started = 0;
  13768.         alarm_struct.thread = -1;
  13769.         alarm_struct.time = 0;
  13770.         alarm_expired();
  13771.     }
  13772.  
  13773. /* Set a new alarm clock, if requested. */
  13774.     if (seconds > 0) {
  13775.         alarm_struct.thread = find_thread(NULL);
  13776.         alarm_struct.time = seconds;
  13777.         time_started = system_time();
  13778.         alarm_thread = spawn_thread (do_alarm,
  13779.                                      "alarm_thread",
  13780.                                      B_NORMAL_PRIORITY,
  13781.                                      (void *) &alarm_struct
  13782.                      );
  13783.         resume_thread (alarm_thread);
  13784.     }
  13785.  
  13786. /* Now return [unused time | 0] */
  13787.     return ((unsigned int) time_left);
  13788. }
  13789.  
  13790. /*
  13791.   This function is the departure from UNIX/POSIX alarm handling. In the case
  13792.   of Be's missing alarm() function, this stuff needs to be done in the SIGALRM
  13793.   handler. When Be implements alarm(), this function call can be eliminated
  13794.   from user's SIGALRM signal handlers.
  13795. */
  13796.  
  13797. void
  13798. alarm_expired(void) {
  13799.     long ret_val;
  13800.  
  13801.     if (alarm_thread != -1) {
  13802.         wait_for_thread (alarm_thread, &ret_val);
  13803.         alarm_thread = -1;
  13804.     }
  13805. }
  13806.  
  13807. /*
  13808.   This is the function that snoozes the requisite number of seconds and then
  13809.   SIGALRMs the calling thread. Note that kill() wants a pid_t arg, whilst Be
  13810.   uses thread_id; currently they are both typdef'ed as long, but I'll do the
  13811.   cast anyway. This function is run in a separate thread.
  13812. */
  13813.  
  13814. long
  13815. do_alarm (void *alarm_struct) {
  13816.     snooze ((double) ((struct ALARM_STRUCT *) alarm_struct)->time * 1000000.0);
  13817.     kill ((pid_t)((struct ALARM_STRUCT *) alarm_struct)->thread, SIGALRM);
  13818.     time_started = 0;
  13819.     ((struct ALARM_STRUCT *) alarm_struct)->thread = -1;
  13820.     ((struct ALARM_STRUCT *) alarm_struct)->time = 0;
  13821. }
  13822. /* #endif */ /* BE_DR_7 */
  13823. #endif /* BEOSORBEBOX */
  13824.  
  13825. #ifdef Plan9
  13826.  
  13827. int
  13828. p9ttyctl(char letter, int num, int param) {
  13829.     char cmd[20];
  13830.     int len;
  13831.  
  13832.     if (ttyctlfd < 0)
  13833.       return -1;
  13834.  
  13835.     cmd[0] = letter;
  13836.     if (num)
  13837.       len = sprintf(cmd + 1, "%d", param) + 1;
  13838.     else {
  13839.     cmd[1] = param;
  13840.     len = 2;
  13841.     }
  13842.     if (write(ttyctlfd, cmd, len) == len) {
  13843.     cmd[len] = 0;
  13844.     /* fprintf(stdout, "wrote '%s'\n", cmd); */
  13845.     return 0;
  13846.     }
  13847.     return -1;
  13848. }
  13849.  
  13850. int
  13851. p9ttyparity(char l) {
  13852.     return p9ttyctl('p', 0, l);
  13853. }
  13854.  
  13855. int
  13856. p9tthflow(int flow, int status) {
  13857.     return p9ttyctl('m', 1, status);
  13858. }
  13859.  
  13860. int
  13861. p9ttsspd(int cps) {
  13862.     if (p9ttyctl('b', 1, cps * 10) < 0)
  13863.       return -1;
  13864.     ttylastspeed = cps * 10;
  13865.     return 0;
  13866. }
  13867.  
  13868. int
  13869. p9openttyctl(char *ttname) {
  13870.     char name[100];
  13871.  
  13872.     if (ttyctlfd >= 0) {
  13873.     close(ttyctlfd);
  13874.     ttyctlfd = -1;
  13875.     ttylastspeed = -1;
  13876.     }
  13877.     sprintf(name, "%sctl", ttname);
  13878.     ttyctlfd = open(name, 1);
  13879.     return ttyctlfd;
  13880. }
  13881.  
  13882. int
  13883. p9concb() {
  13884.     if (consctlfd >= 0) {
  13885.     if (write(consctlfd, "rawon", 5) == 5)
  13886.       return 0;
  13887.     }
  13888.     return -1;
  13889. }
  13890.  
  13891. int
  13892. p9conbin() {
  13893.     return p9concb();
  13894. }
  13895.  
  13896. int
  13897. p9conres() {
  13898.     if (consctlfd >= 0) {
  13899.     if (write(consctlfd, "rawoff", 6) == 6)
  13900.       return 0;
  13901.     }
  13902.     return -1;
  13903. }
  13904.  
  13905. int
  13906. p9sndbrk(int msec) {
  13907.     if (ttyctlfd >= 0) {
  13908.     char cmd[20];
  13909.     int i = sprintf(cmd, "k%d", msec);
  13910.     if (write(ttyctlfd, cmd, i) == i)
  13911.       return 0;
  13912.     }
  13913.     return -1;
  13914. }
  13915.  
  13916. int
  13917. conwrite(char *buf, int n) {
  13918.     int x;
  13919.     static int length = 0;
  13920.     static int holdingcr = 0;
  13921.     int normal = 0;
  13922.     for (x = 0; x < n; x++) {
  13923.     char c = buf[x];
  13924.     if (c == 007) {
  13925.         if (normal) {
  13926.         write(1, buf + (x - normal), normal);
  13927.         length += normal;
  13928.         normal = 0;
  13929.         }
  13930.         /* write(noisefd, "1000 300", 8); */
  13931.         holdingcr = 0;
  13932.     } else if (c == '\r') {
  13933.         if (normal) {
  13934.         write(1, buf + (x - normal), normal);
  13935.         length += normal;
  13936.         normal = 0;
  13937.         }
  13938.         holdingcr = 1;
  13939.     } else if (c == '\n') {
  13940.         write(1, buf + (x - normal), normal + 1);
  13941.         normal = 0;
  13942.         length = 0;
  13943.         holdingcr = 0;
  13944.     } else if (c == '\b') {
  13945.         if (normal) {
  13946.         write(1, buf + (x - normal), normal);
  13947.         length += normal;
  13948.         normal = 0;
  13949.         }
  13950.         if (length) {
  13951.         write(1, &c, 1);
  13952.         length--;
  13953.         }
  13954.         holdingcr = 0;
  13955.     } else {
  13956.         if (holdingcr) {
  13957.         char b = '\b';
  13958.         while (length-- > 0)
  13959.           write(1, &b, 1);
  13960.         length = 0;    /* compiler bug */
  13961.         }
  13962.         holdingcr = 0;
  13963.         normal++;
  13964.     }
  13965.     }
  13966.     if (normal) {
  13967.     write(1, buf + (x - normal), normal);
  13968.     length += normal;
  13969.     }
  13970.     return n;
  13971. }
  13972.  
  13973. void
  13974. conprint(char *fmt, ...) {
  13975.     static char buf[1000];        /* not safe if on the stack */
  13976.  
  13977.     va_list ap;
  13978.     int i;
  13979.  
  13980.     va_start(ap, fmt);
  13981.     i = vsprintf(buf, fmt, ap);
  13982.     conwrite(buf, i);
  13983. }
  13984. #endif /* Plan9 */
  13985.  
  13986. /* fprintf, printf, perror replacements... */
  13987.  
  13988. /* f p r i n t f */
  13989.  
  13990. #ifdef UNIX
  13991. #ifdef CK_ANSIC
  13992. #include <stdarg.h>
  13993. #else /* CK_ANSIC */
  13994. #include <varargs.h>
  13995. #endif /* CK_ANSIC */
  13996. #ifdef fprintf
  13997. #undef fprintf
  13998. static char str1[4096];
  13999. static char str2[4096];
  14000. int
  14001. #ifdef CK_ANSIC
  14002. ckxfprintf(FILE * file, const char * format, ...)
  14003. #else /* CK_ANSIC */
  14004. ckxfprintf(va_alist) va_dcl
  14005. #endif /* CK_ANSIC */
  14006. /* ckxfprintf */ {
  14007.     int i, j, len, got_cr;
  14008.     va_list args;
  14009.     int rc = 0;
  14010.  
  14011. #ifdef CK_ANSIC
  14012.     va_start(args, format);
  14013. #else /* CK_ANSIC */
  14014.     char * format;
  14015.     FILE * file;
  14016.     va_start(args);
  14017.     file = va_arg(args,FILE *);
  14018.     format = va_arg(args,char *);
  14019. #endif /* CK_ANSIC */
  14020.  
  14021.     if (!inserver || (file != stdout && file != stderr && file != stdin)) {
  14022.     rc = vfprintf(file,format,args);
  14023.     } else {
  14024.     unsigned int c;
  14025.         rc = vsprintf(str1, format, args);
  14026.         len = strlen(str1);
  14027.         if (len >= sizeof(str1)) {
  14028.             debug(F101,"ckxfprintf() buffer overflow","",len);
  14029.             doexit(BAD_EXIT,1);
  14030.         }
  14031.         for (i = 0, j = 0, got_cr = 0;
  14032.          i < len && j < sizeof(str1)-2;
  14033.          i++, j++ ) {
  14034.         /* We can't use 255 as a case label because of signed chars */
  14035.         c = (unsigned)(str1[i] & 0xff);
  14036. #ifdef TNCODE
  14037.         if (c == 255) {
  14038.         if (got_cr && !TELOPT_ME(TELOPT_BINARY))
  14039.           str2[j++] = '\0';
  14040.         str2[j++] = IAC;
  14041.         str2[j] = IAC;
  14042.         got_cr = 0;
  14043.         } else
  14044. #endif /* TNCODE */
  14045.         switch (c) {
  14046.           case '\r':
  14047.                 if (got_cr
  14048. #ifdef TNCODE
  14049.             && !TELOPT_ME(TELOPT_BINARY)
  14050. #endif /* TNCODE */
  14051.             )
  14052.           str2[j++] = '\0';
  14053.                 str2[j] = str1[i];
  14054.                 got_cr = 1;
  14055.                 break;
  14056.           case '\n':
  14057.                 if (!got_cr)
  14058.           str2[j++] = '\r';
  14059.                 str2[j] = str1[i];
  14060.                 got_cr = 0;
  14061.                 break;
  14062.           default:
  14063.                 if (got_cr
  14064. #ifdef TNCODE
  14065.             && !TELOPT_ME(TELOPT_BINARY)
  14066. #endif /* TNCODE */
  14067.             )
  14068.           str2[j++] = '\0';
  14069.                 str2[j] = str1[i];
  14070.                 got_cr = 0;
  14071.             }
  14072.         }
  14073.         if (got_cr
  14074. #ifdef TNCODE
  14075.              && !TELOPT_ME(TELOPT_BINARY)
  14076. #endif /* TNCODE */
  14077.              )
  14078.             str2[j++] = '\0';
  14079. #ifdef CK_ENCRYPTION
  14080. #ifdef TNCODE
  14081.         if (TELOPT_ME(TELOPT_ENCRYPTION))
  14082.       ck_tn_encrypt(str2,j);
  14083. #endif /* TNCODE */
  14084. #endif /* CK_ENCRYPTION */
  14085. #ifdef CK_SSL
  14086.     if (inserver && (ssl_active_flag || tls_active_flag)) {
  14087.         /* Write using SSL */
  14088.             char * p = str2;
  14089.           ssl_retry:
  14090.             if (ssl_active_flag)
  14091.           rc = SSL_write(ssl_con, p, j);
  14092.             else
  14093.           rc = SSL_write(tls_con, p, j);
  14094.         debug(F111,"ckxfprintf","SSL_write",rc);
  14095.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,rc)) {
  14096.           case SSL_ERROR_NONE:
  14097.                 if (rc == j)
  14098.           break;
  14099.                 p += rc;
  14100.                 j -= rc;
  14101.                 goto ssl_retry;
  14102.           case SSL_ERROR_WANT_WRITE:
  14103.           case SSL_ERROR_WANT_READ:
  14104.           case SSL_ERROR_SYSCALL:
  14105.                 if (rc != 0)
  14106.           return(-1);
  14107.           case SSL_ERROR_WANT_X509_LOOKUP:
  14108.           case SSL_ERROR_SSL:
  14109.           case SSL_ERROR_ZERO_RETURN:
  14110.           default:
  14111.                 rc = 0;
  14112.             }
  14113.     } else
  14114. #endif /* CK_SSL */
  14115.         fwrite(str2,sizeof(char),j,stdout);
  14116.     }
  14117.     va_end(args);
  14118.     return(rc);
  14119. }
  14120. #endif /* fprintf */
  14121.  
  14122. /* p r i n t f */
  14123.  
  14124. #ifdef printf
  14125. #undef printf
  14126. int
  14127. #ifdef CK_ANSIC
  14128. ckxprintf(const char * format, ...)
  14129. #else /* CK_ANSIC */
  14130. ckxprintf(va_alist) va_dcl
  14131. #endif /* CK_ANSIC */
  14132. /* ckxprintf */ {
  14133.     int i, j, len, got_cr;
  14134.     va_list args;
  14135.     int rc = 0;
  14136.  
  14137. #ifdef CK_ANSIC
  14138.     va_start(args, format);
  14139. #else /* CK_ANSIC */
  14140.     char * format;
  14141.     va_start(args);
  14142.     format = va_arg(args,char *);
  14143. #endif /* CK_ANSIC */
  14144.  
  14145.     if (!inserver) {
  14146.     rc = vprintf(format, args);
  14147.     } else {
  14148.     unsigned int c;
  14149.         rc = vsprintf(str1, format, args);
  14150.         len = strlen(str1);
  14151.         if (len >= sizeof(str1)) {
  14152.             debug(F101,"ckxprintf() buffer overflow","",len);
  14153.             doexit(BAD_EXIT,1);
  14154.         }
  14155.         for (i = 0, j = 0, got_cr=0;
  14156.          i < len && j < sizeof(str1)-2;
  14157.          i++, j++ ) {
  14158.         c = (unsigned)(str1[i] & 0xff);
  14159. #ifdef TNCODE
  14160.         if (c == 255) {
  14161.         if (got_cr && !TELOPT_ME(TELOPT_BINARY))
  14162.           str2[j++] = '\0';
  14163.         str2[j++] = IAC;
  14164.         str2[j] = IAC;
  14165.         got_cr = 0;
  14166.         } else
  14167. #endif /* TNCODE */
  14168.         switch (c) {
  14169.           case '\r':
  14170.                 if (got_cr
  14171. #ifdef TNCODE
  14172.             && !TELOPT_ME(TELOPT_BINARY)
  14173. #endif /* TNCODE */
  14174.             )
  14175.           str2[j++] = '\0';
  14176.                 str2[j] = str1[i];
  14177.                 got_cr = 1;
  14178.                 break;
  14179.           case '\n':
  14180.                 if (!got_cr)
  14181.           str2[j++] = '\r';
  14182.                 str2[j] = str1[i];
  14183.                 got_cr = 0;
  14184.                 break;
  14185.           default:
  14186.                 if (got_cr
  14187. #ifdef TNCODE
  14188.             && !TELOPT_ME(TELOPT_BINARY)
  14189. #endif /* TNCODE */
  14190.             )
  14191.           str2[j++] = '\0';
  14192.                 str2[j] = str1[i];
  14193.                 got_cr = 0;
  14194.                 break;
  14195.         }
  14196.         }
  14197.         if (got_cr
  14198. #ifdef TNCODE
  14199.              && !TELOPT_ME(TELOPT_BINARY)
  14200. #endif /* TNCODE */
  14201.              )
  14202.             str2[j++] = '\0';
  14203. #ifdef CK_ENCRYPTION
  14204. #ifdef TNCODE
  14205.         if (TELOPT_ME(TELOPT_ENCRYPTION))
  14206.       ck_tn_encrypt(str2,j);
  14207. #endif /* TNCODE */
  14208. #endif /* CK_ENCRYPTION */
  14209. #ifdef CK_SSL
  14210.     if (inserver && (ssl_active_flag || tls_active_flag)) {
  14211.             char * p = str2;
  14212.         /* Write using SSL */
  14213.           ssl_retry:
  14214.             if (ssl_active_flag)
  14215.           rc = SSL_write(ssl_con, p, j);
  14216.             else
  14217.           rc = SSL_write(tls_con, p, j);
  14218.         debug(F111,"ckxprintf","SSL_write",rc);
  14219.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,rc)) {
  14220.           case SSL_ERROR_NONE:
  14221.                 if (rc == j)
  14222.           break;
  14223.                 p += rc;
  14224.                 j -= rc;
  14225.                 goto ssl_retry;
  14226.           case SSL_ERROR_WANT_WRITE:
  14227.           case SSL_ERROR_WANT_READ:
  14228.           case SSL_ERROR_SYSCALL:
  14229.                 if (rc != 0)
  14230.           return(-1);
  14231.           case SSL_ERROR_WANT_X509_LOOKUP:
  14232.           case SSL_ERROR_SSL:
  14233.           case SSL_ERROR_ZERO_RETURN:
  14234.           default:
  14235.                 rc = 0;
  14236.             }
  14237.     } else
  14238. #endif /* CK_SSL */
  14239.       rc = fwrite(str2,sizeof(char),j,stdout);
  14240.     }
  14241.     va_end(args);
  14242.     return(rc);
  14243. }
  14244. #endif /* printf */
  14245.  
  14246. /*  p e r r o r  */
  14247.  
  14248. #ifdef perror
  14249. #undef perror
  14250. _PROTOTYP(char * ck_errstr,(VOID));
  14251. #ifdef NEXT
  14252. void
  14253. #else
  14254. #ifdef CK_SCOV5
  14255. void
  14256. #else
  14257. int
  14258. #endif /* CK_SCOV5 */
  14259. #endif /* NEXT */
  14260. #ifdef CK_ANSIC
  14261. ckxperror(const char * str)
  14262. #else /* CK_ANSIC */
  14263. ckxperror(str) char * str;
  14264. #endif /* CK_ANSIC */
  14265. /* ckxperror */ {
  14266.     char * errstr = ck_errstr();
  14267. #ifndef NEXT
  14268. #ifndef CK_SCOV5
  14269.     return
  14270. #endif /* CK_SCOV5 */
  14271. #endif /* NEXT */
  14272.       ckxprintf("%s%s %s\n",str,*errstr?":":"",errstr);
  14273. }
  14274. #endif /* perror */
  14275. #endif /* UNIX */
  14276.  
  14277. #ifdef MINIX2
  14278.  
  14279. /* Minix doesn't have a gettimeofday call. We fake one here using time(2) */
  14280.  
  14281. int
  14282. gettimeofday(struct timeval *tp, struct timezone *tzp) {
  14283.     tp->tv_usec = 0L;            /* Close enough for horseshoes */
  14284.     if(time(&(tp->tv_sec))==-1)
  14285.       return(-1);
  14286.     return(0);
  14287. }
  14288.  
  14289. /* Minix does not support symbolic links. We implement a version of
  14290.    readlink that always fails */
  14291.  
  14292. int
  14293. readlink(const char *path, void *buf, size_t bufsiz) {
  14294.     errno = ENOSYS;
  14295.     return(-1);
  14296. }
  14297. #endif /* MINIX2 */
  14298.