-
Notifications
You must be signed in to change notification settings - Fork 3
/
ckutio.c
3098 lines (2664 loc) · 68.1 KB
/
ckutio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef NOICP
char *ckxv = " TTY IO, 4G(096)";
#endif /* ifndef NOICP */
/* C K U T I O */
/* SPDX-License-Identifier: BSD-3-Clause */
/* uCKermit interrupt, terminal control & I/O functions for UNIX systems */
/*
* Copyright (c) 2021, 2022, 2023 Jeffrey H. Johnson <[email protected]>
*
* Copyright (c) 1981-2011,
* Trustees of Columbia University in the City of New York.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* - Neither the name of Columbia University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h> /* Types */
#include <ctype.h> /* Character types */
#include <sys/dir.h> /* Directory */
#ifdef NULL
#undef NULL
#endif /* ifdef NULL */
#include <signal.h> /* Interrupts */
#include <stdio.h> /* UNIX Standard i/o */
#include <setjmp.h> /* Longjumps */
#include "ckcdeb.h" /* Typedefs, formats for debug() */
#ifdef __linux__
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#endif /* ifdef __linux__ */
#ifndef DEVNAMLEN
#define DEVNAMLEN 25
#endif /* ifndef DEVNAMLEN */
#ifndef NOICP
#ifdef BSD4
#define ANYBSD
#ifdef MAXNAMLEN
#define BSD42
#ifdef SUNOS4
char *ckxsys = " SunOS 4.x";
#else /* ifdef SunOS4 */
#ifdef ultrix
char *ckxsys = " VAX/Ultrix";
#else /* ifdef ultrix */
#ifdef BSD43
char *ckxsys = " 4.3 BSD";
#else /* ifdef BSD43 */
char *ckxsys = " 4.2 BSD";
#endif /* bsd43 */
#endif /* ultrix */
#endif /* sunos4 */
#else /* ifdef MAXNAMLEN */
#define BSD41
char *ckxsys = " 4.1 BSD";
#endif /* maxnamlen */
#endif /* bsd4 */
#ifdef BSD29
#define ANYBSD
char *ckxsys = " 2.9 BSD";
#endif /* bsd29 */
#ifdef V7
char *ckxsys = " Version 7 UNIX";
#endif /* v7 */
#ifdef UXIII
#ifdef XENIX
#ifdef M_I386
char *ckxsys = " Xenix/386";
#else /* ifdef M_I386 */
#ifdef M_I286
char *ckxsys = " Xenix/286";
#else /* ifdef M_I286 */
char *ckxsys = " Xenix/86";
#endif /* ifdef M_I286 */
#endif /* ifdef M_I386 */
#else /* ifdef XENIX */
#ifdef ISIII
char *ckxsys = " ISC System III";
#else /* ifdef ISIII */
#ifdef hpux
char *ckxsys = " HP 9000 Series HP-UX";
#else /* ifdef hpux */
#ifdef __linux__
char *ckxsys = " GNU/Linux";
#else /* ifdef __linux__ */
char *ckxsys = " AT&T System III/System V";
#endif /* linux */
#endif /* hpux */
#endif /* isiii */
#endif /* xenix */
#endif /* uxiii */
#endif /* ifndef NOICP */
void ztime(char **s);
void rtimer();
long ttsspd(long speed);
char coninc(int timo);
#ifdef NEWUUCP
#define LCKDIR
#endif /* ifdef NEWUUCP */
#ifdef ATT3BX
#define HDBUUCP
#endif /* ifdef ATT3BX */
#ifndef LOCK_DIR
#ifdef ISIII
#define LOCK_DIR "/etc/locks";
#else /* ifdef ISIII */
#ifdef HDBUUCP
#define LOCK_DIR "/usr/spool/locks";
#else /* ifdef HDBUUCP */
#ifdef LCKDIR
#define LOCK_DIR "/usr/spool/uucp/LCK";
#else /* ifdef LCKDIR */
#define LOCK_DIR "/usr/spool/uucp";
#endif /* LCKDIR */
#endif /* HDBUUCP */
#endif /* ISIII */
#endif /* !LOCK_DIR (outside ifndef) */
#ifdef UXIII
#define MYREAD
#endif /* uxiii */
#ifdef BSD42
#undef MYREAD
#include <errno.h>
#endif /* bsd42 */
/*
* Variables available to outside world:
*
* dftty -- Pointer to default tty name string, like "/dev/tty".
* dfloc -- 0 if dftty is console, 1 if external line.
* dfprty -- Default parity
* dfflow -- Default flow control
* ckxech -- Flag for who echoes console typein:
* 1 - The program (system echo is turned off)
* 0 - The system (or front end, or terminal).
* functions that want to do their own echoing should check this flag
* before doing so.
*
* flfnam -- Name of lock file, including its path, e.g.,
* "/usr/spool/uucp/LCK..cul0" or "/etc/locks/tty77"
* hasLock -- Flag set if this kermit established a uucp lock.
* inbufc -- number of tty line rawmode unread characters
* (system III/V unixes)
* backgrd -- Flag indicating program executing in background ( & on
* end of shell command). Used to ignore INT and QUIT signals.
* rtu_bug -- Set by stptrap(). RTU treats ^Z as EOF.
* (but only when we handle SIGTSTP)
*
* Functions for assigned communication line
* (either external or console tty):
*
* sysinit() -- System dependent program initialization
* syscleanup() -- System dependent program shutdown
* ttopen(ttname,local,mdmtyp) -- Open the named tty for exclusive access.
* ttclos() -- Close & reset the tty,
* releasing any access lock.
* ttpkt(speed,flow) -- Put the tty in packet mode and set the speed.
* ttvt(speed,flow) -- Put the tty in virtual terminal mode.
* or in DIALING or CONNECTED modem
* control state.
* ttinl(dest,max,timo) -- Timed read line from the tty.
* ttinc(timo) -- Timed read character from tty.
* myread() -- System 3 raw mode bulk buffer read, gives
* subsequent chars one at a time and simulates
* FIONREAD!
* myunrd(c) -- Places c back in buffer to be read (one only)
* ttchk() -- See how many characters in tty input buffer.
* ttxin(n,buf) -- Read n characters from tty (untimed).
* ttol(string,length) -- Write a string to the tty.
* ttoc(c) -- Write a character to the tty.
* ttflui() -- Flush tty input buffer.
*
* ttlock(ttname) -- Lock against uucp collisions (Sys III)
* ttunlck() -- Unlock " " "
* look4lk(ttname) -- Check if a lock file exists
*
* For ATT7300/UNIX PC, Sys III, Sys V:
* attdial(ttname,speed,telnbr) -- dials ATT7300/UNIX PC internal modem
* atthang(ttname) -- Hangs up internal modem for ATT7300's
* offgetty(ttname) -- Turns off getty(1m) for comms line
* ongetty(ttname) -- Restores getty() to comms line
*/
/*
* Functions for console terminal:
*
* congm() - Get console terminal modes.
* concb(esc) - Put the console in single-character wakeup mode without echo
* conbin(esc) - Put the console in binary (raw) mode.
* conres() - Restore the console to mode obtained by congm().
* conoc(c) - Unbuffered output, one character to console.
* conol(s) - Unbuffered output, null-terminated string to the console.
* conola(s) - Unbuffered output, array of strings to the console.
* conxo(n,s) - Unbuffered output, n characters to the console.
* conchk() - Check if characters available at console (bsd 4.2).
* Check if escape char (^\) typed at console (System III/V).
* coninc(timo) - Timed get a character from the console.
* conint() - Enable terminal interrupts on the console if not background.
* connoi() - Disable terminal interrupts on the console if not background.
*
* Time functions
*
* msleep(m) -- Millisecond sleep
* ztime(&s) -- Return pointer to date/time string
* rtimer() -- Reset timer
* gtimer() -- Get elapsed time since last call to rtimer()
*/
#ifndef XENIX
#ifndef unos
#include <sys/file.h> /* File information */
#endif /* ifndef unos */
#endif /* ifndef XENIX */
#ifdef BSD4
#include <fcntl.h>
#include <sys/file.h>
#endif /* ifdef BSD4 */
#ifdef UXIII
#ifdef CK_TERMIOS
#include <termios.h>
#else
#include <termio.h>
#endif /* ifdef CK_TERMIOS */
#include <sys/ioctl.h>
#include <errno.h> /* error numbers for system returns */
#include <fcntl.h> /* directory reading for locking */
#endif /* ifdef UXIII */
#ifdef HPUX
#include <sys/modem.h>
#endif /* ifdef HPUX */
#ifndef UXIII
#include <sgtty.h> /* Set/Get tty modes */
#ifndef V7
#ifndef BSD41
#include <sys/time.h> /* Clock info (for break generation) */
#endif /* ifndef BSD41 */
#endif /* ifndef V7 */
#endif /* ifndef UXIII */
#ifdef BSD41
#include <sys/timeb.h> /* BSD 4.1 ... ceb */
#endif /* ifdef BSD41 */
#ifdef BSD29
#include <sys/timeb.h> /* BSD 2.9 (Vic Abell, Purdue) */
#endif /* ifdef BSD29 */
#ifdef ultrix
#include <sys/ioctl.h>
#endif /* ifdef ultrix */
/*
* The following two conditional #defines
* are catch-alls for those systems that
* didn't have or couldn't find <file.h>
*/
#ifndef FREAD
#define FREAD 0x01
#endif /* ifndef FREAD */
#ifndef FWRITE
#define FWRITE 0x10
#endif /* ifndef FWRITE */
long time(); /* All UNIXes should have this... */
extern int errno; /* System call error code. */
/*
* Special stuff for V7
* input buffer peeking
*/
#ifdef V7
int kmem[2] = {
-1, -1
};
char *initrawq(), *qaddr[2] =
{
0, 0
};
#define CON 0
#define TTY 1
#endif /* ifdef V7 */
/*
* dftty is the device name of the default
* device for file transfer. dfloc is 0 if
* dftty is the user's console terminal,
* and 1 if an external line
*/
char *dftty = CTTNAM; /* Remote by default, use normal */
int dfloc = 0; /* controlling terminal name. */
int dfprty = 0; /* Default parity (0 = none) */
int ttprty = 0; /* Parity in use. */
int ttmdm = 0; /* Modem in use. */
int dfflow = 1; /* Xon/Xoff flow control */
int backgrd = 0; /* Assume in foreground (no '&' ) */
#ifdef ultrix
int iniflags = 0; /* fcntl flags for ttyfd */
#endif /* ifdef ultrix */
int ckxech = 0; /* 0 if system normally echoes */
/* console characters, else 1 */
#ifndef NOSTATS
static long tcount; /* Elapsed time counter */
#endif /* ifndef NOSTATS */
static jmp_buf sjbuf, jjbuf; /* Longjump buffer */
static int lkf = 0, /* Line lock flag */
conif = 0, /* Console interrupts on/off flag */
cgmf = 0, /* Flag that console modes saved */
xlocal = 0, /* Flag for tty local or remote */
ttyfd = -1; /* TTY file descriptor */
static char escchr; /* Escape or attn character */
#ifdef BSD42
#include <time.h>
#include <sys/time.h>
static struct timeval tv; /* For getting time, from sys/time.h */
static struct timezone tz;
#endif /* ifdef BSD42 */
#ifdef BSD29
static long clock; /* For getting time from sys/time.h */
static struct timeb ftp; /* And from sys/timeb.h */
#endif /* ifdef BSD29 */
#ifdef BSD41
static long clock; /* For getting time from sys/time.h */
static struct timeb ftp; /* And from sys/timeb.h */
#endif /* ifdef BSD41 */
#ifdef V7
static long clock;
#endif /* ifdef V7 */
#ifdef UXIII
#ifdef CK_TERMIOS
#define LTERMIO termios
#else
#define LTERMIO termio
#endif /* ifdef CK_TERMIOS */
static struct LTERMIO ttold = { 0 }; /* Init'd for word alignment, */
static struct LTERMIO ttraw = { 0 }; /* which is important for some */
static struct LTERMIO tttvt = { 0 }; /* systems, like Zilog... */
static struct LTERMIO ccold = { 0 };
static struct LTERMIO ccraw = { 0 };
static struct LTERMIO cccbrk = { 0 };
#else /* ifdef UXIII */
static struct sgttyb /* sgtty info... */
ttold, ttraw, tttvt, ttbuf,
ccold, ccraw, cccbrk, vanilla;
#endif /* ifdef UXIII */
static char flfnam[80]; /* UUCP lock file path name */
static int hasLock = 0; /* =1 if this kermit locked uucp */
static int inbufc = 0; /* stuff for efficient SIII raw line */
static int ungotn = -1; /* pushback to unread character */
static int conesc = 0; /* set to 1 if esc char (^\) typed */
static int ttlock(); /* definition of ttlock subprocedure */
static int ttunlck(); /* and unlock subprocedure */
static char ttnmsv[DEVNAMLEN + 1]; /* copy of open path for tthang */
/* S Y S I N I T -- System-dependent program initialization */
int
sysinit()
{
#ifdef BSD42
(void)tv;
(void)tz;
#endif /* ifdef BSD42 */
#ifdef ultrix
gtty(0, &vanilla); /* Get sgtty info */
iniflags = fcntl(0, F_GETFL, 0); /* Get flags */
#else /* ifdef ultrix */
#ifdef AUX
set42sig(); /* Don't ask! ([email protected]) */
#endif /* aux */
#endif /* ifdef ultrix */
return ( 0 );
}
/* S Y S C L E A N U P -- System-dependent program cleanup */
int
syscleanup()
{
#ifdef ultrix
stty(0, &vanilla); /* Get sgtty info */
fcntl(0, F_SETFL, iniflags); /* Restore flags */
#endif /* ifdef ultrix */
return ( 0 );
}
/* T T O P E N -- Open a tty for exclusive access */
/*
* Returns 0 on success, -1 on failure
*
* If called with lcl < 0, sets value of lcl as follows:
* 0: the terminal named by ttname is the job's controlling terminal.
* 1: the terminal named by ttname is not the job's controlling terminal.
* But watch out: if a line is already open, or if requested line can't
* be opened, then lcl remains (and is returned as) -1.
*/
int
ttopen(ttname, lcl, modem)
char *ttname;
int *lcl, modem;
{
#ifdef UXIII
char *ctermid(); /* Wish they all had this! */
#endif /* ifdef UXIII */
char *x;
extern char *ttyname();
char cname[DEVNAMLEN + 4];
if (ttyfd > -1) /* if comms line already opened */
{
if (strncmp(
ttname, ttnmsv, DEVNAMLEN)) /* are new & old names equal? */
{
ttclos(); /* no, close old ttname, open new */
}
else /* else same, ignore this call */
{
return ( 0 );
}
}
ttmdm = modem; /* Make this available to other fns */
xlocal = *lcl; /* Make this available to other fns */
#ifdef NEWUUCP
acucntrl("disable", ttname); /* Open getty on line (4.3BSD) */
#endif /* ifdef NEWUUCP */
/*
* In the following section, we open the tty device for read/write.
* If a modem has been specified via "set modem" prior to "set line"
* then the O_NDELAY parameter is used in the open, provided this symbol
* is defined (e.g. in fcntl.h), so that the program does not hang waiting
* for carrier (which in most cases won't be present because a connection
* has not been dialed yet). It would make more sense to first determine
* if the line is local before doing this, but because ttyname() requires a
* file descriptor, we have to open first.
*/
#ifdef UXIII
ttyfd = open(ttname, O_RDWR | ( modem ? O_NDELAY : 0 ));
#else /* not uxiii */
#ifdef O_NDELAY
ttyfd = open(ttname, O_RDWR | ( modem ? O_NDELAY : 0 ));
#else /* O_NDELAY not defined */
ttyfd = open(ttname, 2);
#endif /* O_NDELAY */
#endif /* uxiii */
debug(F111, "ttopen", "modem", modem);
debug(F101, " ttyfd", "", ttyfd);
if (ttyfd < 0) /* If couldn't open, fail. */
{
perror(ttname);
return ( -1 );
}
strncpy(
ttnmsv, ttname, DEVNAMLEN); /* Open, keep copy of name locally. */
/*
* Caller wants us to figure out
* if line is controlling tty
*/
debug(F111, "ttopen ok", ttname, *lcl);
if (*lcl != 0)
{
if (strcmp(ttname, CTTNAM) == 0) /* /dev/tty is always remote */
{
xlocal = 0;
debug(F111, " ttname=CTTNAM", ttname, xlocal);
}
else if (isatty(0)) /* Else, if stdin not redirected */
{
x = ttyname(0); /* then compare its device name */
strncpy(cname, x, DEVNAMLEN); /* (copy from internal static buf) */
#ifdef __linux__
x = cname;
#else /* ifdef __linux__ */
x = ttyname(ttyfd); /* ...with real name of ttname. */
#endif /* ifdef __linux__ */
if (x == NULL)
{
return ( 0 );
}
#ifdef __linux__
xlocal = 1;
#else /* ifdef __linux__ */
xlocal = ( strncmp(x, cname, DEVNAMLEN) == 0 ) ? 0 : 1;
#endif /* ifdef __linux__ */
debug(F111, " ttyname", x, xlocal);
}
else /* Else, if stdin redirected... */
{
#ifdef UXIII
/*
* Sys III/V provides nice ctermid() function
* to get name of controlling tty
*/
ctermid(cname); /* Get name of controlling terminal */
debug(F110, " ctermid", cname, 0);
x = ttyname(ttyfd); /* Compare with name of comm line. */
xlocal = ( strncmp(x, cname, DEVNAMLEN) == 0 ) ? 0 : 1;
debug(F111, " ttyname", x, xlocal);
#else /* ifdef UXIII */
/*
* Just assume local, so "set speed"
* and similar commands will work
* If not really local, how could
* it work anyway?...
*/
xlocal = 1;
debug(F101, " redirected stdin", "", xlocal);
#endif /* uxiii */
}
}
/*
* Note, the following code was added so that UNIX "idle-line" snoopers
* would not think Kermit was idle when it was transferring files, and
* maybe log people out. But it had to be removed because it broke
* one of UNIX Kermit's very useful features, sending from standard input,
* as in "kermit -s - < file" or "command | kermit -s -". Too bad...
*
* If we're not local, close the ttyfd and just use 0
*/
/*
* if (xlocal == 0) {
* close(ttyfd);
* ttyfd = 0;
* }
*/
/*
* Now check if line is locked --
* if so fail, else lock for ourselves
*/
lkf = 0; /* Check lock */
if (xlocal > 0)
{
if (ttlock(ttname) < 0)
{
fprintf(stderr, "Exclusive access to %s denied\n", ttname);
close(ttyfd);
ttyfd = -1;
debug(F110, " Access denied by lock", ttname, 0);
return ( -1 ); /* Not if already locked */
}
else
{
lkf = 1;
}
}
/*
* Got the line, now set the
* desired value for local.
*/
if (*lcl != 0)
{
*lcl = xlocal;
}
/*
* Some "special"
* stuff for UNIX v7
*/
#ifdef V7
if (kmem[TTY] < 0) /* If open, then skip this. */
{
qaddr[TTY] = initrawq(ttyfd); /* Init the queue. */
if (( kmem[TTY] = \
open("/dev/kmem", 0)) < 0)
{
fprintf(
stderr,
"Can't read /dev/kmem in ttopen.\n");
perror("/dev/kmem");
exit(1);
}
}
#endif /* ifdef V7 */
/*
* no failure returns
* after this point
*/
#ifndef XENIX
#ifndef unos
#ifdef TIOCEXCL
if (xlocal)
{
if (ioctl(ttyfd, TIOCEXCL, NULL) < 0)
{
#ifndef NODOHLP
fprintf(
stderr,
"Warning, problem getting exclusive access\n");
#else /* NODOHLP */
fprintf(
stderr,
"TIOCEXCL failed\n");
#endif /* NODOHLP */
}
}
#endif /* ifdef TIOCEXCL */
#endif /* ifndef unos */
#endif /* ifndef XENIX */
#ifdef ultrix
if (xlocal)
{
int temp = 0;
#ifdef TIOCSINUSE
if (ioctl(ttyfd, TIOCSINUSE, NULL) < 0)
{
fprintf(stderr, "Can't set in-use flag on modem.\n");
perror("TIOCSINUSE");
}
#endif /* ifdef TIOCSINUSE */
if (modem)
{
ioctl(ttyfd, TIOCMODEM, &temp);
ioctl(ttyfd, TIOCHPCL, 0);
}
else
{
ioctl(ttyfd, TIOCNMODEM, &temp);
}
}
#endif /* ifdef ultrix */
/*
* Get tty
* device settings
*/
#ifndef UXIII
gtty(ttyfd, &ttold); /* Get sgtty info */
if (xlocal)
{
ttold.sg_flags &= ~ECHO; /* Turn off echo on local line */
}
gtty(ttyfd, &ttraw); /* And a copy of it for packets */
gtty(ttyfd, &tttvt); /* And one for virtual tty service */
#else /* ifndef UXIII */
ioctl(ttyfd, TCGETA, &ttold); /* Same deal for Sys III, Sys V */
if (xlocal)
{
ttold.c_lflag &= ~ECHO; /* Turn off echo on local line. */
}
ioctl(ttyfd, TCGETA, &ttraw);
ioctl(ttyfd, TCGETA, &tttvt);
#endif /* ifndef UXIII */
#ifdef DEBUG
debug(F101, "ttopen, ttyfd", "", ttyfd);
debug(F101, " lcl", "", *lcl);
debug(F111, " lock file", flfnam, lkf);
#endif /* ifdef DEBUG */
return ( 0 );
}
/* T T C L O S -- Close the TTY, releasing any lock */
#ifdef COMMENT
SIGTYP
ttclosx() /* To avoid pointer type mismatches */
{
ttclos();
}
#endif /* ifdef COMMENT */
int
ttclos()
{
debug(F101, "ttclos", "", ttyfd);
if (ttyfd < 0)
{
return ( 0 ); /* Wasn't open. */
}
#ifdef ultrix
if (xlocal)
{
ioctl(ttyfd, TIOCNCAR, NULL);
}
#endif /* ifdef ultrix */
if (xlocal)
{
if (ttunlck()) /* Release UUCP-style lock */
{
#ifndef NODOHLP
fprintf(stderr, "Warning, problem releasing lock\r\n");
#else /* ifndef NODOHLP */
fprintf(stderr, "ttunlck failed\r\n");
#endif /* ifndef NODOHLP */
}
if (tthang()) /* Hang up phone line */
{
#ifndef NODOHLP
fprintf(stderr, "Warning, problem hanging up the phone\r\n");
#else /* ifndef NODOHLP */
fprintf(stderr, "tthang failed\r\n");
#endif /* ifndef NODOHLP */
}
}
ttres(); /* Reset modes. */
/*
* Relinquish exclusive access
* if we might have had it...
*/
#ifndef XENIX
#ifndef unos
#ifdef TIOCEXCL
#ifdef TIOCNXCL
if (xlocal)
{
if (ioctl(ttyfd, TIOCNXCL, NULL) < 0)
{
#ifndef NODOHLP
fprintf(
stderr,
"Warning, problem relinquishing exclusive access\r\n");
#else /* ifndef NODOHLP */
fprintf(
stderr,
"TIOCNXCL failed\r\n");
#endif /* ifndef NODOHLP */
}
}
#endif /* ifdef TIOCNXCL */
#endif /* ifdef TIOCEXCL */
#endif /* ifndef unos */
#endif /* ifndef XENIX */
if (ttyfd > 0)
{
close(ttyfd); /* Close it if tthang didn't */
}
ttyfd = -1; /* Mark it as closed. */
#ifdef NEWUUCP
acucntrl("enable", flfnam); /* Close getty on line. */
#endif /* ifdef NEWUUCP */
debug(F101, "ttclose exit", "", ttyfd);
return ( 0 );
}
/* T T H A N G -- Hangup phone line */
int
tthang()
{
#ifdef UXIII
#ifdef HPUX
unsigned long dtr_down = 00000000000, modem_rtn;
#else /* ifdef HPUX */
unsigned short ttc_save;
#endif /* ifdef HPUX */
#endif /* ifdef UXIII */
#ifdef ANYBSD
int ttc_save;
#endif /* ifdef ANYBSD */
if (ttyfd < 0)
{
return ( 0 ); /* Not open. */
}
if (xlocal < 1)
{
return ( 0 ); /* Don't do if not local */
}
#ifdef ANYBSD
#ifdef BSD42
ttc_save = fcntl(ttyfd, F_GETFL, 0); /* Get flags */
#endif /* ifdef BSD42 */
ioctl(ttyfd, TIOCCDTR, 0); /* Clear DTR */
msleep(550); /* For about 1/2 sec */
ioctl(ttyfd, TIOCSDTR, 0); /* Restore DTR */
#ifdef COMMENT
close(ttyfd); /* Close/reopen fd */
if (( ttyfd = open(ttnmsv, ttc_save)) < 0)
{
perror(ttnmsv); /* If can't, give message */
ttunlck(); /* and unlock the file */
return ( -1 );
}
#endif /* ifdef COMMENT */
#endif /* ifdef ANYBSD */
#ifdef UXIII
#ifdef HPUX /* HP way of modem control */
if (ioctl(ttyfd, MCSETAF, &dtr_down) < 0)
{
return ( -1 ); /* lower DTR */
}
msleep(550);
if (ioctl(ttyfd, MCGETA, &modem_rtn) < 0)
{
return ( -1 ); /* get line status */
}
if (( modem_rtn & MDCD ) != 0)
{
return ( -1 ); /* check if DCD is low */
}
modem_rtn = MRTS | MDTR; /* bits for RTS & DTR */
if (ioctl(ttyfd, MCSETAF, &modem_rtn) < 0)
{
return ( -1 ); /* set lines */
}
#else /* ifdef HPUX */
ttc_save = ttraw.c_cflag;
ttraw.c_cflag &= ~CBAUD; /* baud rate to 0 to hangup */
if (ioctl(ttyfd, TCSETAF, &ttraw) < 0)
{
return ( -1 ); /* do it */
}
msleep(125); /* let things settle */
ttraw.c_cflag = ttc_save;
/*
* NOTE - The following #ifndef...#endif
* can be removed for SCO Xenix 2.1.3
* or later, but must keep for earlier
* versions, which can't do close/open.
*/
#ifndef XENIX
ttc_save = fcntl(ttyfd, F_GETFL, 0);
(void)ttc_save;
close(ttyfd); /* close/reopen file descriptor */
ttyfd = -1; /* in case reopen fails */
#ifndef UXIII
if (( ttyfd = open(ttnmsv, ttc_save)) < 0)
{
return ( -1 );
}
#else /* ifndef UXIII */
if (( ttyfd = open(ttnmsv, O_RDWR | O_NDELAY)) < 0)
{
return ( -1 );
}
/*
* So Kermit hangup command works
* even if there is no carrier
*/
#endif /* ifndef UXIII */
#endif /* ifndef XENIX */
if (ioctl(ttyfd, TCSETAF, &ttraw) < 0)
{
return ( -1 ); /* un-do it */
}
#endif /* ifdef HPUX */
#endif /* ifdef UXIII */
return ( 0 );
}
/* T T R E S -- Restore terminal to "normal" mode */
int
ttres()
{ /* Restore the tty to normal. */
int x;
(void)x;
if (ttyfd < 0)
{
return ( -1 ); /* Not open. */
}
#ifndef UXIII /* except for sIII, */
sleep(1); /* Wait for i/o to finish. */
#endif /* (sIII does wait in ioctls) */
#ifdef UXIII
if (ioctl(ttyfd, TCSETAW, &ttold) < 0)
{
return ( -1 ); /* restore termio stuff */
}
if (ttmdm)
{
if (fcntl(ttyfd, F_SETFL, fcntl(ttyfd, F_GETFL, 0) & \
~O_NDELAY) < 0)
{
return ( -1 );
}
}
return ( 0 );
#else /* ifdef UXIII */
#ifdef MYREAD
#ifdef FIONBIO
x = 0;
x = ioctl(ttyfd, FIONBIO, &x);
if (x < 0)
{
perror("ttres ioctl");
debug(F101, "ttres ioctl", "", x);
}
#else /* ifdef FIONBIO */
#ifdef FNDELAY
x =
( fcntl(
ttyfd,
F_SETFL,