-
Notifications
You must be signed in to change notification settings - Fork 3
/
ckcfns.c
2015 lines (1764 loc) · 48.2 KB
/
ckcfns.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 *fnsv = " Library, 4G(108)";
#endif /* ifndef NOICP */
/* C K C F N S -- System-independent Kermit protocol support functions */
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* 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 "ckcdeb.h" /* Debug formats, typedefs, etc. */
#include "ckcker.h" /* Symbol definitions for Kermit */
#ifdef __linux__
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#endif /* ifdef __linux__ */
#ifndef NULL
#define NULL 0
#endif /* ifndef NULL */
extern int spsiz, spmax, rpsiz, timint;
extern int srvtim, rtimo, npad, ebq;
extern int ebqflg, rpt, rptq, rptflg;
extern int capas, keep, pktnum, prvpkt;
extern int sndtyp, bctr, bctu, fmask;
extern int size, maxsize, spktl, nfils;
extern int stdouf, warn, timef, spsizf;
extern int parity, speed, turn, turnch;
extern int delay, displa, pktlog, tralog;
extern int seslog, xflg, mypadn, tsecs;
extern int deblog, hcflg, binary, savmod;
extern int fncnv, local;
#ifndef NOSERVER
extern int server;
#endif /* ifndef NOSERVER */
extern int cxseen;
extern int czseen, nakstate, rq, rqf;
extern int sq, wslots, urpsiz, rln;
#ifndef NOATTR
extern int atcapr;
extern int atcapu;
#endif /* ifndef NOATTR */
extern int atcapb, lpcapr;
extern int lpcapb, lpcapu, swcapr, swcapb;
extern int swcapu, bsave, bsavef, numerrs;
extern long filcnt, ffc, fsize;
#ifndef NOSTATS
extern long flci, flco, tlci;
extern long tlco, tfc;
#endif /* ifndef NOSTATS */
extern CHAR padch, mypadc, eol, seol;
extern CHAR *srvptr, ctlq, myctlq, sstate;
extern CHAR filnam[], sndpkt[], recpkt[], data[];
extern CHAR srvcmd[], stchr, mystch;
extern char *cmarg, *cmarg2, *hlptxt;
extern char **cmlist, *rdatap;
void spar();
void sipkt();
long zchki();
char *strcpy();
char *rpar();
void errpkt(char *reason);
extern void errpkt();
extern CHAR zinbuffer[], zoutbuffer[];
extern CHAR *zinptr, *zoutptr;
extern int zincnt, zoutcnt;
/*
* Variables local
* to this module
*/
static char *memptr; /* Pointer for memory strings */
static char cmdstr[100]; /* UNIX system command string */
static int sndsrc; /* Flag for where to send from: */
/* -1: name in cmdata, */
/* 0: stdin, */
/* >0: list in cmlist. */
static int n_len; /* Packet encode-ahead length and flag */
/* if < 0, no pre-encoded data. */
static int memstr; /* Flag for input from memory string */
static int first; /* Flag for first char from input */
static CHAR t; /* Current character */
static CHAR next; /* Next character */
/* E N C S T R -- Encode a string from memory. */
/*
* Call this instead of getpkt() if source
* is a string, rather than a file.
*/
void
encstr(s)
char *s;
{
int m;
char *p;
m = memstr;
p = memptr; /* Save these. */
memptr = s; /* Point to the string. */
memstr = 1; /* Flag memory string as source. */
first = 1; /* Initialize character lookahead. */
getpkt(
spsiz - bctu - 3); /* Fill a packet from the string. */
memstr = m; /* Restore memory string flag */
memptr = p; /* and pointer */
first = 1; /* Put this back as we found it. */
}
/*
* Output functions
* passed to 'decode'
*/
int
putsrv(c)
register char c;
{ /* Put character in server command buffer */
*srvptr++ = c;
*srvptr = '\0'; /* Make sure buffer is null-terminated */
return ( 0 );
}
int
puttrm(c)
register char c;
{ /* Output character to console */
conoc(c);
return ( 0 );
}
int
putfil(c)
register char c;
{ /* Output char to file */
if (zchout(
ZOFILE, c & fmask) < 0)
{
czseen = 1; /* If write error... */
debug(F101,
"putfil zchout write error, setting czseen", "", 1);
return ( -1 );
}
return ( 0 );
}
/* D E C O D E -- Kermit packet decoding procedure */
/*
* Call with string to be decoded,
* and an output function. Returns
* 0 on success, -1 on failure
* (e.g. disk full)
*/
int
decode(buf, fn)
register CHAR *buf;
register int (*fn)();
{
register unsigned int a, a7, b8; /* Low order 7 bits, and the 8th bit */
int x = 0;
rpt = 0; /* Initialize repeat count. */
while (( a = *buf++ ) != '\0')
{
if (rptflg) /* Repeat processing? */
{
if (
(unsigned int)a == \
(unsigned int)rptq)
{ /* Yes, got a repeat prefix? */
rpt = xunchar(*buf++); /* Yes, get the repeat count, */
a = *buf++; /* and get the prefixed character. */
}
}
b8 = 0; /* Check high order "8th" bit */
if (ebqflg) /* 8th-bit prefixing? */
{
if (
(unsigned int)a == \
(unsigned int)ebq)
{ /* Yes, got an 8th-bit prefix? */
b8 = 0200; /* Yes, remember this, */
a = *buf++; /* and get the prefixed character. */
}
}
if (
(unsigned int)a == \
(unsigned int)ctlq)
{ /* If control prefix, */
a = *buf++; /* get its operand. */
a7 = a & 0177; /* Only look at low 7 bits. */
if (
( a7 >= 0100 && a7 <= 0137 ) \
|| a7 == '?') /* Uncontrollify, */
{
a = ctl(a); /* if in control range */
}
}
a |= b8; /* Or, in the 8th bit */
if (rpt == 0)
{
rpt = 1; /* If no repeats, then one */
}
#ifdef NLCHAR
if (!binary) /* If in text mode, */
{
if (a == CR)
{
continue; /* discard carriage returns, */
}
if (a == LF)
{
a = NLCHAR; /* convert LF to system's newline. */
}
}
#endif /* ifdef NLCHAR */
if (fn == putfil) /* Speedup via buffering and a macro */
{
for (; rpt > 0; rpt--) /* Output the char RPT times */
{
x = zmchout(a);
if (x < 0) /* zmchout is a macro */
{
debug(F101,
"decode zmchout", "", x);
return ( -1 );
}
ffc++; /* Count the character */
}
}
else /* Not to the file */
{
for (; rpt > 0; rpt--) /* Output the char RPT times */
{
if (( *fn )(a) < 0)
{
return ( -1 ); /* Send to output function. */
}
}
}
}
return ( 0 );
}
/* G E T P K T -- Fill a packet data field */
/*
* Gets characters from the current source -- file or memory string.
* Encodes the data into the packet, filling the packet optimally.
* Set first = 1 when calling for the first time on a given input stream
* (string or file).
*
* Uses global variables:
* t -- current character.
* first -- flag: 1 to start up, 0 for input in progress, -1 for EOF.
* next -- next character.
* data -- the packet data buffer.
* size -- number of characters in the data buffer.
*
* Returns the size as value of the function, and also sets global size,
* and fills (and NULL-terminates) the global data array.
*
* Returns 0 upon EOF.
*
* Rewritten by Paul W. Placeway (PWP) of Ohio State University, March '89.
* Incorporates old getchx() and encode() inline to eliminate function
* calls, uses buffered input for much-improved efficiency, and clears up
* some confusion with line termination (CR+LF vs. LF vs. CR).
*/
int
getpkt(bufmax)
int bufmax;
{ /* Fill one packet buffer */
register CHAR rt = t, rnext = next; /* register shadows of the globals */
register CHAR *dp, *odp, *p1, *p2; /* pointers... */
register int x; /* Loop index. */
register int a7; /* Low 7 bits of character */
static CHAR leftover[6] = {
'\0', '\0', '\0',
'\0', '\0', '\0'
};
if (first == 1) /* If first time thru... */
{
first = 0; /* remember, */
*leftover = '\0'; /* discard interrupted leftovers, */
/* get 1st character of file into t */
if (memstr) /* watching out for null file */
{
if (( rt = *memptr++ ) == '\0') /* end of string ==> EOF. */
{
first = -1;
size = 0;
debug(F100,
"getpkt: empty string", "", 0);
return ( 0 );
}
}
else
{
if (( x = zminchar()) == -1) /* End of file */
{
first = -1;
debug(F100,
"getpkt: empty file", "", 0);
size = 0;
return ( 0 );
}
ffc++; /* Count a file character */
rt = x;
}
rt &= fmask; /* Bytesize mask */
}
else if (( first == -1 ) && \
( *leftover == '\0' )) /* EOF from last time? */
{
return ( size = 0 );
}
dp = data;
for (
p1 = leftover;
( *dp = *p1 ) != '\0';
p1++, dp++) /* Copy leftovers */
{
;
}
*leftover = '\0';
if (first == -1)
{
return ( size = ( dp - data )); /* Handle final leftovers */
}
/*
* Now, fill up the
* rest of the packet.
*/
rpt = 0; /* Clear out old repeat count. */
while (first > -1) /* Until EOF... */
{
if (memstr) /* get next character */
{
if (( rnext = *memptr++ ) == '\0') /* end of string ==> EOF */
{
first = -1; /* Flag eof for next time. */
}
else
{
rnext &= fmask; /* Bytesize mask. */
}
}
else
{
if (( x = zminchar()) == -1) /* End of file */
{
first = -1; /* Flag eof for next time. */
}
else
{
rnext = x & fmask; /* Bytesize mask. */
ffc++; /* Count it */
}
}
odp = dp; /* Remember current position. */
if (rptflg) /* Repeat processing? */
{
if (
#ifdef NLCHAR
/*
* If the next char is really CRLF, then we cannot
* be doing a repeat (unless CR,CR,LF which becomes
* "~ <n-1> CR CR LF", which is OK, but not most
* efficient). The actual conversion from NL to CRLF
* is done after the rptflg if...
*/
( binary || \
( rnext != NLCHAR )) &&
#endif /* ifdef NLCHAR */
rt == rnext && \
( first == 0 )) /* Got a run... */
{
if (++rpt < 94) /* Below max, just count */
{
continue; /* go back and get another */
}
else if (rpt == 94) /* Reached max, must dump */
{
*dp++ = rptq;
*dp++ = tochar(rpt);
rpt = 0;
}
}
else if (rpt == 1) /* Run broken, only 2? */
{
/* ... */ /* XXX(jhj): Now empty */
}
else if (rpt > 1) /* More than two */
{
*dp++ = rptq; /* Insert the repeat prefix */
*dp++ = tochar(++rpt); /* and count. */
rpt = 0; /* Reset repeat counter. */
}
}
#ifdef NLCHAR
if (!binary && ( rt == NLCHAR ))
{
*dp++ = myctlq; /* Put in the encoding directly */
*dp++ = 'M'; /* == ctl(CR) */
if (( dp - data ) <= maxsize)
{
odp = dp; /* check packet bounds */
}
rt = LF;
}
#endif /* ifdef NLCHAR */
a7 = rt & 0177; /* Low 7 bits of character */
if (ebqflg && ( rt & 0200 )) /* Do 8th bit prefix if needed */
{
*dp++ = ebq;
rt = a7;
}
if (( a7 < SP ) || \
( a7 == DEL )) /* Do control prefix if needed */
{
*dp++ = myctlq;
rt = ctl(rt);
}
if (a7 == myctlq) /* Prefix the control prefix */
{
*dp++ = myctlq;
}
if (( rptflg ) && ( a7 == rptq )) /* If it's the repeat prefix, */
{
*dp++ = myctlq; /* Quote if doing repeat counts. */
}
if (( ebqflg ) && ( a7 == ebq )) /* Prefix the 8th bit prefix */
{
*dp++ = myctlq; /* if doing 8th-bit prefixes */
}
*dp++ = rt; /* Finally, insert the character */
if (rpt == 1) /* Exactly two copies? */
{
rpt = 0;
p2 = dp; /* Save current size temporarily */
for (p1 = odp; p1 < p2; p1++) /* copy the old chars over again */
{
*dp++ = *p1;
}
if (( p2 - data ) <= maxsize)
{
odp = p2; /* check packet bounds */
}
}
rt = rnext; /* Next is now current. */
if (( dp - data ) >= bufmax) /* If too big save some for next */
{
size = ( dp - data );
*dp = '\0'; /* mark (current) the end. */
if (( dp - data ) > bufmax) /* if packet is overfull */
{
for (p1 = leftover, p2 = odp;
( *p1 = *p2 ) != '\0';
p1++, p2++)
{
;
}
debug(F111,
"getpkt leftover", leftover, size);
debug(F101,
" osize", "", ( odp - data ));
size = ( odp - data ); /* Return truncated packet. */
*odp = '\0'; /* Mark real end */
}
else /* If the packet is exactly full */
{
debug(F101,
"getpkt exact fit", "", size);
}
t = rt;
next = rnext; /* save for next time */
return ( size );
}
} /* Otherwise, keep filling. */
size = ( dp - data );
*dp = '\0'; /* mark (current) the end. */
debug(F111,
"getpkt eof/eot", data, size); /* Fell thru before full, */
return ( size ); /* Return part filled last pkt */
}
/* C A N N E D -- Check if current file transfer cancelled */
int
canned(buf)
char *buf;
{
if (*buf == 'X')
{
cxseen = 1;
}
if (*buf == 'Z')
{
czseen = 1;
}
debug(F101, "canned: cxseen", "", cxseen);
debug(F101, " czseen", "", czseen);
return (( czseen || cxseen ) ? 1 : 0 );
}
/* R E S E T C -- Reset per-transaction character counters */
#ifndef NOSTATS
void
resetc()
{
flci = flco = 0;
tfc = tlci = tlco = 0; /* Total file chars, line chars in & out */
}
#endif /* ifndef NOSTATS */
/* T I N I T -- Initialize a transaction */
void
tinit()
{
xflg = 0; /* Reset x-packet flag */
rqf = -1; /* Reset 8th-bit-quote request flag */
memstr = 0; /* Reset memory-string flag */
memptr = NULL; /* and pointer */
n_len = -1; /* No encoded-ahead data */
bctu = 1; /* Reset block check type to 1 */
ebq = ebqflg = 0; /* Reset 8th-bit quoting stuff */
if (savmod) /* If global file mode was saved, */
{
binary = 1; /* restore it, */
savmod = 0; /* unsave it. */
}
prvpkt = -1; /* Reset packet number */
pktnum = 0;
numerrs = 0; /* Transmission error counter */
cxseen = czseen = 0; /* Reset interrupt flags */
*filnam = '\0'; /* Clear file name */
*sndpkt = '\0'; /* Clear retransmission buffer */
spktl = 0; /* And its length */
nakstate = 0; /* Say we're not in a NAK'ing state */
#ifndef NOSERVER
if (server) /* If acting as server, */
{
timint = srvtim; /* Use server timeout interval. */
}
#endif /* ifndef NOSERVER */
}
/* R I N I T -- Respond to S or I packet */
void
rinit(d)
char *d;
{
char *tp;
ztime(&tp);
tlog(F110,
"Transaction begins", tp, 0l); /* Make transaction log entry */
if (binary)
{
tlog(F100,
"Global file mode = binary", "", 0l);
}
else
{
tlog(F100,
"Global file mode = text", "", 0l);
}
filcnt = 0; /* Init file counter */
spar(d);
ack1(rpar());
}
/* S I N I T -- Make sure file exists, then send Send-Init packet */
int
sinit()
{
int x;
char *tp;
filcnt = 0;
sndsrc = nfils; /* Where to look for files to send */
ztime(&tp);
tlog(F110,
"Transaction begins", tp, 0l); /* Make transaction log entry */
debug(F101,
"sinit: sndsrc", "", sndsrc);
if (sndsrc < 0) /* Must expand from 'send' command */
{
#ifdef DTILDE
char *tnam, *tilde_expand(); /* May have to expand tildes */
tnam = tilde_expand(cmarg); /* Try to expand tilde. */
if (*tnam != '\0')
{
cmarg = tnam;
}
#endif /* ifdef DTILDE */
nfils = zxpand(cmarg); /* Look up literal name. */
if (nfils < 0)
{
#ifndef NOSERVER
if (server)
{
errpkt(
"Too many files");
}
else
{
#endif /* ifndef NOSERVER */
screen(SCR_EM, 0, 0l,
"Too many files");
#ifndef NOSERVER
}
#endif /* ifndef NOSERVER */
return ( 0 );
}
else if (nfils == 0) /* If none found, */
{
char xname[100]; /* convert the name. */
zrtol(cmarg, xname);
nfils = zxpand(xname); /* Look it up again. */
}
if (nfils < 1) /* If no match, report error. */
{
#ifndef NOSERVER
if (server)
{
errpkt("File not found");
}
else
{
#endif /* ifndef NOSERVER */
screen(SCR_EM, 0, 0l,
"File not found");
#ifndef NOSERVER
}
#endif /* ifndef NOSERVER */
return ( 0 );
}
x = gnfile(); /* Position to first file. */
if (x < 1)
{
#ifndef NOSERVER
if (!server)
{
#endif /* ifndef NOSERVER */
screen(SCR_EM, 0, 0l,
"No readable file to send");
#ifndef NOSERVER
}
else
{
errpkt(
"No readable file to send");
}
#endif /* ifndef NOSERVER */
return ( 0 );
}
}
else if (sndsrc > 0) /* Command line arglist -- */
{
x = gnfile(); /* Get the first file from it. */
if (x < 1)
{
return ( 0 ); /* (if any) */
}
}
else if (sndsrc == 0) /* stdin or memory always exist... */
{
if (( cmarg2 != NULL ) && \
( *cmarg2 ))
{
strcpy(filnam, cmarg2); /* If F packet, "as" name is used */
cmarg2 = ""; /* if provided, */
}
else /* otherwise */
{
strcpy(filnam, "stdin"); /* just use this. */
}
}
#ifdef DEBUG
debug(F101,
"sinit: nfils", "", nfils);
debug(F110,
" filnam", filnam, 0);
debug(F110,
" cmdstr", cmdstr, 0);
#endif /* ifdef DEBUG */
ttflui(); /* Flush input buffer. */
if (!local
#ifndef NOSERVER
&& !server
#endif /* ifndef NOSERVER */
)
{
sleep(delay);
}
sipkt('S'); /* Send the Send-Init packet. */
return ( 1 );
}
void
sipkt(c)
char c;
{ /* Send S or I packet. */
CHAR *rp;
ttflui(); /* Flush pending input. */
rp = rpar(); /* Get parameters. */
spack(c, pktnum, strlen(rp), rp);
}
/* R C V F I L -- Receive a file */
/*
* Incoming filename is in data field of F packet. This
* function decodes it into the srvcmd buffer, substituting
* an alternate "as-name", if one was given. Finally, it
* does any requested transformations (like converting to
* lowercase) then if a file of the same name already exists,
* makes a new unique name.
*/
int
rcvfil(n)
char *n;
{
char xname[100], *xp; /* Buffer for constructing name */
#ifdef DTILDE
char *dirp, *tilde_expand();
#endif /* ifdef DTILDE */
srvptr = srvcmd; /* Decode file name from packet. */
decode(rdatap, putsrv);
if (*srvcmd == '\0') /* Watch out for null F packet. */
{
strcpy(srvcmd, "NONAME");
}
#ifdef DTILDE
dirp = tilde_expand(srvcmd); /* Expand tilde, if any. */
if (*dirp != '\0')
{
strcpy(srvcmd, dirp);
}
#endif /* ifdef DTILDE */
screen(SCR_FN, 0, 0l, srvcmd); /* Put it on screen */
tlog(F110, "Receiving", srvcmd, 0l); /* Transaction log entry */
if (cmarg2 != NULL) /* Check for alternate name */
{
if (*cmarg2 != '\0')
{
strcpy(srvcmd, cmarg2); /* Got one, use it. */
*cmarg2 = '\0';
}
}
xp = xname; /* OK to proceed. */
if (fncnv) /* If desired, */
{
zrtol(srvcmd, xp); /* convert name to local form */
}
else /* otherwise, */
{
strcpy(xname, srvcmd); /* use it literally */
}
if (warn) /* File collision avoidance? */
{
if (zchki(xname) != -1) /* Yes, file exists? */
{
znewn(xname, &xp); /* Yes, make new name. */
strcpy(xname, xp);
debug(F110,
" exists, new name ",
xname, 0);
}
}
debug(F110,
"rcvfil: xname",
xname, 0);
strcpy(n, xname); /* Return pointer to actual name. */
ffc = 0; /* Init file character counter */
filcnt++;
return ( 1 ); /* Always succeeds */
}
/* O P E N A -- Open a file, with attributes */
/*
* This function tries to open a new file to
* put the arriving data in. The filename is
* the one in the srvcmd buffer. If warning
* is on and a file of that name already
* exists, then a unique name is chosen.
*/
int
opena(f, zz)
char *f;
struct zattr *zz;
{
int x;
#ifdef DEBUG
adebu(f, zz); /* Write attributes to debug log */
#endif /* ifdef DEBUG */
if (bsavef) /* If somehow file mode */
{
binary = bsave; /* was saved but not restored, */
bsavef = 0; /* restore it. */
debug(F101,
"opena restoring binary",
"", binary);
}
if (( x = openo(f))) /* Try to open the file. */
{
tlog(F110, " as", f, 0l); /* OK, open, record name. */
if (zz->type.val[0] == 'A') /* Check attributes */
{
bsave = binary; /* Save global file type */
bsavef = 1; /* ( restore it in clsof() ) */
binary = 0;
debug(F100,
"opena attribute A = text",
"", binary);
}
else if \
(zz->type.val[0] == 'B')
{
bsave = binary; /* Save global file type */
bsavef = 1;
binary = 1;
debug(F100,
"opena attribute B = binary",
"", binary);
}
if (binary) /* Log file mode in transaction log */
{
tlog(F100,
" mode: binary", "", 0l);
}
else
{
tlog(F100,
" mode: text", "", 0l);
}
screen(SCR_AN, 0, 0l, f);
#ifndef NOICP
#ifndef NOCONN
intmsg(filcnt);
#endif /* ifndef NOCONN */
#endif /* ifndef NOICP */
}
else /* Did not open file OK. */
{
tlog(F110,
"Failure to open", f, 0l);
screen(SCR_EM, 0, 0l,
"Can't open file");
}
ffc = 0; /* Init file character counter */
return ( x ); /* Pass on return code from openo */
}
/* R E O F -- Receive End Of File */
int
reof(yy)
struct zattr *yy;
{
int x;
#ifndef NODISP
char *p;
char c;
#endif /* ifndef NODISP */
if (cxseen == 0)
{