-
Notifications
You must be signed in to change notification settings - Fork 11
/
lsz.c
1920 lines (1784 loc) · 46.5 KB
/
lsz.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
/*
lsz - send files with x/y/zmodem
Copyright (C) until 1988 Chuck Forsberg (Omen Technology INC)
Copyright (C) 1994 Matt Porter, Michael D. Black
Copyright (C) 1996, 1997 Uwe Ohse
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
originally written by Chuck Forsberg
*/
#include "zglobal.h"
/* char *getenv(); */
#define SS_NORMAL 0
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
struct lrzsz_config config;
#include "timing.h"
#include "long-options.h"
#include "xstrtoul.h"
#include "error.h"
unsigned Txwindow; /* Control the size of the transmitted window */
unsigned Txwspac; /* Spacing between zcrcq requests */
unsigned Txwcnt; /* Counter used to space ack requests */
size_t Lrxpos; /* Receiver's last reported offset */
int errors;
enum zm_type_enum protocol;
int under_rsh=FALSE;
extern int turbo_escape;
static int no_unixmode;
int Canseek=1; /* 1: can; 0: only rewind, -1: neither */
static int zsendfile (struct zm_fileinfo *zi, const char *buf, size_t blen);
static int getnak (void);
static int wctxpn (struct zm_fileinfo *);
static int wcs (const char *oname, const char *remotename);
static size_t zfilbuf (struct zm_fileinfo *zi);
static size_t filbuf (char *buf, size_t count);
static int getzrxinit (void);
static int calc_blklen (long total_sent);
static int sendzsinit (void);
static int wctx (struct zm_fileinfo *);
static int zsendfdata (struct zm_fileinfo *);
static int getinsync (struct zm_fileinfo *, int flag);
static void countem (int argc, char **argv);
static void chkinvok (const char *s);
static void usage (int exitcode, const char *what);
static void saybibi (void);
static int wcsend (int argc, char *argp[]);
static int wcputsec (char *buf, int sectnum, size_t cseclen);
static void usage1 (int exitcode);
#define ZSDATA(x,y,z) \
do { if (Crc32t) {zsda32(x,y,z); } else {zsdata(x,y,z);}} while(0)
#define DATAADR (txbuf)
int Filesleft;
long Totalleft;
size_t buffersize=16384;
/*
* Attention string to be executed by receiver to interrupt streaming data
* when an error is detected. A pause (0336) may be needed before the
* ^C (03) or after it.
*/
#ifdef READCHECK
char Myattn[] = { 0 };
#else
char Myattn[] = { 03, 0336, 0 };
#endif
FILE *input_f;
#define MAX_BLOCK 8192
char *txbuf;
long vpos = 0; /* Number of bytes read from file */
char Lastrx;
char Crcflg;
int Verbose=0;
int Restricted=0; /* restricted; no /.. or ../ in filenames */
int Quiet=0; /* overrides logic that would otherwise set verbose */
int Ascii=0; /* Add CR's for brain damaged programs */
int Fullname=0; /* transmit full pathname */
int Unlinkafter=0; /* Unlink file after it is sent */
int firstsec;
int errcnt=0; /* number of files unreadable */
size_t blklen=128; /* length of transmitted records */
int Optiong; /* Let it rip no wait for sector ACK's */
int Totsecs; /* total number of sectors this file */
int Filcnt=0; /* count of number of files opened */
int Lfseen=0;
unsigned Rxbuflen = 16384; /* Receiver's max buffer length */
unsigned Tframlen = 0; /* Override for tx frame length */
unsigned blkopt=0; /* Override value for zmodem blklen */
int Rxflags = 0;
int Rxflags2 = 0;
size_t bytcnt;
int Wantfcs32 = TRUE; /* want to send 32 bit FCS */
char Lzconv; /* Local ZMODEM file conversion request */
char Lzmanag; /* Local ZMODEM file management request */
int Lskipnocor;
char Lztrans;
char zconv; /* ZMODEM file conversion request */
char zmanag; /* ZMODEM file management request */
char ztrans; /* ZMODEM file transport request */
int Exitcode;
int enable_timesync=0;
size_t Lastsync; /* Last offset to which we got a ZRPOS */
int Beenhereb4; /* How many times we've been ZRPOS'd same place */
int no_timeout=FALSE;
size_t max_blklen=1024;
size_t start_blklen=0;
int zmodem_requested;
time_t stop_time=0;
int error_count;
#define OVERHEAD 18
#define OVER_ERR 20
#define MK_STRING(x) #x
jmp_buf intrjmp; /* For the interrupt on RX CAN */
static long min_bps;
static long min_bps_time;
static int io_mode_fd=0;
static int zrqinits_sent=0;
static int play_with_sigint=0;
/* called by signal interrupt or terminate to clean things up */
void
bibi (int n)
{
canit(STDOUT_FILENO);
fflush (stdout);
lrzsz_iomode(io_mode_fd, LRZSZ_IOMODE_RESET, &config);
if (n == 99)
error (0, 0, _ ("io_mode(,2) in rbsb.c not implemented\n"));
else
error (0, 0, _ ("caught signal %d; exiting"), n);
if (n == SIGQUIT)
abort ();
exit (128 + n);
}
/* Called when ZMODEM gets an interrupt (^C) */
static void
onintr(int n)
{
signal(SIGINT, SIG_IGN);
n++; /* use it */
longjmp(intrjmp, -1);
}
int Zctlesc; /* Encode control characters */
const char *program_name = "sz";
int Zrwindow = 1400; /* RX window size (controls garbage count) */
static struct option const long_options[] =
{
{"append", no_argument, NULL, '+'},
{"twostop", no_argument, NULL, '2'},
{"try-8k", no_argument, NULL, '8'},
{"start-8k", no_argument, NULL, '9'},
{"try-4k", no_argument, NULL, '4'},
{"start-4k", no_argument, NULL, '5'},
{"ascii", no_argument, NULL, 'a'},
{"binary", no_argument, NULL, 'b'},
{"bufsize", required_argument, NULL, 'B'},
{"full-path", no_argument, NULL, 'f'},
{"escape", no_argument, NULL, 'e'},
{"rename", no_argument, NULL, 'E'},
{"help", no_argument, NULL, 'h'},
{"crc-check", no_argument, NULL, 'H'},
{"1024", no_argument, NULL, 'k'},
{"1k", no_argument, NULL, 'k'},
{"packetlen", required_argument, NULL, 'L'},
{"framelen", required_argument, NULL, 'l'},
{"min-bps", required_argument, NULL, 'm'},
{"min-bps-time", required_argument, NULL, 'M'},
{"newer", no_argument, NULL, 'n'},
{"newer-or-longer", no_argument, NULL, 'N'},
{"16-bit-crc", no_argument, NULL, 'o'},
{"disable-timeouts", no_argument, NULL, 'O'},
{"disable-timeout", no_argument, NULL, 'O'}, /* i can't get it right */
{"protect", no_argument, NULL, 'p'},
{"resume", no_argument, NULL, 'r'},
{"restricted", no_argument, NULL, 'R'},
{"quiet", no_argument, NULL, 'q'},
{"stop-at", required_argument, NULL, 's'},
{"timesync", no_argument, NULL, 'S'},
{"timeout", required_argument, NULL, 't'},
{"turbo", no_argument, NULL, 'T'},
{"unlink", no_argument, NULL, 'u'},
{"unrestrict", no_argument, NULL, 'U'},
{"verbose", no_argument, NULL, 'v'},
{"windowsize", required_argument, NULL, 'w'},
{"xmodem", no_argument, NULL, 'X'},
{"ymodem", no_argument, NULL, 1},
{"zmodem", no_argument, NULL, 'Z'},
{"overwrite", no_argument, NULL, 'y'},
{"overwrite-or-skip", no_argument, NULL, 'Y'},
{"delay-startup", required_argument, NULL, 4},
{"no-unixmode", no_argument, NULL, 8},
{"syslog-facility", required_argument, NULL , 10},
{"syslog-severity", required_argument, NULL , 11},
{"log-level", required_argument, NULL , 12},
{NULL, 0, NULL, 0}
};
static void
show_version(void)
{
printf ("%s (%s) %s\n", program_name, PACKAGE, VERSION);
}
int
main(int argc, char **argv)
{
char *cp;
int npats;
int dm;
int i;
int stdin_files;
char **patts;
int c;
unsigned int startup_delay=0;
if (((cp = getenv("ZNULLS")) != NULL) && *cp)
Znulls = atoi(cp);
if (((cp=getenv("SHELL"))!=NULL) && (strstr(cp, "rsh") || strstr(cp, "rksh")
|| strstr(cp, "rbash") || strstr(cp,"rshell")))
{
under_rsh=TRUE;
Restricted=1;
}
if ((cp=getenv("ZMODEM_RESTRICTED"))!=NULL)
Restricted=1;
lrzsz_check_stderr(&config);
chkinvok(argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
parse_long_options (argc, argv, show_version, usage1);
Rxtimeout = 600;
while ((c = getopt_long (argc, argv,
"2+48abB:C:feEghHkL:l:m:M:NnOopRrqsSt:TUuvw:XYy",
long_options, (int *) 0))!=EOF)
{
unsigned long int tmp;
char *tmpptr;
enum strtol_error s_err;
switch (c)
{
case 0:
break;
case '+': Lzmanag = ZF1_ZMAPND; break;
case '2': config.io.two_stopbits = TRUE; break;
case '8':
if (max_blklen==8192)
start_blklen=8192;
else
max_blklen=8192;
break;
case '9': /* this is a longopt .. */
start_blklen=8192;
max_blklen=8192;
break;
case '4':
if (max_blklen==4096)
start_blklen=4096;
else
max_blklen=4096;
break;
case '5': /* this is a longopt .. */
start_blklen=4096;
max_blklen=4096;
break;
case 'a': Lzconv = ZCNL; Ascii = TRUE; break;
case 'b': Lzconv = ZCBIN; break;
case 'B':
if (0==strcmp(optarg,"auto"))
buffersize= (size_t) -1;
else
buffersize=strtol(optarg,NULL,10);
break;
case 'f': Fullname=TRUE; break;
case 'e': Zctlesc = 1; break;
case 'E': Lzmanag = ZF1_ZMCHNG; break;
case 'h': usage(0,NULL); break;
case 'H': Lzmanag = ZF1_ZMCRC; break;
case 'k': start_blklen=1024; break;
case 'L':
s_err = xstrtoul (optarg, NULL, 0, &tmp, "ck");
blkopt = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("packetlength"), s_err);
if (blkopt<24 || blkopt>MAX_BLOCK)
{
char meld[256];
sprintf(meld,
_("packetlength out of range 24..%ld"),
(long) MAX_BLOCK);
usage(2,meld);
}
break;
case 'l':
s_err = xstrtoul (optarg, NULL, 0, &tmp, "ck");
Tframlen = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("framelength"), s_err);
if (Tframlen<32 || Tframlen>MAX_BLOCK)
{
char meld[256];
sprintf(meld,
_("framelength out of range 32..%ld"),
(long) MAX_BLOCK);
usage(2,meld);
}
break;
case 'm':
s_err = xstrtoul (optarg, &tmpptr, 0, &tmp, "km");
min_bps = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("min_bps"), s_err);
if (min_bps<0)
usage(2,_("min_bps must be >= 0"));
break;
case 'M':
s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
min_bps_time = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("min_bps_time"), s_err);
if (min_bps_time<=1)
usage(2,_("min_bps_time must be > 1"));
break;
case 'N': Lzmanag = ZF1_ZMNEWL; break;
case 'n': Lzmanag = ZF1_ZMNEW; break;
case 'o': Wantfcs32 = FALSE; break;
case 'O': no_timeout = TRUE; break;
case 'p': Lzmanag = ZF1_ZMPROT; break;
case 'r':
if (Lzconv == ZCRESUM)
Lzmanag = ZF1_ZMCRC;
else
Lzconv = ZCRESUM;
break;
case 'R': Restricted = TRUE; break;
case 'q': Quiet=TRUE; Verbose=0; break;
case 's':
if (isdigit((unsigned char) (*optarg))) {
struct tm *tm;
time_t t;
int hh,mm;
char *nex;
hh = strtoul (optarg, &nex, 10);
if (hh>23)
usage(2,_("hour to large (0..23)"));
if (*nex!=':')
usage(2, _("unparsable stop time\n"));
nex++;
mm = strtoul (optarg, &nex, 10);
if (mm>59)
usage(2,_("minute to large (0..59)"));
t=time(NULL);
tm=localtime(&t);
tm->tm_hour=hh;
tm->tm_min=hh;
stop_time=mktime(tm);
if (stop_time<t)
stop_time+=86400L; /* one day more */
if (stop_time - t <10)
usage(2,_("stop time to small"));
} else {
s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
stop_time = tmp + time(0);
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("stop-at"), s_err);
if (tmp<10)
usage(2,_("stop time to small"));
}
break;
case 'S': enable_timesync=1; break;
case 'T': turbo_escape=1; break;
case 't':
s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
Rxtimeout = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("timeout"), s_err);
if (Rxtimeout<10 || Rxtimeout>1000)
usage(2,_("timeout out of range 10..1000"));
break;
case 'u': ++Unlinkafter; break;
case 'U':
if (!under_rsh)
Restricted=0;
else
error(1,0,
_("security violation: can't do that under restricted shell\n"));
break;
case 'v': ++Verbose; break;
case 'w':
s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
Txwindow = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("window size"), s_err);
if (Txwindow < 256)
Txwindow = 256;
Txwindow = (Txwindow/64) * 64;
Txwspac = Txwindow/4;
if (blkopt > Txwspac
|| (!blkopt && Txwspac < MAX_BLOCK))
blkopt = Txwspac;
break;
case 'X': protocol=ZM_XMODEM; break;
case 1: protocol=ZM_YMODEM; break;
case 'Z': protocol=ZM_ZMODEM; break;
case 'Y':
Lskipnocor = TRUE;
/* **** FALLL THROUGH TO **** */
case 'y':
Lzmanag = ZF1_ZMCLOB; break;
case 10:
lrzsz_set_syslog_facility(optarg);
break;
case 11:
lrzsz_set_syslog_severity(optarg);
break;
case 12:
lrzsz_set_locallog_severity(optarg);
break;
case 4:
s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
startup_delay = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("startup delay"), s_err);
break;
case 8: no_unixmode=1; break;
default:
usage (2,NULL);
break;
}
}
if (getuid()!=geteuid()) {
error(1,0,
_("this program was never intended to be used setuid\n"));
}
txbuf=malloc(MAX_BLOCK);
if (!txbuf) error(1,0,_("out of memory"));
zsendline_init();
if (start_blklen==0) {
if (protocol == ZM_ZMODEM) {
start_blklen=1024;
if (Tframlen) {
start_blklen=max_blklen=Tframlen;
}
}
else
start_blklen=128;
}
if (argc<2)
usage(2,_("need at least one file to send"));
if (startup_delay)
sleep(startup_delay);
#ifdef HAVE_SIGINTERRUPT
/* we want interrupted system calls to fail and not to be restarted. */
siginterrupt(SIGALRM,1);
#endif
npats = argc - optind;
patts=&argv[optind];
if (npats < 1)
usage(2,_("need at least one file to send"));
if (config.io.may_use_stderr && !Quiet) {
if (Verbose == 0)
Verbose = 2;
}
lrzsz_log(LOG_DEBUG, NULL, "%s %s\n", program_name, VERSION);
{
/* we write max_blocklen (data) + 18 (ZModem protocol overhead)
* + escape overhead (about 4 %), so buffer has to be
* somewhat larger than max_blklen
*/
char *s=xmalloc(max_blklen+1024);
setvbuf(stdout,s,_IOFBF,max_blklen+1024);
}
blklen=start_blklen;
for (i=optind,stdin_files=0;i<argc;i++) {
if (0==strcmp(argv[i],"-"))
stdin_files++;
}
if (stdin_files>1) {
usage(1,_("can read only one file from stdin"));
} else if (stdin_files==1) {
io_mode_fd=1;
}
lrzsz_iomode(io_mode_fd, LRZSZ_IOMODE_RAW, &config);
readline_setup(io_mode_fd, 128, 256);
if (signal(SIGINT, bibi) == SIG_IGN)
signal(SIGINT, SIG_IGN);
else {
signal(SIGINT, bibi);
play_with_sigint=1;
}
signal(SIGTERM, bibi);
signal(SIGPIPE, bibi);
signal(SIGHUP, bibi);
if ( protocol!=ZM_XMODEM) {
if (protocol==ZM_ZMODEM) {
printf("rz\r");
fflush(stdout);
}
countem(npats, patts);
if (protocol == ZM_ZMODEM) {
/* throw away any input already received. This doesn't harm
* as we invite the receiver to send it's data again, and
* might be useful if the receiver has already died or
* if there is dirt left if the line
*/
#ifdef HAVE_SELECT
struct timeval t;
unsigned char throwaway;
fd_set f;
#endif
purgeline(io_mode_fd);
#ifdef HAVE_SELECT
t.tv_sec = 0;
t.tv_usec = 0;
FD_ZERO(&f);
FD_SET(io_mode_fd,&f);
while (select(1,&f,NULL,NULL,&t)) {
if (0==read(io_mode_fd,&throwaway,1)) /* EOF ... */
break;
}
#endif
purgeline(io_mode_fd);
stohdr(0L);
zshhdr(ZRQINIT, Txhdr);
zrqinits_sent++;
if (Rxflags2 != ZF1_TIMESYNC)
/* disable timesync if there are any flags we don't know.
* dsz/gsz seems to use some other flags! */
enable_timesync=FALSE;
if (Rxflags2 & ZF1_TIMESYNC && enable_timesync) {
Totalleft+=6; /* TIMESYNC never needs more */
Filesleft++;
}
}
}
fflush(stdout);
if (wcsend(npats, patts)==ERROR) {
Exitcode=0200;
canit(STDOUT_FILENO);
}
fflush(stdout);
lrzsz_iomode(io_mode_fd, LRZSZ_IOMODE_RESET, &config);
if (Exitcode)
dm=Exitcode;
else if (errcnt)
dm=1;
else
dm=0;
if (Verbose)
{
fputs("\r\n",stderr);
if (dm)
fputs(_("Transfer incomplete\n"),stderr);
else
fputs(_("Transfer complete\n"),stderr);
}
exit(dm);
/*NOTREACHED*/
}
static int
wcsend (int argc, char *argp[])
{
int n;
Crcflg = FALSE;
firstsec = TRUE;
bytcnt = (size_t) -1;
for (n = 0; n < argc; ++n) {
Totsecs = 0;
if (wcs (argp[n],NULL) == ERROR)
return ERROR;
}
Totsecs = 0;
if (Filcnt == 0) { /* bitch if we couldn't open ANY files */
canit(STDOUT_FILENO);
lrzsz_log(LOG_ERR,NULL, "Can't open any requested files.");
return ERROR;
}
if (zmodem_requested)
saybibi ();
else if (protocol != ZM_XMODEM) {
struct zm_fileinfo zi;
char *pa;
pa=xmalloc(PATH_MAX+1);
*pa='\0';
zi.fname = pa;
zi.modtime = 0;
zi.mode = 0;
zi.bytes_total = 0;
zi.bytes_sent = 0;
zi.bytes_received = 0;
zi.bytes_skipped = 0;
wctxpn (&zi);
free(pa);
}
return OK;
}
static int
wcs(const char *oname, const char *remotename)
{
struct stat f;
char *name;
struct zm_fileinfo zi;
if (Restricted) {
/* restrict pathnames to current dir */
if ( strstr(oname, "../")) {
canit(STDOUT_FILENO);
vchar('\r');
error(1,0,
_("security violation: not allowed to upload from %s"),oname);
}
}
if (0==strcmp(oname,"-")) {
char *p=getenv("ONAME");
if (p) {
name=xstrdup(p);
} else {
name=xmalloc(PATH_MAX+1);
sprintf(name, "s%lu.lsz", (unsigned long) getpid());
}
input_f=stdin;
} else if ((input_f=fopen(oname, "r"))==NULL) {
int e=errno;
error(0,e, _("cannot open %s"),oname);
++errcnt;
return OK; /* pass over it, there may be others */
} else {
name=xstrdup(oname);
}
if (1) {
static char *s=NULL;
static size_t last_length=0;
struct stat st;
if (fstat(fileno(input_f),&st)==-1)
st.st_size=1024*1024;
if (buffersize==(size_t) -1 && s) {
if ((size_t) st.st_size > last_length) {
free(s);
s=NULL;
last_length=0;
}
}
if (!s && buffersize) {
last_length=16384;
if (buffersize==(size_t) -1) {
if (st.st_size>0)
last_length=st.st_size;
} else
last_length=buffersize;
/* buffer whole pages */
last_length=(last_length+4095)&0xfffff000;
s=xmalloc(last_length);
}
if (s) {
setvbuf(input_f,s,_IOFBF,last_length);
}
}
vpos = 0;
/* Check for directory or block special files */
fstat(fileno(input_f), &f);
if (S_ISDIR(f.st_mode) || S_ISBLK(f.st_mode)) {
error(0,0, _("is not a file: %s"),name);
fclose(input_f);
free(name);
return OK;
}
if (remotename) {
/* disqualify const */
union {
const char *c;
char *s;
} cheat;
cheat.c=remotename;
zi.fname=cheat.s;
} else
zi.fname=name;
zi.modtime=f.st_mtime;
zi.mode=f.st_mode;
zi.bytes_total= (S_ISFIFO(f.st_mode)) ? DEFBYTL : f.st_size;
zi.bytes_sent=0;
zi.bytes_received=0;
zi.bytes_skipped=0;
zi.eof_seen=0;
timing(1,NULL);
++Filcnt;
switch (wctxpn(&zi)) {
case ERROR:
lrzsz_log(LOG_INFO, &zi, _("error occured"));
free(name);
return ERROR;
case ZSKIP:
lrzsz_log(LOG_INFO, &zi, _("skipped"));
error(0,0, _("skipped: %s"),name);
free(name);
return OK;
}
if (!zmodem_requested && wctx(&zi)==ERROR)
{
lrzsz_log(LOG_INFO, &zi, _("error occured"));
free(name);
return ERROR;
}
if (Unlinkafter)
unlink(oname);
if (1) {
long bps;
double d=timing(0,NULL);
if (d==0) /* can happen if timing() uses time() */
d=0.5;
bps=zi.bytes_sent/d;
vchar('\r');
lrzsz_log(LOG_INFO, &zi, "%ld bytes, %ld bps", (long) zi.bytes_sent,bps);
}
free(name);
return 0;
}
/*
* generate and transmit pathname block consisting of
* pathname (null terminated),
* file length, mode time and file mode in octal
* as provided by the Unix fstat call.
* N.B.: modifies the passed name, may extend it!
*/
static int
wctxpn(struct zm_fileinfo *zi)
{
register char *p, *q;
struct stat f;
if (protocol==ZM_XMODEM) {
if (*zi->fname && fstat(fileno(input_f), &f)!= -1) {
lrzsz_log(LOG_INFO, zi, "Sending %ld blocks: ",
(long) (f.st_size>>7));
}
lrzsz_log(LOG_INFO,NULL, "Give your local XMODEM receive command now.");
return OK;
}
if (!zmodem_requested)
if (getnak()) {
lrzsz_log(LOG_NOTICE, zi, "getnak failed");
return ERROR;
}
for (p=zi->fname, q=txbuf ; *p; )
if ((*q++ = *p++) == '/' && !Fullname)
q = txbuf;
*q++ = 0;
p=q;
while (q < (txbuf + MAX_BLOCK))
*q++ = 0;
/* note that we may lose some information here in case mode_t is wider than an
* int. But i believe sending %lo instead of %o _could_ break compatability
*/
if (!Ascii && (input_f!=stdin) && *zi->fname && fstat(fileno(input_f), &f)!= -1)
sprintf(p, "%lu %lo %o 0 %d %ld", (long) f.st_size, f.st_mtime,
(unsigned int)((no_unixmode) ? 0 : f.st_mode),
Filesleft, Totalleft);
lrzsz_log(LOG_INFO,zi,"Sending: %s\n",txbuf);
Totalleft -= f.st_size;
if (--Filesleft <= 0)
Totalleft = 0;
if (Totalleft < 0)
Totalleft = 0;
/* force 1k blocks if name won't fit in 128 byte block */
if (txbuf[125])
blklen=1024;
else { /* A little goodie for IMP/KMD */
txbuf[127] = (f.st_size + 127) >>7;
txbuf[126] = (f.st_size + 127) >>15;
}
if (zmodem_requested)
return zsendfile(zi,txbuf, 1+strlen(p)+(p-txbuf));
if (wcputsec(txbuf, 0, 128)==ERROR) {
lrzsz_log(LOG_NOTICE, zi, "wcputsec failed");
return ERROR;
}
return OK;
}
static int
getnak(void)
{
int firstch;
int tries=0;
Lastrx = 0;
for (;;) {
tries++;
switch (firstch = READLINE_PF(100)) {
case ZPAD:
if (getzrxinit())
return ERROR;
Ascii = 0; /* Receiver does the conversion */
return FALSE;
case TIMEOUT:
/* 30 seconds are enough */
if (tries==3) {
lrzsz_log(LOG_INFO,NULL,"Timeout on pathname");
return TRUE;
}
/* don't send a second ZRQINIT _directly_ after the
* first one. Never send more then 4 ZRQINIT, because
* omen rz stops if it saw 5 of them */
if ((zrqinits_sent>1 || tries>1) && zrqinits_sent<4) {
/* if we already sent a ZRQINIT we are using zmodem
* protocol and may send further ZRQINITs
*/
stohdr(0L);
zshhdr(ZRQINIT, Txhdr);
zrqinits_sent++;
}
continue;
case WANTG:
lrzsz_iomode(io_mode_fd, LRZSZ_IOMODE_UNRAW_G, &config);
Optiong = TRUE;
blklen=1024;
case WANTCRC:
Crcflg = TRUE;
case NAK:
return FALSE;
case CAN:
if ((firstch = READLINE_PF(20)) == CAN && Lastrx == CAN)
return TRUE;
default:
break;
}
Lastrx = firstch;
}
}
static int
wctx(struct zm_fileinfo *zi)
{
register size_t thisblklen;
register int sectnum, attempts, firstch;
firstsec=TRUE; thisblklen = blklen;
lrzsz_log(LOG_DEBUG, zi, "wctx: file length=%ld", (long) zi->bytes_total);
while ((firstch=READLINE_PF(Rxtimeout))!=NAK && firstch != WANTCRC
&& firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
;
if (firstch==CAN) {
lrzsz_log(LOG_NOTICE,zi, "Receiver Cancelled");
return ERROR;
}
if (firstch==WANTCRC)
Crcflg=TRUE;
if (firstch==WANTG)
Crcflg=TRUE;
sectnum=0;
for (;;) {
if (zi->bytes_total <= (zi->bytes_sent + 896L))
thisblklen = 128;
if ( !filbuf(txbuf, thisblklen))
break;
if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)
return ERROR;
zi->bytes_sent += thisblklen;
}
fclose(input_f);
attempts=0;
do {
purgeline(io_mode_fd);
sendline(EOT);
flushmo();
++attempts;
} while ((firstch=(READLINE_PF(Rxtimeout)) != ACK) && attempts < RETRYMAX);
if (attempts == RETRYMAX) {
lrzsz_log(LOG_INFO,zi,"No ACK on EOT");
return ERROR;
}
else
return OK;
}
static int
wcputsec(char *buf, int sectnum, size_t cseclen)
{
int checksum, wcj;
char *cp;
unsigned oldcrc;
int firstch;
int attempts;
firstch=0; /* part of logic to detect CAN CAN */
if (Verbose>1) {
vchar('\r');
if (protocol==ZM_XMODEM) {
lrzsz_log(LOG_DEBUG, NULL, "Xmodem sectors/kbytes sent: %3d/%2dk",
Totsecs, Totsecs/8 );
} else {
lrzsz_log(LOG_DEBUG, NULL, "Ymodem sectors/kbytes sent: %3d/%2dk",
Totsecs, Totsecs/8 );
}
}
for (attempts=0; attempts <= RETRYMAX; attempts++) {
Lastrx= firstch;
sendline(cseclen==1024?STX:SOH);
sendline(sectnum);
sendline(-sectnum -1);
oldcrc=checksum=0;
for (wcj=cseclen,cp=buf; --wcj>=0; ) {
sendline(*cp);
oldcrc=updcrc((0377& *cp), oldcrc);
checksum += *cp++;
}
if (Crcflg) {
oldcrc=updcrc(0,updcrc(0,oldcrc));
sendline((int)oldcrc>>8);
sendline((int)oldcrc);
}
else
sendline(checksum);