home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku206.zip / ckutio.c < prev    next >
C/C++ Source or Header  |  2002-10-18  |  424KB  |  14,468 lines

  1. #ifdef aegis
  2. char *ckxv = "Aegis Communications support, 8.0.301, 20 Aug 2002";
  3. #else
  4. #ifdef Plan9
  5. char *ckxv = "Plan 9 Communications support, 8.0.301, 20 Aug 2002";
  6. #else
  7. char *ckxv = "UNIX Communications support, 8.0.301, 20 Aug 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. static int tt_xpixel = 0, tt_ypixel = 0;
  1734.  
  1735. int
  1736. ttgwsiz() {
  1737.     int x = 0;
  1738. #ifndef NONAWS
  1739. #ifdef QNX
  1740. /*
  1741.   NOTE: TIOCGWSIZ works here too, but only in the 32-bit version.
  1742.   This code works for both the 16- and 32-bit versions.
  1743. */
  1744.     extern int dev_size(int, int, int, int *, int *);
  1745.     int r, c;
  1746.  
  1747.     if (dev_size(0, -1, -1, &r, &c) == 0) {
  1748.     debug(F101,"ttgwsiz QNX r","",r);
  1749.     debug(F101,"ttgwsiz QNX c","",c);
  1750.     tt_rows = r;
  1751.     tt_cols = c;
  1752.     return ((r > 0 && c > 0) ? 1 : 0);
  1753.     } else return(xttgwsiz());
  1754. #else /* QNX */
  1755. #ifdef TIOCGWINSZ
  1756.  
  1757. /* Note, this was M_UNIX, changed to XENIX to allow cross compilation... */
  1758. #ifdef XENIX                /* SCO UNIX 3.2v4.0 */
  1759. #include <sys/stream.h>            /* typedef mblk_t needed by ptem.h */
  1760. #include <sys/ptem.h>            /* for ttgwsiz() */
  1761. #endif /* XENIX */
  1762.  
  1763. #ifdef I386IX                /* Ditto for Interactive */
  1764. #include <sys/stream.h>
  1765. #include <sys/ptem.h>
  1766. #endif /* I386IX */
  1767.  
  1768. /* Note, the above might be needed for some other older SVR3 Intel makes... */
  1769.  
  1770.     struct winsize w;
  1771.     tt_xpixel = 0;
  1772.     tt_ypixel = 0;
  1773.  
  1774. #ifdef IKSD
  1775.     if (inserver)
  1776.       return(xttgwsiz());
  1777. #endif /* IKSD */
  1778.     x = ioctl(0, (int)TIOCGWINSZ, (char *)&w);
  1779.     debug(F101,"ttgwsiz TIOCGWINSZ","",x);
  1780.     if (x < 0) {
  1781.     return(xttgwsiz());
  1782.     } else if (w.ws_row > 0 && w.ws_col > 0) {
  1783.     tt_rows = w.ws_row;
  1784.     tt_cols = w.ws_col;
  1785.     tt_xpixel = w.ws_xpixel;
  1786.     tt_ypixel = w.ws_ypixel;
  1787.     debug(F101,"ttgwsiz tt_rows","",tt_rows);
  1788.     debug(F101,"ttgwsiz tt_cols","",tt_cols);
  1789.     return(1);
  1790.     } else {
  1791.     debug(F100,"ttgwsiz TIOCGWINSZ 00","",0);
  1792.     return(xttgwsiz());
  1793.     }
  1794. #else
  1795.     return(xttgwsiz());
  1796. #endif /* TIOCGWINSZ */
  1797. #endif /* QNX */
  1798. #endif /* NONAWS */
  1799. }
  1800.  
  1801.  
  1802. #ifndef NOSIGWINCH
  1803. #ifdef SIGWINCH
  1804. SIGTYP
  1805. winchh(foo) int foo; {            /* SIGWINCH handler */
  1806.     int x = 0;
  1807. #ifdef NETPTY
  1808.     extern int pty_fork_pid;
  1809. #endif /* NETPTY */
  1810. #ifdef CK_TTYFD
  1811. #ifndef VMS
  1812.     extern int ttyfd;
  1813. #endif /* VMS */
  1814. #endif /* CK_TTYFD */
  1815.     extern int tt_rows, tt_cols, cmd_rows, cmd_cols;
  1816. #ifdef DEBUG
  1817.     if (deblog) {
  1818.     debug(F100,"***************","",0);
  1819.     debug(F100,"SIGWINCH caught","",0);
  1820.     debug(F100,"***************","",0);
  1821. #ifdef NETPTY
  1822.     debug(F101,"SIGWINCH pty_fork_pid","",pty_fork_pid);
  1823. #endif /* NETPTY */
  1824.     }
  1825. #endif /* DEUB */
  1826.     signal(SIGWINCH,winchh);            /* Re-arm the signal */
  1827.     x = ttgwsiz();                      /* Get new window size */
  1828.     cmd_rows = tt_rows;            /* Adjust command screen too */
  1829.     cmd_cols = tt_cols;
  1830.  
  1831. #ifdef CK_TTYFD
  1832.     if                    /* If we don't have a connection */
  1833. #ifdef VMS                /* we're done. */
  1834.       (vmsttyfd() == -1)
  1835. #else
  1836.       (ttyfd == -1)
  1837. #endif /* VMS */
  1838. #else
  1839.       (!local)
  1840. #endif /* CK_TTYFD */
  1841.         return;
  1842.  
  1843. #ifdef NETPTY
  1844.     if (pty_fork_pid > -1) {        /* "set host" to a PTY? */
  1845.     int x;
  1846.  
  1847. #ifdef TIOCSWINSZ
  1848.     struct winsize w;        /* Resize the PTY */
  1849.     errno = 0;
  1850.     w.ws_col = tt_cols;
  1851.     w.ws_row = tt_rows;
  1852.     w.ws_xpixel = tt_xpixel;
  1853.     w.ws_ypixel = tt_ypixel;
  1854.     x = ioctl(ttyfd,TIOCSWINSZ,&w);
  1855.     debug(F101,"winchh TIOCSWINSZ","",x);
  1856.     debug(F101,"winchh TIOCSWINSZ errno","",errno);
  1857. #endif /* TIOCSWINSZ */
  1858.  
  1859.     errno = 0;
  1860.     x = kill(pty_fork_pid,SIGWINCH);
  1861.     debug(F101,"winchh kill","",x);
  1862.     debug(F101,"winchh kill errno","",errno);
  1863.     }
  1864. #endif /* NETPTY */
  1865.  
  1866. /*
  1867.   This should be OK.  It might seem that sending this from
  1868.   interrupt level could interfere with another TELNET IAC string
  1869.   that was in the process of being sent.  But we always send
  1870.   TELNET strings with a single write(), which should prevent mixups.
  1871.   blah_snaws() should protect themselves from being called on the
  1872.   wrong kind of connection.
  1873. */
  1874. #ifdef TCPSOCKET
  1875. #ifndef NOTTGWSIZ
  1876.     if (x > 0 && tt_rows > 0 && tt_cols > 0) {
  1877.         tn_snaws();
  1878. #ifdef RLOGCODE
  1879.         rlog_naws();
  1880. #endif /* RLOGCODE */
  1881.     }
  1882. #endif /* NOTTGWSIZ */
  1883. #endif /* TCPSOCKET */
  1884.     SIGRETURN;
  1885. }
  1886. #endif /* SIGWINCH */
  1887. #endif /* NOSIGWINCH */
  1888.  
  1889. SIGTYP
  1890. sighup(foo) int foo; {            /* SIGHUP handler */
  1891.     backgrd = 1;
  1892.     debug(F100,"***************","",0);
  1893.     debug(F100,"SIGHUP received","",0);
  1894.     debug(F100,"***************","",0);
  1895.     doexit(BAD_EXIT,-1);
  1896.     /*NOTREACHED*/
  1897.     SIGRETURN;                /* Shut picky compilers up... */
  1898. }
  1899.  
  1900. #ifdef CK_SCO32V4
  1901. /* Exists but there is no prototype in the header files */
  1902. _PROTOTYP( char * ttyname, (int) );
  1903. #else
  1904. #ifdef SV68R3V6
  1905. _PROTOTYP( char * ttyname, (int) );
  1906. #else
  1907. #ifdef ultrix
  1908. _PROTOTYP( char * ttyname, (int) );
  1909. #else
  1910. #ifdef HPUX6
  1911. _PROTOTYP( char * ttyname, (int) );
  1912. #else
  1913. #ifdef HPUX5
  1914. _PROTOTYP( char * ttyname, (int) );
  1915. #else
  1916. #ifdef PS2AIX10
  1917. _PROTOTYP( char * ttyname, (int) );
  1918. #else
  1919. #ifdef BSD42
  1920. _PROTOTYP( char * ttyname, (int) );
  1921. #endif /* BSD42 */
  1922. #endif /* PS2AIX10 */
  1923. #endif /* HPUX5 */
  1924. #endif /* HPUX6 */
  1925. #endif /* ultrix */
  1926. #endif /* SV68R3V6 */
  1927. #endif /* CK_SCO32V4 */
  1928.  
  1929. #ifndef SIGUSR1                /* User-defined signals */
  1930. #define SIGUSR1 30
  1931. #endif /* SIGUSR1 */
  1932.  
  1933. #ifndef SIGUSR2
  1934. #define SIGUSR2 31
  1935. #endif /* SIGUSR2 */
  1936.  
  1937. /*
  1938.   ignorsigs() sets certain signals to SIG_IGN.  But when a signal is
  1939.   ignored, it remains ignored across exec(), so we have to restore these
  1940.   signals before exec(), which is the purpose of restorsigs().
  1941. */
  1942. static VOID
  1943. ignorsigs() {                /* Ignore these signals */
  1944.     savquit = signal(SIGQUIT,SIG_IGN);    /* Ignore Quit signal */
  1945.  
  1946. #ifdef SIGDANGER            /* Ignore danger signals */
  1947. /*
  1948.   This signal is sent when the system is low on swap space.  Processes
  1949.   that don't handle it are candidates for termination.  If swap space doesn't
  1950.   clear out enough, we still might be terminated via kill() -- nothing we can
  1951.   do about that!  Conceivably, this could be improved by installing a real
  1952.   signal handler that warns the user, but that would be pretty complicated,
  1953.   since we are not always in control of the screen -- e.g. during remote-mode
  1954.   file transfer.
  1955. */
  1956.     savdanger = signal(SIGDANGER,SIG_IGN); /* e.g. in AIX */
  1957. #endif /* SIGDANGER */
  1958. #ifdef SIGPIPE
  1959. /*
  1960.   This one comes when a TCP/IP connection is broken by the remote.
  1961.   We prefer to catch this situation by examining error codes from write().
  1962. */
  1963.     savpipe = signal(SIGPIPE,SIG_IGN);
  1964. #endif /* SIGPIPE */
  1965.     savusr1 = signal(SIGUSR1,SIG_IGN);    /* Ignore user-defined signals */
  1966.     savusr2 = signal(SIGUSR2,SIG_IGN);
  1967. }
  1968.  
  1969. VOID
  1970. restorsigs() {                /* Restore these signals */
  1971.     (VOID) signal(SIGQUIT,savquit);    /* (used in ckufio.c) */
  1972. #ifdef SIGDANGER
  1973.     (VOID) signal(SIGDANGER,savdanger);
  1974. #endif /* SIGDANGER */
  1975. #ifdef SIGPIPE
  1976.     (VOID) signal(SIGPIPE,savpipe);
  1977. #endif /* SIGPIPE */
  1978.     (VOID) signal(SIGUSR1,savusr1);
  1979.     (VOID) signal(SIGUSR2,savusr2);
  1980. }
  1981.  
  1982. int
  1983. sysinit() {
  1984.     int x;
  1985.     char * s;
  1986. #ifdef CK_UTSNAME
  1987.     struct utsname name;
  1988. #endif /* CK_UTSNAME */
  1989.  
  1990.     extern char startupdir[];
  1991. /*
  1992.   BEFORE ANYTHING ELSE: Initialize the setuid package.
  1993.   Change to the user's real user and group ID.
  1994.   If this can't be done, don't run at all.
  1995. */
  1996.     x = priv_ini();
  1997. #ifdef SUIDDEBUG
  1998.     fprintf(stderr,"PRIV_INI=%d\n",x);
  1999. #endif /* SUIDDEBUG */
  2000.     if (x) {
  2001.     if (x & 1) fprintf(stderr,"Fatal: setuid failure.\n");
  2002.     if (x & 2) fprintf(stderr,"Fatal: setgid failure.\n");
  2003.     if (x & 4) fprintf(stderr,"Fatal: C-Kermit setuid to root!\n");
  2004.     exit(1);
  2005.     }
  2006.     signal(SIGINT,SIG_IGN);        /* Ignore interrupts at first */
  2007.     signal(SIGFPE,SIG_IGN);        /* Ignore floating-point exceptions */
  2008.     signal(SIGHUP,sighup);        /* Catch SIGHUP */
  2009. #ifndef NOSIGWINCH
  2010. #ifdef SIGWINCH
  2011.     signal(SIGWINCH,winchh);        /* Catch window-size change */
  2012. #endif /* SIGWINCH */
  2013. #endif /* NOSIGWINCH */
  2014.  
  2015. #ifndef NOJC
  2016. /*
  2017.   Get the initial job control state.
  2018.   If it is SIG_IGN, that means the shell does not support job control,
  2019.   and so we'd better not suspend ourselves.
  2020. */
  2021. #ifdef SIGTSTP
  2022.     jchdlr = signal(SIGTSTP,SIG_IGN);
  2023.     if (jchdlr == SIG_IGN) {
  2024.     jcshell = 0;
  2025.     debug(F100,"sysinit jchdlr: SIG_IGN","",0);
  2026.     } else if (jchdlr == SIG_DFL) {
  2027.     debug(F100,"sysinit jchdlr: SIG_DFL","",0);
  2028.     jcshell = 1;
  2029.     } else {
  2030.     debug(F100,"sysinit jchdlr: other","",0);
  2031.     jcshell = 3;
  2032.     }
  2033.     (VOID) signal(SIGTSTP,jchdlr);    /* Put it back... */
  2034. #endif /* SIGTSTP */
  2035. #endif /* NOJC */
  2036.  
  2037.     conbgt(0);                /* See if we're in the background */
  2038.     congm();                /* Get console modes */
  2039.  
  2040.     (VOID) signal(SIGALRM,SIG_IGN);    /* Ignore alarms */
  2041.  
  2042.     ignorsigs();            /* Ignore some other signals */
  2043.  
  2044. #ifdef F_SETFL
  2045.     iniflags = fcntl(0,F_GETFL,0);    /* Get stdin flags */
  2046. #endif /* F_SETFL */
  2047.  
  2048. #ifdef ultrix
  2049.     gtty(0,&vanilla);            /* Get sgtty info */
  2050. #else
  2051. #ifdef AUX
  2052.     set42sig();                /* Don't ask! (hakanson@cs.orst.edu) */
  2053. #endif /* AUX */
  2054. #endif /* ultrix */
  2055. /*
  2056.   Warning: on some UNIX systems (SVR4?), ttyname() reportedly opens /dev but
  2057.   never closes it.  If it is called often enough, we run out of file
  2058.   descriptors and subsequent open()'s of other devices or files can fail.
  2059. */
  2060.     s = NULL;
  2061. #ifndef MINIX
  2062.     if (isatty(0))            /* Name of controlling terminal */
  2063.       s = ttyname(0);
  2064.     else if (isatty(1))
  2065.       s = ttyname(1);
  2066.     else if (isatty(2))
  2067.       s = ttyname(2);
  2068.     debug(F110,"sysinit ttyname(0)",s,0);
  2069. #endif /* MINIX */
  2070.  
  2071. #ifdef BEOS
  2072.     if (!dftty)
  2073.       makestr(&dftty,s);
  2074. #endif /* BEOS */
  2075.  
  2076.     if (s)
  2077.       ckstrncpy((char *)cttnam,s,DEVNAMLEN+1);
  2078. #ifdef SVORPOSIX
  2079.     if (!cttnam[0])
  2080.       ctermid(cttnam);
  2081. #endif /* SVORPOSIX */
  2082.     if (!cttnam[0])
  2083.       ckstrncpy((char *)cttnam,dftty,DEVNAMLEN+1);
  2084.     debug(F110,"sysinit CTTNAM",CTTNAM,0);
  2085.     debug(F110,"sysinit cttnam",cttnam,0);
  2086.  
  2087.     ttgwsiz();                /* Get window (screen) dimensions. */
  2088.  
  2089. #ifndef NOSYSCONF
  2090. #ifdef _SC_OPEN_MAX
  2091.     ckmaxfiles = sysconf(_SC_OPEN_MAX);
  2092. #endif /* _SC_OPEN_MAX */
  2093. #endif /* NOSYSCONF */
  2094.  
  2095. #ifdef Plan9
  2096.     if (!backgrd) {
  2097.         consctlfd = open("/dev/consctl", O_WRONLY);
  2098.         /*noisefd = open("/dev/noise", O_WRONLY)*/
  2099.     }
  2100.     ckxech = 1;
  2101. #endif /* Plan9 */
  2102.  
  2103. #ifdef CK_UTSNAME
  2104.     if (uname(&name) > -1) {
  2105.     ckstrncpy(unm_mch,name.machine,CK_SYSNMLN);
  2106.     ckstrncpy(unm_nam,name.sysname,CK_SYSNMLN);
  2107.     ckstrncpy(unm_rel,name.release,CK_SYSNMLN);
  2108.     ckstrncpy(unm_ver,name.version,CK_SYSNMLN);
  2109. #ifdef DEBUG
  2110.     if (deblog) {
  2111.         debug(F110,"sysinit uname machine",unm_mch,0);
  2112.         debug(F110,"sysinit uname sysname",unm_nam,0);
  2113.         debug(F110,"sysinit uname release",unm_rel,0);
  2114.         debug(F110,"sysinit uname version",unm_ver,0);
  2115.     }
  2116. #endif /* DEBUG */
  2117.  
  2118. #ifdef HPUX9PLUS
  2119.     if (name.machine[5] == '8')
  2120.       hpis800 = 1;
  2121.     else
  2122.       hpis800 = 0;
  2123.     debug(F101,"sysinit hpis800","",hpis800);
  2124. #endif /* HPUX9PLUS */
  2125. #ifdef TRU64
  2126.         getsysinfo(GSI_PLATFORM_NAME, unm_mod, CK_SYSNMLN, 0, 0);
  2127.         debug(F110,"sysinit getsysinfo model",unm_mod,0);
  2128. #endif /* TRU64 */
  2129. #ifdef SOLARIS25
  2130.         sysinfo(SI_PLATFORM, unm_mod, CK_SYSNMLN);
  2131.         debug(F110,"sysinit sysinfo model",unm_mod,0);
  2132. #endif /* SOLARIS25 */
  2133.     }
  2134. #endif /* CK_UTSNAME */
  2135.  
  2136. #ifdef CK_ENVIRONMENT
  2137.     {
  2138. #ifdef TNCODE
  2139.     extern char tn_env_acct[], tn_env_disp[], tn_env_job[],
  2140.     tn_env_prnt[], tn_env_sys[];
  2141. #endif /* TNCODE */
  2142.     extern char uidbuf[];
  2143.         extern char * whoami();
  2144.     char *p;
  2145. #ifdef CKSENDUID
  2146.         uidbuf[0] = '\0';
  2147. #ifdef IKSD
  2148.         if (!inserver) {
  2149. #endif /* IKSD */
  2150.             p = getenv("USER");
  2151.             debug(F110,"sysinit uidbuf from USER",uidbuf,0);
  2152.         if (!p) p = "";
  2153.             if (!*p) {
  2154.                 p = getenv("LOGNAME");
  2155.                 debug(F110,"sysinit uidbuf from LOGNAME",uidbuf,0);
  2156.             }
  2157.         if (!p) p = "";
  2158.             if (!*p) {
  2159.                 p = whoami();
  2160.                 debug(F110,"sysinit uidbuf from whoami()",uidbuf,0);
  2161.             }
  2162.         if (!p) p = "";
  2163.         ckstrncpy(uidbuf, *p ? p : "UNKNOWN", UIDBUFLEN);
  2164. #ifdef IKSD
  2165.         }
  2166. #endif /* IKSD */
  2167.     debug(F110,"sysinit final uidbuf",uidbuf,0);
  2168. #endif /* CKSENDUID */
  2169.  
  2170. #ifdef TNCODE
  2171.     if ((p = getenv("JOB"))) ckstrncpy(tn_env_job,p,63);
  2172.     if ((p = getenv("ACCT"))) ckstrncpy(tn_env_acct,p,63);
  2173.     if ((p = getenv("PRINTER"))) ckstrncpy(tn_env_prnt,p,63);
  2174.     if ((p = getenv("DISPLAY"))) ckstrncpy(tn_env_disp,p,63);
  2175. #ifdef aegis
  2176.     ckstrncpy(tn_env_sys,"Aegis",64);
  2177. #else
  2178. #ifdef Plan9
  2179.     ckstrncpy(tn_env_sys,"Plan9",64);
  2180. #else
  2181.     ckstrncpy(tn_env_sys,"UNIX",64);
  2182. #endif /* Plan9 */
  2183. #endif /* aegis */
  2184. #endif /* TNCODE */
  2185.     }
  2186. #endif /* CK_ENVIRONMENT */
  2187. #ifdef CK_SNDLOC
  2188.     {
  2189.     extern char * tn_loc;
  2190.     char *p;
  2191.     if (p = getenv("LOCATION"))
  2192.       if (tn_loc = (char *)malloc((int)strlen(p)+1))
  2193.         strcpy(tn_loc,p);        /* safe */
  2194.     }
  2195. #endif /* CK_SNDLOC */
  2196.  
  2197.     ckstrncpy(startupdir, zgtdir(), CKMAXPATH);
  2198.     startupdir[CKMAXPATH] = '\0';
  2199.     x = strlen(startupdir);
  2200.     if (x <= 0) {
  2201.     startupdir[0] = '/';
  2202.     startupdir[1] = '\0';
  2203.     } else if (startupdir[x-1] != '/') {
  2204.     startupdir[x] = '/';
  2205.     startupdir[x+1] = '\0';
  2206.     }
  2207.     debug(F110,"sysinit startupdir",startupdir,0);
  2208. #ifdef TTLEBUF
  2209.     le_init();
  2210. #endif /* TTLEBUF */
  2211. #ifdef BSD44ORPOSIX
  2212.     /* This should catch the ncurses platforms */
  2213.     /* Some platforms don't have putenv(), like NeXTSTEP */
  2214.     putenv("NCURSES_NO_SETBUF=1");
  2215. #endif /* BSD44ORPOSIX */
  2216.     return(0);
  2217. }
  2218.  
  2219. /*  S Y S C L E A N U P  --  System-dependent program cleanup.  */
  2220.  
  2221. int
  2222. syscleanup() {
  2223. #ifdef F_SETFL
  2224.     if (iniflags > -1)
  2225.       fcntl(0,F_SETFL,iniflags);    /* Restore stdin flags */
  2226. #endif /* F_SETFL */
  2227. #ifdef ultrix
  2228.     stty(0,&vanilla);                   /* Get sgtty info */
  2229. #endif /* ultrix */
  2230. #ifdef NETCMD
  2231.     if (ttpid) kill(ttpid,9);
  2232. #endif /* NETCMD */
  2233.     return(0);
  2234. }
  2235.  
  2236. /*  T T O P E N  --  Open a tty for exclusive access.  */
  2237.  
  2238. /*
  2239.   Call with:
  2240.     ttname: character string - device name or network host name.
  2241.     lcl:
  2242.   If called with lcl < 0, sets value of lcl as follows:
  2243.   0: the terminal named by ttname is the job's controlling terminal.
  2244.   1: the terminal named by ttname is not the job's controlling terminal.
  2245.   But watch out: if a line is already open, or if requested line can't
  2246.   be opened, then lcl remains (and is returned as) -1.
  2247.     modem:
  2248.   Less than zero: ttname is a network host name.
  2249.   Zero or greater: ttname is a terminal device name.
  2250.   Zero means a local connection (don't use modem signals).
  2251.   Positive means use modem signals.
  2252.    timo:
  2253.   0 = no timer.
  2254.   nonzero = number of seconds to wait for open() to return before timing out.
  2255.  
  2256.   Returns:
  2257.     0 on success
  2258.    -5 if device is in use
  2259.    -4 if access to device is denied
  2260.    -3 if access to lock directory denied
  2261.    -2 upon timeout waiting for device to open
  2262.    -1 on other error
  2263. */
  2264. static int ttotmo = 0;            /* Timeout flag */
  2265. /* Flag kept here to avoid being clobbered by longjmp.  */
  2266.  
  2267. int
  2268. ttopen(ttname,lcl,modem,timo) char *ttname; int *lcl, modem, timo; {
  2269.  
  2270. #ifdef BSD44
  2271. #define ctermid(x) strcpy(x,"")
  2272. #else
  2273. #ifdef SVORPOSIX
  2274. #ifndef CIE
  2275.     extern char *ctermid();        /* Wish they all had this! */
  2276. #else                    /* CIE Regulus */
  2277. #define ctermid(x) strcpy(x,"")
  2278. #endif /* CIE */
  2279. #endif /* SVORPOSIX */
  2280. #endif /* BSD44 */
  2281.  
  2282. #ifdef ultrix
  2283.     int temp = 0;
  2284. #endif /* ultrix */
  2285.  
  2286. #ifndef OPENFIRST
  2287.     char fullname[DEVNAMLEN+1];
  2288. #endif /* OPENFIRST */
  2289.  
  2290.     char * fnam;            /* Full name after expansion */
  2291.  
  2292.     int y;
  2293.  
  2294. #ifndef pdp11
  2295. #define NAMEFD     /* Feature to allow name to be an open file descriptor */
  2296. #endif /* pdp11 */
  2297.  
  2298. #ifdef NAMEFD
  2299.     char *p;
  2300.     debug(F101,"ttopen telnetfd","",telnetfd);
  2301. #endif /* NAMEFD */
  2302.  
  2303.     debug(F110,"ttopen ttname",ttname,0);
  2304.     debug(F110,"ttopen ttnmsv",ttnmsv,0);
  2305.     debug(F101,"ttopen modem","",modem);
  2306.     debug(F101,"ttopen netconn","",netconn);
  2307.     debug(F101,"ttopen ttyfd","",ttyfd);
  2308.     debug(F101,"ttopen *lcl","",*lcl);
  2309.     debug(F101,"ttopen ttmdm","",ttmdm);
  2310.     debug(F101,"ttopen ttnet","",ttnet);
  2311.  
  2312.     ttpmsk = 0xff;
  2313.     lockpid[0] = '\0';
  2314.  
  2315.     if (ttyfd > -1) {            /* If device already opened */
  2316.         if (!strncmp(ttname,ttnmsv,DEVNAMLEN)) /* are new & old names equal? */
  2317.       return(0);            /* Yes, nothing to do - just return */
  2318.     ttnmsv[0] = '\0';        /* No, clear out old name */
  2319.     ttclos(ttyfd);            /* close old connection.  */
  2320.     }
  2321.     wasclosed = 0;            /* New connection, not closed yet. */
  2322.     ttpipe = 0;                /* Assume it's not a pipe */
  2323.     ttpty = 0;                /* or a pty... */
  2324.  
  2325. #ifdef NETCONN
  2326. /*
  2327.   This is a bit tricky...  Suppose that previously Kermit had dialed a telnet
  2328.   modem server ("set host xxx:2001, set modem type usr, dial ...").  Then the
  2329.   connection was closed (ttyfd = -1), and then a REDIAL command was given.  At
  2330.   this point we've obliterated the negative modem type hack, and so would
  2331.   treat the IP hostname as a device name, and would then fail because of "No
  2332.   such device or directory".  But the previous connection has left behind some
  2333.   clues, so let's use them...
  2334. */
  2335.     if (ttyfd < 0) {            /* Connection is not open */
  2336.     if (!strcmp(ttname,ttnmsv)) {    /* Old and new names the same? */
  2337.         if (((netconn > 0) && (ttmdm < 0)) ||
  2338.         ((ttnet > 0) &&
  2339.          (!ckstrchr(ttname,'/')) && (ckstrchr(ttname,':')))
  2340.         ) {
  2341.         int x, rc;
  2342.         x = (ttmdm < 0) ? -ttmdm : ttnet;
  2343.         rc = netopen(ttname, lcl, x);
  2344.         debug(F111,"ttopen REOPEN netopen",ttname,rc);
  2345.         if (rc > -1) {
  2346.             netconn = 1;
  2347.             xlocal = *lcl = 1;
  2348.         } else {
  2349.             netconn = 0;
  2350.         }
  2351.         gotsigs = 0;
  2352.         return(rc);
  2353.         }
  2354.     }
  2355.     }
  2356. #endif /* NETCONN */
  2357.  
  2358. #ifdef MAXNAMLEN
  2359.     debug(F100,"ttopen MAXNAMLEN defined","",0);
  2360. #else
  2361.     debug(F100,"ttopen MAXNAMLEN *NOT* defined","",0);
  2362. #endif
  2363.  
  2364. #ifdef BSD4
  2365.     debug(F100,"ttopen BSD4 defined","",0);
  2366. #else
  2367.     debug(F100,"ttopen BSD4 *NOT* defined","",0);
  2368. #endif /* BSD4 */
  2369.  
  2370. #ifdef BSD42
  2371.     debug(F100,"ttopen BSD42 defined","",0);
  2372. #else
  2373.     debug(F100,"ttopen BSD42 *NOT* defined","",0);
  2374. #endif /* BSD42 */
  2375.  
  2376. #ifdef MYREAD
  2377.     debug(F100,"ttopen MYREAD defined","",0);
  2378. #else
  2379.     debug(F100,"ttopen MYREAD *NOT* defined","",0);
  2380. #endif /* MYREAD */
  2381.  
  2382. #ifdef    NETCONN
  2383.     if (modem < 0) {            /* modem < 0 = code for network */
  2384.     int x;
  2385.     ttmdm = modem;
  2386.     modem = -modem;            /* Positive network type number */
  2387.     fdflag = 0;            /* Stdio not redirected. */
  2388.     netconn = 1;            /* And it's a network connection */
  2389.     debug(F111,"ttopen net",ttname,modem);
  2390. #ifdef NAMEFD
  2391.     for (p = ttname; isdigit(*p); p++) ; /* Check for all digits */
  2392.      if (*p == '\0' && (telnetfd || x25fd)) { /* Avoid X.121 addresses */
  2393.         ttyfd = atoi(ttname);    /* Is there a way to test it's open? */
  2394.         ttfdflg = 1;        /* We got an open file descriptor */
  2395.         debug(F111,"ttopen got open network fd",ttname,ttyfd);
  2396.         ckstrncpy(ttnmsv,ttname,DEVNAMLEN); /* Remember the "name". */
  2397.         x = 1;            /* Return code is "good". */
  2398.         if (telnetfd) {
  2399.         ttnet = NET_TCPB;
  2400.         if (ttnproto != NP_TCPRAW)
  2401.           ttnproto = NP_TELNET;
  2402. #ifdef SUNX25
  2403.         } else if (x25fd) {
  2404.         ttnet = NET_SX25;
  2405.         ttnproto = NP_NONE;
  2406. #endif /* SUNX25 */
  2407.         }
  2408.     } else {            /* Host name or address given */
  2409. #ifdef NETPTY
  2410.         if (modem == NET_PTY) {
  2411.         int x;
  2412.         if (nopush) {
  2413.             debug(F100,"ttopen PTY: nopush","",0);
  2414.             return(-1);
  2415.         }
  2416.                 ttnet = NET_PTY;
  2417.         ttnproto = NP_NONE;
  2418.                 netconn = 1;            /* but we don't use network i/o */
  2419.                 ttpty = 1;
  2420.                 debug(F110,"ttopen PTY",ttname,0);
  2421.         x = do_pty(ttname);
  2422.         if (x > -1) {
  2423.             ckstrncpy(ttnmsv,ttname,DEVNAMLEN);
  2424.             xlocal = *lcl = 1;    /* It's local */
  2425.         } else {
  2426.             ttpty = 0;
  2427.             netconn = 0;
  2428.         }
  2429.         gotsigs = 0;
  2430.         return(x);
  2431.         }
  2432. #endif /* NETPTY */
  2433. #ifdef NETCMD
  2434. /*
  2435.   dup2() is not available on older System V platforms like AT&T 3Bx.  For
  2436.   those systems we punt by not defining NETCMD, but we might be able to do
  2437.   better -- see workarounds for this problem in ckufio.c (search for dup2).
  2438. */
  2439.         if (modem == NET_CMD) {
  2440.         if (nopush) {
  2441.             debug(F100,"ttopen pipe: nopush","",0);
  2442.             return(-1);
  2443.         }
  2444.         if (pipe(pipe0) || pipe(pipe1)) {
  2445.             perror("Pipe error");
  2446.             return(-1);
  2447.         }
  2448.         ttpid = fork();        /* Make a fork */
  2449.  
  2450.         switch (ttpid) {
  2451.           case -1:        /* Error making fork */
  2452.             close(pipe0[0]);
  2453.             close(pipe0[1]);
  2454.             close(pipe1[0]);
  2455.             close(pipe1[1]);
  2456.             perror("Fork error");
  2457.             return(-1);
  2458.           case 0:        /* Child. */
  2459.             close(pipe0[0]);
  2460.             close(pipe1[1]);
  2461.             dup2(pipe0[1], 1);
  2462.             close(pipe0[1]);
  2463.             dup2(pipe1[0], 0);
  2464.             close(pipe1[0]);
  2465.             system(ttname);
  2466.             _exit(0);
  2467.           default:        /* Parent */
  2468.             close(pipe0[1]);
  2469.             close(pipe1[0]);
  2470.             fdin = pipe0[0];    /* Read from pipe */
  2471.             fdout = pipe1[1];    /* Write to pipe */
  2472.             ttout = fdopen(fdout,"w"); /* Get stream so we can */
  2473.             if (!ttout) {    /* make it unbuffered. */
  2474.             perror("fdopen failure");
  2475.             return(-1);
  2476.             }
  2477.             setbuf(ttout,NULL);
  2478.             ckstrncpy(ttnmsv,ttname,DEVNAMLEN);
  2479.             xlocal = *lcl = 1;    /* It's local */
  2480.             netconn = 1;    /* Call it a network connection */
  2481.             ttmdm = modem;    /* Remember network type */
  2482.             ttyfd = fdin;
  2483.             ttpipe = 1;
  2484.             gotsigs = 0;
  2485.             return(0);
  2486.         }
  2487.         }
  2488. #endif /* NETCMD */
  2489. #endif /* NAMEFD */
  2490.         x = netopen(ttname, lcl, modem); /* (see ckcnet.h) */
  2491.         if (x > -1) {
  2492.         ckstrncpy(ttnmsv,ttname,DEVNAMLEN);
  2493.         } else netconn = 0;
  2494. #ifdef NAMEFD
  2495.     }
  2496. #endif /* NAMEFD */
  2497.  
  2498. #ifdef sony_news            /* Sony NEWS */
  2499.     if (ioctl(ttyfd,TIOCKGET,&km_ext) < 0) { /* Get Kanji mode */
  2500.         perror("ttopen error getting Kanji mode (network)");
  2501.         debug(F111,"ttopen error getting Kanji mode","network",0);
  2502.         km_ext = -1;        /* Make sure this stays undefined. */
  2503.     }
  2504. #endif /* sony_news */
  2505.  
  2506.     xlocal = *lcl = 1;        /* Network connections are local. */
  2507.     debug(F101,"ttopen net x","",x);
  2508. #ifdef COMMENT
  2509. /* Let netopen() do this */
  2510.     if (x > -1 && !x25fd)
  2511.       x = tn_ini();            /* Initialize TELNET protocol */
  2512. #endif /* COMMENT */
  2513.     gotsigs = 0;
  2514.     return(x);
  2515.     } else {                /* Terminal device */
  2516. #endif    /* NETCONN */
  2517.  
  2518. #ifdef NAMEFD
  2519. /*
  2520.   This code lets you give Kermit an open file descriptor for a serial
  2521.   communication device, rather than a device name.  Kermit assumes that the
  2522.   line is already open, locked, conditioned with the right parameters, etc.
  2523. */
  2524.     for (p = ttname; isdigit(*p); p++) ; /* Check for all digits */
  2525.     if (*p == '\0') {
  2526.         ttyfd = atoi(ttname);    /* Is there a way to test it's open? */
  2527.         debug(F111,"ttopen got open fd",ttname,ttyfd);
  2528.         ckstrncpy(ttnmsv,ttname,DEVNAMLEN); /* Remember the "name". */
  2529.         if (ttyfd == 0)        /* If it's stdio... */
  2530.           xlocal = *lcl = 0;    /* we're in remote mode */
  2531.         else            /* otherwise */
  2532.           xlocal = *lcl = 1;    /* local mode. */
  2533.         netconn = 0;        /* Assume it's not a network. */
  2534.         tvtflg = 0;            /* Might need to initialize modes. */
  2535.         ttmdm = modem;        /* Remember modem type. */
  2536.         fdflag = 0;            /* Stdio not redirected. */
  2537.         ttfdflg = 1;        /* Flag we were opened this way. */
  2538.  
  2539. #ifdef sony_news            /* Sony NEWS */
  2540.         /* Get device Kanji mode */
  2541.         if (ioctl(ttyfd,TIOCKGET,&km_ext) < 0) {
  2542.         perror("ttopen error getting Kanji mode");
  2543.         debug(F101,"ttopen error getting Kanji mode","",0);
  2544.         km_ext = -1;        /* Make sure this stays undefined. */
  2545.         }
  2546. #endif /* sony_news */
  2547.         gotsigs = 0;
  2548.         return(0);            /* Return success */
  2549.     }
  2550. #endif /* NAMEFD */
  2551. #ifdef NETCONN
  2552.     }
  2553. #endif /* NETCONN */
  2554.  
  2555. /* Here we have to open a serial device of the given name. */
  2556.  
  2557.     netconn = 0;            /* So it's not a network connection */
  2558.     occt = signal(SIGINT, cctrap);    /* Set Control-C trap, save old one */
  2559.     sigint_ign = 0;
  2560.  
  2561.     tvtflg = 0;            /* Flag for use by ttvt(). */
  2562.                 /* 0 = ttvt not called yet for this device */
  2563.  
  2564.     fdflag = (!isatty(0) || !isatty(1)); /* Flag for stdio redirected */
  2565.     debug(F101,"ttopen fdflag","",fdflag);
  2566.  
  2567.     ttmdm = modem;                      /* Make this available to other fns */
  2568.     xlocal = *lcl;                      /* Make this available to other fns */
  2569.  
  2570. /* Code for handling bidirectional tty lines goes here. */
  2571. /* Use specified method for turning off logins and suppressing getty. */
  2572.  
  2573. #ifdef ACUCNTRL
  2574.     /* Should put call to priv_on() here, but that would be very risky! */
  2575.     acucntrl("disable",ttname);         /* acucntrl() program. */
  2576.     /* and priv_off() here... */
  2577. #else
  2578. #ifdef ATT7300
  2579.     if ((attmodem & DOGETY) == 0)       /* offgetty() program. */
  2580.       attmodem |= offgetty(ttname);    /* Remember response.  */
  2581. #endif /* ATT7300 */
  2582. #endif /* ACUCNTRL */
  2583.  
  2584. #ifdef OPENFIRST
  2585. /*
  2586.  1985-2001: opens device first then gets lock; reason:
  2587.  Kermit usually has to run setuid or setgid in order to create a lockfile.
  2588.  If you give a SET LINE command for a device that happens to be your job's
  2589.  controlling terminal, Kermit doesn't have to create a lockfile, and in fact
  2590.  should not create one, and would fail if it tried to if it did not have the
  2591.  required privileges.  But you can't find out if two tty device names are
  2592.  equivalent until you have a file descriptor that you can give to ttyname().
  2593.  But this can cause a race condition between Kermit and [m]getty.  So see
  2594.  the [#]else part...
  2595. */ 
  2596.  
  2597. /*
  2598.  In the following section, we open the tty device for read/write.
  2599.  If a modem has been specified via "set modem" prior to "set line"
  2600.  then the O_NDELAY parameter is used in the open, provided this symbol
  2601.  is defined (e.g. in fcntl.h), so that the program does not hang waiting
  2602.  for carrier (which in most cases won't be present because a connection
  2603.  has not been dialed yet).  O_NDELAY is removed later on in ttopen().  It
  2604.  would make more sense to first determine if the line is local before
  2605.  doing this, but because ttyname() requires a file descriptor, we have
  2606.  to open it first.  See do_open().
  2607.  
  2608.  Now open the device using the desired treatment of carrier.
  2609.  If carrier is REQUIRED, then open could hang forever, so an optional
  2610.  timer is provided.  If carrier is not required, the timer should never
  2611.  go off, and should do no harm...
  2612. */
  2613.     ttotmo = 0;                /* Flag no timeout */
  2614.     debug(F101,"ttopen timo","",timo);
  2615.     debug(F101,"ttopen xlocal","",xlocal);
  2616.     if (timo > 0) {
  2617.     int xx;
  2618.     saval = signal(SIGALRM,timerh);    /* Timed, set up timer. */
  2619.     xx = alarm(timo);        /* Timed open() */
  2620.     debug(F101,"ttopen alarm","",xx);
  2621.     if (
  2622. #ifdef CK_POSIX_SIG
  2623.         sigsetjmp(sjbuf,1)
  2624. #else
  2625.         setjmp(sjbuf)
  2626. #endif /* CK_POSIX_SIG */
  2627.         ) {
  2628.         ttotmo = 1;            /* Flag timeout. */
  2629.     } else ttyfd = do_open(ttname);
  2630.     ttimoff();
  2631.     debug(F111,"ttopen","modem",modem);
  2632.     debug(F101,"ttopen ttyfd","",ttyfd);
  2633.     debug(F101,"ttopen alarm return","",ttotmo);
  2634.     } else {
  2635.     errno = 0;
  2636.     ttyfd = do_open(ttname);
  2637.     }
  2638.     debug(F111,"ttopen ttyfd",ttname,ttyfd);
  2639.     if (ttyfd < 0) {            /* If couldn't open, fail. */
  2640.     debug(F101,"ttopen errno","",errno);
  2641.     if (errno > 0 && !quiet)
  2642.       perror(ttname);        /* Print message */
  2643.  
  2644. #ifdef ATT7300
  2645.     if (attmodem & DOGETY)        /* was getty(1m) running before us? */
  2646.       ongetty(ttnmsv);        /* yes, restart on tty line */
  2647.     attmodem &= ~DOGETY;        /* no phone in use, getty restored */
  2648. #else
  2649. #ifdef ACUCNTRL
  2650.         /* Should put call to priv_on() here, but that would be risky! */
  2651.     acucntrl("enable",ttname);    /* acucntrl() program. */
  2652.     /* and priv_off() here... */
  2653. #endif /* ACUNTRL */
  2654. #endif /* ATT7300 */
  2655.  
  2656.     signal(SIGINT,occt);        /* Put old Ctrl-C trap back. */
  2657.     if (errno == EACCES) {        /* Device is protected against user */
  2658.         debug(F110,"ttopen EACCESS",ttname,0); /* Return -4 */
  2659.         return(-4);
  2660.     } else return(ttotmo ? -2 : -1); /* Otherwise -2 if timeout, or -1 */
  2661.     }
  2662.  
  2663. #ifdef QNX
  2664.     {
  2665.     extern int qnxportlock;
  2666.     x = qnxopencount();
  2667.     debug(F101,"ttopen qnxopencount","",x);
  2668.     debug(F101,"ttopen qnxportlock","",qnxportlock);
  2669.     if (x < 0 && qnxportlock) {
  2670.         ttclos(0);
  2671.         printf("?Can't get port open count\n");
  2672.         printf("(Try again with SET QNX-PORT-LOCK OFF)\n");
  2673.         return(-1);            /* Indicate device is in use */
  2674.     }
  2675.     if (x > 1) {            /* 1 == me */
  2676.         if (qnxportlock)
  2677.           ttclos(0);
  2678.           return(-2);        /* Indicate device is in use */
  2679.         else if (!quiet)
  2680.           printf("WARNING: \"%s\" looks busy...\n",ttdev);
  2681.     }
  2682.     }
  2683. #endif /* QNX */
  2684.  
  2685. #ifdef Plan9
  2686.     /* take this opportunity to open the control channel */
  2687.     if (p9openttyctl(ttname) < 0)
  2688. #else
  2689.     /* Make sure it's a real tty. */
  2690.     if (!isatty(ttyfd) && strcmp(ttname,"/dev/null"))
  2691. #endif /* Plan9 */
  2692.       {
  2693.     fprintf(stderr,"%s is not a terminal device\n",ttname);
  2694.     debug(F111,"ttopen not a tty",ttname,errno);
  2695.     close(ttyfd);
  2696.     ttyfd = -1;
  2697.     wasclosed = 1;
  2698.     signal(SIGINT,occt);
  2699.     return(-1);
  2700.     }
  2701.  
  2702. #ifdef aegis
  2703.     /* Apollo C runtime claims that console pads are tty devices, which
  2704.      * is reasonable, but they aren't any good for packet transfer. */
  2705.     ios_$inq_type_uid((short)ttyfd, ttyuid, st);
  2706.     if (st.all != status_$ok) {
  2707.         fprintf(stderr, "problem getting tty object type: ");
  2708.         error_$print(st);
  2709.     } else if (ttyuid != sio_$uid) { /* reject non-SIO lines */
  2710.         close(ttyfd); ttyfd = -1;
  2711.         wasclosed = 1;
  2712.         errno = ENOTTY; perror(ttname);
  2713.         signal(SIGINT,occt);
  2714.         return(-1);
  2715.     }
  2716. #endif /* aegis */
  2717.  
  2718.     sigint_ign = (occt == SIG_IGN) ? 1 : 0;
  2719.  
  2720.     ckstrncpy(ttnmsv,ttname,DEVNAMLEN);    /* Keep copy of name locally. */
  2721.  
  2722. /* Caller wants us to figure out if line is controlling tty */
  2723.  
  2724.     if (*lcl < 0) {
  2725.         if (strcmp(ttname,CTTNAM) == 0) { /* "/dev/tty" always remote */
  2726.             xlocal = 0;
  2727.         debug(F111,"ttopen ttname=CTTNAM",ttname,xlocal);
  2728.         } else if (strcmp(ttname,cttnam) == 0) {
  2729.             xlocal = 0;
  2730.         debug(F111,"ttopen ttname=cttnam",ttname,xlocal);
  2731.     } else if (cttnam[0]) {
  2732. #ifdef BEBOX_DR7
  2733.             x = ttnmsv;            /* ttyname() is broken */
  2734. #else
  2735.             x = ttyname(ttyfd);         /* Get real name of ttname. */
  2736. #endif /* BEBOX_DR7 */
  2737.         if (!x) x = "";
  2738.         if (*x)
  2739.           xlocal = ((strncmp(x,cttnam,DEVNAMLEN) == 0) ? 0 : 1);
  2740.         else
  2741.           xlocal = 1;
  2742.             debug(F111,"ttopen ttyname(ttyfd) xlocal",x,xlocal);
  2743.         }
  2744.     }
  2745.  
  2746. #ifndef NOFDZERO
  2747. /* Note, the following code was added so that Unix "idle-line" snoopers */
  2748. /* would not think Kermit was idle when it was transferring files, and */
  2749. /* maybe log people out. */
  2750.     if (xlocal == 0) {            /* Remote mode */
  2751.     if (fdflag == 0) {        /* Standard i/o is not redirected */
  2752.         debug(F100,"ttopen setting ttyfd = 0","",0);
  2753. #ifdef LYNXOS
  2754.         /* On Lynx OS, fd 0 is open for read only. */
  2755.         dup2(ttyfd,0);
  2756. #endif /* LYNXOS */
  2757.         close(ttyfd);        /* Use file descriptor 0 */
  2758.         ttyfd = 0;
  2759.     } else {            /* Standard i/o is redirected */
  2760.         debug(F101,"ttopen stdio redirected","",ttyfd);
  2761.     }
  2762.     }
  2763. #endif /* NOFDZERO */
  2764.  
  2765. /* Now check if line is locked -- if so fail, else lock for ourselves */
  2766. /* Note: After having done this, don't forget to delete the lock if you */
  2767. /* leave ttopen() with an error condition. */
  2768.  
  2769.     lkf = 0;                            /* Check lock */
  2770.     if (xlocal > 0) {
  2771.     int xx; int xpid;
  2772.         if ((xx = ttlock(ttname)) < 0) { /* Can't lock it. */
  2773.             debug(F111,"ttopen ttlock fails",ttname,xx);
  2774.         /* WARNING - This close() can hang if tty is an empty socket... */
  2775.             close(ttyfd);        /* Close the device. */
  2776.         ttyfd = -1;            /* Erase its file descriptor. */
  2777.         wasclosed = 1;
  2778.         signal(SIGINT,occt);    /* Put old SIGINT back. */
  2779.         sigint_ign = (occt == SIG_IGN) ? 1 : 0;
  2780.         if (xx == -2) {        /* If lockfile says device in use, */
  2781. #ifndef NOUUCP
  2782.         debug(F111,"ttopen reading lockfile pid",flfnam,xx);
  2783.         xpid = ttrpid(flfnam);    /* Try to read pid from lockfile */
  2784.         if (xpid > -1) {    /* If we got a pid */
  2785.                     if (!quiet)
  2786.               printf("Locked by process %d\n",xpid); /* tell them. */
  2787.             sprintf(lockpid,"%d",xpid);    /* Record it too */
  2788.             debug(F110,"ttopen lockpid",lockpid,0);
  2789.         } else if (*flfnam) {
  2790.             extern char *DIRCMD;
  2791.             char *p = NULL;
  2792.             int x;
  2793.             x = (int)strlen(flfnam) + (int)strlen(DIRCMD) + 2;
  2794.             p = malloc(x);    /* Print a directory listing. */
  2795. /*
  2796.   Note: priv_on() won't help here, because we do not pass privs along to
  2797.   to inferior processes, in this case ls.  So if the real user does not have
  2798.   directory-listing access to the lockfile directory, this will result in
  2799.   something like "not found".  That's why we try this only as a last resort.
  2800. */
  2801.             if (p) {        /* If we got the space... */
  2802.             ckmakmsg(p,x,DIRCMD," ",flfnam,NULL);
  2803.             zsyscmd(p);    /* Get listing. */
  2804.             if (p) {    /* free the space */
  2805.                 free(p);
  2806.                 p = NULL;
  2807.             }
  2808.             }
  2809.         }
  2810. #endif /* NOUUCP */
  2811.         return(-5);        /* Code for device in use */
  2812.         } else return(-3);        /* Access denied */
  2813.         } else lkf = 1;
  2814.     }
  2815. #else  /* OPENFIRST */
  2816.  
  2817. /*
  2818.   27 Oct 2001: New simpler code that gets the lock first and then opens the
  2819.   device, which eliminates the race condition.  The downside is you can no
  2820.   longer say "set line /dev/ttyp0" or whatever, where /dev/ttyp0 is your login
  2821.   terminal, without trying to create a lockfile, which fails if C-Kermit lacks
  2822.   privs, and if it succeeds, it has created a lockfile where it didn't create
  2823.   one before.
  2824. */
  2825.     xlocal = *lcl;            /* Is the device my login terminal? */
  2826.     debug(F111,"ttopen xlocal","A",xlocal);
  2827.     fnam = ttname;
  2828.     if (strcmp(ttname,CTTNAM) && netconn == 0) {
  2829.     if (zfnqfp(ttname,DEVNAMLEN+1,fullname)) {
  2830.         if ((int)strlen(fullname) > 0)
  2831.           fnam = fullname;
  2832.     }
  2833.     }
  2834.     debug(F110,"ttopen fnam",fnam,0);
  2835.     if (xlocal < 0) {
  2836.     xlocal = (strcmp(fnam,CTTNAM) != 0);
  2837.     }
  2838.     debug(F111,"ttopen xlocal","B",xlocal);
  2839.  
  2840.     lkf = 0;                            /* No lock yet */
  2841.     if (xlocal > 0) {            /* If not... */
  2842.     int xx; int xpid;
  2843.     xx = ttlock(fnam);        /* Try to lock it. */
  2844.     debug(F101,"ttopen ttlock","",xx);
  2845.         if (xx < 0) {            /* Can't lock it. */
  2846.             debug(F111,"ttopen ttlock fails",fnam,xx);
  2847.         if (xx == -2) {        /* If lockfile says device in use, */
  2848. #ifndef NOUUCP
  2849.         debug(F111,"ttopen reading lockfile pid",flfnam,xx);
  2850.         xpid = ttrpid(flfnam);    /* Try to read pid from lockfile */
  2851.         if (xpid > -1) {    /* If we got a pid */
  2852.                     if (!quiet)
  2853.               printf("Locked by process %d\n",xpid); /* tell them. */
  2854.             ckstrncpy(lockpid,ckitoa(xpid),16);
  2855.             debug(F110,"ttopen lockpid",lockpid,0);
  2856. #ifndef NOPUSH
  2857.         } else if (flfnam[0] && !nopush) {
  2858.             extern char *DIRCMD;
  2859.             char *p = NULL;
  2860.             int x;
  2861.             x = (int)strlen(flfnam) + (int)strlen(DIRCMD) + 2;
  2862.             p = malloc(x);    /* Print a directory listing. */
  2863. /*
  2864.   Note: priv_on() won't help here, because we do not pass privs along to
  2865.   to inferior processes, in this case ls.  So if the real user does not have
  2866.   directory-listing access to the lockfile directory, this will result in
  2867.   something like "not found".  That's why we try this only as a last resort.
  2868. */
  2869.             if (p) {        /* If we got the space... */
  2870.             ckmakmsg(p,x,DIRCMD," ",flfnam,NULL);
  2871.             zsyscmd(p);    /* Get listing. */
  2872.             if (p) {    /* free the space */
  2873.                 free(p);
  2874.                 p = NULL;
  2875.             }
  2876.             }
  2877. #endif /* NOPUSH */
  2878.         }
  2879. #endif /* NOUUCP */
  2880.         return(-5);        /* Code for device in use */
  2881.         } else return(-3);        /* Access denied */
  2882.         } else lkf = 1;
  2883.     }
  2884.     /* Have lock -- now it's safe to open the device */
  2885.  
  2886.     debug(F101,"ttopen lkf","",lkf);
  2887.     debug(F101,"ttopen timo","",timo);
  2888.  
  2889.     ttotmo = 0;                /* Flag no timeout */
  2890.     if (timo > 0) {
  2891.     int xx;
  2892.     saval = signal(SIGALRM,timerh);    /* Timed, set up timer. */
  2893.     xx = alarm(timo);        /* Timed open() */
  2894.     debug(F101,"ttopen alarm","",xx);
  2895.     if (
  2896. #ifdef CK_POSIX_SIG
  2897.         sigsetjmp(sjbuf,1)
  2898. #else
  2899.         setjmp(sjbuf)
  2900. #endif /* CK_POSIX_SIG */
  2901.         ) {
  2902.         ttotmo = 1;            /* Flag timeout. */
  2903.     } else {
  2904.         ttyfd = do_open(fnam);
  2905.     }
  2906.     ttimoff();
  2907.     debug(F111,"ttopen timed ttyfd",fnam,ttyfd);
  2908.     } else {
  2909.     errno = 0;
  2910.     ttyfd = do_open(fnam);
  2911.     debug(F111,"ttopen untimed ttyfd",fnam,ttyfd);
  2912.     }
  2913.     if (ttyfd < 0) {            /* If couldn't open, fail. */
  2914.     debug(F111,"ttopen errno",fnam,errno);
  2915.     debug(F111,"ttopen xlocal","C",xlocal);
  2916.     if (xlocal == 0) {
  2917.         debug(F100,"ttopen substituting 0","",0);
  2918.         ttyfd = 0;
  2919.     } else {
  2920.         if (errno > 0 && !quiet) {
  2921.             debug(F111,"ttopen perror",fnam,errno);
  2922.         perror(fnam);        /* Print message */
  2923.         }
  2924.         if (ttunlck())                  /* Release the lock file */
  2925.           fprintf(stderr,"Warning, problem releasing lock\r\n");
  2926.     }
  2927.     }
  2928.  
  2929.     if (ttyfd < 0) {            /* ttyfd is still < 0? */
  2930. #ifdef ATT7300
  2931.     if (attmodem & DOGETY)        /* was getty(1m) running before us? */
  2932.       ongetty(ttnmsv);        /* yes, restart on tty line */
  2933.     attmodem &= ~DOGETY;        /* no phone in use, getty restored */
  2934. #else
  2935. #ifdef ACUCNTRL
  2936.         /* Should put call to priv_on() here, but that would be risky! */
  2937.     acucntrl("enable",fnam);    /* acucntrl() program. */
  2938.     /* and priv_off() here... */
  2939. #endif /* ACUNTRL */
  2940. #endif /* ATT7300 */
  2941.  
  2942.     signal(SIGINT,occt);        /* Put old Ctrl-C trap back. */
  2943.     if (errno == EACCES) {        /* Device is protected against user */
  2944.         debug(F110,"ttopen EACCESS",fnam,0); /* Return -4 */
  2945.         return(-4);
  2946.     } else return(ttotmo ? -2 : -1); /* Otherwise -2 if timeout, or -1 */
  2947.     }
  2948.  
  2949. /* Make sure it's a real tty. */
  2950.  
  2951. #ifdef Plan9
  2952.     /* take this opportunity to open the control channel */
  2953.     if (p9openttyctl(fnam) < 0)       
  2954. #else
  2955.       if (!isatty(ttyfd) && strcmp(fnam,"/dev/null"))
  2956. #endif /* Plan9 */
  2957.     {
  2958.         fprintf(stderr,"%s is not a terminal device\n",fnam);
  2959.         debug(F111,"ttopen not a tty",fnam,errno);
  2960.         if (ttunlck())        /* Release the lock file */
  2961.           fprintf(stderr,"Warning, problem releasing lock\r\n");
  2962.         close(ttyfd);
  2963.         ttyfd = -1;
  2964.         wasclosed = 1;
  2965.         signal(SIGINT,occt);
  2966.         return(-1);
  2967.     }
  2968.  
  2969. #ifdef aegis
  2970.     /*
  2971.       Apollo C runtime claims that console pads are tty devices, which
  2972.       is reasonable, but they aren't any good for packet transfer.
  2973.     */
  2974.     ios_$inq_type_uid((short)ttyfd, ttyuid, st);
  2975.     if (st.all != status_$ok) {
  2976.     fprintf(stderr, "problem getting tty object type: ");
  2977.     error_$print(st);
  2978.     } else if (ttyuid != sio_$uid) {    /* Reject non-SIO lines */
  2979.     close(ttyfd); ttyfd = -1;
  2980.     wasclosed = 1;
  2981.     errno = ENOTTY; perror(fnam);
  2982.     signal(SIGINT,occt);
  2983.     return(-1);
  2984.     }
  2985. #endif /* aegis */
  2986.  
  2987.     sigint_ign = (occt == SIG_IGN) ? 1 : 0;
  2988.  
  2989.     ckstrncpy(ttnmsv,ttname,DEVNAMLEN);    /* Keep copy of name locally. */
  2990.  
  2991. /* Caller wants us to figure out if line is controlling tty */
  2992.  
  2993.     if (*lcl < 0) {
  2994.     char * s;
  2995.         if (strcmp(fnam,CTTNAM) == 0) { /* "/dev/tty" always remote */
  2996.             xlocal = 0;
  2997.         debug(F111,"ttopen fnam=CTTNAM",fnam,xlocal);
  2998.         } else if (strcmp(fnam,cttnam) == 0) {
  2999.             xlocal = 0;
  3000.         debug(F111,"ttopen fnam=cttnam",fnam,xlocal);
  3001.     } else if (cttnam[0]) {
  3002. #ifdef BEBOX_DR7
  3003.             s = ttnmsv;            /* ttyname() is broken */
  3004. #else
  3005.             s = ttyname(ttyfd);         /* Get real name of ttname. */
  3006. #endif /* BEBOX_DR7 */
  3007.         if (!s) s = "";
  3008.         if (*s)
  3009.           xlocal = ((strncmp(s,cttnam,DEVNAMLEN) == 0) ? 0 : 1);
  3010.         else
  3011.           xlocal = 1;
  3012.             debug(F111,"ttopen ttyname(ttyfd) xlocal",s,xlocal);
  3013.         }
  3014.     }
  3015.  
  3016. #ifndef NOFDZERO
  3017. /* Note, the following code was added so that Unix "idle-line" snoopers */
  3018. /* would not think Kermit was idle when it was transferring files, and */
  3019. /* maybe log people out. */
  3020.     if (xlocal == 0) {            /* Remote mode */
  3021.     if (fdflag == 0) {        /* Standard i/o is not redirected */
  3022.         debug(F100,"ttopen setting ttyfd = 0","",0);
  3023. #ifdef LYNXOS
  3024.         /* On Lynx OS, fd 0 is open for read only. */
  3025.         dup2(ttyfd,0);
  3026. #endif /* LYNXOS */
  3027.         close(ttyfd);        /* Use file descriptor 0 */
  3028.         ttyfd = 0;
  3029.     } else {            /* Standard i/o is redirected */
  3030.         debug(F101,"ttopen stdio redirected","",ttyfd);
  3031.     }
  3032.     }
  3033. #endif /* NOFDZERO */
  3034. #endif /* OPENFIRST */
  3035.  
  3036. /* Got the line, now set the desired value for local. */
  3037.  
  3038.     if (*lcl != 0) *lcl = xlocal;
  3039.  
  3040. /* Some special stuff for v7... */
  3041.  
  3042. #ifdef  V7
  3043. #ifndef MINIX
  3044.     if (kmem[TTY] < 0) {        /*  If open, then skip this.  */
  3045.     qaddr[TTY] = initrawq(ttyfd);   /* Init the queue. */
  3046.     if ((kmem[TTY] = open("/dev/kmem", 0)) < 0) {
  3047.         fprintf(stderr, "Can't read /dev/kmem in ttopen.\n");
  3048.         perror("/dev/kmem");
  3049.         exit(1);
  3050.     }
  3051.     }
  3052. #endif /* !MINIX */
  3053. #endif /* V7 */
  3054.  
  3055. /* No failure returns after this point */
  3056.  
  3057. #ifdef ultrix
  3058.     ioctl(ttyfd, TIOCMODEM, &temp);
  3059. #ifdef TIOCSINUSE
  3060.     if (xlocal && ioctl(ttyfd, TIOCSINUSE, NULL) < 0) {
  3061.     if (!quiet)
  3062.       perror(fnam);
  3063.     }
  3064. #endif /* TIOCSINUSE */
  3065. #endif /* ultrix */
  3066.  
  3067. /* Get tty device settings  */
  3068.  
  3069. #ifdef BSD44ORPOSIX            /* POSIX */
  3070.     tcgetattr(ttyfd,&ttold);
  3071.     debug(F101,"ttopen tcgetattr ttold.c_lflag","",ttold.c_lflag);
  3072.     tcgetattr(ttyfd,&ttraw);
  3073.     debug(F101,"ttopen tcgetattr ttraw.c_lflag","",ttraw.c_lflag);
  3074.     tcgetattr(ttyfd,&tttvt);
  3075.     debug(F101,"ttopen tcgetattr tttvt.c_lflag","",tttvt.c_lflag);
  3076. #else                    /* BSD, V7, and all others */
  3077. #ifdef ATTSV                /* AT&T UNIX */
  3078.     ioctl(ttyfd,TCGETA,&ttold);
  3079.     debug(F101,"ttopen ioctl TCGETA ttold.c_lflag","",ttold.c_lflag);
  3080.     ioctl(ttyfd,TCGETA,&ttraw);
  3081.     ioctl(ttyfd,TCGETA,&tttvt);
  3082. #else
  3083. #ifdef BELLV10
  3084.     ioctl(ttyfd,TIOCGETP,&ttold);
  3085.     debug(F101,"ttopen BELLV10 ttold.sg_flags","",ttold.sg_flags);
  3086.     ioctl(ttyfd,TIOCGDEV,&tdold);
  3087.     debug(F101,"ttopen BELLV10 tdold.flags","",tdold.flags);
  3088. #else
  3089.     gtty(ttyfd,&ttold);
  3090.     debug(F101,"ttopen gtty ttold.sg_flags","",ttold.sg_flags);
  3091. #endif /* BELLV10 */
  3092.  
  3093. #ifdef sony_news            /* Sony NEWS */
  3094.     if (ioctl(ttyfd,TIOCKGET,&km_ext) < 0) { /* Get console Kanji mode */
  3095.     perror("ttopen error getting Kanji mode");
  3096.     debug(F101,"ttopen error getting Kanji mode","",0);
  3097.     km_ext = -1;            /* Make sure this stays undefined. */
  3098.     }
  3099. #endif /* sony_news */
  3100.  
  3101. #ifdef TIOCGETC
  3102.     debug(F100,"ttopen TIOCGETC","",0);
  3103.     tcharf = 0;                /* In remote mode, also get */
  3104.     if (xlocal == 0) {            /* special characters */
  3105.     if (ioctl(ttyfd,TIOCGETC,&tchold) < 0) {
  3106.         debug(F100,"ttopen TIOCGETC failed","",0);
  3107.     } else {
  3108.         tcharf = 1;            /* It worked. */
  3109.         ioctl(ttyfd,TIOCGETC,&tchnoi); /* Get another copy */
  3110.         debug(F100,"ttopen TIOCGETC ok","",0);
  3111.     }
  3112.     }
  3113. #else
  3114.     debug(F100,"ttopen TIOCGETC not defined","",0);
  3115. #endif /* TIOCGETC */
  3116.  
  3117. #ifdef TIOCGLTC
  3118.     debug(F100,"ttopen TIOCGLTC","",0);
  3119.     ltcharf = 0;            /* In remote mode, also get */
  3120.     if (xlocal == 0) {            /* local special characters */
  3121.     if (ioctl(ttyfd,TIOCGLTC,<chold) < 0) {
  3122.         debug(F100,"ttopen TIOCGLTC failed","",0);
  3123.     } else {
  3124.         ltcharf = 1;        /* It worked. */
  3125.         ioctl(ttyfd,TIOCGLTC,<chnoi); /* Get another copy */
  3126.         debug(F100,"ttopen TIOCGLTC ok","",0);
  3127.     }
  3128.     }
  3129. #else
  3130.     debug(F100,"ttopen TIOCGLTC not defined","",0);
  3131. #endif /* TIOCGLTC */
  3132.  
  3133. #ifdef TIOCLGET
  3134.     debug(F100,"ttopen TIOCLGET","",0);
  3135.     lmodef = 0;
  3136.     if (ioctl(ttyfd,TIOCLGET,&lmode) < 0) {
  3137.     debug(F100,"ttopen TIOCLGET failed","",0);
  3138.     } else {
  3139.     lmodef = 1;
  3140.     debug(F100,"ttopen TIOCLGET ok","",0);
  3141.     }
  3142. #endif /* TIOCLGET */
  3143.  
  3144. #ifdef BELLV10
  3145.     ioctl(ttyfd,TIOCGETP,&ttraw);
  3146.     ioctl(ttyfd,TIOCGETP,&tttvt);
  3147. #else
  3148.     gtty(ttyfd,&ttraw);                 /* And a copy of it for packets*/
  3149.     gtty(ttyfd,&tttvt);                 /* And one for virtual tty service */
  3150. #endif /* BELLV10 */
  3151.  
  3152. #endif /* ATTSV */
  3153. #endif /* BSD44ORPOSIX */
  3154.  
  3155. /* Section for changing line discipline.  It's restored in ttres(). */
  3156.  
  3157. #ifdef AIXRS
  3158. #ifndef AIX41
  3159.     { union txname ld_name; int ld_idx = 0;
  3160.       ttld = 0;
  3161.         do {
  3162.         ld_name.tx_which = ld_idx++;
  3163.         ioctl(ttyfd, TXGETCD, &ld_name);
  3164.       if (!strncmp(ld_name.tx_name, "rts", 3))
  3165.           ttld |= 1;
  3166.         } while (*ld_name.tx_name);
  3167.         debug(F101,"AIX line discipline","",ttld);
  3168.       }
  3169. #endif /* AIX41 */
  3170. #endif /* AIXRS */
  3171.  
  3172. #ifdef BSD41
  3173. /* For 4.1BSD only, force "old" tty driver, new one botches TANDEM. */
  3174.     { int k;
  3175.       ioctl(ttyfd, TIOCGETD, &ttld);    /* Get and save line discipline */
  3176.       debug(F101,"4.1bsd line discipline","",ttld);
  3177.       k = OTTYDISC;            /* Switch to "old" discipline */
  3178.       k = ioctl(ttyfd, TIOCSETD, &k);
  3179.       debug(F101,"4.1bsd tiocsetd","",k);
  3180.     }
  3181. #endif /* BSD41 */
  3182.  
  3183. #ifdef aegis
  3184.     /* This was previously done before the last two TCGETA or gtty above,
  3185.      * in both the ATTSV and not-ATTSV case.  If it is not okay to have only
  3186.      * one copy if it here instead, give us a shout!
  3187.      */
  3188.     sio_$control((short)ttyfd, sio_$raw_nl, false, st);
  3189.     if (xlocal) {       /* ignore breaks from local line */
  3190.         sio_$control((short)ttyfd, sio_$int_enable, false, st);
  3191.         sio_$control((short)ttyfd, sio_$quit_enable, false, st);
  3192.     }
  3193. #endif /* aegis */
  3194.  
  3195. #ifdef VXVE
  3196.     ttraw.c_line = 0;                   /* STTY line 0 for VX/VE */
  3197.     tttvt.c_line = 0;                   /* STTY line 0 for VX/VE */
  3198.     ioctl(ttyfd,TCSETA,&ttraw);
  3199. #endif /* vxve */
  3200.  
  3201. /* If O_NDELAY was used during open(), then remove it now. */
  3202.  
  3203. #ifdef O_NDELAY
  3204.     debug(F100,"ttopen O_NDELAY","",0);
  3205.     if (xlocal > 0) {
  3206.       if (fcntl(ttyfd, F_GETFL, 0) & O_NDELAY) {
  3207.     debug(F100,"ttopen fcntl O_NDELAY","",0);
  3208. #ifndef aegis
  3209.     if (fcntl(ttyfd,F_SETFL, fcntl(ttyfd, F_GETFL, 0) & ~O_NDELAY) < 0) {
  3210.         debug(F100,"ttopen fcntl failure to unset O_NDELAY","",0);
  3211.         perror("Can't unset O_NDELAY");
  3212.     }
  3213. #endif /* aegis */
  3214.     /* Some systems, notably Xenix (don't know how common this is in
  3215.      * other systems), need special treatment to get rid of the O_NDELAY
  3216.      * behaviour on read() with respect to carrier presence (i.e. read()
  3217.      * returning 0 when carrier absent), even though the above fcntl()
  3218.      * is enough to make read() wait for input when carrier is present.
  3219.      * This magic, in turn, requires CLOCAL for working when the carrier
  3220.      * is absent. But if xlocal == 0, presumably you already have CLOCAL
  3221.      * or you have a carrier, otherwise you wouldn't be running this.
  3222.      */
  3223.     debug(F101,"ttopen xlocal","",xlocal);
  3224. #ifdef ATTSV
  3225. #ifdef BSD44ORPOSIX
  3226. #ifdef COMMENT                /* 12 Aug 1997 */
  3227. #ifdef __bsdi__
  3228.     if (xlocal)
  3229.       ttraw.c_cflag |= CLOCAL;
  3230. #else
  3231. #ifdef __FreeBSD__
  3232.     if (xlocal)
  3233.       ttraw.c_cflag |= CLOCAL;
  3234. #endif /* __FreeBSD__ */
  3235. #endif /* __bsdi__ */
  3236. #else /* Not COMMENT */
  3237. #ifdef CLOCAL
  3238.     if (xlocal)            /* Unset this if it's defined. */
  3239.       ttraw.c_cflag |= CLOCAL;
  3240. #endif /* CLOCAL */
  3241. #endif /* COMMENT */
  3242.     debug(F101,"ttopen BSD44ORPOSIX calling tcsetattr","",TCSADRAIN);
  3243.     if (tcsetattr(ttyfd, TCSADRAIN, &ttraw) < 0) {
  3244.         debug(F100,"ttopen POSIX tcseattr fails","",0);
  3245.         perror("tcsetattr");
  3246.     }
  3247. #else /* !BSD44ORPOSIX */
  3248.     if (xlocal) {
  3249.         ttraw.c_cflag |= CLOCAL;
  3250.         debug(F100,"ttopen calling ioctl(TCSETA)","",0);
  3251.         errno = 0;
  3252.         if (ioctl(ttyfd, TCSETA, &ttraw) < 0) {
  3253.                 debug(F101,"ttopen ioctl(TCSETA) fails","",errno);
  3254.                 perror("ioctl(TCSETA)");
  3255.             }
  3256.     }
  3257. #endif /* BSD44ORPOSIX */
  3258. #endif /* ATTSV */
  3259. #ifndef NOCOTFMC /* = NO Close(Open()) To Force Mode Change */
  3260. /* Reportedly lets uugetty grab the device in SCO UNIX 3.2 / XENIX 2.3 */
  3261.     debug(F100,"ttopen executing close/open","",0);
  3262.     close( priv_opn(fnam, O_RDWR) ); /* Magic to force change. */
  3263. #endif /* NOCOTFMC */
  3264.       }
  3265.     }
  3266. #endif /* O_NDELAY */
  3267.  
  3268. /* Instruct the system how to treat the carrier, and set a few other tty
  3269.  * parameters.
  3270.  *
  3271.  * This also undoes the temporary setting of CLOCAL that may have been done
  3272.  * for the close(open()) above (except in Xenix).  Also throw in ~ECHO, to
  3273.  * prevent the other end of the line from sitting there talking to itself,
  3274.  * producing garbage when the user performs a connect.
  3275.  *
  3276.  * SCO Xenix unfortunately seems to ignore the actual state of CLOCAL.
  3277.  * Now it thinks CLOCAL is always on. It seems the only real solution for
  3278.  * Xenix is to switch between the lower and upper case device names.
  3279.  *
  3280.  * This section may at some future time expand into setting a complete
  3281.  * collection of tty parameters, or call a function shared with ttpkt()/
  3282.  * ttvt() that does so.  On the other hand, the initial parameters are not
  3283.  * that important, since ttpkt() or ttvt() should always fix that before
  3284.  * any communication is done.  Well, we'll see...
  3285.  */
  3286.     if (xlocal) {
  3287.         curcarr = -2;
  3288.     debug(F100,"ttopen calling carrctl","",0);
  3289.     carrctl(&ttraw, ttcarr == CAR_ON);
  3290.     debug(F100,"ttopen carrctl ok","",0);
  3291.  
  3292. #ifdef COHERENT
  3293. #define SVORPOSIX
  3294. #endif /* COHERENT */
  3295.  
  3296. #ifdef SVORPOSIX
  3297.     ttraw.c_lflag &= ~ECHO;
  3298.     ttold.c_lflag &= ~ECHO;
  3299. #ifdef BSD44ORPOSIX
  3300.     y = tcsetattr(ttyfd, TCSADRAIN, &ttraw);
  3301.     debug(F101,"ttopen tcsetattr","",y);
  3302. #else
  3303.     y = ioctl(ttyfd, TCSETA, &ttraw);
  3304.     debug(F100,"ttopen ioctl","",y);
  3305. #endif /* BSD44ORPOSIX */
  3306.  
  3307. #else /* BSD, etc */
  3308.     ttraw.sg_flags &= ~ECHO;
  3309.     ttold.sg_flags &= ~ECHO;
  3310. #ifdef BELLV10
  3311.     y = ioctl(ttyfd,TIOCSETP,&ttraw);
  3312.     debug(F100,"ttopen ioctl","",y);
  3313. #else
  3314.     y = stty(ttyfd,&ttraw);
  3315.     debug(F100,"ttopen stty","",y);
  3316. #endif /* BELLV10 */
  3317. #endif /* SVORPOSIX */
  3318.  
  3319. #ifdef COHERENT
  3320. #undef SVORPOSIX
  3321. #endif /* COHERENT */
  3322.  
  3323.     /* ttflui(); */  /*  This fails for some reason.  */
  3324.     }
  3325.  
  3326.     /* Get current speed */
  3327.  
  3328. #ifndef BEBOX
  3329.     ttspeed = ttgspd();
  3330. #else
  3331.     ttspeed = 19200;
  3332. #endif /* !BEBOX */
  3333.     debug(F101,"ttopen ttspeed","",ttspeed);
  3334.  
  3335.     /* Done, make entries in debug log, restore Ctrl-C trap, and return. */
  3336.  
  3337.     debug(F101,"ttopen ttyfd","",ttyfd);
  3338.     debug(F101,"ttopen *lcl","",*lcl);
  3339.     debug(F111,"ttopen lock file",flfnam,lkf);
  3340.     signal(SIGINT,occt);
  3341.     sigint_ign = (occt == SIG_IGN) ? 1 : 0;
  3342.     gotsigs = 0;
  3343.     return(0);
  3344. }
  3345.  
  3346.  
  3347. /*  D O _ O P E N  --  Do the right kind of open() call for the tty. */
  3348.  
  3349. int
  3350. do_open(ttname) char *ttname; {
  3351.     int flags;
  3352.  
  3353. #ifdef QNX6
  3354.     /* O_NONBLOCK on /dev/tty makes open() fail */
  3355.     return(priv_opn(ttname, O_RDWR |
  3356.             (
  3357.              ((int)strcmp(ttname,"/dev/tty") == 0) ?
  3358.              0 :
  3359.              (ttcarr != CAR_ON) ? O_NONBLOCK : 0)
  3360.             )
  3361.        ); 
  3362. #else  /* !QNX6 */
  3363.  
  3364. #ifndef    O_NDELAY            /* O_NDELAY not defined */
  3365.     return(priv_opn(ttname,2));
  3366. #else                    /* O_NDELAY defined */
  3367.  
  3368. #ifdef ATT7300
  3369. /*
  3370.  Open comms line without waiting for carrier so initial call does not hang
  3371.  because state of "modem" is likely unknown at the initial call  -jrd.
  3372.  If this is needed for the getty stuff to work, and the open would not work
  3373.  without O_NDELAY when getty is still on, then this special case is ok.
  3374.  Otherwise, get rid of it. -ske
  3375. */
  3376.     return(priv_opn(ttname, O_RDWR | O_NDELAY));
  3377.  
  3378. #else    /* !ATT7300 */
  3379.  
  3380.     /* Normal case. Use O_NDELAY according to SET CARRIER. See ttscarr(). */
  3381.     flags = O_RDWR;
  3382.     debug(F101,"do_open xlocal","",xlocal);
  3383.     debug(F111,"do_open flags A",ttname,flags);
  3384.     if (xlocal && (ttcarr != CAR_ON))
  3385.       flags |= O_NDELAY;
  3386.     debug(F111,"do_open flags B",ttname,flags);
  3387.     return(priv_opn(ttname, flags));
  3388. #endif /* !ATT7300 */
  3389. #endif /* O_NDELAY */
  3390. #endif /* QNX6 */
  3391. }
  3392.  
  3393. /*  T T C L O S  --  Close the TTY, releasing any lock.  */
  3394.  
  3395. static int ttc_state = 0;        /* ttclose() state */
  3396. static char * ttc_nam[] = { "setup", "hangup", "reset", "close" };
  3397.  
  3398. int
  3399. ttclos(foo) int foo; {            /* Arg req'd for signal() prototype */
  3400.     int xx, x = 0;
  3401.     extern int exithangup;
  3402.  
  3403.     debug(F101,"ttclos ttyfd","",ttyfd);
  3404.     debug(F101,"ttclos netconn","",netconn);
  3405.     debug(F101,"ttclos xlocal","",xlocal);
  3406. #ifdef NOFDZERO
  3407.     debug(F100,"ttclos NOFDZERO","",0);
  3408. #endif /* NOFDZERO */
  3409.  
  3410. #ifdef COMMENT
  3411. #ifdef TTLEBUF
  3412.     le_init();                /* No need for any of this */
  3413. #endif /* TTLEBUF */
  3414. #endif /* COMMENT */
  3415.  
  3416.     if (ttyfd < 0)            /* Wasn't open. */
  3417.       return(0);
  3418.  
  3419.     if (ttfdflg)            /* If we inherited ttyfd from */
  3420.       return(0);            /* another process, don't close it. */
  3421.  
  3422.     tvtflg = 0;                /* (some day get rid of this...) */
  3423.     gotsigs = 0;
  3424.  
  3425. #ifdef IKSD
  3426.     if (inserver) {
  3427. #ifdef TNCODE
  3428.           tn_push();                    /* Place any waiting data into input*/
  3429.           tn_sopt(DO,TELOPT_LOGOUT);    /* Send LOGOUT option before close */
  3430.           TELOPT_UNANSWERED_DO(TELOPT_LOGOUT) = 1;
  3431.           tn_reset();                   /* The Reset Telnet Option table.  */
  3432. #endif /* TNCODE */
  3433. #ifdef CK_SSL
  3434.       if (ssl_active_flag) {
  3435.           if (ssl_debug_flag)
  3436.         BIO_printf(bio_err,"calling SSL_shutdown(ssl)\n");
  3437.           SSL_shutdown(ssl_con);
  3438.           SSL_free(ssl_con);
  3439.           ssl_con = NULL;
  3440.           ssl_active_flag = 0;
  3441.       }
  3442.       if (tls_active_flag) {
  3443.           if (ssl_debug_flag)
  3444.         BIO_printf(bio_err,"calling SSL_shutdown(tls)\n");
  3445.           SSL_shutdown(tls_con);
  3446.           SSL_free(tls_con);
  3447.           tls_con = NULL;
  3448.           tls_active_flag = 0;
  3449.       }
  3450. #endif /* CK_SSL */
  3451.     }
  3452. #endif /* IKSD */
  3453. #ifdef NETCMD
  3454.     if (ttpipe) {            /* We've been using a pipe */
  3455.     /* ttpipe = 0; */
  3456.     if (ttpid > 0) {
  3457.         int wstat;
  3458.         int statusp;
  3459.         close(fdin);        /* Close these. */
  3460.         close(fdout);
  3461.         fdin = fdout = -1;
  3462.         kill(ttpid,1);        /* Kill fork with SIGHUP */
  3463.         while (1) {
  3464.         wstat = wait(&statusp);
  3465.         if (wstat == ttpid || wstat == -1)
  3466.           break;
  3467.         pexitstat = (statusp & 0xff) ? statusp : statusp >> 8;
  3468.         }
  3469.         ttpid = 0;
  3470.     }
  3471.     netconn = 0;
  3472.     wasclosed = 1;
  3473.     ttyfd = -1;
  3474.     return(0);
  3475.     }
  3476. #endif /* NETCMD */
  3477. #ifdef NETPTY
  3478.     if (ttpty) {
  3479. #ifndef NODOPTY
  3480.         end_pty();
  3481. #endif /* NODOPTY */
  3482.         close(ttyfd);
  3483.     netconn = 0;
  3484.     wasclosed = 1;
  3485.         ttpty = 0;
  3486.         ttyfd = -1;
  3487.         return(0);
  3488.     }
  3489. #endif /* NETPTY */
  3490.  
  3491. #ifdef    NETCONN
  3492.     if (netconn) {            /* If it's a network connection. */
  3493.     debug(F100,"ttclos closing net","",0);
  3494.     netclos();            /* Let the network module close it. */
  3495.     netconn = 0;            /* No more network connection. */
  3496.     debug(F101,"ttclos ttyfd after netclos","",ttyfd); /* Should be -1 */
  3497.     return(0);
  3498.     }
  3499. #endif    /* NETCONN */
  3500.  
  3501.     if (xlocal) {            /* We're closing a SET LINE device */
  3502. #ifdef FT21                /* Fortune 2.1-specific items ... */
  3503.     ioctl(ttyfd,TIOCHPCL, NULL);
  3504. #endif /* FT21 */
  3505. #ifdef ultrix                /* Ultrix-specific items ... */
  3506. #ifdef TIOCSINUSE
  3507.     /* Unset the INUSE flag that we set in ttopen() */
  3508.     ioctl(ttyfd, TIOCSINUSE, NULL);
  3509. #endif /* TIOCSINUSE */
  3510.     ioctl(ttyfd, TIOCNMODEM, &x);
  3511. #ifdef COMMENT
  3512.     /* What was this? */
  3513.     ioctl(ttyfd, TIOCNCAR, NULL);
  3514. #endif /* COMMENT */
  3515. #endif /* ultrix */
  3516.     }
  3517.  
  3518.     /* This is to prevent us from sticking in tthang() or close(). */
  3519.  
  3520. #ifdef O_NDELAY
  3521. #ifndef aegis
  3522.     if (ttyfd > 0) {            /* But skip it on stdin. */
  3523.     debug(F100,"ttclos setting O_NDELAY","",0);
  3524.     x = fcntl(ttyfd,F_SETFL,fcntl(ttyfd,F_GETFL, 0)|O_NDELAY);
  3525. #ifdef DEBUG
  3526.     if (deblog && x == -1) {
  3527.         perror("Warning - Can't set O_NDELAY");
  3528.         debug(F101,"ttclos fcntl failure to set O_NDELAY","",x);
  3529.     }
  3530. #endif /* DEBUG */
  3531.     }
  3532. #endif /* aegis */
  3533. #endif /* O_NDELAY */
  3534.  
  3535.     x = 0;
  3536.     ttc_state = 0;
  3537.     if (xlocal
  3538. #ifdef NOFDZERO
  3539.     || ttyfd > 0
  3540. #endif /* NOFDZERO */
  3541.     ) {
  3542.     saval = signal(SIGALRM,xtimerh); /* Enable timer interrupt. */
  3543.     xx = alarm(8);            /* Allow 8 seconds. */
  3544.     debug(F101,"ttclos alarm","",xx);
  3545.     if (
  3546. #ifdef CK_POSIX_SIG
  3547.         sigsetjmp(sjbuf,1)
  3548. #else
  3549.         setjmp(sjbuf)
  3550. #endif /* CK_POSIX_SIG */
  3551.         ) {                /* Timer went off? */
  3552.         x = -1;
  3553. #ifdef DEBUG
  3554.         debug(F111,"ttclos ALARM TRAP errno",ckitoa(ttc_state),errno);
  3555.         printf("ttclos() timeout: %s\n", ttc_nam[ttc_state]);
  3556. #endif /* DEBUG */
  3557.     }
  3558.     /* Hang up the device (drop DTR) */
  3559.  
  3560.     errno = 0;
  3561.     debug(F111,"ttclos A",ckitoa(x),ttc_state);
  3562.     if (ttc_state < 1) {
  3563.         ttc_state = 1;
  3564.         debug(F101,"ttclos exithangup","",exithangup);
  3565.         if (exithangup) {
  3566.         alarm(8);        /* Re-arm the timer */
  3567.         debug(F101,"ttclos calling tthang()","",x);
  3568.         x = tthang();        /* Hang up first, then... */
  3569.         debug(F101,"ttclos tthang()","",x);
  3570.         }
  3571.     }
  3572.     /* Put back device modes as we found them */
  3573.  
  3574.     errno = 0;
  3575.     debug(F111,"ttclos B",ckitoa(x),ttc_state);
  3576.     if (ttc_state < 2) {
  3577.         ttc_state = 2;
  3578.         /* Don't try to mess with tty modes if tthang failed() */
  3579.         /* since it probably won't work. */
  3580.         if (x > -1) {
  3581.         debug(F101,"ttclos calling ttres()","",x);
  3582.         signal(SIGALRM,xtimerh); /* Re-enable the alarm. */
  3583.         alarm(8);        /* Re-arm the timer */
  3584.         x = ttres();        /* Reset device modes. */
  3585.         debug(F101,"ttclos ttres()","",x);
  3586.         alarm(0);
  3587.         }
  3588.     }
  3589.     /* Close the device */
  3590.  
  3591.     errno = 0;
  3592.     debug(F101,"ttclos C","",ttc_state);
  3593.     if (ttc_state < 3) {
  3594.         ttc_state = 3;
  3595.         errno = 0;
  3596.         debug(F101,"ttclos calling close","",x);
  3597.         signal(SIGALRM,xtimerh);    /* Re-enable alarm. */
  3598.         alarm(8);            /* Re-arm the timer */
  3599.         x = close(ttyfd);        /* Close the device. */
  3600.         debug(F101,"ttclos close()","",x);
  3601.         if (x > -1)
  3602.           ttc_state = 3;
  3603.     }
  3604.     debug(F101,"ttclos D","",ttc_state);
  3605.     ttimoff();            /* Turn off timer. */
  3606.     if (x < 0) {
  3607.         printf("?WARNING - close failed: %s\n",ttnmsv);
  3608. #ifdef DEBUG
  3609.         if (deblog) {
  3610.         printf("errno = %d\n", errno);
  3611.         debug(F101,"ttclos failed","",errno);
  3612.         }
  3613. #endif /* DEBUG */
  3614.     }
  3615.     /* Unlock after closing but before any getty mumbo jumbo */
  3616.  
  3617.     debug(F100,"ttclos about to call ttunlck","",0);
  3618.         if (ttunlck())                  /* Release uucp-style lock */
  3619.       fprintf(stderr,"Warning, problem releasing lock\r\n");
  3620.     }
  3621.  
  3622. /* For bidirectional lines, restore getty if it was there before. */
  3623.  
  3624. #ifdef ACUCNTRL                /* 4.3BSD acucntrl() method. */
  3625.     if (xlocal) {
  3626.     debug(F100,"ttclos ACUCNTRL","",0);
  3627.     acucntrl("enable",ttnmsv);    /* Enable getty on the device. */
  3628.     }
  3629. #else
  3630. #ifdef ATT7300                /* ATT UNIX PC (3B1, 7300) method. */
  3631.     if (xlocal) {
  3632.     debug(F100,"ttclos ATT7300 ongetty","",0);
  3633.     if (attmodem & DOGETY)        /* Was getty(1m) running before us? */
  3634.       ongetty(ttnmsv);        /* Yes, restart getty on tty line */
  3635.     attmodem &= ~DOGETY;        /* No phone in use, getty restored */
  3636.     }
  3637. #endif /* ATT7300 */
  3638. #endif /* System-dependent getty-restoring methods */
  3639.  
  3640. #ifdef sony_news
  3641.     km_ext = -1;            /* Invalidate device's Kanji-mode */
  3642. #endif /* sony_news */
  3643.  
  3644.     ttyfd = -1;                         /* Invalidate the file descriptor. */
  3645.     wasclosed = 1;
  3646.     debug(F100,"ttclos done","",0);
  3647.     return(0);
  3648. }
  3649.  
  3650. /*  T T H A N G  --  Hangup phone line or network connection.  */
  3651. /*
  3652.   Returns:
  3653.   0 if it does nothing.
  3654.   1 if it believes that it hung up successfully.
  3655.  -1 if it believes that the hangup attempt failed.
  3656. */
  3657.  
  3658. #define HUPTIME 500            /* Milliseconds for hangup */
  3659.  
  3660. #ifdef COMMENT
  3661. /* The following didn't work but TIOCSDTR does work */
  3662. #ifdef UNIXWARE
  3663. /* Define HUP_POSIX to force non-POSIX builds to use the POSIX hangup method */
  3664. #ifndef POSIX                /* Such as Unixware 1.x, 2.x */
  3665. #ifndef HUP_POSIX
  3666. #define HUP_POSIX
  3667. #endif /* HUP_POSIX */
  3668. #endif /* POSIX */
  3669. #endif /* UNIXWARE */
  3670. #endif /* COMMENT */
  3671.  
  3672. #ifndef USE_TIOCSDTR
  3673. #ifdef __NetBSD__
  3674. /* Because the POSIX method (set output speed to 0) doesn't work in NetBSD */
  3675. #ifdef TIOCSDTR
  3676. #ifdef TIOCCDTR
  3677. #define USE_TIOCSDTR
  3678. #endif /* TIOCCDTR */
  3679. #endif /* TIOCSDTR */
  3680. #endif /* __NetBSD__ */
  3681. #endif /* USE_TIOCSDTR */
  3682.  
  3683. #ifndef HUP_CLOSE_POSIX
  3684. #ifdef OU8
  3685. #define HUP_CLOSE_POSIX
  3686. #else
  3687. #ifdef CK_SCOV5
  3688. #define HUP_CLOSE_POSIX
  3689. #endif /* CK_SCOV5 */
  3690. #endif /* OU8 */
  3691. #endif /* HUP_CLOSE_POSIX */
  3692.  
  3693. #ifdef NO_HUP_CLOSE_POSIX
  3694. #ifdef HUP_CLOSE_POSIX
  3695. #undef HUP_CLOSE_POSIX
  3696. #endif /* HUP_CLOSE_POSIX */
  3697. #endif /* NO_HUP_CLOSE_POSIX */
  3698.  
  3699. int
  3700. tthang() {
  3701. #ifdef NOLOCAL
  3702.     return(0);
  3703. #else
  3704.     int x = 0;                /* Sometimes used as return code. */
  3705. #ifndef POSIX
  3706.     int z;                /* worker */
  3707. #endif /* POSIX */
  3708.  
  3709. #ifdef COHERENT
  3710. #define SVORPOSIX
  3711. #endif /* COHERENT */
  3712.  
  3713. #ifdef SVORPOSIX            /* AT&T, POSIX, HPUX declarations. */
  3714.     int spdsav;                /* for saving speed */
  3715. #ifdef HUP_POSIX
  3716.     int spdsavi;
  3717. #else
  3718. #ifdef BSD44ORPOSIX
  3719.     int spdsavi;
  3720. #endif /* BSD44ORPOSIX */
  3721. #endif /* HUP_POSIX */
  3722. #ifdef HPUX
  3723. /*
  3724.   Early versions of HP-UX omitted the mflag typedef.  If you get complaints
  3725.   about it, just change it to long (or better still, unsigned long).
  3726. */
  3727.     mflag
  3728.       dtr_down = 00000000000,
  3729.       modem_rtn,
  3730.       modem_sav;
  3731.     char modem_state[64];
  3732. #endif /* HPUX */
  3733.     int flags;                /* fcntl flags */
  3734.     unsigned short ttc_save;
  3735. #endif /* SVORPOSIX */
  3736.  
  3737.     if (ttyfd < 0) return(0);           /* Don't do this if not open  */
  3738.     if (xlocal < 1) return(0);        /* Don't do this if not local */
  3739.  
  3740. #ifdef NETCMD
  3741.     if (ttpipe)
  3742.       return((ttclos(0) < 0) ? -1 : 1);
  3743. #endif /* NETCMD */
  3744. #ifdef NETPTY
  3745.     if (ttpty)
  3746.       return((ttclos(0) < 0) ? -1 : 1);
  3747. #endif /* NETPTY */
  3748. #ifdef NETCONN
  3749.     if (netconn) {            /* Network connection. */
  3750. #ifdef TN_COMPORT
  3751.         if (istncomport()) {
  3752.             int rc = tnc_set_dtr_state(0);
  3753.             if (rc >= 0) {
  3754.                 msleep(HUPTIME);
  3755.                 rc = tnc_set_dtr_state(1);
  3756.             }
  3757.             return(rc >= 0 ? 1 : -1);
  3758.         } else
  3759. #endif /* TN_COMPORT */
  3760.       return((netclos() < 0) ? -1 : 1); /* Just close it. */
  3761.   }
  3762. #endif /* NETCONN */
  3763.  
  3764. /* From here down, we handle real tty devices. */
  3765. #ifdef HUP_POSIX
  3766. /*
  3767.   e.g. for Unixware 2, where we don't have a full POSIX build, we
  3768.   still have to use POSIX-style hangup.  Thus the duplication of this
  3769.   and the next case, the only difference being we use a local termios
  3770.   struct here, since a different model is used elsewhere.
  3771.  
  3772.   NO LONGER USED as of C-Kermit 8.0 -- it turns out that this method,
  3773.   even though it compiles and executes without error, doesn't actually
  3774.   work (i.e. DTR does not drop), whereas the TIOCSDTR method works just fine,
  3775. */
  3776.     {
  3777.     struct termios ttcur;
  3778.     int x;
  3779.     debug(F100,"tthang HUP_POSIX style","",0);
  3780.     x = tcgetattr(ttyfd, &ttcur);    /* Get current attributes */
  3781.     debug(F111,"tthang tcgetattr",ckitoa(errno),x);
  3782.     if (x < 0) return(-1);
  3783.     spdsav = cfgetospeed(&ttcur);    /* Get current speed */
  3784.     debug(F111,"tthang cfgetospeed",ckitoa(errno),spdsav);
  3785.     spdsavi = cfgetispeed(&ttcur);    /* Get current speed */
  3786.     debug(F111,"tthang cfgetispeed",ckitoa(errno),spdsavi);
  3787.     x = cfsetospeed(&ttcur,B0);    /* Replace by 0 */
  3788.     debug(F111,"tthang cfsetospeed",ckitoa(errno),x);
  3789.     if (x < 0) return(-1);
  3790.     x = cfsetispeed(&ttcur,B0);
  3791.     debug(F111,"tthang cfsetispeed",ckitoa(errno),x);
  3792.     if (x < 0) return(-1);
  3793.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3794.     debug(F111,"tthang tcsetattr B0",ckitoa(errno),x);
  3795.     if (x < 0) return(-1);
  3796.     msleep(HUPTIME);        /* Sleep 0.5 sec */
  3797.     x = cfsetospeed(&ttcur,spdsav); /* Restore prev speed */
  3798.     if (x < 0) return(-1);
  3799.     debug(F111,"tthang cfsetospeed prev",ckitoa(errno),x);
  3800.     x = cfsetispeed(&ttcur,spdsavi);
  3801.     debug(F111,"tthang cfsetispeed prev",ckitoa(errno),x);
  3802.     if (x < 0) return(-1);
  3803.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3804.     debug(F111,"tthang tcsetattr restore",ckitoa(errno),x);
  3805.     if (x < 0) return(-1);
  3806.     return(1);
  3807.     }
  3808. #else
  3809. #ifdef BSD44ORPOSIX
  3810. #ifdef QNX
  3811.     {
  3812.     int x;
  3813.     x = tcdropline(ttyfd,500);
  3814.     debug(F101,"tthang QNX tcdropline","",x);
  3815.     ttcur.c_cflag |= CLOCAL;
  3816.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3817.     debug(F101,"tthang QNX tcsetattr restore","",x);
  3818.     if (x < 0) {
  3819.         debug(F101,"tthang QNX tcsetattr restore errno","",errno);
  3820.         return(-1);
  3821.     }
  3822.     /* Fix flags - ensure O_NONBLOCK is off */
  3823.  
  3824.     errno = 0;
  3825.     debug(F101,"tthang QNX iniflags","",iniflags);
  3826.     if (fcntl(ttyfd, F_SETFL, iniflags) == -1) {
  3827.         debug(F101,"tthang QNX F_SETFL errno","",errno);
  3828.         return(-1);
  3829.     }
  3830.     return(x);
  3831.     }
  3832. #else  /* QNX */
  3833.     {
  3834.     int x;
  3835. #ifdef USE_TIOCSDTR
  3836.     debug(F100,"tthang BSD44ORPOSIX USE_TIOCSDTR","",0);
  3837.     errno = 0;
  3838.     x = ioctl(ttyfd, TIOCCDTR, NULL);
  3839.     debug(F111,"tthang BSD44ORPOSIX ioctl TIOCCDTR",ckitoa(errno),x);
  3840.     if (x < 0) return(-1);
  3841.     msleep(HUPTIME);        /* Sleep 0.5 sec */
  3842.     errno = 0;
  3843.     x = ioctl(ttyfd, TIOCSDTR, NULL);
  3844.     debug(F111,"tthang BSD44ORPOSIX ioctl TIOCSDTR",ckitoa(errno),x);
  3845.     if (x < 0) return(-1);
  3846. #else  /* USE_TIOCSDTR */
  3847.  
  3848. #ifdef HUP_CLOSE_POSIX
  3849. /*
  3850.   In OSR5 versions where TIOCSDTR is not defined (up to and including at
  3851.   least 5.0.6a) the POSIX APIs in the "#else" part below are available but
  3852.   don't work, and no other APIs are available that do work.  In this case
  3853.   we have to drop DTR by brute force: close and reopen the port.  This
  3854.   code actually works, but all the steps are crucial: setting CLOCAL, the
  3855.   O_NDELAY manipulations, etc.
  3856. */
  3857.     debug(F100,"tthang HUP_CLOSE_POSIX close/open","",0);
  3858.     debug(F101,"tthang HUP_CLOSE_POSIX O_NONBLOCK","",O_NONBLOCK);
  3859.     debug(F101,"tthang HUP_CLOSE_POSIX O_NDELAY","",O_NDELAY);
  3860.     errno = 0;
  3861.     x = tcgetattr(ttyfd, &ttcur);    /* Get current attributes */
  3862.     debug(F101,"tthang HUP_CLOSE_POSIX tcgetattr","",x);
  3863.     if (x < 0) {
  3864.         debug(F101,"tthang HUP_CLOSE_POSIX tcgetattr errno","",errno);
  3865.         return(-1);
  3866.     }
  3867.     errno = 0;
  3868.  
  3869.     x = close(ttyfd);        /* Close without releasing lock */
  3870.     if (x < 0) {
  3871.         debug(F101,"tthang HUP_CLOSE_POSIX close errno","",errno);
  3872.         return(-1);
  3873.     }
  3874.     errno = 0;
  3875.     x = msleep(500);        /* Pause half a second */
  3876.     if (x < 0) {            /* Or if that doesn't work, 1 sec */
  3877.         debug(F101,"tthang HUP_CLOSE_POSIX msleep errno","",errno);
  3878.         sleep(1);
  3879.     }
  3880.     errno = 0;
  3881.     ttyfd = priv_opn(ttnmsv, (O_RDWR|O_NDELAY)); /* Reopen the device */
  3882.     debug(F111,"tthang HUP_CLOSE_POSIX reopen",ttnmsv,ttyfd);
  3883.     if (ttyfd < 0) {
  3884.         debug(F101,"tthang HUP_CLOSE_POSIX reopen errno","",errno);
  3885.         return(-1);
  3886.     }
  3887.     debug(F101,"tthang HUP_CLOSE_POSIX re-ttopen ttyfd","",ttyfd);
  3888.  
  3889.     /* Restore previous attributes */
  3890.  
  3891.     errno = 0;
  3892.     tvtflg = 0;
  3893.     ttcur.c_cflag |= CLOCAL;
  3894.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3895.     debug(F101,"tthang HUP_CLOSE_POSIX tcsetattr restore","",x);
  3896.     if (x < 0) {
  3897.         debug(F101,"tthang HUP_CLOSE_POSIX tcsetattr restore errno",
  3898.           "",errno);
  3899.         return(-1);
  3900.     }
  3901.     /* Fix flags - ensure O_NDELAY and O_NONBLOCK are off */
  3902.  
  3903.     errno = 0;
  3904.         if ((x = fcntl(ttyfd, F_GETFL, 0)) == -1) {
  3905.         debug(F101,"tthang HUP_CLOSE_POSIX F_GETFL errno","",errno);
  3906.         return(-1);
  3907.     }
  3908.     debug(F101,"tthang HUP_CLOSE_POSIX flags","",x);
  3909.     errno = 0;
  3910.         x &= ~(O_NONBLOCK|O_NDELAY);
  3911.     debug(F101,"tthang HUP_CLOSE_POSIX flags to set","",x);
  3912.     debug(F101,"tthang HUP_CLOSE_POSIX iniflags","",iniflags);
  3913.     if (fcntl(ttyfd, F_SETFL, x) == -1) {
  3914.         debug(F101,"tthang HUP_CLOSE_POSIX F_SETFL errno","",errno);
  3915.         return(-1);
  3916.     }
  3917. #ifdef DEBUG
  3918.     if (deblog) {
  3919.         if ((x = fcntl(ttyfd, F_GETFL, 0)) > -1) {
  3920.         debug(F101,"tthang HUP_CLOSE_POSIX flags","",x);
  3921.         debug(F101,"tthang HUP_CLOSE_POSIX flags & O_NONBLOCK",
  3922.               "",x&O_NONBLOCK);
  3923.         debug(F101,"tthang HUP_CLOSE_POSIX flags & O_NDELAY",
  3924.               "",x&O_NDELAY);
  3925.         }
  3926.     }
  3927. #endif /* DEBUG */
  3928.  
  3929. #else  /* HUP_CLOSE_POSIX */
  3930.     
  3931.     /* General BSD44ORPOSIX case (Linux, BSDI, FreeBSD, etc) */
  3932.  
  3933.     debug(F100,"tthang BSD44ORPOSIX B0","",0);
  3934.     x = tcgetattr(ttyfd, &ttcur);    /* Get current attributes */
  3935.     debug(F111,"tthang BSD44ORPOSIX tcgetattr",ckitoa(errno),x);
  3936.     if (x < 0) return(-1);
  3937.     spdsav = cfgetospeed(&ttcur);    /* Get current speed */
  3938.     debug(F111,"tthang BSD44ORPOSIX cfgetospeed",ckitoa(errno),spdsav);
  3939.     spdsavi = cfgetispeed(&ttcur);    /* Get current speed */
  3940.     debug(F111,"tthang BSD44ORPOSIX cfgetispeed",ckitoa(errno),spdsavi);
  3941.     x = cfsetospeed(&ttcur,B0);    /* Replace by 0 */
  3942.     debug(F111,"tthang BSD44ORPOSIX cfsetospeed",ckitoa(errno),x);
  3943.     if (x < 0) return(-1);
  3944.     x = cfsetispeed(&ttcur,B0);
  3945.     debug(F111,"tthang BSD44ORPOSIX cfsetispeed",ckitoa(errno),x);
  3946.     if (x < 0) return(-1);
  3947.     /* This gets EINVAL on NetBSD 1.4.1 because of B0... */
  3948.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3949.     debug(F111,"tthang BSD44ORPOSIX tcsetattr B0",ckitoa(errno),x);
  3950.     if (x < 0) return(-1);
  3951.     msleep(HUPTIME);        /* Sleep 0.5 sec */
  3952.     debug(F101,"tthang BSD44ORPOSIX restore output speed","",spdsav);
  3953.     x = cfsetospeed(&ttcur,spdsav); /* Restore prev speed */
  3954.     debug(F111,"tthang BSD44ORPOSIX cfsetospeed prev",ckitoa(errno),x);
  3955.     if (x < 0) return(-1);
  3956.     debug(F101,"tthang BSD44ORPOSIX restore input speed","",spdsavi);
  3957.     x = cfsetispeed(&ttcur,spdsavi);
  3958.     debug(F111,"tthang BSD44ORPOSIX cfsetispeed prev",ckitoa(errno),x);
  3959.     if (x < 0) return(-1);
  3960.     ttcur.c_cflag |= CLOCAL;    /* Don't expect CD after hangup */
  3961.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  3962.     debug(F111,"tthang BSD44ORPOSIX tcsetattr restore",ckitoa(errno),x);
  3963.     if (x < 0) return(-1);
  3964.  
  3965. #endif /* HUP_CLOSE_POSIX */
  3966. #endif /* USE_TIOCSDTR */
  3967.  
  3968.     return(1);
  3969.     }
  3970.  
  3971. #endif /* QNX */
  3972. #else /* BSD44ORPOSIX */
  3973.  
  3974. #ifdef aegis                /* Apollo Aegis */
  3975.     sio_$control((short)ttyfd, sio_$dtr, false, st);    /* DTR down */
  3976.     msleep(HUPTIME);                    /* pause */
  3977.     sio_$control((short)ttyfd, sio_$dtr, true,  st);    /* DTR up */
  3978.     return(1);
  3979. #endif /* aegis */
  3980.  
  3981. #ifdef ANYBSD                /* Any BSD version. */
  3982. #ifdef TIOCCDTR                /* Except those that don't have this */
  3983.     debug(F100,"tthang BSD style","",0);
  3984.     if (ioctl(ttyfd,TIOCCDTR,0) < 0) {    /* Clear DTR. */
  3985.     debug(F101,"tthang TIOCCDTR fails","",errno);
  3986.     return(-1);
  3987.     }
  3988.     msleep(HUPTIME);            /* For about 1/2 sec */
  3989.     errno = 0;
  3990.     x = ioctl(ttyfd,TIOCSDTR,0);    /* Restore DTR */
  3991.     if (x < 0) {
  3992.     /*
  3993.       For some reason, this tends to fail with "no such device or address"
  3994.       but the operation still works, probably because of the close/open
  3995.       later on.  So let's not scare the user unnecessarily here.
  3996.     */
  3997.     debug(F101,"tthang TIOCSDTR errno","",errno); /* Log the error */
  3998.     x = 1;                /* Pretend we succeeded */
  3999.     } else if (x == 0) x = 1;        /* Success */
  4000. #ifdef COMMENT
  4001. #ifdef FT21
  4002.     ioctl(ttyfd, TIOCSAVEMODES, 0);
  4003.     ioctl(ttyfd, TIOCHPCL, 0);
  4004.     close(ttyfd);            /* Yes, must do this twice */
  4005.     if ((ttyfd = open(ttnmsv,2)) < 0)    /* on Fortune computers... */
  4006.       return(-1);            /* (but why?) */
  4007.     else x = 1;
  4008. #endif /* FT21 */
  4009. #endif /* COMMENT */
  4010. #endif /* TIOCCDTR */
  4011.     close(do_open(ttnmsv));        /* Clear i/o error condition */
  4012.     errno = 0;
  4013. #ifdef COMMENT
  4014. /* This is definitely dangerous.  Why was it here? */
  4015.     z = ttvt(ttspeed,ttflow);        /* Restore modes. */
  4016.     debug(F101,"tthang ttvt returns","",z);
  4017.     return(z < 0 ? -1 : 1);
  4018. #else
  4019.     return(x);
  4020. #endif /* COMMENT */
  4021. #endif /* ANYBSD */
  4022.  
  4023. #ifdef ATTSV
  4024. /* AT&T UNIX section, includes HP-UX and generic AT&T System III/V... */
  4025.  
  4026. #ifdef HPUX
  4027. /* Hewlett Packard allows explicit manipulation of modem signals. */
  4028.  
  4029. #ifdef COMMENT
  4030. /* Old way... */
  4031.     debug(F100,"tthang HP-UX style","",0);
  4032.     if (ioctl(ttyfd,MCSETAF,&dtr_down) < 0)        /* lower DTR */
  4033.       return(-1);                           /* oops, can't. */
  4034.     msleep(HUPTIME);                       /* Pause half a second. */
  4035.     x = 1;                           /* Set return code */
  4036.     if (ioctl(ttyfd,MCGETA,&modem_rtn) > -1) {     /* Get line status. */
  4037.     if ((modem_rtn & MDCD) != 0)             /* Check if CD is low. */
  4038.       x = -1;                                  /* CD didn't drop, fail. */
  4039.     } else x = -1;
  4040.  
  4041.     /* Even if above calls fail, RTS & DTR should be turned back on. */
  4042.     modem_rtn = MRTS | MDTR;
  4043.     if (ioctl(ttyfd,MCSETAF,&modem_rtn) < 0) x = -1;
  4044.     return(x);
  4045. #else
  4046. /* New way, from Hellmuth Michaelis */
  4047.     debug(F100,"tthang HP-UX style, HPUXDEBUG","",0);
  4048.     if (ioctl(ttyfd,MCGETA,&modem_rtn) == -1) { /* Get current status. */
  4049.     debug(F100,"tthang HP-UX: can't get modem lines, NO HANGUP!","",0);
  4050.     return(-1);
  4051.     }
  4052.     sprintf(modem_state,"%#lx",modem_rtn);
  4053.     debug(F110,"tthang HP-UX: modem lines = ",modem_state,0);
  4054.     modem_sav = modem_rtn;        /* Save current modem signals */
  4055.     modem_rtn &= ~MDTR;            /* Turn DTR bit off */
  4056.     sprintf(modem_state,"%#lx",modem_rtn);
  4057.     debug(F110,"tthang HP-UX: DTR down = ",modem_state,0);
  4058.     if (ioctl(ttyfd,MCSETAF,&modem_rtn) < 0) { /* lower DTR */
  4059.     debug(F100,"tthang HP-UX: can't lower DTR!","",0);
  4060.     return(-1);            /* oops, can't. */
  4061.     }
  4062.     msleep(HUPTIME);            /* Pause half a second. */
  4063.     x = 1;                /* Set return code */
  4064.     if (ioctl(ttyfd,MCGETA,&modem_rtn) > -1) { /* Get line status. */
  4065.     sprintf(modem_state,"%#lx",modem_rtn);
  4066.     debug(F110,"tthang HP-UX: modem lines got = ",modem_state,0);
  4067.     if ((modem_rtn & MDCD) != 0) {    /* Check if CD is low. */
  4068.         debug(F100,"tthang HP-UX: DCD not down","",0);
  4069.         x = -1;            /* CD didn't drop, fail. */
  4070.     } else {
  4071.         debug(F100,"tthang HP-UX: DCD down","",0);
  4072.     }
  4073.     } else {
  4074.     x = -1;
  4075.     debug(F100,"tthang HP-UX: can't get DCD status !","",0);
  4076.     }
  4077.  
  4078.     /* Even if above calls fail, DTR should be turned back on. */
  4079.  
  4080.     modem_sav |= MDTR;
  4081.     if (ioctl(ttyfd,MCSETAF,&modem_sav) < 0) {
  4082.     x = -1;
  4083.     debug(F100,"tthang HP-UX: can't set saved state","",0);
  4084.     } else {
  4085.     sprintf(modem_state,"%#lx",modem_sav);
  4086.     debug(F110,"tthang HP-UX: final modem lines = ",modem_state,0);
  4087.     }
  4088.     return(x);
  4089. #endif /* COMMENT */
  4090.  
  4091. #else /* AT&T but not HP-UX */
  4092.  
  4093. /* SVID for AT&T System V R3 defines ioctl's for handling modem signals. */
  4094. /* It is not known how many, if any, systems actually implement them, */
  4095. /* so we include them here in ifdef's. */
  4096.  
  4097. /*
  4098.   Unixware has the TIOCMxxx symbols defined, but calling ioctl() with them
  4099.   gives error 22 (invalid argument).
  4100. */
  4101. #ifndef _IBMR2
  4102. /*
  4103.   No modem-signal twiddling for IBM RT PC or RS/6000.
  4104.   In AIX 3.1 and earlier, the ioctl() call is broken.
  4105.   This code could be activated for AIX 3.1 with PTF 2006 or later
  4106.   (e.g. AIX 3.2), but close/open does the job too, so why bother.
  4107. */
  4108. #ifdef TIOCMBIS                /* Bit Set */
  4109. #ifdef TIOCMBIC                /* Bit Clear */
  4110. #ifdef TIOCM_DTR            /* DTR */
  4111.  
  4112. /* Clear DTR, sleep 300 msec, turn it back on. */
  4113. /* If any of the ioctl's return failure, go on to the next section. */
  4114.  
  4115.     z = TIOCM_DTR;            /* Code for DTR. */
  4116. #ifdef COMMENT
  4117. /*
  4118.   This was the cause of the troubles with the Solaris Port Monitor.
  4119.   The problem is: RTS never comes back on.  Moral: Don't do it!
  4120.   (But why doesn't it come back on?  See the TIOCMBIS call...)
  4121. */
  4122. #ifdef TIOCM_RTS            /* Lower RTS too if symbol is known. */
  4123.     z |= TIOCM_RTS;
  4124. #endif /* TIOCM_RTS */
  4125. #endif /* COMMENT */
  4126.  
  4127.     debug(F101,"tthang TIOCM signal mask","",z);
  4128.     if (ioctl(ttyfd,TIOCMBIC,&z) > -1) {   /* Try to lower DTR. */
  4129.     debug(F100,"tthang TIOCMBIC ok","",0);
  4130.     msleep(HUPTIME);           /* Pause half a second. */
  4131.     if (ioctl(ttyfd,TIOCMBIS,&z) > -1) { /* Try to turn it back on. */
  4132.         debug(F100,"tthang TIOCMBIS ok","",0);
  4133. #ifndef CLSOPN
  4134.         return(1);            /* Success, done. */
  4135. #endif /* CLSOPN */
  4136.     } else {            /* Couldn't raise, continue. */
  4137.         debug(F101,"tthang TIOCMBIS errno","",errno);
  4138.     }
  4139.     } else {                /* Couldn't lower, continue. */
  4140.      debug(F101,"tthang TIOCMBIC errno","",errno);
  4141.     }
  4142. #endif /* TIOCM_DTR */
  4143. #endif /* TIOCMBIC */
  4144. #endif /* TIOCMBIS */
  4145. #endif /* _IBMR2 */
  4146.  
  4147. /*
  4148.   General AT&T UNIX case, not HPUX.  The following code is highly suspect.  No
  4149.   two AT&T-based systems seem to do this the same way.  The object is simply
  4150.   to turn off DTR and then turn it back on.  SVID says the universal method
  4151.   for turning off DTR is to set the speed to zero, and this does seem to do
  4152.   the trick in all cases.  But neither SVID nor any known man pages say how to
  4153.   turn DTR back on again.  Some variants, like most Xenix implementations,
  4154.   raise DTR again when the speed is restored to a nonzero value.  Others
  4155.   require the device to be closed and opened again, but this is risky because
  4156.   getty could seize the device during the instant it is closed.
  4157. */
  4158.  
  4159. /* Return code for ioctl failures... */
  4160. #ifdef ATT6300
  4161.     x = 1;                /* ATT6300 doesn't want to fail... */
  4162. #else
  4163.     x = -1;
  4164. #endif /* ATT6300 */
  4165.  
  4166.     debug(F100,"tthang get settings","",0);
  4167.     if (ioctl(ttyfd,TCGETA,&ttcur) < 0) /* Get current settings. */
  4168.       return(x);            /* Fail if this doesn't work. */
  4169.     if ((flags = fcntl(ttyfd,F_GETFL,0)) < 0) /* Get device flags. */
  4170.       return(x);
  4171.     ttc_save = ttcur.c_cflag;        /* Remember current speed. */
  4172.     spdsav = ttc_save & CBAUD;
  4173.     debug(F101,"tthang speed","",spdsav);
  4174.  
  4175. #ifdef O_NDELAY
  4176.     debug(F100,"tthang turning O_NDELAY on","",0);
  4177.     fcntl(ttyfd, F_SETFL, flags | O_NDELAY); /* Activate O_NDELAY */
  4178. #endif /* O_NDELAY */
  4179.  
  4180. #ifdef ATT7300 /* This is the way it is SUPPOSED to work */
  4181.     ttcur.c_cflag &= ~CBAUD;        /* Change the speed to zero.  */
  4182. #else
  4183. #ifdef RTAIX
  4184.     ttcur.c_cflag &= ~CBAUD;        /* Change the speed to zero.  */
  4185. #else          /* This way really works but may be dangerous */
  4186. #ifdef u3b2
  4187.     ttcur.c_cflag = ~(CBAUD|CLOCAL);    /* Special for AT&T 3B2s */
  4188.                     /* (CLOCAL must be OFF) */
  4189. #else
  4190. #ifdef SCO3R2                /* SCO UNIX 3.2 */
  4191. /*
  4192.   This is complete nonsense, but an SCO user claimed this change made
  4193.   hanging up work.  Comments from other SCO UNIX 3.2 users would be
  4194.   appreciated.
  4195. */
  4196.     ttcur.c_cflag = CBAUD|B0;
  4197. #else
  4198. #ifdef AIXRS                /* AIX on RS/6000 */
  4199. /*
  4200.   Can't set speed to zero on AIX 3.1 on RS/6000 64-port adapter,
  4201.   even though you can do it on the built-in port and the 8- and 16-port
  4202.   adapters.  (Untested on 128-port adapter.)
  4203. */
  4204.     ttcur.c_cflag = CLOCAL|HUPCL|spdsav; /* Speed 0 causes EINVAL */
  4205. #else                    /* None of the above */
  4206. /*
  4207.   Set everything, including the speed, to zero, except for the CLOCAL
  4208.   and HUPCL bits.
  4209. */
  4210.     ttcur.c_cflag = CLOCAL|HUPCL;
  4211. #endif /* AIXRS */
  4212. #endif /* SCO3R2 */
  4213. #endif /* u3b2 */
  4214. #endif /* RTAIX */
  4215. #endif /* ATT7300 */
  4216.  
  4217. #ifdef COMMENT
  4218.     /* and if none of those work, try one of these... */
  4219.     ttcur.c_cflag = 0;
  4220.     ttcur.c_cflag = CLOCAL;
  4221.     ttcur.c_cflag &= ~(CBAUD|HUPCL);
  4222.     ttcur.c_cflag &= ~(CBAUD|CREAD);
  4223.     ttcur.c_cflag &= ~(CBAUD|CREAD|HUPCL);
  4224.     /* or other combinations */
  4225. #endif /* COMMENT */
  4226.  
  4227. #ifdef TCXONC
  4228.     debug(F100,"tthang TCXONC","",0);
  4229.     if (ioctl(ttyfd, TCXONC, 1) < 0) {
  4230.     debug(F101,"tthang TCXONC failed","",errno);
  4231.     }
  4232. #endif /* TCXONC */
  4233.  
  4234. #ifdef TIOCSTART
  4235.     debug(F100,"tthang TIOCSTART","",0);
  4236.     if (ioctl(ttyfd, TIOCSTART, 0) < 0) {
  4237.     debug(F101,"tthang TIOCSTART failed","",errno);
  4238.     }
  4239. #endif /* TIOCSTART */
  4240.  
  4241.     if (ioctl(ttyfd,TCSETAF,&ttcur) < 0) { /* Fail if we can't. */
  4242.     debug(F101,"tthang TCSETAF failed","",errno);
  4243.     fcntl(ttyfd, F_SETFL, flags);    /* Restore flags */
  4244.     return(-1);            /* before returning. */
  4245.     }
  4246.     msleep(300);            /* Give modem time to notice. */
  4247.  
  4248. #ifndef NOCOTFMC
  4249.  
  4250. /* Now, even though it doesn't say this in SVID or any man page, we have */
  4251. /* to close and reopen the device.  This is not necessary for all systems, */
  4252. /* but it's impossible to predict which ones need it and which ones don't. */
  4253.  
  4254. #ifdef ATT7300
  4255. /*
  4256.   Special handling for ATT 7300 UNIX PC and 3B1, which have "phone"
  4257.   related ioctl's for their internal modems.  attmodem has getty status and
  4258.   modem-in-use bit.  Reportedly the ATT7300/3B1 PIOCDISC call is necessary,
  4259.   but also ruins the file descriptor, and no other phone(7) ioctl call can fix
  4260.   it.  Whatever it does, it seems to escape detection with PIOCGETA and TCGETA.
  4261.   The only way to undo the damage is to close the fd and then reopen it.
  4262. */
  4263.     if (attmodem & ISMODEM) {
  4264.     debug(F100,"tthang attmodem close/open","",0);
  4265.     ioctl(ttyfd,PIOCUNHOLD,&dialer); /* Return call to handset. */
  4266.     ioctl(ttyfd,PIOCDISC,&dialer);    /* Disconnect phone. */
  4267.     close(ttyfd);            /* Close and reopen the fd. */
  4268.     ttyfd = priv_opn(ttnmsv, O_RDWR | O_NDELAY);
  4269.     attmodem &= ~ISMODEM;        /* Phone no longer in use. */
  4270.     }
  4271. #else /* !ATT7300 */
  4272. /* It seems we have to close and open the device for other AT&T systems */
  4273. /* too, and this is the place to do it.  The following code does the */
  4274. /* famous close(open(...)) magic by default.  If that doesn't work for you, */
  4275. /* then try uncommenting the following statement or putting -DCLSOPN in */
  4276. /* the makefile CFLAGS. */
  4277.  
  4278. /* #define CLSOPN */
  4279.  
  4280. #ifndef SCO32 /* Not needed by, and harmful to, SCO UNIX 3.2 / Xenix 2.3 */
  4281.  
  4282. #ifdef O_NDELAY
  4283. #define OPENFLGS O_RDWR | O_NDELAY
  4284. #else
  4285. #define OPENFLGS O_RDWR
  4286. #endif
  4287.  
  4288. #ifndef CLSOPN
  4289. /* This method is used by default, i.e. unless CLSOPN is defined. */
  4290. /* It is thought to be safer because there is no window where getty */
  4291. /* can seize control of the device.  The drawback is that it might not work. */
  4292.  
  4293.     debug(F101,"tthang close(open()), OPENFLGS","",OPENFLGS);
  4294.     close(priv_opn(ttnmsv, OPENFLGS));
  4295.  
  4296. #else
  4297. /* This method is used if you #define CLSOPN.  It is more likely to work */
  4298. /* than the previous method, but it's also more dangerous. */
  4299.  
  4300.     debug(F101,"tthang close/open, OPENFLGS","",OPENFLGS);
  4301.     close(ttyfd);
  4302.     msleep(10);
  4303.     ttyfd = priv_opn(ttnmsv, OPENFLGS);    /* Open it again */
  4304. #endif /* CLSOPN */
  4305. #undef OPENFLGS
  4306.  
  4307. #endif /* SCO32 */
  4308. #endif /* ATT7300 */
  4309.  
  4310. #endif /* NOCOTFMC */
  4311.  
  4312. /* Now put all flags & modes back the way we found them. */
  4313. /* (Does the order of ioctl & fcntl matter ? ) */
  4314.  
  4315.     debug(F100,"tthang restore settings","",0);
  4316.     ttcur.c_cflag = ttc_save;        /* Get old speed back. */
  4317.     if (ioctl(ttyfd,TCSETAF,&ttcur) < 0) /* ioctl parameters. */
  4318.       return(-1);
  4319. #ifdef O_NDELAY
  4320. /*
  4321.   This is required for IBM RT and RS/6000, probably helps elsewhere too (?).
  4322.   After closing a modem line, the modem will probably not be asserting
  4323.   carrier any more, so we should not require carrier any more.  If this
  4324.   causes trouble on non-IBM UNIXes, change the #ifdef to use _IBMR2 rather
  4325.   than O_NDELAY.
  4326. */
  4327.     flags &= ~O_NDELAY;            /* Don't require carrier on reopen */
  4328. #endif /* O_NDELAY */
  4329.     if (fcntl(ttyfd,F_SETFL,flags) < 0)    /* fcntl parameters */
  4330.       return(-1);
  4331.  
  4332.     return(1);
  4333. #endif /* not HPUX */
  4334. #endif /* ATTSV */
  4335. #endif /* BSD44ORPOSIX */
  4336. #endif /* HUP_POSIX */
  4337. #endif /* NOLOCAL */
  4338. }
  4339.  
  4340. /*
  4341.   Major change in 5A(174).  We used to use LPASS8, if it was defined, to
  4342.   allow 8-bit data and Xon/Xoff flow control at the same time.  But this
  4343.   LPASS8 business seems to have been causing trouble for everybody but me!
  4344.   For example, Annex terminal servers, commonly used with Encore computers,
  4345.   do not support LPASS8 even though the Encore itself does.  Ditto for many
  4346.   other terminal servers, TELNET connections, rlogin connections, etc etc.
  4347.   Now, reportedly, even vanilla 4.3 BSD systems can't do this right on their
  4348.   serial lines, even though LPASS8 is a feature of 4.3BSD.  So let's turn it
  4349.   off for everybody.  That means we goes back to using raw mode, with no
  4350.   flow control.  Phooey.
  4351.  
  4352.   NOTE: This must be done before the first reference to LPASS8 in this file,
  4353.   and after the last #include statment.
  4354. */
  4355. #ifdef LPASS8
  4356. #undef LPASS8
  4357. #endif /* LPASS8 */
  4358.  
  4359. /*  T T R E S  --  Restore terminal to "normal" mode.  */
  4360.  
  4361. /* ske@pkmab.se: There are two choices for what this function should do.
  4362.  * (1) Restore the tty to current "normal" mode, with carrier treatment
  4363.  * according to ttcarr, to be used after every kermit command. (2) Restore
  4364.  * the tty to the state it was in before kermit opened it. These choices
  4365.  * conflict, since ttold can't hold both choices of tty parameters.  ttres()
  4366.  * is currently being called as in choice (1), but ttold basically holds
  4367.  * the initial parameters, as in (2), and the description at the beginning
  4368.  * of this file says (2).
  4369.  *
  4370.  * I don't think restoring tty parameters after all kermit commands makes
  4371.  * much of a difference.  Restoring them upon exit from kermit may be of
  4372.  * some use in some cases (when the line is not restored automatically on
  4373.  * close, by the operating system).
  4374.  *
  4375.  * I can't choose which one it should be, so I haven't changed it. It
  4376.  * probably works as it is, too. It would probably even work even with
  4377.  * ttres() entirely deleted...
  4378.  *
  4379.  * (from fdc: Actually, this function operates in remote mode too, so
  4380.  * it restores the console (command) terminal to whatever mode it was
  4381.  * in before packet operations began, so that commands work right again.)
  4382.  */
  4383. int
  4384. ttres() {                               /* Restore the tty to normal. */
  4385.     int x;
  4386.  
  4387.     if (ttyfd < 0) return(-1);          /* Not open. */
  4388.  
  4389.     if (ttfdflg) return(0);        /* Don't mess with terminal modes if */
  4390.                     /* we got ttyfd from another process */
  4391. #ifdef    NETCONN
  4392.     if (netconn) {            /* Network connection */
  4393.         tvtflg = 0;
  4394. #ifdef TCPSOCKET
  4395. #ifdef TCP_NODELAY
  4396.         {
  4397.         extern int tcp_nodelay;    /* Just put this back if necessary */
  4398.         if (ttnet == NET_TCPB) {
  4399.         if (nodelay_sav > -1) {
  4400.             no_delay(ttyfd,nodelay_sav);
  4401.             nodelay_sav = -1;
  4402.         }
  4403.         }
  4404.         }
  4405. #endif /* TCP_NODELAY */
  4406. #ifdef TN_COMPORT
  4407.         if (istncomport()) {
  4408.             int rc = -1;
  4409.             if ((rc = tnsetflow(ttflow)) < 0)
  4410.           return(rc);
  4411.             if (ttspeed <= 0) 
  4412.           ttspeed = tnc_get_baud();
  4413.             else if ((rc = tnc_set_baud(ttspeed)) < 0)
  4414.           return(rc);
  4415.             tnc_set_datasize(8);
  4416.         tnc_set_stopsize(stopbits);
  4417.  
  4418. #ifdef HWPARITY
  4419.             if (hwparity) {
  4420.                 switch (hwparity) {
  4421.           case 'e':            /* Even */
  4422.                     debug(F100,"ttres 8 bits + even parity","",0);
  4423.                     tnc_set_parity(3);
  4424.                     break;
  4425.           case 'o':            /* Odd */
  4426.                     debug(F100,"ttres 8 bits + odd parity","",0);
  4427.                     tnc_set_parity(2);
  4428.                     break;
  4429.           case 'm':            /* Mark */
  4430.                     debug(F100,"ttres 8 bits + invalid parity: mark","",0);
  4431.                     tnc_set_parity(4);
  4432.                     break;
  4433.           case 's':            /* Space */
  4434.                     debug(F100,"ttres 8 bits + invalid parity: space","",0);
  4435.                     tnc_set_parity(5);
  4436.                     break;
  4437.                 }
  4438.             } else
  4439. #endif /* HWPARITY */
  4440.         {
  4441.                 tnc_set_parity(1);              /* None */
  4442.             }
  4443.             tvtflg = 0;
  4444.             return(0);
  4445.         }
  4446. #endif /* TN_COMPORT */
  4447. #endif /* TCPSOCKET */
  4448.     return(0);
  4449.     }
  4450. #endif    /* NETCONN */
  4451. #ifdef NETCMD
  4452.     if (ttpipe) return(0);
  4453. #endif /* NETCMD */
  4454. #ifdef NETPTY
  4455.     if (ttpty) return(0);
  4456. #endif /* NETPTY */
  4457.  
  4458. /* Real terminal device, so restore its original modes */
  4459.  
  4460. #ifdef BSD44ORPOSIX            /* For POSIX like this */
  4461.     debug(F100,"ttres BSD44ORPOSIX","",0);
  4462.     x = tcsetattr(ttyfd,TCSADRAIN,&ttold);
  4463. #else                    /* For all others... */
  4464. #ifdef ATTSV                            /* For AT&T versions... */
  4465.     debug(F100,"ttres ATTSV","",0);
  4466.     x = ioctl(ttyfd,TCSETAW,&ttold);    /* Restore tty modes this way. */
  4467. #else
  4468. /* Here we restore the modes for BSD */
  4469.  
  4470. #ifdef LPASS8                /* Undo "pass8" if it were done */
  4471.     if (lmodef) {
  4472.     if (ioctl(ttyfd,TIOCLSET,&lmode) < 0)
  4473.       debug(F100,"ttres TIOCLSET failed","",0);
  4474.     else
  4475.       debug(F100,"ttres TIOCLSET ok","",0);
  4476.     }
  4477. #endif /* LPASS8 */
  4478.  
  4479. #ifdef CK_DTRCTS           /* Undo hardware flow if it were done */
  4480.     if (lmodef) {
  4481.      if (ioctl(ttyfd,TIOCLSET,&lmode) < 0)
  4482.        debug(F100,"ttres TIOCLSET failed","",0);
  4483.      else
  4484.        debug(F100,"ttres TIOCLSET ok","",0);
  4485.     }
  4486. #endif /* CK_DTRCTS */
  4487.  
  4488. #ifdef TIOCGETC                /* Put back special characters */
  4489.     if (tcharf && (xlocal == 0)) {
  4490.     if (ioctl(ttyfd,TIOCSETC,&tchold) < 0)
  4491.       debug(F100,"ttres TIOCSETC failed","",0);
  4492.     else
  4493.       debug(F100,"ttres TIOCSETC ok","",0);
  4494.     }
  4495. #endif /* TIOCGETC */
  4496.  
  4497. #ifdef TIOCGLTC                /* Put back local special characters */
  4498.     if (ltcharf && (xlocal == 0)) {
  4499.     if (ioctl(ttyfd,TIOCSLTC,<chold) < 0)
  4500.       debug(F100,"ttres TIOCSLTC failed","",0);
  4501.     else
  4502.       debug(F100,"ttres TIOCSLTC ok","",0);
  4503.     }
  4504. #endif /* TIOCGLTC */
  4505.  
  4506. #ifdef BELLV10
  4507.     debug(F100,"ttres BELLV10","",0);
  4508.     x = ioctl(ttyfd,TIOCSETP,&ttold);    /* Restore both structs */
  4509.     x = ioctl(ttyfd,TIOCSDEV,&tdold);
  4510. #else
  4511.     debug(F100,"ttres stty","",0);
  4512.     x = stty(ttyfd,&ttold);             /* Restore tty modes the old way. */
  4513. #endif /* BELLV10 */
  4514.  
  4515.     if (!xlocal)
  4516.       msleep(100);            /* This replaces sleep(1)... */
  4517.                     /* Put back sleep(1) if tty is */
  4518.                     /* messed up after close. */
  4519. #endif /* ATTSV */
  4520. #endif /* BSD44ORPOSIX */
  4521.  
  4522.     debug(F101,"ttres result","",x);
  4523. #ifndef QNX
  4524.     if (x < 0) debug(F101,"ttres errno","",errno);
  4525. #endif /* QNX */
  4526.  
  4527. #ifdef AIXRS
  4528. #ifndef AIX41
  4529.     x = ioctl(ttyfd, ttld & 1 ? TXADDCD : TXDELCD, "rts");
  4530.     debug(F101,"ttres AIX line discipline rts restore","",x);
  4531. #endif /* AIX41 */
  4532. #endif /* AIXRS */
  4533.  
  4534. #ifdef BSD41
  4535.     if (ttld > -1) {            /* Put back line discipline */
  4536.     x = ioctl(ttyfd, TIOCSETD, &ttld);
  4537.     debug(F101,"ttres BSD41 line discipline restore","",x);
  4538.     if (x < 0) debug(F101,"...ioctl errno","",errno);
  4539.     ttld = -1;
  4540.     }
  4541. #endif /* BSD41 */
  4542.  
  4543. #ifdef sony_news
  4544.     x = xlocal ? km_ext : km_con;    /* Restore Kanji mode. */
  4545.     if (x != -1) {            /* Make sure we know original modes. */
  4546.     if (ioctl(ttyfd,TIOCKSET, &x) < 0) {
  4547.         perror("ttres can't set Kanji mode");
  4548.         debug(F101,"ttres error setting Kanji mode","",x);
  4549.         return(-1);
  4550.     }
  4551.     }
  4552.     debug(F100,"ttres set Kanji mode ok","",0);
  4553. #endif /* sony_news */
  4554.  
  4555.     tvtflg = 0;                /* Invalidate terminal mode settings */
  4556.     debug(F101,"ttres return code","",x);
  4557.     return(x);
  4558. }
  4559.  
  4560. #ifndef NOUUCP
  4561.  
  4562. /*  T T C H K P I D  --  Check lockfile pid  */
  4563. /*
  4564.   Read pid from lockfile named f, check that it's still valid.
  4565.   If so, return 1.
  4566.   On failure to read pid, return 1.
  4567.   Otherwise, try to delete lockfile f and return 0 if successful, else 1.
  4568. */
  4569. static int
  4570. ttchkpid(f) char *f; {
  4571.     int pid, mypid, x;
  4572.     pid = ttrpid(f);            /* Read pid from file. */
  4573.     if (pid > -1) {            /* If we were able to read the pid.. */
  4574.     debug(F101,"ttchkpid lock pid","",pid);
  4575.     errno = 0;            /* See if process still exists. */
  4576.     mypid = (int)getpid();        /* Get my own pid. */
  4577.     debug(F101,"ttchkpid my pid","",mypid);
  4578.     if (pid == mypid) {        /* It's me! */
  4579.         x = -1;            /* So I can delete it */
  4580.         errno = ESRCH;        /* pretend it's invalid */
  4581.     } else {            /* It's not me */
  4582.         x = kill((PID_T)pid, 0);    /* See if it's a live process */
  4583.         debug(F101,"ttchkpid kill errno","",errno);
  4584.     }
  4585.     debug(F101,"ttchkpid pid test","",x);
  4586.     if (x < 0 && errno == ESRCH) { /* pid is invalid */
  4587.         debug(F111,"removing stale lock",f,pid);
  4588.         if (!backgrd)
  4589.           printf("Removing stale lock %s (pid %d terminated)\n", f, pid);
  4590.         priv_on();
  4591.         x = unlink(f);        /* Remove the lockfile. */
  4592.         priv_off();
  4593.         debug(F111,"ttchkpid unlink",f,x);
  4594.         if (x > -1)
  4595.           return(0);        /* Device is not locked after all */
  4596.         else if (!backgrd)
  4597.           perror(f);
  4598.     }
  4599.     return(1);
  4600.     }
  4601.     return(1);                /* Failure to read pid */
  4602. }
  4603.  
  4604. #ifdef HPUX
  4605.  
  4606. /* Aliases (different drivers) for HP-UX dialout devices: */
  4607.  
  4608. static char *devprefix[] = { "tty", "ttyd", "cul", "cua", "cuad", "culd", "" };
  4609. static int ttydexists = 0;
  4610.  
  4611. #endif /* HPUX */
  4612.  
  4613. /*  T T R P I D  --  Read pid from lockfile "name" */
  4614.  
  4615. static int
  4616. ttrpid(name) char *name; {
  4617.     long len;
  4618.     int x, fd, pid;
  4619.     short spid;
  4620.     char buf[32];
  4621.  
  4622.     debug(F110,"ttrpid",name,0);
  4623.     if (!name) return(-1);
  4624.     if (!*name) return(-1);
  4625.     priv_on();
  4626.     len = zchki(name);            /* Get file length */
  4627.     priv_off();
  4628.     debug(F101,"ttrpid zchki","",len);
  4629.     if (len < 0)
  4630.       return(-1);
  4631.     if (len > 31)
  4632.       return(-1);
  4633.     priv_on();
  4634.     fd = open(name,O_RDONLY);        /* Try to open lockfile. */
  4635.     priv_off();
  4636.     debug(F101,"ttrpid fd","",fd);
  4637.     if (fd <= 0)
  4638.       return(-1);
  4639. /*
  4640.   Here we try to be flexible and allow for all different binary and string
  4641.   formats at runtime, rather than a specific format for each configuration
  4642.   hardwired at compile time.
  4643. */
  4644.     pid = -1;
  4645. #ifndef COHERENT
  4646. /*
  4647.   COHERENT uses a string PID but without leading spaces or 0's, so there is
  4648.   no way to tell from the file's length whether it contains a string or binary
  4649.   pid.  So for COHERENT only, we only allow string pids.  For all others, we
  4650.   decide based on the size of the lockfile.
  4651. */
  4652.     if (len > 4) {            /* If file > 4 bytes it's a string */
  4653. #endif /* COHERENT */
  4654.     x = read(fd,buf,(int)len);
  4655.     debug(F111,"ttrpid string read",buf,x);
  4656.     if (x < 0) {
  4657.         pid = -1;
  4658.     } else {
  4659.         buf[31] = '\0';
  4660.         x = sscanf(buf,"%d",&pid);    /* Get the integer pid from it. */
  4661.     }
  4662. #ifndef COHERENT
  4663.     } else if (len == 4) {        /* 4 bytes so binary */
  4664.     x = read(fd, (char *)&pid, 4);    /* Read the bytes into an int */
  4665.     debug(F101,"ttrpid integer read","",x);
  4666.     if (x < 4)
  4667.       pid = -1;
  4668.     } else if (len == 2) {        /* 2 bytes binary */
  4669.     x = read(fd, (char *)&spid, 2);    /* Read the bytes into a short */
  4670.     debug(F101,"ttrpid short read","",x);
  4671.     if (x < 2)
  4672.       pid = -1;
  4673.     else
  4674.       pid = spid;
  4675.     } else
  4676.       pid = -1;
  4677. #endif /* COHERENT */
  4678.     close(fd);                /* Close the lockfile */
  4679.     debug(F101,"ttrpid pid","",pid);
  4680.     return(pid);
  4681. }
  4682. #endif /* NOUUCP */
  4683.  
  4684. /*  T T L O C K  */
  4685.  
  4686. /*
  4687.   This function attempts to coordinate use of the communication device with
  4688.   other copies of Kermit and any other program that follows the UUCP
  4689.   device-locking conventions, which, unfortunately, vary among different UNIX
  4690.   implementations.  The idea is to look for a file of a certain name, the
  4691.   "lockfile", in a certain directory.  If such a file is found, then the line
  4692.   is presumed to be in use, and Kermit should not use it.  If no such file is
  4693.   found, Kermit attempts to create one so that other programs will not use the
  4694.   same line at the same time.  Because the lockfile and/or the directory it's
  4695.   in might lack write permission for the person running Kermit, Kermit could
  4696.   find itself running setuid to uucp or other user that does have the
  4697.   necessary permissions.  At startup, Kermit has changed its effective uid to
  4698.   the user's real uid, and so ttlock() must switch back to the original
  4699.   effective uid in order to create the lockfile, and then back again to the
  4700.   real uid to prevent unauthorized access to other directories or files owned
  4701.   by the user the program is setuid to.
  4702.  
  4703.   Totally rewritten for C-Kermit 5A to eliminate windows of vulnerability,
  4704.   based on suggestions from Warren Tucker.  Call with pointer to name of
  4705.   tty device.  Returns:
  4706.  
  4707.    0 on success
  4708.   -1 on failure
  4709.  
  4710.   Note: Once privileges are turned on using priv_on(), it is essential that
  4711.   they are turned off again before this function returns.
  4712. */
  4713. #ifdef SVR4                /* Lockfile uses device numbers. */
  4714. /*
  4715.   Although I can't find this in writing anywhere (e.g. in SVID for SVR4),
  4716.   it is the behavior of the "reference version" of SVR4, i.e. the Intel
  4717.   port from UNIX Systems Laboratories, then called Univel UnixWare,
  4718.   then called Novell UnixWare, then called SCO Unixware, then called Caldera
  4719.   Open UNIX...  It also makes much more sense than device-name-based lockfiles
  4720.   since there can be multiple names for the same device, symlinks, etc.
  4721. */
  4722. #ifndef LFDEVNO                /* Define this for SVR4 */
  4723. #ifndef AIXRS                /* But not for RS/6000 AIX 3.2, etc. */
  4724. #ifndef BSD44                /* If anybody else needs it... */
  4725. #ifndef __386BSD__
  4726. #ifndef __FreeBSD__
  4727. #ifndef HPUX10
  4728. #ifndef IRIX51                /* SGI IRIX 5.1 or later */
  4729. #ifndef CK_SCOV5            /* SCO Open Server 5.0 */
  4730. #define LFDEVNO
  4731. #endif /* CK_SCOV5 */
  4732. #endif /* IRIX51 */
  4733. #endif /* HPUX10 */
  4734. #endif /* __FreeBSD__ */
  4735. #endif /* __386BSD__ */
  4736. #endif /* BSD44 */
  4737. #endif /* AIXRS */
  4738. #endif /* LFDEVNO */            /* ... define it here or on CC */
  4739. #endif /* SVR4 */            /* command line. */
  4740.  
  4741. #ifdef COHERENT
  4742. #define LFDEVNO
  4743. #endif /* COHERENT */
  4744.  
  4745. /*
  4746.   For platforms where the lockfile name is made from device/major/minor
  4747.   device number, as in SVR4.  Which, if we must have lockfiles at all, is
  4748.   by far the best format, since it eliminates all the confusion that stems
  4749.   from multiple names (or drivers) for the same port, not to mention
  4750.   symlinks.  It might even be a good idea to start using this form even
  4751.   on platforms where it's not supported, alongside the normal forms for those
  4752.   platforms, in order to get people used to it...
  4753. */
  4754. #ifdef LFDEVNO
  4755. #ifndef major                /* If we didn't find it */
  4756. #ifdef SVR4                /* then for Sys V R4 */
  4757. #include <sys/mkdev.h>            /* look here */
  4758. #else                    /* or for SunOS versions */
  4759. #ifdef SUNOS4                /* ... */
  4760. #include <sys/sysmacros.h>        /* look here */
  4761. #else                    /* Otherwise take a chance: */
  4762. #define    major(dev) ( (int) ( ((unsigned)(dev) >> 8) & 0xff))
  4763. #define    minor(dev) ( (int) ( (dev) & 0xff))
  4764. #endif /* SUNOS4 */
  4765. #endif /* SVR4 */
  4766. #endif /* major */
  4767. #endif /* LFDEVNO */
  4768.  
  4769. /* No advisory locks if F_TLOCK and F_ULOCK are not defined at this point */
  4770.  
  4771. #ifdef LOCKF
  4772. #ifndef F_TLOCK
  4773. #undef LOCKF
  4774. #ifndef NOLOCKF
  4775. #define NOLOCKF
  4776. #endif /* NOLOCKF */
  4777. #endif /* F_TLOCK */
  4778. #endif /* LOCKF */
  4779.  
  4780. #ifdef LOCKF
  4781. #ifndef F_ULOCK
  4782. #undef LOCKF
  4783. #ifndef NOLOCKF
  4784. #define NOLOCKF
  4785. #endif /* NOLOCKF */
  4786. #endif /* F_ULOCK */
  4787. #endif /* LOCKF */
  4788.  
  4789. static char linkto[DEVNAMLEN+1];
  4790. static char * linkdev = NULL;
  4791.  
  4792. #ifndef NOUUCP
  4793. #ifdef USETTYLOCK
  4794. #ifdef LOCK_DIR
  4795. char * uucplockdir = LOCK_DIR;
  4796. #else
  4797. char * uucplockdir = "";
  4798. #endif /* LOCK_DIR */
  4799. #else
  4800. #ifdef LOCK_DIR
  4801. char * uucplockdir = LOCK_DIR;
  4802. #else
  4803. char * uucplockdir = "";
  4804. #endif /* LOCK_DIR */
  4805. #endif /* USETTYLOCK */
  4806. #else
  4807. char * uucplockdir = "";
  4808. #endif /* NOUUCP */
  4809.  
  4810. #ifdef QNX                /* Only for QNX4 */
  4811. int                    /* Visible to outside world */
  4812. qnxopencount() {            /* Get QNX device open count */
  4813.     struct _dev_info_entry info;
  4814.     int x;
  4815.  
  4816.     x = -1;                /* Unknown */
  4817.     if (ttyfd > -1) {
  4818.     if (!dev_info(ttyfd, &info)) {
  4819.         debug(F101,"ttlock QNX open_count","",info.open_count);
  4820.         x = info.open_count;
  4821.     }
  4822.     }
  4823.     return(x);
  4824. }
  4825. #endif /* QNX */
  4826.  
  4827. char *
  4828. ttglckdir() {                /* Get Lockfile directory name */
  4829. #ifdef __OpenBSD__
  4830.     return("/var/spool/lock");
  4831. #else /* __OpenBSD__ */
  4832. #ifdef __FreeBSD__
  4833.     return("/var/spool/lock");
  4834. #else  /* __FreeBSD__ */
  4835. #ifdef LOCK_DIR
  4836.     char * s = LOCK_DIR;
  4837. #endif /* LOCK_DIR */
  4838. #ifdef NOUUCP
  4839.     return("");
  4840. #else  /* NOUUCP */
  4841. #ifdef LOCK_DIR
  4842.     return(s);
  4843. #else  /* LOCK_DIR */
  4844.     return("");
  4845. #endif /* LOCK_DIR */
  4846. #endif /* NOUUCP */
  4847. #endif /* __FreeBSD__ */
  4848. #endif /* __OpenBSD__ */
  4849. }
  4850.  
  4851. static int
  4852. ttlock(ttdev) char *ttdev; {
  4853.  
  4854.     int x, n;
  4855.     int islink = 0;
  4856.  
  4857. #ifdef NOUUCP
  4858.     debug(F100,"ttlock NOUUCP","",0);
  4859.     ckstrncpy(flfnam,"NOLOCK",FLFNAML);
  4860.     haslock = 1;
  4861.     return(0);
  4862. #else /* !NOUUCP */
  4863.  
  4864. #ifdef USETTYLOCK
  4865.     haslock = 0;                        /* Not locked yet. */
  4866.     *flfnam = '\0';            /* Lockfile name is empty. */
  4867.     if (!strncmp(ttdev,"/dev/",5) && ttdev[5])
  4868.       ckstrncpy(lockname,ttdev+5,DEVNAMLEN);
  4869.     else
  4870.       ckstrncpy(lockname,ttdev,DEVNAMLEN);
  4871. /*
  4872.   This might be overkill, but it's not clear from the man pages whether
  4873.   ttylock() can be called without calling ttylocked() first, since the doc
  4874.   says that ttylocked() removes any stale lockfiles, but it does not say this
  4875.   about ttylock().  Also the docs don't say what ttylocked() returns in the
  4876.   case when it finds and removes a stale lockfile.  So one or both calls to
  4877.   to ttylocked() might be superfluous, but they should do no harm.  Also I'm
  4878.   assuming that we have to do all the same ID swapping, etc, with these
  4879.   routines as we do without them.  Thus the priv_on/off() sandwich.
  4880. */
  4881. #ifdef USE_UU_LOCK
  4882.     priv_on();                /* Turn on privs */
  4883.     x = uu_lock(lockname);        /* Try to set the lock */
  4884.     priv_off();                /* Turn privs off */
  4885.     debug(F111,"ttlock uu_lock",lockname,x);
  4886.     switch (x) {
  4887.       case UU_LOCK_INUSE:
  4888.     return(-2);
  4889.       case UU_LOCK_OK:
  4890. #ifdef BSD44
  4891.     ckmakmsg(flfnam,FLFNAML,"/var/spool/lock/LCK..",lockname,NULL,NULL);
  4892. #endif /* BSD44 */
  4893.     haslock = 1;
  4894.     return(0);
  4895.       default:
  4896.     return(-1);
  4897.     }
  4898. #else  /* USE_UU_LOCK */
  4899.     priv_on();                /* Turn on privs */
  4900.     if (ttylocked(lockname)) {        /* This should remove any stale lock */
  4901.     if (ttylocked(lockname)) {    /* so check again. */
  4902.         priv_off();
  4903.         return(-5);            /* Still locked, fail. */
  4904.     }
  4905.     }
  4906.     x = ttylock(lockname);        /* Lock it. */
  4907.     priv_off();                /* Turn off privs */
  4908.  
  4909.     debug(F111,"ttlock lockname",lockname,x);
  4910.     if (x > -1) {
  4911.     /*
  4912.       We don't really know the name of the lockfile, but
  4913.       this is what the man page says it is.  In USETTYLOCK
  4914.           builds, it is used only for display by SHOW COMM.
  4915.     */
  4916.     ckmakmsg(flfnam,FLFNAML,"/etc/locks/LCK..",lockname,NULL,NULL);
  4917.     haslock = 1;
  4918.     }
  4919.     return(x);
  4920. #endif /* USE_UU_LOCK */
  4921. #else  /* Systems that don't have ttylock()... */
  4922.  
  4923. #ifndef HPUX
  4924.  
  4925.     int lockfd;                /* File descriptor for lock file. */
  4926.     PID_T pid;                /* Process id of this process. */
  4927.     int tries;                /* How many times we've tried... */
  4928.     struct stat devbuf;            /* For device numbers (SVR4). */
  4929.  
  4930. #ifdef PIDSTRING
  4931.     char pid_str[32];            /* My pid in string format. */
  4932. #endif /* PIDSTRING */
  4933.  
  4934.     char *device, *devname;
  4935.  
  4936. #define LFNAML 256            /* Max length for lock file name. */
  4937.     char lockfil[LFNAML];        /* Lock file name */
  4938. #ifdef RTAIX
  4939.     char lklockf[LFNAML];        /* Name for link to lock file  */
  4940. #endif /* RTAIX */
  4941. #ifdef CKSYMLINK
  4942.     char symlock[LFNAML];        /* Name for symlink lockfile name */
  4943. #endif /* CKSYMLINK */
  4944.     char tmpnam[LFNAML+30];        /* Temporary lockfile name. */
  4945.     char *lockdir = LOCK_DIR;        /* Defined near top of this file, */
  4946.                     /* or on cc command line. */
  4947.     haslock = 0;                        /* Not locked yet. */
  4948.     *flfnam = '\0';            /* Lockfile name is empty. */
  4949.     lock2[0] = '\0';            /* Clear secondary lockfile name. */
  4950.     pid = getpid();            /* Get id of this process. */
  4951.  
  4952. /*  Construct name of lockfile and temporary file */
  4953.  
  4954. /*  device  = name of tty device without the path, e.g. "ttyh8" */
  4955. /*  lockfil = name of lock file, without path, e.g. "LCK..ttyh8" */
  4956.  
  4957.     device = ((devname = xxlast(ttdev,'/')) != NULL ? devname+1 : ttdev);
  4958.  
  4959.     if (stat(ttdev,&devbuf) < 0)
  4960.       return(-1);
  4961.  
  4962. #ifdef CKSYMLINK
  4963.     islink = 1;                /* Assume it's a symlink */
  4964.     linkto[0] = '\0';            /* But we don't know to what */
  4965. #ifdef COMMENT
  4966. /*
  4967.   This is undependable.  If it worked it would save the readlink call if
  4968.   we knew the device name was not a link.
  4969. */
  4970. #ifdef S_ISLNK
  4971.     islink = S_ISLNK(devbuf.st_mode);
  4972.     debug(F101,"ttlock stat S_ISLNK","",islink);
  4973. #endif /* S_ISLNK */
  4974. #endif /* COMMENT */
  4975.     if (islink) {
  4976.     n = readlink(ttdev,linkto,DEVNAMLEN); /* See if it's a link */
  4977.     debug(F111,"ttlock readlink",ttdev,n);
  4978.     if (n > -1)            /* It is */
  4979.       linkto[n] = '\0';
  4980.     else                /* It's not */
  4981.       islink = 0;
  4982.     debug(F111,"ttlock link",linkto,islink);
  4983.     }
  4984.     if (islink) {
  4985.     linkdev = (devname = xxlast(linkto,'/')) ? devname + 1 : linkto;
  4986.     debug(F110,"ttlock linkdev",linkdev,0);
  4987.     }
  4988. #endif /* CKSYMLINK */
  4989.  
  4990. /*
  4991.   On SCO platforms, if we don't have a symlink, then let's pretend the
  4992.   name given for the device is a symlink, because later we will change
  4993.   the name if it contains any uppercase characters.
  4994. */
  4995. #ifdef CK_SCOV5                /* SCO Open Server 5.0 */
  4996.     if (!islink) {
  4997.     islink = 1;
  4998.     ckstrncpy(linkto,ttdev,DEVNAMLEN);
  4999.     linkdev = (devname = xxlast(linkto,'/')) ? devname + 1 : linkto;
  5000.     debug(F110,"ttlock linkdev",linkdev,0);
  5001.     }
  5002. #else
  5003. #ifdef M_XENIX                /* SCO Xenix or UNIX */
  5004.     if (!islink) {
  5005.     islink = 1;
  5006.     ckstrncpy(linkto,ttdev,DEVNAMLEN);
  5007.     linkdev = (devname = xxlast(linkto,'/')) ? devname + 1 : linkto;
  5008.     debug(F110,"ttlock linkdev",linkdev,0);
  5009.     }
  5010. #endif /* M_XENIX */
  5011. #endif /* CK_SCOV5 */
  5012.  
  5013. #ifdef ISIII                /* Interactive System III, PC/IX */
  5014.     ckstrncpy(lockfil, device, DEVNAMLEN);
  5015. #else  /* not ISIII */
  5016. #ifdef LFDEVNO                /* Lockfilename has device numbers. */
  5017. #ifdef COHERENT
  5018.     sprintf(lockfil,"LCK..%d.%d",    /* SAFE */
  5019.         major(devbuf.st_rdev),       /* major device number */
  5020.         0x1f & minor(devbuf.st_rdev)); /* minor device number */
  5021. #else
  5022.     /* Note: %d changed to %u in 8.0 -- %u is part of SVID for SVR4 */
  5023.     /* Lockfile name format verified to agree with Solaris cu, Dec 2001 */
  5024.     sprintf(lockfil,"LK.%03u.%03u.%03u", /* SAFE */
  5025.         major(devbuf.st_dev),    /* device */
  5026.         major(devbuf.st_rdev),    /* major device number */
  5027.         minor(devbuf.st_rdev));    /* minor device number */
  5028. #endif /* COHERENT */
  5029. #else  /* Not LFDEVNO */
  5030. #ifdef PTX                /* Dynix PTX */
  5031.     if ((device != &ttdev[5]) && (strncmp(ttdev,"/dev/",5) == 0)) {
  5032.     if ((int)strlen(device) + 8 < LFNAML)
  5033.       sprintf(lockfil,"LCK..%.3s%s", &ttdev[5], device);
  5034.     else
  5035.       ckstrncpy(lockfil,"LOCKFILE_NAME_TOO_LONG",LFNAML);
  5036.     } else
  5037. #endif /* PTX */
  5038.       if ((int)strlen(device) + 5 < LFNAML)
  5039.     sprintf(lockfil,"LCK..%s", device);
  5040.       else
  5041.     ckstrncpy(lockfil,"LOCKFILE_NAME_TOO_LONG",LFNAML);
  5042. #ifdef RTAIX
  5043.     ckstrncpy(lklockf,device,DEVNAMLEN);
  5044. #endif /* RTAIX */
  5045. #ifdef CKSYMLINK
  5046.     symlock[0] = '\0';
  5047.     if (islink)
  5048.       ckmakmsg(symlock,LFNAML, "LCK..", linkdev, NULL, NULL);
  5049. #endif /* CKSYMLINK */
  5050. #endif /* LFDEVNO */
  5051. #endif /* ISIII */
  5052.  
  5053. #ifdef CK_SCOV5                /* SCO Open Server 5.0 */
  5054.     {
  5055.     /* Lowercase the entire filename. */
  5056.         /* SCO says we must do this in V5.0 and later. */
  5057.     /* BUT... watch out for devices -- like Digiboard Portserver */
  5058.     /* That can have hundreds of ports... */
  5059.     char *p = (char *)(lockfil + 5);
  5060.     while (*p) { if (isupper(*p)) *p = (char) tolower(*p); p++; }
  5061.     }
  5062. #ifdef CKSYMLINK
  5063.     if (islink) {            /* If no change */
  5064.     if (!strcmp(lockfil,symlock)) {    /* then no second lockfile needed */
  5065.         islink = 0;
  5066.         symlock[0] = '\0';
  5067.     }
  5068.     }
  5069. #endif /* CKSYMLINK */
  5070. #else
  5071. #ifdef M_XENIX                /* SCO Xenix or UNIX */
  5072.     {
  5073.     int x; char c;
  5074.     x = (int)strlen(lockfil) - 1;    /* Get last letter of device name. */
  5075.     if (x > 0) {            /* If it's uppercase, lower it. */
  5076.         c = lockfil[x];
  5077.         if (c >= 'A' && c <= 'Z') lockfil[x] += ('a' - 'A');
  5078.     }
  5079.     }
  5080. #ifdef CKSYMLINK
  5081.     if (islink) {
  5082.     if (!strcmp(lockfil,symlock)) {    /* No change */
  5083.         islink = 0;            /* so no second lockfile */
  5084.         symlock[0] = '\0';
  5085.     }
  5086.     }
  5087. #endif /* CKSYMLINK */
  5088. #endif /* M_XENIX */
  5089. #endif /* CK_SCOV5 */
  5090.  
  5091. /*  flfnam = full lockfile pathname, e.g. "/usr/spool/uucp/LCK..ttyh8" */
  5092. /*  tmpnam = temporary unique, e.g. "/usr/spool/uucp/LTMP..pid" */
  5093.  
  5094.     ckmakmsg(flfnam,LFNAML,lockdir,"/",lockfil,NULL);
  5095.  
  5096. #ifdef RTAIX
  5097.     ckmakmsg(lkflfn,FLFNAML,lockdir,"/",lklockf,NULL);
  5098. #endif /* RTAIX */
  5099.  
  5100. #ifndef LFDEVNO
  5101. #ifdef CKSYMLINK
  5102.     /* If it's a link then also make a lockfile for the real name */
  5103.     debug(F111,"ttlock link symlock",symlock,islink);
  5104.     if (islink && symlock[0]) {
  5105.     /* But only if the lockfile names would be different. */
  5106.     /* WARNING: They won't be, e.g. for /dev/ttyd2 => /hw/ttys/ttyd2 */
  5107.     ckmakmsg(lock2,FLFNAML,lockdir,"/",symlock,NULL);
  5108.     debug(F110,"ttlock lock2",lock2,0);
  5109.     if (!strcmp(lock2,flfnam)) {    /* Are lockfile names the same? */
  5110.         debug(F100,"ttlock lock2 cleared","",0);
  5111.         lock2[0] = '\0';        /* Clear secondary lockfile name. */
  5112.     }
  5113.     }
  5114. #endif /* CKSYMLINK */
  5115. #endif /* LFDEVNO */
  5116.  
  5117.     sprintf(tmpnam,"%s/LTMP.%05d",lockdir,(int) pid); /* safe */
  5118.     debug(F110,"ttlock flfnam",flfnam,0);
  5119.     debug(F110,"ttlock tmpnam",tmpnam,0);
  5120.  
  5121.     priv_on();                /* Turn on privileges if possible. */
  5122.     lockfd = creat(tmpnam, 0444);    /* Try to create temp lock file. */
  5123.     if (lockfd < 0) {            /* Create failed. */
  5124.     debug(F111,"ttlock creat failed",tmpnam,errno);
  5125.     if (errno == ENOENT) {
  5126.         perror(lockdir);
  5127.         printf("UUCP not installed or Kermit misconfigured\n");
  5128.     } else {
  5129.         if (!quiet)
  5130.           perror(lockdir);
  5131.         unlink(tmpnam);        /* Get rid of the temporary file. */
  5132.     }
  5133.     priv_off();            /* Turn off privileges!!! */
  5134.     return(-1);            /* Return failure code. */
  5135.     }
  5136. /* Now write the pid into the temp lockfile in the appropriate format */
  5137.  
  5138. #ifdef PIDSTRING            /* For Honey DanBer UUCP, */
  5139.     sprintf(                /* write PID as decimal string */
  5140.         pid_str,
  5141. #ifdef LINUXFSSTND            /* The "Linux File System Standard" */
  5142. #ifdef FSSTND10                /* Version 1.0 calls for */
  5143.         "%010d\n",            /* leading zeros */
  5144. #else                    /* while version 1.2 calls for */
  5145.         "%10d\n",            /* leading spaces */
  5146. #endif /* FSSTND10 */
  5147. #else
  5148. #ifdef COHERENT
  5149.         "%d\n",            /* with leading nothing */
  5150. #else
  5151.         "%10d\n",            /* with leading blanks */
  5152. #endif /* COHERENT */
  5153. #endif /* LINUXFSSTND */
  5154.         (int) pid
  5155.         );                /* safe */
  5156.     write(lockfd, pid_str, 11);
  5157.     debug(F111,"ttlock hdb pid string",pid_str,(int) pid);
  5158.  
  5159. #else /* Not PIDSTRING, use integer PID */
  5160.  
  5161.     write(lockfd, (char *)&pid, sizeof(pid) );
  5162.     debug(F101,"ttlock pid","",(int) pid);
  5163.  
  5164. #endif /* PIDSTRING */
  5165.  
  5166. /* Now try to rename the temp file to the real lock file name. */
  5167. /* This will fail if a lock file of that name already exists.  */
  5168.  
  5169.     close(lockfd);            /* Close the temp lockfile. */
  5170.     chmod(tmpnam,0444);            /* Permission for a valid lock. */
  5171.     tries = 0;
  5172.     while (!haslock && tries++ < 2) {
  5173.     haslock = (link(tmpnam,flfnam) == 0); /* Create a link to it. */
  5174.     if (haslock) {                  /* If we got the lockfile */
  5175. #ifdef RTAIX
  5176.         link(flfnam,lkflfn);
  5177. #endif /* RTAIX */
  5178. #ifdef CKSYMLINK
  5179. #ifndef LFDEVNO
  5180.         if (islink && lock2[0])
  5181.           link(flfnam,lock2);
  5182. #endif /* LFDEVNO */
  5183. #endif /* CKSYMLINK */
  5184.  
  5185. #ifdef COMMENT
  5186. /* Can't do this any more because device is not open yet so no ttyfd. */
  5187. #ifdef LOCKF
  5188. /*
  5189.   Advisory file locking works on SVR4, so we use it.  In fact, it is
  5190.   necessary in some cases, e.g. when SLIP is involved.  But it still doesn't
  5191.   seem to prevent multiple users accessing the same device by different names.
  5192. */
  5193.             while (lockf(ttyfd, F_TLOCK, 0L) != 0) {
  5194.                 debug(F111, "ttlock lockf returns errno", "", errno);
  5195.                 if ((++tries >= 3) || (errno != EAGAIN)) {
  5196.                     x = unlink(flfnam); /* remove the lockfile */
  5197. #ifdef RTAIX
  5198.             unlink(lkflfn);    /* And any links to it... */
  5199. #endif /* RTAIX */
  5200. #ifdef CKSYMLINK
  5201. #ifndef LFDEVNO
  5202.             if (islink && lock2[0])
  5203.               unlink(lock2);    /* ditto... */
  5204. #endif /* LFDEVNO */
  5205. #endif /* CKSYMLINK */
  5206.                     debug(F111,"ttlock unlink",flfnam,x);
  5207.                     haslock = 0;
  5208.             break;
  5209.         }
  5210.                 sleep(2);
  5211.         }
  5212.         if (haslock)        /* If we got an advisory lock */
  5213. #endif /* LOCKF */
  5214. #endif /* COMMENT */
  5215.           break;            /* We're done. */
  5216.  
  5217.     } else {            /* We didn't create a new lockfile. */
  5218.         priv_off();
  5219.         if (ttchkpid(flfnam)) {    /* Check existing lockfile */
  5220.         priv_on();        /* cause ttchkpid turns priv_off... */
  5221.         unlink(tmpnam);        /* Delete the tempfile */
  5222.         debug(F100,"ttlock found tty locked","",0);
  5223.         priv_off();        /* Turn off privs */
  5224.         return(-2);        /* Code for device is in use. */
  5225.         }
  5226.         priv_on();
  5227.     }
  5228.     }
  5229.     unlink(tmpnam);            /* Unlink (remove) the temp file. */
  5230.     priv_off();                /* Turn off privs */
  5231.     return(haslock ? 0 : -1);        /* Return link's return code. */
  5232.  
  5233. #else /* HPUX */
  5234.  
  5235. /*
  5236.   HP-UX gets its own copy of this routine, modeled after the observed behavior
  5237.   of the HP-UX 'cu' program.  HP-UX serial device names consist of a base name
  5238.   such as "tty", "ttyd", "cua", "cul", "cuad", or "culd", followed by a unit
  5239.   designator which is a string of digits, possibly containing an imbedded
  5240.   letter "p".  Examples (for base name "tty"):
  5241.  
  5242.      /dev/tty0, /dev/tty00, dev/ttyd00, /dev/tty0p0
  5243.  
  5244.   According to the HP-UX UUCP manual of 1988, the "0p0" notation has been
  5245.   used on Series 800 since HP-UX 2.00, and the "non-p" notation was used
  5246.   on other models.  In HP-UX 10.00, "0p0" notation was adopted for all models.
  5247.   However, we make and enforce no such distinctions; either notation is
  5248.   accepted on any model or HP-UX version as a valid unit designator.
  5249.  
  5250.   If a valid unit is specified (as opposed to a designer name or symlink), we
  5251.   check for all aliases of the given unit according to the devprefix[] array.
  5252.   If no lockfiles are found for the given unit, we can have the device; we
  5253.   create a lockfile LCK..name in the lockfile directory appropriate for the
  5254.   HP-UX version (/var/spool/locks for 10.00 and later, /usr/spool/uucp for
  5255.   9.xx and earlier).  If it is a "cua" or "cul" device, a second lockfile is
  5256.   created with the "ttyd" prefix.  This is exactly what cu does.
  5257.  
  5258.   If the "set line" device does not have a valid unit designator, then it is
  5259.   used literally and no synomyms are searched for and only one lockfile is
  5260.   created.
  5261.  
  5262.   -fdc, March 1998.
  5263. */
  5264. #define LFNAML 80            /* Max length for lock file name. */
  5265.  
  5266.     int lockfd;                /* File descriptor for lock file. */
  5267.     PID_T pid;                /* Process ID of this process. */
  5268.     int fpid;                /* pid found in existing lockfile. */
  5269.     int tries;                /* How many times we've tried... */
  5270.     int i, k;                /* Workers */
  5271.  
  5272.     char *device, *devname;        /* "/dev/xxx", "xxx" */
  5273.     char *unit, *p;            /* <instance>p<port> part of xxx */
  5274.  
  5275.     char lockfil[LFNAML];        /* Lockfile name (no path) */
  5276.     char tmpnam[LFNAML];        /* Temporary lockfile name. */
  5277.  
  5278. #ifdef HPUX10                /* Lockfile directory */
  5279.     char *lockdir = "/var/spool/locks";    /* Always this for 10.00 and higher */
  5280. #else  /* HP-UX 9.xx and below */
  5281. #ifdef LOCK_DIR
  5282.     char *lockdir = LOCK_DIR;        /* Defined near top of this file */
  5283. #else
  5284.     char *lockdir = "/usr/spool/uucp";    /* or not... */
  5285. #endif /* LOCK_DIR */
  5286. #endif /* HPUX10 */
  5287.  
  5288.     haslock = 0;                        /* Not locked yet. */
  5289.     *flfnam = '\0';            /* Lockfile name is empty. */
  5290.     lock2[0] = '\0';            /* Second one too. */
  5291.     pid = getpid();            /* Get my process ID */
  5292. /*
  5293.   Construct name of lockfile and temporary file...
  5294.   device  = name of tty device without the path, e.g. "tty0p0"
  5295.   lockfil = name of lock file, without path, e.g. "LCK..tty0p0"
  5296. */
  5297.     device = ((devname = xxlast(ttdev,'/')) != NULL ? devname+1 : ttdev);
  5298.     debug(F110,"TTLOCK device",device,0);
  5299.     ckmakmsg(lockfil,LFNAML,"LCK..",device,NULL,NULL);
  5300.  
  5301.     k = 0;                /* Assume device is not locked */
  5302.     n = 0;                /* Digit counter */
  5303.     unit = device;            /* Unit = <instance>p<port> */
  5304.     while (*unit && !isdigit(*unit))    /* Search for digit... */
  5305.       unit++;
  5306.     p = unit;                /* Verify <num>p<num> format... */
  5307.     debug(F110,"TTLOCK unit 1",unit,0);
  5308. /*
  5309.   The unit number is recognized as:
  5310.   (a) any sequence of digits that runs to the end of the string.
  5311.   (b) any (a) that includes one and only one letter "p", with at least
  5312.       one digit before and after it.
  5313. */
  5314.     while (isdigit(*p)) p++, n++;    /* Get a run of digits */
  5315.     if (*p && n > 0) {            /* Have a "p"? */
  5316.     if (*p == 'p' && isdigit(*(p+1))) {
  5317.         p++;
  5318.         n = 0;
  5319.         while (isdigit(*p)) p++, n++;
  5320.     }
  5321.     }
  5322.     if (n == 0 || *p) unit = "";
  5323.     debug(F110,"TTLOCK unit 2",unit,0);
  5324.  
  5325.     if (*unit) {            /* Device name has unit number. */
  5326.     /* The following loop not only searches for the various lockfile    */
  5327.     /* synonyms, but also removes all -- not just one -- stale lockfile */
  5328.     /* for the device, should there be more than one.  See ttchkpid().  */
  5329.     ttydexists = 0;
  5330.     for (i = 0; *devprefix[i]; i++) { /* For each driver... */
  5331.         /* Make device name */
  5332.         ckmakmsg(lock2,FLFNAML,"/dev/",devprefix[i],unit,NULL);
  5333.         priv_on();            /* Privs on */
  5334.         k = zchki(lock2) != -1;    /* See if device exists */
  5335.         priv_off();            /* Privs off */
  5336.         debug(F111,"TTLOCK exist",lock2,k);
  5337.             if (k) {
  5338.         if (!strcmp(devprefix[i],"ttyd")) /* ttyd device exists */
  5339.           ttydexists = 1;
  5340.         /* Make lockfile name */
  5341.         ckmakmsg(lock2,FLFNAML,lockdir,"/LCK..",devprefix[i],unit);
  5342.         debug(F110,"TTLOCK checking",lock2,0);
  5343.         priv_on();        /* Privs on */
  5344.         k = zchki(lock2) != -1;    /* See if lockfile exists */
  5345.         priv_off();        /* Privs off */
  5346.         debug(F111,"TTLOCK check for lock A",lock2,k);
  5347.         if (k) if (ttchkpid(lock2)) { /* If pid still active, fail. */
  5348.             ckstrncpy(flfnam,lock2,FLFNAML);
  5349.             return(-2);
  5350.         }
  5351.         }
  5352.     }
  5353.     } else {                /* Some other device-name format */
  5354.     /* This takes care of symbolic links, etc... */
  5355.     /* But does not chase them down! */
  5356.     ckmakmsg(lock2,FLFNAML,lockdir,"/LCK..",device,NULL);
  5357.     priv_on();
  5358.     k = zchki(lock2) != -1;        /* Check for existing lockfile */
  5359.     priv_off();
  5360.     debug(F111,"TTLOCK check for lock B",lock2,k);
  5361.     if (k) if (ttchkpid(lock2)) {    /* Check pid from lockfile */
  5362.         ckstrncpy(flfnam,lock2,FLFNAML);
  5363.         debug(F110,"TTLOCK in use",device,0);
  5364.         debug(F101,"TTLOCK returns","",-2);
  5365.         return(-2);
  5366.     }
  5367.     }
  5368. /*
  5369.   Get here only if there is no (more) lockfile, so now we make one (or two)...
  5370.   flfnam = full lockfile pathname, e.g. "/usr/spool/uucp/LCK..cul0p0".
  5371.   tmpnam = unique temporary filname, e.g. "/usr/spool/uucp/LTMP..pid".
  5372. */
  5373.     ckmakmsg(flfnam,FLFNAML,lockdir,"/",lockfil,NULL); /* SET LINE device */
  5374.  
  5375.     /* If dialout device, also make one for corresponding dialin device */
  5376.     lock2[0] = '\0';
  5377.     if (!strncmp(device,"cu",2) && *unit && ttydexists)
  5378.       ckmakmsg(lock2,FLFNAML,lockdir,"/LCK..ttyd",unit,NULL);
  5379.  
  5380.     if ((int)strlen(lockdir)+12 < LFNAML)
  5381.       sprintf(tmpnam,"%s/LTMP.%05d",lockdir,(int) pid); /* Make temp name */
  5382. #ifdef DEBUG
  5383.     if (deblog) {
  5384.     debug(F110,"TTLOCK flfnam",flfnam,0);
  5385.     debug(F110,"TTLOCK lock2",lock2,0);
  5386.     debug(F110,"TTLOCK tmpnam",tmpnam,0);
  5387.     }
  5388. #endif /* DEBUG */
  5389. /*
  5390.    Lockfile permissions...
  5391.    444 is standard, HP-UX 10.00 uses 664.  It doesn't matter.
  5392.    Kermit uses 444; the difference lets us tell whether Kermit created
  5393.    the lock file.
  5394. */
  5395.     priv_on();                /* Turn on privileges. */
  5396.     lockfd = creat(tmpnam, 0444);    /* Try to create temporary file. */
  5397.     if (lockfd < 0) {            /* Create failed. */
  5398.     debug(F111,"TTLOCK creat failed",tmpnam,errno);
  5399.     if (errno == ENOENT) {
  5400.         perror(lockdir);
  5401.         printf("UUCP not installed or Kermit misconfigured\n");
  5402.     } else {
  5403.         if (!quiet)
  5404.           perror(lockdir);
  5405.         unlink(tmpnam);        /* Get rid of the temporary file. */
  5406.     }
  5407.     priv_off();            /* Turn off privileges!!! */
  5408.     debug(F101,"TTLOCK returns","",-1);
  5409.     return(-1);            /* Return failure code. */
  5410.     }
  5411.     debug(F110,"TTLOCK temp ok",tmpnam,0);
  5412.  
  5413. /* Now write our pid into the temp lockfile in integer format. */
  5414.  
  5415.     i = write(lockfd, (char *)&pid, sizeof(pid));
  5416.  
  5417. #ifdef DEBUG
  5418.     if (deblog) {
  5419.     debug(F101,"TTLOCK pid","",pid);
  5420.     debug(F101,"TTLOCK sizeof pid","",sizeof(pid));
  5421.     debug(F101,"TTLOCK write pid returns","",i);
  5422.     }
  5423. #endif /* DEBUG */
  5424.  
  5425. /*
  5426.   Now try to rename the temporary file to the real lockfile name.
  5427.   This will fail if a lock file of that name already exists, which
  5428.   will catch race conditions with other users.
  5429. */
  5430.     close(lockfd);            /* Close the temp lockfile. */
  5431.     chmod(tmpnam,0444);
  5432.  
  5433.     tries = 0;
  5434.     while (!haslock && tries++ < 2) {
  5435.     haslock = (link(tmpnam,flfnam) == 0); /* Create a link to it. */
  5436.     debug(F101,"TTLOCK link","",haslock);
  5437.     if (haslock) {            /* If we made the lockfile... */
  5438.  
  5439. #ifdef COMMENT
  5440. /* We can't do this any more because we don't have a file descriptor yet. */
  5441. #ifdef LOCKF                /* Can be canceled with -DNOLOCKF */
  5442. /*
  5443.   Create an advisory lock on the device through its file descriptor.
  5444.   This code actually seems to work.  If it is executed, and then another
  5445.   process tries to open the same device under a different name to circumvent
  5446.   the lockfile, they get a "device busy" error.
  5447. */
  5448.         debug(F100,"TTLOCK LOCKF code...","",0);
  5449.             while ( lockf(ttyfd, F_TLOCK, 0L) != 0 ) {
  5450.                 debug(F111, "TTLOCK lockf error", "", errno);
  5451.                 if ((++tries >= 3) || (errno != EAGAIN)) {
  5452.                     x = unlink(flfnam); /* Remove the lockfile */
  5453.             if (errno == EACCES && !quiet)
  5454.               printf("Device already locked by another process\n");
  5455.                     haslock = 0;
  5456.             break;
  5457.         }
  5458.                 sleep(2);
  5459.         }
  5460. #endif /* LOCKF */
  5461. #endif /* COMMENT */
  5462.  
  5463.         if (haslock) {        /* If we made the lockfile ... */
  5464.         if (lock2[0]) {        /* if there is to be a 2nd lockfile */
  5465.             lockfd = creat(lock2, 0444); /* Create it */
  5466.             debug(F111,"TTLOCK lock2 creat", lock2, lockfd);
  5467.             if (lockfd > -1) {    /* Created OK, write pid. */
  5468.             write(lockfd, (char *)&pid, sizeof(pid) );
  5469.             close(lockfd);    /* Close and */
  5470.             chmod(lock2, 0444); /* set permissions. */
  5471.             } else {         /* Not OK, but don't fail. */
  5472.             lock2[0] = '\0'; /* Just remember it's not there. */
  5473.             }
  5474.         }
  5475.         break;            /* and we're done. */
  5476.         }
  5477.     }
  5478.     }
  5479.     unlink(tmpnam);            /* Unlink (remove) the temp file. */
  5480.     priv_off();                /* Turn off privs */
  5481.     i = haslock ? 0 : -1;        /* Our return value */
  5482.     debug(F101,"TTLOCK returns","",i);
  5483.     return(i);
  5484. #endif /* HPUX */
  5485. #endif /* USETTYLOCK */
  5486. #endif /* !NOUUCP */
  5487. }
  5488.  
  5489. /*  T T U N L O C K  */
  5490.  
  5491. static int
  5492. ttunlck() {                             /* Remove UUCP lockfile(s). */
  5493. #ifndef NOUUCP
  5494.     int x;
  5495.  
  5496.     debug(F111,"ttunlck",flfnam,haslock);
  5497.  
  5498. #ifdef USETTYLOCK
  5499.  
  5500.     if (haslock && *flfnam) {
  5501.     int x;
  5502.     priv_on();            /* Turn on privs */
  5503. #ifdef USE_UU_LOCK
  5504.     x = uu_unlock(lockname);
  5505. #else  /* USE_UU_LOCK */
  5506.     x = ttyunlock(lockname);    /* Try to unlock */
  5507. #endif /* USE_UU_LOCK */
  5508.     priv_off();            /* Turn off privs */
  5509.     if (x < 0 && !quiet)
  5510.       printf("Warning - Can't remove lockfile: %s\n", flfnam);
  5511.  
  5512.     *flfnam = '\0';            /* Erase the name. */
  5513.     haslock = 0;
  5514.     return(0);
  5515.     }
  5516.  
  5517. #else  /* No ttylock()... */
  5518.  
  5519.     if (haslock && *flfnam) {
  5520.     /* Don't remove lockfile if we didn't make it ourselves */
  5521.     if ((x = ttrpid(flfnam)) != (int)getpid()) {
  5522.         debug(F111,"ttunlck lockfile seized",flfnam,x);
  5523.         printf("Warning - Lockfile %s seized by pid %d\n",
  5524.            flfnam,
  5525.            x
  5526.            );
  5527.         return(0);
  5528.     }
  5529.     priv_on();            /* Turn privileges on.  */
  5530.     errno = 0;
  5531.     x = unlink(flfnam);        /* Remove the lockfile. */
  5532.     debug(F111,"ttunlck unlink",flfnam,x);
  5533.     if (x < 0) {
  5534.         if (errno && !quiet)
  5535.           perror(ttnmsv);
  5536.         printf("Warning - Can't remove lockfile: %s\n", flfnam);
  5537.     }
  5538.     haslock = 0;
  5539.     *flfnam = '\0';            /* Erase the name. */
  5540.  
  5541. #ifdef RTAIX
  5542.     errno = 0;
  5543.     x = unlink(lkflfn);        /* Remove link to lockfile */
  5544.     debug(F111,"ttunlck AIX link unlink",lkflfn,x);
  5545.     if (x < 0) {
  5546.         if (errno && !quiet)
  5547.           perror(ttnmsv);
  5548.         printf("Warning - Can't remove link to lockfile: %s\n", lkflfn);
  5549.     }
  5550.     *lkflfn = '\0';
  5551. #else
  5552.     if (lock2[0]) {            /* If there is a second lockfile, */
  5553.         errno = 0;
  5554.         x = unlink(lock2);        /*  remove it too. */
  5555.         debug(F111,"ttunlck lock2 unlink",lock2,x);
  5556.         if (x < 0) {
  5557.         if (errno && !quiet)
  5558.           perror(ttnmsv);
  5559.         printf("Warning - Can't remove secondary lockfile: %s\n",
  5560.                lock2
  5561.                );
  5562.         }
  5563.         lock2[0] = '\0';        /* Forget its name. */
  5564.     }
  5565. #endif /* RTAIX */
  5566.  
  5567. #ifdef COMMENT
  5568. #ifdef LOCKF
  5569.         (VOID) lockf(ttyfd, F_ULOCK, 0L); /* Remove advisory lock */
  5570. #endif /* LOCKF */
  5571. #endif /* COMMENT */
  5572.  
  5573.     priv_off();            /* Turn privileges off. */
  5574.     }
  5575. #endif /* USETTYLOCK */
  5576. #endif /* !NOUUCP */
  5577.     return(0);
  5578. }
  5579.  
  5580. /*
  5581.   4.3BSD-style UUCP line direction control.
  5582.   (Stan Barber, Rice U, 1980-something...)
  5583. */
  5584. #ifndef NOUUCP
  5585. #ifdef ACUCNTRL
  5586. VOID
  5587. acucntrl(flag,ttname) char *flag, *ttname; {
  5588.     char x[DEVNAMLEN+32], *device, *devname;
  5589.  
  5590.     if (strcmp(ttname,CTTNAM) == 0 || xlocal == 0) /* If not local, */
  5591.       return;                /* just return. */
  5592.     device = ((devname = xxlast(ttname,'/')) != NULL ? devname+1 : ttname);
  5593.     if (strncmp(device,"LCK..",4) == 0) device += 5;
  5594.     ckmakmsg(x,DEVNAMLEN+32,"/usr/lib/uucp/acucntrl ",flag," ",device);
  5595.     debug(F110,"called ",x,0);
  5596.     zsyscmd(x);
  5597. }
  5598. #endif /* ACUCNTRL */
  5599. #endif /* NOUUCP */
  5600.  
  5601. /*
  5602.   T T H F L O W  --  Set or Reset hardware flow control.
  5603.  
  5604.   This is an attempt to collect all hardware-flow-control related code
  5605.   into a single module.  Thanks to Rick Sladkey and John Kohl for lots of
  5606.   help here.  Overview:
  5607.  
  5608.   Hardware flow control is not supported in many UNIX implementions.  Even
  5609.   when it is supported, there is no (ha ha) "standard" for the programming
  5610.   interface.  In general, 4.3BSD and earlier (sometimes), 4.4BSD, System V,
  5611.   SunOS, AIX, etc, have totally different methods.  (And, not strictly
  5612.   relevant here, the programming interface often brings one only to a no-op
  5613.   in the device driver!)
  5614.  
  5615.   Among all these, we have two major types of APIs: those in which hardware
  5616.   flow control is determined by bits in the same termio/termios/sgtty mode
  5617.   word(s) that are used for controlling such items as CBREAK vs RAW mode, and
  5618.   which are also used by the ttvt(), ttpkt(), conbin(), and concb() routines
  5619.   for changing terminal modes.  And those that use entirely different
  5620.   mechanisms.
  5621.  
  5622.   In the first category, it is important that any change in the mode bits be
  5623.   reflected in the relevant termio(s)/sgtty structure, so that subsequent
  5624.   changes to that structure do not wipe out the effects of this routine.  That
  5625.   is why a pointer, attrs, to the appropriate structure is passed as a
  5626.   parameter to this routine.
  5627.  
  5628.   The second category should give us no worries, since any changes to hardware
  5629.   flow control accomplished by this routine should not affect the termio(s)/
  5630.   sgtty structures, and therefore will not be undone by later changes to them.
  5631.  
  5632.   The second argument, status, means to turn on hardware flow control if
  5633.   nonzero, and to turn it off if zero.
  5634.  
  5635.   Returns: 0 on apparent success, -1 on probable failure.
  5636. */
  5637.  
  5638. /*
  5639.   The following business is for BSDI, where it was discovered that two
  5640.   separate bits, CCTS_OFLOW and CRTS_IFLOW, are used in hardware flow control,
  5641.   but CTRSCTS is defined (in <termios.h>) to be just CCTS_OFLOW rather both
  5642.   bits, so hwfc only works in one direction if you use CRTSCTS to control it.
  5643.   Other 4.4BSD-based Unixes such as FreeBSD 4.1, which use these two bits,
  5644.   define CRTSCTS correctly.
  5645. */
  5646. #ifdef FIXCRTSCTS
  5647. #ifdef CRTSCTS
  5648. #ifdef CCTS_OFLOW
  5649. #ifdef CRTS_IFLOW
  5650. #undef CRTSCTS
  5651. #define CRTSCTS (CRTS_IFLOW|CCTS_OFLOW)
  5652. #endif /* CRTS_IFLOW */
  5653. #endif /* CCTS_OFLOW */
  5654. #endif /* CRTSCTS */
  5655. #endif /* FIXCRTSCTS */
  5656.  
  5657. static int
  5658. tthflow(flow, status, attrs)
  5659.     int flow,                /* Type of flow control (ckcdeb.h) */
  5660.     status;                /* Nonzero = turn it on */
  5661.                     /* Zero = turn it off */
  5662. #ifdef BSD44ORPOSIX            /* POSIX or BSD44 */
  5663.     struct termios *attrs;
  5664. #else                    /* System V */
  5665. #ifdef ATTSV
  5666. #ifdef ATT7300
  5667. #ifdef UNIX351M
  5668. /* AT&T UNIX 3.51m can set but not test for hardware flow control */
  5669. #define RTSFLOW CTSCD
  5670. #define CTSFLOW CTSCD
  5671. #endif /* ATT7300 */
  5672. #endif /* UNIX351M */
  5673.     struct termio *attrs;
  5674. #else                    /* BSD, V7, etc */
  5675.     struct sgttyb *attrs;        /* sgtty info... */
  5676. #endif /* ATTSV */
  5677. #endif /* BSD44ORPOSIX */
  5678. /* tthflow */ {
  5679.  
  5680.     int x = 0;                /* tthflow() return code */
  5681.  
  5682. #ifdef Plan9
  5683.     return p9tthflow(flow, status);
  5684. #else
  5685.  
  5686. #ifndef OXOS                /* NOT Olivetti X/OS... */
  5687. /*
  5688.   For SunOS 4.0 and later in the BSD environment ...
  5689.  
  5690.   The declarations are copied and interpreted from the System V header files,
  5691.   so we don't actually have to pull in all the System V junk when building
  5692.   C-Kermit for SunOS in the BSD environment, which would be dangerous because
  5693.   having those symbols defined would cause us to take the wrong paths through
  5694.   the code.  The code in this section is used in both the BSD and Sys V SunOS
  5695.   versions.
  5696. */
  5697. #ifdef SUNOS41
  5698. /*
  5699.   In SunOS 4.1 and later, we use the POSIX calls rather than ioctl calls
  5700.   because GNU CC uses different formats for the _IOxxx macros than regular CC;
  5701.   the POSIX forms work for both.  But the POSIX calls are not available in
  5702.   SunOS 4.0.
  5703. */
  5704. #define CRTSCTS 0x80000000        /* RTS/CTS flow control */
  5705. #define TCSANOW 0            /* Do it now */
  5706.  
  5707.     struct termios {
  5708.     unsigned long c_iflag;        /* Input modes */
  5709.     unsigned long c_oflag;        /* Output modes */
  5710.     unsigned long c_cflag;        /* Control modes */
  5711.     unsigned long c_lflag;        /* Line discipline modes */
  5712.     char c_line;
  5713.     CHAR c_cc[17];
  5714.     };
  5715.     struct termios temp;
  5716.  
  5717. _PROTOTYP( int tcgetattr, (int, struct termios *) );
  5718. _PROTOTYP( int tcsetattr, (int, int, struct termios *) );
  5719. /*
  5720.   When CRTSCTS is set, SunOS won't do output unless both CTS and CD are
  5721.   asserted.  So we don't set CRTSCTS unless CD is up.  This should be OK,
  5722.   since we don't need RTS/CTS during dialing, and after dialing is complete,
  5723.   we should have CD.  If not, we still communicate, but without RTS/CTS.
  5724. */
  5725.     int mflags;                /* Modem signal flags */
  5726.  
  5727. #ifdef NETCMD
  5728.     if (ttpipe) return(0);
  5729. #endif /* NETCMD */
  5730. #ifdef NETPTY
  5731.     if (ttpty) return(0);
  5732. #endif /* NETPTY */
  5733.  
  5734.     debug(F101,"tthflow SUNOS41 entry status","",status);
  5735.     if (!status) {            /* Turn hard flow off */
  5736.     if (tcgetattr(ttyfd, &temp) > -1 && /* Get device attributes */
  5737.         (temp.c_cflag & CRTSCTS)) { /* Check for RTS/CTS */
  5738.         temp.c_cflag &= ~CRTSCTS;    /* It's there, remove it */
  5739.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5740.     }
  5741.     } else {                /* Turn hard flow on */
  5742.     if (ioctl(ttyfd,TIOCMGET,&mflags) > -1 && /* Get modem signals */
  5743.         (mflags & TIOCM_CAR)) {        /* Check for CD */
  5744.         debug(F100,"tthflow SunOS has CD","",0);
  5745.         if (tcgetattr(ttyfd, &temp) > -1 && /* Get device attributes */
  5746.         !(temp.c_cflag & CRTSCTS)) { /* Check for RTS/CTS */
  5747.         temp.c_cflag |= CRTSCTS;    /* Not there, add it */
  5748.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5749.         }
  5750.     } else {
  5751.         x = -1;
  5752.         debug(F100,"tthflow SunOS no CD","",0);
  5753.     }
  5754.     }
  5755. #else
  5756. #ifdef QNX
  5757.     struct termios temp;
  5758. #ifdef NETCMD
  5759.     if (ttpipe) return(0);
  5760. #endif /* NETCMD */
  5761. #ifdef NETPTY
  5762.     if (ttpty) return(0);
  5763. #endif /* NETPTY */
  5764.     debug(F101,"tthflow QNX entry status","",status);
  5765.     if (tcgetattr(ttyfd, &temp) > -1) {    /* Get device attributes */
  5766.     if (!status) {            /* Turn hard flow off */
  5767.         if ((temp.c_cflag & (IHFLOW|OHFLOW)) == (IHFLOW|OHFLOW)) {
  5768.         temp.c_cflag &= ~(IHFLOW|OHFLOW); /* It's there, remove it */
  5769.         attrs->c_cflag &= ~(IHFLOW|OHFLOW);
  5770.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5771.         }
  5772.     } else {            /* Turn hard flow on */
  5773.         if ((temp.c_cflag & (IHFLOW|OHFLOW)) != (IHFLOW|OHFLOW)) {
  5774.         temp.c_cflag |= (IHFLOW|OHFLOW); /* Not there, add it */
  5775.         temp.c_iflag &= ~(IXON|IXOFF);   /* Bye to IXON/IXOFF */
  5776.         ttraw.c_lflag |= IEXTEN;         /* Must be on */
  5777.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5778.         attrs->c_cflag |= (IHFLOW|OHFLOW);
  5779.         attrs->c_iflag &= ~(IXON|IXOFF);
  5780.         }
  5781.     }
  5782.     } else {
  5783.     x = -1;
  5784.     debug(F100, "tthflow QNX getattr fails", "", 0);
  5785.     }
  5786. #else
  5787. #ifdef POSIX_CRTSCTS
  5788. /*
  5789.   POSIX_CRTSCTS is defined in ckcdeb.h or on CC command line.
  5790.   Note: Do not assume CRTSCTS is a one-bit field!
  5791. */
  5792.     struct termios temp;
  5793. #ifdef NETCMD
  5794.     if (ttpipe) return(0);
  5795. #endif /* NETCMD */
  5796. #ifdef NETPTY
  5797.     if (ttpty) return(0);
  5798. #endif /* NETPTY */
  5799.     debug(F101,"tthflow POSIX_CRTSCTS entry status","",status);
  5800.     errno = 0;
  5801.     x = tcgetattr(ttyfd, &temp);
  5802.     debug(F111,"tthflow POSIX_CRTSCTS tcgetattr",ckitoa(x),errno);
  5803.     errno = 0;
  5804.     if (x < 0) {
  5805.     x = -1;
  5806.     } else {
  5807.     if (!status) {            /* Turn hard flow off */
  5808.         if (
  5809. #ifdef COMMENT
  5810.         /* This can fail because of sign extension */
  5811.         /* e.g. in Linux where it's Bit 31 */
  5812.         (temp.c_cflag & CRTSCTS) == CRTSCTS
  5813. #else
  5814.         (temp.c_cflag & CRTSCTS) != 0
  5815. #endif /* COMMENT */
  5816.         ) {
  5817.         temp.c_cflag &= ~CRTSCTS; /* It's there, remove it */
  5818.         attrs->c_cflag &= ~CRTSCTS;
  5819.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5820.         debug(F111,"tthflow POSIX_CRTSCTS OFF tcsetattr",
  5821.               ckitoa(x),errno);
  5822.         }
  5823.     } else {            /* Turn hard flow on */
  5824.         if (
  5825. #ifdef COMMENT
  5826.         /* This can fail because of sign extension */
  5827.         (temp.c_cflag & CRTSCTS) != CRTSCTS
  5828. #else
  5829.         (temp.c_cflag & CRTSCTS) == 0
  5830. #endif /* COMMENT */
  5831.         ) {
  5832.         temp.c_cflag |= CRTSCTS; /* Not there, add it */
  5833.         temp.c_iflag &= ~(IXON|IXOFF|IXANY); /* Bye to IXON/IXOFF */
  5834.         x = tcsetattr(ttyfd,TCSANOW,&temp);
  5835.         debug(F111,"tthflow POSIX_CRTSCTS ON tcsetattr",
  5836.               ckitoa(x),errno);
  5837.         attrs->c_cflag |= CRTSCTS;
  5838.         attrs->c_iflag &= ~(IXON|IXOFF|IXANY);
  5839.         }
  5840.     }
  5841.     }
  5842. #else
  5843. #ifdef SUNOS4
  5844. /*
  5845.   SunOS 4.0 (and maybe earlier?).  This code is dangerous because it
  5846.   prevents compilation with GNU gcc, which uses different formats for the
  5847.   _IORxxx macros than regular cc.  SunOS 4.1 and later can use the POSIX
  5848.   routines above, which work for both cc and gcc.
  5849. */
  5850. #define TCGETS _IOR(T, 8, struct termios) /* Get modes into termios struct */
  5851. #define TCSETS _IOW(T, 9, struct termios) /* Set modes from termios struct */
  5852. #define CRTSCTS 0x80000000          /* RTS/CTS flow control */
  5853.  
  5854.     struct termios {
  5855.     unsigned long c_iflag;        /* Input modes */
  5856.     unsigned long c_oflag;        /* Output modes */
  5857.     unsigned long c_cflag;        /* Control modes */
  5858.     unsigned long c_lflag;        /* Line discipline modes */
  5859.     char c_line;
  5860.     CHAR c_cc[17];
  5861.     };
  5862.     struct termios temp;
  5863. #ifdef NETCMD
  5864.     if (ttpipe) return(0);
  5865. #endif /* NETCMD */
  5866. #ifdef NETPTY
  5867.     if (ttpty) return(0);
  5868. #endif /* NETPTY */
  5869.     debug(F101,"tthflow entry status","",status);
  5870.     if (ioctl(ttyfd,TCGETS,&temp) > -1) { /* Get terminal modes. */
  5871.     if (status) {            /* Turn hard flow on */
  5872.         temp.c_cflag |= CRTSCTS;    /* Add RTS/CTS to them. */
  5873.         x = ioctl(ttyfd,TCSETS,&temp); /* Set them again. */
  5874.         attrs->c_cflag |= CRTSCTS;    /* Add to global info. */
  5875.     } else {            /* Turn hard flow off */
  5876.         temp.c_cflag &= ~CRTSCTS;
  5877.         x = ioctl(ttyfd,TCSETS,&temp);
  5878.         attrs->c_cflag &= ~CRTSCTS;
  5879.     }
  5880.     }
  5881. #else                    /* Not SunOS 4.0 or later */
  5882. #ifdef AIXRS                /* IBM AIX RS/6000 */
  5883. #ifndef AIX41                /* But only pre-4.x == SVR4 */
  5884. #ifdef NETCMD
  5885.     if (ttpipe) return(0);
  5886. #endif /* NETCMD */
  5887. #ifdef NETPTY
  5888.     if (ttpty) return(0);
  5889. #endif /* NETPTY */
  5890.     if (status) {
  5891.     if ((x = ioctl(ttyfd, TXADDCD, "rts")) < 0 && errno != EBUSY)
  5892.       debug(F100,"hardflow TXADDCD (rts) error", "", 0);
  5893.     } else {
  5894.     if ((x = ioctl(ttyfd, TXDELCD, "rts")) < 0 && errno != EINVAL)
  5895.       debug(F100,"hardflow TXDELCD (rts) error", "", 0);
  5896.     }
  5897. #endif /* AIX41 */
  5898. #else                    /* Not AIX RS/6000 */
  5899.  
  5900. #ifdef ATTSV                /* System V... */
  5901.  
  5902. #ifdef CK_SCOV5                /* SCO Open Server 5.0 */
  5903. #define CK_SCOUNIX
  5904. #else
  5905. #ifdef M_UNIX                /* SCO UNIX 3.2v4.x or earlier */
  5906. #define CK_SCOUNIX
  5907. #endif /* M_UNIX */
  5908. #endif /* CK_SCOV5 */
  5909.  
  5910. #ifdef SCO_FORCE_RTSXOFF
  5911. #ifdef CK_SCOUNIX            /* But not SCO OpenServer 5.0.4 */
  5912. #ifdef SCO_OSR504            /* or later... */
  5913. #undef CK_SCOUNIX
  5914. #endif /* SCO_OSR504 */
  5915. #endif /* CK_SCOUNIX */
  5916. #endif /* SCO_FORCE_RTSXOFF */
  5917.  
  5918. #ifdef CK_SCOUNIX
  5919. #ifdef POSIX
  5920.     struct termios temp;
  5921. #ifdef NETCMD
  5922.     if (ttpipe) return(0);
  5923. #endif /* NETCMD */
  5924. #ifdef NETPTY
  5925.     if (ttpty) return(0);
  5926. #endif /* NETPTY */
  5927.     debug(F101,"tthflow SCOUNIX POSIX entry status","",status);
  5928.     errno = 0;
  5929.     x = tcgetattr(ttyfd, &temp);
  5930.     debug(F111,"tthflow SCO UNIX POSIX tcgetattr",ckitoa(x),errno);
  5931. #else /* POSIX */
  5932.     struct termio temp;
  5933. #ifdef NETCMD
  5934.     if (ttpipe) return(0);
  5935. #endif /* NETCMD */
  5936. #ifdef NETPTY
  5937.     if (ttpty) return(0);
  5938. #endif /* NETPTY */
  5939.     debug(F101,"tthflow SCOUNIX non-POSIX entry status","",status);
  5940.     x = ioctl(ttyfd, TCGETA, &temp);
  5941.     debug(F111,"tthflow SCO UNIX non-POSIX TCGETA",ckitoa(x),errno);
  5942. #endif /* POSIX */
  5943. /*
  5944.   This is not really POSIX, since POSIX does not deal with hardware flow
  5945.   control, but we are using the POSIX APIs.  In fact, RTSFLOW and CTSFLOW
  5946.   are defined in termio.h, but within #ifndef _POSIX_SOURCE..#endif.  So
  5947.   let's try forcing their definitions here.
  5948. */
  5949. #ifndef CTSFLOW
  5950. #define CTSFLOW 0020000
  5951.     debug(F101,"tthflow SCO defining CTSFLOW","",CTSFLOW);
  5952. #else
  5953.     debug(F101,"tthflow SCO CTSFLOW","",CTSFLOW);
  5954. #endif /* CTSFLOW */
  5955. #ifndef RTSFLOW
  5956. #define RTSFLOW 0040000
  5957.     debug(F101,"tthflow SCO defining RTSFLOW","",RTSFLOW);
  5958. #else
  5959.     debug(F101,"tthflow SCO RTSFLOW","",RTSFLOW);
  5960. #endif /* RTSFLOW */
  5961. #ifndef ORTSFL
  5962. #define ORTSFL 0100000
  5963.     debug(F101,"tthflow SCO defining ORTSFL","",ORTSFL);
  5964. #else
  5965.     debug(F101,"tthflow SCO ORTSFL","",ORTSFL);
  5966. #endif /* ORTSFL */
  5967.  
  5968.     if (x != -1) {
  5969.     if (status) {            /* Turn it ON */
  5970.         temp.c_cflag |= RTSFLOW|CTSFLOW;
  5971.         attrs->c_cflag |= RTSFLOW|CTSFLOW;
  5972. #ifdef ORTSFL
  5973.         temp.c_cflag &= ~ORTSFL;
  5974.         attrs->c_cflag &= ~ORTSFL;
  5975. #endif /* ORTSFL */
  5976.         temp.c_iflag &= ~(IXON|IXOFF|IXANY);
  5977.         attrs->c_iflag &= ~(IXON|IXOFF|IXANY);
  5978.     } else {            /* Turn it OFF */
  5979. #ifdef ORTSFL
  5980.         temp.c_cflag &= ~(RTSFLOW|CTSFLOW|ORTSFL);
  5981.         attrs->c_cflag &= ~(RTSFLOW|CTSFLOW|ORTSFL);
  5982. #else  /* ORTSFL */
  5983.         temp.c_cflag &= ~(RTSFLOW|CTSFLOW);
  5984.         attrs->c_cflag &= ~(RTSFLOW|CTSFLOW);
  5985. #endif /* ORTSFL */
  5986.     }
  5987. #ifdef POSIX
  5988.     x = tcsetattr(ttyfd, TCSADRAIN, &temp);
  5989. #else
  5990.     x = ioctl(ttyfd, TCSETA, &temp);
  5991. #endif /* POSIX */
  5992.     debug(F101,"tthflow SCO set modes","",x);
  5993.     }
  5994. #else /* Not SCO UNIX */
  5995. #ifdef NETCMD
  5996.     if (ttpipe) return(0);
  5997. #endif /* NETCMD */
  5998. #ifdef NETPTY
  5999.     if (ttpty) return(0);
  6000. #endif /* NETPTY */
  6001.     if (!status) {            /* Turn it OFF */
  6002. #ifdef RTSXOFF
  6003.     debug(F100,"tthflow ATTSV RTS/CTS OFF","",0);
  6004.     rctsx.x_hflag &= ~(RTSXOFF|CTSXON);
  6005. #ifdef TCSETX
  6006.     x = ioctl(ttyfd,TCSETX,&rctsx);
  6007.     debug(F101,"tthflow ATTSV TCSETX OFF","",x);
  6008. #else
  6009.     x = -1
  6010.     debug(F100,"tthflow TCSETX not defined","",0);
  6011. #endif /* TCSETX */
  6012. #else
  6013.     debug(F100,"tthflow ATTSV RTSXOFF not defined","",0);
  6014. #endif /* RTSXOFF */
  6015. #ifdef DTRXOFF
  6016.     debug(F100,"tthflow ATTSV DTR/CD OFF","",0);
  6017.     rctsx.x_hflag &= ~(DTRXOFF|CDXON);
  6018.     x = ioctl(ttyfd,TCSETX,&rctsx);
  6019.     debug(F101,"tthflow ATTSV DTRXOFF OFF","",x);
  6020. #else
  6021.     debug(F100,"tthflow ATTSV DTRXOFF not defined","",0);
  6022. #endif /* DTRXOFF */
  6023.     } else {                /* Turn it ON. */
  6024.     if (flow == FLO_RTSC) {    /* RTS/CTS Flow control... */
  6025.         debug(F100,"tthflow ATTSV RTS/CTS ON","",0);
  6026. #ifdef RTSXOFF
  6027.         /* This is the preferred way, according to SVID3 */
  6028. #ifdef TCGETX
  6029.         x = ioctl(ttyfd,TCGETX,&rctsx);
  6030.         debug(F101,"tthflow TCGETX","",x);
  6031.         if (x > -1) {
  6032.         rctsx.x_hflag |= RTSXOFF | CTSXON;
  6033.         x = ioctl(ttyfd,TCSETX,&rctsx);
  6034.         debug(F100,"tthflow ATTSV ioctl","",x);
  6035.         }
  6036. #else
  6037.         debug(F100,"tthflow TCGETX not defined","",0);
  6038.         x = -1
  6039. #endif /* TCGETX */
  6040. #else
  6041.         debug(F100,"tthflow RTSXOFF not defined","",0);
  6042.         x = -1;
  6043. #endif /* RTSXOFF */
  6044.     } else if (flow == FLO_DTRC) {    /* DTR/CD Flow control... */
  6045.         debug(F100,"tthflow ATTSV DTR/CD ON","",0);
  6046. #ifdef DTRXOFF
  6047.         /* This is straight out of SVID R4 */
  6048.         if (ioctl(ttyfd,TCGETX,&rctsx) > -1) {
  6049.         rctsx.x_hflag &= ~(DTRXOFF|CDXON);
  6050.         x = ioctl(ttyfd,TCSETX,&rctsx);
  6051.         }
  6052. #else
  6053.         debug(F100,"tthflow ATTSV DTRXOFF not defined","",0);
  6054.         x = -1;
  6055. #endif /* DTRXOFF */
  6056.     }
  6057.     }
  6058. #endif /* CK_SCOUNIX */
  6059.  
  6060. #else /* not System V... */
  6061.  
  6062. #ifdef CK_DTRCTS
  6063. #ifdef LDODTR
  6064. #ifdef LDOCTS
  6065. #ifdef NETCMD
  6066.     if (ttpipe) return(0);
  6067. #endif /* NETCMD */
  6068. #ifdef NETPTY
  6069.     if (ttpty) return(0);
  6070. #endif /* NETPTY */
  6071.     x = LDODTR | LDOCTS;        /* Found only on UTEK? */
  6072.     if (flow == FLO_DTRT && status) {    /* Use hardware flow control */
  6073.     if (lmodef) {
  6074.         x = ioctl(ttyfd,TIOCLBIS,&x);
  6075.         if (x < 0) {
  6076.             debug(F100,"hardflow TIOCLBIS error","",0);
  6077.         } else {
  6078.         lmodef++;
  6079.         debug(F100,"hardflow TIOCLBIS ok","",0);
  6080.         }
  6081.     }
  6082.     } else {
  6083.     if (lmodef) {
  6084.         x = ioctl(ttyfd,TIOCLBIC,&x);
  6085.         if (x < 0) {
  6086.             debug(F100,"hardflow TIOCLBIC error","",0);
  6087.         } else {
  6088.         lmodef++;
  6089.         debug(F100,"hardflow TIOCLBIC ok","",0);
  6090.         }
  6091.     }
  6092.     }
  6093. #endif /* LDODTR */
  6094. #endif /* LDOCTS */
  6095. #endif /* CK_DTRCTS */
  6096. #endif /* ATTSV */
  6097. #endif /* AIXRS */
  6098. #endif /* SUNOS4 */
  6099. #endif /* QNX */
  6100. #endif /* POSIX_CRTSCTS */
  6101. #endif /* SUNOS41 */
  6102.  
  6103. #else /* OXOS */
  6104.  
  6105.     struct termios temp;        /* Olivetti X/OS ... */
  6106.  
  6107. #ifdef NETCMD
  6108.     if (ttpipe) return(0);
  6109. #endif /* NETCMD */
  6110. #ifdef NETPTY
  6111.     if (ttpty) return(0);
  6112. #endif /* NETPTY */
  6113.     x = ioctl(ttyfd,TCGETS,&temp);
  6114.     if (x == 0) {
  6115.     temp.c_cflag &= ~(CRTSCTS|CDTRCTS|CBRKFLOW|CDTRDSR|CRTSDSR);
  6116.     if (status) {
  6117.         switch (flow) {
  6118.           case FLO_RTSC: temp.c_cflag |= CRTSCTS; /* RTS/CTS (hard) */
  6119.         break;
  6120.           case FLO_DTRT: temp.c_cflag |= CDTRCTS; /* DTR/CTS (hard) */
  6121.         break;
  6122.         }
  6123.     }
  6124.     x = ioctl(ttyfd,TCSETS,&temp);
  6125.     }
  6126. #endif /* OXOS */
  6127.     return(x);
  6128.  
  6129. #endif /* Plan9 */
  6130. }
  6131.  
  6132. /*  T T P K T  --  Condition the communication line for packets */
  6133. /*                 or for modem dialing */
  6134.  
  6135. /*
  6136.   If called with speed > -1, also set the speed.
  6137.   Returns 0 on success, -1 on failure.
  6138.  
  6139.   NOTE: the "xflow" parameter is supposed to be the currently selected
  6140.   type of flow control, but for historical reasons, this parameter is also
  6141.   used to indicate that we are dialing.  Therefore, when the true flow
  6142.   control setting is needed, we access the external variable "flow", rather
  6143.   than trusting our "xflow" argument.
  6144. */
  6145. int
  6146. #ifdef CK_ANSIC
  6147. ttpkt(long speed, int xflow, int parity)
  6148. #else
  6149. ttpkt(speed,xflow,parity) long speed; int xflow, parity;
  6150. #endif /* CK_ANSIC */
  6151. /* ttpkt */ {
  6152. #ifndef NOLOCAL
  6153.     int s2;
  6154.     int s = -1;
  6155. #endif /* NOLOCAL */
  6156.     int x;
  6157.     extern int flow;            /* REAL flow-control setting */
  6158.  
  6159.     if (ttyfd < 0) return(-1);          /* Not open. */
  6160.  
  6161.     debug(F101,"ttpkt parity","",parity);
  6162.     debug(F101,"ttpkt xflow","",xflow);
  6163.     debug(F101,"ttpkt speed","",(int) speed);
  6164.  
  6165.     ttprty = parity;                    /* Let other tt functions see these. */
  6166.     ttspeed = speed;            /* Make global copy for this module */
  6167.     ttpmsk = ttprty ? 0177 : 0377;    /* Parity stripping mask */
  6168. #ifdef PARSENSE
  6169.     needpchk = ttprty ? 0 : 1;        /* Parity check needed? */
  6170. #else
  6171.     needpchk = 0;
  6172. #endif /* PARSENSE */
  6173.  
  6174.     debug(F101,"ttpkt ttpmsk","",ttpmsk);
  6175.     debug(F101,"ttpkt netconn","",netconn);
  6176.  
  6177. #ifdef NETCONN                /* No mode-changing for telnet */
  6178.     if (netconn) {
  6179. #ifdef TCPSOCKET
  6180. #ifdef TCP_NODELAY
  6181.         if (ttnet == NET_TCPB) {    /* But turn off Nagle */
  6182.             extern int tcp_nodelay;
  6183.             nodelay_sav = tcp_nodelay;
  6184.             no_delay(ttyfd,1);
  6185.         }
  6186. #endif /* TCP_NODELAY */
  6187. #ifdef TN_COMPORT
  6188.         if (istncomport()) {
  6189.             int rc = -1;
  6190.             if (tvtflg == 0 && speed == ttspeed && flow == ttflow
  6191.                  /* && ttcarr == curcarr */ ) {
  6192.                 debug(F100,"ttpkt modes already set, skipping...","",0);
  6193.                 return(0);        /* Already been called. */
  6194.             }
  6195.             if (flow != ttflow) {
  6196.                 if ((rc = tnsetflow(flow)) < 0)
  6197.           return(rc);
  6198.                 ttflow = flow;
  6199.             }
  6200.             if (speed != ttspeed) {
  6201.                 if (speed <= 0) 
  6202.           speed = tnc_get_baud();
  6203.                 else if ((rc = tnc_set_baud(speed)) < 0)
  6204.           return(rc);
  6205.                 ttspeed = speed;
  6206.             }
  6207.             tnc_set_datasize(8);
  6208.         tnc_set_stopsize(stopbits);
  6209.  
  6210. #ifdef HWPARITY
  6211.             if (hwparity) {
  6212.                 switch (hwparity) {
  6213.           case 'e':            /* Even */
  6214.                     debug(F100,"ttres 8 bits + even parity","",0);
  6215.                     tnc_set_parity(3);
  6216.                     break;
  6217.           case 'o':            /* Odd */
  6218.                     debug(F100,"ttres 8 bits + odd parity","",0);
  6219.                     tnc_set_parity(2);
  6220.                     break;
  6221.           case 'm':            /* Mark */
  6222.                     debug(F100,"ttres 8 bits + invalid parity: mark","",0);
  6223.                     tnc_set_parity(4);
  6224.                     break;
  6225.           case 's':            /* Space */
  6226.                     debug(F100,"ttres 8 bits + invalid parity: space","",0);
  6227.                     tnc_set_parity(5);
  6228.                     break;
  6229.                 }
  6230.             } else 
  6231. #endif /* HWPARITY */
  6232.         {
  6233.                 tnc_set_parity(1);              /* None */
  6234.             }
  6235.             tvtflg = 0;
  6236.             return(0);
  6237.         }
  6238. #endif /* TN_COMPORT */
  6239. #endif /* TCPSOCKET */
  6240.         tvtflg = 0;
  6241.         return(0);
  6242.     }
  6243. #endif /* NETCONN */
  6244. #ifdef NETCMD
  6245.     if (ttpipe) return(0);
  6246. #endif /* NETCMD */
  6247. #ifdef NETPTY
  6248.     if (ttpty) return(0);
  6249. #endif /* NETPTY */
  6250.  
  6251. #ifndef Plan9
  6252.     if (ttfdflg && !isatty(ttyfd)) return(0);
  6253. #endif /* Plan9 */
  6254.  
  6255. #ifdef COHERENT
  6256. #define SVORPOSIX
  6257. #endif /* COHERENT */
  6258.  
  6259. #ifndef SVORPOSIX            /* Berkeley, V7, etc. */
  6260. #ifdef LPASS8
  6261. /*
  6262.  For some reason, with BSD terminal drivers, you can't set FLOW to XON/XOFF
  6263.  after having previously set it to NONE without closing and reopening the
  6264.  device.  Unless there's something I overlooked below...
  6265. */
  6266.     if (ttflow == FLO_NONE && flow == FLO_XONX && xlocal == 0) {
  6267.     debug(F101,"ttpkt executing horrible flow kludge","",0);
  6268.     ttclos(0);            /* Close it */
  6269.     x = 0;
  6270.     ttopen(ttnmsv,&x,ttmdm,0);    /* Open it again */
  6271.     }
  6272. #endif /* LPASS8 */
  6273. #endif /* SVORPOSIX */
  6274.  
  6275. #ifdef COHERENT                /* This must be vestigial since we */
  6276. #undef SVORPOSIX            /* reverse it a few lines below... */
  6277. #endif /* COHERENT */
  6278.  
  6279.     if (xflow != FLO_DIAL && xflow != FLO_DIAX)
  6280.       ttflow = xflow;            /* Now make this available too. */
  6281.  
  6282. #ifndef NOLOCAL
  6283.     if (xlocal) {
  6284.     s2 = (int) (speed / 10L);    /* Convert bps to cps */
  6285.     debug(F101,"ttpkt calling ttsspd","",s2);
  6286.     s = ttsspd(s2);            /* Check and set the speed */
  6287.     debug(F101,"ttpkt ttsspd result","",s);
  6288.      carrctl(&ttraw, xflow != FLO_DIAL /* Carrier control */
  6289.         && (ttcarr == CAR_ON || (ttcarr == CAR_AUT && ttmdm != 0)));
  6290.     tvtflg = 0;            /* So ttvt() will work next time */
  6291.     }
  6292. #endif /* NOLOCAL */
  6293.  
  6294. #ifdef COHERENT
  6295. #define SVORPOSIX
  6296. #endif /* COHERENT */
  6297.  
  6298. #ifndef SVORPOSIX            /* BSD section */
  6299.     if (flow == FLO_RTSC ||        /* Hardware flow control */
  6300.     flow == FLO_DTRC ||
  6301.     flow == FLO_DTRT) {
  6302.     tthflow(flow, 1, &ttraw);
  6303.     debug(F100,"ttpkt hard flow, TANDEM off, RAW on","",0);
  6304.     ttraw.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6305.     ttraw.sg_flags |= RAW;        /* Enter raw mode */
  6306.     } else if (flow == FLO_NONE) {    /* No flow control */
  6307.     debug(F100,"ttpkt no flow, TANDEM off, RAW on","",0);
  6308.     ttraw.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6309.     tthflow(flow, 0, &ttraw);    /* Turn off any hardware f/c too */
  6310.     ttraw.sg_flags |= RAW;        /* Enter raw mode */
  6311.     } else if (flow == FLO_KEEP) {    /* Keep device's original setting */
  6312.     debug(F100,"ttpkt keeping original TANDEM","",0);
  6313.     ttraw.sg_flags &= ~TANDEM;
  6314.     ttraw.sg_flags |= (ttold.sg_flags & TANDEM);
  6315.     /* NOTE: We should also handle hardware flow control here! */
  6316.     }
  6317.  
  6318. /* SET FLOW XON/XOFF is in effect, or SET FLOW KEEP resulted in Xon/Xoff */
  6319.  
  6320.     if ((flow == FLO_XONX) || (ttraw.sg_flags & TANDEM)) {
  6321.     debug(F100,"ttpkt turning on TANDEM","",0);
  6322.     ttraw.sg_flags |= TANDEM;    /* So ask for it. */
  6323.  
  6324. #ifdef LPASS8                /* Can pass 8-bit data through? */
  6325. /* If the LPASS8 local mode is available, then flow control can always  */
  6326. /* be used, even if parity is none and we are transferring 8-bit data.  */
  6327. /* But we only need to do all this if Xon/Xoff is requested. */
  6328. /* BUT... this tends not to work through IP or LAT connections, terminal */
  6329. /* servers, telnet, rlogin, etc, so it is currently disabled. */
  6330.     x = LPASS8;            /* If LPASS8 defined, then */
  6331.     debug(F100,"ttpkt executing LPASS8 code","",0);
  6332.     if (lmodef) {            /* TIOCLBIS must be too. */
  6333.         x = ioctl(ttyfd,TIOCLBIS,&x); /* Try to set LPASS8. */
  6334.         if (x < 0) {
  6335.         debug(F100,"ttpkt TIOCLBIS error","",0);
  6336.         } else {
  6337.         lmodef++;
  6338.         debug(F100,"ttpkt TIOCLBIS ok","",0);
  6339.         }
  6340.     }
  6341. /*
  6342.  But if we use LPASS8 mode, we must explicitly turn off
  6343.  terminal interrupts of all kinds.
  6344. */
  6345. #ifdef TIOCGETC                /* Not rawmode, */
  6346.     if (tcharf && (xlocal == 0)) {    /* must turn off */
  6347.         tchnoi.t_intrc = -1;    /* interrupt character */
  6348.         tchnoi.t_quitc = -1;    /* and quit character. */
  6349.         tchnoi.t_startc = 17;    /* Make sure xon */
  6350.         tchnoi.t_stopc = 19;    /* and xoff not ignored. */
  6351. #ifndef NOBRKC
  6352.         tchnoi.t_eofc = -1;        /* eof character. */
  6353.         tchnoi.t_brkc = -1;        /* brk character. */
  6354. #endif /* NOBRKC */
  6355.         if (ioctl(ttyfd,TIOCSETC,&tchnoi) < 0) {
  6356.         debug(F100,"ttpkt TIOCSETC failed","",0);
  6357.         } else {
  6358.         tcharf = 1;
  6359.         debug(F100,"ttpkt TIOCSETC ok","",0);
  6360.         }
  6361. #ifdef COMMENT
  6362. /* only for paranoid debugging */
  6363.         if (tcharf) {
  6364.         struct tchars foo;
  6365.         char tchbuf[100];
  6366.         ioctl(0,TIOCGETC,&foo);
  6367.         sprintf(tchbuf,
  6368.             "intr=%d,quit=%d, start=%d, stop=%d, eof=%d, brk=%d",
  6369.             foo.t_intrc, foo.t_quitc, foo.t_startc,
  6370.             foo.t_stopc, foo.t_eofc,  foo.t_brkc);
  6371.         debug(F110,"ttpkt chars",tchbuf,0);
  6372.         }
  6373. #endif /* COMMENT */
  6374.     }
  6375.     ttraw.sg_flags |= CBREAK;    /* Needed for unknown reason */
  6376. #endif /* TIOCGETC */
  6377.  
  6378. /* Prevent suspend during packet mode */
  6379. #ifdef TIOCGLTC                /* Not rawmode, */
  6380.     if (ltcharf && (xlocal == 0)) {    /* must turn off */
  6381.         ltchnoi.t_suspc = -1;    /* suspend character */
  6382.         ltchnoi.t_dsuspc = -1;    /* and delayed suspend character */
  6383.         if (ioctl(ttyfd,TIOCSLTC,&tchnoi) < 0) {
  6384.         debug(F100,"ttpkt TIOCSLTC failed","",0);
  6385.         } else {
  6386.         ltcharf = 1;
  6387.         debug(F100,"ttpkt TIOCSLTC ok","",0);
  6388.         }
  6389.     }
  6390. #endif /* TIOCGLTC */
  6391.  
  6392. #else /* LPASS8 not defined */
  6393.  
  6394. /* Previously, BSD-based implementations always */
  6395. /* used rawmode for packets.  Now, we use rawmode only if parity is NONE. */
  6396. /* This allows the flow control requested above to actually work, but only */
  6397. /* if the user asks for parity (which also means they get 8th-bit quoting). */
  6398.  
  6399.     if (parity) {            /* If parity, */
  6400.         ttraw.sg_flags &= ~RAW;    /* use cooked mode */
  6401. #ifdef COMMENT
  6402. /* WHY??? */
  6403.         if (xlocal)
  6404. #endif /* COMMENT */
  6405.           ttraw.sg_flags |= CBREAK;
  6406.         debug(F101,"ttpkt cooked, cbreak, parity","",parity);
  6407. #ifdef TIOCGETC                /* Not rawmode, */
  6408.         if (tcharf && (xlocal == 0)) { /* must turn off */
  6409.         tchnoi.t_intrc = -1;    /* interrupt character */
  6410.         tchnoi.t_quitc = -1;    /* and quit character. */
  6411.         tchnoi.t_startc = 17;    /* Make sure xon */
  6412.         tchnoi.t_stopc = 19;    /* and xoff not ignored. */
  6413. #ifndef NOBRKC
  6414.         tchnoi.t_eofc = -1;    /* eof character. */
  6415.         tchnoi.t_brkc = -1;    /* brk character. */
  6416. #endif /* NOBRKC */
  6417.         if (ioctl(ttyfd,TIOCSETC,&tchnoi) < 0) {
  6418.             debug(F100,"ttpkt TIOCSETC failed","",0);
  6419.         } else {
  6420.             tcharf = 1;
  6421.             debug(F100,"ttpkt TIOCSETC ok","",0);
  6422.         }
  6423.         }
  6424. #endif /* TIOCGETC */
  6425. #ifdef TIOCGLTC                /* Not rawmode, */
  6426. /* Prevent suspend during packet mode */
  6427.         if (ltcharf && (xlocal == 0)) { /* must turn off */
  6428.         ltchnoi.t_suspc = -1;    /* suspend character */
  6429.         ltchnoi.t_dsuspc = -1;    /* and delayed suspend character */
  6430.         if (ioctl(ttyfd,TIOCSLTC,&tchnoi) < 0) {
  6431.             debug(F100,"ttpkt TIOCSLTC failed","",0);
  6432.         } else {
  6433.             ltcharf = 1;
  6434.             debug(F100,"ttpkt TIOCSLTC ok","",0);
  6435.         }
  6436.         }
  6437. #endif /* TIOCGLTC */
  6438.     } else {            /* If no parity, */
  6439.         ttraw.sg_flags |= RAW;    /* must use 8-bit raw mode. */
  6440.         debug(F101,"ttpkt setting rawmode, parity","",parity);
  6441.     }
  6442. #endif /* LPASS8 */
  6443.     } /* End of Xon/Xoff section */
  6444.  
  6445.     /* Don't echo, don't map CR to CRLF on output, don't fool with case */
  6446. #ifdef LCASE
  6447.     ttraw.sg_flags &= ~(ECHO|CRMOD|LCASE);
  6448. #else
  6449.     ttraw.sg_flags &= ~(ECHO|CRMOD);
  6450. #endif /* LCASE */
  6451.  
  6452. #ifdef TOWER1
  6453.     ttraw.sg_flags &= ~ANYP;            /* Must set this on old Towers */
  6454. #endif /* TOWER1 */
  6455.  
  6456. #ifdef BELLV10
  6457.     if (ioctl(ttyfd,TIOCSETP,&ttraw) < 0) /* Set the new modes. */
  6458.       return(-1);
  6459. #else
  6460.     errno = 0;
  6461.     if (stty(ttyfd,&ttraw) < 0) {       /* Set the new modes. */
  6462.         debug(F101,"ttpkt stty failed","",errno);
  6463.         return(-1);
  6464.     }
  6465. #endif /* BELLV10 */
  6466.     debug(F100,"ttpkt stty ok","",0);
  6467.  
  6468. #ifdef sony_news
  6469.     x = xlocal ? km_ext : km_con;    /* Put line in ASCII mode. */
  6470.     if (x != -1) {            /* Make sure we know original modes. */
  6471.     x &= ~KM_TTYPE;
  6472.     x |= KM_ASCII;
  6473.     if (ioctl(ttyfd,TIOCKSET, &x) < 0) {
  6474.         perror("ttpkt can't set ASCII mode");
  6475.         debug(F101,"ttpkt error setting ASCII mode","",x);
  6476.         return(-1);
  6477.     }
  6478.     }
  6479.     debug(F100,"ttpkt set ASCII mode ok","",0);
  6480. #endif /* sony_news */
  6481.  
  6482.     if (xlocal == 0) {            /* Turn this off so we can read */
  6483.     signal(SIGINT,SIG_IGN);        /* Ctrl-C chars typed at console */
  6484.     sigint_ign = 1;
  6485.     }
  6486.     tvtflg = 0;                /* So ttvt() will work next time */
  6487.     debug(F100,"ttpkt success","",0);
  6488.     return(0);
  6489.  
  6490. #endif /* Not ATTSV or POSIX */
  6491.  
  6492. /* AT&T UNIX and POSIX */
  6493.  
  6494. #ifdef COHERENT
  6495. #define SVORPOSIX
  6496. #endif /* COHERENT */
  6497.  
  6498. #ifdef SVORPOSIX
  6499.     if (flow == FLO_XONX) {        /* Xon/Xoff */
  6500.     ttraw.c_iflag |= (IXON|IXOFF);
  6501.     tthflow(flow, 0, &ttraw);
  6502.     } else if (flow == FLO_NONE) {    /* None */
  6503.     /* NOTE: We should also turn off hardware flow control here! */
  6504.     ttraw.c_iflag &= ~(IXON|IXOFF);
  6505.     tthflow(flow, 0, &ttraw);
  6506.     } else if (flow == FLO_KEEP) {    /* Keep */
  6507.     ttraw.c_iflag &= ~(IXON|IXOFF);    /* Turn off Xon/Xoff flags */
  6508.     ttraw.c_iflag |= (ttold.c_iflag & (IXON|IXOFF)); /* OR in old ones */
  6509.     /* NOTE: We should also handle hardware flow control here! */
  6510. #ifdef POSIX_CRTSCTS
  6511. /* In Linux case, we do this, which is unlikely to be portable */
  6512.         ttraw.c_cflag &= ~CRTSCTS;    /* Turn off RTS/CTS flag */
  6513.         ttraw.c_cflag |= (ttold.c_cflag & CRTSCTS); /* OR in old one */
  6514. #endif /* POSIX_CRTSCTS */
  6515.     } else if (flow == FLO_RTSC ||    /* Hardware */
  6516.            flow == FLO_DTRC ||
  6517.            flow == FLO_DTRT) {
  6518.     ttraw.c_iflag &= ~(IXON|IXOFF);    /* (190) */
  6519.     tthflow(flow, 1, &ttraw);
  6520.     }
  6521.     ttraw.c_lflag &= ~(ICANON|ECHO);
  6522.     ttraw.c_lflag &= ~ISIG;        /* Do NOT check for interrupt chars */
  6523.  
  6524. #ifndef OXOS
  6525. #ifdef QNX
  6526.     if (flow != FLO_RTSC && flow != FLO_DTRC && flow != FLO_DTRT)
  6527. #endif /* QNX */
  6528. #ifndef COHERENT
  6529.       ttraw.c_lflag &= ~IEXTEN;        /* Turn off ^O/^V processing */
  6530. #endif /* COHERENT */
  6531. #else /* OXOS */
  6532.     ttraw.c_cc[VDISCARD] = ttraw.c_cc[VLNEXT] = CDISABLE;
  6533. #endif /* OXOS */
  6534.     ttraw.c_lflag |= NOFLSH;        /* Don't flush */
  6535.     ttraw.c_iflag |= IGNPAR;        /* Ignore parity errors */
  6536. #ifdef ATTSV
  6537. #ifdef BSD44
  6538.     ttraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|INPCK|ISTRIP|IXANY);
  6539. #else
  6540.     ttraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IUCLC|INPCK|ISTRIP|IXANY);
  6541. #endif /* BSD44 */
  6542. #else /* POSIX */
  6543.     ttraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|INPCK|ISTRIP);
  6544. #endif /* ATTSV */
  6545.     ttraw.c_oflag &= ~OPOST;
  6546.     ttraw.c_cflag &= ~(CSIZE);
  6547.     ttraw.c_cflag |= (CS8|CREAD|HUPCL);
  6548.  
  6549. #ifdef CSTOPB
  6550.     if (xlocal) {
  6551.     if (stopbits == 2) {
  6552.         ttraw.c_cflag |= CSTOPB;    /* 2 stop bits */
  6553.         debug(F100,"ttpkt 2 stopbits","",0);
  6554.     } else if (stopbits == 1) {
  6555.         ttraw.c_cflag &= ~(CSTOPB);    /* 1 stop bit */
  6556.         debug(F100,"ttpkt 1 stopbit","",0);
  6557.     }
  6558.     }
  6559. #endif /* CSTOPB */
  6560.  
  6561. #ifdef HWPARITY
  6562.     if (hwparity && xlocal) {        /* Hardware parity */
  6563.     ttraw.c_cflag |= PARENB;    /* Enable parity */
  6564. #ifdef COMMENT
  6565. /* Uncomment this only if needed -- I don't think it is */
  6566.     ttraw.c_cflag &= ~(CSIZE);    /* Clear out character-size mask */
  6567.     ttraw.c_cflag |= CS8;        /* And set it to 8 */
  6568. #endif /* COMMENT */
  6569. #ifdef IGNPAR
  6570.     ttraw.c_iflag |= IGNPAR;    /* Don't discard incoming bytes */
  6571.     debug(F100,"ttpkt IGNPAR","",0); /* that have parity errors */
  6572. #endif /* IGNPAR */
  6573.     switch (hwparity) {
  6574.       case 'e':            /* Even */
  6575.         ttraw.c_cflag &= ~(PARODD);
  6576.         debug(F100,"ttpkt 8 bits + even parity","",0);
  6577.         break;
  6578.       case 'o':            /* Odd */
  6579.         ttraw.c_cflag |= PARODD;
  6580.         debug(F100,"ttpkt 8 bits + odd parity","",0);
  6581.         break;
  6582.       case 'm':            /* Mark */
  6583.       case 's':            /* Space */
  6584.         /* PAREXT is mentioned in SVID but the details are not given. */
  6585.         /* PAREXT is not included in POSIX ISO/IEC 9945-1. */
  6586.         debug(F100,"ttpkt 8 bits + invalid parity","",0);
  6587.         break;
  6588.     }
  6589.     } else {                /* We handle parity ourselves */
  6590. #endif /* HWPARITY */
  6591.     ttraw.c_cflag &= ~(PARENB);    /* Don't enable parity */
  6592. #ifdef HWPARITY
  6593.     }
  6594. #endif /* HWPARITY */
  6595.  
  6596. #ifdef IX370
  6597.     ttraw.c_cc[4] = 48;  /* So Series/1 doesn't interrupt on every char */
  6598.     ttraw.c_cc[5] = 1;
  6599. #else
  6600. #ifndef VEOF                /* for DGUX this is VEOF, not VMIN */
  6601.     ttraw.c_cc[4] = 1;   /* [VMIN]  return max of this many characters or */
  6602. #else
  6603. #ifndef OXOS
  6604. #ifdef VMIN
  6605.     ttraw.c_cc[VMIN] = 1;
  6606. #endif /* VMIN */
  6607. #else /* OXOS */
  6608.     ttraw.c_min = 1;
  6609. #endif /* OXOS */
  6610. #endif /* VEOF */
  6611. #ifndef VEOL                /* for DGUX this is VEOL, not VTIME */
  6612.     ttraw.c_cc[5] = 0;     /* [VTIME] when this many secs/10 expire w/no input */
  6613. #else
  6614. #ifndef OXOS
  6615. #ifdef VTIME
  6616.     ttraw.c_cc[VTIME] = 0;
  6617. #endif /* VTIME */
  6618. #else /* OXOS */
  6619.     ttraw.c_time = 0;
  6620. #endif /* OXOS */
  6621. #endif /* VEOL */
  6622. #endif /* IX370 */
  6623.  
  6624. #ifdef VINTR                /* Turn off interrupt character */
  6625.     if (xlocal == 0)            /* so ^C^C can break us out of */
  6626.       ttraw.c_cc[VINTR] = 0;        /* packet mode. */
  6627. #endif /* VINTR */
  6628.  
  6629. #ifdef Plan9
  6630.     if (p9ttyparity('n') < 0)
  6631.     return -1;
  6632. #else
  6633. #ifdef BSD44ORPOSIX
  6634.     errno = 0;
  6635. #ifdef BEOSORBEBOX
  6636.     ttraw.c_cc[VMIN] = 0;        /* DR7 can only poll. */
  6637. #endif /* BEOSORBEBOX */
  6638.     debug(F100,"ttpkt calling tcsetattr(TCSETAW)","",0);
  6639.     x = tcsetattr(ttyfd,TCSADRAIN,&ttraw);
  6640.     debug(F101,"ttpkt BSD44ORPOSIX tcsetattr","",x);
  6641.     if (x < 0) {
  6642.     debug(F101,"ttpkt BSD44ORPOSIX tcsetattr errno","",errno);
  6643.         return(-1);
  6644.     }
  6645. #else /* BSD44ORPOSIX */
  6646.     x = ioctl(ttyfd,TCSETAW,&ttraw);
  6647.     debug(F101,"ttpkt ATTSV ioctl TCSETAW","",x);
  6648.     if (x < 0) {  /* set new modes . */
  6649.     debug(F101,"ttpkt ATTSV ioctl TCSETAW errno","",errno);
  6650.         return(-1);
  6651.     }
  6652. #endif /* BSD44ORPOSIX */
  6653. #endif /* Plan9 */
  6654.     tvtflg = 0;
  6655.     debug(F100,"ttpkt ok","",0);
  6656.     return(0);
  6657. #endif /* ATTSV */
  6658.  
  6659. #ifdef COHERENT
  6660. #undef SVORPOSIX
  6661. #endif /* COHERENT */
  6662.  
  6663. }
  6664.  
  6665. /*  T T S E T F L O W  --  Set flow control immediately.  */
  6666.  
  6667. #ifdef COHERENT
  6668. #define SVORPOSIX
  6669. #endif /* COHERENT */
  6670.  
  6671. int
  6672. ttsetflow(flow) int flow; {
  6673.     if (ttyfd < 0)            /* A channel must be open */
  6674.       return(-1);
  6675.  
  6676.     debug(F101,"ttsetflow flow","",flow);
  6677.  
  6678. #ifdef TN_COMPORT
  6679.     if (netconn && istncomport()) {
  6680.     debug(F101,"ttsetflow net modem","",ttmdm);
  6681.     return(tnsetflow(flow));
  6682.     }
  6683. #endif /* TN_COMPORT */
  6684. #ifdef NETCMD
  6685.     if (ttpipe) return(0);
  6686. #endif /* NETCMD */
  6687. #ifdef NETPTY
  6688.     if (ttpty) return(0);
  6689. #endif /* NETPTY */
  6690.  
  6691. #ifdef COMMENT
  6692.     /* This seems to hurt... */
  6693.     if (flow == FLO_KEEP)
  6694.       return(0);
  6695. #endif /* COMMENT */
  6696.  
  6697.     if (flow == FLO_RTSC ||        /* Hardware flow control... */
  6698.     flow == FLO_DTRC ||
  6699.     flow == FLO_DTRT) {
  6700.     tthflow(flow, 1, &ttraw);
  6701. #ifndef SVORPOSIX
  6702.     ttraw.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6703. #else
  6704.     ttraw.c_iflag &= ~(IXON|IXOFF);
  6705. #endif /* SVORPOSIX */
  6706.  
  6707.     } else if (flow == FLO_XONX) {    /* Xon/Xoff... */
  6708.  
  6709. #ifndef SVORPOSIX
  6710.     ttraw.sg_flags |= TANDEM;
  6711. #else
  6712.     ttraw.c_iflag |= (IXON|IXOFF);
  6713. #endif /* SVORPOSIX */
  6714.     tthflow(FLO_RTSC, 0, &ttraw);    /* Turn off hardware flow control */
  6715.  
  6716.     } else if (flow == FLO_NONE) {    /* No flow control */
  6717.  
  6718. #ifndef SVORPOSIX
  6719.     ttraw.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6720. #else
  6721.     ttraw.c_iflag &= ~(IXON|IXOFF);
  6722. #endif /* SVORPOSIX */
  6723.     tthflow(FLO_RTSC, 0, &ttraw);    /* Turn off any hardware f/c too */
  6724.     }
  6725.  
  6726. /* Set the new modes... */
  6727.  
  6728. #ifndef SVORPOSIX            /* BSD and friends */
  6729. #ifdef BELLV10
  6730.     if (ioctl(ttyfd,TIOCSETP,&ttraw) < 0)
  6731.       return(-1);
  6732. #else
  6733. #ifndef MINIX2
  6734.     if (stty(ttyfd,&ttraw) < 0)
  6735.       return(-1);
  6736. #endif /* MINIX2 */
  6737. #endif /* BELLV10 */
  6738. #else
  6739. #ifdef BSD44ORPOSIX            /* POSIX */
  6740.     if (tcsetattr(ttyfd,TCSADRAIN,&ttraw) < 0)
  6741.       return(-1);
  6742. #else                    /* System V */
  6743.     if (ioctl(ttyfd,TCSETAW,&ttraw) < 0)
  6744.       return(-1);
  6745. #endif /* BSD44ORPOSIX */
  6746. #endif /* SVORPOSIX */
  6747.     return(0);
  6748. }
  6749. #ifdef COHERENT
  6750. #undef SVORPOSIX
  6751. #endif /* COHERENT */
  6752.  
  6753. /*  T T V T -- Condition communication device for use as virtual terminal. */
  6754.  
  6755. int
  6756. #ifdef CK_ANSIC
  6757. ttvt(long speed, int flow)
  6758. #else
  6759. ttvt(speed,flow) long speed; int flow;
  6760. #endif /* CK_ANSIC */
  6761. /* ttvt */ {
  6762.     int s, s2, x;
  6763.  
  6764.     debug(F101,"ttvt ttyfd","",ttyfd);
  6765.     debug(F101,"ttvt tvtflg","",tvtflg);
  6766.     debug(F111,"ttvt speed",ckitoa(ttspeed),speed);
  6767.     debug(F111,"ttvt flow",ckitoa(ttflow),flow);
  6768.     debug(F111,"ttvt curcarr",ckitoa(ttcarr),curcarr);
  6769.  
  6770. /* Note: NetBSD and maybe other BSD44s have cfmakeraw() */
  6771. /* Maybe it would be simpler to use it... */
  6772.  
  6773.     ttpmsk = 0xff;
  6774. #ifdef NOLOCAL
  6775.     return(conbin((char)escchr));
  6776. #else
  6777.     if (ttyfd < 0) {            /* Not open. */
  6778.     if (ttchk() < 0)
  6779.       return(-1);
  6780.     else                /* But maybe something buffered. */
  6781.       return(0);
  6782.     }
  6783. #ifdef NETCMD
  6784.     if (ttpipe) return(0);
  6785. #endif /* NETCMD */
  6786. #ifdef NETPTY
  6787.     if (ttpty) return(0);
  6788. #endif /* NETPTY */
  6789. #ifdef NETCONN
  6790.     if (netconn) {
  6791. #ifdef TCPSOCKET
  6792. #ifdef TCP_NODELAY
  6793.         {
  6794.         extern int tcp_nodelay;
  6795.         if (ttnet == NET_TCPB) {
  6796.         if (nodelay_sav > -1) {
  6797.             no_delay(ttyfd,nodelay_sav);
  6798.             nodelay_sav = -1;
  6799.         }
  6800.         }
  6801.         }
  6802. #endif /* TCP_NODELAY */
  6803. #ifdef TN_COMPORT
  6804.         if (istncomport()) {
  6805.             int rc = -1;
  6806.             if (tvtflg != 0 && speed == ttspeed && flow == ttflow
  6807.                  /* && ttcarr == curcarr */ ) {
  6808.                 debug(F100,"ttvt modes already set, skipping...","",0);
  6809.                 return(0);            /* Already been called. */
  6810.             }
  6811.             if (flow != ttflow) {
  6812.                 if ((rc = tnsetflow(flow)) < 0)
  6813.           return(rc);
  6814.                 ttflow = flow;
  6815.             }
  6816.             if (speed != ttspeed) {
  6817.                 if (speed <= 0) 
  6818.           speed = tnc_get_baud();
  6819.                 else if ((rc = tnc_set_baud(speed)) < 0)
  6820.           return(rc);
  6821.                 ttspeed = speed;
  6822.             }
  6823.             tnc_set_datasize(8);
  6824.         tnc_set_stopsize(stopbits);
  6825.  
  6826. #ifdef HWPARITY
  6827.             if (hwparity) {
  6828.                 switch (hwparity) {
  6829.           case 'e':        /* Even */
  6830.                     debug(F100,"ttres 8 bits + even parity","",0);
  6831.                     tnc_set_parity(3);
  6832.                     break;
  6833.           case 'o':        /* Odd */
  6834.                     debug(F100,"ttres 8 bits + odd parity","",0);
  6835.                     tnc_set_parity(2);
  6836.                     break;
  6837.           case 'm':        /* Mark */
  6838.                     debug(F100,"ttres 8 bits + invalid parity: mark","",0);
  6839.                     tnc_set_parity(4);
  6840.                     break;
  6841.           case 's':        /* Space */
  6842.                     debug(F100,"ttres 8 bits + invalid parity: space","",0);
  6843.                     tnc_set_parity(5);
  6844.                     break;
  6845.                 }
  6846.             } else
  6847. #endif /* HWPARITY */
  6848.             {
  6849.                 tnc_set_parity(1);    /* None */
  6850.             }
  6851.             tvtflg = 1;
  6852.             return(0);
  6853.         }
  6854. #endif /* TN_COMPORT */
  6855. #endif /* TCPSOCKET */
  6856.     tvtflg = 1;            /* Network connections... */
  6857.     debug(F100,"ttvt network connection, skipping...","",0);
  6858.     return(0);            /* ... require no special setup */
  6859.     }
  6860. #endif /* NETCONN */
  6861.  
  6862.     if (tvtflg != 0 && speed == ttspeed && flow == ttflow
  6863.     /* && ttcarr == curcarr */ )
  6864.       {
  6865.       debug(F100,"ttvt modes already set, skipping...","",0);
  6866.       return(0);            /* Already been called. */
  6867.       }
  6868.  
  6869.     if (ttfdflg
  6870. #ifndef Plan9
  6871.     && !isatty(ttyfd)
  6872. #endif /* Plan9 */
  6873.     ) {
  6874.     debug(F100,"ttvt using external fd, skipping...","",0);
  6875.     return(0);
  6876.     }
  6877.  
  6878.     debug(F100,"ttvt setting modes...","",0);
  6879.  
  6880.     if (xlocal) {            /* For external lines... */
  6881.     s2 = (int) (speed / 10L);
  6882.     s = ttsspd(s2);            /* Check/set the speed */
  6883.     carrctl(&tttvt, flow != FLO_DIAL /* Do carrier control */
  6884.         && (ttcarr == CAR_ON || (ttcarr == CAR_AUT && ttmdm != 0)));
  6885.     } else
  6886.       s = s2 = -1;
  6887.  
  6888. #ifdef COHERENT
  6889. #define SVORPOSIX
  6890. #endif /* COHERENT */
  6891.  
  6892. #ifndef SVORPOSIX
  6893.     /* Berkeley, V7, etc */
  6894.     if (flow == FLO_RTSC ||        /* Hardware flow control */
  6895.     flow == FLO_DTRC ||
  6896.     flow == FLO_DTRT) {
  6897.     tthflow(flow, 1, &tttvt);
  6898.     debug(F100,"ttvt hard flow, TANDEM off","",0);
  6899.     tttvt.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6900.     } else if (flow == FLO_XONX) {    /* Xon/Xoff flow control */
  6901.     debug(F100,"ttvt TANDEM on","",0);
  6902.     tttvt.sg_flags |= TANDEM;    /* Ask for it. */
  6903.     tthflow(flow, 0, &tttvt);    /* Turn off hardware f/c */
  6904.     } else if (flow == FLO_NONE) {
  6905.     debug(F100,"ttvt no flow, TANDEM off, RAW on","",0);
  6906.     tttvt.sg_flags &= ~TANDEM;    /* Turn off software flow control */
  6907.     tthflow(flow, 0, &tttvt);    /* Turn off any hardware f/c too */
  6908.     tttvt.sg_flags |= RAW;        /* Enter raw mode */
  6909.     } else if (flow == FLO_KEEP) {    /* Keep device's original setting */
  6910.     debug(F100,"ttvt keeping original TANDEM","",0);
  6911.     tttvt.sg_flags &= ~TANDEM;
  6912.     tttvt.sg_flags |= (ttold.sg_flags & TANDEM);
  6913.     /* NOTE: We should also handle hardware flow control here! */
  6914.     }
  6915.     tttvt.sg_flags |= RAW;              /* Raw mode in all cases */
  6916. #ifdef TOWER1
  6917.     tttvt.sg_flags &= ~(ECHO|ANYP);     /* No echo or parity */
  6918. #else
  6919.     tttvt.sg_flags &= ~ECHO;            /* No echo */
  6920. #endif /* TOWER1 */
  6921.  
  6922. #ifdef BELLV10
  6923.     if (ioctl(ttyfd,TIOCSETP,&tttvt) < 0) /* Set the new modes */
  6924.       return(-1);
  6925. #else
  6926.     if (stty(ttyfd,&tttvt) < 0)        /* Set the new modes */
  6927.       return(-1);
  6928. #endif /* BELLV10 */
  6929.  
  6930. #else /* It is ATTSV or POSIX */
  6931.  
  6932.     if (flow == FLO_XONX) {        /* Software flow control */
  6933.     tttvt.c_iflag |= (IXON|IXOFF);    /* On if requested. */
  6934.     tthflow(flow, 0, &tttvt);    /* Turn off hardware f/c */
  6935.     debug(F100,"ttvt SVORPOSIX flow XON/XOFF","",0);
  6936.     } else if (flow == FLO_NONE) {    /* NONE */
  6937.     tttvt.c_iflag &= ~(IXON|IXOFF);    /* Turn off Xon/Xoff */
  6938.     tthflow(flow, 0, &tttvt);    /* Turn off hardware f/c */
  6939.     debug(F100,"ttvt SVORPOSIX flow NONE","",0);
  6940.     } else if (flow == FLO_KEEP) {
  6941.     tttvt.c_iflag &= ~(IXON|IXOFF);    /* Turn off Xon/Xoff flags */
  6942.     tttvt.c_iflag |= (ttold.c_iflag & (IXON|IXOFF)); /* OR in old ones */
  6943. #ifdef POSIX_CRTSCTS
  6944.         tttvt.c_cflag &= ~CRTSCTS;    /* Turn off RTS/CTS flag */
  6945.         tttvt.c_cflag |= (ttold.c_cflag & CRTSCTS); /* OR in old one */
  6946. #endif /* POSIX_CRTSCTS */
  6947.     debug(F100,"ttvt SVORPOSIX flow KEEP","",0);
  6948.     } else if (flow == FLO_RTSC ||    /* Hardware flow control */
  6949.            flow == FLO_DTRC ||
  6950.            flow == FLO_DTRT) {
  6951.     tttvt.c_iflag &= ~(IXON|IXOFF);    /* (196) */
  6952.     tthflow(flow, 1, &tttvt);
  6953.     debug(F100,"ttvt SVORPOSIX flow HARD","",0);
  6954.     }
  6955. #ifndef OXOS
  6956. #ifdef COHERENT
  6957.     tttvt.c_lflag &= ~(ISIG|ICANON|ECHO);
  6958. #else
  6959.     tttvt.c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
  6960. #endif /* COHERENT */
  6961. #ifdef QNX
  6962.     /* Needed for hwfc */
  6963.     if (flow == FLO_RTSC || flow == FLO_DTRC || flow == FLO_DTRT)
  6964.       tttvt.c_lflag |= IEXTEN;
  6965. #endif /* QNX */
  6966. #else /* OXOS */
  6967.     tttvt.c_lflag &= ~(ISIG|ICANON|ECHO);
  6968.     tttvt.c_cc[VDISCARD] = tttvt.c_cc[VLNEXT] = CDISABLE;
  6969. #endif /* OXOS */
  6970.  
  6971.     tttvt.c_iflag |= (IGNBRK|IGNPAR);
  6972.  
  6973. /* Stop bits */
  6974.  
  6975. #ifdef CSTOPB
  6976.     if (xlocal) {
  6977.     if (stopbits == 2) {
  6978.         tttvt.c_cflag |= CSTOPB;    /* 2 stop bits */
  6979.         debug(F100,"ttvt 2 stopbits","",0);
  6980.     } else if (stopbits == 1) {
  6981.         tttvt.c_cflag &= ~(CSTOPB);    /* 1 stop bit */
  6982.         debug(F100,"ttvt 1 stopbit","",0);
  6983.     }
  6984.     }
  6985. #endif /* CSTOPB */
  6986.  
  6987. /* Parity */
  6988.  
  6989. #ifdef HWPARITY
  6990.     if (hwparity && xlocal) {        /* Hardware parity */
  6991. #ifdef COMMENT
  6992. /* Uncomment this only if needed -- I don't think it is */
  6993.     ttraw.c_cflag &= ~(CSIZE);    /* Clear out character-size mask */
  6994.     ttraw.c_cflag |= CS8;        /* And set it to 8 */
  6995. #endif /* COMMENT */
  6996. #ifdef IGNPAR
  6997.     debug(F101,"ttvt hwparity IGNPAR","",IGNPAR);
  6998.     tttvt.c_iflag |= IGNPAR;    /* Don't discard incoming bytes */
  6999. #endif /* IGNPAR */
  7000.     tttvt.c_cflag |= PARENB;    /* Enable parity */
  7001.  
  7002.     switch (hwparity) {
  7003.       case 'e':            /* Even */
  7004.         tttvt.c_cflag &= ~(PARODD);
  7005.         debug(F100,"ttvt 8 bits + even parity","",0);
  7006.         break;
  7007.       case 'o':            /* Odd */
  7008.         tttvt.c_cflag |= PARODD;
  7009.         debug(F100,"ttvt 8 bits + odd parity","",0);
  7010.         break;
  7011.       case 'm':            /* Mark */
  7012.       case 's':            /* Space */
  7013.         /* PAREXT is mentioned in SVID but the details are not given. */
  7014.         /* PAREXT is not included in POSIX ISO/IEC 9945-1. */
  7015.         debug(F100,"ttvt 8 bits + invalid parity","",0);
  7016.         break;
  7017.     }
  7018.     } else {                /* We handle parity ourselves */
  7019. #endif /* HWPARITY */
  7020.     tttvt.c_cflag &= ~(PARENB);    /* Don't enable parity */
  7021. #ifdef HWPARITY
  7022.     }
  7023. #endif /* HWPARITY */
  7024.  
  7025. #ifdef ATTSV
  7026. #ifdef BSD44
  7027.     /* Things not to do... */
  7028.     tttvt.c_iflag &= ~(INLCR|IGNCR|ICRNL|INPCK|ISTRIP|IXANY);
  7029. #else
  7030.     tttvt.c_iflag &= ~(INLCR|IGNCR|ICRNL|IUCLC|INPCK|ISTRIP|IXANY);
  7031. #endif /* BSD44 */
  7032. #else /* POSIX */
  7033.     tttvt.c_iflag &= ~(INLCR|IGNCR|ICRNL|INPCK|ISTRIP);
  7034. #endif /* ATTSV */
  7035.     tttvt.c_cflag &= ~(CSIZE);        /* Zero out the char size field */
  7036.     tttvt.c_cflag |= (CS8|CREAD|HUPCL);    /* Char size 8, enable receiver, hup */
  7037.     tttvt.c_oflag &= ~OPOST;        /* Don't postprocess output */
  7038. #ifndef VEOF /* DGUX termio has VEOF at entry 4, see comment above */
  7039.     tttvt.c_cc[4] = 1;
  7040. #else
  7041. #ifndef OXOS
  7042. #ifdef VMIN
  7043.     tttvt.c_cc[VMIN] = 1;
  7044. #endif /* VMIN */
  7045. #else /* OXOS */
  7046.     tttvt.c_min = 1;
  7047. #endif /* OXOS */
  7048. #endif /* VEOF */
  7049. #ifndef VEOL    /* DGUX termio has VEOL at entry 5, see comment above */
  7050.     tttvt.c_cc[5] = 0;
  7051. #else
  7052. #ifndef OXOS
  7053. #ifdef VTIME
  7054.     tttvt.c_cc[VTIME] = 0;
  7055. #endif /* VTIME */
  7056. #else /* OXOS */
  7057.     tttvt.c_time = 0;
  7058. #endif /* OXOS */
  7059. #endif /* VEOL */
  7060.  
  7061. #ifdef Plan9
  7062.     if (p9ttyparity('n') < 0)
  7063.       return -1;
  7064. #else
  7065. #ifdef BSD44ORPOSIX
  7066.     errno = 0;
  7067. #ifdef BEOSORBEBOX
  7068.     tttvt.c_cc[VMIN] = 0;        /* DR7 can only poll. */
  7069. #endif /* BEOSORBEBOX */
  7070.  
  7071.     x = tcsetattr(ttyfd,TCSADRAIN,&tttvt);
  7072.     debug(F101,"ttvt BSD44ORPOSIX tcsetattr","",x);
  7073.     if (x < 0) {
  7074.     debug(F101,"ttvt BSD44ORPOSIX tcsetattr errno","",errno);
  7075.     return(-1);
  7076.     }
  7077. #else /* ATTSV */
  7078.     x = ioctl(ttyfd,TCSETAW,&tttvt);
  7079.     debug(F101,"ttvt ATTSV ioctl TCSETAW","",x);
  7080.     if (x < 0) {            /* set new modes . */
  7081.     debug(F101,"ttvt ATTSV ioctl TCSETAW errno","",errno);
  7082.     return(-1);    
  7083.     }
  7084. #endif /* BSD44ORPOSIX */
  7085. #endif /* Plan9 */
  7086. #endif /* ATTSV */
  7087.  
  7088.     ttspeed = speed;            /* Done, remember how we were */
  7089.     ttflow = flow;            /* called, so we can decide how to */
  7090.     tvtflg = 1;                /* respond next time. */
  7091.     debug(F100,"ttvt ok","",0);
  7092.     return(0);
  7093.  
  7094. #ifdef COHERENT
  7095. #undef SVORPOSIX
  7096. #endif /* COHERENT */
  7097.  
  7098. #endif /* NOLOCAL */
  7099. }
  7100.  
  7101. #ifndef NOLOCAL
  7102.  
  7103. /* Serial speed department . . . */
  7104.  
  7105. /*
  7106.   SCO OSR5.0.x might or might not support high speeds.  Sometimes they are not
  7107.   defined in the header files but they are supported (e.g. when building with
  7108.   UDK compiler rather than /bin/cc), sometimes vice versa.  Even though 5.0.4
  7109.   was the first release that came with high serial speeds standard, releases
  7110.   back to 5.0.0 could use them if certain patches (or "supplements") were
  7111.   applied to the SIO driver.  Plus a lot of SCO installations run third-party
  7112.   drivers.
  7113. */
  7114. #ifdef CK_SCOV5
  7115. #ifndef B38400
  7116. #define    B38400    0000017
  7117. #endif /* B38400 */
  7118. #ifndef B57600
  7119. #define    B57600    0000021
  7120. #endif /* B57600 */
  7121. #ifndef B76800
  7122. #define    B76800    0000022
  7123. #endif /* B76800 */
  7124. #ifndef B115200
  7125. #define    B115200    0000023
  7126. #endif /* B115200 */
  7127. #ifndef B230400
  7128. #define    B230400    0000024
  7129. #endif /* B230400 */
  7130. #ifndef B460800
  7131. #define    B460800    0000025
  7132. #endif /* B460800 */
  7133. #ifndef B921600
  7134. #define    B921600    0000026
  7135. #endif /* B921600 */
  7136. #endif /* CK_SCOV5 */
  7137. /*
  7138.   Plan 9's native speed setting interface lets you set anything you like,
  7139.   but will fail if the hardware doesn't like it, so we allow all the common
  7140.   speeds.
  7141. */
  7142. #ifdef Plan9
  7143. #ifndef B50
  7144. #define B50 50
  7145. #endif /* B50 */
  7146. #ifndef B75
  7147. #define B75 75
  7148. #endif /* B75 */
  7149. #ifndef B110
  7150. #define B110 110
  7151. #endif /* B110 */
  7152. #ifndef B134
  7153. #define B134 134
  7154. #endif /* B134 */
  7155. #ifndef B200
  7156. #define B200 200
  7157. #endif /* B200 */
  7158. #ifndef B300
  7159. #define B300 300
  7160. #endif /* B300 */
  7161. #ifndef B1200
  7162. #define B1200 1200
  7163. #endif /* B1200 */
  7164. #ifndef B1800
  7165. #define B1800 1800
  7166. #endif /* B1800 */
  7167. #ifndef B2400
  7168. #define B2400 2400
  7169. #endif /* B2400 */
  7170. #ifndef B4800
  7171. #define B4800 4800
  7172. #endif /* B4800 */
  7173. #ifndef B9600
  7174. #define B9600 9600
  7175. #endif /* B9600 */
  7176. #ifndef B14400
  7177. #define B14400 14400
  7178. #endif /* B14400 */
  7179. #ifndef B19200
  7180. #define B19200 19200
  7181. #endif /* B19200 */
  7182. #ifndef B28800
  7183. #define B28800 28800
  7184. #endif /* B28800 */
  7185. #ifndef B38400
  7186. #define B38400 38400
  7187. #endif /* B38400 */
  7188. #ifndef B57600
  7189. #define B57600 57600
  7190. #endif /* B57600 */
  7191. #ifndef B76800
  7192. #define B76800 76800
  7193. #endif /* B76800 */
  7194. #ifndef B115200
  7195. #define B115200 115200
  7196. #endif /* B115200 */
  7197. #ifndef B230400
  7198. #define B230400 230400
  7199. #endif /* B230400 */
  7200. #ifndef B460800
  7201. #define B460800 460800
  7202. #endif /* B460800 */
  7203. #ifndef B921600
  7204. #define B921600 921600
  7205. #endif /* B921600 */
  7206. #endif /* Plan9 */
  7207.  
  7208. /*  T T S S P D  --  Checks and sets transmission rate.  */
  7209.  
  7210. /*  Call with speed in characters (not bits!) per second. */
  7211. /*  Returns -1 on failure, 0 if it did nothing, 1 if it changed the speed. */
  7212.  
  7213. #ifdef USETCSETSPEED
  7214. /*
  7215.   The tcsetspeed() / tcgetspeed() interface lets you pass any number at all
  7216.   to be used as a speed to be set, rather than forcing a choice from a
  7217.   predefined list.  It seems to be peculiar to UnixWare 7.
  7218.  
  7219.   These are the function codes to be passed to tc[gs]etspeed(),
  7220.   but for some reason they don't seem to be picked up from termios.h.
  7221. */
  7222. #ifndef TCS_ALL
  7223. #define TCS_ALL 0
  7224. #endif /* TCS_ALL */
  7225. #ifndef TCS_IN
  7226. #define TCS_IN 1
  7227. #endif /* TCS_IN */
  7228. #ifndef TCS_OUT
  7229. #define TCS_OUT 2
  7230. #endif /* TCS_OUT */
  7231. #endif /* USETCSETSPEED */
  7232.  
  7233. int
  7234. ttsspd(cps) int cps; {
  7235.     int x;
  7236. #ifdef POSIX
  7237. /* Watch out, speed_t should be unsigned, so don't compare with -1, etc... */
  7238.     speed_t
  7239. #else
  7240.     int
  7241. #endif /* POSIX */
  7242.       s, s2;
  7243.     int ok = 1;                /* Speed check result, assume ok */
  7244.  
  7245. #ifdef OLINUXHISPEED
  7246.     unsigned int spd_flags = 0;
  7247.     struct serial_struct serinfo;
  7248. #endif /* OLINUXHISPEED */
  7249.  
  7250.     debug(F101,"ttsspd cps","",cps);
  7251.     debug(F101,"ttsspd ttyfd","",ttyfd);
  7252.     debug(F101,"ttsspd xlocal","",xlocal);
  7253.  
  7254.     if (ttyfd < 0 || xlocal == 0)    /* Don't set speed on console */
  7255.       return(0);
  7256.  
  7257. #ifdef    NETCONN
  7258.     if (netconn) {
  7259. #ifdef TN_COMPORT
  7260.         if (istncomport())
  7261.       return(tnc_set_baud(cps * 10));
  7262.         else
  7263. #endif /* TN_COMPORT */
  7264.     return(0);
  7265.   }
  7266. #endif    /* NETCONN */
  7267. #ifdef NETCMD
  7268.     if (ttpipe) return(0);
  7269. #endif /* NETCMD */
  7270. #ifdef NETPTY
  7271.     if (ttpty) return(0);
  7272. #endif /* NETPTY */
  7273.  
  7274.     if (cps < 0) return(-1);
  7275.     s = s2 = 0;                /* NB: s and s2 might be unsigned */
  7276.  
  7277. #ifdef USETCSETSPEED
  7278.  
  7279.     s = cps * 10L;
  7280.  
  7281.     x = tcgetattr(ttyfd,&ttcur);    /* Get current speed */
  7282.     debug(F101,"ttsspd tcgetattr","",x);
  7283.     if (x < 0)
  7284.       return(-1);
  7285.     debug(F101,"ttsspd TCSETSPEED speed","",s);
  7286.  
  7287.     errno = 0;
  7288.     if (s == 8880L) {            /* 75/1200 split speed requested */
  7289.     tcsetspeed(TCS_IN, &ttcur, 1200L);
  7290.     tcsetspeed(TCS_OUT, &ttcur, 75L);
  7291.     } else
  7292.       tcsetspeed(TCS_ALL, &ttcur, s);    /* Put new speed in structs */
  7293. #ifdef DEBUG
  7294.     if (errno & deblog) {
  7295.     debug(F101,"ttsspd TCSETSPEED errno","",errno);
  7296.     }
  7297. #endif /* DEBUG */
  7298.  
  7299. #ifdef COMMENT
  7300.     tcsetspeed(TCS_ALL, &ttraw, s);
  7301.     tcsetspeed(TCS_ALL, &tttvt, s);
  7302.     tcsetspeed(TCS_ALL, &ttold, s);
  7303. #else
  7304.     if (s == 8880L) {            /* 75/1200 split speed requested */
  7305.     tcsetspeed(TCS_IN, &ttraw, 1200L);
  7306.     tcsetspeed(TCS_OUT, &ttraw, 75L);
  7307.     tcsetspeed(TCS_IN, &tttvt, 1200L);
  7308.     tcsetspeed(TCS_OUT, &tttvt, 75L);
  7309.     tcsetspeed(TCS_IN, &ttold, 1200L);
  7310.     tcsetspeed(TCS_OUT, &ttold, 75L);
  7311.     } else {
  7312.     tcsetspeed(TCS_ALL, &ttraw, s);
  7313.     tcsetspeed(TCS_ALL, &tttvt, s);
  7314.     tcsetspeed(TCS_ALL, &ttold, s);
  7315.     }
  7316. #endif /* COMMENT */
  7317.  
  7318.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur); /* Set the speed */
  7319.     debug(F101,"ttsspd tcsetattr","",x);
  7320.     if (x < 0)
  7321.       return(-1);
  7322.  
  7323. #else  /* Not USETCSETSPEED */
  7324.  
  7325. #ifdef MINIX2        /* Hack alert */
  7326. #define MINIX        /* Use pre-2.0 speed selection for Minix 2.0 as well */
  7327. #endif /* MINIX2 */
  7328.  
  7329.     /* First check that the given speed is valid. */
  7330.  
  7331.     switch (cps) {
  7332. #ifndef MINIX
  7333.       case 0:   s = B0;    break;
  7334.       case 5:   s = B50;   break;
  7335.       case 7:   s = B75;   break;
  7336. #endif /* MINIX */
  7337.       case 11:  s = B110;  break;
  7338. #ifndef MINIX
  7339.       case 13:  s = B134;  break;
  7340.       case 15:  s = B150;  break;
  7341.       case 20:  s = B200;  break;
  7342. #endif /* MINIX */
  7343.       case 30:  s = B300;  break;
  7344. #ifndef MINIX
  7345.       case 60:  s = B600;  break;
  7346. #endif /* MINIX */
  7347.       case 120: s = B1200; break;
  7348. #ifndef MINIX
  7349.       case 180: s = B1800; break;
  7350. #endif /* MINIX */
  7351.       case 240: s = B2400; break;
  7352.       case 480: s = B4800; break;
  7353. #ifndef MINIX
  7354.       case 888: s = B75; s2 = B1200; break; /* 888 = 75/1200 split speed */
  7355. #endif /* MINIX */
  7356. #ifdef B7200
  7357.       case 720: s = B7200; break;
  7358. #endif /* B7200 */
  7359.       case 960: s = B9600; break;
  7360. #ifdef B14400
  7361.       case 1440: s = B14400; break;
  7362. #endif /* B14400 */
  7363. #ifdef B19200
  7364.       case 1920: s = B19200; break;
  7365. #else
  7366. #ifdef EXTA
  7367.       case 1920: s = EXTA; break;
  7368. #endif /* EXTA */
  7369. #endif /* B19200 */
  7370. #ifdef B28800
  7371.       case 2880: s = B28800; break;
  7372. #endif /* B28800 */
  7373. #ifdef B38400
  7374.       case 3840: s = B38400;
  7375. #ifdef OLINUXHISPEED
  7376.         spd_flags = ~ASYNC_SPD_MASK;    /* Nonzero, but zero flags */
  7377. #endif /* OLINUXHISPEED */
  7378.     break;
  7379. #else /* B38400 not defined... */
  7380. #ifdef EXTB
  7381.       case 3840: s = EXTB; break;
  7382. #endif /* EXTB */
  7383. #endif /* B38400 */
  7384.  
  7385. #ifdef HPUX
  7386. #ifdef _B57600
  7387.       case 5760: s = _B57600; break;
  7388. #endif /* _B57600 */
  7389. #ifdef _B115200
  7390.       case 11520: s = _B115200; break;
  7391. #endif /* _B115200 */
  7392. #else
  7393. #ifdef OLINUXHISPEED
  7394. /*
  7395.   This bit from <carlo@sg.tn.tudelft.nl>:
  7396.   "Only note to make is maybe this: When the ASYNC_SPD_CUST flags are set then
  7397.   setting the speed to 38400 will set the custom speed (and ttgspd returns
  7398.   38400), but speeds 57600 and 115200 won't work any more because I didn't
  7399.   want to mess up the speed flags when someone is doing sophisticated stuff
  7400.   like custom speeds..."
  7401. */
  7402.       case 5760: s = B38400; spd_flags = ASYNC_SPD_HI; break;
  7403.       case 11520: s = B38400; spd_flags = ASYNC_SPD_VHI; break;
  7404. #else
  7405. #ifdef B57600
  7406.       case 5760: s = B57600; break;
  7407. #endif /* B57600 */
  7408. #ifdef B76800
  7409.       case 7680: s = B76800; break;
  7410. #endif /* B76800 */
  7411. #ifdef B115200
  7412.       case 11520: s = B115200; break;
  7413. #endif /* B115200 */
  7414. #endif /* OLINUXHISPEED */
  7415. #ifdef B153600
  7416.       case 15360: s = B153600; break;
  7417. #endif /* B153600 */
  7418. #ifdef B230400
  7419.       case 23040: s = B230400; break;
  7420. #endif /* B230400 */
  7421. #ifdef B307200
  7422.       case 30720: s = B307200; break;
  7423. #endif /* B307200 */
  7424. #ifdef B460800
  7425.       case 46080: s = B460800; break;
  7426. #endif /* 460800 */
  7427. #ifdef B921600
  7428.       case 92160: s = B921600; break;
  7429. #endif /* B921600 */
  7430. #endif /* HPUX */
  7431.       default:
  7432.     ok = 0;                /* Good speed not found, so not ok */
  7433.     break;
  7434.     }
  7435.     debug(F101,"ttsspd ok","",ok);
  7436.     debug(F101,"ttsspd s","",s);
  7437.  
  7438.     if (!ok) {
  7439.     debug(F100,"ttsspd fails","",0);
  7440.     return(-1);
  7441.     } else {
  7442.     if (!s2) s2 = s;        /* Set input speed */
  7443. #ifdef Plan9
  7444.     if (p9ttsspd(cps) < 0)
  7445.       return(-1);
  7446. #else
  7447. #ifdef BSD44ORPOSIX
  7448.     x = tcgetattr(ttyfd,&ttcur);    /* Get current speed */
  7449.     debug(F101,"ttsspd tcgetattr","",x);
  7450.     if (x < 0)
  7451.       return(-1);
  7452. #ifdef OLINUXHISPEED
  7453.     debug(F101,"ttsspd spd_flags","",spd_flags);
  7454.     if (spd_flags && spd_flags != ASYNC_SPD_CUST) {
  7455.         if (ioctl(ttyfd, TIOCGSERIAL, &serinfo) < 0) {
  7456.         debug(F100,"ttsspd: TIOCGSERIAL failed","",0);
  7457.         return(-1);
  7458.         } else debug(F100,"ttsspd: TIOCGSERIAL ok","",0);
  7459.         serinfo.flags &= ~ASYNC_SPD_MASK;
  7460.         serinfo.flags |= (spd_flags & ASYNC_SPD_MASK);
  7461.         if (ioctl(ttyfd, TIOCSSERIAL, &serinfo) < 0)
  7462.           return(-1);
  7463.     }
  7464. #endif /* OLINUXHISPEED */
  7465.     cfsetospeed(&ttcur,s);
  7466.     cfsetispeed(&ttcur,s2);
  7467.     cfsetospeed(&ttraw,s);
  7468.     cfsetispeed(&ttraw,s2);
  7469.     cfsetospeed(&tttvt,s);
  7470.     cfsetispeed(&tttvt,s2);
  7471.     cfsetospeed(&ttold,s);
  7472.     cfsetispeed(&ttold,s2);
  7473.     x = tcsetattr(ttyfd,TCSADRAIN,&ttcur);
  7474.     debug(F101,"ttsspd tcsetattr","",x);
  7475.     if (x < 0) return(-1);
  7476. #else
  7477. #ifdef ATTSV
  7478.     if (cps == 888) return(-1);    /* No split speeds, sorry. */
  7479.     x = ioctl(ttyfd,TCGETA,&ttcur);
  7480.     debug(F101,"ttsspd TCGETA ioctl","",x);
  7481.     if (x < 0) return(-1);
  7482.     ttcur.c_cflag &= ~CBAUD;
  7483.     ttcur.c_cflag |= s;
  7484.     tttvt.c_cflag &= ~CBAUD;
  7485.     tttvt.c_cflag |= s;
  7486.     ttraw.c_cflag &= ~CBAUD;
  7487.     ttraw.c_cflag |= s;
  7488.     ttold.c_cflag &= ~CBAUD;
  7489.     ttold.c_cflag |= s;
  7490.     x = ioctl(ttyfd,TCSETAW,&ttcur);
  7491.     debug(F101,"ttsspd TCSETAW ioctl","",x);
  7492.     if (x < 0) return(-1);
  7493. #else
  7494. #ifdef BELLV10
  7495.     x = ioctl(ttyfd,TIOCGDEV,&tdcur);
  7496.     debug(F101,"ttsspd TIOCGDEV ioctl","",x);
  7497.     if (x < 0) return(-1);
  7498.     tdcur.ispeed = s2;
  7499.     tdcur.ospeed = s;
  7500.     errno = 0;
  7501.     ok = ioctl(ttyfd,TIOCSDEV,&tdcur);
  7502.     debug(F101,"ttsspd BELLV10 ioctl","",ok);
  7503.     if (ok < 0) {
  7504.         perror(ttnmsv);
  7505.         debug(F101,"ttsspd BELLV10 errno","",ok);
  7506.         return(-1);
  7507.     }
  7508. #else
  7509.     x = gtty(ttyfd,&ttcur);
  7510.     debug(F101,"ttsspd gtty","",x);
  7511.     if (x < 0) return(-1);
  7512.     ttcur.sg_ospeed = s; ttcur.sg_ispeed = s2;
  7513.     tttvt.sg_ospeed = s; tttvt.sg_ispeed = s2;
  7514.     ttraw.sg_ospeed = s; ttraw.sg_ispeed = s2;
  7515.     ttold.sg_ospeed = s; ttold.sg_ispeed = s2;
  7516.     x = stty(ttyfd,&ttcur);
  7517.     debug(F101,"ttsspd stty","",x);
  7518.     if (x < 0) return(-1);
  7519. #endif /* BELLV10 */
  7520. #endif /* ATTSV */
  7521. #endif /* BSD44ORPOSIX */
  7522. #endif /* Plan9 */
  7523.     }
  7524.     return(1);                /* Return 1 = success. */
  7525. #endif /* USETCSETSPEED */
  7526. }
  7527.  
  7528. #endif /* NOLOCAL */
  7529.  
  7530. /* C O N G S P D  -  Get speed of console terminal  */
  7531.  
  7532. long
  7533. congspd() {
  7534. /*
  7535.   This is a disgusting hack.  The right way to do this would be to pass an
  7536.   argument to ttgspd(), but then we'd need to change the Kermit API and
  7537.   all of the ck?tio.c modules.  (Currently used only for rlogin.)
  7538. */
  7539.     int t1;
  7540.     long spd;
  7541. #ifdef NETCONN
  7542.     int t2 = netconn;
  7543.     netconn = 0;
  7544. #endif /* NETCONN */
  7545.     t1 = ttyfd;
  7546.     ttyfd = -1;
  7547.     spd = ttgspd();
  7548.     debug(F101,"congspd","",spd);
  7549. #ifdef NETCONN
  7550.     netconn = t2;
  7551. #endif /* NETCONN */
  7552.     ttyfd = t1;
  7553.     return(spd);
  7554. }
  7555.  
  7556. /*  T T S P D L I S T  -- Get list of serial speeds allowed on this platform */
  7557.  
  7558. #define NSPDLIST 64
  7559. static long spdlist[NSPDLIST];
  7560. /*
  7561.   As written, this picks up the speeds known at compile time, and thus
  7562.   apply to the system where C-Kermit was built, rather than to the one where
  7563.   it is running.  Suggestions for improvement are always welcome.
  7564. */
  7565. long *
  7566. ttspdlist() {
  7567.     int i;
  7568.     for (i = 0; i < NSPDLIST; i++)    /* Initialize the list */
  7569.       spdlist[i] = -1L;
  7570.     i = 1;
  7571.  
  7572. #ifdef USETCSETSPEED            /* No way to find out what's legal */
  7573.     debug(F100,"ttspdlist USETCSETSPEED","",0);
  7574.     spdlist[i++] = 50L;
  7575. #ifndef UW7
  7576.     spdlist[i++] = 75L;
  7577. #endif /* UW7 */
  7578.     spdlist[i++] = 110L;
  7579. #ifndef UW7
  7580.     spdlist[i++] = 134L;
  7581. #endif /* UW7 */
  7582.     spdlist[i++] = 150L;
  7583.     spdlist[i++] = 200L;
  7584.     spdlist[i++] = 300L;
  7585.     spdlist[i++] = 600L;
  7586.     spdlist[i++] = 1200L;
  7587.     spdlist[i++] = 1800L;
  7588.     spdlist[i++] = 2400L;
  7589.     spdlist[i++] = 4800L;
  7590.     spdlist[i++] = 8880L;
  7591.     spdlist[i++] = 9600L;
  7592.     spdlist[i++] = 14400L;
  7593.     spdlist[i++] = 19200L;
  7594.     spdlist[i++] = 28800L;
  7595. #ifndef UW7
  7596.     spdlist[i++] = 33600L;
  7597. #endif /* UW7 */
  7598.     spdlist[i++] = 38400L;
  7599.     spdlist[i++] = 57600L;
  7600.     spdlist[i++] = 76800L;
  7601.     spdlist[i++] = 115200L;
  7602. #ifndef UW7
  7603.     spdlist[i++] = 153600L;
  7604.     spdlist[i++] = 230400L;
  7605.     spdlist[i++] = 307200L;
  7606.     spdlist[i++] = 460800L;
  7607.     spdlist[i++] = 921600L;
  7608. #endif /* UW7 */
  7609.  
  7610. #else  /* USETCSETSPEED */
  7611.  
  7612.     debug(F100,"ttspdlist no USETCSETSPEED","",0);
  7613.  
  7614. #ifdef B50
  7615.     debug(F101,"ttspdlist B50","",B50);
  7616.     spdlist[i++] = 50L;
  7617. #endif /* B50 */
  7618. #ifdef B75
  7619.     debug(F101,"ttspdlist B75","",B75);
  7620.     spdlist[i++] = 75L;
  7621. #endif /* B75 */
  7622. #ifdef B110
  7623.     debug(F101,"ttspdlist B110","",B110);
  7624.     spdlist[i++] = 110L;
  7625. #endif /* B110 */
  7626. #ifdef B134
  7627.     debug(F101,"ttspdlist B134","",B134);
  7628.     spdlist[i++] = 134L;
  7629. #endif /* B134 */
  7630. #ifdef B150
  7631.     debug(F101,"ttspdlist B150","",B150);
  7632.     spdlist[i++] = 150L;
  7633. #endif /* B150 */
  7634. #ifdef B200
  7635.     debug(F101,"ttspdlist B200","",B200);
  7636.     spdlist[i++] = 200L;
  7637. #endif /* B200 */
  7638. #ifdef B300
  7639.     debug(F101,"ttspdlist B300","",B300);
  7640.     spdlist[i++] = 300L;
  7641. #endif /* B300 */
  7642. #ifdef B600
  7643.     debug(F101,"ttspdlist B600","",B600);
  7644.     spdlist[i++] = 600L;
  7645. #endif /* B600 */
  7646. #ifdef B1200
  7647.     debug(F101,"ttspdlist B1200","",B1200);
  7648.     spdlist[i++] = 1200L;
  7649. #endif /* B1200 */
  7650. #ifdef B1800
  7651.     debug(F101,"ttspdlist B1800","",B1800);
  7652.     spdlist[i++] = 1800L;
  7653. #endif /* B1800 */
  7654. #ifdef B2400
  7655.     debug(F101,"ttspdlist B2400","",B2400);
  7656.     spdlist[i++] = 2400L;
  7657. #endif /* B2400 */
  7658. #ifdef B4800
  7659.     debug(F101,"ttspdlist B4800","",B4800);
  7660.     spdlist[i++] = 4800L;
  7661. #endif /* B4800 */
  7662. #ifdef B9600
  7663.     debug(F101,"ttspdlist B9600","",B9600);
  7664.     spdlist[i++] = 9600L;
  7665. #endif /* B9600 */
  7666. #ifdef B14400
  7667.     debug(F101,"ttspdlist B14400","",B14400);
  7668.     spdlist[i++] = 14400L;
  7669. #endif /* B14400 */
  7670. #ifdef B19200
  7671.     debug(F101,"ttspdlist B19200","",B19200);
  7672.     spdlist[i++] = 19200L;
  7673. #else
  7674. #ifdef EXTA
  7675.     debug(F101,"ttspdlist EXTA","",EXTA);
  7676.     spdlist[i++] = 19200L;
  7677. #endif /* EXTA */
  7678. #endif /* B19200 */
  7679. #ifdef B28800
  7680.     debug(F101,"ttspdlist B28800","",B28800);
  7681.     spdlist[i++] = 28800L;
  7682. #endif /* B28800 */
  7683. #ifdef B33600
  7684.     debug(F101,"ttspdlist B33600","",B33600);
  7685.     spdlist[i++] = 33600L;
  7686. #endif /* B33600 */
  7687. #ifdef B38400
  7688.     debug(F101,"ttspdlist B38400","",B38400);
  7689.     spdlist[i++] = 38400L;
  7690. #else
  7691. #ifdef EXTB
  7692.     debug(F101,"ttspdlist EXTB","",EXTB);
  7693.     spdlist[i++] = 38400L;
  7694. #endif /* EXTB */
  7695. #endif /* B38400 */
  7696. #ifdef _B57600
  7697.     debug(F101,"ttspdlist _B57600","",_B57600);
  7698.     spdlist[i++] = 57600L;
  7699. #else
  7700. #ifdef B57600
  7701.     debug(F101,"ttspdlist B57600","",B57600);
  7702.     spdlist[i++] = 57600L;
  7703. #endif /* B57600 */
  7704. #endif /* _B57600 */
  7705. #ifdef B76800
  7706.     debug(F101,"ttspdlist B76800","",B76800);
  7707.     spdlist[i++] = 76800L;
  7708. #endif /* B76800 */
  7709. #ifdef _B115200
  7710.     debug(F101,"ttspdlist _B115200","",_B115200);
  7711.     spdlist[i++] = 115200L;
  7712. #else
  7713. #ifdef B115200
  7714.     debug(F101,"ttspdlist B115200","",B115200);
  7715.     spdlist[i++] = 115200L;
  7716. #endif /* B115200 */
  7717. #endif /* _B115200 */
  7718. #ifdef B153600
  7719.     debug(F101,"ttspdlist B153600","",B153600);
  7720.     spdlist[i++] = 153600L;
  7721. #endif /* B153600 */
  7722. #ifdef B230400
  7723.     debug(F101,"ttspdlist B230400","",B230400);
  7724.     spdlist[i++] = 230400L;
  7725. #endif /* B230400 */
  7726. #ifdef B307200
  7727.     debug(F101,"ttspdlist B307200","",B307200);
  7728.     spdlist[i++] = 307200L;
  7729. #endif /* B307200 */
  7730. #ifdef B460800
  7731.     debug(F101,"ttspdlist B460800","",B460800);
  7732.     spdlist[i++] = 460800L;
  7733. #endif /* B460800 */
  7734. #ifdef B921600
  7735.     debug(F101,"ttspdlist B921600","",B921600);
  7736.     spdlist[i++] = 921600L;
  7737. #endif /* B921600 */
  7738. #endif /* USETCSETSPEED */
  7739.     spdlist[0] = i - 1;            /* Return count in 0th element */
  7740.     debug(F111,"ttspdlist spdlist","0",spdlist[0]);
  7741.     return((long *)spdlist);
  7742. }
  7743.  
  7744. /* T T G S P D  -  Get speed of currently selected tty line  */
  7745.  
  7746. /*
  7747.   Unreliable.  After SET LINE, it returns an actual speed, but not necessarily
  7748.   the real speed.  On some systems, it returns the line's nominal speed, from
  7749.   /etc/ttytab.  Even if you SET SPEED to something else, this function might
  7750.   not notice.
  7751. */
  7752. long
  7753. ttgspd() {                /* Get current serial device speed */
  7754. #ifdef NOLOCAL
  7755.     return(-1L);
  7756. #else
  7757. #ifdef POSIX
  7758.     speed_t                /* Should be unsigned */
  7759. #else
  7760.     int                    /* Isn't unsigned */
  7761. #endif /* POSIX */
  7762.       s;
  7763.     int x;
  7764.     long ss;
  7765. #ifdef OLINUXHISPEED
  7766.     unsigned int spd_flags = 0;
  7767.     struct serial_struct serinfo;
  7768. #endif /* OLINUXHISPEED */
  7769.  
  7770. #ifdef NETCONN
  7771.     if (netconn) {
  7772. #ifdef TN_COMPORT
  7773.     if (istncomport())
  7774.       return(tnc_get_baud());
  7775.     else
  7776. #endif /* TN_COMPORT */
  7777.       return(-1);            /* -1 if network connection */
  7778.     }
  7779. #endif /* NETCONN */
  7780. #ifdef NETCMD
  7781.     if (ttpipe) return(-1);
  7782. #endif /* NETCMD */
  7783. #ifdef NETPTY
  7784.     if (ttpty) return(-1);
  7785. #endif /* NETPTY */
  7786.  
  7787.     debug(F101,"ttgspd ttyfd","",ttyfd);
  7788.  
  7789. #ifdef USETCSETSPEED
  7790.  
  7791.     x = tcgetattr(ttyfd,&ttcur);    /* Get current speed */
  7792.     debug(F101,"ttgspd tcgetattr","",x);
  7793.     if (x < 0)
  7794.       return(-1);
  7795.     errno = 0;
  7796.     s = tcgetspeed(TCS_ALL, &ttcur);
  7797.     debug(F101,"ttsspd TCGETSPEED speed","",s);
  7798.     if (s == 0) {
  7799.     long s1, s2;
  7800.     s1 = tcgetspeed(TCS_IN, &ttcur);
  7801.     s2 = tcgetspeed(TCS_OUT, &ttcur);
  7802.     if (s1 == 1200L && s2 == 75L)
  7803.       return(8880L);
  7804.     }
  7805. #ifdef DEBUG
  7806.     if (errno & deblog) {
  7807.     debug(F101,"ttsspd TCGETSPEED errno","",errno);
  7808.     }
  7809. #endif /* DEBUG */
  7810.     return(s);
  7811.  
  7812. #else  /* Not USETCSETSPEED */
  7813.  
  7814. #ifdef Plan9
  7815.     if (ttyfd < 0)
  7816.       ss = -1;
  7817.     else
  7818.       ss = ttylastspeed;
  7819. #else
  7820. #ifdef OLINUXHISPEED
  7821.     debug(F100,"ttgspd Linux OLINUXHISPEED","",0);
  7822. #endif /* OLINUXHISPEED */
  7823.  
  7824.     if (ttyfd < 0) {
  7825. #ifdef BSD44ORPOSIX
  7826.     s = cfgetospeed(&ccold);
  7827.     debug(F101,"ttgspd cfgetospeed 1 POSIX","",s);
  7828. #else
  7829. #ifdef ATTSV
  7830.     s = ccold.c_cflag & CBAUD;
  7831.     debug(F101,"ttgspd c_cflag CBAUD 1 ATTSV","",s);
  7832. #else
  7833.     s = ccold.sg_ospeed;        /* (obtained by congm()) */
  7834.     debug(F101,"ttgspd sg_ospeed 1","",s);
  7835. #endif /* ATTSV */
  7836. #endif /* BSD44POSIX */
  7837.  
  7838.     } else {
  7839. #ifdef BSD44ORPOSIX
  7840.     if (tcgetattr(ttyfd,&ttcur) < 0) return(-1);
  7841.     s = cfgetospeed(&ttcur);
  7842.     debug(F101,"ttgspd cfgetospeed 2 BSDORPOSIX","",s);
  7843. #ifdef OLINUXHISPEED
  7844.     if (ioctl(ttyfd,TIOCGSERIAL,&serinfo) > -1)
  7845.       spd_flags = serinfo.flags & ASYNC_SPD_MASK;
  7846.     debug(F101,"ttgspd spd_flags","",spd_flags);
  7847. #endif /* OLINUXHISPEED */
  7848. #else
  7849. #ifdef ATTSV
  7850.     x = ioctl(ttyfd,TCGETA,&ttcur);
  7851.     debug(F101,"ttgspd ioctl 2 ATTSV x","",x);
  7852.     debug(F101,"ttgspd ioctl 2 ATTSV errno","",errno);
  7853.     if (x < 0) return(-1);
  7854.     s = ttcur.c_cflag & CBAUD;
  7855.     debug(F101,"ttgspd ioctl 2 ATTSV speed","",s);
  7856. #else
  7857. #ifdef BELLV10
  7858.     x = ioctl(ttyfd,TIOCGDEV,&tdcur);
  7859.     debug(F101,"ttgspd ioctl 2 BELLV10 x","",x);
  7860.     if (x < 0) return(-1);
  7861.     s = tdcur.ospeed;
  7862.     debug(F101,"ttgspd ioctl 2 BELLV10 speed","",s);
  7863. #else
  7864.     x = gtty(ttyfd,&ttcur);
  7865.     debug(F101,"ttgspd gtty 2 x","",x);
  7866.     debug(F101,"ttgspd gtty 2 errno","",errno);
  7867.     if (x < 0) return(-1);
  7868.     s = ttcur.sg_ospeed;
  7869.     debug(F101,"ttgspd gtty 2 speed","",s);
  7870. #endif /* BELLV10 */
  7871. #endif /* ATTSV */
  7872. #endif /* BSD44ORPOSIX */
  7873.     }
  7874.     debug(F101,"ttgspd code","",s);
  7875. #ifdef OLINUXHISPEED
  7876.     debug(F101,"ttgspd spd_flags","",spd_flags);
  7877. #endif /* OLINUXHISPEED */
  7878.     switch (s) {
  7879. #ifdef B0
  7880.       case B0:    ss = 0L; break;
  7881. #endif /* B0 */
  7882.  
  7883. #ifndef MINIX
  7884. /*
  7885.  MINIX defines the Bxx symbols to be bps/100, so B50==B75, B110==B134==B150,
  7886.  etc, making for many "duplicate case in switch" errors, which are fatal.
  7887. */
  7888. #ifdef B50
  7889.       case B50:   ss = 50L; break;
  7890. #endif /* B50 */
  7891. #ifdef B75
  7892.       case B75:   ss = 75L; break;
  7893. #endif /* B75 */
  7894. #endif /* MINIX */
  7895.  
  7896. #ifdef B110
  7897.       case B110:  ss = 110L; break;
  7898. #endif /* B110 */
  7899.  
  7900. #ifndef MINIX
  7901. #ifdef B134
  7902.       case B134:  ss = 134L; break;
  7903. #endif /* B134 */
  7904. #ifdef B150
  7905.       case B150:  ss = 150L; break;
  7906. #endif /* B150 */
  7907. #endif /* MINIX */
  7908.  
  7909. #ifdef B200
  7910.       case B200:  ss = 200L; break;
  7911. #endif /* B200 */
  7912.  
  7913. #ifdef B300
  7914.       case B300:  ss = 300L; break;
  7915. #endif /* B300 */
  7916.  
  7917. #ifdef B600
  7918.       case B600:  ss = 600L; break;
  7919. #endif /* B600 */
  7920.  
  7921. #ifdef B1200
  7922.       case B1200: ss = 1200L; break;
  7923. #endif /* B1200 */
  7924.  
  7925. #ifdef B1800
  7926.       case B1800: ss = 1800L; break;
  7927. #endif /* B1800 */
  7928.  
  7929. #ifdef B2400
  7930.       case B2400: ss = 2400L; break;
  7931. #endif /* B2400 */
  7932.  
  7933. #ifdef B4800
  7934.       case B4800: ss = 4800L; break;
  7935. #endif /* B4800 */
  7936.  
  7937. #ifdef B7200
  7938.       case B7200: ss = 7200L; break;
  7939. #endif /* B7200 */
  7940.  
  7941. #ifdef B9600
  7942.       case B9600: ss = 9600L; break;
  7943. #endif /* B9600 */
  7944.  
  7945. #ifdef B19200
  7946.       case B19200: ss = 19200L; break;
  7947. #else
  7948. #ifdef EXTA
  7949.       case EXTA: ss = 19200L; break;
  7950. #endif /* EXTA */
  7951. #endif /* B19200 */
  7952.  
  7953. #ifdef MINIX2
  7954. /* End of hack to make MINIX2 use MINIX1 speed setting */
  7955. #undef MINIX
  7956. #endif /* MINIX2 */
  7957.  
  7958. #ifndef MINIX
  7959. #ifdef B38400
  7960.       case B38400:
  7961.         ss = 38400L;
  7962. #ifdef OLINUXHISPEED
  7963.         switch(spd_flags) {
  7964.           case ASYNC_SPD_HI:  ss =  57600L; break;
  7965.           case ASYNC_SPD_VHI: ss = 115200L; break;
  7966.     }
  7967. #endif /* OLINUXHISPEED */
  7968.         break;
  7969. #else
  7970. #ifdef EXTB
  7971.       case EXTB: ss = 38400L; break;
  7972. #endif /* EXTB */
  7973. #endif /* B38400 */
  7974. #endif /* MINIX */
  7975.  
  7976. #ifdef HPUX
  7977. #ifdef _B57600
  7978.       case _B57600: ss = 57600L; break;
  7979. #endif /* _B57600 */
  7980. #ifdef _B115200
  7981.       case _B115200: ss = 115200L; break;
  7982. #endif /* _B115200 */
  7983. #else
  7984. #ifdef B57600
  7985.       case B57600: ss = 57600L; break;
  7986. #endif /* B57600 */
  7987. #ifdef B76800
  7988.       case B76800: ss = 76800L; break;
  7989. #endif /* B76800 */
  7990. #ifdef B115200
  7991.       case B115200: ss = 115200L; break;
  7992. #endif /* B115200 */
  7993. #ifdef B153600
  7994.       case B153600: ss = 153600L; break;
  7995. #endif /* B153600 */
  7996. #ifdef B230400
  7997.       case B230400: ss = 230400L; break;
  7998. #endif /* B230400 */
  7999. #ifdef B307200
  8000.       case B307200: ss = 307200L; break;
  8001. #endif /* B307200 */
  8002. #ifdef B460800
  8003.       case B460800: ss = 460800L; break;
  8004. #endif /* B460800 */
  8005. #endif /* HPUX */
  8006. #ifdef B921600
  8007.       case 92160: ss = 921600L; break;
  8008. #endif /* B921600 */
  8009.       default:
  8010.     ss = -1; break;
  8011.     }
  8012. #endif /* Plan9 */
  8013.     debug(F101,"ttgspd speed","",ss);
  8014.     return(ss);
  8015.  
  8016. #endif /* USETCSETSPEED */
  8017. #endif /* NOLOCAL */
  8018. }
  8019. #ifdef MINIX2                /* Another hack alert */
  8020. #define MINIX
  8021. #endif /* MINIX2 */
  8022.  
  8023. /*
  8024.   FIONREAD data type...  This has been defined as "long" for many, many
  8025.   years, and it worked OK until 64-bit platforms appeared.  Thus we use
  8026.   int for 64-bit platforms, but keep long for the others.  If we changed
  8027.   the default PEEKTYPE to int, this would probably break 16-bit builds
  8028.   (note that sizeof(long) == sizeof(int) on most 32-bit platforms), many
  8029.   of which we have no way of testing any more.  Therefore, do not change
  8030.   the default definition of PEEKTYPE -- only add exceptions to it as needed.
  8031. */
  8032. #ifdef COHERENT
  8033. #ifdef FIONREAD
  8034. #undef FIONREAD
  8035. #endif /* FIONREAD */
  8036. /* #define FIONREAD TIOCQUERY */
  8037. /* #define PEEKTYPE int */
  8038. #else  /* Not COHERENT... */
  8039.  
  8040. #ifdef OSF32                /* Digital UNIX 3.2 or higher */
  8041. #define PEEKTYPE int
  8042. #else
  8043. #define PEEKTYPE long            /* Elsewhere (see notes above) */
  8044. #endif /* OSF32 */
  8045. #endif /* COHERENT */
  8046.  
  8047. /* ckumyr.c by Kristoffer Eriksson, ske@pkmab.se, 15 Mar 1990. */
  8048.  
  8049. #ifdef MYREAD
  8050.  
  8051. /* Private buffer for myread() and its companions.  Not for use by anything
  8052.  * else.  ttflui() is allowed to reset them to initial values.  ttchk() is
  8053.  * allowed to read my_count.
  8054.  *
  8055.  * my_item is an index into mybuf[].  Increment it *before* reading mybuf[].
  8056.  *
  8057.  * A global parity mask variable could be useful too.  We could use it to
  8058.  * let myread() strip the parity on its own, instead of stripping sign
  8059.  * bits as it does now.
  8060.  */
  8061. #ifdef BIGBUFOK
  8062. #define MYBUFLEN 32768
  8063. #else
  8064. #ifdef pdp11
  8065. #define MYBUFLEN 256
  8066. #else
  8067. #define MYBUFLEN 1024
  8068. #endif /* pdp11 */
  8069. #endif /* BIGBUFOK */
  8070.  
  8071. #ifdef ANYX25
  8072. #undef MYBUFLEN
  8073. #define MYBUFLEN 256
  8074. /*
  8075.   On X.25 connections, there is an extra control byte at the beginning.
  8076. */
  8077. static CHAR x25buf[MYBUFLEN+1];        /* Communication device input buffer */
  8078. static CHAR  *mybuf = x25buf+1;
  8079. #else
  8080. static CHAR mybuf[MYBUFLEN];
  8081. #endif /* ANYX25 */
  8082.  
  8083. static int my_count = 0;        /* Number of chars still in mybuf */
  8084. static int my_item = -1;        /* Last index read from mybuf[]   */
  8085.  
  8086. /*  T T P E E K  --  Peek into our internal communications input buffers. */
  8087.  
  8088. /*
  8089.   NOTE: This routine is peculiar to UNIX, and is used only by the
  8090.   select()-based CONNECT module, ckucns.c.  It need not be replicated in
  8091.   the ck?tio.c of other platforms.
  8092. */
  8093. int
  8094. ttpeek() {
  8095. #ifdef TTLEBUF
  8096.     int rc = 0;
  8097.     if (ttpush >= 0)
  8098.       rc++;
  8099.     rc += le_inbuf();
  8100.     if (rc > 0)
  8101.       return(rc);
  8102.     else
  8103. #endif /* TTLEBUF */
  8104.  
  8105. #ifdef MYREAD
  8106.     return(my_count);
  8107. #else
  8108.     return(0);
  8109. #endif /* MYREAD */
  8110. }
  8111.  
  8112. /* myread() -- Efficient read of one character from communications line.
  8113.  *
  8114.  * Uses a private buffer to minimize the number of expensive read() system
  8115.  * calls.  Essentially performs the equivalent of read() of 1 character, which
  8116.  * is then returned.  By reading all available input from the system buffers
  8117.  * to the private buffer in one chunk, and then working from this buffer, the
  8118.  * number of system calls is reduced in any case where more than one character
  8119.  * arrives during the processing of the previous chunk, for instance high
  8120.  * baud rates or network type connections where input arrives in packets.
  8121.  * If the time needed for a read() system call approaches the time for more
  8122.  * than one character to arrive, then this mechanism automatically compensates
  8123.  * for that by performing bigger read()s less frequently.  If the system load
  8124.  * is high, the same mechanism compensates for that too.
  8125.  *
  8126.  * myread() is a macro that returns the next character from the buffer.  If the
  8127.  * buffer is empty, mygetbuf() is called.  See mygetbuf() for possible error
  8128.  * returns.
  8129.  *
  8130.  * This should be efficient enough for any one-character-at-a-time loops.
  8131.  * For even better efficiency you might use memcpy()/bcopy() or such between
  8132.  * buffers (since they are often better optimized for copying), but it may not
  8133.  * be worth it if you have to take an extra pass over the buffer to strip
  8134.  * parity and check for CTRL-C anyway.
  8135.  *
  8136.  * Note that if you have been using myread() from another program module, you
  8137.  * may have some trouble accessing this macro version and the private variables
  8138.  * it uses.  In that case, just add a function in this module, that invokes the
  8139.  * macro.
  8140.  */
  8141. #define myread() (--my_count < 0 ? mygetbuf() : 255 & (int)mybuf[++my_item])
  8142.  
  8143. /* Specification: Push back up to one character onto myread()'s queue.
  8144.  *
  8145.  * This implementation: Push back characters into mybuf. At least one character
  8146.  * must have been read through myread() before myunrd() may be used.  After
  8147.  * EOF or read error, again, myunrd() can not be used.  Sometimes more than
  8148.  * one character can be pushed back, but only one character is guaranteed.
  8149.  * Since a previous myread() must have read its character out of mybuf[],
  8150.  * that guarantees that there is space for at least one character.  If push
  8151.  * back was really needed after EOF, a small addition could provide that.
  8152.  *
  8153.  * myunrd() is currently not called from anywhere inside kermit...
  8154.  */
  8155. #ifdef COMMENT /* not used */
  8156. myunrd(ch) CHAR ch; {
  8157.     if (my_item >= 0) {
  8158.     mybuf[my_item--] = ch;
  8159.     ++my_count;
  8160.     }
  8161. }
  8162. #endif /* COMMENT */
  8163.  
  8164. /*  T T P U S H B A C K  --  Put n bytes back into the myread buffer */
  8165.  
  8166. static CHAR * pushbuf = NULL;
  8167. /* static int pushed = 0; */
  8168.  
  8169. int
  8170. ttpushback(s,n) CHAR * s; int n; {
  8171.     debug(F101,"ttpushback n","",n);
  8172.     if (pushbuf || n > MYBUFLEN || n < 1)
  8173.       return(-1);
  8174.     debug(F101,"ttpushback my_count","",my_count);
  8175.     if (my_count > 0) {
  8176.     if (!(pushbuf = (CHAR *)malloc(n+1)))
  8177.       return(-1);
  8178.     memcpy(pushbuf,mybuf,my_count);
  8179.     /* pushed = my_count; */ /* (set but never used) */
  8180.     }
  8181.     memcpy(mybuf,s,n);
  8182.     my_count = n;
  8183.     my_item = -1;
  8184.     return(0);
  8185. }
  8186.  
  8187. /* mygetbuf() -- Fill buffer for myread() and return first character.
  8188.  *
  8189.  * This function is what myread() uses when it can't get the next character
  8190.  * directly from its buffer.  First, it calls a system dependent myfillbuf()
  8191.  * to read at least one new character into the buffer, and then it returns
  8192.  * the first character just as myread() would have done.  This function also
  8193.  * is responsible for all error conditions that myread() can indicate.
  8194.  *
  8195.  * Returns: When OK    => a positive character, 0 or greater.
  8196.  *        When EOF    => -2.
  8197.  *        When error    => -3, error code in errno.
  8198.  *
  8199.  * Older myread()s additionally returned -1 to indicate that there was nothing
  8200.  * to read, upon which the caller would call myread() again until it got
  8201.  * something.  The new myread()/mygetbuf() always gets something.  If it
  8202.  * doesn't, then make it do so!  Any program that actually depends on the old
  8203.  * behaviour will break.
  8204.  *
  8205.  * The older version also used to return -2 both for EOF and other errors,
  8206.  * and used to set errno to 9999 on EOF.  The errno stuff is gone, EOF and
  8207.  * other errors now return different results, although Kermit currently never
  8208.  * checks to see which it was.  It just disconnects in both cases.
  8209.  *
  8210.  * Kermit lets the user use the quit key to perform some special commands
  8211.  * during file transfer.  This causes read(), and thus also mygetbuf(), to
  8212.  * finish without reading anything and return the EINTR error.  This should
  8213.  * be checked by the caller.  Mygetbuf() could retry the read() on EINTR,
  8214.  * but if there is nothing to read, this could delay Kermit's reaction to
  8215.  * the command, and make Kermit appear unresponsive.
  8216.  *
  8217.  * The debug() call should be removed for optimum performance.
  8218.  */
  8219. int
  8220. mygetbuf() {
  8221.     int x;
  8222.     errno = 0;
  8223. #ifdef DEBUG
  8224.     if (deblog && my_count > 0)
  8225.       debug(F101,"mygetbuf IMPROPERLY CALLED with my_count","",my_count);
  8226. #endif /* DEBUG */
  8227.     if (my_count <= 0)
  8228.       my_count = myfillbuf();
  8229.  
  8230. #ifdef DEBUG
  8231. #ifdef COMMENT
  8232.     if (deblog) debug(F101, "mygetbuf read", "", my_count);
  8233. #else /* COMMENT */
  8234.     if (deblog) hexdump("mygetbuf read", mybuf, my_count);
  8235. #endif /* COMMENT */
  8236. #endif /* DEBUG */
  8237.     x = my_count;
  8238.     if (my_count <= 0) {
  8239.     my_count = 0;
  8240.     my_item = -1;
  8241.     debug(F101,"mygetbuf errno","",errno);
  8242. #ifdef TCPSOCKET
  8243.     if (netconn && ttnet == NET_TCPB && errno != 0) {
  8244.         if (errno != EINTR) {
  8245.         debug(F101,"mygetbuf TCP error","",errno);
  8246.         ttclos(0);        /* Close the connection. */
  8247.         }
  8248.         return(-3);
  8249.     }
  8250. #endif /* TCPSOCKET */
  8251.     if (!netconn && xlocal && errno) {
  8252.         if (errno != EINTR) {
  8253.         debug(F101,"mygetbuf SERIAL error","",errno);
  8254.         x = -3;
  8255.         ttclos(0);        /* Close the connection. */
  8256.         }
  8257.     }
  8258.     return((x < 0) ? -3 : -2);
  8259.     }
  8260.     --my_count;
  8261.     return((unsigned)(0xff & mybuf[my_item = 0]));
  8262. }
  8263.  
  8264. /* myfillbuf():
  8265.  * System-dependent read() into mybuf[], as many characters as possible.
  8266.  *
  8267.  * Returns: OK => number of characters read, always more than zero.
  8268.  *          EOF => 0
  8269.  *          Error => -1, error code in errno.
  8270.  *
  8271.  * If there is input available in the system's buffers, all of it should be
  8272.  * read into mybuf[] and the function return immediately.  If no input is
  8273.  * available, it should wait for a character to arrive, and return with that
  8274.  * one in mybuf[] as soon as possible.  It may wait somewhat past the first
  8275.  * character, but be aware that any such delay lengthens the packet turnaround
  8276.  * time during kermit file transfers.  Should never return with zero characters
  8277.  * unless EOF or irrecoverable read error.
  8278.  *
  8279.  * Correct functioning depends on the correct tty parameters being used.
  8280.  * Better control of current parameters is required than may have been the
  8281.  * case in older Kermit releases.  For instance, O_NDELAY (or equivalent) can
  8282.  * no longer be sometimes off and sometimes on like it used to, unless a
  8283.  * special myfillbuf() is written to handle that.  Otherwise the ordinary
  8284.  * myfillbuf()s may think they have come to EOF.
  8285.  *
  8286.  * If your system has a facility to directly perform the functioning of
  8287.  * myfillbuf(), then use it.  If the system can tell you how many characters
  8288.  * are available in its buffers, then read that amount (but not less than 1).
  8289.  * If the system can return a special indication when you try to read without
  8290.  * anything to read, while allowing you to read all there is when there is
  8291.  * something, you may loop until there is something to read, but probably that
  8292.  * is not good for the system load.
  8293.  */
  8294.  
  8295. #ifdef SVORPOSIX
  8296.     /* This is for System III/V with VMIN>0, VTIME=0 and O_NDELAY off,
  8297.      * and CLOCAL set any way you like.  This way, read() will do exactly
  8298.      * what is required by myfillbuf(): If there is data in the buffers
  8299.      * of the O.S., all available data is read into mybuf, up to the size
  8300.      * of mybuf.  If there is none, the first character to arrive is
  8301.      * awaited and returned.
  8302.      */
  8303. int
  8304. myfillbuf() {
  8305.     int fd, n;
  8306. #ifdef NETCMD
  8307.     if (ttpipe)
  8308.       fd = fdin;
  8309.     else
  8310. #endif /* NETCMD */
  8311.       fd = ttyfd;
  8312.  
  8313. #ifdef sxaE50
  8314.     /* From S. Dezawa at Fujifilm in Japan.  I don't know why this is */
  8315.     /* necessary for the sxa E50, but it is. */
  8316.     return read(fd, mybuf, 255);
  8317. #else
  8318. #ifdef BEOSORBEBOX
  8319.     while (1) {
  8320. #ifdef NETCONN
  8321.         if (netconn) {
  8322.             n = netxin(sizeof(mybuf), (char *)mybuf);
  8323.             debug(F101,"BEBOX SVORPOSIX network myfillbuf","",n);
  8324.     }
  8325.         else
  8326. #endif /* NETCONN */
  8327.       n = read(fd, mybuf, sizeof(mybuf));
  8328.     debug(F101,"BEBOX SVORPOSIX notnet myfillbuf","",n);
  8329.         if (n > 0)
  8330.       return(n);
  8331.         snooze(1000.0);
  8332.     }
  8333. #else /* BEOSORBEBOX */
  8334.     errno = 0;
  8335.     debug(F100,"SVORPOSIX myfillbuf calling read()","",0);
  8336. #ifdef IBMX25
  8337.     if (netconn && (nettype == NET_IX25)) {
  8338.     /* can't use sizeof because mybuf is a pointer, and not an array! */
  8339.     n = x25xin( MYBUFLEN, mybuf );
  8340.     } else
  8341. #endif /* IBMX25 */
  8342.  
  8343. #ifdef CK_SSL
  8344.       if (ssl_active_flag || tls_active_flag) {
  8345.       int error, n = 0;
  8346.       while (n == 0) {
  8347.           if (ssl_active_flag)
  8348.                 n = SSL_read(ssl_con, (char *)mybuf, sizeof(mybuf));
  8349.           else if (tls_active_flag)
  8350.                 n = SSL_read(tls_con, (char *)mybuf, sizeof(mybuf));
  8351.               else
  8352.         break;
  8353.           switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,n)) {
  8354.         case SSL_ERROR_NONE:
  8355.           if (n > 0)
  8356.                     return(n);
  8357.           if (n < 0)
  8358.                     return(-2);
  8359.           msleep(50);
  8360.           break;
  8361.         case SSL_ERROR_WANT_WRITE:
  8362.         case SSL_ERROR_WANT_READ:
  8363.           return(-1);
  8364.         case SSL_ERROR_SYSCALL:
  8365.           if (n != 0)
  8366.             return(-1);
  8367.         case SSL_ERROR_WANT_X509_LOOKUP:
  8368.         case SSL_ERROR_SSL:
  8369.         case SSL_ERROR_ZERO_RETURN:
  8370.         default:
  8371.           ttclos(0);
  8372.           return(-3);
  8373.             }
  8374.         }
  8375.     }
  8376. #endif /* CK_SSL */
  8377. #ifdef CK_KERBEROS
  8378. #ifdef KRB4
  8379. #ifdef RLOGCODE
  8380.     if (ttnproto == NP_EK4LOGIN) {
  8381.         if ((n = krb4_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8382.       return(-3);
  8383.         else
  8384.       return(n);
  8385.     }
  8386. #endif /* RLOGCODE */
  8387. #endif /* KRB4 */
  8388. #ifdef KRB5
  8389. #ifdef RLOGCODE
  8390.     if (ttnproto == NP_EK5LOGIN) {
  8391.         if ((n = krb5_des_read(ttyfd,mybuf,sizeof(mybuf),0)) < 0)
  8392.       return(-3);
  8393.         else
  8394.       return(n);
  8395.     }
  8396. #endif /* RLOGCODE */
  8397. #ifdef KRB5_U2U
  8398.     if (ttnproto == NP_K5U2U) {
  8399.         if ((n = krb5_u2u_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8400.       return(-3);
  8401.         else
  8402.       return(n);
  8403.     }
  8404. #endif /* KRB5_U2U */
  8405. #endif /* KRB5 */
  8406. #endif /* CK_KERBEROS */
  8407.  
  8408. #ifdef NETPTY
  8409. #ifdef HAVE_PTYTRAP
  8410.     /* Special handling for HP-UX pty i/o */
  8411.   ptyread:
  8412.     if (ttpty && pty_trap_pending(ttyfd) > 0) {
  8413.         if (pty_trap_handler(ttyfd) > 0) {
  8414.             ttclos(0);
  8415.             return(-3);
  8416.         }
  8417.     }
  8418. #endif /* HAVE_PTYTRAP */
  8419. #endif /* NETPTY */
  8420.     n = read(fd, mybuf, sizeof(mybuf));
  8421.     debug(F101,"SVORPOSIX myfillbuf","",n);
  8422.     debug(F101,"SVORPOSIX myfillbuf ttcarr","",ttcarr);
  8423.     debug(F101,"SVORPOSIX myfillbuf errno","",errno);
  8424.     if (n < 1) {
  8425. #ifdef NETPTY
  8426. #ifdef HAVE_PTYTRAP
  8427.         /* When we have a PTY trap in place the connection cannot */
  8428.         /* be closed until the trap receives a close indication.  */
  8429.         if (n == 0 && ttpty)
  8430.             goto ptyread;
  8431. #endif /* HAVE_PTYTRAP */
  8432. #endif /* NETPTY */
  8433.         return(-3);
  8434.     }
  8435.     return(n);
  8436. #endif /* BEOSORBEBOX */
  8437. #endif /* sxaE50 */
  8438. }
  8439.  
  8440. #else /* not AT&T or POSIX */
  8441.  
  8442. #ifdef aegis
  8443.     /* This is quoted from the old myread().  The semantics seem to be
  8444.      * alright, but maybe errno would not need to be set even when
  8445.      * there is no error?  I don't know aegis.
  8446.      */
  8447. int
  8448. myfillbuf() {
  8449.     int count;
  8450. #ifdef NETCMD
  8451.     if (ttpipe)
  8452.       fd = fdin;
  8453.     else
  8454. #endif /* NETCMD */
  8455.       fd = ttyfd;
  8456.  
  8457.     count = ios_$get((short)fd, ios_$cond_opt, mybuf, 256L, st);
  8458.     errno = EIO;
  8459.     if (st.all == ios_$get_conditional_failed) /* get at least one */
  8460.       count = ios_$get((short)fd, 0, mybuf, 1L, st);
  8461.     if (st.all == ios_$end_of_file)
  8462.       return(-3);
  8463.     else if (st.all != status_$ok) {
  8464.     errno = EIO;
  8465.     return(-1);
  8466.     }
  8467.     return(count > 0 ? count : -3);
  8468. }
  8469. #else /* !aegis */
  8470.  
  8471. #ifdef FIONREAD
  8472.     /* This is for systems with FIONREAD.  FIONREAD returns the number
  8473.      * of characters available for reading. If none are available, wait
  8474.      * until something arrives, otherwise return all there is.
  8475.      */
  8476. int
  8477. myfillbuf() {
  8478.     PEEKTYPE avail = 0;
  8479.     int x, fd;
  8480. #ifdef NETCMD
  8481.     if (ttpipe)
  8482.       fd = fdin;
  8483.     else
  8484. #endif /* NETCMD */
  8485.       fd = ttyfd;
  8486.  
  8487. #ifdef SUNX25
  8488. /*
  8489.   SunLink X.25 support in this routine from Stefaan A. Eeckels, Eurostat (CEC).
  8490.   Depends on SunOS having FIONREAD, not because we use it, but just so this
  8491.   code is grouped correctly within the #ifdefs.  Let's hope Solaris keeps it.
  8492.  
  8493.   We call x25xin() instead of read() so that Q-Bit packets, which contain
  8494.   X.25 service-level information (e.g. PAD parameter changes), can be processed
  8495.   transparently to the upper-level code.  This is a blocking read, and so
  8496.   we depend on higher-level code (such as ttinc()) to set any necessary alarms.
  8497. */
  8498.     extern int nettype;
  8499.     if (netconn && nettype == NET_SX25) {
  8500.     while ((x = x25xin(sizeof(x25buf), x25buf)) < 1) ;
  8501.     return(x - 1);            /* "-1" compensates for extra status byte */
  8502.     }
  8503. #endif /* SUNX25 */
  8504.  
  8505. #ifdef CK_SSL
  8506.     if (ssl_active_flag || tls_active_flag) {
  8507.         int error, n = 0;
  8508.         while (n == 0) {
  8509.             if (ssl_active_flag)
  8510.           n = SSL_read(ssl_con, (char *)mybuf, sizeof(mybuf));
  8511.             else
  8512.           n = SSL_read(tls_con, (char *)mybuf, sizeof(mybuf));
  8513.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,n)) {
  8514.           case SSL_ERROR_NONE:
  8515.                 if (n > 0)
  8516.           return(n);
  8517.                 if (n < 0)
  8518.           return(-2);
  8519.                 msleep(50);
  8520.                 break;
  8521.           case SSL_ERROR_WANT_WRITE:
  8522.           case SSL_ERROR_WANT_READ:
  8523.                 return(-1);
  8524.           case SSL_ERROR_SYSCALL:
  8525.         if (n != 0)
  8526.           return(-1);
  8527.           case SSL_ERROR_WANT_X509_LOOKUP:
  8528.           case SSL_ERROR_SSL:
  8529.           case SSL_ERROR_ZERO_RETURN:
  8530.           default:
  8531.                 ttclos(0);
  8532.                 return(-2);
  8533.             }
  8534.         }
  8535.     }
  8536. #endif /* CK_SSL */
  8537. #ifdef CK_KERBEROS
  8538. #ifdef KRB4
  8539. #ifdef RLOGCODE
  8540.     if (ttnproto == NP_EK4LOGIN) {
  8541.         if ((x = krb4_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8542.       return(-1);
  8543.         else
  8544.       return(x);
  8545.     }
  8546. #endif /* RLOGCODE */
  8547. #endif /* KRB4 */
  8548. #ifdef KRB5
  8549. #ifdef RLOGCODE
  8550.     if (ttnproto == NP_EK5LOGIN) {
  8551.         if ((x = krb5_des_read(ttyfd,mybuf,sizeof(mybuf),0)) < 0)
  8552.       return(-1);
  8553.         else
  8554.       return(x);
  8555.     }
  8556. #endif /* RLOGCODE */
  8557. #ifdef KRB5_U2U
  8558.     if (ttnproto == NP_K5U2U) {
  8559.         if ((x = krb5_u2u_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8560.       return(-1);
  8561.         else
  8562.       return(x);
  8563.     }
  8564. #endif /* KRB5_U2U */
  8565. #endif /* KRB5 */
  8566. #endif /* CK_KERBEROS */
  8567.  
  8568.     errno = 0;
  8569.     debug(F101,"myfillbuf calling FIONREAD ioctl","",xlocal);
  8570.     x = ioctl(fd, FIONREAD, &avail);
  8571. #ifdef DEBUG
  8572.     if (deblog) {
  8573.     debug(F101,"myfillbuf FIONREAD","",x);
  8574.     debug(F101,"myfillbuf FIONREAD avail","",avail);
  8575.     debug(F101,"myfillbuf FIONREAD errno","",errno);
  8576.     }
  8577. #endif /* DEBUG */
  8578.     if (x < 0 || avail == 0)
  8579.       avail = 1;
  8580.  
  8581.     if (avail > MYBUFLEN)
  8582.       avail = MYBUFLEN;
  8583.  
  8584.     errno = 0;
  8585.  
  8586.     x = read(fd, mybuf, (int) avail);
  8587. #ifdef DEBUG
  8588.     if (deblog) {
  8589.     debug(F101,"myfillbuf avail","",avail);
  8590.     debug(F101,"myfillbuf read","",x);
  8591.     debug(F101,"myfillbuf read errno","",errno);
  8592.         if (x > 0)
  8593.       hexdump("myfillbuf mybuf",mybuf,x);
  8594.     }
  8595. #endif /* DEBUG */
  8596.     if (x < 1) x = -3;            /* read 0 == connection loss */
  8597.     return(x);
  8598. }
  8599.  
  8600. #else /* !FIONREAD */
  8601. /* Add other systems here, between #ifdef and #else, e.g. NETCONN. */
  8602. /* When there is no other possibility, read 1 character at a time. */
  8603. int
  8604. myfillbuf() {
  8605.     int x;
  8606.  
  8607. #ifdef CK_SSL
  8608.     if (ssl_active_flag || tls_active_flag) {
  8609.         int error, n = 0;
  8610.         while (n == 0) {
  8611.             if (ssl_active_flag)
  8612.           n = SSL_read(ssl_con, (char *)mybuf, sizeof(mybuf));
  8613.             else
  8614.           count = SSL_read(tls_con, (char *)mybuf, sizeof(mybuf));
  8615.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,n)) {
  8616.           case SSL_ERROR_NONE:
  8617.                 if (n > 0)
  8618.           return(n);
  8619.                 if (n < 0)
  8620.           return(-2);
  8621.                 msleep(50);
  8622.                 break;
  8623.           case SSL_ERROR_WANT_WRITE:
  8624.           case SSL_ERROR_WANT_READ:
  8625.                 return(-1);
  8626.           case SSL_ERROR_SYSCALL:
  8627.         if (n != 0)
  8628.           return(-1);
  8629.           case SSL_ERROR_WANT_X509_LOOKUP:
  8630.           case SSL_ERROR_SSL:
  8631.           case SSL_ERROR_ZERO_RETURN:
  8632.           default:
  8633.                 ttclos(0);
  8634.                 return(-2);
  8635.             }
  8636.         }
  8637.     }
  8638. #endif /* CK_SSL */
  8639. #ifdef CK_KERBEROS
  8640. #ifdef KRB4
  8641. #ifdef RLOGCODE
  8642.     if (ttnproto == NP_EK4LOGIN) {
  8643.         if ((len = krb4_des_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8644.       return(-1);
  8645.         else
  8646.       return(len);
  8647.     }
  8648. #endif /* RLOGCODE */
  8649. #endif /* KRB4 */
  8650. #ifdef KRB5
  8651. #ifdef RLOGCODE
  8652.     if (ttnproto == NP_EK5LOGIN) {
  8653.         if ((len = krb5_des_read(ttyfd,mybuf,sizeof(mybuf),0)) < 0)
  8654.       return(-1);
  8655.         else
  8656.       return(len);
  8657.     }
  8658. #endif /* RLOGCODE */
  8659. #ifdef KRB5_U2U
  8660.     if (ttnproto == NP_K5U2U) {
  8661.         if ((len = krb5_u2u_read(ttyfd,mybuf,sizeof(mybuf))) < 0)
  8662.       return(-1);
  8663.         else
  8664.       return(len);
  8665.     }
  8666. #endif /* KRB5_U2U */
  8667. #endif /* KRB5 */
  8668. #endif /* CK_KERBEROS */
  8669.  
  8670. #ifdef NETCMD
  8671.     if (ttpipe)
  8672.       fd = fdin;
  8673.     else
  8674. #endif /* NETCMD */
  8675.       fd = ttyfd;
  8676.     x = read(fd, mybuf, 1);
  8677.     return(x > 0 ? x : -3);
  8678. }
  8679.  
  8680. #endif /* !FIONREAD */
  8681. #endif /* !aegis */
  8682. #endif /* !ATTSV */
  8683.  
  8684. #endif /* MYREAD */
  8685.  
  8686. #ifdef MINIX2
  8687. #undef MINIX
  8688. #endif /* MINIX2 */
  8689.  
  8690. /*  T T _ T N O P T  --  Handle Telnet negotions in incoming data */
  8691. /*
  8692.   Call with the IAC that was encountered.
  8693.   Returns:
  8694.    -3: If connection has dropped or gone bad.
  8695.    -2: On Telnet protocol error resulting in inconsistent states.
  8696.     0: If negotiation OK and caller has nothing to do.
  8697.     1: If packet start character has changed (new value is in global stchr).
  8698.   255: If there was a quoted IAC as data.
  8699.    or: Not at all if we got a legitimate Telnet Logout request.
  8700. */
  8701. #ifdef TCPSOCKET
  8702. static int
  8703. tt_tnopt(n) int n; {            /* Handle Telnet options */
  8704.     /* In case caller did not already check these conditions...  */
  8705.     if (n == IAC &&
  8706.     ((xlocal && netconn && IS_TELNET()) ||
  8707.      (!xlocal && sstelnet))) {
  8708.     extern int duplex;
  8709.     extern int server;
  8710.     int tx = 0;
  8711.     debug(F100,"ttinl calling tn_doop()","",0);
  8712.     tx = tn_doop((CHAR)(n & 0xff),duplex,ttinc);
  8713.     debug(F111,"ttinl tn_doop() returned","tx",tx);
  8714.     switch (tx) {
  8715.       case 0:
  8716.         return(0);
  8717.       case -1:            /* I/O error */
  8718.         ttimoff();            /* Turn off timer */
  8719.         return(-3);
  8720.           case -2:            /* Connection failed. */
  8721.           case -3:
  8722.         ttimoff();            /* Turn off timer */
  8723.         ttclos(0);
  8724.         return(-3);
  8725.       case 1:            /* ECHO change */
  8726.         duplex = 1;
  8727.         return(0);
  8728.       case 2:            /* ECHO change */
  8729.         duplex = 0;
  8730.         return(0);
  8731.       case 3:            /* Quoted IAC */
  8732.         n = 255;
  8733.         return((unsigned)255);
  8734. #ifdef IKS_OPTION
  8735.       case 4: {
  8736.           if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start && server
  8737. #ifdef IKSD
  8738.           && !inserver
  8739. #endif /* IKSD */
  8740.           ) {            /* Remote in Server mode */
  8741.           ttimoff();        /* Turn off timer */
  8742.           debug(F100,"u_start and !inserver","",0);
  8743.           return(-2);        /* End server mode */
  8744.           } else if (!TELOPT_SB(TELOPT_KERMIT).kermit.me_start &&
  8745.              server
  8746.              ) {        /* I'm no longer in Server Mode */
  8747.           debug(F100,"me_start and server","",0);
  8748.           ttimoff();
  8749.           return(-2);
  8750.           }
  8751.           return(0);
  8752.       }
  8753.       case 5: {            /* Start character change */
  8754.           /* extern CHAR stchr; */
  8755.           /* start = stchr; */
  8756.           return(1);
  8757.       }
  8758. #endif /* IKS_OPTION */
  8759.       case 6:            /* Remote Logout */
  8760.         ttimoff();
  8761.         ttclos(0);
  8762. #ifdef IKSD
  8763.         if (inserver && !local)
  8764.           doexit(GOOD_EXIT,0);
  8765.         else
  8766. #endif /* IKSD */
  8767.           return(-2);
  8768.       default:
  8769.         return(0);
  8770.     }
  8771.     } else
  8772.       return(0);
  8773. }
  8774. #endif /* TCPSOCKET */
  8775.  
  8776. /*  T T F L U I  --  Flush tty input buffer */
  8777.  
  8778. void
  8779. ttflux() {                /* But first... */
  8780. #ifdef MYREAD
  8781. /*
  8782.   Flush internal MYREAD buffer.
  8783. */
  8784. #ifdef TCPSOCKET
  8785.     int dotnopts, x;
  8786.     dotnopts = (((xlocal && netconn && IS_TELNET()) ||
  8787.          (!xlocal && sstelnet)));
  8788. #endif /* TCPSOCKET */
  8789.     debug(F101,"ttflux my_count","",my_count);
  8790. #ifdef TCPSOCKET
  8791.     if (dotnopts) {
  8792.     CHAR ch = '\0';
  8793.         while (my_count > 0) {
  8794.         ch = myread();
  8795. #ifdef CK_ENCRYPTION
  8796.             if (TELOPT_U(TELOPT_ENCRYPTION))
  8797.           ck_tn_decrypt(&ch,1);
  8798. #endif /* CK_ENCRYPTION */
  8799.             if (ch == IAC)
  8800.           x = tt_tnopt(ch);
  8801.         }
  8802.     } else
  8803. #endif /* TCPSOCKET */
  8804. #ifdef COMMENT
  8805. #ifdef CK_ENCRYPTION
  8806.     if (TELOPT_U(TELOPT_ENCRYPTION) && my_count > 0)
  8807.       ck_tn_decrypt(&mybuf[my_item+1],my_count);
  8808. #endif /* CK_ENCRYPTION */
  8809. #endif /* COMMENT */
  8810.     my_count = 0;            /* Reset count to zero */
  8811.     my_item = -1;            /* And buffer index to -1 */
  8812. #endif /* MYREAD */
  8813. }
  8814.  
  8815. int
  8816. ttflui() {
  8817.     int n, fd;
  8818. #ifdef TCPSOCKET
  8819.     int dotnopts;
  8820.     dotnopts = (((xlocal && netconn && IS_TELNET()) ||
  8821.          (!xlocal && sstelnet)));
  8822. #endif /* TCPSOCKET */
  8823.  
  8824. #ifdef NETCMD
  8825.     if (ttpipe)
  8826.       fd = fdin;
  8827.     else
  8828. #endif /* NETCMD */
  8829.       fd = ttyfd;
  8830.  
  8831. #ifdef TTLEBUF
  8832.     ttpush = -1;            /* Clear the peek-ahead char */
  8833.     while (le_data && (le_inbuf() > 0)) {
  8834.         CHAR ch = '\0';
  8835.         if (le_getchar(&ch) > 0) {    /* Clear any more... */
  8836.             debug(F101,"ttflui le_inbuf ch","",ch);
  8837.         }
  8838.     }
  8839. #endif /* TTLEBUF */
  8840.     debug(F101,"ttflui ttpipe","",ttpipe);
  8841.  
  8842. #ifdef MYREAD
  8843. /*
  8844.   Flush internal MYREAD buffer *NEXT*, in all cases.
  8845. */
  8846.     ttflux();
  8847. #endif /* MYREAD */
  8848.  
  8849. #ifdef NETCONN
  8850. /* Network flush is done specially, in the network support module. */
  8851.     if ((netconn || sstelnet) && !ttpipe && !ttpty) {
  8852.     debug(F100,"ttflui netflui","",0);
  8853. #ifdef COMMENT
  8854. #ifdef TN_COMPORT
  8855.     if (istncomport())
  8856.             tnc_send_purge_data(TNC_PURGE_RECEIVE);
  8857. #endif /* TN_COMPORT */
  8858. #endif /* COMMENT */
  8859.     return(netflui());
  8860.     }
  8861. #endif /* NETCONN */
  8862.  
  8863.     debug(F101,"ttflui ttyfd","",ttyfd); /* Not network */
  8864.     if (ttyfd < 0)
  8865.       return(-1);
  8866.  
  8867. #ifdef aegis
  8868.     sio_$control((short)yfd, sio_$flush_in, true, st);
  8869.     if (st.all != status_$ok) {
  8870.     fprintf(stderr, "flush failed: "); error_$print(st);
  8871.     } else {      /* sometimes the flush doesn't work */
  8872.         for (;;) {
  8873.         char buf[256];
  8874.             /* eat all the characters that shouldn't be available */
  8875.             ios_$get((short)fd, ios_$cond_opt, buf, 256L, st); /* (void) */
  8876.             if (st.all == ios_$get_conditional_failed) break;
  8877.             fprintf(stderr, "flush failed(2): "); error_$print(st);
  8878.         }
  8879.     }
  8880. #else
  8881. #ifdef BSD44                /* 4.4 BSD */
  8882.     n = FREAD;                          /* Specify read queue */
  8883.     debug(F100,"ttflui BSD44","",0);
  8884.     ioctl(fd,TIOCFLUSH,&n);
  8885. #else
  8886. #ifdef Plan9
  8887. #undef POSIX                /* Uh oh... */
  8888. #endif /* Plan9 */
  8889. #ifdef POSIX                /* POSIX */
  8890.     debug(F100,"ttflui POSIX","",0);
  8891.     tcflush(fd,TCIFLUSH);
  8892. #else
  8893. #ifdef ATTSV                /* System V */
  8894. #ifndef VXVE
  8895.     debug(F100,"ttflui ATTSV","",0);
  8896.     ioctl(fd,TCFLSH,0);
  8897. #endif /* VXVE */
  8898. #else                    /* Not BSD44, POSIX, or Sys V */
  8899. #ifdef TIOCFLUSH            /* Those with TIOCFLUSH defined */
  8900. #ifdef ANYBSD                /* Berkeley */
  8901.     n = FREAD;                          /* Specify read queue */
  8902.     debug(F100,"ttflui TIOCFLUSH ANYBSD","",0);
  8903.     ioctl(fd,TIOCFLUSH,&n);
  8904. #else                    /* Others (V7, etc) */
  8905.     debug(F100,"ttflui TIOCFLUSH","",0);
  8906.     ioctl(fd,TIOCFLUSH,0);
  8907. #endif /* ANYBSD */
  8908. #else                    /* All others... */
  8909. /*
  8910.   No system call (that we know about) for input buffer flushing.
  8911.   So see how many there are and read them in a loop, using ttinc().
  8912.   ttinc() is buffered, so we're not getting charged with a system call
  8913.   per character, just a function call.
  8914. */
  8915.     if ((n = ttchk()) > 0) {
  8916.     debug(F101,"ttflui read loop","",n);
  8917.     while ((n--) && ttinc(0) > 0) ;
  8918.     }
  8919. #endif /* TIOCFLUSH */
  8920. #endif /* ATTSV */
  8921. #endif /* POSIX */
  8922. #ifdef Plan9
  8923. #define POSIX
  8924. #endif /* Plan9 */
  8925. #endif /* BSD44 */
  8926. #endif /* aegis */
  8927.     return(0);
  8928. }
  8929.  
  8930. int
  8931. ttfluo() {                /* Flush output buffer */
  8932.     int fd;
  8933. #ifdef NETCMD
  8934.     if (ttpipe)
  8935.       fd = fdout;
  8936.     else
  8937. #endif /* NETCMD */
  8938.       fd = ttyfd;
  8939.  
  8940. #ifdef Plan9
  8941.     return 0;
  8942. #else
  8943. #ifdef POSIX
  8944.     return(tcflush(fd,TCOFLUSH));
  8945. #else
  8946. #ifdef OXOS
  8947.     return(ioctl(fd,TCFLSH,1));
  8948. #else
  8949.     return(0);                /* All others, nothing */
  8950. #endif /* OXOS */
  8951. #endif /* POSIX */
  8952. #endif /* Plan9 */
  8953. }
  8954.  
  8955. /* Interrupt Functions */
  8956.  
  8957. /* Set up terminal interrupts on console terminal */
  8958.  
  8959. #ifndef FIONREAD            /* We don't need esctrp() */
  8960. #ifndef SELECT                /* if we have any of these... */
  8961. #ifndef CK_POLL
  8962. #ifndef RDCHK
  8963.  
  8964. #ifndef OXOS
  8965. #ifdef SVORPOSIX
  8966. SIGTYP
  8967. esctrp(foo) int foo; {            /* trap console escapes (^\) */
  8968.     signal(SIGQUIT,SIG_IGN);            /* ignore until trapped */
  8969.     conesc = 1;
  8970.     debug(F101,"esctrp caught SIGQUIT","",conesc);
  8971. }
  8972. #endif /* SVORPOSIX */
  8973. #endif /* OXOS */
  8974.  
  8975. #ifdef V7
  8976. #ifndef MINIX2
  8977. SIGTYP
  8978. esctrp(foo) int foo; {            /* trap console escapes (^\) */
  8979.     signal(SIGQUIT,SIG_IGN);            /* ignore until trapped */
  8980.     conesc = 1;
  8981.     debug(F101,"esctrp caught SIGQUIT","",conesc);
  8982. }
  8983. #endif /* MINIX2 */
  8984. #endif /* V7 */
  8985.  
  8986. #ifdef C70
  8987. SIGTYP
  8988. esctrp(foo) int foo; {            /* trap console escapes (^\) */
  8989.     conesc = 1;
  8990.     signal(SIGQUIT,SIG_IGN);            /* ignore until trapped */
  8991. }
  8992. #endif /* C70 */
  8993.  
  8994. #endif /* RDCHK */
  8995. #endif /* CK_POLL */
  8996. #endif /* SELECT */
  8997. #endif /* FIONREAD */
  8998.  
  8999. /*  C O N B G T  --  Background Test  */
  9000.  
  9001. static int jc = 0;            /* 0 = no job control */
  9002.  
  9003. /*
  9004.   Call with flag == 1 to prevent signal test, which can not be expected
  9005.   to work during file transfer, when SIGINT probably *is* set to SIG_IGN.
  9006.  
  9007.   Call with flag == 0 to use the signal test, but only if the process-group
  9008.   test fails, as it does on some UNIX systems, where getpgrp() is buggy,
  9009.   requires an argument when the man page says it doesn't, or vice versa.
  9010.  
  9011.   If flag == 0 and the process-group test fails, then we determine background
  9012.   status simply (but not necessarily reliably) from isatty().
  9013.  
  9014.   conbgt() sets the global backgrd = 1 if we appear to be in the background,
  9015.   and to 0 if we seem to be in the foreground.  conbgt() is highly prone to
  9016.   misbehavior.
  9017. */
  9018. VOID
  9019. conbgt(flag) int flag; {
  9020.     int x = -1,                /* process group or SIGINT test */
  9021.         y = 0;                /* isatty() test */
  9022. /*
  9023.   Check for background operation, even if not running on real tty, so that
  9024.   background flag can be set correctly.  If background status is detected,
  9025.   then Kermit will not issue its interactive prompt or most messages.
  9026.   If your prompt goes away, you can blame (and fix?) this function.
  9027. */
  9028.  
  9029. /* Use process-group test if possible. */
  9030.  
  9031. #ifdef POSIX                /* We can do it in POSIX */
  9032. #define PGROUP_T
  9033. #else
  9034. #ifdef BSD4                /* and in BSD 4.x. */
  9035. #define PGROUP_T
  9036. #else
  9037. #ifdef HPUXJOBCTL            /* and in most HP-UX's */
  9038. #define PGROUP_T
  9039. #else
  9040. #ifdef TIOCGPGRP            /* and anyplace that has this ioctl. */
  9041. #define PGROUP_T
  9042. #endif /* TIOCGPGRP */
  9043. #endif /* HPUXJOBCTL */
  9044. #endif /* BSD4 */
  9045. #endif /* POSIX */
  9046.  
  9047. #ifdef MIPS                /* Except if it doesn't work... */
  9048. #undef PGROUP_T
  9049. #endif /* MIPS */
  9050.  
  9051. #ifdef PGROUP_T
  9052. /*
  9053.   Semi-reliable process-group test.  Check whether this process's group is
  9054.   the same as the controlling terminal's process group.  This works if the
  9055.   getpgrp() call doesn't lie (as it does in the SUNOS System V environment).
  9056. */
  9057.     PID_T mypgrp = (PID_T)0;        /* Kermit's process group */
  9058.     PID_T ctpgrp = (PID_T)0;        /* The terminal's process group */
  9059. #ifndef _POSIX_SOURCE
  9060. /*
  9061.   The getpgrp() prototype is obtained from system header files for POSIX
  9062.   and Sys V R4 compilations.  Other systems, who knows.  Some complain about
  9063.   a duplicate declaration here, others don't, so it's safer to leave it in
  9064.   if we don't know for certain.
  9065. */
  9066. #ifndef SVR4
  9067. #ifndef PS2AIX10
  9068. #ifndef HPUX9
  9069.     extern PID_T getpgrp();
  9070. #endif /* HPUX9 */
  9071. #endif /* PS2AIX10 */
  9072. #endif /* SVR4 */
  9073. #endif /* _POSIX_SOURCE */
  9074.  
  9075. /* Get my process group. */
  9076.  
  9077. #ifdef SVR3 /* Maybe this should be ATTSV? */
  9078. /* This function is not described in SVID R2 */
  9079.     mypgrp = getpgrp();
  9080.     /* debug(F101,"ATTSV conbgt process group","",(int) mypgrp); */
  9081. #else
  9082. #ifdef POSIX
  9083.     mypgrp = getpgrp();
  9084.     /* debug(F101,"POSIX conbgt process group","",(int) mypgrp); */
  9085. #else
  9086. #ifdef OSFPC
  9087.     mypgrp = getpgrp();
  9088.     /* debug(F101,"OSF conbgt process group","",(int) mypgrp); */
  9089. #else
  9090. #ifdef QNX
  9091.     mypgrp = getpgrp();
  9092.     /* debug(F101,"QNX conbgt process group","",(int) mypgrp); */
  9093. #else
  9094. #ifdef OSF32                /* (was OSF40) */
  9095.     mypgrp = getpgrp();
  9096.     /* debug(F101,"Digital UNIX conbgt process group","",(int) mypgrp); */
  9097. #else /* BSD, V7, etc */
  9098. #ifdef MINIX2
  9099.     mypgrp = getpgrp();
  9100. #else
  9101.     mypgrp = getpgrp(0);
  9102. #endif /* MINIX2 */
  9103.     /* debug(F101,"BSD conbgt process group","",(int) mypgrp); */
  9104. #endif /* OSF32 */
  9105. #endif /* QNX */
  9106. #endif /* OSFPC */
  9107. #endif /* POSIX */
  9108. #endif /* SVR3 */
  9109.  
  9110. #ifdef MINIX2
  9111. #undef BSD44ORPOSIX
  9112. #endif /* MINIX2 */
  9113.  
  9114. /* Now get controlling tty's process group */
  9115. #ifdef BSD44ORPOSIX
  9116.     ctpgrp = tcgetpgrp(1);        /* The POSIX way */
  9117.     /* debug(F101,"POSIX conbgt terminal process group","",(int) ctpgrp); */
  9118. #else
  9119.     ioctl(1, TIOCGPGRP, &ctpgrp);    /* Or the BSD way */
  9120.    /* debug(F101,"non-POSIX conbgt terminal process group","",(int) ctpgrp); */
  9121. #endif /* BSD44ORPOSIX */
  9122.  
  9123. #ifdef MINIX2
  9124. #define BSD44ORPOSIX
  9125. #endif /* MINIX2 */
  9126.  
  9127.     if ((mypgrp > (PID_T) 0) && (ctpgrp > (PID_T) 0))
  9128.       x = (mypgrp == ctpgrp) ? 0 : 1;    /* If they differ, then background. */
  9129.     else x = -1;            /* If error, remember. */
  9130.     debug(F101,"conbgt process group test","",x);
  9131. #endif /* PGROUP_T */
  9132.  
  9133. /* Try to see if job control is available */
  9134.  
  9135. #ifdef NOJC                /* User override */
  9136.     jc = 0;                /* No job control allowed */
  9137.     debug(F111,"NOJC","jc",jc);
  9138. #else
  9139. #ifdef BSD44
  9140.     jc = 1;
  9141. #else
  9142. #ifdef SVR4ORPOSIX            /* POSIX actually tells us */
  9143.     debug(F100,"SVR4ORPOSIX jc test...","",0);
  9144. #ifdef _SC_JOB_CONTROL
  9145. #ifdef __bsdi__
  9146.     jc = 1;
  9147. #else
  9148. #ifdef __386BSD__
  9149.     jc = 1;
  9150. #else
  9151.     jc = sysconf(_SC_JOB_CONTROL);    /* Whatever system says */
  9152.     if (jc < 0) {
  9153.     debug(F101,"sysconf fails, jcshell","",jcshell);
  9154.     jc = (jchdlr == SIG_DFL) ? 1 : 0;
  9155.     } else
  9156.       debug(F111,"sysconf(_SC_JOB_CONTROL)","jc",jc);
  9157. #endif /* __386BSD__ */
  9158. #endif /* __bsdi__ */
  9159. #else
  9160. #ifdef _POSIX_JOB_CONTROL
  9161.     jc = 1;                /* By definition */
  9162.     debug(F111,"_POSIX_JOB_CONTROL is defined","jc",jc);
  9163. #else
  9164.     jc = 0;                /* Assume job control not allowed */
  9165.     debug(F111,"SVR4ORPOSIX _SC/POSIX_JOB_CONTROL not defined","jc",jc);
  9166. #endif /* _POSIX_JOB_CONTROL */
  9167. #endif /* _SC_JOB_CONTROL */
  9168. #else
  9169. #ifdef BSD4
  9170.     jc = 1;                /* Job control allowed */
  9171.     debug(F111,"BSD job control","jc",jc);
  9172. #else
  9173. #ifdef SVR3JC
  9174.     jc = 1;                /* JC allowed */
  9175.     debug(F111,"SVR3 job control","jc",jc);
  9176. #else
  9177. #ifdef OXOS
  9178.     jc = 1;                /* JC allowed */
  9179.     debug(F111,"X/OS job control","jc",jc);
  9180. #else
  9181. #ifdef HPUX9
  9182.     jc = 1;                /* JC allowed */
  9183.     debug(F111,"HP-UX 9.0 job control","jc",jc);
  9184. #else
  9185. #ifdef HPUX10
  9186.     jc = 1;                /* JC allowed */
  9187.     debug(F111,"HP-UX 10.0 job control","jc",jc);
  9188. #else
  9189.     jc = 0;                /* JC not allowed */
  9190.     debug(F111,"job control catch-all","jc",jc);
  9191. #endif /* HPUX10 */
  9192. #endif /* HPUX9 */
  9193. #endif /* OXOS */
  9194. #endif /* SVR3JC */
  9195. #endif /* BSD4 */
  9196. #endif /* SVR4ORPOSIX */
  9197. #endif /* BSD44 */
  9198. #endif /* NOJC */
  9199.     debug(F101,"conbgt jc","",jc);
  9200. #ifndef NOJC
  9201.     debug(F101,"conbgt jcshell","",jcshell);
  9202. /*
  9203.   At this point, if jc == 1 but jcshell == 0, it means that the OS supports
  9204.   job control, but the shell or other process we are running under does not
  9205.   (jcshell is set in sysinit()) and so if we suspend ourselves, nothing good
  9206.   will come of it.  So...
  9207. */
  9208.     if (jc < 0) jc = 0;
  9209.     if (jc > 0 && jcshell == 0) jc = 0;
  9210. #endif /* NOJC */
  9211.  
  9212. /*
  9213.   Another background test.
  9214.   Test if SIGINT (terminal interrupt) is set to SIG_IGN (ignore),
  9215.   which is done by the shell (sh) if the program is started with '&'.
  9216.   Unfortunately, this is NOT done by csh or ksh so watch out!
  9217.   Note, it's safe to set SIGINT to SIG_IGN here, because further down
  9218.   we always set it to something else.
  9219.   Note: as of 16 Jul 1999, we also skip this test if we set SIGINT to
  9220.   SIG_IGN ourselves.
  9221. */
  9222.     if (x < 0 && !flag && !sigint_ign) { /* Didn't get good results above... */
  9223.  
  9224.     SIGTYP (*osigint)();
  9225.  
  9226.     osigint = signal(SIGINT,SIG_IGN);    /* What is SIGINT set to? */
  9227.     sigint_ign = 1;
  9228.     x = (osigint == SIG_IGN) ? 1 : 0;    /* SIG_IGN? */
  9229.     debug(F101,"conbgt osigint","",osigint);
  9230.     debug(F101,"conbgt signal test","",x);
  9231.     }
  9232.  
  9233. /* Also check to see if we're running with redirected stdio. */
  9234. /* This is not really background operation, but we want to act as though */
  9235. /* it were. */
  9236.  
  9237. #ifdef IKSD
  9238.     if (inserver) {            /* Internet Kermit Server */
  9239.     backgrd = 0;            /* is not in the background */
  9240.     return;
  9241.     }
  9242. #endif /* IKSD */
  9243.  
  9244.     y = (isatty(0) && isatty(1)) ? 1 : 0;
  9245.     debug(F101,"conbgt isatty test","",y);
  9246.  
  9247. #ifdef BSD29
  9248. /* The process group and/or signal test doesn't work under these... */
  9249.     backgrd = !y;
  9250. #else
  9251. #ifdef sxaE50
  9252.     backgrd = !y;
  9253. #else
  9254. #ifdef MINIX
  9255.     backgrd = !y;
  9256. #else
  9257. #ifdef MINIX2
  9258.     backgrd = !y;
  9259. #else
  9260.     if (x > -1)
  9261.       backgrd = (x || !y) ? 1 : 0;
  9262.     else backgrd = !y;
  9263. #endif /* BSD29 */
  9264. #endif /* sxaE50 */
  9265. #endif /* MINIX */
  9266. #endif /* MINIX2 */
  9267.     debug(F101,"conbgt backgrd","",backgrd);
  9268. }
  9269.  
  9270. /*  C O N I N T  --  Console Interrupt setter  */
  9271.  
  9272. /*
  9273.   First arg is pointer to function to handle SIGTERM & SIGINT (like Ctrl-C).
  9274.   Second arg is pointer to function to handle SIGTSTP (suspend).
  9275. */
  9276.  
  9277. VOID                    /* Set terminal interrupt traps. */
  9278. #ifdef CK_ANSIC
  9279. #ifdef apollo
  9280. conint(f,s) SIGTYP (*f)(), (*s)();
  9281. #else
  9282. conint(SIGTYP (*f)(int), SIGTYP (*s)(int))
  9283. #endif /* apollo */
  9284. #else
  9285. conint(f,s) SIGTYP (*f)(), (*s)();
  9286. #endif /* CK_ANSIC */
  9287. /* conint */ {
  9288.  
  9289.     debug(F101,"conint conistate","",conistate);
  9290.  
  9291.     conbgt(0);                /* Do background test. */
  9292.  
  9293. /* Set the desired handlers for hangup and software termination. */
  9294.  
  9295. #ifdef SIGTERM
  9296.     signal(SIGTERM,f);                  /* Software termination */
  9297. #endif /* SIGTERM */
  9298.  
  9299. /*
  9300.   Prior to July 1999 we used to call sighup() here but now it's called in
  9301.   sysinit() so SIGHUP can be caught during execution of the init file or
  9302.   a kerbang script.
  9303. */
  9304.  
  9305. /* Now handle keyboard stop, quit, and interrupt signals. */
  9306. /* Check if invoked in background -- if so signals set to be ignored. */
  9307. /* However, if running under a job control shell, don't ignore them. */
  9308. /* We won't be getting any, as we aren't in the terminal's process group. */
  9309.  
  9310.     debug(F101,"conint backgrd","",backgrd);
  9311.     debug(F101,"conint jc","",jc);
  9312.  
  9313.     if (backgrd && !jc) {        /* In background, ignore signals */
  9314.     debug(F101,"conint background ignoring signals, jc","",jc);
  9315. #ifdef SIGTSTP
  9316.         signal(SIGTSTP,SIG_IGN);        /* Keyboard stop */
  9317. #endif /* SIGTSTP */
  9318.         signal(SIGQUIT,SIG_IGN);        /* Keyboard quit */
  9319.         signal(SIGINT,SIG_IGN);         /* Keyboard interrupt */
  9320.     sigint_ign = 1;
  9321.     conistate = CONI_NOI;
  9322.     } else {                /* Else in foreground or suspended */
  9323.     debug(F101,"conint foreground catching signals, jc","",jc);
  9324.         signal(SIGINT,f);               /* Catch terminal interrupt */
  9325.     sigint_ign = (f == SIG_IGN) ? 1 : 0;
  9326.  
  9327. #ifdef SIGTSTP                /* Keyboard stop (suspend) */
  9328.     /* debug(F101,"conint SIGSTSTP","",s); */
  9329.     if (s == NULL) s = SIG_DFL;
  9330. #ifdef NOJC                /* No job control allowed. */
  9331.     signal(SIGTSTP,SIG_IGN);
  9332. #else                    /* Job control allowed */
  9333.     if (jc)                /* if available. */
  9334.       signal(SIGTSTP,s);
  9335.     else
  9336.       signal(SIGTSTP,SIG_IGN);
  9337. #endif /* NOJC */
  9338. #endif /* SIGTSTP */
  9339.  
  9340. #ifndef OXOS
  9341. #ifdef SVORPOSIX
  9342. #ifndef FIONREAD            /* Watch out, we don't know this... */
  9343. #ifndef SELECT
  9344. #ifndef CK_POLL
  9345. #ifndef RDCHK
  9346.         signal(SIGQUIT,esctrp);         /* Quit signal, Sys III/V. */
  9347. #endif /* RDCHK */
  9348. #endif /* CK_POLL */
  9349. #endif /* SELECT */
  9350. #endif /* FIONREAD */
  9351.         if (conesc) conesc = 0;         /* Clear out pending escapes */
  9352. #else
  9353. #ifdef V7
  9354.         signal(SIGQUIT,esctrp);         /* V7 like Sys III/V */
  9355.         if (conesc) conesc = 0;
  9356. #else
  9357. #ifdef aegis
  9358.         signal(SIGQUIT,f);              /* Apollo, catch it like others. */
  9359. #else
  9360.         signal(SIGQUIT,SIG_IGN);        /* Others, ignore like 4D & earlier. */
  9361. #endif /* aegis */
  9362. #endif /* V7 */
  9363. #endif /* SVORPOSIX */
  9364. #endif /* OXOS */
  9365.     conistate = CONI_INT;
  9366.     }
  9367. }
  9368.  
  9369.  
  9370. /*  C O N N O I  --  Reset console terminal interrupts */
  9371.  
  9372. VOID
  9373. connoi() {                              /* Console-no-interrupts */
  9374.  
  9375.     debug(F101,"connoi conistate","",conistate);
  9376. #ifdef SIGTSTP
  9377.     signal(SIGTSTP,SIG_IGN);        /* Suspend */
  9378. #endif /* SIGTSTP */
  9379.     conint(SIG_IGN,SIG_IGN);        /* Interrupt */
  9380.     sigint_ign = 1;            /* Remember we did this ourselves */
  9381. #ifdef SIGQUIT
  9382.     signal(SIGQUIT,SIG_IGN);        /* Quit */
  9383. #endif /* SIGQUIT */
  9384. #ifdef SIGTERM
  9385.     signal(SIGTERM,SIG_IGN);        /* Term */
  9386. #endif /* SIGTERM */
  9387.     conistate = CONI_NOI;
  9388. }
  9389.  
  9390. /*  I N I T R A W Q  --  Set up to read /dev/kmem for character count.  */
  9391.  
  9392. #ifdef  V7
  9393. /*
  9394.  Used in Version 7 to simulate Berkeley's FIONREAD ioctl call.  This
  9395.  eliminates blocking on a read, because we can read /dev/kmem to get the
  9396.  number of characters available for raw input.  If your system can't
  9397.  or you won't let the world read /dev/kmem then you must figure out a
  9398.  different way to do the counting of characters available, or else replace
  9399.  this by a dummy function that always returns 0.
  9400. */
  9401. /*
  9402.  * Call this routine as: initrawq(tty)
  9403.  * where tty is the file descriptor of a terminal.  It will return
  9404.  * (as a char *) the kernel-mode memory address of the rawq character
  9405.  * count, which may then be read.  It has the side-effect of flushing
  9406.  * input on the terminal.
  9407.  */
  9408. /*
  9409.  * John Mackin, Physiology Dept., University of Sydney (Australia)
  9410.  * ...!decvax!mulga!physiol.su.oz!john
  9411.  *
  9412.  * Permission is hereby granted to do anything with this code, as
  9413.  * long as this comment is retained unmodified and no commercial
  9414.  * advantage is gained.
  9415.  */
  9416. #ifndef MINIX
  9417. #ifndef MINIX2
  9418. #ifndef COHERENT
  9419. #include <a.out.h>
  9420. #include <sys/proc.h>
  9421. #endif /* COHERENT */
  9422. #endif /* MINIX2 */
  9423. #endif /* MINIX */
  9424.  
  9425. #ifdef COHERENT
  9426. #include <l.out.h>
  9427. #include <sys/proc.h>
  9428. #endif /* COHERENT */
  9429.  
  9430. char *
  9431. initrawq(tty) int tty; {
  9432. #ifdef MINIX
  9433.     return(0);
  9434. #else
  9435. #ifdef MINIX2
  9436.     return(0);
  9437. #else
  9438. #ifdef UTS24
  9439.     return(0);
  9440. #else
  9441. #ifdef BSD29
  9442.     return(0);
  9443. #else
  9444.     long lseek();
  9445.     static struct nlist nl[] = {
  9446.         {PROCNAME},
  9447.         {NPROCNAME},
  9448.         {""}
  9449.     };
  9450.     static struct proc *pp;
  9451.     char *qaddr, *p, c;
  9452.     int m;
  9453.     PID_T pid, me;
  9454.     NPTYPE xproc;                       /* Its type is defined in makefile. */
  9455.     int catch();
  9456.  
  9457.     me = getpid();
  9458.     if ((m = open("/dev/kmem", 0)) < 0) err("kmem");
  9459.     nlist(BOOTNAME, nl);
  9460.     if (nl[0].n_type == 0) err("proc array");
  9461.  
  9462.     if (nl[1].n_type == 0) err("nproc");
  9463.  
  9464.     lseek(m, (long)(nl[1].n_value), 0);
  9465.     read (m, &xproc, sizeof(xproc));
  9466.     saval = signal(SIGALRM, catch);
  9467.     if ((pid = fork()) == 0) {
  9468.         while(1)
  9469.             read(tty, &c, 1);
  9470.     }
  9471.     alarm(2);
  9472.  
  9473.     if(setjmp(jjbuf) == 0) {
  9474.         while(1)
  9475.       read(tty, &c, 1);
  9476.     }
  9477.     signal(SIGALRM, SIG_DFL);
  9478.  
  9479. #ifdef DIRECT
  9480.     pp = (struct proc *) nl[0].n_value;
  9481. #else
  9482.     if (lseek(m, (long)(nl[0].n_value), 0) < 0L) err("seek");
  9483.     if (read(m, &pp, sizeof(pp)) != sizeof(pp))  err("no read of proc ptr");
  9484. #endif
  9485.     lseek(m, (long)(nl[1].n_value), 0);
  9486.     read(m, &xproc, sizeof(xproc));
  9487.  
  9488.     if (lseek(m, (long)pp, 0) < 0L) err("Can't seek to proc");
  9489.     if ((p = malloc(xproc * sizeof(struct proc))) == NULL) err("malloc");
  9490.     if (read(m,p,xproc * sizeof(struct proc)) != xproc*sizeof(struct proc))
  9491.         err("read proc table");
  9492.     for (pp = (struct proc *)p; xproc > 0; --xproc, ++pp) {
  9493.         if (pp -> p_pid == (short) pid) goto iout;
  9494.     }
  9495.     err("no such proc");
  9496.  
  9497. iout:
  9498.     close(m);
  9499.     qaddr = (char *)(pp -> p_wchan);
  9500.     free (p);
  9501.     kill(pid, SIGKILL);
  9502.     wait((WAIT_T *)0);
  9503.     return (qaddr);
  9504. #endif
  9505. #endif
  9506. #endif
  9507. #endif
  9508. }
  9509.  
  9510. /*  More V7-support functions...  */
  9511.  
  9512. static VOID
  9513. err(s) char *s; {
  9514.     char buf[200];
  9515.  
  9516.     ckmakmsg(buf,200,"fatal error in initrawq: ", s, NULL, NULL);
  9517.     perror(buf);
  9518.     doexit(1,-1);
  9519. }
  9520.  
  9521. static VOID
  9522. catch(foo) int foo; {
  9523.     longjmp(jjbuf, -1);
  9524. }
  9525.  
  9526.  
  9527. /*  G E N B R K  --  Simulate a modem break.  */
  9528.  
  9529. #ifdef MINIX
  9530. #define BSPEED B110
  9531. #else
  9532. #ifdef MINIX2
  9533. #define BSPEED B110
  9534. #else
  9535. #define BSPEED B150
  9536. #endif /* MINIX2 */
  9537. #endif /* MINIX */
  9538.  
  9539. #ifndef MINIX2
  9540. VOID
  9541. genbrk(fn,msec) int fn, msec; {
  9542.     struct sgttyb ttbuf;
  9543.     int ret, sospeed, x, y;
  9544.  
  9545.     ret = ioctl(fn, TIOCGETP, &ttbuf);
  9546.     sospeed = ttbuf.sg_ospeed;
  9547.     ttbuf.sg_ospeed = BSPEED;
  9548.     ret = ioctl(fn, TIOCSETP, &ttbuf);
  9549.     y = (int)strlen(brnuls);
  9550.     x = ( BSPEED * 100 ) / msec;
  9551.     if (x > y) x = y;
  9552.     ret = write(fn, brnuls, (( BSPEED * 100 ) / msec ));
  9553.     ttbuf.sg_ospeed = sospeed;
  9554.     ret = ioctl(fn, TIOCSETP, &ttbuf);
  9555.     ret = write(fn, "@", 1);
  9556.     return;
  9557. }
  9558. #endif /* MINIX2 */
  9559.  
  9560. #ifdef MINIX2
  9561. int
  9562. genbrk(fn,msec) int fn, msec; {
  9563.     struct termios ttbuf;
  9564.     int ret, x, y;
  9565.     speed_t sospeed;
  9566.  
  9567.     ret = tcgetattr(fn, &ttbuf);
  9568.     sospeed = ttbuf.c_ospeed;
  9569.     ttbuf.c_ospeed = BSPEED;
  9570.     ret = tcsetattr(fn,TCSADRAIN, &ttbuf);
  9571.     y = (int)strlen(brnuls);
  9572.     x = ( BSPEED * 100 ) / msec;
  9573.     if (x > y) x = y;
  9574.     ret = write(fn, brnuls, (( BSPEED * 100 ) / msec ));
  9575.     ttbuf.c_ospeed = sospeed;
  9576.     ret = tcsetattr(fn, TCSADRAIN, &ttbuf);
  9577.     ret = write(fn, "@", 1);
  9578.     return ret;
  9579. }
  9580. #endif /* MINIX2 */
  9581. #endif /* V7 */
  9582.  
  9583. /*
  9584.   I N C H K  --  Check if chars waiting to be read on given file descriptor.
  9585.  
  9586.   This routine is a merger of ttchk() and conchk().
  9587.   Call with:
  9588.     channel == 0 to check console.
  9589.     channel == 1 to check communications connection.
  9590.   and:
  9591.     fd = file descriptor.
  9592.   Returns:
  9593.    >= 0: number of characters waiting, 0 or greater,
  9594.      -1: on any kind of error,
  9595.      -2: if there is (definitely) no connection.
  9596.   Note: In UNIX we don't have to call nettchk() because a socket
  9597.   file descriptor works just like in serial i/o, ioctls and all.
  9598.   (But this will change if we add non-file-descriptor channels,
  9599.   such as IBM X.25 for AIX...)
  9600. */
  9601. static int
  9602. in_chk(channel, fd) int channel, fd; {
  9603.     int x, n = 0;            /* Workers, n = return value */
  9604.     extern int clsondisc;        /* Close on disconnect */
  9605. /*
  9606.   The first section checks to make sure we have a connection,
  9607.   but only if we're in local mode.
  9608. */
  9609. #ifdef DEBUG
  9610.     if (deblog) {
  9611.     debug(F111,"in_chk entry",ckitoa(fd),channel);
  9612.     debug(F101,"in_chk ttyfd","",ttyfd);
  9613.     debug(F101,"in_chk ttpty","",ttpty);
  9614.     }
  9615. #endif /* DEBUG */
  9616. /*
  9617.   But don't say connection is gone if we have any buffered-stuff.
  9618. */
  9619. #ifdef TTLEBUF
  9620.     debug(F101,"in_chk ttpush","",ttpush);
  9621.     if (channel == 1) {
  9622.     if (ttpush >= 0)
  9623.       n++;
  9624.     n += le_inbuf();
  9625.     if (n > 0)
  9626.       return(n);
  9627.     }
  9628. #endif /* TTLEBUF */
  9629.  
  9630. #ifdef NETPTY
  9631. #ifdef HAVE_PTYTRAP
  9632.     /* Special handling for HP-UX pty i/o */
  9633.     if (ttpty && pty_trap_pending(ttyfd) > 0) {
  9634.         if (pty_trap_handler(ttyfd) > 0) {
  9635.             ttclos(0);
  9636.             return(-2);
  9637.         }
  9638.     }
  9639. #endif /* HAVE_PTYTRAP */
  9640. #endif /* NETPTY */
  9641.  
  9642.     if (channel) {            /* Checking communications channel */
  9643.     if (ttyfd < 0) {        /* No connection */
  9644.       return(-2);            /* That's what this means */
  9645.     } else if (xlocal &&        /* In local mode */
  9646.            (!netconn        /* Serial connection or */
  9647. #ifdef TN_COMPORT
  9648.             || istncomport()    /* Telnet Com Port */
  9649. #endif /* TN_COMPORT */
  9650.            ) && ttcarr != CAR_OFF /* with CARRIER WATCH ON (or AUTO) */
  9651. #ifdef COMMENT
  9652. #ifdef MYREAD
  9653. /*
  9654.   Seems like this would be a good idea but it prevents C-Kermit from
  9655.   popping back to the prompt automatically when carrier drops.  However,
  9656.   commenting this out prevents us from seeing the NO CARRIER message.
  9657.   Needs more work...
  9658. */
  9659.            && my_count < 1    /* Nothing in our internal buffer */
  9660. #endif /* MYREAD */
  9661. #endif /* COMMENT */
  9662.            ) {
  9663.         int x;
  9664.         x = ttgmdm();        /* So get modem signals */
  9665.         debug(F101,"in_chk close-on-disconnect","",clsondisc);
  9666.         if (x > -1) {        /* Check for carrier */
  9667.         if (!(x & BM_DCD)) {    /* No carrier */
  9668.             debug(F101,"in_chk carrier lost","",x);
  9669.             if (clsondisc)    /* If "close-on-disconnect" */
  9670.               ttclos(0);    /* close device & release lock. */
  9671.             return(-2);        /* This means "disconnected" */
  9672.         }
  9673.         /* In case I/O to device after CD dropped always fails */
  9674.         /* as in Debian Linux 2.1 and Unixware 2.1... */
  9675.         } else {
  9676.             debug(F101,"in_chk ttgmdm I/O error","",errno);
  9677.             debug(F101,"in_chk ttgmdm gotsigs","",gotsigs);
  9678.             if (gotsigs) {        /* If we got signals before... */
  9679.             if (errno == 5 || errno == 6) { /* I/O error etc */
  9680.                 if (clsondisc)    /* like when modem hangs up */
  9681.               ttclos(0);
  9682.             return(-2);
  9683.             }
  9684.         }
  9685.         /* If we never got modem signals successfully on this */
  9686.         /* connection before, we can't conclude that THIS failure */
  9687.         /* means the connection was lost. */
  9688.         return(0);
  9689.         }
  9690.     }
  9691.     }
  9692.  
  9693. /* We seem to have a connection so now see if any bytes are waiting on it */
  9694.  
  9695. #ifdef CK_SSL
  9696.     if (ssl_active_flag || tls_active_flag) {
  9697.         n += SSL_pending(ssl_active_flag?ssl_con:tls_con);
  9698.         debug(F101,"in_chk SSL_pending","",n);
  9699.         if (n < 0) {
  9700.             ttclos(0);
  9701.             return(-1);
  9702.         } else if (n > 0) {
  9703.             return(n);
  9704.         }
  9705.     }
  9706. #endif /* CK_SSL */
  9707. #ifdef RLOGCODE
  9708. #ifdef CK_KERBEROS
  9709.     /* It is not safe to read any data when using encrypted Klogin */
  9710.     if (ttnproto == NP_EK4LOGIN || ttnproto == NP_EK5LOGIN) {
  9711. #ifdef KRB4
  9712.         if (ttnproto == NP_EK4LOGIN) {
  9713.             n += krb4_des_avail(ttyfd);
  9714.             debug(F101,"in_chk krb4_des_avail","",n);
  9715.         }
  9716. #endif /* KRB4 */
  9717. #ifdef KRB5
  9718.         if (ttnproto == NP_EK5LOGIN) {
  9719.             n += krb5_des_avail(ttyfd);
  9720.             debug(F101,"in_chk krb5_des_avail","",n);
  9721.         }
  9722. #ifdef KRB5_U2U
  9723.         if (ttnproto == NP_K5U2U) {
  9724.             n += krb5_u2u_avail(ttyfd);
  9725.             debug(F101,"in_chk krb5_des_avail","",n);
  9726.         }
  9727. #endif /* KRB5_U2U */
  9728. #endif /* KRB5 */
  9729.         if (n < 0)            /* Is this right? */
  9730.       return(-1);
  9731.         else
  9732.       return(n);
  9733.     }
  9734. #endif /* CK_KERBEROS */
  9735. #endif /* RLOGCODE */
  9736.  
  9737.     errno = 0;                /* Reset this so we log good info */
  9738. #ifdef FIONREAD
  9739.     x = ioctl(fd, FIONREAD, &n);    /* BSD and lots of others */
  9740. #ifdef DEBUG                /* (the more the better) */
  9741.     if (deblog) {
  9742.     debug(F101,"in_chk FIONREAD return code","",x);
  9743.     debug(F101,"in_chk FIONREAD count","",n);
  9744.     debug(F101,"in_chk FIONREAD errno","",errno);
  9745.     }
  9746. #endif /* DEBUG */
  9747. #else /* FIONREAD not defined */
  9748. /*
  9749.   Here, if (netconn && ttnet == NET_TCPB), we might try calling recvmsg()
  9750.   with flags MSG_PEEK|MSG_DONTWAIT on the socket (ttyfd), except this is not
  9751.   portable (MSG_DONTWAIT isn't defined in any of the <sys/socket.h> files
  9752.   that I looked at, but it is needed to prevent the call from blocking), and
  9753.   the msghdr struct differs from place to place, so we would need another
  9754.   avalanche of ifdefs.  Still, when FIONREAD is not available, this is the
  9755.   only other known method of asking the OS for the *number* of characters
  9756.   available for reading.
  9757. */
  9758. #ifdef V7                /* UNIX V7: look in kernel memory */
  9759. #ifdef MINIX
  9760.     n = 0;                /* But not in MINIX */
  9761. #else
  9762. #ifdef MINIX2
  9763.     n = 0;
  9764. #else
  9765.     lseek(kmem[TTY], (long) qaddr[TTY], 0); /* 7th Edition Unix */
  9766.     x = read(kmem[TTY], &n, sizeof(int));
  9767.     if (x != sizeof(int))
  9768.       n = 0;
  9769. #endif /* MINIX2 */
  9770. #endif /* MINIX */
  9771. #else /* Not V7 */
  9772. #ifdef PROVX1
  9773.     x = ioctl(fd, TIOCQCNT, &ttbuf);    /* DEC Pro/3xx Venix V.1 */
  9774.     n = ttbuf.sg_ispeed & 0377;        /* Circa 1984 */
  9775.     if (x < 0) n = 0;
  9776. #else
  9777. #ifdef MYREAD
  9778. /*
  9779.   Here we skip all the undependable and expensive calls below if we
  9780.   already have something in our internal buffer.  This tends to work quite
  9781.   nicely, so the only really bad case remaining is the one in which neither
  9782.   FIONREAD or MYREAD are defined, which is increasingly rare these days.
  9783. */
  9784.     if (channel != 0 && my_count > 0) {
  9785.     debug(F101,"in_chk buf my_count","",my_count);
  9786.     n = my_count;            /* n was 0 before we got here */
  9787.     return(n);
  9788.     }
  9789. #endif /* MYREAD */
  9790. /*
  9791.   rdchk(), select(), and poll() tell us *if* data is available to be read, but
  9792.   not how much, so these should be used only as a final resort.  Especially
  9793.   since these calls tend to add a lot overhead.
  9794. */
  9795. #ifdef RDCHK                /* This mostly SCO-specific */
  9796.     n = rdchk(fd);
  9797.     debug(F101,"in_chk rdchk","",n);
  9798. #else /* No RDCHK */
  9799. #ifdef SELECT
  9800. #ifdef Plan9
  9801.     /* Only allows select on the console ... don't ask */
  9802.     if (channel == 0)
  9803. #endif /* Plan9 */
  9804.       {
  9805.     fd_set rfds;            /* Read file descriptors */
  9806. #ifdef BELLV10
  9807.     FD_ZERO(rfds);            /* Initialize them */
  9808.     FD_SET(fd,rfds);        /* We want to look at this fd */
  9809. #else
  9810.     FD_ZERO(&rfds);            /* Initialize them */
  9811.     FD_SET(fd,&rfds);        /* We want to look at this fd */
  9812.     tv.tv_sec = tv.tv_usec = 0L;    /* A 0-valued timeval structure */
  9813. #endif /* BELLV10 */
  9814. #ifdef Plan9
  9815.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9816.     debug(F101,"in_chk Plan 9 select","",n);
  9817. #else
  9818. #ifdef BELLV10
  9819.     n = select( 128, rfds, (fd_set *)0, (fd_set *)0, 0 );
  9820.     debug(F101,"in_chk BELLV10 select","",n);
  9821. #else
  9822. #ifdef BSD44
  9823.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9824.     debug(F101,"in_chk BSD44 select","",n);
  9825. #else
  9826. #ifdef BSD43
  9827.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9828.     debug(F101,"in_chk BSD43 select","",n);
  9829. #else
  9830. #ifdef SOLARIS
  9831.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9832.     debug(F101,"in_chk SOLARIS select","",n);
  9833. #else
  9834. #ifdef QNX6
  9835.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9836.     debug(F101,"in_chk QNX6 select","",n);
  9837. #else
  9838. #ifdef QNX
  9839.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9840.     debug(F101,"in_chk QNX select","",n);
  9841. #else
  9842. #ifdef COHERENT
  9843.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9844.     debug(F101,"in_chk COHERENT select","",n);
  9845. #else
  9846. #ifdef SVR4
  9847.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9848.     debug(F101,"in_chk SVR4 select","",n);
  9849. #else
  9850. #ifdef __linux__
  9851.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9852.     debug(F101,"in_chk LINUX select","",n);
  9853. #ifdef OSF
  9854.     n = select( FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv );
  9855.     debug(F101,"in_chk OSF select","",n);
  9856. #else
  9857.     n = select( FD_SETSIZE, &rfds, (int *)0, (int *)0, &tv );
  9858.     debug(F101,"in_chk catchall select","",n);
  9859. #endif /* OSF */
  9860. #endif /* __linux__ */
  9861. #endif /* SVR4 */
  9862. #endif /* COHERENT */
  9863. #endif /* QNX */
  9864. #endif /* QNX6 */
  9865. #endif /* SOLARIS */
  9866. #endif /* BSD43 */
  9867. #endif /* BSD44 */
  9868. #endif /* BELLV10 */
  9869. #endif /* Plan9 */
  9870.     }
  9871. #else  /* Not SELECT */
  9872. #ifdef CK_POLL
  9873.     {
  9874.       struct pollfd pfd;
  9875.  
  9876.       pfd.fd = fd;
  9877.       pfd.events = POLLIN;
  9878.       pfd.revents = 0;
  9879.       n = poll(&pfd, 1, 0);
  9880.       debug(F101,"in_chk poll","",n);
  9881.       if ((n > 0) && (pfd.revents & POLLIN))
  9882.     n = 1;
  9883.     }
  9884. #endif /* CK_POLL */
  9885. #endif /* SELECT */
  9886. #endif /* RDCHK */
  9887. #endif /* PROVX1 */
  9888. #endif /* V7 */
  9889. #endif /* FIONREAD */
  9890.  
  9891. /* From here down, treat console and communication device differently... */
  9892.  
  9893.     if (channel == 0) {            /* Console */
  9894.  
  9895. #ifdef SVORPOSIX
  9896. #ifndef FIONREAD
  9897. #ifndef SELECT
  9898. #ifndef CK_POLL
  9899. #ifndef RDCHK
  9900. /*
  9901.   This is the hideous hack used in System V and POSIX systems that don't
  9902.   support FIONREAD, rdchk(), select(), poll(), etc, in which the user's
  9903.   CONNECT-mode escape character is attached to SIGQUIT.  Used, obviously,
  9904.   only on the console.
  9905. */
  9906.     if (conesc) {            /* Escape character typed == SIGQUIT */
  9907.         debug(F100,"in_chk conesc","",conesc);
  9908.         conesc = 0;
  9909.         signal(SIGQUIT,esctrp);    /* Restore signal */
  9910.         n += 1;
  9911.     }
  9912. #endif /* RDCHK */
  9913. #endif /* CK_POLL */
  9914. #endif /* SELECT */
  9915. #endif /* FIONREAD */
  9916. #endif /* SVORPOSIX */
  9917.  
  9918.     return(n);            /* Done with console */
  9919.     }
  9920.  
  9921.     if (channel != 0) {            /* Communications connection */
  9922.  
  9923. #ifdef MYREAD
  9924. #ifndef FIONREAD
  9925. /*
  9926.   select() or rdchk(), etc, has told us that something is waiting, but we
  9927.   don't know how much.  So we do a read to get it and then we know.  Note:
  9928.   This read is NOT nonblocking if nothing is there (because of VMIN=1), but
  9929.   it should be safe in this case since the OS tells us at least one byte is
  9930.   waiting to be read, and MYREAD reads return as much as is there without
  9931.   waiting for any more.  Controlled tests on Solaris and Unixware (with
  9932.   FIONREAD deliberately undefined) show this to be true.
  9933. */
  9934.     debug(F101,"in_chk read my_count","",my_count);
  9935.     debug(F101,"in_chk read n","",n);
  9936.     if (n > 0 && my_count == 0) {
  9937.         /* This also catches disconnects etc */
  9938.         /* Do what mygetbuf does except don't grab a character */
  9939.         my_count = myfillbuf();
  9940.         my_item = -1;        /* ^^^ */
  9941.         debug(F101,"in_chk myfillbuf my_count","",my_count);
  9942.         if (my_count < 0)
  9943.           return(-1);
  9944.         else
  9945.           n = 0;            /* NB: n is replaced by my_count */
  9946.     }
  9947. #endif /* FIONREAD */
  9948. /*
  9949.   Here we add whatever we think is unread to what is still in our
  9950.   our internal buffer.  Thus the importance of setting n to 0 just above.
  9951. */
  9952.     debug(F101,"in_chk my_count","",my_count);
  9953.     debug(F101,"in_chk n","",n);
  9954.     if (my_count > 0)
  9955.       n += my_count;
  9956. #endif /* MYREAD */
  9957.     }
  9958.     debug(F101,"in_chk result","",n);
  9959.  
  9960.     /* Errors here don't prove the connection has dropped so just say 0 */
  9961.  
  9962.     return(n < 0 ? 0 : n);
  9963. }
  9964.  
  9965.  
  9966. /*  T T C H K  --  Tell how many characters are waiting in tty input buffer  */
  9967.  
  9968. int
  9969. ttchk() {
  9970.     int fd;
  9971. #ifdef NETCMD
  9972.     if (ttpipe)
  9973.       fd = fdin;
  9974.     else
  9975. #endif /* NETCMD */
  9976.       fd = ttyfd;
  9977.     return(in_chk(1,fd));
  9978. }
  9979.  
  9980. /*  T T X I N  --  Get n characters from tty input buffer  */
  9981.  
  9982. /*  Returns number of characters actually gotten, or -1 on failure  */
  9983.  
  9984. /*  Intended for use only when it is known that n characters are actually */
  9985. /*  Available in the input buffer.  */
  9986.  
  9987. int
  9988. ttxin(n,buf) int n; CHAR *buf; {
  9989.     register int x = 0, c = -2;
  9990. #ifdef TTLEBUF
  9991.     register int i = 0;
  9992. #endif /* TTLEBUF */
  9993.     int fd;
  9994.  
  9995.     if (n < 1)                /* Nothing to do */
  9996.       return(0);
  9997.  
  9998. #ifdef TTLEBUF
  9999.     if (ttpush >= 0) {
  10000.         buf[0] = ttpush;        /* Put pushed char in buffer*/
  10001.         ttpush = -1;            /* Clear the push buffer */
  10002.         if (ttchk() > 0)
  10003.       return(ttxin(n-1, &buf[1]) + 1);
  10004.         else
  10005.       return(1);
  10006.     }
  10007.     if (le_data) {
  10008.         while (le_inbuf() > 0) {
  10009.         if (le_getchar(&buf[i])) {
  10010.                 i++;
  10011.                 n--;
  10012.             }
  10013.         }
  10014.         if (ttchk() > 0)
  10015.       return(ttxin(n,&buf[i])+i);
  10016.         else
  10017.       return(i);
  10018.     }
  10019. #endif /* TTLEBUF */
  10020.  
  10021. #ifdef NETCMD
  10022.     if (ttpipe)
  10023.       fd = fdin;
  10024.     else
  10025. #endif /* NETCMD */
  10026.       fd = ttyfd;
  10027.  
  10028. #ifdef SUNX25
  10029.     if (netconn && (ttnet == NET_SX25))    /* X.25 connection */
  10030.       return(x25xin(n,buf));
  10031. #endif /* SUNX25 */
  10032.  
  10033. #ifdef IBMX25
  10034.     /* riehm: possibly not needed. Test worked with normal reads and writes */
  10035.     if (netconn && (ttnet == NET_IX25))    { /* X.25 connection */
  10036.     x = x25xin(n,buf);
  10037.     if (x > 0) buf[x] = '\0';
  10038.     return(x);
  10039.     }
  10040. #endif /* IBMX25 */
  10041.  
  10042. #ifdef MYREAD
  10043.     debug(F101,"ttxin MYREAD","",n);
  10044.     while (x < n) {
  10045.     c = myread();
  10046.     if (c < 0) {
  10047.         debug(F101,"ttxin myread returns","",c);
  10048.         if (c == -3) x = -1;
  10049.         break;
  10050.         }
  10051.     buf[x++] = c & ttpmsk;
  10052. #ifdef RLOGCODE
  10053. #ifdef CK_KERBEROS
  10054.         /* It is impossible to know how many characters are waiting */
  10055.         /* to be read when you are using Encrypted Rlogin or SSL    */
  10056.         /* as the transport since the number of real data bytes     */
  10057.         /* can be greater or less than the number of bytes on the   */
  10058.         /* wire which is what ttchk() returns.                      */
  10059.         if (netconn && (ttnproto == NP_EK4LOGIN || ttnproto == NP_EK5LOGIN))
  10060.       if (ttchk() <= 0)
  10061.         break;
  10062. #endif /* CK_KERBEROS */
  10063. #endif /* RLOGCODE */
  10064. #ifdef CK_SSL
  10065.         if (ssl_active_flag || tls_active_flag)
  10066.       if (ttchk() <= 0)
  10067.         break;
  10068. #endif /* CK_SSL */
  10069.     }
  10070. #else
  10071.     debug(F101,"ttxin READ","",n);
  10072.     x = read(fd,buf,n);
  10073.     for (c = 0; c < n; c++)        /* Strip any parity */
  10074.       buf[c] &= ttpmsk;
  10075. #endif /* MYREAD */
  10076.  
  10077.     debug(F101,"ttxin x","",x);        /* Done */
  10078.     if (x > 0) buf[x] = '\0';
  10079.     if (x < 0) x = -1;
  10080.     return(x);
  10081. }
  10082.  
  10083. /*  T T O L  --  Write string s, length n, to communication device.  */
  10084. /*
  10085.   Returns:
  10086.    >= 0 on success, number of characters actually written.
  10087.    -1 on failure.
  10088. */
  10089. #ifdef CK_ENCRYPTION
  10090. CHAR * xpacket = NULL;
  10091. int nxpacket = 0;
  10092. #endif /* CK_ENCRYPTION */
  10093.  
  10094. #define TTOLMAXT 5
  10095. int
  10096. ttol(s,n) int n; CHAR *s; {
  10097.     int x, len, tries, fd;
  10098. #ifdef CKXXCHAR
  10099.     extern int dblflag;            /* For SET SEND DOUBLE-CHARACTER */
  10100.     extern short dblt[];
  10101.     CHAR *p = NULL, *p2, *s2, c;
  10102.     int n2 = 0;
  10103. #endif /* CKXXCHAR */
  10104.  
  10105.     if (ttyfd < 0)            /* Not open? */
  10106.       return(-3);
  10107. #ifdef DEBUG
  10108.     if (deblog) hexdump("ttol s",s,n);
  10109. #endif /* DEBUG */
  10110.  
  10111. #ifdef NETCMD
  10112.     if (ttpipe)
  10113.       fd = fdout;
  10114.     else
  10115. #endif /* NETCMD */
  10116.       fd = ttyfd;
  10117.  
  10118. #ifdef CKXXCHAR
  10119. /*  Double any characters that must be doubled.  */
  10120.     debug(F101,"ttol dblflag","",dblflag);
  10121.     if (dblflag) {
  10122.     p = (CHAR *) malloc(n + n + 1);
  10123.     if (p) {
  10124.         s2 = s;
  10125.         p2 = p;
  10126.         n2 = 0;
  10127.         while (*s2) {
  10128.         c = *s2++;
  10129.         *p2++ = c;
  10130.         n2++;
  10131.         if (dblt[(unsigned) c] & 2) {
  10132.             *p2++ = c;
  10133.             n2++;
  10134.         }
  10135.         }
  10136.         s = p;
  10137.         n = n2;
  10138.         s[n] = '\0';
  10139.     }
  10140. #ifdef DEBUG
  10141.         if (deblog) hexdump("ttol doubled s",s,n);
  10142. #endif /* DEBUG */
  10143.     }
  10144. #endif /* CKXXCHAR */
  10145.  
  10146.     tries = TTOLMAXT;            /* Allow up to this many tries */
  10147.     len = n;                /* Remember original length */
  10148.  
  10149. #ifdef CK_ENCRYPTION
  10150. /*
  10151.   This is to avoid encrypting a packet that is already encrypted, e.g.
  10152.   when we resend a packet directly out of the packet buffer, and also to
  10153.   avoid encrypting a constant (literal) string, which can cause a memory
  10154.   fault.
  10155. */
  10156.     if (TELOPT_ME(TELOPT_ENCRYPTION)) {
  10157.     int x;
  10158.     if (nxpacket < n) {
  10159.         if (xpacket) {
  10160.         free(xpacket);
  10161.         xpacket = NULL;
  10162.         nxpacket = 0;
  10163.         }
  10164.         x = n > 10240 ? n : 10240;
  10165.         xpacket = (CHAR *)malloc(x);
  10166.         if (!xpacket) {
  10167.         fprintf(stderr,"ttol malloc failure\n");
  10168.         return(-1);
  10169.         } else
  10170.           nxpacket = x;
  10171.     }
  10172.     memcpy((char *)xpacket,(char *)s,n);
  10173.     s = xpacket;
  10174.     ck_tn_encrypt((char *)s,n);
  10175.     }
  10176. #endif /* CK_ENCRYPTION */
  10177.  
  10178.     while (n > 0 &&
  10179.        (tries-- > 0
  10180. #ifdef CK_ENCRYPTION
  10181.         /* keep trying if we are encrypting */
  10182.         || TELOPT_ME(TELOPT_ENCRYPTION)
  10183. #endif /* CK_ENCRYPTION */
  10184.             )) {            /* Be persistent */
  10185.     debug(F101,"ttol try","",TTOLMAXT - tries);
  10186. #ifdef BEOSORBEBOX
  10187.         if (netconn && !ttpipe && !ttpty)
  10188.       x = nettol((char *)s,n);    /* Write string to device */
  10189.         else
  10190. #endif /* BEOSORBEBOX */
  10191. #ifdef IBMX25
  10192.       if (ttnet == NET_IX25)
  10193.         /*
  10194.          * this is a more controlled way of writing to X25
  10195.          * STREAMS, however write should also work!
  10196.          */
  10197.         x = x25write(ttyfd, s, n);
  10198.       else
  10199. #endif /* IBMX25 */
  10200. #ifdef CK_SSL
  10201.         if (ssl_active_flag || tls_active_flag) {
  10202.         int error;
  10203.         /* Write using SSL */
  10204.                 ssl_retry:
  10205.         if (ssl_active_flag)
  10206.                   x = SSL_write(ssl_con, s, n);
  10207.         else
  10208.                   x = SSL_write(tls_con, s, n);
  10209.         switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,x)) {
  10210.                 case SSL_ERROR_NONE:
  10211.                     if (x == n)
  10212.               return(len);
  10213.                     s += x;
  10214.                     n -= x;
  10215.                     goto ssl_retry;
  10216.           case SSL_ERROR_WANT_WRITE:
  10217.           case SSL_ERROR_WANT_READ:
  10218.             x = 0;
  10219.             break;
  10220.           case SSL_ERROR_SYSCALL:
  10221.                     if (x != 0)
  10222.               return(-1);
  10223.           case SSL_ERROR_WANT_X509_LOOKUP:
  10224.           case SSL_ERROR_SSL:
  10225.           case SSL_ERROR_ZERO_RETURN:
  10226.           default:
  10227.             ttclos(0);
  10228.             return(-3);
  10229.         }
  10230.         } else
  10231. #endif /* CK_SSL */
  10232. #ifdef CK_KERBEROS
  10233. #ifdef KRB4
  10234. #ifdef RLOGCODE
  10235.         if (ttnproto == NP_EK4LOGIN) {
  10236.         return(krb4_des_write(ttyfd,s,n));
  10237.         } else
  10238. #endif /* RLOGCODE */
  10239. #endif /* KRB4 */
  10240. #ifdef KRB5
  10241. #ifdef RLOGCODE
  10242.             if (ttnproto == NP_EK5LOGIN) {
  10243.                 return(krb5_des_write(ttyfd,s,n,0));
  10244.             } else
  10245. #endif /* RLOGCODE */
  10246. #ifdef KRB5_U2U
  10247.             if (ttnproto == NP_K5U2U) {
  10248.                 return(krb5_u2u_write(ttyfd,s,n));
  10249.             } else
  10250. #endif /* KRB5_U2U */
  10251. #endif /* KRB5 */
  10252. #endif /* CK_KERBEROS */
  10253.           x = write(fd,s,n);    /* Write string to device */
  10254.  
  10255.     if (x == n) {            /* Worked? */
  10256.         debug(F101,"ttol ok","",x);    /* OK */
  10257. #ifdef CKXXCHAR
  10258.         if (p) free(p);
  10259. #endif /* CKXXCHAR */
  10260.         return(len);        /* Done */
  10261.     } else if (x < 0) {        /* No, got error? */
  10262.         debug(F101,"ttol write error","",errno);
  10263. #ifdef EWOULDBLOCK
  10264.         if (errno == EWOULDBLOCK) {
  10265.         msleep(10);
  10266.         continue;
  10267.         } else
  10268. #endif /* EWOULDBLOCK */
  10269. #ifdef TCPSOCKET
  10270.         if (netconn && ttnet == NET_TCPB) {
  10271.         debug(F101,"ttol TCP error","",errno);
  10272.         ttclos(0);        /* Close the connection. */
  10273.         x = -3;
  10274.         }
  10275. #endif /* TCPSOCKET */
  10276. #ifdef CKXXCHAR
  10277.         if (p) free(p);
  10278. #endif /* CKXXCHAR */
  10279.         return(x);
  10280.     } else {            /* No error, so partial success */
  10281.         debug(F101,"ttol partial","",x); /* This never happens */
  10282.         s += x;            /* Point to part not written yet */
  10283.         n -= x;            /* Adjust length */
  10284.         if (x > 0) msleep(10);    /* Wait 10 msec */
  10285.     }                /* Go back and try again */
  10286.     }
  10287. #ifdef CKXXCHAR
  10288.     if (p) free(p);
  10289. #endif /* CKXXCHAR */
  10290.     return(n < 1 ? len : -1);        /* Return the results */
  10291. }
  10292.  
  10293. /*  T T O C  --  Output a character to the communication line  */
  10294.  
  10295. /*
  10296.  This function should only be used for interactive, character-mode operations,
  10297.  like terminal connection, script execution, dialer i/o, where the overhead
  10298.  of the signals and alarms does not create a bottleneck.
  10299. */
  10300. int
  10301. #ifdef CK_ANSIC
  10302. ttoc(char c)
  10303. #else
  10304. ttoc(c) char c;
  10305. #endif /* CK_ANSIC */
  10306. /* ttoc */ {
  10307. #define TTOC_TMO 15            /* Timeout in case we get stuck */
  10308.     int xx, fd;
  10309.  
  10310.     if (ttyfd < 0)            /* Check for not open. */
  10311.       return(-1);
  10312.  
  10313. #ifdef NETCMD
  10314.     if (ttpipe)
  10315.       fd = fdout;
  10316.     else
  10317. #endif /* NETCMD */
  10318.       fd = ttyfd;
  10319.  
  10320.     c &= 0xff;
  10321.     /* debug(F101,"ttoc","",(CHAR) c); */
  10322.     saval = signal(SIGALRM,timerh);    /* Enable timer interrupt */
  10323.     xx = alarm(TTOC_TMO);        /* for this many seconds. */
  10324.     if (xx < 0) xx = 0;            /* Save old alarm value. */
  10325.     /* debug(F101,"ttoc alarm","",xx); */
  10326.     if (
  10327. #ifdef CK_POSIX_SIG
  10328.     sigsetjmp(sjbuf,1)
  10329. #else
  10330.     setjmp(sjbuf)
  10331. #endif /* CK_POSIX_SIG */
  10332.     ) {        /* Timer went off? */
  10333.     ttimoff();            /* Yes, cancel this alarm. */
  10334.     if (xx - TTOC_TMO > 0) alarm(xx - TTOC_TMO); /* Restore previous one */
  10335.         /* debug(F100,"ttoc timeout","",0); */
  10336. #ifdef NETCONN
  10337.     if (!netconn) {
  10338. #endif /* NETCONN */
  10339.         debug(F101,"ttoc timeout","",c);
  10340.         if (ttflow == FLO_XONX) {
  10341.         debug(F101,"ttoc flow","",ttflow); /* Maybe we're xoff'd */
  10342. #ifndef Plan9
  10343. #ifdef POSIX
  10344.         /* POSIX way to unstick. */
  10345.         debug(F100,"ttoc tcflow","",tcflow(ttyfd,TCOON));
  10346. #else
  10347. #ifdef BSD4                /* Berkeley way to do it. */
  10348. #ifdef TIOCSTART
  10349. /* .... Used to be "ioctl(ttyfd, TIOCSTART, 0);".  Who knows? */
  10350.         {
  10351.           int x = 0;
  10352.           debug(F101,"ttoc TIOCSTART","",ioctl(ttyfd, TIOCSTART, &x));
  10353.         }
  10354. #endif /* TIOCSTART */
  10355. #endif /* BSD4 */
  10356.                     /* Is there a Sys V way to do this? */
  10357. #endif /* POSIX */
  10358. #endif /* Plan9 */
  10359.         }
  10360. #ifdef NETCONN
  10361.         }
  10362. #endif /* NETCONN */
  10363.     return(-1);            /* Return failure code. */
  10364.     } else {
  10365.         int rc;
  10366. #ifdef BEOSORBEBOX
  10367. #ifdef NETCONN
  10368.         if (netconn && !ttpipe && !ttpty)
  10369.       rc = nettoc(c);
  10370.         else
  10371. #endif /*  BEOSORBEBOX */
  10372. #endif /* NETCONN */
  10373. #ifdef CK_ENCRYPTION
  10374.       if (TELOPT_ME(TELOPT_ENCRYPTION))
  10375.         ck_tn_encrypt(&c,1);
  10376. #endif /* CK_ENCRYPTION */
  10377. #ifdef IBMX25
  10378.     /* riehm: maybe this isn't necessary after all. Test program
  10379.      * worked fine with data being sent and retrieved with normal
  10380.      * read's and writes!
  10381.      */
  10382.     if (ttnet == NET_IX25)
  10383.       rc = x25write(ttyfd,&c,1); /* as above for X25 streams */
  10384.     else
  10385. #endif /* IBMX25 */
  10386. #ifdef CK_SSL
  10387.       if (ssl_active_flag || tls_active_flag) {
  10388.           int error;
  10389.           /* Write using SSL */
  10390.           if (ssl_active_flag)
  10391.                 rc = SSL_write(ssl_con, &c, 1);
  10392.           else
  10393.                 rc = SSL_write(tls_con, &c, 1);
  10394.           switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,rc)){
  10395.         case SSL_ERROR_NONE:
  10396.           break;
  10397.         case SSL_ERROR_WANT_WRITE:
  10398.         case SSL_ERROR_WANT_READ:
  10399.           rc = 0;
  10400.           break;
  10401.         case SSL_ERROR_SYSCALL:
  10402.           if (rc != 0)
  10403.             return(-1);
  10404.         case SSL_ERROR_WANT_X509_LOOKUP:
  10405.         case SSL_ERROR_SSL:
  10406.         case SSL_ERROR_ZERO_RETURN:
  10407.         default:
  10408.           ttclos(0);
  10409.           return(-1);
  10410.           }
  10411.       } else
  10412. #endif /* CK_SSL */
  10413. #ifdef CK_KERBEROS
  10414. #ifdef KRB4
  10415. #ifdef RLOGCODE
  10416.       if (ttnproto == NP_EK4LOGIN) {
  10417.           rc = (krb4_des_write(ttyfd,&c,1) == 1);
  10418.       } else
  10419. #endif /* RLOGCODE */
  10420. #endif /* KRB4 */
  10421. #ifdef KRB5
  10422. #ifdef RLOGCODE
  10423.           if (ttnproto == NP_EK5LOGIN) {
  10424.               rc = (krb5_des_write(ttyfd,&c,1,0) == 1);
  10425.           } else
  10426. #endif /* RLOGCODE */
  10427. #ifdef KRB5_U2U
  10428.           if (ttnproto == NP_K5U2U) {
  10429.               rc = (krb5_u2u_write(ttyfd,&c,1) == 1);
  10430.           } else
  10431. #endif /* KRB5_U2U */
  10432. #endif /* KRB5 */
  10433. #endif /* CK_KERBEROS */
  10434.         rc = write(fd,&c,1);    /* Try to write the character. */
  10435.     if (rc < 1) {            /* Failed */
  10436.         ttimoff();            /* Turn off the alarm. */
  10437.         alarm(xx);            /* Restore previous alarm. */
  10438.         debug(F101,"ttoc errno","",errno); /* Log the error, */
  10439.         return(-1);            /* and return the error code. */
  10440.     }
  10441.     }
  10442.     ttimoff();                /* Success, turn off the alarm. */
  10443.     alarm(xx);                /* Restore previous alarm. */
  10444.     return(0);                /* Return good code. */
  10445. }
  10446.  
  10447. /*  T T I N L  --  Read a record (up to break character) from comm line.  */
  10448. /*
  10449.   Reads up to "max" characters from the communication line, terminating on:
  10450.     (a) the packet length field if the "turn" argument is zero, or
  10451.     (b) on the packet-end character (eol) if the "turn" argument is nonzero
  10452.     (c) a certain number of Ctrl-C's in a row
  10453.  
  10454.   Returns:
  10455.     >= 0, the number of characters read upon success;
  10456.     -1 if "max" exceeded, timeout, or other correctable error;
  10457.     -2 on user interruption (c);
  10458.     -3 on fatal error like connection lost.
  10459.  
  10460.   The characters that were input are copied into "dest" with their parity bits
  10461.   stripped if parity was selected.  Returns the number of characters read.
  10462.   Characters after the eol are available upon the next call to this function.
  10463.  
  10464.   The idea is to minimize the number of system calls per packet, and also to
  10465.   minimize timeouts.  This function is the inner loop of the protocol and must
  10466.   be as efficient as possible.  The current strategy is to use myread().
  10467.  
  10468.   WARNING: This function calls parchk(), which is defined in another module.
  10469.   Normally, ckutio.c does not depend on code from any other module, but there
  10470.   is an exception in this case because all the other ck?tio.c modules also
  10471.   need to call parchk(), so it's better to have it defined in a common place.
  10472. */
  10473. #ifdef CTRLC
  10474. #undef CTRLC
  10475. #endif /* CTRLC */
  10476. #define CTRLC '\03'
  10477. /*
  10478.   We have four different declarations here because:
  10479.   (a) to allow Kermit to be built without the automatic parity sensing feature
  10480.   (b) one of each type for ANSI C, one for non-ANSI.
  10481. */
  10482.  
  10483. static int csave = -1;
  10484.  
  10485. #ifndef NOXFER
  10486. int
  10487. #ifdef PARSENSE
  10488. #ifdef CK_ANSIC
  10489. ttinl(CHAR *dest, int max,int timo, CHAR eol, CHAR start, int turn)
  10490. #else
  10491. ttinl(dest,max,timo,eol,start,turn) int max,timo,turn; CHAR *dest, eol, start;
  10492. #endif /* CK_ANSIC */
  10493. #else /* not PARSENSE */
  10494. #ifdef CK_ANSIC
  10495. ttinl(CHAR *dest, int max,int timo, CHAR eol)
  10496. #else
  10497. ttinl(dest,max,timo,eol) int max,timo; CHAR *dest, eol;
  10498. #endif /* __SDTC__ */
  10499. #endif /* PARSENSE */
  10500. /* ttinl */ {
  10501.  
  10502. #ifndef MYREAD
  10503.     CHAR ch, dum;
  10504. #endif /* MYREAD */
  10505. #ifdef PARSENSE
  10506.     int pktlen = -1;
  10507.     int lplen = 0;
  10508.     int havelen = 0;
  10509. #endif /* PARSENSE */
  10510.     int fd;
  10511.     int sopmask = 0xff;            /* Start-Of-Packet mask */
  10512. #ifdef CKXXCHAR
  10513.     extern short dblt[];        /* Ignore-character table */
  10514.     extern int ignflag;
  10515. #endif /* CKXXCHAR */
  10516. #ifdef TCPSOCKET
  10517.     extern CHAR stchr;
  10518. #endif /* TCPSOCKET */
  10519.     int x;
  10520. #ifdef STREAMING
  10521.     extern int streaming;
  10522.     extern int sndtyp;
  10523. #endif /* STREAMING */
  10524.  
  10525.     if (ttyfd < 0) return(-3);          /* Not open. */
  10526.  
  10527.     debug(F101,"ttinl max","",max);
  10528.     debug(F101,"ttinl timo","",timo);
  10529.  
  10530. #ifdef NETCMD
  10531.     if (ttpipe)
  10532.       fd = fdin;
  10533.     else
  10534. #endif /* NETCMD */
  10535.       fd = ttyfd;
  10536.  
  10537. #ifdef COMMENT
  10538.     if (xlocal && conchk() > 0)        /* Allow for console interruptions */
  10539.       return(-1);
  10540. #endif /* COMMENT */
  10541.  
  10542.     *dest = '\0';                       /* Clear destination buffer */
  10543.     if (timo < 0) timo = 0;        /* Safety */
  10544.     if (timo) {                /* Don't time out if timo == 0 */
  10545.     int xx;
  10546.     saval = signal(SIGALRM,timerh);    /* Enable timer interrupt */
  10547.     xx = alarm(timo);        /* Set it. */
  10548.     debug(F101,"ttinl alarm","",xx);
  10549.     }
  10550.     if (
  10551. #ifdef CK_POSIX_SIG
  10552.     sigsetjmp(sjbuf,1)
  10553. #else
  10554.     setjmp(sjbuf)
  10555. #endif /* CK_POSIX_SIG */
  10556.     ) {                /* Timer went off? */
  10557.     debug(F100,"ttinl timout","",0); /* Get here on timeout. */
  10558.     /* debug(F110," with",(char *) dest,0); */
  10559.     ttimoff();            /* Turn off timer */
  10560.     return(-1);            /* and return error code. */
  10561.     } else {
  10562.     register int i, n = -1;        /* local variables */
  10563.     int ccn = 0;
  10564. #ifdef PARSENSE
  10565.     register int flag = 0;
  10566.     debug(F000,"ttinl start","",start);
  10567. #endif /* PARSENSE */
  10568.  
  10569.     ttpmsk = ttprty ? 0177 : 0377;    /* Set parity stripping mask. */
  10570.     sopmask = needpchk ? 0177 : ttpmsk; /* And SOP matching mask. */
  10571.  
  10572. /* Now read into destination, stripping parity and looking for the */
  10573. /* the packet terminator, and also for several Ctrl-C's typed in a row. */
  10574.  
  10575.     i = 0;                /* Destination index */
  10576.     debug(F101,"ttinl eol","",eol);
  10577.  
  10578.     while (i < max-1) {
  10579. #ifdef MYREAD
  10580.         /* debug(F101,"ttinl i","",i); */
  10581.         errno = 0;
  10582.         if (csave > -1) {
  10583.             n = csave;
  10584.         debug(F101,"ttinl unsaving","",n);
  10585.         } else
  10586. #ifdef COMMENT
  10587.           if (xlocal && conchk() > 0) {
  10588.           /* Here we could catch keyboard interruptions. */
  10589.           /* But this would be VERY expensive. */
  10590.           /* We could also do it in myread() but it would be */
  10591.           /* expensive there too -- even if done with select()... */
  10592.           }
  10593. #endif /* COMMENT */
  10594.           if ((n = myread()) < 0) {    /* Timeout or i/o error? */
  10595. #ifdef DEBUG
  10596.         if (deblog) {
  10597.             debug(F101,"ttinl myread failure, n","",n);
  10598.             debug(F101,"ttinl myread errno","",errno);
  10599.         }
  10600. #endif /* DEBUG */
  10601.         /* Don't let EINTR break packets. */
  10602.         if (n == -3) {
  10603.             if (errno == EINTR && i > 0) {
  10604.             debug(F111,"ttinl EINTR myread i","continuing",i);
  10605.             continue;
  10606.             } else {
  10607.             debug(F110,"ttinl non-EINTR -3","closing",0);
  10608.             wasclosed = 1;
  10609.             ttimoff();    /* Turn off timer */
  10610.             ttclos(0);
  10611.             return(n);
  10612.             }
  10613.         } else if (n == -2 && netconn /* && timo == 0 */ ) {
  10614.             /* Here we try to catch broken network connections */
  10615.             /* even when ioctl() and read() do not catch them */
  10616.             debug(F111,"ttinl network myread failure","closing",n);
  10617.             wasclosed = 1;
  10618.             ttimoff();
  10619.             ttclos(0);
  10620.             return(-3);
  10621.         }
  10622. #ifdef STREAMING
  10623.         /* Streaming and no data to read */
  10624.         else if (n == 0 && streaming && sndtyp == 'D')
  10625.           return(0);
  10626. #endif /* STREAMING */
  10627.         break;            /* Break out of while loop */
  10628.         }
  10629.  
  10630. #else /* not MYREAD (is this code used anywhere any more?) */
  10631.  
  10632.         if (csave > -1)        /* Char saved from last time */
  10633.           ch = csave;
  10634.         else if ((n = read(fd, &ch, 1)) < 1)
  10635.           break;            /* Error - break out of while loop */
  10636.         n = ch;
  10637.  
  10638. #endif /* MYREAD */
  10639.  
  10640.         /* Get here with char in n */
  10641.  
  10642. #ifdef CK_ENCRYPTION
  10643.         /* If csave > -1 we already decrypted this character */
  10644.         /* So don't decrypt it again */
  10645.         if (TELOPT_U(TELOPT_ENCRYPTION) && csave == -1) {
  10646.         CHAR ch = n;
  10647.         ck_tn_decrypt(&ch,1);
  10648.         n = ch;
  10649.         }
  10650. #endif /* CK_ENCRYPTION */
  10651.  
  10652.         csave = -1;            /* Unflag that we unsaved a char */
  10653.  
  10654. #ifdef TCPSOCKET
  10655.         if (n == IAC &&        /* Handle Telnet options */
  10656.         ((xlocal && netconn && IS_TELNET()) ||
  10657.         (!xlocal && sstelnet))) {
  10658.         n = tt_tnopt(n);
  10659.         if (n < 0)
  10660.           return(n);
  10661. #ifndef NOPARSEN
  10662.         else if (n == 1)
  10663.           start = stchr;
  10664. #endif /* NOPARSEN */
  10665.         if (n != 255)        /* No data - go back for next char */
  10666.           continue;
  10667.         }                /* Quoted IAC - keep going */
  10668. #endif /* TCPSOCKET */
  10669. #ifdef CKXXCHAR
  10670.         if (ignflag)
  10671.           if (dblt[(unsigned) n] & 1) /* Character to ignore? */
  10672.         continue;
  10673. #endif /* CKXXCHAR */
  10674.  
  10675. /*
  10676.   Use parity mask, rather than always stripping parity, to check for
  10677.   cancellation.  Otherwise, runs like \x03\x83\x03 in a packet could cancel
  10678.   the transfer when parity is NONE.  (Note that \x03\x03\x03 is extremely
  10679.   unlikely due to run-length encoding.)
  10680. */
  10681.         /* Check cancellation */
  10682.         if (!xlocal && xfrcan && ((n & ttpmsk) == xfrchr)) {
  10683.         if (++ccn >= xfrnum) {    /* If xfrnum in a row, bail out. */
  10684.             if (timo) {        /* Clear timer. */
  10685.             ttimoff();
  10686.             }
  10687.             if (xfrchr < 32)
  10688.               printf("^%c...\r\n",(char)(xfrchr+64));
  10689.             else
  10690.               printf("Canceled...\r\n");
  10691.             return(-2);
  10692.         }
  10693.         } else ccn = 0;        /* No cancellation, reset counter, */
  10694.  
  10695. #ifdef PARSENSE
  10696.         if (flag == 0) {        /* Find the Start-Of-Packet. */
  10697.         if ((n & sopmask) == start) { /* Got it */
  10698.             flag = 1;
  10699.         } else {        /* Keep looking... */
  10700.             debug(F000,"ttinl skipping","",n);
  10701.             continue;
  10702.         }
  10703.         }
  10704.         dest[i++] = n & ttpmsk;
  10705. /*
  10706.   If we have not been instructed to wait for a turnaround character, we
  10707.   can go by the packet length field.  If turn != 0, we must wait for the
  10708.   end of line (eol) character before returning.  This is an egregious
  10709.   violation of all principles of layering...
  10710. */
  10711.         if (!havelen) {
  10712.         if (i == 2) {
  10713.             pktlen = xunchar(dest[1] & 0x7f);
  10714.             if (pktlen > 1) {
  10715.             havelen = 1;
  10716.             debug(F101,"ttinl length","",pktlen);
  10717.             }
  10718.         } else if (i == 5 && pktlen == 0) {
  10719.             lplen = xunchar(dest[4] & 0x7f);
  10720.         } else if (i == 6 && pktlen == 0) {
  10721.             pktlen = lplen * 95 + xunchar(dest[5] & 0x7f) + 5;
  10722.             havelen = 1;
  10723.             debug(F101,"ttinl extended length","",pktlen);
  10724.         }
  10725.         }
  10726.  
  10727. /*
  10728.   Suppose we looked at the sequence number here and found it was out of
  10729.   range?  This would mean either (a) incoming packets had SOP unprefixed
  10730.   and we are out of sync, or (b) the packet is damaged.  Since (a) is bad
  10731.   practice, let's ignore it.  So what should we do here if we know the
  10732.   packet is damaged?
  10733.  
  10734.    1. Nothing -- keep trying to read the packet till we find what we think
  10735.       is the end, or we time out, and let the upper layer decide what to
  10736.       do.  But since either the packet is corrupt or we are out of sync,
  10737.       our criterion for finding the end does not apply and we are likely
  10738.       to time out (or swallow a piece of the next packet) if our assumed
  10739.       length is too long.  (This was the behavior prior to version 7.0.)
  10740.  
  10741.    2. set flag = 0 and continue?  This would force us to wait for the
  10742.       next packet to come in, and therefore (in the nonwindowing case),
  10743.       would force a timeout in the other Kermit.
  10744.  
  10745.    3. set flag = 0 and continue, but only if the window size is > 1 and
  10746.       the window is not blocked?  Talk about cheating!
  10747.  
  10748.    4. Return a failure code and let the upper layer decide what to do.
  10749.       This should be equivalent to 3, but without the cheating.  So let's
  10750.       do it that way...  But note that we must ignore the parity bit
  10751.       in case this is the first packet and we have not yet run parchk().
  10752. */
  10753.         if (i == 3) {        /* Peek at sequence number */
  10754.         x = xunchar((dest[i-1] & 0x7f)); /* If it's not in range... */
  10755.         if (x < 0 || x > 63) {
  10756.             debug(F111,"ttinl bad seq",dest,x);
  10757.             if (timo) ttimoff();
  10758.             return(-1);        /* return a nonfatal error */
  10759.         }
  10760.         }
  10761.  
  10762. #else /* PARSENSE */
  10763.         dest[i++] = n & ttpmsk;
  10764. #endif /* PARSENSE */
  10765.  
  10766.     /* Check for end of packet */
  10767.  
  10768.         if (
  10769. #ifdef PARSENSE
  10770. /*
  10771.   Purely length-driven if SET HANDSHAKE NONE (i.e. turn == 0).
  10772.   This allows packet terminators and handshake characters to appear
  10773.   literally inside a packet data field.
  10774. */
  10775.         (havelen && (i > pktlen+1) &&
  10776.          (!turn || (turn && (n & 0x7f) == turn))) /* (turn, not eol) */
  10777. #else /* !PARSENSE */
  10778. /*
  10779.   Built without PARSENSE, so just look for packet terminator.
  10780. */
  10781.         ((n & 0x7f) == eol)
  10782. #endif /* PARSENSE */
  10783.         ) {
  10784. #ifndef PARSENSE
  10785.         debug(F101,"ttinl got eol","",eol); /* (or turn) */
  10786.         dest[i] = '\0';        /* Yes, terminate the string, */
  10787.         /* debug(F101,"ttinl i","",i); */
  10788. #else
  10789. #ifdef DEBUG
  10790.         if (deblog) {
  10791.             if ((n & 0x7f) != eol) {
  10792.             debug(F101,"ttinl EOP length","",pktlen);
  10793.             debug(F101,"ttinl i","",i);
  10794. #ifdef MYREAD
  10795. #ifdef PARSENSE
  10796. /*
  10797.   We read a packet based on its length.  This leaves the EOP character still
  10798.   unread, and so ttchk() will always return at least 1 because of this.  But
  10799.   if we know it is there, we can safely get rid of it.  So...
  10800. */
  10801.             {
  10802.                 int x;
  10803.                 while (my_count > 0) {
  10804.                 x = ttinc(0);
  10805.                 /* Start of next packet */
  10806.                 if (x == start) { /* Save for next time */
  10807.                     csave = (unsigned)((unsigned)x & 0xff);
  10808.                     debug(F000,"ttinl csaved","",x);
  10809.                     break;
  10810.                 }
  10811.                 debug(F000,"ttinl removed","",x);
  10812.                 }
  10813.             }
  10814. #endif /* PARSENSE */
  10815. #endif /* MYREAD */
  10816.  
  10817.             } else debug(F101,"ttinl got eol","",eol); /* (or turn) */
  10818.         }
  10819. #endif /* DEBUG */
  10820.         dest[i] = '\0';        /* Terminate the string, */
  10821.             if (needpchk) {        /* Parity checked yet? */
  10822.             if (ttprty == 0) {    /* No, check. */
  10823.             if ((ttprty = parchk(dest,start,i)) > 0) {
  10824.                 int j;
  10825.                 debug(F101,"ttinl senses parity","",ttprty);
  10826.                 debug(F110,"ttinl packet before",dest,0);
  10827.                 ttpmsk = 0x7f;
  10828.                 for (j = 0; j < i; j++)
  10829.                   dest[j] &= 0x7f;    /* Strip parity from packet */
  10830.                 debug(F110,"ttinl packet after ",dest,0);
  10831.             } else ttprty = 0; /* Restore if parchk error */
  10832.             }
  10833.             sopmask = ttprty;
  10834.             needpchk = 0;
  10835.         }
  10836. #endif /* PARSENSE */
  10837.         if (timo) {        /* Turn off timer. */
  10838.             ttimoff();
  10839.         }
  10840. #ifdef COMMENT
  10841.         debug(F011,"ttinl got", dest, (i < 60) ? i : -60);
  10842. #else /* COMMENT */
  10843.                 hexdump("ttinl got",dest,i);
  10844. #endif /* COMMENT */
  10845. #ifdef STREAMING
  10846.         /* ttinl() was called because there was non-packet */
  10847.         /* data sitting int the channel.  Ignore it.       */
  10848.         if (streaming && sndtyp == 'D')
  10849.           return(-1);
  10850. #endif /* STREAMING */
  10851.         return(i);
  10852.         }
  10853.     } /* End of while() */
  10854.     ttimoff();
  10855.     return(n);
  10856.     }
  10857. }
  10858. #endif /* NOXFER */
  10859.  
  10860. /*  T T I N C --  Read a character from the communication line  */
  10861. /*
  10862.  On success, returns the character that was read, >= 0.
  10863.  On failure, returns -1 or other negative myread error code,
  10864.    or -2 if connection is broken or ttyfd < 0.
  10865.    or -3 if session limit has expired,
  10866.    or -4 if something or other...
  10867.  NOTE: The API does not provide for ttinc() returning a special code
  10868.  upon timeout, but we need it.  So for this we have a global variable,
  10869.  ttinctimo.
  10870. */
  10871. static int ttinctimo = 0;        /* Yuk */
  10872.  
  10873. int
  10874. ttinc(timo) int timo; {
  10875.  
  10876.     int n = 0, fd;
  10877.     int is_tn = 0;
  10878.     CHAR ch = 0;
  10879.  
  10880.     ttinctimo = 0;
  10881.  
  10882.     if (ttyfd < 0) return(-2);          /* Not open. */
  10883.  
  10884.     is_tn = (xlocal && netconn && IS_TELNET()) ||
  10885.         (!xlocal && sstelnet);
  10886.  
  10887. #ifdef TTLEBUF
  10888.     if (ttpush >= 0) {
  10889.         debug(F111,"ttinc","ttpush",ttpush);
  10890.         ch = ttpush;
  10891.         ttpush = -1;
  10892.         return(ch);
  10893.     }
  10894.     if (le_data) {
  10895.         if (le_getchar(&ch) > 0) {
  10896.             debug(F111,"ttinc le_getchar","ch",ch);
  10897.             return(ch);
  10898.         }
  10899.     }
  10900. #endif /* TTLEBUF */
  10901.  
  10902. #ifdef NETCMD
  10903.     if (ttpipe)
  10904.       fd = fdin;
  10905.     else
  10906. #endif /* NETCMD */
  10907.       fd = ttyfd;
  10908.  
  10909.     if ((timo <= 0)            /* Untimed. */
  10910. #ifdef MYREAD
  10911.     || (my_count > 0)        /* Buffered char already waiting. */
  10912. #endif /* MYREAD */
  10913.     ) {
  10914. #ifdef MYREAD
  10915.         /* Comm line failure returns -1 thru myread, so no &= 0377 */
  10916.     n = myread();            /* Wait for a character... */
  10917.     /* debug(F000,"ttinc MYREAD n","",n); */
  10918. #ifdef CK_ENCRYPTION
  10919.     /* debug(F101,"ttinc u_encrypt","",TELOPT_U(TELOPT_ENCRYPTION)); */
  10920.     if (TELOPT_U(TELOPT_ENCRYPTION) && n >= 0) {
  10921.         ch = n;
  10922.         ck_tn_decrypt(&ch,1);
  10923.         n = ch;
  10924.     }
  10925. #endif /* CK_ENCRYPTION */
  10926.  
  10927. #ifdef NETPTY
  10928.     if (ttpty && n < 0) {
  10929.         debug(F101,"ttinc error on pty","",n);
  10930.         ttclos(0);
  10931.         return(n);
  10932.     }
  10933. #endif /* NETPTY */
  10934.  
  10935. #ifdef TNCODE
  10936.     if ((n > -1) && is_tn)
  10937.       return((unsigned)(n & 0xff));
  10938.     else
  10939. #endif /* TNCODE */
  10940.       return(n < 0 ? n : (unsigned)(n & ttpmsk));
  10941.  
  10942. #else  /* MYREAD */
  10943.  
  10944.         while ((n = read(fd,&ch,1)) == 0) /* Wait for a character. */
  10945.         /* Shouldn't have to loop in ver 5A. */
  10946. #ifdef NETCONN
  10947.       if (netconn) {        /* Special handling for net */
  10948.           netclos();        /* If read() returns 0 it means */
  10949.           netconn = 0;        /* the connection has dropped. */
  10950.           errno = ENOTCONN;
  10951.           return(-2);
  10952.       }
  10953. #endif /* NETCONN */
  10954.       ;
  10955.     /* debug(F101,"ttinc","",ch); */
  10956. #ifdef TNCODE
  10957.     if ((n > 0) && is_tn) {
  10958. #ifdef CK_ENCRYPTION
  10959.         if (TELOPT_U(TELOPT_ENCRYPTION)) {
  10960.         ck_tn_decrypt(&ch,1);
  10961.         n = ch;
  10962.         }
  10963. #endif /* CK_ENCRYPTION */
  10964.         return((unsigned)(ch & 0xff));
  10965.     } else
  10966. #endif /* TNCODE */
  10967.         return((n < 0) ? -4 : ((n == 0) ? -1 : (unsigned)(ch & ttpmsk)));
  10968. #endif /* MYREAD */
  10969.  
  10970.     } else {                /* Timed read */
  10971.  
  10972.     int oldalarm;
  10973.     saval = signal(SIGALRM,timerh);    /* Set up handler, save old one. */
  10974.     oldalarm = alarm(timo);        /* Set alarm, save old one. */
  10975.     if (
  10976. #ifdef CK_POSIX_SIG
  10977.         sigsetjmp(sjbuf,1)
  10978. #else
  10979.         setjmp(sjbuf)
  10980. #endif /* CK_POSIX_SIG */
  10981.         ) {                /* Timer expired */
  10982.         ttinctimo = 1;
  10983.         n = -1;            /* set flag */
  10984.     } else {
  10985. #ifdef MYREAD
  10986.         n = myread();        /* If managing own buffer... */
  10987.         debug(F101,"ttinc myread","",n);
  10988.         ch = n;
  10989. #else
  10990.         n = read(fd,&ch,1);        /* Otherwise call the system. */
  10991.         if (n == 0) n = -1;
  10992.         debug(F101,"ttinc read","",n);
  10993. #endif /* MYREAD */
  10994.  
  10995. #ifdef CK_ENCRYPTION
  10996.         if (TELOPT_U(TELOPT_ENCRYPTION) && n >= 0) {
  10997.         ck_tn_decrypt(&ch,1);
  10998.         }
  10999. #endif /* CK_ENCRYPTION */
  11000.         if (n >= 0)
  11001.           n = (unsigned) (ch & 0xff);
  11002.         else
  11003.           n = (n < 0) ? -4 : -2;    /* Special return codes. */
  11004.     }
  11005.     ttimoff();            /* Turn off the timer */
  11006.     if (oldalarm > 0) {
  11007.         if (n == -1)        /* and restore any previous alarm */
  11008.           oldalarm -= timo;
  11009.         if (oldalarm < 0)        /* adjusted by our timeout interval */
  11010.           oldalarm = 0;
  11011.         if (oldalarm) {
  11012.             debug(F101,"ttinc restoring oldalarm","",oldalarm);
  11013.         alarm(oldalarm);
  11014.         }
  11015.     }
  11016. #ifdef NETCONN
  11017.     if (netconn) {
  11018.         if (n == -2) {        /* read() returns 0 */
  11019.         netclos();        /* on network read failure */
  11020.         netconn = 0;
  11021.         errno = ENOTCONN;
  11022.         }
  11023.     }
  11024. #endif    /* NETCONN */
  11025. #ifdef TNCODE
  11026.     if ((n > -1) && is_tn)
  11027.       return((unsigned)(n & 0xff));
  11028.     else
  11029. #endif /* TNCODE */
  11030.       /* Return masked char or neg. */
  11031.       return( (n < 0) ? n : (unsigned)(n & ttpmsk) );
  11032.     }
  11033. }
  11034.  
  11035. /*  S N D B R K  --  Send a BREAK signal of the given duration  */
  11036.  
  11037. static int
  11038. #ifdef CK_ANSIC
  11039. sndbrk(int msec) {            /* Argument is milliseconds */
  11040. #else
  11041. sndbrk(msec) int msec; {
  11042. #endif /* CK_ANSIC */
  11043. #ifndef POSIX
  11044.     int x, n;
  11045. #endif /* POSIX */
  11046.  
  11047. #ifdef OXOS
  11048. #define BSDBREAK
  11049. #endif /* OXOS */
  11050.  
  11051. #ifdef ANYBSD
  11052. #define BSDBREAK
  11053. #endif /* ANYBSD */
  11054.  
  11055. #ifdef BSD44
  11056. #define BSDBREAK
  11057. #endif /* BSD44 */
  11058.  
  11059. #ifdef COHERENT
  11060. #ifdef BSDBREAK
  11061. #undef BSDBREAK
  11062. #endif /* BSDBREAK */
  11063. #endif /* COHERENT */
  11064.  
  11065. #ifdef BELLV10
  11066. #ifdef BSDBREAK
  11067. #undef BSDBREAK
  11068. #endif /* BSDBREAK */
  11069. #endif /* BELLV10 */
  11070.  
  11071. #ifdef PROVX1
  11072.     char spd;
  11073. #endif /* PROVX1 */
  11074.  
  11075.     debug(F101,"ttsndb ttyfd","",ttyfd);
  11076.     if (ttyfd < 0) return(-1);          /* Not open. */
  11077.  
  11078. #ifdef Plan9
  11079.     return p9sndbrk(msec);
  11080. #else
  11081. #ifdef NETCONN
  11082. #ifdef NETCMD
  11083.     if (ttpipe)                /* Pipe */
  11084.       return(ttoc('\0'));
  11085. #endif /* NETCMD */
  11086. #ifdef NETPTY
  11087.     if (ttpty)
  11088.       return(ttoc('\0'));
  11089. #endif /* NETPTY */
  11090.     if (netconn)             /* Send network BREAK */
  11091.       return(netbreak());
  11092. #endif /* NETCONN */
  11093.  
  11094.     if (msec < 1 || msec > 5000) return(-1); /* Bad argument */
  11095.  
  11096. #ifdef POSIX                /* Easy in POSIX */
  11097.     {
  11098.     int x;
  11099.     debug(F111,"sndbrk POSIX",ckitoa(msec),(msec/375));
  11100.     errno = 0;
  11101.     x = tcsendbreak(ttyfd,msec / 375);
  11102.     debug(F111,"sndbrk tcsendbreak",ckitoa(errno),x);
  11103.     return(x);
  11104.     }
  11105. #else
  11106. #ifdef PROVX1
  11107.     gtty(ttyfd,&ttbuf);                 /* Get current tty flags */
  11108.     spd = ttbuf.sg_ospeed;              /* Save speed */
  11109.     ttbuf.sg_ospeed = B50;              /* Change to 50 baud */
  11110.     stty(ttyfd,&ttbuf);                 /*  ... */
  11111.     n = (int)strlen(brnuls);        /* Send the right number of nulls */
  11112.     x = msec / 91;
  11113.     if (x > n) x = n;
  11114.     write(ttyfd,brnuls,n);
  11115.     ttbuf.sg_ospeed = spd;              /* Restore speed */
  11116.     stty(ttyfd,&ttbuf);                 /*  ... */
  11117.     return(0);
  11118. #else
  11119. #ifdef aegis
  11120.     sio_$control((short)ttyfd, sio_$send_break, msec, st);
  11121.     return(0);
  11122. #else
  11123. #ifdef BSDBREAK
  11124.     n = FWRITE;                         /* Flush output queue. */
  11125. /* Watch out for int vs long problems in &n arg! */
  11126.     debug(F101,"sndbrk BSDBREAK","",msec);
  11127.     ioctl(ttyfd,TIOCFLUSH,&n);          /* Ignore any errors.. */
  11128.     if (ioctl(ttyfd,TIOCSBRK,(char *)0) < 0) {  /* Turn on BREAK */
  11129.         perror("Can't send BREAK");
  11130.         return(-1);
  11131.     }
  11132.     x = msleep(msec);                    /* Sleep for so many milliseconds */
  11133.     if (ioctl(ttyfd,TIOCCBRK,(char *)0) < 0) {  /* Turn off BREAK */
  11134.         perror("BREAK stuck!!!");
  11135.         doexit(BAD_EXIT,-1);        /* Get out, closing the line. */
  11136.                                         /*   with bad exit status */
  11137.     }
  11138.     return(x);
  11139. #else
  11140. #ifdef ATTSV
  11141. /*
  11142.   No way to send a long BREAK in Sys V, so send a bunch of regular ones.
  11143.   (Actually, Sys V R4 is *supposed* to have the POSIX tcsendbreak() function,
  11144.   but there's no way for this code to know for sure.)
  11145. */
  11146.     debug(F101,"sndbrk ATTSV","",msec);
  11147.     x = msec / 275;
  11148.     for (n = 0; n < x; n++) {
  11149.     /* Reportedly the cast breaks this function on some systems */
  11150.     /* But then why was it here in the first place? */
  11151.     if (ioctl(ttyfd,TCSBRK, /* (char *) */ 0) < 0) {
  11152.         perror("Can't send BREAK");
  11153.         return(-1);
  11154.     }
  11155.     }
  11156.     return(0);
  11157. #else
  11158. #ifdef  V7
  11159.     debug(F101,"sndbrk V7","",msec);
  11160.     return(genbrk(ttyfd,250));        /* Simulate a BREAK */
  11161. #else
  11162.     debug(F101,"sndbrk catchall","",msec);
  11163.     ttoc(0);ttoc(0);ttoc(0);ttoc(0);
  11164.     return(0);
  11165. #endif /* V7 */
  11166. #endif /* BSDBREAK */
  11167. #endif /* ATTSV */
  11168. #endif /* aegis */
  11169. #endif /* PROVX1 */
  11170. #endif /* POSIX */
  11171. #endif /* Plan9 */
  11172. }
  11173.  
  11174. /*  T T S N D B  --  Send a BREAK signal  */
  11175.  
  11176. int
  11177. ttsndb() {
  11178. #ifdef TN_COMPORT
  11179.     if (netconn && istncomport())
  11180.       return((tnsndb(275L) >= 0) ? 0 : -1);
  11181.     else
  11182. #endif /* TN_COMPORT */
  11183.       return(sndbrk(275));
  11184. }
  11185.  
  11186. /*  T T S N D L B  --  Send a Long BREAK signal  */
  11187.  
  11188. int
  11189. ttsndlb() {
  11190. #ifdef TN_COMPORT
  11191.     if (netconn && istncomport())
  11192.       return((tnsndb(1800L) >= 0) ? 0 : -1);
  11193.     else
  11194. #endif /* TN_COMPORT */
  11195.     return(sndbrk(1500));
  11196. }
  11197.  
  11198. /*  M S L E E P  --  Millisecond version of sleep().  */
  11199.  
  11200. /*
  11201.   Call with number of milliseconds (thousandths of seconds) to sleep.
  11202.   Intended only for small intervals.  For big ones, just use sleep().
  11203.   Highly system-dependent.
  11204.   Returns 0 always, even if it didn't work.
  11205. */
  11206.  
  11207. /* Define MSLFTIME for systems that must use an ftime() loop. */
  11208. #ifdef ANYBSD                /* For pre-4.2 BSD versions */
  11209. #ifndef BSD4
  11210. #define MSLFTIME
  11211. #endif /* BSD4 */
  11212. #endif /* ANYBSD */
  11213.  
  11214. #ifdef TOWER1                /* NCR Tower OS 1.0 */
  11215. #define MSLFTIME
  11216. #endif /* TOWER1 */
  11217.  
  11218. #ifdef COHERENT         /* Coherent... */
  11219. #ifndef _I386           /* Maybe Coherent/386 should get this, too */
  11220. #define MSLFTIME        /* Opinions are divided */
  11221. #endif /* _I386 */
  11222. #endif /* COHERENT */
  11223.  
  11224. #ifdef COMMENT
  11225. #ifdef GETMSEC
  11226.  
  11227. /* Millisecond timer */
  11228.  
  11229. static long msecbase = 0L;        /* Unsigned long not portable */
  11230.  
  11231. long
  11232. getmsec() {                /* Milliseconds since base time */
  11233.     struct timeval xv;
  11234.     struct timezone xz;
  11235.     long secs, msecs;
  11236.     if (
  11237. #ifdef GTODONEARG
  11238.     gettimeofday(&tv)
  11239. #else
  11240. #ifdef PTX
  11241.     gettimeofday(&tv, NULL)
  11242. #else
  11243.     gettimeofday(&tv, &tz)
  11244. #endif /* PTX */
  11245. #endif /* GTODONEARG */
  11246.     < 0)
  11247.       return(-1);
  11248.     if (msecbase == 0L) {        /* First call, set base time. */
  11249.     msecbase = tv.tv_sec;
  11250.     debug(F101,"getmsec base","",msecbase);
  11251.     }
  11252.     return(((tv.tv_sec - msecbase) * 1000L) + (tv.tv_usec / 1000L));
  11253. }
  11254. #endif /* GETMSEC */
  11255. #endif /* COMMENT */
  11256.  
  11257. #ifdef SELECT
  11258. int
  11259. ttwait(fd, secs) int fd, secs; {
  11260.     int x;
  11261.     fd_set rfds;
  11262.     FD_ZERO(&rfds);
  11263.     FD_SET(fd,&rfds);
  11264.     tv.tv_sec = secs;
  11265.     tv.tv_usec = 0L;
  11266.     errno = 0;
  11267.     if ((x = select(FD_SETSIZE,
  11268. #ifdef HPUX9
  11269.             (int *)
  11270. #else
  11271. #ifdef HPUX1000
  11272.             (int *)
  11273. #endif /* HPUX1000 */
  11274. #endif /* HPUX9 */
  11275.             &rfds,
  11276.             0, 0, &tv)) < 0) {
  11277.     debug(F101,"ttwait select errno","",errno);
  11278.     return(0);
  11279.     } else {
  11280.     debug(F101,"ttwait OK","",errno);
  11281.     x = FD_ISSET(fd, &rfds);
  11282.     debug(F101,"ttwait select x","",x);
  11283.     return(x ? 1 : 0);
  11284.     }
  11285. }
  11286. #endif /* SELECT */
  11287.  
  11288. int
  11289. msleep(m) int m; {
  11290. /*
  11291.   Other possibilities here are:
  11292.    nanosleep(), reportedly defined in POSIX.4.
  11293.    sginap(), IRIX only (back to what IRIX version I don't know).
  11294. */
  11295. #ifdef Plan9
  11296.     return _SLEEP(m);
  11297. #else
  11298. #ifdef BEOSORBEBOX
  11299.     snooze(m*1000);
  11300. #else /* BEOSORBEBOX */
  11301. #ifdef SELECT
  11302.     int t1, x;
  11303.     debug(F101,"msleep SELECT 1","",m);
  11304.     if (m <= 0) return(0);
  11305.     if (m >= 1000) {            /* Catch big arguments. */
  11306.     sleep(m/1000);
  11307.     m = m % 1000;
  11308.     if (m < 10) return(0);
  11309.     }
  11310.     debug(F101,"msleep SELECT 2","",m);
  11311. #ifdef BELLV10
  11312.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, m );
  11313.     debug(F101,"msleep BELLV10 select","",x);
  11314. #else /* BELLV10 */
  11315. #ifdef HPUX9
  11316.     gettimeofday(&tv, &tz);
  11317. #else
  11318.  
  11319. #ifndef COHERENT
  11320. #ifdef GTODONEARG
  11321.     if (gettimeofday(&tv) < 0)
  11322. #else
  11323. #ifdef PTX
  11324.     if (gettimeofday(&tv,NULL) < 0)
  11325. #else
  11326. #ifdef NOTIMEZONE
  11327.     if (gettimeofday(&tv, NULL) < 0)    /* wonder what this does... */
  11328. #else
  11329.     if (gettimeofday(&tv, &tz) < 0)
  11330. #endif /* NOTIMEZONE */
  11331. #endif /* PTX */
  11332. #endif /* GTODONEARG */
  11333.       return(-1);
  11334.     t1 = tv.tv_sec;                     /* Seconds */
  11335. #endif /* COHERENT */
  11336. #endif /* HPUX9 */
  11337.     tv.tv_sec = 0;                      /* Use select() */
  11338.     tv.tv_usec = m * 1000L;
  11339. #ifdef BSD44
  11340.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11341.     debug(F101,"msleep BSD44 select","",x);
  11342. #else /* BSD44 */
  11343. #ifdef __linux__
  11344.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11345.     debug(F101,"msleep __linux__ select","",x);
  11346. #else /* __linux__ */
  11347. #ifdef BSD43
  11348.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11349.     debug(F101,"msleep BSD43 select","",x);
  11350. #else /* BSD43 */
  11351. #ifdef QNX6
  11352.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11353.     debug(F101,"msleep QNX6 select","",x);
  11354. #else /* QNX6 */
  11355. #ifdef QNX
  11356.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11357.     debug(F101,"msleep QNX select","",x);
  11358. #else /* QNX */
  11359. #ifdef COHERENT
  11360.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11361.     debug(F101,"msleep COHERENT select","",x);
  11362. #else /* COHERENT */
  11363. #ifdef HPUX1000                /* 10.00 only, not 10.10 or later */
  11364.     x = select( 0, (int *)0, (int *)0, (int *)0, &tv );
  11365.     debug(F101,"msleep HP-UX 10.00 select","",x);
  11366. #else /* HPUX1000 */
  11367. #ifdef SVR4
  11368.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11369.     debug(F101,"msleep SVR4 select","",x);
  11370. #else /* SVR4 */
  11371. #ifdef OSF40
  11372.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11373.     debug(F101,"msleep OSF40 select","",x);
  11374. #else /* OSF40 */
  11375. #ifdef PTX
  11376.     x = select( 0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
  11377.     debug(F101,"msleep OSF40 select","",x);
  11378. #else
  11379.     x = select( 0, (int *)0, (int *)0, (int *)0, &tv );
  11380.     debug(F101,"msleep catch-all select","",x);
  11381. #endif /* PTX */
  11382. #endif /* OSF40 */
  11383. #endif /* HP1000 */
  11384. #endif /* SVR4 */
  11385. #endif /* COHERENT */
  11386. #endif /* QNX */
  11387. #endif /* QNX6 */
  11388. #endif /* BSD43 */
  11389. #endif /* __linux__ */
  11390. #endif /* BSD44 */
  11391. #endif /* BELLV10 */
  11392.     return(0);
  11393.  
  11394. #else                    /* Not SELECT */
  11395. #ifdef CK_POLL                /* We have poll() */
  11396.     struct pollfd pfd;            /* Supply a valid address for poll() */
  11397.  
  11398. #ifdef ODT30                /* But in SCO ODT 3.0 */
  11399. #ifdef NAP                /* we should use nap() instead */
  11400.     debug(F101,"msleep ODT 3.0 NAP","",m); /* because using poll() here */
  11401.     nap((long)m);               /* seems to break dialing. */
  11402.     return(0);
  11403. #else
  11404.     debug(F101,"msleep ODT 3.0 POLL","",m);
  11405.     poll(&pfd, 0, m);
  11406.     return(0);
  11407. #endif /* NAP */
  11408. #else
  11409.     debug(F101,"msleep POLL","",m);
  11410.     poll(&pfd, 0, m);
  11411.     return(0);
  11412. #endif /* ODT30 */
  11413.  
  11414. /*
  11415.   We could handle the above more cleanly by just letting nap() always
  11416.   take precedence over poll() in this routine, but there is no way to know
  11417.   whether that would break something else.
  11418. */
  11419.  
  11420. #else                    /* Not POLL */
  11421. #ifdef USLEEP
  11422. /*
  11423.   "This routine is implemented using setitimer(2); it requires eight
  11424.   system calls...".  In other words, it might take 5 minutes to sleep
  11425.   10 milliseconds...
  11426. */
  11427.     debug(F101,"msleep USLEEP","",m);
  11428.     if (m >= 1000) {            /* Catch big arguments. */
  11429.     sleep(m/1000);
  11430.     m = m % 1000;
  11431.     if (m < 10) return(0);
  11432.     }
  11433.     usleep((unsigned int)(m * 1000));
  11434.     return(0);
  11435. #else
  11436. #ifdef aegis
  11437.     time_$clock_t dur;
  11438.     debug(F101,"msleep aegis","",m);
  11439.     dur.c2.high16 = 0;
  11440.     dur.c2.low32  = 250 * m; /* one millisecond = 250 four microsecond ticks */
  11441.     time_$wait(time_$relative, dur, st);
  11442.     return(0);
  11443. #else
  11444. #ifdef PROVX1
  11445.     debug(F101,"msleep Venix","",m);
  11446.     if (m <= 0) return(0);
  11447.     sleep(-((m * 60 + 500) / 1000));
  11448.     return(0);
  11449. #else
  11450. #ifdef NAP
  11451.     debug(F101,"msleep NAP","",m);
  11452.     nap((long)m);
  11453.     return(0);
  11454. #else
  11455. #ifdef ATTSV
  11456. #ifndef BSD44
  11457.     extern long times();        /* Or #include <times.h> ? */
  11458. #endif /* BSD44 */
  11459.     long t1, t2, tarray[4];
  11460.     int t3;
  11461.     char *cp = getenv("HZ");
  11462.     int CLOCK_TICK;
  11463.     int hertz;
  11464.  
  11465.     if (cp && (hertz = atoi(cp))) {
  11466.         CLOCK_TICK  = 1000 / hertz;
  11467.     } else {                /* probably single user mode */
  11468. #ifdef HZ
  11469.         CLOCK_TICK  = 1000 / HZ;
  11470. #else
  11471.     static warned = 0;
  11472.     /* HZ always exists in, for instance, SCO Xenix, so you don't have to
  11473.      * make special #ifdefs for XENIX here, like in ver 4F. Also, if you
  11474.      * have Xenix, you have should have nap(), so the best is to use -DNAP
  11475.      * in the makefile. Most systems have HZ.
  11476.      */
  11477.     CLOCK_TICK = 17;        /* 1/60 sec */
  11478.     if (!warned) {
  11479.           printf("warning: environment variable HZ bad... using HZ=%d\r\n",
  11480.          1000 / CLOCK_TICK);
  11481.           warned = 1;
  11482.     }
  11483. #endif /* !HZ */
  11484.     }
  11485.     debug(F101,"msleep ATTSV","",m);
  11486.     if (m <= 0) return(0);
  11487.     if (m >= 1000) {            /* Catch big arguments. */
  11488.     sleep(m/1000);
  11489.     m = m % 1000;
  11490.     if (m < 10) return(0);
  11491.     }
  11492.     if ((t1 = times(tarray)) < 0) return(-1);
  11493.     while (1) {
  11494.         if ((t2 = times(tarray)) < 0) return(-1);
  11495.         t3 = ((int)(t2 - t1)) * CLOCK_TICK;
  11496.         if (t3 > m) return(t3);
  11497.     }
  11498. #else /* Not ATTSV */
  11499. #ifdef MSLFTIME                /* Use ftime() loop... */
  11500.     int t1, t3 = 0;
  11501.     debug(F101,"msleep MSLFTIME","",m);
  11502.     if (m <= 0) return(0);
  11503.     if (m >= 1000) {            /* Catch big arguments. */
  11504.     sleep(m/1000);
  11505.     m = m % 1000;
  11506.     if (m < 10) return(0);
  11507.     }
  11508. #ifdef QNX
  11509.     ftime(&ftp);            /* void ftime() in QNX */
  11510. #else
  11511.     if (ftime(&ftp) < 0) return(-1);    /* Get base time. */
  11512. #endif /* QNX */
  11513.     t1 = ((ftp.time & 0xff) * 1000) + ftp.millitm;
  11514.     while (1) {
  11515.         ftime(&ftp);            /* Get current time and compare. */
  11516.         t3 = (((ftp.time & 0xff) * 1000) + ftp.millitm) - t1;
  11517.         if (t3 > m) return(0);
  11518.     }
  11519. #else
  11520. /* This includes true POSIX, which has no way to do this. */
  11521.     debug(F101,"msleep busy loop","",m);
  11522.     if (m >= 1000) {            /* Catch big arguments. */
  11523.     sleep(m/1000);
  11524.     m = m % 1000;
  11525.     if (m < 10) return(0);
  11526.     }
  11527.     if (m > 0) while (m > 0) m--;    /* Just a dumb busy loop */
  11528.     return(0);
  11529. #endif /* MSLFTIME */
  11530. #endif /* ATTSV */
  11531. #endif /* NAP */
  11532. #endif /* PROVX1 */
  11533. #endif /* aegis */
  11534. #endif /* CK_POLL */
  11535. #endif /* SELECT */
  11536. #endif /* BEOSORBEBOX */
  11537. #endif /* USLEEP */
  11538. #endif /* Plan9 */
  11539. }
  11540.  
  11541. /*  R T I M E R --  Reset elapsed time counter  */
  11542.  
  11543. VOID
  11544. rtimer() {
  11545.     tcount = time( (time_t *) 0 );
  11546. }
  11547.  
  11548.  
  11549. /*  G T I M E R --  Get current value of elapsed time counter in seconds  */
  11550.  
  11551. int
  11552. gtimer() {
  11553.     int x;
  11554.     x = (int) (time( (time_t *) 0 ) - tcount);
  11555.     debug(F101,"gtimer","",x);
  11556.     return( (x < 0) ? 0 : x );
  11557. }
  11558.  
  11559. #ifdef GFTIMER
  11560. /*
  11561.   Floating-point timers.  Require not only floating point support, but
  11562.   also gettimeofday().
  11563. */
  11564. static struct timeval tzero;
  11565.  
  11566. VOID
  11567. rftimer() {
  11568. #ifdef GTODONEARG    /* Account for Mot's definition */
  11569.     (VOID) gettimeofday(&tzero);
  11570. #else
  11571.     (VOID) gettimeofday(&tzero, (struct timezone *)0);
  11572. #endif /* GTODONEARG */
  11573. }
  11574.  
  11575. CKFLOAT
  11576. gftimer() {
  11577.     struct timeval tnow, tdelta;
  11578.     CKFLOAT s;
  11579. #ifdef DEBUG
  11580.     char fpbuf[64];
  11581. #endif /* DEBUG */
  11582. #ifdef GTODONEARG    /* Acount for Mot's definition */
  11583.     (VOID) gettimeofday(&tnow);
  11584. #else
  11585.     (VOID) gettimeofday(&tnow, (struct timezone *)0);
  11586. #endif /* GTODONEARG */
  11587.     tdelta.tv_sec = tnow.tv_sec - tzero.tv_sec;
  11588.     tdelta.tv_usec = tnow.tv_usec - tzero.tv_usec;
  11589.     if (tdelta.tv_usec < 0) {
  11590.     tdelta.tv_sec--;
  11591.     tdelta.tv_usec += 1000000;
  11592.     }
  11593.     s = (CKFLOAT) tdelta.tv_sec + ((CKFLOAT) tdelta.tv_usec / 1000000.0);
  11594.     if (s < GFMINTIME)
  11595.       s = GFMINTIME;
  11596. #ifdef DEBUG
  11597.     if (deblog) {
  11598.     sprintf(fpbuf,"%f",s);
  11599.     debug(F110,"gftimer",fpbuf,0);
  11600.     }
  11601. #endif /* DEBUG */
  11602.     return(s);
  11603. }
  11604. #endif /* GFTIMER */
  11605.  
  11606. /*  Z T I M E  --  Return asctime()-format date/time string  */
  11607. /*
  11608.   NOTE: as a side effect of calling this routine, we can also set the
  11609.   following two variables, giving the micro- and milliseconds (fractions of
  11610.   seconds) of the clock time.  Currently this is done only in BSD-based builds
  11611.   that use gettimeofday().  When these variables are not filled in, they are
  11612.   left with a value of -1L.
  11613. */
  11614. static char asctmbuf[64];
  11615.  
  11616. VOID
  11617. ztime(s) char **s; {
  11618.  
  11619. #ifdef GFTIMER
  11620. /*
  11621.   The gettimeofday() method, which also sets ztmsec and ztusec, works for
  11622.   all GFTIMER builds.  NOTE: ztmsec and ztusec are defined in ckcmai.c,
  11623.   and extern declarations for them are in ckcdeb.h; thus they are
  11624.   declared in this file by inclusion of ckcdeb.h.
  11625. */
  11626.     char *asctime();
  11627.     struct tm *localtime();
  11628.     struct tm *tp;
  11629.     ztmsec = -1L;
  11630.     ztusec = -1L;
  11631.  
  11632.     if (!s)
  11633.       debug(F100,"ztime s==NULL","",0);
  11634.  
  11635. #ifdef GTODONEARG
  11636.     /* No 2nd arg in Motorola SV88 and some others */
  11637.     if (gettimeofday(&tv) > -1)
  11638. #else
  11639. #ifndef COHERENT
  11640. #ifdef PTX
  11641.     if (gettimeofday(&tv,NULL) > -1)
  11642. #else
  11643. #ifdef NOTIMEZONE
  11644.     if (gettimeofday(&tv, NULL) > -1)    /* wonder what this does... */
  11645. #else
  11646.     if (gettimeofday(&tv, &tz) > -1)
  11647. #endif /* NOTIMEZONE */
  11648. #endif /* PTX */
  11649. #endif /* COHERENT */
  11650. #endif /* GTODONEARG */
  11651.       {                    /* Fill in tm struct */
  11652.     ztusec = tv.tv_usec;        /* Microseconds */
  11653.     ztmsec = ztusec / 1000L;    /* Milliseconds */
  11654. #ifdef HPUX9
  11655.     {
  11656.         time_t zz;
  11657.         zz = tv.tv_sec;
  11658.         tp = localtime(&zz);    /* Convert to local time */
  11659.     }
  11660. #else
  11661. #ifdef HPUX1000
  11662.     {
  11663.         time_t zz;
  11664.         zz = tv.tv_sec;
  11665.         tp = localtime(&zz);
  11666.     }
  11667. #else
  11668. #ifdef LINUX
  11669.     {   /* avoid unaligned access trap on 64-bit platforms */
  11670.         time_t zz;
  11671.         zz = tv.tv_sec;
  11672.         tp = localtime(&zz);
  11673.     }
  11674. #else
  11675. #ifdef MACOSX
  11676.     tp = localtime((time_t *)&tv.tv_sec); /* Convert to local time */
  11677. #else
  11678.     tp = localtime(&tv.tv_sec);
  11679. #endif /* MACOSX */
  11680. #endif /* LINUX */
  11681. #endif /* HPUX1000 */
  11682. #endif /* HPUX9 */
  11683.     if (s) {
  11684.         char * s2;
  11685.         s2 = asctime(tp);        /* Convert result to ASCII string */
  11686.         asctmbuf[0] = '\0';
  11687.         if (s2) ckstrncpy(asctmbuf,s2,64);
  11688.         *s = asctmbuf;
  11689.         debug(F111,"ztime GFTIMER gettimeofday",*s,ztusec);
  11690.     }
  11691.     }
  11692. #else  /* Not GFTIMER */
  11693.  
  11694. #undef ZTIMEV7                /* Which systems need to use */
  11695. #ifdef COHERENT                /* old UNIX Version 7 way... */
  11696. #define ZTIMEV7
  11697. #endif /* COHERENT */
  11698. #ifdef TOWER1
  11699. #define ZTIMEV7
  11700. #endif /* TOWER1 */
  11701. #ifdef ANYBSD
  11702. #ifndef BSD42
  11703. #define ZTIMEV7
  11704. #endif /* BSD42 */
  11705. #endif /* ANYBSD */
  11706. #ifdef V7
  11707. #ifndef MINIX
  11708. #define ZTIMEV7
  11709. #endif /* MINIX */
  11710. #endif /* V7 */
  11711. #ifdef POSIX
  11712. #define ZTIMEV7
  11713. #endif /* POSIX */
  11714.  
  11715. #ifdef HPUX1020
  11716. /*
  11717.   Prototypes are in <time.h>, included above.
  11718. */
  11719.     time_t clock_storage;
  11720.     clock_storage = time((void *) 0);
  11721.     if (s) {
  11722.     *s = ctime(&clock_storage);
  11723.     debug(F110,"ztime: HPUX 10.20",*s,0);
  11724.     }
  11725. #else
  11726. #ifdef ATTSV                /* AT&T way */
  11727. /*  extern long time(); */        /* Theoretically these should */
  11728.     char *ctime();            /* already been dcl'd in <time.h> */
  11729.     time_t clock_storage;
  11730.     clock_storage = time(
  11731. #ifdef IRIX60
  11732.              (time_t *)
  11733. #else
  11734. #ifdef BSD44
  11735.              (time_t *)
  11736. #else
  11737.              (long *)
  11738. #endif /* BSD44 */
  11739. #endif /* IRIX60 */
  11740.              0 );
  11741.     if (s) {
  11742.     *s = ctime( &clock_storage );
  11743.     debug(F110,"ztime: ATTSV",*s,0);
  11744.     }
  11745. #else
  11746. #ifdef PROVX1                /* Venix 1.0 way */
  11747.     int utime[2];
  11748.     time(utime);
  11749.     if (s) {
  11750.     *s = ctime(utime);
  11751.     debug(F110,"ztime: PROVX1",*s,0);
  11752.     }
  11753. #else
  11754. #ifdef BSD42                /* 4.2BSD way */
  11755.     char *asctime();
  11756.     struct tm *localtime();
  11757.     struct tm *tp;
  11758.     gettimeofday(&tv, &tz);
  11759.     ztusec = tv.tv_usec;
  11760.     ztmsec = tv.tv_usec / 1000L;
  11761.     tp = localtime(&tv.tv_sec);
  11762.     if (s) {
  11763.     *s = asctime(tp);
  11764.     debug(F111,"ztime: BSD42",*s,ztusec);
  11765.     }
  11766. #else
  11767. #ifdef MINIX                /* MINIX way */
  11768. #ifdef COMMENT
  11769.     extern long time();            /* Already got these from <time.h> */
  11770.     extern char *ctime();
  11771. #endif /* COMMENT */
  11772.     time_t utime[2];
  11773.     time(utime);
  11774.     if (s) {
  11775.     *s = ctime(utime);
  11776.     debug(F110,"ztime: MINIX",*s,0);
  11777.     }
  11778. #else
  11779. #ifdef ZTIMEV7                /* The regular way */
  11780.     char *asctime();
  11781.     struct tm *localtime();
  11782.     struct tm *tp;
  11783.     long xclock;            /* or unsigned long for BeBox? */
  11784.     time(&xclock);
  11785.     tp = localtime(&xclock);
  11786.     if (s) {
  11787.     *s = asctime(tp);
  11788.     debug(F110,"ztime: ZTIMEV7",*s,0);
  11789.     }
  11790. #else                    /* Catch-all for others... */
  11791.     if (s) {
  11792.     *s = "Day Mon 00 00:00:00 0000\n"; /* Dummy in asctime() format */
  11793.     debug(F110,"ztime: catch-all",*s,0);
  11794.     }
  11795. #endif /* ZTIMEV7 */
  11796. #endif /* MINIX */
  11797. #endif /* BSD42 */
  11798. #endif /* PROVX1 */
  11799. #endif /* ATTSV */
  11800. #endif /* HPUX1020 */
  11801. #endif /* GFTIMER */
  11802. }
  11803.  
  11804. /*  C O N G M  --  Get console terminal modes.  */
  11805.  
  11806. /*
  11807.   Saves initial console mode, and establishes variables for switching
  11808.   between current (presumably normal) mode and other modes.
  11809.   Should be called when program starts, but only after establishing
  11810.   whether program is in the foreground or background.
  11811.   Returns 1 if it got the modes OK, 0 if it did nothing, -1 on error.
  11812. */
  11813. int
  11814. congm() {
  11815.     int fd;
  11816.     if (backgrd || !isatty(0)) {    /* If in background. */
  11817.     cgmf = -1;            /* Don't bother, modes are garbage. */
  11818.     return(-1);
  11819.     }
  11820.     if (cgmf > 0) return(0);        /* Already did this. */
  11821.     debug(F100,"congm getting modes","",0); /* Need to do it. */
  11822. #ifdef aegis
  11823.     ios_$inq_type_uid(ios_$stdin, conuid, st);
  11824.     if (st.all != status_$ok) {
  11825.     fprintf(stderr, "problem getting stdin objtype: ");
  11826.     error_$print(st);
  11827.     }
  11828.     concrp = (conuid == mbx_$uid);
  11829.     conbufn = 0;
  11830. #endif /* aegis */
  11831.  
  11832. #ifndef BEBOX
  11833.     if ((fd = open(CTTNAM,2)) < 0) {    /* Open controlling terminal */
  11834. #ifdef COMMENT
  11835.     fprintf(stderr,"Error opening %s\n", CTTNAM);
  11836.     perror("congm");
  11837.     return(-1);
  11838. #else
  11839.     fd = 0;
  11840. #endif /* COMMENT */
  11841.     }
  11842. #else
  11843.     fd = 0;
  11844. #endif /* !BEBOX */
  11845. #ifdef BSD44ORPOSIX
  11846.     if (tcgetattr(fd,&ccold) < 0) return(-1);
  11847.     if (tcgetattr(fd,&cccbrk) < 0) return(-1);
  11848.     if (tcgetattr(fd,&ccraw) < 0) return(-1);
  11849. #else
  11850. #ifdef ATTSV
  11851.     if (ioctl(fd,TCGETA,&ccold)  < 0) return(-1);
  11852.     if (ioctl(fd,TCGETA,&cccbrk) < 0) return(-1);
  11853.     if (ioctl(fd,TCGETA,&ccraw)  < 0) return(-1);
  11854. #ifdef VXVE
  11855.     cccbrk.c_line = 0;            /* STTY line 0 for CDC VX/VE */
  11856.     if (ioctl(fd,TCSETA,&cccbrk) < 0) return(-1);
  11857.     ccraw.c_line = 0;            /* STTY line 0 for CDC VX/VE */
  11858.     if (ioctl(fd,TCSETA,&ccraw) < 0) return(-1);
  11859. #endif /* VXVE */
  11860. #else
  11861. #ifdef BELLV10
  11862.     if (ioctl(fd,TIOCGETP,&ccold) < 0) return(-1);
  11863.     if (ioctl(fd,TIOCGETP,&cccbrk) < 0) return(-1);
  11864.     if (ioctl(fd,TIOCGETP,&ccraw) < 0) return(-1);
  11865.     debug(F101,"cccbrk.sg_flags orig","", cccbrk.sg_flags);
  11866. #else
  11867.     if (gtty(fd,&ccold) < 0) return(-1);
  11868.     if (gtty(fd,&cccbrk) < 0) return(-1);
  11869.     if (gtty(fd,&ccraw) < 0) return(-1);
  11870. #endif /* BELLV10 */
  11871. #endif /* ATTSV */
  11872. #endif /* BSD44ORPOSIX */
  11873. #ifdef sony_news            /* Sony NEWS */
  11874.     if (ioctl(fd,TIOCKGET,&km_con) < 0) { /* Get console Kanji mode */
  11875.     perror("congm error getting Kanji mode");
  11876.     debug(F101,"congm error getting Kanji mode","",0);
  11877.     km_con = -1;            /* Make sure this stays undefined. */
  11878.     return(-1);
  11879.     }
  11880. #endif /* sony_news */
  11881.     if (fd > 0)
  11882.       close(fd);
  11883.     cgmf = 1;                /* Flag that we got them. */
  11884.     return(1);
  11885. }
  11886.  
  11887.  
  11888. static VOID
  11889. congetbuf(x) int x; {
  11890.     int n;
  11891.     n = CONBUFSIZ - (conbufp - conbuf);    /* How much room left in buffer? */
  11892.     if (x > n) {
  11893.     debug(F101,"congetbuf char loss","",x-n);
  11894.     x = n;
  11895.     }
  11896.     x = read(0,conbufp,x);
  11897.     conbufn += x;
  11898.     debug(F111,"congetbuf readahead",conbuf,x);
  11899. }
  11900.  
  11901.  
  11902. /*  C O N C B --  Put console in cbreak mode.  */
  11903.  
  11904. /*  Returns 0 if ok, -1 if not  */
  11905.  
  11906. int
  11907. #ifdef CK_ANSIC
  11908. concb(char esc)
  11909. #else
  11910. concb(esc) char esc;
  11911. #endif /* CK_ANSIC */
  11912. /* concb */ {
  11913.     int x;
  11914.     debug(F101,"concb constate","",constate);
  11915.     debug(F101,"concb cgmf","",cgmf);
  11916.     debug(F101,"concb backgrd","",backgrd);
  11917.  
  11918.     if (constate == CON_CB)
  11919.       return(0);
  11920.  
  11921.     if (cgmf < 1)            /* Did we get console modes yet? */
  11922.       if (!backgrd)            /* No, in background? */
  11923.     congm();            /* No, try to get them now. */
  11924.     if (cgmf < 1)            /* Still don't have them? */
  11925.       return(0);            /* Give up. */
  11926.     debug(F101,"concb ttyfd","",ttyfd);
  11927.     debug(F101,"concb ttfdflg","",ttfdflg);
  11928. #ifdef COMMENT
  11929.     /* This breaks returning to prompt after protocol with "-l 0" */
  11930.     /* Commented out July 1998 */
  11931.     if (ttfdflg && ttyfd >= 0 && ttyfd < 3)
  11932.       return(0);
  11933. #endif /* COMMENT */
  11934.     x = isatty(0);
  11935.     debug(F101,"concb isatty","",x);
  11936.     if (!x) return(0);            /* Only when running on real ttys */
  11937.     debug(F101,"concb suspend","",suspend);
  11938.     if (backgrd)            /* Do nothing if in background. */
  11939.       return(0);
  11940.     escchr = esc;                       /* Make this available to other fns */
  11941.     ckxech = 1;                         /* Program can echo characters */
  11942. #ifdef aegis
  11943.     conbufn = 0;
  11944.     if (concrp) return(write(1, "\035\002", 2));
  11945.     if (conuid == input_pad_$uid) {pad_$raw(ios_$stdin, st); return(0);}
  11946. #endif /* aegis */
  11947.  
  11948. #ifdef COHERENT
  11949. #define SVORPOSIX
  11950. #endif /* COHERENT */
  11951.  
  11952. #ifdef Plan9
  11953.     x = p9concb();
  11954. #else
  11955. #ifndef SVORPOSIX            /* BSD, V7, etc */
  11956.     debug(F101,"cccbrk.sg_flags concb 1","", cccbrk.sg_flags);
  11957.     debug(F101,"concb stty CBREAK","",0);
  11958.     cccbrk.sg_flags |= (CBREAK|CRMOD);    /* Set to character wakeup, */
  11959.     cccbrk.sg_flags &= ~ECHO;           /* no echo. */
  11960.     debug(F101,"cccbrk.sg_flags concb 2","", cccbrk.sg_flags);
  11961.     errno = 0;
  11962. /*
  11963.   BSD stty() clears the console buffer.  So if anything is waiting in it,
  11964.   we have to read it now to avoid losing it.
  11965. */
  11966.     x = conchk();
  11967.     if (x > 0)
  11968.       congetbuf(x);
  11969.  
  11970. #ifdef BELLV10
  11971.     x = ioctl(0,TIOCSETP,&cccbrk);
  11972. #else
  11973.     x = stty(0,&cccbrk);
  11974.     debug(F101,"cccbrk.sg_flags concb x","", x);
  11975. #endif /* BELLV10 */
  11976. #else                    /* Sys V and POSIX */
  11977. #ifndef OXOS
  11978.     debug(F101,"concb cccbrk.c_flag","",cccbrk.c_lflag);
  11979. #ifdef QNX
  11980.     /* Don't mess with IEXTEN */
  11981.     cccbrk.c_lflag &= ~(ICANON|ECHO);
  11982. #else
  11983. #ifdef COHERENT
  11984.     cccbrk.c_lflag &= ~(ICANON|ECHO);
  11985. #else
  11986.     cccbrk.c_lflag &= ~(ICANON|ECHO|IEXTEN);
  11987. #endif /* COHERENT */
  11988. #endif /* QNX */
  11989.     cccbrk.c_lflag |= ISIG;        /* Allow signals in command mode. */
  11990.     cccbrk.c_iflag |= IGNBRK;        /* But ignore BREAK signal */
  11991.     cccbrk.c_iflag &= ~BRKINT;
  11992.  
  11993. #else /* OXOS */
  11994.     debug(F100,"concb OXOS is defined","",0);
  11995.     cccbrk.c_lflag &= ~(ICANON|ECHO);
  11996.     cccbrk.c_cc[VDISCARD] = cccbrk.c_cc[VLNEXT] = CDISABLE;
  11997. #endif /* OXOS */
  11998. #ifdef COMMENT
  11999. /*
  12000.   Believe it or not, in SCO UNIX, VSUSP is greater than NCC, and so this
  12001.   array reference is out of bounds.  It's only a debug() call so who needs it.
  12002. */
  12003. #ifdef VSUSP
  12004.     debug(F101,"concb c_cc[VSUSP]","",cccbrk.c_cc[VSUSP]);
  12005. #endif /* VSUSP */
  12006. #endif /* COMMENT */
  12007. #ifndef VINTR
  12008.     debug(F101,"concb c_cc[0]","",cccbrk.c_cc[0]);
  12009.     cccbrk.c_cc[0] = 003;               /* Interrupt char is Control-C */
  12010. #else
  12011.     debug(F101,"concb c_cc[VINTR]","",cccbrk.c_cc[0]);
  12012.     cccbrk.c_cc[VINTR] = 003;
  12013. #endif /* VINTR */
  12014. #ifndef VQUIT
  12015.     cccbrk.c_cc[1] = escchr;            /* escape during packet modes */
  12016. #else
  12017.     cccbrk.c_cc[VQUIT] = escchr;
  12018. #endif /* VQUIT */
  12019. #ifndef VEOF
  12020.     cccbrk.c_cc[4] = 1;
  12021. #else
  12022. #ifndef OXOS
  12023. #ifdef VMIN
  12024.     cccbrk.c_cc[VMIN] = 1;
  12025. #endif /* VMIN */
  12026. #else /* OXOS */
  12027.     cccbrk.c_min = 1;
  12028. #endif /* OXOS */
  12029. #endif /* VEOF */
  12030. #ifdef ZILOG
  12031.     cccbrk.c_cc[5] = 0;
  12032. #else
  12033. #ifndef VEOL
  12034.     cccbrk.c_cc[5] = 1;
  12035. #else
  12036. #ifndef OXOS
  12037. #ifdef VTIME
  12038.     cccbrk.c_cc[VTIME] = 1;
  12039. #endif /* VTIME */
  12040. #else /* OXOS */
  12041.     cccbrk.c_time = 1;
  12042. #endif /* OXOS */
  12043. #endif /* VEOL */
  12044. #endif /* ZILOG */
  12045.     errno = 0;
  12046. #ifdef BSD44ORPOSIX            /* Set new modes */
  12047.     x = tcsetattr(0,TCSADRAIN,&cccbrk);
  12048. #else /* ATTSV */                  /* or the POSIX way */
  12049.     x = ioctl(0,TCSETAW,&cccbrk);    /* the Sys V way */
  12050. #endif /* BSD44ORPOSIX */
  12051. #endif /* SVORPOSIX */
  12052.  
  12053. #ifdef COHERENT
  12054. #undef SVORPOSIX
  12055. #endif /* COHERENT */
  12056.     debug(F101,"concb x","",x);
  12057.     debug(F101,"concb errno","",errno);
  12058. #ifdef NONOSETBUF
  12059.     if (x > -1) {
  12060.     setbuf(stdout,NULL);    /* Make console unbuffered. */
  12061.     debug(F100,"concb setbuf A","",0);
  12062.     }
  12063. #else
  12064. #ifndef aegis
  12065. #ifndef NOSETBUF
  12066.     if (x > -1) {
  12067.     setbuf(stdout,NULL);    /* Make console unbuffered. */
  12068.     debug(F100,"concb setbuf B","",0);
  12069.     }
  12070. #endif /* NOSETBUF */
  12071. #endif /* aegis */
  12072. #endif /* NONOSETBUF */
  12073.  
  12074. #ifdef  V7
  12075. #ifndef MINIX
  12076.     if (kmem[CON] < 0) {
  12077.         qaddr[CON] = initrawq(0);
  12078.         if((kmem[CON] = open("/dev/kmem", 0)) < 0) {
  12079.             fprintf(stderr, "Can't read /dev/kmem in concb.\n");
  12080.             perror("/dev/kmem");
  12081.             exit(1);
  12082.         }
  12083.     }
  12084. #endif /* MINIX */
  12085. #endif /* V7 */
  12086. #endif /* Plan9 */
  12087.  
  12088.     if (x > -1)
  12089.       constate = CON_CB;
  12090.  
  12091.     debug(F101,"concb returns","",x);
  12092.     return(x);
  12093. }
  12094.  
  12095. /*  C O N B I N  --  Put console in binary mode  */
  12096.  
  12097. /*  Returns 0 if ok, -1 if not  */
  12098.  
  12099. int
  12100. #ifdef CK_ANSIC
  12101. conbin(char esc)
  12102. #else
  12103. conbin(esc) char esc;
  12104. #endif /* CK_ANSIC */
  12105. /* conbin */  {
  12106.  
  12107.     int x;
  12108.  
  12109.     debug(F101,"conbin constate","",constate);
  12110.  
  12111.     if (constate == CON_BIN)
  12112.       return(0);
  12113.  
  12114.     if (!isatty(0)) return(0);          /* only for real ttys */
  12115.     congm();                /* Get modes if necessary. */
  12116.     debug(F100,"conbin","",0);
  12117.     escchr = esc;                       /* Make this available to other fns */
  12118.     ckxech = 1;                         /* Program can echo characters */
  12119. #ifdef aegis
  12120.     conbufn = 0;
  12121.     if (concrp) return(write(1, "\035\002", 2));
  12122.     if (conuid == input_pad_$uid) {
  12123.     pad_$raw(ios_$stdin, st);
  12124.     return(0);
  12125.       }
  12126. #endif /* aegis */
  12127.  
  12128. #ifdef COHERENT
  12129. #define SVORPOSIX
  12130. #endif /* COHERENT */
  12131.  
  12132. #ifdef Plan9
  12133.     return p9conbin();
  12134. #else
  12135. #ifdef SVORPOSIX
  12136. #ifndef OXOS
  12137. #ifdef QNX
  12138.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO);
  12139. #else
  12140. #ifdef COHERENT
  12141.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO);
  12142. #else
  12143.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
  12144. #endif /* COHERENT */
  12145. #endif /* QNX */
  12146. #else /* OXOS */
  12147.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO);
  12148.     ccraw.c_cc[VDISCARD] = ccraw.c_cc[VLNEXT] = CDISABLE;
  12149. #endif /* OXOS */
  12150.     ccraw.c_iflag |= IGNPAR;
  12151. /*
  12152.   Note that for terminal sessions we disable Xon/Xoff flow control to allow
  12153.   the passage ^Q and ^S as data characters for EMACS, and to allow XMODEM
  12154.   transfers to work when C-Kermit is in the middle, etc.  Hardware flow
  12155.   control, if in use, is not affected.
  12156. */
  12157. #ifdef ATTSV
  12158. #ifdef BSD44
  12159.     ccraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IXON|IXANY|IXOFF
  12160.                         |INPCK|ISTRIP);
  12161. #else
  12162.     ccraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IUCLC|IXON|IXANY|IXOFF
  12163.                         |INPCK|ISTRIP);
  12164. #endif /* BSD44 */
  12165. #else /* POSIX */
  12166.     ccraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IXON|IXOFF|INPCK|ISTRIP);
  12167. #endif /* ATTSV */
  12168.     ccraw.c_oflag &= ~OPOST;
  12169. #ifdef COMMENT
  12170. /*
  12171.   WHAT THE HECK WAS THIS FOR?
  12172.   The B9600 setting (obviously) prevents CONNECT from working at any
  12173.   speed other than 9600 when you are logged in to the 7300 on a serial
  12174.   line.  Maybe some of the other flags are necessary -- if so, put back
  12175.   the ones that are needed.  This code is supposed to work the same, no
  12176.   matter whether you are logged in to the 7300 on the real console device,
  12177.   or through a serial port.
  12178. */
  12179. #ifdef ATT7300
  12180.     ccraw.c_cflag = CLOCAL | B9600 | CS8 | CREAD | HUPCL;
  12181. #endif /* ATT7300 */
  12182. #endif /* COMMENT */
  12183.  
  12184. /*** Kermit used to put the console in 8-bit raw mode, but some users have
  12185.  *** pointed out that this should not be done, since some sites actually
  12186.  *** use terminals with parity settings on their Unix systems, and if we
  12187.  *** override the current settings and stop doing parity, then their terminals
  12188.  *** will display blotches for characters whose parity is wrong.  Therefore,
  12189.  *** the following two lines are commented out (Larry Afrin, Clemson U):
  12190.  ***
  12191.  ***   ccraw.c_cflag &= ~(PARENB|CSIZE);
  12192.  ***   ccraw.c_cflag |= (CS8|CREAD);
  12193.  ***
  12194.  *** Sys III/V sites that have trouble with this can restore these lines.
  12195.  ***/
  12196. #ifndef VINTR
  12197.     ccraw.c_cc[0] = 003;        /* Interrupt char is Ctrl-C */
  12198. #else
  12199.     ccraw.c_cc[VINTR] = 003;
  12200. #endif /* VINTR */
  12201. #ifndef VQUIT
  12202.     ccraw.c_cc[1] = escchr;        /* Escape during packet mode */
  12203. #else
  12204.     ccraw.c_cc[VQUIT] = escchr;
  12205. #endif /* VQUIT */
  12206. #ifndef VEOF
  12207.     ccraw.c_cc[4] = 1;
  12208. #else
  12209. #ifndef OXOS
  12210. #ifdef VMIN
  12211.     ccraw.c_cc[VMIN] = 1;
  12212. #endif /* VMIN */
  12213. #else /* OXOS */
  12214.     ccraw.c_min = 1;
  12215. #endif /* OXOS */
  12216. #endif /* VEOF */
  12217.  
  12218. #ifdef ZILOG
  12219.     ccraw.c_cc[5] = 0;
  12220. #else
  12221. #ifndef VEOL
  12222.     ccraw.c_cc[5] = 1;
  12223. #else
  12224. #ifndef OXOS
  12225. #ifdef VTIME
  12226.     ccraw.c_cc[VTIME] = 1;
  12227. #endif /* VTIME */
  12228. #else /* OXOS */
  12229.     ccraw.c_time = 1;
  12230. #endif /* OXOS */
  12231. #endif /* VEOL */
  12232. #endif /* ZILOG */
  12233.  
  12234. #ifdef BSD44ORPOSIX
  12235.     x = tcsetattr(0,TCSADRAIN,&ccraw);    /* Set new modes. */
  12236. #else
  12237.     x = ioctl(0,TCSETAW,&ccraw);
  12238. #endif /* BSD44ORPOSIX */
  12239. #else /* Berkeley, etc. */
  12240.     x = conchk();            /* Because stty() is destructive */
  12241.     if (x > 0)
  12242.       congetbuf(x);
  12243.     ccraw.sg_flags |= (RAW|TANDEM);     /* Set rawmode, XON/XOFF (ha) */
  12244.     ccraw.sg_flags &= ~(ECHO|CRMOD);    /* Set char wakeup, no echo */
  12245. #ifdef BELLV10
  12246.     x = ioctl(0,TIOCSETP,&ccraw);
  12247. #else
  12248.     x = stty(0,&ccraw);
  12249. #endif /* BELLV10 */
  12250. #endif /* SVORPOSIX */
  12251. #endif /* Plan9 */
  12252.  
  12253.     if (x > -1)
  12254.       constate = CON_BIN;
  12255.  
  12256.     debug(F101,"conbin returns","",x);
  12257.     return(x);
  12258.  
  12259. #ifdef COHERENT
  12260. #undef SVORPOSIX
  12261. #endif /* COHERENT */
  12262.  
  12263. }
  12264.  
  12265.  
  12266. /*  C O N R E S  --  Restore the console terminal  */
  12267.  
  12268. int
  12269. conres() {
  12270.     int x;
  12271.     debug(F101,"conres cgmf","",cgmf);
  12272.     debug(F101,"conres constate","",constate);
  12273.  
  12274.     if (cgmf < 1)            /* Do nothing if modes unchanged */
  12275.       return(0);
  12276.     if (constate == CON_RES)
  12277.       return(0);
  12278.  
  12279.     if (!isatty(0)) return(0);          /* only for real ttys */
  12280.     debug(F100,"conres isatty ok","",0);
  12281.     ckxech = 0;                         /* System should echo chars */
  12282.  
  12283. #ifdef aegis
  12284.     conbufn = 0;
  12285.     if (concrp) return(write(1, "\035\001", 2));
  12286.     if (conuid == input_pad_$uid) {
  12287.     pad_$cooked(ios_$stdin, st);
  12288.     constate = CON_RES;
  12289.     return(0);
  12290.     }
  12291. #endif /* aegis */
  12292.  
  12293. #ifdef Plan9
  12294.     p9conres();
  12295. #else
  12296. #ifdef BSD44ORPOSIX
  12297.     debug(F100,"conres restoring tcsetattr","",0);
  12298.     x = tcsetattr(0,TCSADRAIN,&ccold);
  12299. #else
  12300. #ifdef ATTSV
  12301.     debug(F100,"conres restoring ioctl","",0);
  12302.     x = ioctl(0,TCSETAW,&ccold);
  12303. #else /* BSD, V7, and friends */
  12304. #ifdef sony_news            /* Sony NEWS */
  12305.     if (km_con != -1)
  12306.       ioctl(0,TIOCKSET,&km_con);    /* Restore console Kanji mode */
  12307. #endif /* sony_news */
  12308.     msleep(100);
  12309.     debug(F100,"conres restoring stty","",0);
  12310.     x = conchk();            /* Because stty() is destructive */
  12311.     if (x > 0)
  12312.       congetbuf(x);
  12313. #ifdef BELLV10
  12314.     x = ioctl(0,TIOCSETP,&ccold);
  12315. #else
  12316.     x = stty(0,&ccold);
  12317. #endif /* BELLV10 */
  12318. #endif /* ATTSV */
  12319. #endif /* BSD44ORPOSIX */
  12320. #endif /* Plan9 */
  12321.     if (x > -1)
  12322.       constate = CON_RES;
  12323.  
  12324.     debug(F101,"conres returns","",x);
  12325.     return(x);
  12326. }
  12327.  
  12328. /*  C O N O C  --  Output a character to the console terminal  */
  12329.  
  12330. int
  12331. #ifdef CK_ANSIC
  12332. conoc(char c)
  12333. #else
  12334. conoc(c) char c;
  12335. #endif /* CK_ANSIC */
  12336. /* conoc */ {
  12337.  
  12338. #ifdef IKSD
  12339.     if (inserver && !local)
  12340.       return(ttoc(c));
  12341.  
  12342. #ifdef CK_ENCRYPTION
  12343.     if (inserver && TELOPT_ME(TELOPT_ENCRYPTION))
  12344.         ck_tn_encrypt(&c,1);
  12345. #endif /* CK_ENCRYPTION */
  12346. #endif /* IKSD */
  12347.  
  12348. #ifdef Plan9
  12349.     return conwrite(&c,1);
  12350. #else
  12351.     return(write(1,&c,1));
  12352. #endif /* Plan9 */
  12353. }
  12354.  
  12355. /*  C O N X O  --  Write x characters to the console terminal  */
  12356.  
  12357. int
  12358. conxo(x,s) int x; char *s; {
  12359.  
  12360. #ifdef IKSD
  12361.     if (inserver && !local)
  12362.       return(ttol((CHAR *)s,x));
  12363.  
  12364. #ifdef CK_ENCRYPTION
  12365.     if (inserver && TELOPT_ME(TELOPT_ENCRYPTION))
  12366.         ck_tn_encrypt(s,x);
  12367. #endif /* CK_ENCRYPTION */
  12368. #endif /* IKSD */
  12369.  
  12370. #ifdef Plan9
  12371.     return(conwrite(s,x));
  12372. #else
  12373.     return(write(1,s,x));
  12374. #endif /* Plan9 */
  12375. }
  12376.  
  12377. /*  C O N O L  --  Write a line to the console terminal  */
  12378.  
  12379. int
  12380. conol(s) char *s; {
  12381.     int len;
  12382.     if (!s) s = "";            /* Always do this! */
  12383.     len = strlen(s);
  12384.     if (len == 0)
  12385.       return(0);
  12386.  
  12387. #ifdef IKSD
  12388.     if (inserver && !local)
  12389.       return(ttol((CHAR *)s,len));
  12390.  
  12391. #ifdef CK_ENCRYPTION
  12392.     if (inserver && TELOPT_ME(TELOPT_ENCRYPTION)) {
  12393.     if (nxpacket < len) {
  12394.         if (xpacket) {
  12395.         free(xpacket);
  12396.         xpacket = NULL;
  12397.         nxpacket = 0;
  12398.         }
  12399.         len = len > 10240 ? len : 10240;
  12400.         xpacket = (char *)malloc(len);
  12401.         if (!xpacket) {
  12402.         fprintf(stderr,"ttol malloc failure\n");
  12403.         return(-1);
  12404.         } else
  12405.           nxpacket = len;
  12406.     }
  12407.     memcpy(xpacket,s,len);
  12408.     s = xpacket;
  12409.     ck_tn_encrypt(s,len);
  12410.     }
  12411. #endif /* CK_ENCRYPTION */
  12412. #endif /* IKSD */
  12413.  
  12414. #ifdef Plan9
  12415.     return(conwrite(s,len));
  12416. #else
  12417.     return(write(1,s,len));
  12418. #endif /* Plan9 */
  12419. }
  12420.  
  12421. /*  C O N O L A  --  Write an array of lines to the console terminal */
  12422.  
  12423. int
  12424. conola(s) char *s[]; {
  12425.     char * p;
  12426.     int i, x;
  12427.  
  12428.  
  12429.     if (!s) return(0);
  12430.     for (i = 0; ; i++) {
  12431.     p = s[i];
  12432.     if (!p) p = "";            /* Let's not dump core shall we? */
  12433.     if (!*p)
  12434.       break;
  12435. #ifdef IKSD
  12436.     if (inserver && !local)
  12437.       x = ttol((CHAR *)p,(int)strlen(p));
  12438.     else
  12439. #endif /* IKSD */
  12440.       x = conol(p);
  12441.     if (x < 0)
  12442.       return(-1);
  12443.     }
  12444.     return(0);
  12445. }
  12446.  
  12447. /*  C O N O L L  --  Output a string followed by CRLF  */
  12448.  
  12449. int
  12450. conoll(s) char *s; {
  12451.     CHAR buf[3];
  12452.     buf[0] = '\r';
  12453.     buf[1] = '\n';
  12454.     buf[2] = '\0';
  12455.     if (!s) s = "";
  12456.  
  12457. #ifdef IKSD
  12458.     if (inserver && !local) {
  12459.     if (*s) ttol((CHAR *)s,(int)strlen(s));
  12460.     return(ttol(buf,2));
  12461.     }
  12462. #endif /* IKSD */
  12463.  
  12464.     if (*s) conol(s);
  12465. #ifdef IKSD
  12466. #ifdef CK_ENCRYPTION
  12467.     if (inserver && TELOPT_ME(TELOPT_ENCRYPTION))
  12468.       ck_tn_encrypt(buf,2);
  12469. #endif /* CK_ENCRYPTION */
  12470. #endif /* IKSD */
  12471.  
  12472. #ifdef Plan9
  12473.     return(conwrite(buf, 2));
  12474. #else
  12475.     return(write(1,buf,2));
  12476. #endif /* Plan9 */
  12477. }
  12478.  
  12479. /*  C O N C H K  --  Return how many characters available at console  */
  12480. /*
  12481.   We could also use select() here to cover a few more systems that are not
  12482.   covered by any of the following, e.g. HP-UX 9.0x on the model 800.
  12483. */
  12484. int
  12485. conchk() {
  12486.     static int contyp = 0;        /* +1 for isatty, -1 otherwise */
  12487.  
  12488.     if (contyp == 0)            /* This prevents unnecessary */
  12489.       contyp = (isatty(0) ? 1 : -1);    /* duplicated calls to isatty() */
  12490.     debug(F101,"conchk contyp","",contyp);
  12491.     if (backgrd || (contyp < 0))
  12492.       return(0);
  12493.  
  12494. #ifdef aegis
  12495.     if (conbufn > 0) return(conbufn);   /* use old count if nonzero */
  12496.  
  12497.     /* read in more characters */
  12498.     conbufn = ios_$get(ios_$stdin,
  12499.               ios_$cond_opt, conbuf, (long)sizeof(conbuf), st);
  12500.     if (st.all != status_$ok) conbufn = 0;
  12501.     conbufp = conbuf;
  12502.     return(conbufn);
  12503. #else
  12504. #ifdef IKSD
  12505.     if (inserver && !local)
  12506.       return(in_chk(1,ttyfd));
  12507.     else
  12508. #endif /* IKSD */
  12509.       return(in_chk(0,0));
  12510. #endif /* aegis */
  12511. }
  12512.  
  12513. /*  C O N I N C  --  Get a character from the console  */
  12514. /*
  12515.   Call with timo > 0 to do a timed read, timo == 0 to do an untimed blocking
  12516.   read.  Upon success, returns the character.  Upon failure, returns -1.
  12517.   A timed read that does not complete within the timeout period returns -2.
  12518. */
  12519. int
  12520. coninc(timo) int timo; {
  12521.     int n = 0; CHAR ch;
  12522.     int xx;
  12523.  
  12524.     if (conbufn > 0) {            /* If something already buffered */
  12525.     --conbufn;
  12526.     return((unsigned)(*conbufp++ & 0xff));
  12527.     }
  12528.  
  12529.     errno = 0;                /* Clear this */
  12530. #ifdef IKSD
  12531.     if (inserver && !local) {
  12532.     xx = ttinc(timo);
  12533.     if (xx < 0)
  12534.       return(ttinctimo ? -2 : -1);
  12535.     else
  12536.       return(xx);
  12537.     }
  12538. #endif /* IKSD */
  12539.  
  12540. #ifdef aegis                /* Apollo Aegis only... */
  12541.     debug(F101,"coninc timo","",timo);
  12542.     fflush(stdout);
  12543.     if (conchk() > 0) {
  12544.     --conbufn;
  12545.     return((unsigned)(*conbufp++ & 0xff));
  12546.     }
  12547. #endif /* aegis */
  12548.  
  12549. #ifdef TTLEBUF
  12550.     if (
  12551. #ifdef IKSD
  12552.     inserver &&
  12553. #endif /* IKSD */
  12554.     !xlocal
  12555.     ) {
  12556.     if (ttpush >= 0) {
  12557.         debug(F111,"ttinc","ttpush",ttpush);
  12558.         ch = ttpush;
  12559.         ttpush = -1;
  12560.         return(ch);
  12561.     }
  12562.     if (le_data) {
  12563.         if (le_getchar(&ch) > 0) {
  12564.         debug(F111,"ttinc LocalEchoInBuf","ch",ch);
  12565.         return(ch);
  12566.         }
  12567.     }
  12568.     }
  12569. #endif /* TTLEBUF */
  12570.  
  12571.     if (timo <= 0) {            /* Untimed, blocking read. */
  12572.     while (1) {            /* Keep trying till we get one. */
  12573.         n = read(0, &ch, 1);    /* Read a character. */
  12574.         if (n == 0) continue;    /* Shouldn't happen. */
  12575.         if (n > 0) {        /* If read was successful, */
  12576. #ifdef IKSD
  12577. #ifdef CK_ENCRYPTION
  12578.                 debug(F100,"coninc decrypt 1","",0);
  12579.                 if (inserver && !local && TELOPT_U(TELOPT_ENCRYPTION))
  12580.           ck_tn_decrypt(&ch,1);
  12581. #endif /* CK_ENCRYPTION */
  12582. #endif /* IKSD */
  12583.         return((unsigned)(ch & 0xff)); /* return the character. */
  12584.             }
  12585.  
  12586. /* Come here if read() returned an error. */
  12587.  
  12588.         debug(F101, "coninc(0) errno","",errno); /* Log the error. */
  12589. #ifndef OXOS
  12590. #ifdef SVORPOSIX
  12591. #ifdef CIE                             /* CIE Regulus has no EINTR symbol? */
  12592. #ifndef EINTR
  12593. #define EINTR 4
  12594. #endif /* EINTR */
  12595. #endif /* CIE */
  12596. /*
  12597.   This routine is used for several different purposes.  In CONNECT mode, it is
  12598.   used to do an untimed, blocking read from the keyboard in the lower CONNECT
  12599.   fork.  During local-mode file transfer, it reads a character from the
  12600.   console to interrupt the file transfer (like A for a status report, X to
  12601.   cancel a file, etc).  Obviously, we don't want the reads in the latter case
  12602.   to be blocking, or the file transfer would stop until the user typed
  12603.   something.  Unfortunately, System V does not allow the console device input
  12604.   buffer to be sampled nondestructively (e.g. by conchk()), so a kludge is
  12605.   used instead.  During local-mode file transfer, the SIGQUIT signal is armed
  12606.   and trapped by esctrp(), and this routine pretends to have read the quit
  12607.   character from the keyboard normally.  But, kludge or no kludge, the read()
  12608.   issued by this command, under System V only, can fail if a signal -- ANY
  12609.   signal -- is caught while the read is pending.  This can occur not only when
  12610.   the user types the quit character, but also during telnet negotiations, when
  12611.   the lower CONNECT fork signals the upper one about an echoing mode change.
  12612.   When this happens, we have to post the read() again.  This is apparently not
  12613.   a problem in BSD-based UNIX versions.
  12614. */
  12615.         if (errno == EINTR)        /* Read interrupted. */
  12616.           if (conesc)  {        /* If by SIGQUIT, */
  12617.           conesc = 0;        /* the conesc variable is set, */
  12618.           return(escchr);    /* so return the escape character. */
  12619.          } else continue;        /* By other signal, try again. */
  12620. #else
  12621. /*
  12622.   This might be dangerous, but let's do this on non-System V versions too,
  12623.   since at least one SunOS 4.1.2 user complains of immediate disconnections
  12624.   upon first making a TELNET connection.
  12625. */
  12626.         if (errno == EINTR)        /* Read interrupted. */
  12627.           continue;
  12628. #endif /* SVORPOSIX */
  12629. #else /* OXOS */
  12630.         if (errno == EINTR)        /* Read interrupted. */
  12631.           continue;
  12632. #endif /* OXOS */
  12633.         return(-1);            /* Error */
  12634.     }
  12635.     }
  12636. #ifdef DEBUG
  12637.     if (deblog && timo <= 0) {
  12638.     debug(F100,"coninc timeout logic error","",0);
  12639.     timo = 1;
  12640.     }
  12641. #endif /* DEBUG */
  12642.  
  12643. /* Timed read... */
  12644.  
  12645.     saval = signal(SIGALRM,timerh);    /* Set up timeout handler. */
  12646.     xx = alarm(timo);            /* Set the alarm. */
  12647.     debug(F101,"coninc alarm set","",timo);
  12648.     if (
  12649. #ifdef CK_POSIX_SIG
  12650.     sigsetjmp(sjbuf,1)
  12651. #else
  12652.     setjmp(sjbuf)
  12653. #endif /* CK_POSIX_SIG */
  12654.     )                /* The read() timed out. */
  12655.       n = -2;                /* Code for timeout. */
  12656.     else
  12657.       n = read(0, &ch, 1);
  12658.     ttimoff();                /* Turn off timer */
  12659.     if (n > 0) {            /* Got character OK. */
  12660. #ifdef IKSD
  12661. #ifdef CK_ENCRYPTION
  12662.         debug(F100,"coninc decrypt 2","",0);
  12663.         if (inserver && !local && TELOPT_U(TELOPT_ENCRYPTION))
  12664.       ck_tn_decrypt(&ch,1);
  12665. #endif /* CK_ENCRYPTION */
  12666. #endif /* IKSD */
  12667.     return((unsigned)(ch & 0xff));    /* Return it. */
  12668.     }
  12669. /*
  12670.   read() returned an error.  Same deal as above, but without the loop.
  12671. */
  12672.     debug(F101, "coninc(timo) n","",n);
  12673.     debug(F101, "coninc(timo) errno","",errno);
  12674. #ifndef OXOS
  12675. #ifdef SVORPOSIX
  12676.     if (n == -1 && errno == EINTR && conesc != 0) {
  12677.     conesc = 0;
  12678.     return(escchr);            /* User entered escape character. */
  12679.     }
  12680. #endif /* SVORPOSIX */
  12681.     if (n == 0 && errno > 0) {        /* It's an error */
  12682.     return(-1);
  12683.     }
  12684. #endif /* ! OXOS */
  12685.     return(n);
  12686. }
  12687.  
  12688. /*  C O N G K S  --  Console Get Keyboard Scancode  */
  12689.  
  12690. #ifndef congks
  12691. /*
  12692.   This function needs to be filled in with the various system-dependent
  12693.   system calls used by SUNOS, NeXT OS, Xenix, Aviion, etc, to read a full
  12694.   keyboard scan code.  Unfortunately there aren't any.
  12695. */
  12696. int
  12697. congks(timo) int timo; {
  12698.  
  12699. #ifdef IKSD
  12700.     if (inserver && !local)
  12701.       return(ttinc(timo));
  12702. #endif /* IKSD */
  12703.  
  12704.     return(coninc(timo));
  12705. }
  12706. #endif /* congks */
  12707.  
  12708. #ifdef ATT7300
  12709.  
  12710. /*  A T T D I A L  --  Dial up the remote system using internal modem
  12711.  * Purpose: to open and dial a number on the internal modem available on the
  12712.  * ATT7300 UNIX PC.  Written by Joe Doupnik. Superceeds version written by
  12713.  * Richard E. Hill, Dickinson, TX. which employed dial(3c).
  12714.  * Uses information in <sys/phone.h> and our status int attmodem.
  12715.  */
  12716. attdial(ttname,speed,telnbr) char *ttname,*telnbr; long speed; {
  12717.     char *telnum;
  12718.  
  12719.     attmodem &= ~ISMODEM;                       /* modem not in use yet */
  12720.                     /* Ensure O_NDELAY is set, else i/o traffic hangs */
  12721.                     /* We turn this flag off once the dial is complete */
  12722.     fcntl(ttyfd, F_SETFL, fcntl(ttyfd, F_GETFL, 0) | O_NDELAY);
  12723.  
  12724.     /* Condition line, check availability & DATA mode, turn on speaker */
  12725.     if (ioctl(ttyfd,PIOCOFFHOOK, &dialer) == -1) {
  12726.         printf("cannot access phone\n");
  12727.         ttclos(0);
  12728.         return (-2);
  12729.     }
  12730.     ioctl(ttyfd,PIOCGETP,&dialer);      /* get phone dialer parameters */
  12731.  
  12732.     if (dialer.c_lineparam & VOICE) {    /* phone must be in DATA mode */
  12733.         printf(" Should not dial with modem in VOICE mode.\n");
  12734.         printf(" Exit Kermit, switch to DATA and retry call.\n");
  12735.         ttclos(0);
  12736.         return (-2);
  12737.     }
  12738. #ifdef ATTTONED                /* Old way, tone dialing only. */
  12739.     dialer.c_lineparam = DATA | DTMF;    /* Dial with tones, */
  12740.     dialer.c_lineparam &= ~PULSE;    /* not with pulses. */
  12741. #else
  12742.     /* Leave current pulse/tone state alone. */
  12743.     /* But what about DATA?  Add it back if you have trouble. */
  12744.     /* sys/phone says you get DATA automatically by opening device RDWR */
  12745. #endif
  12746.     dialer.c_waitdialtone = 5;                  /* wait 5 sec for dialtone */
  12747. #ifdef COMMENT
  12748.     dialer.c_feedback = SPEAKERON|NORMSPK|RINGON;  /* control speaker */
  12749. #else
  12750.     /* sys/phone says RINGON used only for incoming voice calls */
  12751.     dialer.c_feedback &= ~(SOFTSPK|LOUDSPK);
  12752.     dialer.c_feedback |= SPEAKERON|NORMSPK;
  12753. #endif
  12754.     dialer.c_waitflash = 500;                   /* 0.5 sec flash hook */
  12755.     if(ioctl(ttyfd,PIOCSETP,&dialer) == -1) {   /* set phone parameters */
  12756.         printf("Cannot set modem characteristics\n");
  12757.         ttclos(0);
  12758.         return (-2);
  12759.     }
  12760.     ioctl(ttyfd,PIOCRECONN,0);        /* Turns on speaker for pulse */
  12761.  
  12762. #ifdef COMMENT
  12763.     fprintf(stderr,"Phone line status. line_par:%o dialtone_wait:%o \
  12764. line_status:%o feedback:%o\n",
  12765.     dialer.c_lineparam, dialer.c_waitdialtone,
  12766.     dialer.c_linestatus, dialer.c_feedback);
  12767. #endif
  12768.  
  12769.     attmodem |= ISMODEM;                        /* modem is now in-use */
  12770.     sleep(1);
  12771.     for (telnum = telnbr; *telnum != '\0'; telnum++)    /* dial number */
  12772. #ifdef ATTTONED
  12773.       /* Tone dialing only */
  12774.       if (ioctl(ttyfd,PIOCDIAL,telnum) != 0) {
  12775.       perror("Error in dialing");
  12776.       ttclos(0);
  12777.       return(-2);
  12778.       }
  12779. #else /* Allow Pulse or Tone dialing */
  12780.     switch (*telnum) {
  12781.       case 't': case 'T': case '%':    /* Tone dialing requested */
  12782.     dialer.c_lineparam |= DTMF;
  12783.     dialer.c_lineparam &= ~PULSE;
  12784.     if (ioctl(ttyfd,PIOCSETP,&dialer) == -1) {
  12785.         printf("Cannot set modem to tone dialing\n");
  12786.         ttclos(0);
  12787.         return(-2);
  12788.     }
  12789.     break;
  12790.       case 'd': case 'D': case 'p': case 'P': case '^':
  12791.     dialer.c_lineparam |= PULSE;
  12792.     dialer.c_lineparam &= ~DTMF;
  12793.     if (ioctl(ttyfd,PIOCSETP,&dialer) == -1) {
  12794.         printf("Cannot set modem to pulse dialing\n");
  12795.         ttclos(0);
  12796.         return(-2);
  12797.     }
  12798.     break;
  12799.       default:
  12800.         if (ioctl(ttyfd,PIOCDIAL,telnum) != 0) {
  12801.         perror("Dialing error");
  12802.         ttclos(0);
  12803.         return(-2);
  12804.     }
  12805.     break;
  12806.     }
  12807. #endif
  12808.  
  12809.     ioctl(ttyfd,PIOCDIAL,"@");        /* terminator for data call */
  12810.     do {                /* wait for modems to Connect */
  12811.         if (ioctl(ttyfd,PIOCGETP,&dialer) != 0)    { /* get params */
  12812.         perror("Cannot get modems to connect");
  12813.         ttclos(0);
  12814.         return(-2);
  12815.     }
  12816.     } while ((dialer.c_linestatus & MODEMCONNECTED) == 0);
  12817.     /* Turn off O_NDELAY flag now. */
  12818.     fcntl(ttyfd, F_SETFL, fcntl(ttyfd, F_GETFL, 0) & ~O_NDELAY);
  12819.     signal(SIGHUP, sighup);             /* hangup on loss of carrier */
  12820.     return(0);                          /* return success */
  12821. }
  12822.  
  12823. /*
  12824.   Offgetty, ongetty functions. These function get the 'getty(1m)' off
  12825.   and restore it to the indicated line.  Shell's return codes are:
  12826.     0: Can't do it.  Probably a user logged on.
  12827.     1: No need.  No getty on that line.
  12828.     2: Done, you should restore the getty when you're done.
  12829.   DOGETY System(3), however, returns them as 0, 256, 512, respectively.
  12830.   Thanks to Kevin O'Gorman, Anarm Software Systems.
  12831.  
  12832.    getoff.sh looks like:   geton.sh looks like:
  12833.      setgetty $1 0           setgetty $1 1
  12834.      err=$?                  exit $?
  12835.      sleep 2
  12836.      exit $err
  12837. */
  12838.  
  12839. /*  O F F G E T T Y  --  Turn off getty(1m) for the communications tty line
  12840.  * and get status so it can be restarted after the line is hung up.
  12841.  */
  12842. int
  12843. offgetty(ttname) char *ttname; {
  12844.     char temp[30];
  12845.     while (*ttname != '\0') ttname++;       /* seek terminator of path */
  12846.     ttname -= 3;                            /* get last 3 chars of name */
  12847.     sprintf(temp,"/usr/bin/getoff.sh %s",ttname);
  12848.     return(zsyscmd(temp));
  12849. }
  12850.  
  12851. /*  O N G E T T Y  --  Turn on getty(1m) for the communications tty line */
  12852.  
  12853. int
  12854. ongetty(ttname) char *ttname; {
  12855.     char temp[30];
  12856.     while (*ttname != '\0') ttname++;       /* comms tty path name */
  12857.     ttname -= 3;
  12858.     sprintf(temp,"/usr/bin/geton.sh %s",ttname);
  12859.     return(zsyscmd(temp));
  12860. }
  12861. #endif /* ATT7300 */
  12862.  
  12863. /*  T T S C A R R  --  Set ttcarr variable, controlling carrier handling.
  12864.  *
  12865.  *  0 = Off: Always ignore carrier. E.g. you can connect without carrier.
  12866.  *  1 = On: Heed carrier, except during dialing. Carrier loss gives disconnect.
  12867.  *  2 = Auto: For "modem direct": The same as "Off".
  12868.  *            For real modem types: Heed carrier during connect, but ignore
  12869.  *                it anytime else.  Compatible with pre-5A C-Kermit versions.
  12870.  *
  12871.  * As you can see, this setting does not affect dialing, which always ignores
  12872.  * carrier (unless there is some special exception for some modem type).  It
  12873.  * does affect ttopen() if it is set before ttopen() is used.  This setting
  12874.  * takes effect on the next call to ttopen()/ttpkt()/ttvt().  And they are
  12875.  * (or should be) always called before any communications is tried, which
  12876.  * means that, practically speaking, the effect is immediate.
  12877.  *
  12878.  * Of course, nothing of this applies to remote mode (xlocal = 0).
  12879.  *
  12880.  * Someone has yet to uncover how to manipulate the carrier in the BSD
  12881.  * environment (or any non-termio using environment).  Until that time, this
  12882.  * will simply be a no-op for BSD.
  12883.  *
  12884.  * Note that in previous versions, the carrier was most often left unchanged
  12885.  * in ttpkt()/ttvt() unless they were called with FLO_DIAL or FLO_DIAX.  This
  12886.  * has changed.  Now it is controlled by ttcarr in conjunction with these
  12887.  * modes.
  12888.  */
  12889. int
  12890. ttscarr(carrier) int carrier; {
  12891.     ttcarr = carrier;
  12892.     debug(F101, "ttscarr","",ttcarr);
  12893.     return(ttcarr);
  12894. }
  12895.  
  12896. /* C A R R C T L  --  Set tty modes for carrier treatment.
  12897.  *
  12898.  * Sets the appropriate bits in a termio or sgttyb struct for carrier control
  12899.  * (actually, there are no bits in sgttyb for that), or performs any other
  12900.  * operations needed to control this on the current system.  The function does
  12901.  * not do the actual TCSETA or stty, since often we want to set other bits too
  12902.  * first.  Don't call this function when xlocal is 0, or the tty is not opened.
  12903.  *
  12904.  * We don't know how to do anything like carrier control on non-ATTSV systems,
  12905.  * except, apparently, ultrix.  See above.  It is also known that this doesn't
  12906.  * have much effect on a Xenix system.  For Xenix, one should switch back and
  12907.  * forth between the upper and lower case device files.  Maybe later.
  12908.  * Presently, Xenix will stick to the mode it was opened with.
  12909.  *
  12910.  * carrier: 0 = ignore carrier, 1 = require carrier.
  12911.  * The current state is saved in curcarr, and checked to save labour.
  12912.  */
  12913. #ifdef SVORPOSIX
  12914. int
  12915. #ifdef BSD44ORPOSIX
  12916. carrctl(ttpar, carrier)    struct termios *ttpar; int carrier;
  12917. #else /* ATTSV */
  12918. carrctl(ttpar, carrier)    struct termio *ttpar; int carrier;
  12919. #endif /* BSD44ORPOSIX */
  12920. /* carrctl */ {
  12921.     debug(F101, "carrctl","",carrier);
  12922.     if (carrier)
  12923.       ttpar->c_cflag &= ~CLOCAL;
  12924.     else
  12925.       ttpar->c_cflag |= CLOCAL;
  12926.     return(0);
  12927. }
  12928. #else /* Berkeley, V7, et al... */
  12929. int
  12930. carrctl(ttpar, carrier) struct sgttyb *ttpar; int carrier; {
  12931.     debug(F101, "carrctl","",carrier);
  12932.     if (carrier == curcarr)
  12933.       return(0);
  12934.     curcarr = carrier;
  12935. #ifdef ultrix
  12936. #ifdef COMMENT
  12937. /*
  12938.   Old code from somebody at DEC that tends to get stuck, time out, etc.
  12939. */
  12940.     if (carrier) {
  12941.     ioctl(ttyfd, TIOCMODEM, &temp);
  12942.     ioctl(ttyfd, TIOCHPCL, 0);
  12943.     } else {
  12944.     /* (According to the manuals, TIOCNCAR should be preferred */
  12945.     /* over TIOCNMODEM...) */
  12946.     ioctl(ttyfd, TIOCNMODEM, &temp);
  12947.     }
  12948. #else
  12949. /*
  12950.   New code from Jamie Watson that, he says, eliminates the problems.
  12951. */
  12952.     if (carrier) {
  12953.     ioctl(ttyfd, TIOCCAR);
  12954.     ioctl(ttyfd, TIOCHPCL);
  12955.     } else {
  12956.     ioctl(ttyfd, TIOCNCAR);
  12957.     }
  12958. #endif /* COMMENT */
  12959. #endif /* ultrix */
  12960.     return(0);
  12961. }
  12962. #endif /* SVORPOSIX */
  12963.  
  12964.  
  12965. /*  T T G M D M  --  Get modem signals  */
  12966. /*
  12967.  Looks for RS-232 modem signals, and returns those that are on in as its
  12968.  return value, in a bit mask composed of the BM_xxx values defined in ckcdeb.h.
  12969.  Returns:
  12970.  -3 Not implemented
  12971.  -2 if the communication device does not have modem control (e.g. telnet)
  12972.  -1 on error.
  12973.  >= 0 on success, with a bit mask containing the modem signals that are on.
  12974. */
  12975.  
  12976. /*
  12977.   Define the symbol K_MDMCTL if we have Sys V R3 / 4.3 BSD style
  12978.   modem control, namely the TIOCMGET ioctl.
  12979. */
  12980.  
  12981. #ifdef BSD43
  12982. #define K_MDMCTL
  12983. #endif /* BSD43 */
  12984.  
  12985. #ifdef SUNOS4
  12986. #define K_MDMCTL
  12987. #endif /* SUNOS4 */
  12988.  
  12989. /*
  12990.   SCO OpenServer R5.0.4.  The TIOCMGET definition is hardwired in because it
  12991.   is skipped in termio.h when _POSIX_SOURCE is defined.  But _POSIX_SOURCE
  12992.   must be defined in order to get the high serial speeds that are new to
  12993.   5.0.4.  However, the regular SCO drivers do not implement TIOCMGET, so the
  12994.   ioctl() returns -1 with errno 22 (invalid function).  But third-party
  12995.   drivers, e.g. for Digiboard, do implement it, and so it should work on ports
  12996.   driven by those drivers.
  12997. */
  12998. #ifdef SCO_OSR504
  12999. #ifndef TIOCMGET
  13000. #define TIOCMGET (('t'<<8)|29)
  13001. #endif /* TIOCMGET */
  13002. #endif /* SCO_OSR504 */
  13003.  
  13004. #ifdef CK_SCOV5
  13005. /* Because POSIX strictness in <sys/termio.h> won't let us see these. */
  13006. #ifndef TIOCM_DTR
  13007. #define TIOCM_DTR    0x0002        /* data terminal ready */
  13008. #define TIOCM_RTS    0x0004        /* request to send */
  13009. #define TIOCM_CTS    0x0020        /* clear to send */
  13010. #define TIOCM_CAR    0x0040        /* carrier detect */
  13011. #define TIOCM_RNG    0x0080        /* ring */
  13012. #define TIOCM_DSR    0x0100        /* data set ready */
  13013. #define TIOCM_CD    TIOCM_CAR
  13014. #define TIOCM_RI    TIOCM_RNG
  13015. #endif /* TIOCM_DTR */
  13016. #endif /* CK_SCOV5 */
  13017.  
  13018. #ifdef QNX
  13019. #define K_MDMCTL
  13020. #else
  13021. #ifdef TIOCMGET
  13022. #define K_MDMCTL
  13023. #endif /* TIOCMGET */
  13024. #endif /* QNX */
  13025. /*
  13026.   "A serial communication program that can't read modem signals
  13027.    is like a car without windows."
  13028. */
  13029. int
  13030. ttgmdm() {
  13031.  
  13032. #ifdef QNX
  13033. #include <sys/qioctl.h>
  13034.  
  13035.     unsigned long y, mdmbits[2];
  13036.     int x, z = 0;
  13037.  
  13038.     if (xlocal && ttyfd < 0)
  13039.       return(-1);
  13040.  
  13041. #ifdef NETCONN
  13042.     if (netconn) {            /* Network connection */
  13043. #ifdef TN_COMPORT
  13044.         if (istncomport()) {
  13045.         gotsigs = 1;
  13046.         return(tngmdm());
  13047.     } else
  13048. #endif /* TN_COMPORT */
  13049.       return(-2);            /* No modem signals */
  13050.     }
  13051. #endif /* NETCONN */
  13052.  
  13053. #ifdef NETCMD
  13054.     if (ttpipe) return(-2);
  13055. #endif /* NETCMD */
  13056. #ifdef NETPTY
  13057.     if (ttpty) return(-2);
  13058. #endif /* NETPTY */
  13059.  
  13060.     mdmbits[0] = 0L;
  13061.     mdmbits[1] = 0L;
  13062. /*
  13063.  * From <sys/qioctl.h>:
  13064.  *
  13065.  * SERIAL devices   (all Dev.ser versions)
  13066.  * 0 : DTR           8 = Data Bits 0  16 - reserved     24 - reserved
  13067.  * 1 : RTS           9 = Data Bits 1  17 - reserved     25 - reserved
  13068.  * 2 = Out 1        10 = Stop Bits    18 - reserved     26 - reserved
  13069.  * 3 = Int Enable   11 = Par Enable   19 - reserved     27 - reserved
  13070.  * 4 = Loop         12 = Par Even     20 = CTS          28 - reserved
  13071.  * 5 - reserved     13 = Par Stick    21 = DSR          29 - reserved
  13072.  * 6 - reserved     14 : Break        22 = RI           30 - reserved
  13073.  * 7 - reserved     15 = 0            23 = CD           31 - reserved
  13074.  */
  13075.     errno = 0;
  13076.     x = qnx_ioctl(ttyfd, QCTL_DEV_CTL, &mdmbits[0], 8, &mdmbits[0], 4);
  13077.     debug(F101,"ttgmdm qnx_ioctl","",x);
  13078.     debug(F101,"ttgmdm qnx_ioctl errno","",errno);
  13079.     if (!x) {
  13080.     debug(F101,"ttgmdm qnx_ioctl mdmbits[0]","",mdmbits[0]);
  13081.     debug(F101,"ttgmdm qnx_ioctl mdmbits[1]","",mdmbits[1]);
  13082.     y = mdmbits[0];
  13083.     if (y & 0x000001L) z |= BM_DTR;    /* Bit  0 */
  13084.     if (y & 0x000002L) z |= BM_RTS;    /* Bit  1 */
  13085.     if (y & 0x100000L) z |= BM_CTS;    /* Bit 20 */
  13086.     if (y & 0x200000L) z |= BM_DSR;    /* Bit 21 */
  13087.     if (y & 0x400000L) z |= BM_RNG;    /* Bit 22 */
  13088.     if (y & 0x800000L) z |= BM_DCD;    /* Bit 23 */
  13089.     debug(F101,"ttgmdm qnx result","",z);
  13090.     debug(F110,"ttgmdm qnx CD = ",(z & BM_DCD) ? "On" : "Off", 0);
  13091.     gotsigs = 1;
  13092.     return(z);
  13093.     } else return(-1);
  13094. #else /* QNX */
  13095. #ifdef HPUX                /* HPUX has its own way */
  13096.     int x, z;
  13097.  
  13098. #ifdef HPUX10                /* Modem flag word */
  13099.     mflag y;                /* mflag typedef'd in <sys/modem.h> */
  13100. #else
  13101. #ifdef HPUX9
  13102.     mflag y;
  13103. #else
  13104. #ifdef HPUX8
  13105.     mflag y;
  13106. #else
  13107.     unsigned long y;            /* Not sure about pre-8.0... */
  13108. #endif /* HPUX8 */
  13109. #endif /* HPUX9 */
  13110. #endif /* HPUX10 */
  13111.  
  13112.     if (xlocal && ttyfd < 0)
  13113.       return(-1);
  13114.  
  13115. #ifdef NETCONN
  13116.     if (netconn) {            /* Network connection */
  13117. #ifdef TN_COMPORT
  13118.         if (istncomport()) {
  13119.         gotsigs = 1;
  13120.         return(tngmdm());
  13121.     } else
  13122. #endif /* TN_COMPORT */
  13123.       return(-2);            /* No modem signals */
  13124.     }
  13125. #endif /* NETCONN */
  13126.  
  13127. #ifdef NETCMD
  13128.     if (ttpipe) return(-2);
  13129. #endif /* NETCMD */
  13130. #ifdef NETPTY
  13131.     if (ttpty) return(-2);
  13132. #endif /* NETPTY */
  13133.  
  13134.     if (xlocal)                /* Get modem signals */
  13135.       x = ioctl(ttyfd,MCGETA,&y);
  13136.     else
  13137.       x = ioctl(0,MCGETA,&y);
  13138.     if (x < 0) return(-1);
  13139.     debug(F101,"ttgmdm","",y);
  13140.  
  13141.     z = 0;                /* Initialize return value */
  13142.  
  13143. /* Now set bits for each modem signal that is reported to be on. */
  13144.  
  13145. #ifdef MCTS
  13146.     /* Clear To Send */
  13147.     debug(F101,"ttgmdm HPUX CTS","",y & MCTS);
  13148.     if (y & MCTS) z |= BM_CTS;
  13149. #endif
  13150. #ifdef MDSR
  13151.     /* Data Set Ready */
  13152.     debug(F101,"ttgmdm HPUX DSR","",y & MDSR);
  13153.     if (y & MDSR) z |= BM_DSR;
  13154. #endif
  13155. #ifdef MDCD
  13156.     /* Carrier */
  13157.     debug(F101,"ttgmdm HPUX DCD","",y & MDCD);
  13158.     if (y & MDCD) z |= BM_DCD;
  13159. #endif
  13160. #ifdef MRI
  13161.     /* Ring Indicate */
  13162.     debug(F101,"ttgmdm HPUX RI","",y & MRI);
  13163.     if (y & MRI) z |= BM_RNG;
  13164. #endif
  13165. #ifdef MDTR
  13166.     /* Data Terminal Ready */
  13167.     debug(F101,"ttgmdm HPUX DTR","",y & MDTR);
  13168.     if (y & MDTR) z |= BM_DTR;
  13169. #endif
  13170. #ifdef MRTS
  13171.     /* Request To Send */
  13172.     debug(F101,"ttgmdm HPUX RTS","",y & MRTS);
  13173.     if (y & MRTS) z |= BM_RTS;
  13174. #endif
  13175.     gotsigs = 1;
  13176.     return(z);
  13177.  
  13178. #else /* ! HPUX */
  13179.  
  13180. #ifdef K_MDMCTL
  13181. /*
  13182.   Note, TIOCMGET might already have been defined in <sys/ioctl.h> or elsewhere.
  13183.   If not, we try including <sys/ttycom.h> -- if this blows up then more ifdefs
  13184.   are needed.
  13185. */
  13186. #ifndef TIOCMGET
  13187. #include <sys/ttycom.h>
  13188. #endif /* TIOCMGET */
  13189.  
  13190.     int x, y, z;
  13191.  
  13192.     debug(F100,"ttgmdm K_MDMCTL defined","",0);
  13193.  
  13194. #ifdef NETCONN
  13195.     if (netconn) {            /* Network connection */
  13196. #ifdef TN_COMPORT
  13197.         if (istncomport()) {
  13198.         gotsigs = 1;
  13199.         return(tngmdm());
  13200.     } else
  13201. #endif /* TN_COMPORT */
  13202.       return(-2);            /* No modem signals */
  13203.     }
  13204. #endif /* NETCONN */
  13205.  
  13206. #ifdef NETCMD
  13207.     if (ttpipe) return(-2);
  13208. #endif /* NETCMD */
  13209. #ifdef NETPTY
  13210.     if (ttpty) return(-2);
  13211. #endif /* NETPTY */
  13212.  
  13213.     if (xlocal && ttyfd < 0)
  13214.       return(-1);
  13215.  
  13216.     if (xlocal)
  13217.       x = ioctl(ttyfd,TIOCMGET,&y);    /* Get modem signals. */
  13218.     else
  13219.       x = ioctl(0,TIOCMGET,&y);
  13220.     debug(F101,"ttgmdm TIOCMGET ioctl","",x);
  13221.     if (x < 0) {
  13222.     debug(F101,"ttgmdm errno","",errno);
  13223.     return(-1);
  13224.     }
  13225.     debug(F101,"ttgmdm bits","",y);
  13226.  
  13227.     z = 0;                /* Initialize return value. */
  13228. #ifdef TIOCM_CTS
  13229.     /* Clear To Send */
  13230.     if (y & TIOCM_CTS) z |= BM_CTS;
  13231.     debug(F101,"ttgmdm TIOCM_CTS defined","",TIOCM_CTS); 
  13232. #else
  13233.     debug(F100,"ttgmdm TIOCM_CTS not defined","",0);
  13234. #endif
  13235. #ifdef TIOCM_DSR
  13236.     /* Data Set Ready */
  13237.     if (y & TIOCM_DSR) z |= BM_DSR;
  13238.     debug(F101,"ttgmdm TIOCM_DSR defined","",TIOCM_DSR); 
  13239. #else
  13240.     debug(F100,"ttgmdm TIOCM_DSR not defined","",0);
  13241. #endif
  13242. #ifdef TIOCM_CAR
  13243.     /* Carrier */
  13244.     if (y & TIOCM_CAR) z |= BM_DCD;
  13245.     debug(F101,"ttgmdm TIOCM_CAR defined","",TIOCM_CAR); 
  13246. #else
  13247.     debug(F100,"ttgmdm TIOCM_CAR not defined","",0);
  13248. #endif
  13249. #ifdef TIOCM_RNG
  13250.     /* Ring Indicate */
  13251.     if (y & TIOCM_RNG) z |= BM_RNG;
  13252.     debug(F101,"ttgmdm TIOCM_RNG defined","",TIOCM_RNG); 
  13253. #else
  13254.     debug(F100,"ttgmdm TIOCM_RNG not defined","",0);
  13255. #endif
  13256. #ifdef TIOCM_DTR
  13257.     /* Data Terminal Ready */
  13258.     if (y & TIOCM_DTR) z |= BM_DTR;
  13259.     debug(F101,"ttgmdm TIOCM_DTR defined","",TIOCM_DTR); 
  13260. #else
  13261.     debug(F100,"ttgmdm TIOCM_DTR not defined","",0);
  13262. #endif
  13263. #ifdef TIOCM_RTS
  13264.     /* Request To Send */
  13265.     if (y & TIOCM_RTS) z |= BM_RTS;
  13266.     debug(F101,"ttgmdm TIOCM_RTS defined","",TIOCM_RTS); 
  13267. #else
  13268.     debug(F100,"ttgmdm TIOCM_RTS not defined","",0);
  13269. #endif
  13270.     gotsigs = 1;
  13271.     return(z);
  13272.  
  13273. #else /* !K_MDMCTL catch-All */
  13274.  
  13275.     debug(F100,"ttgmdm K_MDMCTL not defined","",0);
  13276. #ifdef TIOCMGET
  13277.     debug(F100,"ttgmdm TIOCMGET defined","",0);
  13278. #else
  13279.     debug(F100,"ttgmdm TIOCMGET not defined","",0);
  13280. #endif /* TIOCMGET */
  13281. #ifdef _SVID3
  13282.     debug(F100,"ttgmdm _SVID3 defined","",0);
  13283. #else
  13284.     debug(F100,"ttgmdm _SVID3 not defined","",0);
  13285. #endif /* _SVID3 */
  13286.  
  13287. #ifdef NETCONN
  13288.     if (netconn) {            /* Network connection */
  13289. #ifdef TN_COMPORT
  13290.         if (istncomport()) {
  13291.         gotsigs = 1;
  13292.         return(tngmdm());
  13293.     } else
  13294. #endif /* TN_COMPORT */
  13295.       return(-2);            /* No modem signals */
  13296.     }
  13297. #endif /* NETCONN */
  13298.  
  13299. #ifdef NETCMD
  13300.     if (ttpipe) return(-2);
  13301. #endif /* NETCMD */
  13302. #ifdef NETPTY
  13303.     if (ttpty) return(-2);
  13304. #endif /* NETPTY */
  13305.  
  13306.     return(-3);                /* Sorry, I don't know how... */
  13307.  
  13308. #endif /* K_MDMCTL */
  13309. #endif /* HPUX */
  13310. #endif /* QNX */
  13311. }
  13312.  
  13313. /*  P S U S P E N D  --  Put this process in the background.  */
  13314.  
  13315. /*
  13316.   Call with flag nonzero if suspending is allowed, zero if not allowed.
  13317.   Returns 0 on apparent success, -1 on failure (flag was zero, or
  13318.   kill() returned an error code.
  13319. */
  13320. int
  13321. psuspend(flag) int flag; {
  13322.  
  13323. #ifdef RTU
  13324.     extern int rtu_bug;
  13325. #endif /* RTU */
  13326.  
  13327.     if (flag == 0) return(-1);
  13328.  
  13329. #ifdef NOJC
  13330.     return(-1);
  13331. #else
  13332. #ifdef SIGTSTP
  13333. /*
  13334.   The big question here is whether job control is *really* supported.
  13335.   There's no way Kermit can know for sure.  The fact that SIGTSTP is
  13336.   defined does not guarantee the Unix kernel supports it, and the fact
  13337.   that the Unix kernel supports it doesn't guarantee that the user's
  13338.   shell (or other process that invoked Kermit) supports it.
  13339. */
  13340. #ifdef RTU
  13341.     rtu_bug = 1;
  13342. #endif /* RTU */
  13343.     if (kill(0,SIGSTOP) < 0
  13344. #ifdef MIPS
  13345. /* Let's try this for MIPS too. */
  13346.     && kill(getpid(),SIGSTOP) < 0
  13347. #endif /* MIPS */
  13348.     ) {                /* If job control, suspend the job */
  13349.     perror("suspend");
  13350.     debug(F101,"psuspend error","",errno);
  13351.     return(-1);
  13352.     }
  13353.     debug(F100,"psuspend ok","",0);
  13354.     return(0);
  13355. #else
  13356.     return(-1);
  13357. #endif /* SIGTSTP */
  13358. #endif /* NOJC */
  13359. }
  13360.  
  13361. /*
  13362.   setuid package, by Kristoffer Eriksson, with contributions from Dean
  13363.   Long and fdc.
  13364. */
  13365.  
  13366. /* The following is for SCO when CK_ANSILIBS is defined... */
  13367. #ifdef M_UNIX
  13368. #ifdef CK_ANSILIBS
  13369. #ifndef NOGETID_PROTOS
  13370. #define NOGETID_PROTOS
  13371. #endif /* NOGETID_PROTOS */
  13372. #endif /* CK_ANSILIBS */
  13373. #endif /* M_UNIX */
  13374.  
  13375. #ifndef _POSIX_SOURCE
  13376. #ifndef SUNOS4
  13377. #ifndef NEXT
  13378. #ifndef PS2AIX10
  13379. #ifndef sequent
  13380. #ifndef HPUX9
  13381. #ifndef COHERENT
  13382. #ifndef NOGETID_PROTOS
  13383. extern UID_T getuid(), geteuid(), getreuid();
  13384. extern GID_T getgid(), getegid(), getregid();
  13385. #endif /* NOGETID_PROTOS */
  13386. #else
  13387. extern UID_T getreuid();
  13388. extern GID_T getregid();
  13389. #endif /* COHERENT */
  13390. #endif /* HPUX9 */
  13391. #endif /* sequent */
  13392. #endif /* PS2AIX10 */
  13393. #endif /* NEXT */
  13394. #endif /* SUNOS4 */
  13395. #endif /* _POSIX_SOURCE */
  13396.  
  13397. /*
  13398. Subject: Set-user-id
  13399. To: fdc@watsun.cc.columbia.edu (Frank da Cruz)
  13400. Date: Sat, 21 Apr 90 4:48:25 MES
  13401. From: Kristoffer Eriksson <ske@pkmab.se>
  13402.  
  13403. This is a set of functions to be used in programs that may be run set-user-id
  13404. and/or set-group-id. They handle both the case where the program is not run
  13405. with such privileges (nothing special happens then), and the case where one
  13406. or both of these set-id modes are used.  The program is made to run with the
  13407. user's real user and group ids most of the time, except for when more
  13408. privileges are needed.  Don't set-user-id to "root".
  13409.  
  13410. This works on System V and POSIX.  In BSD, it depends on the
  13411. "saved-set-user-id" feature.
  13412. */
  13413.  
  13414. #define UID_ROOT 0            /* Root user and group ids */
  13415. #define GID_ROOT 0
  13416.  
  13417. /*
  13418.   The following code defines the symbol SETEUID for UNIX systems based
  13419.   on BSD4.4 (either -Encumbered or -Lite).  This program will then use
  13420.   seteuid() and setegid() instead of setuid() and setgid(), which still
  13421.   don't allow arbitrary switching.  It also avoids setreuid() and
  13422.   setregid(), which are included in BSD4.4 for compatibility only, are
  13423.   insecure, and print warnings to stderr under at least one system (NetBSD
  13424.   1.0).  Note that POSIX systems should still use setuid() and setgid();
  13425.   the seteuid() and setegid() functions are BSD4.4 extensions to the
  13426.   POSIX model.  Mike Long <mike.long@analog.com>, 8/94.
  13427. */
  13428. #ifdef BSD44
  13429. #define SETEUID
  13430. #endif /* BSD44 */
  13431.  
  13432. /*
  13433.   The following construction automatically defines the symbol SETREUID for
  13434.   UNIX versions based on Berkeley Unix 4.2 and 4.3.  If this symbol is
  13435.   defined, then this program will use getreuid() and getregid() calls in
  13436.   preference to getuid() and getgid(), which in Berkeley-based Unixes do
  13437.   not allow arbitrary switching back and forth of real & effective uid.
  13438.   This construction also allows -DSETREUID to be put on the cc command line
  13439.   for any system that has and wants to use setre[ug]id().  It also prevents
  13440.   automatic definition of SETREUID if -DNOSETREU is included on the cc
  13441.   command line (or otherwise defined).
  13442. */
  13443. #ifdef FT18                /* None of this for Fortune. */
  13444. #define NOSETREU
  13445. #endif /* FT18 */
  13446.  
  13447. #ifdef ANYBSD
  13448. #ifndef BSD29
  13449. #ifndef BSD41
  13450. #ifndef SETREUID
  13451. #ifndef NOSETREU
  13452. #ifndef SETEUID
  13453. #define SETREUID
  13454. #endif /* SETEUID */
  13455. #endif /* NOSETREU */
  13456. #endif /* SETREUID */
  13457. #endif /* !BSD41 */
  13458. #endif /* !BSD29 */
  13459. #endif /* ANYBSD */
  13460.  
  13461. /* Variables for user and group IDs. */
  13462.  
  13463. static UID_T realuid = (UID_T) -1, privuid = (UID_T) -1;
  13464. static GID_T realgid = (GID_T) -1, privgid = (GID_T) -1;
  13465.  
  13466.  
  13467. /* P R I V _ I N I  --  Initialize privileges package  */
  13468.  
  13469. /* Called as early as possible in a set-uid or set-gid program to store the
  13470.  * set-to uid and/or gid and step down to the users real uid and gid. The
  13471.  * stored id's can be temporarily restored (allowed in System V) during
  13472.  * operations that require the privilege.  Most of the time, the program
  13473.  * should execute in unpriviliged state, to not impose any security threat.
  13474.  *
  13475.  * Note: Don't forget that access() always uses the real id:s to determine
  13476.  * file access, even with privileges restored.
  13477.  *
  13478.  * Returns an error mask, with error values or:ed together:
  13479.  *   1 if setuid() fails,
  13480.  *   2 if setgid() fails, and
  13481.  *   4 if the program is set-user-id to "root", which can't be handled.
  13482.  *
  13483.  * Only the return value 0 indicates real success. In case of failure,
  13484.  * those privileges that could be reduced have been, at least, but the
  13485.  * program should be aborted none-the-less.
  13486.  *
  13487.  * Also note that these functions do not expect the uid or gid to change
  13488.  * without their knowing. It may work if it is only done temporarily, but
  13489.  * you're on your own.
  13490.  */
  13491. int
  13492. priv_ini() {
  13493.     int err = 0;
  13494.  
  13495.     /* Save real ID:s. */
  13496.     realuid = getuid();
  13497.     realgid = getgid();
  13498.  
  13499.     /* Save current effective ID:s, those set to at program exec. */
  13500.     privuid = geteuid();
  13501.     privgid = getegid();
  13502.  
  13503.     /* If running set-uid, go down to real uid, otherwise remember that
  13504.      * no privileged uid is available.
  13505.      *
  13506.      * Exceptions:
  13507.      *
  13508.      * 1) If the real uid is already "root" and the set-uid uid (the
  13509.      * initial effective uid) is not "root", then we would have trouble
  13510.      * if we went "down" to "root" here, and then temporarily back to the
  13511.      * set-uid uid (not "root") and then again tried to become "root". I
  13512.      * think the "saved set-uid" is lost when changing uid from effective
  13513.      * uid "root", which changes all uid, not only the effective uid. But
  13514.      * in this situation, we can simply go to "root" and stay there all
  13515.      * the time. That should give sufficient privilege (understatement!),
  13516.      * and give the right uids for subprocesses.
  13517.      *
  13518.      * 2) If the set-uid (the initial effective uid) is "root", and we
  13519.      * change uid to the real uid, we can't change it back to "root" when
  13520.      * we need the privilege, for the same reason as in 1). Thus, we can't
  13521.      * handle programs that are set-user-id to "root" at all. The program
  13522.      * should be stopped.  Use some other uid.  "root" is probably too
  13523.      * privileged for such things, anyway. (The uid is reverted to the
  13524.      * real uid until termination.)
  13525.      *
  13526.      * These two exceptions have the effect that the "root" uid will never
  13527.      * be one of the two uids that are being switched between, which also
  13528.      * means we don't have to check for such cases in the switching
  13529.      * functions.
  13530.      *
  13531.      * Note that exception 1) is handled by these routines (by constantly
  13532.      * running with uid "root", while exception 2) is a serious error, and
  13533.      * is not provided for at all in the switching functions.
  13534.      */
  13535.     if (realuid == privuid)
  13536.     privuid = (UID_T) -1;        /* Not running set-user-id. */
  13537.  
  13538.     /* If running set-gid, go down to real gid, otherwise remember that
  13539.      * no privileged gid is available.
  13540.      *
  13541.      * There are no exception like there is for the user id, since there
  13542.      * is no group id that is privileged in the manner of uid "root".
  13543.      * There could be equivalent problems for group changing if the
  13544.      * program sometimes ran with uid "root" and sometimes not, but
  13545.      * that is already avoided as explained above.
  13546.      *
  13547.      * Thus we can expect always to be able to switch to the "saved set-
  13548.      * gid" when we want, and back to the real gid again. You may also
  13549.      * draw the conclusion that set-gid provides for fewer hassles than
  13550.      * set-uid.
  13551.      */
  13552.  
  13553. #ifdef SUIDDEBUG
  13554.     fprintf(stderr,"UID_ROOT=%d\n",UID_ROOT);
  13555.     fprintf(stderr,"realuid=%d\n",realuid);
  13556.     fprintf(stderr,"privuid=%d\n",privuid);
  13557. #endif /* SUIDDEBUG */
  13558.  
  13559.     if (realgid == privgid)        /* If not running set-user-id, */
  13560.       privgid = (GID_T) -1;        /*  remember it this way. */
  13561.  
  13562.     err = priv_off();            /* Turn off setuid privilege. */
  13563.  
  13564.     if (privuid == UID_ROOT)        /* If setuid to root, */
  13565.       err |= 4;                /* return this error. */
  13566.  
  13567.     if (realuid == UID_ROOT) {        /* If real id is root, */
  13568.     privuid = (UID_T) -1;        /* stay root at all times. */
  13569. #ifdef ATT7300
  13570.     /* If Kermit installed SUID uucp and user is running as root */
  13571.     err &= ~1;            /* System V R0 does not save UID */
  13572. #endif /* ATT7300 */
  13573.     }
  13574.     return(err);
  13575. }
  13576.  
  13577.  
  13578. /* Macros for hiding the differences in UID/GID setting between various Unix
  13579.  * systems. These macros should always be called with both the privileged ID
  13580.  * and the non-privileged ID. The one in the second argument, will become the
  13581.  * effective ID. The one in the first argument will be retained for later
  13582.  * retrieval.
  13583.  */
  13584. #ifdef SETREUID
  13585. #ifdef SAVEDUID
  13586. /* On BSD systems with the saved-UID feature, we just juggle the effective
  13587.  * UID back and forth, and leave the real UID at its true value.  The kernel
  13588.  * allows switching to both the current real UID, the effective UID, and the
  13589.  * UID which the program is set-UID to.  The saved set-UID always holds the
  13590.  * privileged UID for us, and the real UID will always be the non-privileged,
  13591.  * and we can freely choose one of them for the effective UID at any time.
  13592.  */
  13593. #define switchuid(hidden,active) setreuid( (UID_T) -1, active)
  13594. #define switchgid(hidden,active) setregid( (GID_T) -1, active)
  13595.  
  13596. #else   /* SETREUID,!SAVEDUID */
  13597.  
  13598. /* On systems with setreXid() but without the saved-UID feature, notably
  13599.  * BSD 4.2, we swap the real and effective UIDs each time.  It's
  13600.  * the effective UID that we are interested in, but we have to retain the
  13601.  * unused UID somewhere to enable us to restore it later, and we do this
  13602.  * in the real UID.  The kernel only allows switching to either the current
  13603.  * real or the effective UID, unless you're "root".
  13604.  */
  13605. #define switchuid(hidden,active)    setreuid(hidden,active)
  13606. #define switchgid(hidden,active)    setregid(hidden,active)
  13607. #endif
  13608.  
  13609. #else /* !SETREUID, !SAVEDUID */
  13610.  
  13611. #ifdef SETEUID
  13612. /*
  13613.   BSD 4.4 works similarly to System V and POSIX (see below), but uses
  13614.   seteXid() instead of setXid() to change effective IDs.  In addition, the
  13615.   seteXid() functions work the same for "root" as for other users.
  13616. */
  13617. #define switchuid(hidden,active)    seteuid(active)
  13618. #define switchgid(hidden,active)    setegid(active)
  13619.  
  13620. #else /* !SETEUID */
  13621.  
  13622. /* On System V and POSIX, the only thing we can change is the effective UID
  13623.  * (unless the current effective UID is "root", but initsuid() avoids that for
  13624.  * us).  The kernel allows switching to the current real UID or to the saved
  13625.  * set-UID.  These are always set to the non-privileged UID and the privileged
  13626.  * UID, respectively, and we only change the effective UID.  This breaks if
  13627.  * the current effective UID is "root", though, because for "root" setuid/gid
  13628.  * becomes more powerful, which is why initsuid() treats "root" specially.
  13629.  * Note: That special treatment maybe could be ignored for BSD?  Note: For
  13630.  * systems that don't fit any of these four cases, we simply can't support
  13631.  * set-UID.
  13632.  */
  13633. #define switchuid(hidden,active)    setuid(active)
  13634. #define switchgid(hidden,active)    setgid(active)
  13635.  
  13636. #endif /* SETEUID */
  13637. #endif /* SETREUID */
  13638.  
  13639.  
  13640. /* P R I V _ O N  --  Turn on the setuid and/or setgid */
  13641.  
  13642. /* Go to the privileged uid (gid) that the program is set-user-id
  13643.  * (set-group-id) to, unless the program is running unprivileged.
  13644.  * If setuid() fails, return value will be 1. If getuid() fails it
  13645.  * will be 2.  Return immediately after first failure, and the function
  13646.  * tries to restore any partial work done.  Returns 0 on success.
  13647.  * Group id is changed first, since it is less serious than user id.
  13648.  */
  13649. int
  13650. priv_on() {
  13651.     if (privgid != (GID_T) -1)
  13652.       if (switchgid(realgid,privgid))
  13653.         return(2);
  13654.  
  13655.     if (privuid != (UID_T) -1)
  13656.       if (switchuid(realuid,privuid)) {
  13657.       if (privgid != (GID_T) -1)
  13658.         switchgid(privgid,realgid);
  13659.       return(1);
  13660.       }
  13661.     return(0);
  13662. }
  13663.  
  13664. /* P R I V _ O F F  --  Turn on the real uid and gid */
  13665.  
  13666. /* Return to the unprivileged uid (gid) after an temporary visit to
  13667.  * privileged status, unless the program is running without set-user-id
  13668.  * (set-group-id). Returns 1 for failure in setuid() and 2 for failure
  13669.  * in setgid() or:ed together. The functions tries to return both uid
  13670.  * and gid to unprivileged state, regardless of errors. Returns 0 on
  13671.  * success.
  13672.  */
  13673. int
  13674. priv_off() {
  13675.     int err = 0;
  13676.  
  13677.     if (privuid != (UID_T) -1)
  13678.        if (switchuid(privuid,realuid))
  13679.       err |= 1;
  13680.  
  13681.     if (privgid != (GID_T) -1)
  13682.        if (switchgid(privgid,realgid))
  13683.     err |= 2;
  13684.  
  13685.     return(err);
  13686. }
  13687.  
  13688. /* Turn off privilege permanently.  No going back.  This is necessary before
  13689.  * a fork() on BSD43 machines that don't save the setUID or setGID, because
  13690.  * we swap the real and effective ids, and we don't want to let the forked
  13691.  * process swap them again and get the privilege back. It will work on other
  13692.  * machines too, such that you can rely on its effect always being the same,
  13693.  * for instance, even when you're in priv_on() state when this is called.
  13694.  * (Well, that part about "permanent" is on System V only true if you follow
  13695.  * this with a call to exec(), but that's what we want it for anyway.)
  13696.  * Added by Dean Long -- dlong@midgard.ucsc.edu
  13697.  */
  13698. int
  13699. priv_can() {
  13700.  
  13701. #ifdef SETREUID
  13702.     int err = 0;
  13703.     if (privuid != (UID_T) -1)
  13704.        if (setreuid(realuid,realuid))
  13705.       err |= 1;
  13706.  
  13707.     if (privgid != (GID_T) -1)
  13708.         if (setregid(realgid,realgid))
  13709.        err |= 2;
  13710.  
  13711.     return(err);
  13712.  
  13713. #else
  13714. #ifdef SETEUID
  13715.     int err = 0;
  13716.     if (privuid != (UID_T) -1)
  13717.     if (setuid(realuid)) {
  13718.         debug(F101,"setuid failed","",errno);
  13719.         err |= 1;
  13720.         debug(F101,"ruid","",getuid());
  13721.         debug(F101,"euid","",geteuid());
  13722.     }
  13723.     debug(F101,"setuid","",realuid);
  13724.     if (privgid != (GID_T) -1)
  13725.         if (setgid(realgid)) {
  13726.         debug(F101,"setgid failed","",errno);
  13727.         err |= 2;
  13728.         debug(F101,"rgid","",getgid());
  13729.         debug(F101,"egid","",getegid());
  13730.     }
  13731.     debug(F101,"setgid","",realgid);
  13732.     return(err);
  13733. #else
  13734.     /* Easy way of using setuid()/setgid() instead of setreuid()/setregid().*/
  13735.     return(priv_off());
  13736. #endif /* SETEUID */
  13737. #endif /* SETREUID */
  13738. }
  13739.  
  13740. /* P R I V _ O P N  --  For opening protected files or devices. */
  13741.  
  13742. int
  13743. priv_opn(name, modes) char *name; int modes; {
  13744.     int x;
  13745.     priv_on();                /* Turn privileges on */
  13746.     debug(F111,"priv_opn",name,modes);
  13747.     errno = 0;
  13748.     x = open(name, modes);        /* Try to open the device */
  13749.     debug(F101,"priv_opn result","",x);
  13750.     debug(F101,"priv_opn errno","",errno);
  13751.     priv_off();                /* Turn privileges off */
  13752.     return(x);                /* Return open's return code */
  13753. }
  13754.  
  13755. /*  P R I V _ C H K  --  Check privileges.  */
  13756.  
  13757. /*  Try to turn them off.  If turning them off did not succeed, cancel them */
  13758.  
  13759. int
  13760. priv_chk() {
  13761.     int x, y = 0;
  13762.     x = priv_off();            /* Turn off privs. */
  13763.     if (x != 0 || getuid() == privuid || geteuid() == privuid)
  13764.       y = priv_can();
  13765.     if (x != 0 || getgid() == privgid || getegid() == privgid)
  13766.       y = y | priv_can();
  13767.     return(y);
  13768. }
  13769.  
  13770. UID_T
  13771. real_uid() {
  13772.     return(realuid);
  13773. }
  13774.  
  13775. VOID
  13776. ttimoff() {                /* Turn off any timer interrupts */
  13777.     /* int xx; */
  13778. /*
  13779.   As of 5A(183), we set SIGALRM to SIG_IGN (to ignore alarms) rather than to
  13780.   SIG_DFL (to catch alarms, or if there is no handler, to exit).  This is to
  13781.   cure (mask, really) a deeper problem with stray alarms that occurs on some
  13782.   systems, possibly having to do with sleep(), that caused core dumps.  It
  13783.   should be OK to do this, because no code in this module uses nested alarms.
  13784.   (But we still have to watch out for SCRIPT and DIAL...)
  13785. */
  13786.     /* xx = */ alarm(0);
  13787.     /* debug(F101,"ttimoff alarm","",xx); */
  13788.     if (saval) {            /* Restore any previous */
  13789.     signal(SIGALRM,saval);        /* alarm handler. */
  13790.     /* debug(F101,"ttimoff alarm restoring saval","",saval); */
  13791.     saval = NULL;
  13792.     } else {
  13793.     signal(SIGALRM,SIG_IGN);    /* Used to be SIG_DFL */
  13794.     /* debug(F100,"ttimoff alarm SIG_IGN","",0); */
  13795.     }
  13796. }
  13797.  
  13798. /* T T R U N C M D  --  Redirect an external command over the connection. */
  13799.  
  13800. #ifdef CK_REDIR
  13801. int
  13802. ttruncmd(s) char *s; {
  13803.     PID_T pid;                /* pid of lower fork */
  13804.     int wstat;                /* for wait() */
  13805.     int x;
  13806.     int statusp;
  13807.  
  13808.     if (ttyfd == -1) {
  13809.     printf("?Sorry, device is not open\n");
  13810.     return(0);
  13811.     }
  13812.     if (nopush) {
  13813.     debug(F100,"ttruncmd fail: nopush","",0);
  13814.     return(0);
  13815.     }
  13816.     conres();                /* Make console normal  */
  13817.     pexitstat = -4;
  13818.     if ((pid = fork()) == 0) {        /* Make a child fork */
  13819.     if (priv_can())            /* Child: turn off privs. */
  13820.       exit(1);
  13821.     dup2(ttyfd, 0);            /* Give stdin/out to the line */
  13822.     dup2(ttyfd, 1);
  13823.     x = system(s);
  13824.     debug(F101,"ttruncmd system",s,x);
  13825.     _exit(x ? BAD_EXIT : 0);
  13826.     } else {
  13827.     SIGTYP (*istat)(), (*qstat)();
  13828.     if (pid == (PID_T) -1)        /* fork() failed? */
  13829.       return(0);
  13830.     istat = signal(SIGINT,SIG_IGN); /* Let the fork handle keyboard */
  13831.     qstat = signal(SIGQUIT,SIG_IGN); /* interrupts itself... */
  13832.  
  13833. #ifdef COMMENT
  13834.         while (((wstat = wait(&statusp)) != pid) && (wstat != -1)) ;
  13835. #else  /* Not COMMENT */
  13836.         while (1) {
  13837.         wstat = wait(&statusp);
  13838.         debug(F101,"ttruncmd wait","",wstat);
  13839.         if (wstat == pid || wstat == -1)
  13840.           break;
  13841.     }
  13842. #endif /* COMMENT */
  13843.  
  13844.     pexitstat = (statusp & 0xff) ? statusp : statusp >> 8;
  13845.     debug(F101,"ttruncmd wait statusp","",statusp);
  13846.     debug(F101,"ttruncmd wait pexitstat","",pexitstat);
  13847.     signal(SIGINT,istat);        /* Restore interrupts */
  13848.     signal(SIGQUIT,qstat);
  13849.     }
  13850.     concb((char)escchr);        /* Restore console to CBREAK mode */
  13851.     return(statusp == 0 ? 1 : 0);
  13852. }
  13853. #endif /* CK_REDIR */
  13854.  
  13855. struct tm *
  13856. #ifdef CK_ANSIC
  13857. cmdate2tm(char * date, int gmt)         /* date as "yyyymmdd hh:mm:ss" */
  13858. #else
  13859. cmdate2tm(date,gmt) char * date; int gmt;
  13860. #endif
  13861. {
  13862.     /* date as "yyyymmdd hh:mm:ss" */
  13863.     static struct tm _tm;
  13864.     time_t now;
  13865.  
  13866.     if (strlen(date) != 17 ||
  13867.     date[8] != ' ' ||
  13868.     date[11] != ':' ||
  13869.     date[14] != ':')
  13870.       return(NULL);
  13871.  
  13872.     time(&now);
  13873.     if (gmt)
  13874.       _tm = *gmtime(&now);
  13875.     else
  13876.       _tm = *localtime(&now);
  13877.     _tm.tm_year = (date[0]-'0')*1000 + (date[1]-'0')*100 +
  13878.                   (date[2]-'0')*10   + (date[3]-'0')-1900;
  13879.     _tm.tm_mon  = (date[4]-'0')*10   + (date[5]-'0')-1;
  13880.     _tm.tm_mday = (date[6]-'0')*10   + (date[7]-'0');
  13881.     _tm.tm_hour = (date[9]-'0')*10   + (date[10]-'0');
  13882.     _tm.tm_min  = (date[12]-'0')*10  + (date[13]-'0');
  13883.     _tm.tm_sec  = (date[15]-'0')*10  + (date[16]-'0');
  13884.  
  13885.     /* Should we set _tm.tm_isdst to -1 here? */
  13886.  
  13887.     _tm.tm_wday = 0;
  13888.     _tm.tm_yday = 0;
  13889.  
  13890.     return(&_tm);
  13891. }
  13892.  
  13893. #ifdef OXOS
  13894. #undef kill
  13895. #endif /* OXOS */
  13896.  
  13897. #ifdef OXOS
  13898. int
  13899. priv_kill(pid, sig) int pid, sig; {
  13900.     int    i;
  13901.  
  13902.     if (priv_on())
  13903.     debug(F100,"priv_kill priv_on failed","",0);
  13904.     i = kill(pid, sig);
  13905.     if (priv_off())
  13906.     debug(F100,"priv_kill priv_off failed","",0);
  13907.     return(i);
  13908. }
  13909. #endif /* OXOS */
  13910.  
  13911. #ifdef BEOSORBEBOX
  13912. /* #ifdef BE_DR_7 */
  13913. /*
  13914.   alarm() function not supplied with Be OS DR7 - this one contributed by
  13915.   Neal P. Murphy.
  13916. */
  13917.  
  13918. /*
  13919.   This should mimic the UNIX/POSIX alarm() function well enough, with the
  13920.   caveat that one's SIGALRM handler must call alarm_expired() to clean up vars
  13921.   and wait for the alarm thread to finish.
  13922. */
  13923. unsigned int
  13924. alarm(unsigned int seconds) {
  13925.     long time_left = 0;
  13926.  
  13927. /* If an alarm is active, turn it off, saving the unused time */
  13928.     if (alarm_thread != -1) {
  13929.         /* We'll be generous and count partial seconds as whole seconds. */
  13930.         time_left = alarm_struct.time -
  13931.       ((system_time() - time_started) / 1000000.0);
  13932.  
  13933.         /* Kill the alarm thread */
  13934.         kill_thread (alarm_thread);
  13935.  
  13936.         /* We need to clean up as though the alarm occured. */
  13937.         time_started = 0;
  13938.         alarm_struct.thread = -1;
  13939.         alarm_struct.time = 0;
  13940.         alarm_expired();
  13941.     }
  13942.  
  13943. /* Set a new alarm clock, if requested. */
  13944.     if (seconds > 0) {
  13945.         alarm_struct.thread = find_thread(NULL);
  13946.         alarm_struct.time = seconds;
  13947.         time_started = system_time();
  13948.         alarm_thread = spawn_thread (do_alarm,
  13949.                                      "alarm_thread",
  13950.                                      B_NORMAL_PRIORITY,
  13951.                                      (void *) &alarm_struct
  13952.                      );
  13953.         resume_thread (alarm_thread);
  13954.     }
  13955.  
  13956. /* Now return [unused time | 0] */
  13957.     return ((unsigned int) time_left);
  13958. }
  13959.  
  13960. /*
  13961.   This function is the departure from UNIX/POSIX alarm handling. In the case
  13962.   of Be's missing alarm() function, this stuff needs to be done in the SIGALRM
  13963.   handler. When Be implements alarm(), this function call can be eliminated
  13964.   from user's SIGALRM signal handlers.
  13965. */
  13966.  
  13967. void
  13968. alarm_expired(void) {
  13969.     long ret_val;
  13970.  
  13971.     if (alarm_thread != -1) {
  13972.         wait_for_thread (alarm_thread, &ret_val);
  13973.         alarm_thread = -1;
  13974.     }
  13975. }
  13976.  
  13977. /*
  13978.   This is the function that snoozes the requisite number of seconds and then
  13979.   SIGALRMs the calling thread. Note that kill() wants a pid_t arg, whilst Be
  13980.   uses thread_id; currently they are both typdef'ed as long, but I'll do the
  13981.   cast anyway. This function is run in a separate thread.
  13982. */
  13983.  
  13984. long
  13985. do_alarm (void *alarm_struct) {
  13986.     snooze ((double) ((struct ALARM_STRUCT *) alarm_struct)->time * 1000000.0);
  13987.     kill ((pid_t)((struct ALARM_STRUCT *) alarm_struct)->thread, SIGALRM);
  13988.     time_started = 0;
  13989.     ((struct ALARM_STRUCT *) alarm_struct)->thread = -1;
  13990.     ((struct ALARM_STRUCT *) alarm_struct)->time = 0;
  13991. }
  13992. /* #endif */ /* BE_DR_7 */
  13993. #endif /* BEOSORBEBOX */
  13994.  
  13995. #ifdef Plan9
  13996.  
  13997. int
  13998. p9ttyctl(char letter, int num, int param) {
  13999.     char cmd[20];
  14000.     int len;
  14001.  
  14002.     if (ttyctlfd < 0)
  14003.       return -1;
  14004.  
  14005.     cmd[0] = letter;
  14006.     if (num)
  14007.       len = sprintf(cmd + 1, "%d", param) + 1;
  14008.     else {
  14009.     cmd[1] = param;
  14010.     len = 2;
  14011.     }
  14012.     if (write(ttyctlfd, cmd, len) == len) {
  14013.     cmd[len] = 0;
  14014.     /* fprintf(stdout, "wrote '%s'\n", cmd); */
  14015.     return 0;
  14016.     }
  14017.     return -1;
  14018. }
  14019.  
  14020. int
  14021. p9ttyparity(char l) {
  14022.     return p9ttyctl('p', 0, l);
  14023. }
  14024.  
  14025. int
  14026. p9tthflow(int flow, int status) {
  14027.     return p9ttyctl('m', 1, status);
  14028. }
  14029.  
  14030. int
  14031. p9ttsspd(int cps) {
  14032.     if (p9ttyctl('b', 1, cps * 10) < 0)
  14033.       return -1;
  14034.     ttylastspeed = cps * 10;
  14035.     return 0;
  14036. }
  14037.  
  14038. int
  14039. p9openttyctl(char *ttname) {
  14040.     char name[100];
  14041.  
  14042.     if (ttyctlfd >= 0) {
  14043.     close(ttyctlfd);
  14044.     ttyctlfd = -1;
  14045.     ttylastspeed = -1;
  14046.     }
  14047.     sprintf(name, "%sctl", ttname);
  14048.     ttyctlfd = open(name, 1);
  14049.     return ttyctlfd;
  14050. }
  14051.  
  14052. int
  14053. p9concb() {
  14054.     if (consctlfd >= 0) {
  14055.     if (write(consctlfd, "rawon", 5) == 5)
  14056.       return 0;
  14057.     }
  14058.     return -1;
  14059. }
  14060.  
  14061. int
  14062. p9conbin() {
  14063.     return p9concb();
  14064. }
  14065.  
  14066. int
  14067. p9conres() {
  14068.     if (consctlfd >= 0) {
  14069.     if (write(consctlfd, "rawoff", 6) == 6)
  14070.       return 0;
  14071.     }
  14072.     return -1;
  14073. }
  14074.  
  14075. int
  14076. p9sndbrk(int msec) {
  14077.     if (ttyctlfd >= 0) {
  14078.     char cmd[20];
  14079.     int i = sprintf(cmd, "k%d", msec);
  14080.     if (write(ttyctlfd, cmd, i) == i)
  14081.       return 0;
  14082.     }
  14083.     return -1;
  14084. }
  14085.  
  14086. int
  14087. conwrite(char *buf, int n) {
  14088.     int x;
  14089.     static int length = 0;
  14090.     static int holdingcr = 0;
  14091.     int normal = 0;
  14092.     for (x = 0; x < n; x++) {
  14093.     char c = buf[x];
  14094.     if (c == 007) {
  14095.         if (normal) {
  14096.         write(1, buf + (x - normal), normal);
  14097.         length += normal;
  14098.         normal = 0;
  14099.         }
  14100.         /* write(noisefd, "1000 300", 8); */
  14101.         holdingcr = 0;
  14102.     } else if (c == '\r') {
  14103.         if (normal) {
  14104.         write(1, buf + (x - normal), normal);
  14105.         length += normal;
  14106.         normal = 0;
  14107.         }
  14108.         holdingcr = 1;
  14109.     } else if (c == '\n') {
  14110.         write(1, buf + (x - normal), normal + 1);
  14111.         normal = 0;
  14112.         length = 0;
  14113.         holdingcr = 0;
  14114.     } else if (c == '\b') {
  14115.         if (normal) {
  14116.         write(1, buf + (x - normal), normal);
  14117.         length += normal;
  14118.         normal = 0;
  14119.         }
  14120.         if (length) {
  14121.         write(1, &c, 1);
  14122.         length--;
  14123.         }
  14124.         holdingcr = 0;
  14125.     } else {
  14126.         if (holdingcr) {
  14127.         char b = '\b';
  14128.         while (length-- > 0)
  14129.           write(1, &b, 1);
  14130.         length = 0;    /* compiler bug */
  14131.         }
  14132.         holdingcr = 0;
  14133.         normal++;
  14134.     }
  14135.     }
  14136.     if (normal) {
  14137.     write(1, buf + (x - normal), normal);
  14138.     length += normal;
  14139.     }
  14140.     return n;
  14141. }
  14142.  
  14143. void
  14144. conprint(char *fmt, ...) {
  14145.     static char buf[1000];        /* not safe if on the stack */
  14146.  
  14147.     va_list ap;
  14148.     int i;
  14149.  
  14150.     va_start(ap, fmt);
  14151.     i = vsprintf(buf, fmt, ap);
  14152.     conwrite(buf, i);
  14153. }
  14154. #endif /* Plan9 */
  14155.  
  14156. /* fprintf, printf, perror replacements... */
  14157.  
  14158. /* f p r i n t f */
  14159.  
  14160. #ifdef UNIX
  14161. #ifdef CK_ANSIC
  14162. #include <stdarg.h>
  14163. #else /* CK_ANSIC */
  14164. #include <varargs.h>
  14165. #endif /* CK_ANSIC */
  14166. #ifdef fprintf
  14167. #undef fprintf
  14168. static char str1[4096];
  14169. static char str2[4096];
  14170. int
  14171. #ifdef CK_ANSIC
  14172. ckxfprintf(FILE * file, const char * format, ...)
  14173. #else /* CK_ANSIC */
  14174. ckxfprintf(va_alist) va_dcl
  14175. #endif /* CK_ANSIC */
  14176. /* ckxfprintf */ {
  14177.     int i, j, len, got_cr;
  14178.     va_list args;
  14179.     int rc = 0;
  14180.  
  14181. #ifdef CK_ANSIC
  14182.     va_start(args, format);
  14183. #else /* CK_ANSIC */
  14184.     char * format;
  14185.     FILE * file;
  14186.     va_start(args);
  14187.     file = va_arg(args,FILE *);
  14188.     format = va_arg(args,char *);
  14189. #endif /* CK_ANSIC */
  14190.  
  14191.     if (!inserver || (file != stdout && file != stderr && file != stdin)) {
  14192.     rc = vfprintf(file,format,args);
  14193.     } else {
  14194.     unsigned int c;
  14195.         rc = vsprintf(str1, format, args);
  14196.         len = strlen(str1);
  14197.         if (len >= sizeof(str1)) {
  14198.             debug(F101,"ckxfprintf() buffer overflow","",len);
  14199.             doexit(BAD_EXIT,1);
  14200.         }
  14201.         for (i = 0, j = 0, got_cr = 0;
  14202.          i < len && j < sizeof(str1)-2;
  14203.          i++, j++ ) {
  14204.         /* We can't use 255 as a case label because of signed chars */
  14205.         c = (unsigned)(str1[i] & 0xff);
  14206. #ifdef TNCODE
  14207.         if (c == 255) {
  14208.         if (got_cr && !TELOPT_ME(TELOPT_BINARY))
  14209.           str2[j++] = '\0';
  14210.         str2[j++] = IAC;
  14211.         str2[j] = IAC;
  14212.         got_cr = 0;
  14213.         } else
  14214. #endif /* TNCODE */
  14215.         switch (c) {
  14216.           case '\r':
  14217.                 if (got_cr
  14218. #ifdef TNCODE
  14219.             && !TELOPT_ME(TELOPT_BINARY)
  14220. #endif /* TNCODE */
  14221.             )
  14222.           str2[j++] = '\0';
  14223.                 str2[j] = str1[i];
  14224.                 got_cr = 1;
  14225.                 break;
  14226.           case '\n':
  14227.                 if (!got_cr)
  14228.           str2[j++] = '\r';
  14229.                 str2[j] = str1[i];
  14230.                 got_cr = 0;
  14231.                 break;
  14232.           default:
  14233.                 if (got_cr
  14234. #ifdef TNCODE
  14235.             && !TELOPT_ME(TELOPT_BINARY)
  14236. #endif /* TNCODE */
  14237.             )
  14238.           str2[j++] = '\0';
  14239.                 str2[j] = str1[i];
  14240.                 got_cr = 0;
  14241.             }
  14242.         }
  14243.         if (got_cr
  14244. #ifdef TNCODE
  14245.              && !TELOPT_ME(TELOPT_BINARY)
  14246. #endif /* TNCODE */
  14247.              )
  14248.             str2[j++] = '\0';
  14249. #ifdef CK_ENCRYPTION
  14250. #ifdef TNCODE
  14251.         if (TELOPT_ME(TELOPT_ENCRYPTION))
  14252.       ck_tn_encrypt(str2,j);
  14253. #endif /* TNCODE */
  14254. #endif /* CK_ENCRYPTION */
  14255. #ifdef CK_SSL
  14256.     if (inserver && (ssl_active_flag || tls_active_flag)) {
  14257.         /* Write using SSL */
  14258.             char * p = str2;
  14259.           ssl_retry:
  14260.             if (ssl_active_flag)
  14261.           rc = SSL_write(ssl_con, p, j);
  14262.             else
  14263.           rc = SSL_write(tls_con, p, j);
  14264.         debug(F111,"ckxfprintf","SSL_write",rc);
  14265.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,rc)) {
  14266.           case SSL_ERROR_NONE:
  14267.                 if (rc == j)
  14268.           break;
  14269.                 p += rc;
  14270.                 j -= rc;
  14271.                 goto ssl_retry;
  14272.           case SSL_ERROR_WANT_WRITE:
  14273.           case SSL_ERROR_WANT_READ:
  14274.           case SSL_ERROR_SYSCALL:
  14275.                 if (rc != 0)
  14276.           return(-1);
  14277.           case SSL_ERROR_WANT_X509_LOOKUP:
  14278.           case SSL_ERROR_SSL:
  14279.           case SSL_ERROR_ZERO_RETURN:
  14280.           default:
  14281.                 rc = 0;
  14282.             }
  14283.     } else
  14284. #endif /* CK_SSL */
  14285.         fwrite(str2,sizeof(char),j,stdout);
  14286.     }
  14287.     va_end(args);
  14288.     return(rc);
  14289. }
  14290. #endif /* fprintf */
  14291.  
  14292. /* p r i n t f */
  14293.  
  14294. #ifdef printf
  14295. #undef printf
  14296. int
  14297. #ifdef CK_ANSIC
  14298. ckxprintf(const char * format, ...)
  14299. #else /* CK_ANSIC */
  14300. ckxprintf(va_alist) va_dcl
  14301. #endif /* CK_ANSIC */
  14302. /* ckxprintf */ {
  14303.     int i, j, len, got_cr;
  14304.     va_list args;
  14305.     int rc = 0;
  14306.  
  14307. #ifdef CK_ANSIC
  14308.     va_start(args, format);
  14309. #else /* CK_ANSIC */
  14310.     char * format;
  14311.     va_start(args);
  14312.     format = va_arg(args,char *);
  14313. #endif /* CK_ANSIC */
  14314.  
  14315.     if (!inserver) {
  14316.     rc = vprintf(format, args);
  14317.     } else {
  14318.     unsigned int c;
  14319.         rc = vsprintf(str1, format, args);
  14320.         len = strlen(str1);
  14321.         if (len >= sizeof(str1)) {
  14322.             debug(F101,"ckxprintf() buffer overflow","",len);
  14323.             doexit(BAD_EXIT,1);
  14324.         }
  14325.         for (i = 0, j = 0, got_cr=0;
  14326.          i < len && j < sizeof(str1)-2;
  14327.          i++, j++ ) {
  14328.         c = (unsigned)(str1[i] & 0xff);
  14329. #ifdef TNCODE
  14330.         if (c == 255) {
  14331.         if (got_cr && !TELOPT_ME(TELOPT_BINARY))
  14332.           str2[j++] = '\0';
  14333.         str2[j++] = IAC;
  14334.         str2[j] = IAC;
  14335.         got_cr = 0;
  14336.         } else
  14337. #endif /* TNCODE */
  14338.         switch (c) {
  14339.           case '\r':
  14340.                 if (got_cr
  14341. #ifdef TNCODE
  14342.             && !TELOPT_ME(TELOPT_BINARY)
  14343. #endif /* TNCODE */
  14344.             )
  14345.           str2[j++] = '\0';
  14346.                 str2[j] = str1[i];
  14347.                 got_cr = 1;
  14348.                 break;
  14349.           case '\n':
  14350.                 if (!got_cr)
  14351.           str2[j++] = '\r';
  14352.                 str2[j] = str1[i];
  14353.                 got_cr = 0;
  14354.                 break;
  14355.           default:
  14356.                 if (got_cr
  14357. #ifdef TNCODE
  14358.             && !TELOPT_ME(TELOPT_BINARY)
  14359. #endif /* TNCODE */
  14360.             )
  14361.           str2[j++] = '\0';
  14362.                 str2[j] = str1[i];
  14363.                 got_cr = 0;
  14364.                 break;
  14365.         }
  14366.         }
  14367.         if (got_cr
  14368. #ifdef TNCODE
  14369.              && !TELOPT_ME(TELOPT_BINARY)
  14370. #endif /* TNCODE */
  14371.              )
  14372.             str2[j++] = '\0';
  14373. #ifdef CK_ENCRYPTION
  14374. #ifdef TNCODE
  14375.         if (TELOPT_ME(TELOPT_ENCRYPTION))
  14376.       ck_tn_encrypt(str2,j);
  14377. #endif /* TNCODE */
  14378. #endif /* CK_ENCRYPTION */
  14379. #ifdef CK_SSL
  14380.     if (inserver && (ssl_active_flag || tls_active_flag)) {
  14381.             char * p = str2;
  14382.         /* Write using SSL */
  14383.           ssl_retry:
  14384.             if (ssl_active_flag)
  14385.           rc = SSL_write(ssl_con, p, j);
  14386.             else
  14387.           rc = SSL_write(tls_con, p, j);
  14388.         debug(F111,"ckxprintf","SSL_write",rc);
  14389.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,rc)) {
  14390.           case SSL_ERROR_NONE:
  14391.                 if (rc == j)
  14392.           break;
  14393.                 p += rc;
  14394.                 j -= rc;
  14395.                 goto ssl_retry;
  14396.           case SSL_ERROR_WANT_WRITE:
  14397.           case SSL_ERROR_WANT_READ:
  14398.           case SSL_ERROR_SYSCALL:
  14399.                 if (rc != 0)
  14400.           return(-1);
  14401.           case SSL_ERROR_WANT_X509_LOOKUP:
  14402.           case SSL_ERROR_SSL:
  14403.           case SSL_ERROR_ZERO_RETURN:
  14404.           default:
  14405.                 rc = 0;
  14406.             }
  14407.     } else
  14408. #endif /* CK_SSL */
  14409.       rc = fwrite(str2,sizeof(char),j,stdout);
  14410.     }
  14411.     va_end(args);
  14412.     return(rc);
  14413. }
  14414. #endif /* printf */
  14415.  
  14416. /*  p e r r o r  */
  14417.  
  14418. #ifdef perror
  14419. #undef perror
  14420. _PROTOTYP(char * ck_errstr,(VOID));
  14421. #ifdef NEXT
  14422. void
  14423. #else
  14424. #ifdef CK_SCOV5
  14425. void
  14426. #else
  14427. int
  14428. #endif /* CK_SCOV5 */
  14429. #endif /* NEXT */
  14430. #ifdef CK_ANSIC
  14431. ckxperror(const char * str)
  14432. #else /* CK_ANSIC */
  14433. ckxperror(str) char * str;
  14434. #endif /* CK_ANSIC */
  14435. /* ckxperror */ {
  14436.     char * errstr = ck_errstr();
  14437. #ifndef NEXT
  14438. #ifndef CK_SCOV5
  14439.     return
  14440. #endif /* CK_SCOV5 */
  14441. #endif /* NEXT */
  14442.       ckxprintf("%s%s %s\n",str,*errstr?":":"",errstr);
  14443. }
  14444. #endif /* perror */
  14445. #endif /* UNIX */
  14446.  
  14447. #ifdef MINIX2
  14448.  
  14449. /* Minix doesn't have a gettimeofday call. We fake one here using time(2) */
  14450.  
  14451. int
  14452. gettimeofday(struct timeval *tp, struct timezone *tzp) {
  14453.     tp->tv_usec = 0L;            /* Close enough for horseshoes */
  14454.     if(time(&(tp->tv_sec))==-1)
  14455.       return(-1);
  14456.     return(0);
  14457. }
  14458.  
  14459. /* Minix does not support symbolic links. We implement a version of
  14460.    readlink that always fails */
  14461.  
  14462. int
  14463. readlink(const char *path, void *buf, size_t bufsiz) {
  14464.     errno = ENOSYS;
  14465.     return(-1);
  14466. }
  14467. #endif /* MINIX2 */
  14468.