Debian Codegraph @

src/lsz.c

raw ◀ ▶
Source reconstructed at patch state 0 of 7 — not the installed (fully patched) source.
7 Debian patches touch this file
1 /*
2 lsz - send files with x/y/zmodem
3 Copyright (C) until 1988 Chuck Forsberg (Omen Technology INC)
4 Copyright (C) 1994 Matt Porter, Michael D. Black
5 Copyright (C) 1996, 1997 Uwe Ohse
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21
22 originally written by Chuck Forsberg
23 */
24 #include "zglobal.h"
25
26 /* char *getenv(); */
27
28 #define SS_NORMAL 0
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <signal.h>
32 #include <setjmp.h>
33 #include <ctype.h>
34 #include <errno.h>
35 #include <getopt.h>
36
37 #ifndef R_OK
38 # define R_OK 4
39 #endif
40
41 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MMAP)
42 # include <sys/mman.h>
43 size_t mm_size;
44 void *mm_addr=NULL;
45 #else
46 # undef HAVE_MMAP
47 #endif
48 #include "timing.h"
49 #include "long-options.h"
50 #include "xstrtoul.h"
51 #include "error.h"
52
53 #ifndef STRICT_PROTOTYPES
54 extern time_t time();
55 extern char *strerror();
56 extern char *strstr();
57 #endif
58
59 #ifndef HAVE_ERRNO_DECLARATION
60 extern int errno;
61 #endif
62
63 unsigned Baudrate=2400; /* Default, should be set by first mode() call */
64 unsigned Txwindow; /* Control the size of the transmitted window */
65 unsigned Txwspac; /* Spacing between zcrcq requests */
66 unsigned Txwcnt; /* Counter used to space ack requests */
67 size_t Lrxpos; /* Receiver's last reported offset */
68 int errors;
69 enum zm_type_enum protocol;
70 int under_rsh=FALSE;
71 extern int turbo_escape;
72 static int no_unixmode;
73
74 int Canseek=1; /* 1: can; 0: only rewind, -1: neither */
75
76 static int zsendfile __P ((struct zm_fileinfo *zi, const char *buf, size_t blen));
77 static int getnak __P ((void));
78 static int wctxpn __P ((struct zm_fileinfo *));
79 static int wcs __P ((const char *oname, const char *remotename));
80 static size_t zfilbuf __P ((struct zm_fileinfo *zi));
81 static size_t filbuf __P ((char *buf, size_t count));
82 static int getzrxinit __P ((void));
83 static int calc_blklen __P ((long total_sent));
84 static int sendzsinit __P ((void));
85 static int wctx __P ((struct zm_fileinfo *));
86 static int zsendfdata __P ((struct zm_fileinfo *));
87 static int getinsync __P ((struct zm_fileinfo *, int flag));
88 static void countem __P ((int argc, char **argv));
89 static void chkinvok __P ((const char *s));
90 static void usage __P ((int exitcode, const char *what));
91 static int zsendcmd __P ((const char *buf, size_t blen));
92 static void saybibi __P ((void));
93 static int wcsend __P ((int argc, char *argp[]));
94 static int wcputsec __P ((char *buf, int sectnum, size_t cseclen));
95 static void usage1 __P ((int exitcode));
96
97 #ifdef ENABLE_SYSLOG
98 #define DO_SYSLOG(message) do { \
99 if (enable_syslog) { \
100 const char *shortname; \
101 if (!zi->fname) \
102 shortname="no.name"; \
103 else { \
104 shortname=strrchr(zi->fname,'/'); \
105 if (!shortname) \
106 shortname=zi->fname; \
107 else \
108 shortname++; \
109 } \
110 lsyslog message ; \
111 } \
112 } while(0)
113 #else
114 #define DO_SYSLOG(message) do { } while(0)
115 #endif
116
117 #define ZSDATA(x,y,z) \
118 do { if (Crc32t) {zsda32(x,y,z); } else {zsdata(x,y,z);}} while(0)
119 #ifdef HAVE_MMAP
120 #define DATAADR (mm_addr ? ((char *)mm_addr)+zi->bytes_sent : txbuf)
121 #else
122 #define DATAADR (txbuf)
123 #endif
124
125 int Filesleft;
126 long Totalleft;
127 size_t buffersize=16384;
128 #ifdef HAVE_MMAP
129 int use_mmap=1;
130 #endif
131
132 /*
133 * Attention string to be executed by receiver to interrupt streaming data
134 * when an error is detected. A pause (0336) may be needed before the
135 * ^C (03) or after it.
136 */
137 #ifdef READCHECK
138 char Myattn[] = { 0 };
139 #else
140 char Myattn[] = { 03, 0336, 0 };
141 #endif
142
143 FILE *input_f;
144
145 #define MAX_BLOCK 8192
146 char *txbuf;
147
148 long vpos = 0; /* Number of bytes read from file */
149
150 char Lastrx;
151 char Crcflg;
152 int Verbose=0;
153 int Restricted=0; /* restricted; no /.. or ../ in filenames */
154 int Quiet=0; /* overrides logic that would otherwise set verbose */
155 int Ascii=0; /* Add CR's for brain damaged programs */
156 int Fullname=0; /* transmit full pathname */
157 int Unlinkafter=0; /* Unlink file after it is sent */
158 int Dottoslash=0; /* Change foo.bar.baz to foo/bar/baz */
159 int firstsec;
160 int errcnt=0; /* number of files unreadable */
161 size_t blklen=128; /* length of transmitted records */
162 int Optiong; /* Let it rip no wait for sector ACK's */
163 int Totsecs; /* total number of sectors this file */
164 int Filcnt=0; /* count of number of files opened */
165 int Lfseen=0;
166 unsigned Rxbuflen = 16384; /* Receiver's max buffer length */
167 unsigned Tframlen = 0; /* Override for tx frame length */
168 unsigned blkopt=0; /* Override value for zmodem blklen */
169 int Rxflags = 0;
170 int Rxflags2 = 0;
171 size_t bytcnt;
172 int Wantfcs32 = TRUE; /* want to send 32 bit FCS */
173 char Lzconv; /* Local ZMODEM file conversion request */
174 char Lzmanag; /* Local ZMODEM file management request */
175 int Lskipnocor;
176 char Lztrans;
177 char zconv; /* ZMODEM file conversion request */
178 char zmanag; /* ZMODEM file management request */
179 char ztrans; /* ZMODEM file transport request */
180 int command_mode; /* Send a command, then exit. */
181 int Cmdtries = 11;
182 int Cmdack1; /* Rx ACKs command, then do it */
183 int Exitcode;
184 int enable_timesync=0;
185 size_t Lastsync; /* Last offset to which we got a ZRPOS */
186 int Beenhereb4; /* How many times we've been ZRPOS'd same place */
187
188 int no_timeout=FALSE;
189 size_t max_blklen=1024;
190 size_t start_blklen=0;
191 int zmodem_requested;
192 time_t stop_time=0;
193 char *tcp_server_address=0;
194 int tcp_socket=-1;
195 int tcp_flag=0;
196
197 int error_count;
198 #define OVERHEAD 18
199 #define OVER_ERR 20
200
201 #define MK_STRING(x) #x
202
203 #ifdef ENABLE_SYSLOG
204 # if defined(ENABLE_SYSLOG_FORCE) || defined(ENABLE_SYSLOG_DEFAULT)
205 int enable_syslog=TRUE;
206 # else
207 int enable_syslog=FALSE;
208 # endif
209 #endif
210
211 jmp_buf intrjmp; /* For the interrupt on RX CAN */
212
213 static long min_bps;
214 static long min_bps_time;
215
216 static int io_mode_fd=0;
217 static int zrqinits_sent=0;
218 static int play_with_sigint=0;
219
220 /* called by signal interrupt or terminate to clean things up */
221 RETSIGTYPE
222 bibi (int n)
223 {
224 canit(STDOUT_FILENO);
225 fflush (stdout);
226 io_mode (io_mode_fd,0);
227 if (n == 99)
228 error (0, 0, _ ("io_mode(,2) in rbsb.c not implemented\n"));
229 else
230 error (0, 0, _ ("caught signal %d; exiting"), n);
231 if (n == SIGQUIT)
232 abort ();
233 exit (128 + n);
234 }
235
236 /* Called when ZMODEM gets an interrupt (^C) */
237 static RETSIGTYPE
238 onintr(int n)
239 {
240 signal(SIGINT, SIG_IGN);
241 n++; /* use it */
242 longjmp(intrjmp, -1);
243 }
244
245 int Zctlesc; /* Encode control characters */
246 const char *program_name = "sz";
247 int Zrwindow = 1400; /* RX window size (controls garbage count) */
248
249 static struct option const long_options[] =
250 {
251 {"append", no_argument, NULL, '+'},
252 {"twostop", no_argument, NULL, '2'},
253 {"try-8k", no_argument, NULL, '8'},
254 {"start-8k", no_argument, NULL, '9'},
255 {"try-4k", no_argument, NULL, '4'},
256 {"start-4k", no_argument, NULL, '5'},
257 {"ascii", no_argument, NULL, 'a'},
258 {"binary", no_argument, NULL, 'b'},
259 {"bufsize", required_argument, NULL, 'B'},
260 {"cmdtries", required_argument, NULL, 'C'},
261 {"command", required_argument, NULL, 'c'},
262 {"immediate-command", required_argument, NULL, 'i'},
263 {"dot-to-slash", no_argument, NULL, 'd'},
264 {"full-path", no_argument, NULL, 'f'},
265 {"escape", no_argument, NULL, 'e'},
266 {"rename", no_argument, NULL, 'E'},
267 {"help", no_argument, NULL, 'h'},
268 {"crc-check", no_argument, NULL, 'H'},
269 {"1024", no_argument, NULL, 'k'},
270 {"1k", no_argument, NULL, 'k'},
271 {"packetlen", required_argument, NULL, 'L'},
272 {"framelen", required_argument, NULL, 'l'},
273 {"min-bps", required_argument, NULL, 'm'},
274 {"min-bps-time", required_argument, NULL, 'M'},
275 {"newer", no_argument, NULL, 'n'},
276 {"newer-or-longer", no_argument, NULL, 'N'},
277 {"16-bit-crc", no_argument, NULL, 'o'},
278 {"disable-timeouts", no_argument, NULL, 'O'},
279 {"disable-timeout", no_argument, NULL, 'O'}, /* i can't get it right */
280 {"protect", no_argument, NULL, 'p'},
281 {"resume", no_argument, NULL, 'r'},
282 {"restricted", no_argument, NULL, 'R'},
283 {"quiet", no_argument, NULL, 'q'},
284 {"stop-at", required_argument, NULL, 's'},
285 {"syslog", optional_argument, NULL , 2},
286 {"timesync", no_argument, NULL, 'S'},
287 {"timeout", required_argument, NULL, 't'},
288 {"turbo", no_argument, NULL, 'T'},
289 {"unlink", no_argument, NULL, 'u'},
290 {"unrestrict", no_argument, NULL, 'U'},
291 {"verbose", no_argument, NULL, 'v'},
292 {"windowsize", required_argument, NULL, 'w'},
293 {"xmodem", no_argument, NULL, 'X'},
294 {"ymodem", no_argument, NULL, 1},
295 {"zmodem", no_argument, NULL, 'Z'},
296 {"overwrite", no_argument, NULL, 'y'},
297 {"overwrite-or-skip", no_argument, NULL, 'Y'},
298
299 {"delay-startup", required_argument, NULL, 4},
300 {"tcp-server", no_argument, NULL, 6},
301 {"tcp-client", required_argument, NULL, 7},
302 {"no-unixmode", no_argument, NULL, 8},
303 {NULL, 0, NULL, 0}
304 };
305
306 static void
307 show_version(void)
308 {
309 printf ("%s (%s) %s\n", program_name, PACKAGE, VERSION);
310 }
311
312
313 int
314 main(int argc, char **argv)
315 {
316 char *cp;
317 int npats;
318 int dm;
319 int i;
320 int stdin_files;
321 char **patts;
322 int c;
323 const char *Cmdstr=NULL; /* Pointer to the command string */
324 unsigned int startup_delay=0;
325
326 if (((cp = getenv("ZNULLS")) != NULL) && *cp)
327 Znulls = atoi(cp);
328 if (((cp=getenv("SHELL"))!=NULL) && (strstr(cp, "rsh") || strstr(cp, "rksh")
329 || strstr(cp, "rbash") || strstr(cp,"rshell")))
330 {
331 under_rsh=TRUE;
332 Restricted=1;
333 }
334 if ((cp=getenv("ZMODEM_RESTRICTED"))!=NULL)
335 Restricted=1;
336 from_cu();
337 chkinvok(argv[0]);
338
339 #ifdef ENABLE_SYSLOG
340 openlog(program_name,LOG_PID,ENABLE_SYSLOG);
341 #endif
342
343 setlocale (LC_ALL, "");
344 bindtextdomain (PACKAGE, LOCALEDIR);
345 textdomain (PACKAGE);
346
347 parse_long_options (argc, argv, show_version, usage1);
348
349 Rxtimeout = 600;
350
351 while ((c = getopt_long (argc, argv,
352 "2+48abB:C:c:dfeEghHi:kL:l:m:M:NnOopRrqsSt:TUuvw:XYy",
353 long_options, (int *) 0))!=EOF)
354 {
355 unsigned long int tmp;
356 char *tmpptr;
357 enum strtol_error s_err;
358
359 switch (c)
360 {
361 case 0:
362 break;
363 case '+': Lzmanag = ZF1_ZMAPND; break;
364 case '2': Twostop = TRUE; break;
365 case '8':
366 if (max_blklen==8192)
367 start_blklen=8192;
368 else
369 max_blklen=8192;
370 break;
371 case '9': /* this is a longopt .. */
372 start_blklen=8192;
373 max_blklen=8192;
374 break;
375 case '4':
376 if (max_blklen==4096)
377 start_blklen=4096;
378 else
379 max_blklen=4096;
380 break;
381 case '5': /* this is a longopt .. */
382 start_blklen=4096;
383 max_blklen=4096;
384 break;
385 case 'a': Lzconv = ZCNL; Ascii = TRUE; break;
386 case 'b': Lzconv = ZCBIN; break;
387 case 'B':
388 if (0==strcmp(optarg,"auto"))
389 buffersize= (size_t) -1;
390 else
391 buffersize=strtol(optarg,NULL,10);
392 #ifdef HAVE_MMAP
393 use_mmap=0;
394 #endif
395 break;
396 case 'C':
397 s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
398 Cmdtries = tmp;
399 if (s_err != LONGINT_OK)
400 STRTOL_FATAL_ERROR (optarg, _("command tries"), s_err);
401 break;
402 case 'i':
403 Cmdack1 = ZCACK1;
404 /* **** FALL THROUGH TO **** */
405 case 'c':
406 command_mode = TRUE;
407 Cmdstr = optarg;
408 break;
409 case 'd':
410 ++Dottoslash;
411 /* **** FALL THROUGH TO **** */
412 case 'f': Fullname=TRUE; break;
413 case 'e': Zctlesc = 1; break;
414 case 'E': Lzmanag = ZF1_ZMCHNG; break;
415 case 'h': usage(0,NULL); break;
416 case 'H': Lzmanag = ZF1_ZMCRC; break;
417 case 'k': start_blklen=1024; break;
418 case 'L':
419 s_err = xstrtoul (optarg, NULL, 0, &tmp, "ck");
420 blkopt = tmp;
421 if (s_err != LONGINT_OK)
422 STRTOL_FATAL_ERROR (optarg, _("packetlength"), s_err);
423 if (blkopt<24 || blkopt>MAX_BLOCK)
424 {
425 char meld[256];
426 sprintf(meld,
427 _("packetlength out of range 24..%ld"),
428 (long) MAX_BLOCK);
429 usage(2,meld);
430 }
431 break;
432 case 'l':
433 s_err = xstrtoul (optarg, NULL, 0, &tmp, "ck");
434 Tframlen = tmp;
435 if (s_err != LONGINT_OK)
436 STRTOL_FATAL_ERROR (optarg, _("framelength"), s_err);
437 if (Tframlen<32 || Tframlen>MAX_BLOCK)
438 {
439 char meld[256];
440 sprintf(meld,
441 _("framelength out of range 32..%ld"),
442 (long) MAX_BLOCK);
443 usage(2,meld);
444 }
445 break;
446 case 'm':
447 s_err = xstrtoul (optarg, &tmpptr, 0, &tmp, "km");
448 min_bps = tmp;
449 if (s_err != LONGINT_OK)
450 STRTOL_FATAL_ERROR (optarg, _("min_bps"), s_err);
451 if (min_bps<0)
452 usage(2,_("min_bps must be >= 0"));
453 break;
454 case 'M':
455 s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
456 min_bps_time = tmp;
457 if (s_err != LONGINT_OK)
458 STRTOL_FATAL_ERROR (optarg, _("min_bps_time"), s_err);
459 if (min_bps_time<=1)
460 usage(2,_("min_bps_time must be > 1"));
461 break;
462 case 'N': Lzmanag = ZF1_ZMNEWL; break;
463 case 'n': Lzmanag = ZF1_ZMNEW; break;
464 case 'o': Wantfcs32 = FALSE; break;
465 case 'O': no_timeout = TRUE; break;
466 case 'p': Lzmanag = ZF1_ZMPROT; break;
467 case 'r':
468 if (Lzconv == ZCRESUM)
469 Lzmanag = ZF1_ZMCRC;
470 else
471 Lzconv = ZCRESUM;
472 break;
473 case 'R': Restricted = TRUE; break;
474 case 'q': Quiet=TRUE; Verbose=0; break;
475 case 's':
476 if (isdigit((unsigned char) (*optarg))) {
477 struct tm *tm;
478 time_t t;
479 int hh,mm;
480 char *nex;
481
482 hh = strtoul (optarg, &nex, 10);
483 if (hh>23)
484 usage(2,_("hour to large (0..23)"));
485 if (*nex!=':')
486 usage(2, _("unparsable stop time\n"));
487 nex++;
488 mm = strtoul (optarg, &nex, 10);
489 if (mm>59)
490 usage(2,_("minute to large (0..59)"));
491
492 t=time(NULL);
493 tm=localtime(&t);
494 tm->tm_hour=hh;
495 tm->tm_min=hh;
496 stop_time=mktime(tm);
497 if (stop_time<t)
498 stop_time+=86400L; /* one day more */
499 if (stop_time - t <10)
500 usage(2,_("stop time to small"));
501 } else {
502 s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
503 stop_time = tmp + time(0);
504 if (s_err != LONGINT_OK)
505 STRTOL_FATAL_ERROR (optarg, _("stop-at"), s_err);
506 if (tmp<10)
507 usage(2,_("stop time to small"));
508 }
509 break;
510 case 'S': enable_timesync=1; break;
511 case 'T': turbo_escape=1; break;
512 case 't':
513 s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
514 Rxtimeout = tmp;
515 if (s_err != LONGINT_OK)
516 STRTOL_FATAL_ERROR (optarg, _("timeout"), s_err);
517 if (Rxtimeout<10 || Rxtimeout>1000)
518 usage(2,_("timeout out of range 10..1000"));
519 break;
520 case 'u': ++Unlinkafter; break;
521 case 'U':
522 if (!under_rsh)
523 Restricted=0;
524 else
525 error(1,0,
526 _("security violation: can't do that under restricted shell\n"));
527 break;
528 case 'v': ++Verbose; break;
529 case 'w':
530 s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
531 Txwindow = tmp;
532 if (s_err != LONGINT_OK)
533 STRTOL_FATAL_ERROR (optarg, _("window size"), s_err);
534 if (Txwindow < 256)
535 Txwindow = 256;
536 Txwindow = (Txwindow/64) * 64;
537 Txwspac = Txwindow/4;
538 if (blkopt > Txwspac
539 || (!blkopt && Txwspac < MAX_BLOCK))
540 blkopt = Txwspac;
541 break;
542 case 'X': protocol=ZM_XMODEM; break;
543 case 1: protocol=ZM_YMODEM; break;
544 case 'Z': protocol=ZM_ZMODEM; break;
545 case 'Y':
546 Lskipnocor = TRUE;
547 /* **** FALLL THROUGH TO **** */
548 case 'y':
549 Lzmanag = ZF1_ZMCLOB; break;
550 case 2:
551 #ifdef ENABLE_SYSLOG
552 # ifndef ENABLE_SYSLOG_FORCE
553 if (optarg && (!strcmp(optarg,"off") || !strcmp(optarg,"no")))
554 {
555 if (under_rsh)
556 error(0,0, _("cannot turnoff syslog"));
557 else
558 enable_syslog=FALSE;
559 }
560 else
561 enable_syslog=TRUE;
562 # else
563 error(0,0, _("cannot turnoff syslog"));
564 # endif
565 #endif
566 break;
567 case 4:
568 s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
569 startup_delay = tmp;
570 if (s_err != LONGINT_OK)
571 STRTOL_FATAL_ERROR (optarg, _("startup delay"), s_err);
572 break;
573 case 6:
574 tcp_flag=2;
575 break;
576 case 7:
577 tcp_flag=3;
578 tcp_server_address=(char *)strdup(optarg);
579 if (!tcp_server_address) {
580 error(1,0,_("out of memory"));
581 }
582 break;
583 case 8: no_unixmode=1; break;
584 default:
585 usage (2,NULL);
586 break;
587 }
588 }
589
590 if (getuid()!=geteuid()) {
591 error(1,0,
592 _("this program was never intended to be used setuid\n"));
593 }
594
595 txbuf=malloc(MAX_BLOCK);
596 if (!txbuf) error(1,0,_("out of memory"));
597
598 zsendline_init();
599
600 if (start_blklen==0) {
601 if (protocol == ZM_ZMODEM) {
602 start_blklen=1024;
603 if (Tframlen) {
604 start_blklen=max_blklen=Tframlen;
605 }
606 }
607 else
608 start_blklen=128;
609 }
610
611 if (argc<2)
612 usage(2,_("need at least one file to send"));
613
614 if (startup_delay)
615 sleep(startup_delay);
616
617 #ifdef HAVE_SIGINTERRUPT
618 /* we want interrupted system calls to fail and not to be restarted. */
619 siginterrupt(SIGALRM,1);
620 #endif
621
622
623 npats = argc - optind;
624 patts=&argv[optind];
625
626 if (npats < 1 && !command_mode)
627 usage(2,_("need at least one file to send"));
628 if (command_mode && Restricted) {
629 printf(_("Can't send command in restricted mode\n"));
630 exit(1);
631 }
632
633 if (Fromcu && !Quiet) {
634 if (Verbose == 0)
635 Verbose = 2;
636 }
637 vfile("%s %s\n", program_name, VERSION);
638
639 if (tcp_flag==2) {
640 char buf[256];
641 #ifdef MAXHOSTNAMELEN
642 char hn[MAXHOSTNAMELEN];
643 #else
644 char hn[256];
645 #endif
646 char *p,*q;
647 int d;
648
649 /* tell receiver to receive via tcp */
650 d=tcp_server(buf);
651 p=strchr(buf+1,'<');
652 p++;
653 q=strchr(p,'>');
654 *q=0;
655 if (gethostname(hn,sizeof(hn))==-1) {
656 error(1,0, _("hostname too long\n"));
657 }
658 fprintf(stdout,"connect with lrz --tcp-client \"%s:%s\"\n",hn,p);
659 fflush(stdout);
660 /* ok, now that this file is sent we can switch to tcp */
661
662 tcp_socket=tcp_accept(d);
663 dup2(tcp_socket,0);
664 dup2(tcp_socket,1);
665 }
666 if (tcp_flag==3) {
667 char buf[256];
668 char *p;
669 p=strchr(tcp_server_address,':');
670 if (!p)
671 error(1,0, _("illegal server address\n"));
672 *p++=0;
673 sprintf(buf,"[%s] <%s>\n",tcp_server_address,p);
674
675 fprintf(stdout,"connecting to %s\n",buf);
676 fflush(stdout);
677
678 /* we need to switch to tcp mode */
679 tcp_socket=tcp_connect(buf);
680 dup2(tcp_socket,0);
681 dup2(tcp_socket,1);
682 }
683
684
685 {
686 /* we write max_blocklen (data) + 18 (ZModem protocol overhead)
687 * + escape overhead (about 4 %), so buffer has to be
688 * somewhat larger than max_blklen
689 */
690 char *s=malloc(max_blklen+1024);
691 if (!s)
692 {
693 zperr(_("out of memory"));
694 exit(1);
695 }
696 #ifdef SETVBUF_REVERSED
697 setvbuf(stdout,_IOFBF,s,max_blklen+1024);
698 #else
699 setvbuf(stdout,s,_IOFBF,max_blklen+1024);
700 #endif
701 }
702 blklen=start_blklen;
703
704 for (i=optind,stdin_files=0;i<argc;i++) {
705 if (0==strcmp(argv[i],"-"))
706 stdin_files++;
707 }
708
709 if (stdin_files>1) {
710 usage(1,_("can read only one file from stdin"));
711 } else if (stdin_files==1) {
712 io_mode_fd=1;
713 }
714 io_mode(io_mode_fd,1);
715 readline_setup(io_mode_fd, 128, 256);
716
717 if (signal(SIGINT, bibi) == SIG_IGN)
718 signal(SIGINT, SIG_IGN);
719 else {
720 signal(SIGINT, bibi);
721 play_with_sigint=1;
722 }
723 signal(SIGTERM, bibi);
724 signal(SIGPIPE, bibi);
725 signal(SIGHUP, bibi);
726
727 if ( protocol!=ZM_XMODEM) {
728 if (protocol==ZM_ZMODEM) {
729 printf("rz\r");
730 fflush(stdout);
731 }
732 countem(npats, patts);
733 if (protocol == ZM_ZMODEM) {
734 /* throw away any input already received. This doesn't harm
735 * as we invite the receiver to send it's data again, and
736 * might be useful if the receiver has already died or
737 * if there is dirt left if the line
738 */
739 #ifdef HAVE_SELECT
740 struct timeval t;
741 unsigned char throwaway;
742 fd_set f;
743 #endif
744
745 purgeline(io_mode_fd);
746
747 #ifdef HAVE_SELECT
748 t.tv_sec = 0;
749 t.tv_usec = 0;
750
751 FD_ZERO(&f);
752 FD_SET(io_mode_fd,&f);
753
754 while (select(1,&f,NULL,NULL,&t)) {
755 if (0==read(io_mode_fd,&throwaway,1)) /* EOF ... */
756 break;
757 }
758 #endif
759
760 purgeline(io_mode_fd);
761 stohdr(0L);
762 if (command_mode)
763 Txhdr[ZF0] = ZCOMMAND;
764 zshhdr(ZRQINIT, Txhdr);
765 zrqinits_sent++;
766 #if defined(ENABLE_TIMESYNC)
767 if (Rxflags2 != ZF1_TIMESYNC)
768 /* disable timesync if there are any flags we don't know.
769 * dsz/gsz seems to use some other flags! */
770 enable_timesync=FALSE;
771 if (Rxflags2 & ZF1_TIMESYNC && enable_timesync) {
772 Totalleft+=6; /* TIMESYNC never needs more */
773 Filesleft++;
774 }
775 #endif
776 }
777 }
778 fflush(stdout);
779
780 if (Cmdstr) {
781 if (getzrxinit()) {
782 Exitcode=0200; canit(STDOUT_FILENO);
783 }
784 else if (zsendcmd(Cmdstr, strlen(Cmdstr)+1)) {
785 Exitcode=0200; canit(STDOUT_FILENO);
786 }
787 } else if (wcsend(npats, patts)==ERROR) {
788 Exitcode=0200;
789 canit(STDOUT_FILENO);
790 }
791 fflush(stdout);
792 io_mode(io_mode_fd,0);
793 if (Exitcode)
794 dm=Exitcode;
795 else if (errcnt)
796 dm=1;
797 else
798 dm=0;
799 if (Verbose)
800 {
801 fputs("\r\n",stderr);
802 if (dm)
803 fputs(_("Transfer incomplete\n"),stderr);
804 else
805 fputs(_("Transfer complete\n"),stderr);
806 }
807 exit(dm);
808 /*NOTREACHED*/
809 }
810
811 static int
812 send_pseudo(const char *name, const char *data)
813 {
814 char *tmp;
815 const char *p;
816 int ret=0; /* ok */
817 size_t plen;
818 int fd;
819 int lfd;
820
821 p = getenv ("TMPDIR");
822 if (!p)
823 p = getenv ("TMP");
824 if (!p)
825 p = "/tmp";
826 tmp=malloc(PATH_MAX+1);
827 if (!tmp)
828 error(1,0,_("out of memory"));
829
830 plen=strlen(p);
831 memcpy(tmp,p,plen);
832 tmp[plen++]='/';
833
834 lfd=0;
835 do {
836 if (lfd++==10) {
837 free(tmp);
838 vstringf (_ ("send_pseudo %s: cannot open tmpfile %s: %s"),
839 name, tmp, strerror (errno));
840 vstring ("\r\n");
841 return 1;
842 }
843 sprintf(tmp+plen,"%s.%lu.%d",name,(unsigned long) getpid(),lfd);
844 fd=open(tmp,O_WRONLY|O_CREAT|O_EXCL,0700);
845 /* is O_EXCL guaranted to not follow symlinks?
846 * I don`t know ... so be careful
847 */
848 if (fd!=-1) {
849 struct stat st;
850 if (0!=lstat(tmp,&st)) {
851 vstringf (_ ("send_pseudo %s: cannot lstat tmpfile %s: %s"),
852 name, tmp, strerror (errno));
853 vstring ("\r\n");
854 unlink(tmp);
855 close(fd);
856 fd=-1;
857 } else {
858 if (S_ISLNK(st.st_mode)) {
859 vstringf (_ ("send_pseudo %s: avoiding symlink trap"),name);
860 vstring ("\r\n");
861 unlink(tmp);
862 close(fd);
863 fd=-1;
864 }
865 }
866 }
867 } while (fd==-1);
868 if (write(fd,data,strlen(data))!=(signed long) strlen(data)
869 || close(fd)!=0) {
870 vstringf (_ ("send_pseudo %s: cannot write to tmpfile %s: %s"),
871 name, tmp, strerror (errno));
872 vstring ("\r\n");
873 free(tmp);
874 return 1;
875 }
876
877 if (wcs (tmp,name) == ERROR) {
878 if (Verbose)
879 vstringf (_ ("send_pseudo %s: failed"),name);
880 else {
881 if (Verbose)
882 vstringf (_ ("send_pseudo %s: ok"),name);
883 Filcnt--;
884 }
885 vstring ("\r\n");
886 ret=1;
887 }
888 unlink (tmp);
889 free(tmp);
890 return ret;
891 }
892
893 static int
894 wcsend (int argc, char *argp[])
895 {
896 int n;
897
898 Crcflg = FALSE;
899 firstsec = TRUE;
900 bytcnt = (size_t) -1;
901
902 for (n = 0; n < argc; ++n) {
903 Totsecs = 0;
904 if (wcs (argp[n],NULL) == ERROR)
905 return ERROR;
906 }
907 #if defined(ENABLE_TIMESYNC)
908 if (Rxflags2 & ZF1_TIMESYNC && enable_timesync) {
909 /* implement Peter Mandrellas extension */
910 char buf[60];
911 time_t t = time (NULL);
912 struct tm *tm = localtime (&t); /* sets timezone */
913 strftime (buf, sizeof (buf) - 1, "%H:%M:%S", tm);
914 if (Verbose) {
915 vstring ("\r\n");
916 vstringf (_("Answering TIMESYNC at %s"),buf);
917 }
918 #if defined(HAVE_TIMEZONE_VAR)
919 sprintf(buf+strlen(buf),"%ld\r\n", timezone / 60);
920 if (Verbose)
921 vstringf (" (%s %ld)\r\n", _ ("timezone"), timezone / 60);
922 #else
923 if (Verbose)
924 vstringf (" (%s)\r\n", _ ("timezone unknown"));
925 #endif
926 send_pseudo("/$time$.t",buf);
927 }
928 #endif
929 Totsecs = 0;
930 if (Filcnt == 0) { /* bitch if we couldn't open ANY files */
931 #if 0
932 /* i *really* do not like this */
933 if (protocol != ZM_XMODEM) {
934 const char *Cmdstr; /* Pointer to the command string */
935 command_mode = TRUE;
936 Cmdstr = "echo \"lsz: Can't open any requested files\"";
937 if (getnak ()) {
938 Exitcode = 0200;
939 canit(STDOUT_FILENO);
940 }
941 if (!zmodem_requested)
942 canit(STDOUT_FILENO);
943 else if (zsendcmd (Cmdstr, 1 + strlen (Cmdstr))) {
944 Exitcode = 0200;
945 canit(STDOUT_FILENO);
946 }
947 Exitcode = 1;
948 return OK;
949 }
950 #endif
951 canit(STDOUT_FILENO);
952 vstring ("\r\n");
953 vstringf (_ ("Can't open any requested files."));
954 vstring ("\r\n");
955 return ERROR;
956 }
957 if (zmodem_requested)
958 saybibi ();
959 else if (protocol != ZM_XMODEM) {
960 struct zm_fileinfo zi;
961 char *pa;
962 pa=alloca(PATH_MAX+1);
963 *pa='\0';
964 zi.fname = pa;
965 zi.modtime = 0;
966 zi.mode = 0;
967 zi.bytes_total = 0;
968 zi.bytes_sent = 0;
969 zi.bytes_received = 0;
970 zi.bytes_skipped = 0;
971 wctxpn (&zi);
972 }
973 return OK;
974 }
975
976 static int
977 wcs(const char *oname, const char *remotename)
978 {
979 #if !defined(S_ISDIR)
980 int c;
981 #endif
982 struct stat f;
983 char *name;
984 struct zm_fileinfo zi;
985 #ifdef HAVE_MMAP
986 int dont_mmap_this=0;
987 #endif
988 #ifdef ENABLE_SYSLOG
989 const char *shortname;
990 shortname=strrchr(oname,'/');
991 if (shortname)
992 shortname++;
993 else
994 shortname=oname;
995 #endif
996
997
998 if (Restricted) {
999 /* restrict pathnames to current tree or uucppublic */
1000 if ( strstr(oname, "../")
1001 #ifdef PUBDIR
1002 || (oname[0]== '/' && strncmp(oname, MK_STRING(PUBDIR),
1003 strlen(MK_STRING(PUBDIR))))
1004 #endif
1005 ) {
1006 canit(STDOUT_FILENO);
1007 vchar('\r');
1008 error(1,0,
1009 _("security violation: not allowed to upload from %s"),oname);
1010 }
1011 }
1012
1013 if (0==strcmp(oname,"-")) {
1014 char *p=getenv("ONAME");
1015 name=alloca(PATH_MAX+1);
1016 if (p) {
1017 strcpy(name, p);
1018 } else {
1019 sprintf(name, "s%lu.lsz", (unsigned long) getpid());
1020 }
1021 input_f=stdin;
1022 #ifdef HAVE_MMAP
1023 dont_mmap_this=1;
1024 #endif
1025 } else if ((input_f=fopen(oname, "r"))==NULL) {
1026 int e=errno;
1027 error(0,e, _("cannot open %s"),oname);
1028 ++errcnt;
1029 return OK; /* pass over it, there may be others */
1030 } else {
1031 name=alloca(PATH_MAX+1);
1032 strcpy(name, oname);
1033 }
1034 #ifdef HAVE_MMAP
1035 if (!use_mmap || dont_mmap_this)
1036 #endif
1037 {
1038 static char *s=NULL;
1039 static size_t last_length=0;
1040 struct stat st;
1041 if (fstat(fileno(input_f),&st)==-1)
1042 st.st_size=1024*1024;
1043 if (buffersize==(size_t) -1 && s) {
1044 if ((size_t) st.st_size > last_length) {
1045 free(s);
1046 s=NULL;
1047 last_length=0;
1048 }
1049 }
1050 if (!s && buffersize) {
1051 last_length=16384;
1052 if (buffersize==(size_t) -1) {
1053 if (st.st_size>0)
1054 last_length=st.st_size;
1055 } else
1056 last_length=buffersize;
1057 /* buffer whole pages */
1058 last_length=(last_length+4095)&0xfffff000;
1059 s=malloc(last_length);
1060 if (!s) {
1061 zpfatal(_("out of memory"));
1062 exit(1);
1063 }
1064 }
1065 if (s) {
1066 #ifdef SETVBUF_REVERSED
1067 setvbuf(input_f,_IOFBF,s,last_length);
1068 #else
1069 setvbuf(input_f,s,_IOFBF,last_length);
1070 #endif
1071 }
1072 }
1073 vpos = 0;
1074 /* Check for directory or block special files */
1075 fstat(fileno(input_f), &f);
1076 #if defined(S_ISDIR)
1077 if (S_ISDIR(f.st_mode) || S_ISBLK(f.st_mode)) {
1078 #else
1079 c = f.st_mode & S_IFMT;
1080 if (c == S_IFDIR || c == S_IFBLK) {
1081 #endif
1082 error(0,0, _("is not a file: %s"),name);
1083 fclose(input_f);
1084 return OK;
1085 }
1086
1087 if (remotename) {
1088 /* disqualify const */
1089 union {
1090 const char *c;
1091 char *s;
1092 } cheat;
1093 cheat.c=remotename;
1094 zi.fname=cheat.s;
1095 } else
1096 zi.fname=name;
1097 zi.modtime=f.st_mtime;
1098 zi.mode=f.st_mode;
1099 #if defined(S_ISFIFO)
1100 zi.bytes_total= (S_ISFIFO(f.st_mode)) ? DEFBYTL : f.st_size;
1101 #else
1102 zi.bytes_total= c == S_IFIFO ? DEFBYTL : f.st_size;
1103 #endif
1104 zi.bytes_sent=0;
1105 zi.bytes_received=0;
1106 zi.bytes_skipped=0;
1107 zi.eof_seen=0;
1108 timing(1,NULL);
1109
1110 ++Filcnt;
1111 switch (wctxpn(&zi)) {
1112 case ERROR:
1113 #ifdef ENABLE_SYSLOG
1114 if (enable_syslog)
1115 lsyslog(LOG_INFO, _("%s/%s: error occured"),protname(),shortname);
1116 #endif
1117 return ERROR;
1118 case ZSKIP:
1119 error(0,0, _("skipped: %s"),name);
1120 #ifdef ENABLE_SYSLOG
1121 if (enable_syslog)
1122 lsyslog(LOG_INFO, _("%s/%s: skipped"),protname(),shortname);
1123 #endif
1124 return OK;
1125 }
1126 if (!zmodem_requested && wctx(&zi)==ERROR)
1127 {
1128 #ifdef ENABLE_SYSLOG
1129 if (enable_syslog)
1130 lsyslog(LOG_INFO, _("%s/%s: error occured"),protname(),shortname);
1131 #endif
1132 return ERROR;
1133 }
1134 if (Unlinkafter)
1135 unlink(oname);
1136
1137 if (Verbose > 1
1138 #ifdef ENABLE_SYSLOG
1139 || enable_syslog
1140 #endif
1141 ) {
1142 long bps;
1143 double d=timing(0,NULL);
1144 if (d==0) /* can happen if timing() uses time() */
1145 d=0.5;
1146 bps=zi.bytes_sent/d;
1147 vchar('\r');
1148 if (Verbose > 1)
1149 vstringf(_("Bytes Sent:%7ld BPS:%-8ld \n"),
1150 (long) zi.bytes_sent,bps);
1151 #ifdef ENABLE_SYSLOG
1152 if (enable_syslog)
1153 lsyslog(LOG_INFO, "%s/%s: %ld Bytes, %ld BPS",shortname,
1154 protname(), (long) zi.bytes_sent,bps);
1155 #endif
1156 }
1157 return 0;
1158 }
1159
1160 /*
1161 * generate and transmit pathname block consisting of
1162 * pathname (null terminated),
1163 * file length, mode time and file mode in octal
1164 * as provided by the Unix fstat call.
1165 * N.B.: modifies the passed name, may extend it!
1166 */
1167 static int
1168 wctxpn(struct zm_fileinfo *zi)
1169 {
1170 register char *p, *q;
1171 char *name2;
1172 struct stat f;
1173
1174 name2=alloca(PATH_MAX+1);
1175
1176 if (protocol==ZM_XMODEM) {
1177 if (Verbose && *zi->fname && fstat(fileno(input_f), &f)!= -1) {
1178 vstringf(_("Sending %s, %ld blocks: "),
1179 zi->fname, (long) (f.st_size>>7));
1180 }
1181 vstringf(_("Give your local XMODEM receive command now."));
1182 vstring("\r\n");
1183 return OK;
1184 }
1185 if (!zmodem_requested)
1186 if (getnak()) {
1187 vfile("getnak failed");
1188 DO_SYSLOG((LOG_INFO, "%s/%s: getnak failed",
1189 shortname,protname()));
1190 return ERROR;
1191 }
1192
1193 q = (char *) 0;
1194 if (Dottoslash) { /* change . to . */
1195 for (p=zi->fname; *p; ++p) {
1196 if (*p == '/')
1197 q = p;
1198 else if (*p == '.')
1199 *(q=p) = '/';
1200 }
1201 if (q && strlen(++q) > 8) { /* If name>8 chars */
1202 q += 8; /* make it .ext */
1203 strcpy(name2, q); /* save excess of name */
1204 *q = '.';
1205 strcpy(++q, name2); /* add it back */
1206 }
1207 }
1208
1209 for (p=zi->fname, q=txbuf ; *p; )
1210 if ((*q++ = *p++) == '/' && !Fullname)
1211 q = txbuf;
1212 *q++ = 0;
1213 p=q;
1214 while (q < (txbuf + MAX_BLOCK))
1215 *q++ = 0;
1216 /* note that we may lose some information here in case mode_t is wider than an
1217 * int. But i believe sending %lo instead of %o _could_ break compatability
1218 */
1219 if (!Ascii && (input_f!=stdin) && *zi->fname && fstat(fileno(input_f), &f)!= -1)
1220 sprintf(p, "%lu %lo %o 0 %d %ld", (long) f.st_size, f.st_mtime,
1221 (unsigned int)((no_unixmode) ? 0 : f.st_mode),
1222 Filesleft, Totalleft);
1223 if (Verbose)
1224 vstringf(_("Sending: %s\n"),txbuf);
1225 Totalleft -= f.st_size;
1226 if (--Filesleft <= 0)
1227 Totalleft = 0;
1228 if (Totalleft < 0)
1229 Totalleft = 0;
1230
1231 /* force 1k blocks if name won't fit in 128 byte block */
1232 if (txbuf[125])
1233 blklen=1024;
1234 else { /* A little goodie for IMP/KMD */
1235 txbuf[127] = (f.st_size + 127) >>7;
1236 txbuf[126] = (f.st_size + 127) >>15;
1237 }
1238 if (zmodem_requested)
1239 return zsendfile(zi,txbuf, 1+strlen(p)+(p-txbuf));
1240 if (wcputsec(txbuf, 0, 128)==ERROR) {
1241 vfile("wcputsec failed");
1242 DO_SYSLOG((LOG_INFO, "%s/%s: wcputsec failed",
1243 shortname,protname()));
1244 return ERROR;
1245 }
1246 return OK;
1247 }
1248
1249 static int
1250 getnak(void)
1251 {
1252 int firstch;
1253 int tries=0;
1254
1255 Lastrx = 0;
1256 for (;;) {
1257 tries++;
1258 switch (firstch = READLINE_PF(100)) {
1259 case ZPAD:
1260 if (getzrxinit())
1261 return ERROR;
1262 Ascii = 0; /* Receiver does the conversion */
1263 return FALSE;
1264 case TIMEOUT:
1265 /* 30 seconds are enough */
1266 if (tries==3) {
1267 zperr(_("Timeout on pathname"));
1268 return TRUE;
1269 }
1270 /* don't send a second ZRQINIT _directly_ after the
1271 * first one. Never send more then 4 ZRQINIT, because
1272 * omen rz stops if it saw 5 of them */
1273 if ((zrqinits_sent>1 || tries>1) && zrqinits_sent<4) {
1274 /* if we already sent a ZRQINIT we are using zmodem
1275 * protocol and may send further ZRQINITs
1276 */
1277 stohdr(0L);
1278 zshhdr(ZRQINIT, Txhdr);
1279 zrqinits_sent++;
1280 }
1281 continue;
1282 case WANTG:
1283 io_mode(io_mode_fd,2); /* Set cbreak, XON/XOFF, etc. */
1284 Optiong = TRUE;
1285 blklen=1024;
1286 case WANTCRC:
1287 Crcflg = TRUE;
1288 case NAK:
1289 return FALSE;
1290 case CAN:
1291 if ((firstch = READLINE_PF(20)) == CAN && Lastrx == CAN)
1292 return TRUE;
1293 default:
1294 break;
1295 }
1296 Lastrx = firstch;
1297 }
1298 }
1299
1300
1301 static int
1302 wctx(struct zm_fileinfo *zi)
1303 {
1304 register size_t thisblklen;
1305 register int sectnum, attempts, firstch;
1306
1307 firstsec=TRUE; thisblklen = blklen;
1308 vfile("wctx:file length=%ld", (long) zi->bytes_total);
1309
1310 while ((firstch=READLINE_PF(Rxtimeout))!=NAK && firstch != WANTCRC
1311 && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
1312 ;
1313 if (firstch==CAN) {
1314 zperr(_("Receiver Cancelled"));
1315 return ERROR;
1316 }
1317 if (firstch==WANTCRC)
1318 Crcflg=TRUE;
1319 if (firstch==WANTG)
1320 Crcflg=TRUE;
1321 sectnum=0;
1322 for (;;) {
1323 if (zi->bytes_total <= (zi->bytes_sent + 896L))
1324 thisblklen = 128;
1325 if ( !filbuf(txbuf, thisblklen))
1326 break;
1327 if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)
1328 return ERROR;
1329 zi->bytes_sent += thisblklen;
1330 }
1331 fclose(input_f);
1332 attempts=0;
1333 do {
1334 purgeline(io_mode_fd);
1335 sendline(EOT);
1336 flushmo();
1337 ++attempts;
1338 } while ((firstch=(READLINE_PF(Rxtimeout)) != ACK) && attempts < RETRYMAX);
1339 if (attempts == RETRYMAX) {
1340 zperr(_("No ACK on EOT"));
1341 return ERROR;
1342 }
1343 else
1344 return OK;
1345 }
1346
1347 static int
1348 wcputsec(char *buf, int sectnum, size_t cseclen)
1349 {
1350 int checksum, wcj;
1351 char *cp;
1352 unsigned oldcrc;
1353 int firstch;
1354 int attempts;
1355
1356 firstch=0; /* part of logic to detect CAN CAN */
1357
1358 if (Verbose>1) {
1359 vchar('\r');
1360 if (protocol==ZM_XMODEM) {
1361 vstringf(_("Xmodem sectors/kbytes sent: %3d/%2dk"), Totsecs, Totsecs/8 );
1362 } else {
1363 vstringf(_("Ymodem sectors/kbytes sent: %3d/%2dk"), Totsecs, Totsecs/8 );
1364 }
1365 }
1366 for (attempts=0; attempts <= RETRYMAX; attempts++) {
1367 Lastrx= firstch;
1368 sendline(cseclen==1024?STX:SOH);
1369 sendline(sectnum);
1370 sendline(-sectnum -1);
1371 oldcrc=checksum=0;
1372 for (wcj=cseclen,cp=buf; --wcj>=0; ) {
1373 sendline(*cp);
1374 oldcrc=updcrc((0377& *cp), oldcrc);
1375 checksum += *cp++;
1376 }
1377 if (Crcflg) {
1378 oldcrc=updcrc(0,updcrc(0,oldcrc));
1379 sendline((int)oldcrc>>8);
1380 sendline((int)oldcrc);
1381 }
1382 else
1383 sendline(checksum);
1384
1385 flushmo();
1386 if (Optiong) {
1387 firstsec = FALSE; return OK;
1388 }
1389 firstch = READLINE_PF(Rxtimeout);
1390 gotnak:
1391 switch (firstch) {
1392 case CAN:
1393 if(Lastrx == CAN) {
1394 cancan:
1395 zperr(_("Cancelled")); return ERROR;
1396 }
1397 break;
1398 case TIMEOUT:
1399 zperr(_("Timeout on sector ACK")); continue;
1400 case WANTCRC:
1401 if (firstsec)
1402 Crcflg = TRUE;
1403 case NAK:
1404 zperr(_("NAK on sector")); continue;
1405 case ACK:
1406 firstsec=FALSE;
1407 Totsecs += (cseclen>>7);
1408 return OK;
1409 case ERROR:
1410 zperr(_("Got burst for sector ACK")); break;
1411 default:
1412 zperr(_("Got %02x for sector ACK"), firstch); break;
1413 }
1414 for (;;) {
1415 Lastrx = firstch;
1416 if ((firstch = READLINE_PF(Rxtimeout)) == TIMEOUT)
1417 break;
1418 if (firstch == NAK || firstch == WANTCRC)
1419 goto gotnak;
1420 if (firstch == CAN && Lastrx == CAN)
1421 goto cancan;
1422 }
1423 }
1424 zperr(_("Retry Count Exceeded"));
1425 return ERROR;
1426 }
1427
1428 /* fill buf with count chars padding with ^Z for CPM */
1429 static size_t
1430 filbuf(char *buf, size_t count)
1431 {
1432 int c;
1433 size_t m;
1434
1435 if ( !Ascii) {
1436 m = read(fileno(input_f), buf, count);
1437 if (m <= 0)
1438 return 0;
1439 while (m < count)
1440 buf[m++] = 032;
1441 return count;
1442 }
1443 m=count;
1444 if (Lfseen) {
1445 *buf++ = 012; --m; Lfseen = 0;
1446 }
1447 while ((c=getc(input_f))!=EOF) {
1448 if (c == 012) {
1449 *buf++ = 015;
1450 if (--m == 0) {
1451 Lfseen = TRUE; break;
1452 }
1453 }
1454 *buf++ =c;
1455 if (--m == 0)
1456 break;
1457 }
1458 if (m==count)
1459 return 0;
1460 else
1461 while (m--!=0)
1462 *buf++ = CPMEOF;
1463 return count;
1464 }
1465
1466 /* Fill buffer with blklen chars */
1467 static size_t
1468 zfilbuf (struct zm_fileinfo *zi)
1469 {
1470 size_t n;
1471
1472 n = fread (txbuf, 1, blklen, input_f);
1473 if (n < blklen)
1474 zi->eof_seen = 1;
1475 else {
1476 /* save one empty paket in case file ends ob blklen boundary */
1477 int c = getc(input_f);
1478
1479 if (c != EOF || !feof(input_f))
1480 ungetc(c, input_f);
1481 else
1482 zi->eof_seen = 1;
1483 }
1484 return n;
1485 }
1486
1487 static void
1488 usage1 (int exitcode)
1489 {
1490 usage (exitcode, NULL);
1491 }
1492
1493 static void
1494 usage(int exitcode, const char *what)
1495 {
1496 FILE *f=stdout;
1497
1498 if (exitcode)
1499 {
1500 if (what)
1501 fprintf(stderr, "%s: %s\n",program_name,what);
1502 fprintf (stderr, _("Try `%s --help' for more information.\n"),
1503 program_name);
1504 exit(exitcode);
1505 }
1506
1507 fprintf(f, _("%s version %s\n"), program_name,
1508 VERSION);
1509
1510 fprintf(f,_("Usage: %s [options] file ...\n"),
1511 program_name);
1512 fprintf(f,_(" or: %s [options] -{c|i} COMMAND\n"),program_name);
1513 fputs(_("Send file(s) with ZMODEM/YMODEM/XMODEM protocol\n"),f);
1514 fputs(_(
1515 " (X) = option applies to XMODEM only\n"
1516 " (Y) = option applies to YMODEM only\n"
1517 " (Z) = option applies to ZMODEM only\n"
1518 ),f);
1519 /* splitted into two halves for really bad compilers */
1520 fputs(_(
1521 " -+, --append append to existing destination file (Z)\n"
1522 " -2, --twostop use 2 stop bits\n"
1523 " -4, --try-4k go up to 4K blocksize\n"
1524 " --start-4k start with 4K blocksize (doesn't try 8)\n"
1525 " -8, --try-8k go up to 8K blocksize\n"
1526 " --start-8k start with 8K blocksize\n"
1527 " -a, --ascii ASCII transfer (change CR/LF to LF)\n"
1528 " -b, --binary binary transfer\n"
1529 " -B, --bufsize N buffer N bytes (N==auto: buffer whole file)\n"
1530 " -c, --command COMMAND execute remote command COMMAND (Z)\n"
1531 " -C, --command-tries N try N times to execute a command (Z)\n"
1532 " -d, --dot-to-slash change '.' to '/' in pathnames (Y/Z)\n"
1533 " --delay-startup N sleep N seconds before doing anything\n"
1534 " -e, --escape escape all control characters (Z)\n"
1535 " -E, --rename force receiver to rename files it already has\n"
1536 " -f, --full-path send full pathname (Y/Z)\n"
1537 " -i, --immediate-command CMD send remote CMD, return immediately (Z)\n"
1538 " -h, --help print this usage message\n"
1539 " -k, --1k send 1024 byte packets (X)\n"
1540 " -L, --packetlen N limit subpacket length to N bytes (Z)\n"
1541 " -l, --framelen N limit frame length to N bytes (l>=L) (Z)\n"
1542 " -m, --min-bps N stop transmission if BPS below N\n"
1543 " -M, --min-bps-time N for at least N seconds (default: 120)\n"
1544 ),f);
1545 fputs(_(
1546 " -n, --newer send file if source newer (Z)\n"
1547 " -N, --newer-or-longer send file if source newer or longer (Z)\n"
1548 " -o, --16-bit-crc use 16 bit CRC instead of 32 bit CRC (Z)\n"
1549 " -O, --disable-timeouts disable timeout code, wait forever\n"
1550 " -p, --protect protect existing destination file (Z)\n"
1551 " -r, --resume resume interrupted file transfer (Z)\n"
1552 " -R, --restricted restricted, more secure mode\n"
1553 " -q, --quiet quiet (no progress reports)\n"
1554 " -s, --stop-at {HH:MM|+N} stop transmission at HH:MM or in N seconds\n"
1555 " --tcp-server open socket, wait for connection (Z)\n"
1556 " --tcp-client ADDR:PORT open socket, connect to ... (Z)\n"
1557 " -u, --unlink unlink file after transmission\n"
1558 " -U, --unrestrict turn off restricted mode (if allowed to)\n"
1559 " -v, --verbose be verbose, provide debugging information\n"
1560 " -w, --windowsize N Window is N bytes (Z)\n"
1561 " -X, --xmodem use XMODEM protocol\n"
1562 " -y, --overwrite overwrite existing files\n"
1563 " -Y, --overwrite-or-skip overwrite existing files, else skip\n"
1564 " --ymodem use YMODEM protocol\n"
1565 " -Z, --zmodem use ZMODEM protocol\n"
1566 "\n"
1567 "short options use the same arguments as the long ones\n"
1568 ),f);
1569 exit(exitcode);
1570 }
1571
1572 /*
1573 * Get the receiver's init parameters
1574 */
1575 static int
1576 getzrxinit(void)
1577 {
1578 static int dont_send_zrqinit=1;
1579 int old_timeout=Rxtimeout;
1580 int n;
1581 struct stat f;
1582 size_t rxpos;
1583 int timeouts=0;
1584
1585 Rxtimeout=100; /* 10 seconds */
1586 /* XXX purgeline(io_mode_fd); this makes _real_ trouble. why? -- uwe */
1587
1588 for (n=10; --n>=0; ) {
1589 /* we might need to send another zrqinit in case the first is
1590 * lost. But *not* if getting here for the first time - in
1591 * this case we might just get a ZRINIT for our first ZRQINIT.
1592 * Never send more then 4 ZRQINIT, because
1593 * omen rz stops if it saw 5 of them.
1594 */
1595 if (zrqinits_sent<4 && n!=10 && !dont_send_zrqinit) {
1596 zrqinits_sent++;
1597 stohdr(0L);
1598 zshhdr(ZRQINIT, Txhdr);
1599 }
1600 dont_send_zrqinit=0;
1601
1602 switch (zgethdr(Rxhdr, 1,&rxpos)) {
1603 case ZCHALLENGE: /* Echo receiver's challenge numbr */
1604 stohdr(rxpos);
1605 zshhdr(ZACK, Txhdr);
1606 continue;
1607 case ZCOMMAND: /* They didn't see our ZRQINIT */
1608 /* ??? Since when does a receiver send ZCOMMAND? -- uwe */
1609 continue;
1610 case ZRINIT:
1611 Rxflags = 0377 & Rxhdr[ZF0];
1612 Rxflags2 = 0377 & Rxhdr[ZF1];
1613 Txfcs32 = (Wantfcs32 && (Rxflags & CANFC32));
1614 {
1615 int old=Zctlesc;
1616 Zctlesc |= Rxflags & TESCCTL;
1617 /* update table - was initialised to not escape */
1618 if (Zctlesc && !old)
1619 zsendline_init();
1620 }
1621 Rxbuflen = (0377 & Rxhdr[ZP0])+((0377 & Rxhdr[ZP1])<<8);
1622 if ( !(Rxflags & CANFDX))
1623 Txwindow = 0;
1624 vfile("Rxbuflen=%d Tframlen=%d", Rxbuflen, Tframlen);
1625 if ( play_with_sigint)
1626 signal(SIGINT, SIG_IGN);
1627 io_mode(io_mode_fd,2); /* Set cbreak, XON/XOFF, etc. */
1628 #ifndef READCHECK
1629 /* Use MAX_BLOCK byte frames if no sample/interrupt */
1630 if (Rxbuflen < 32 || Rxbuflen > MAX_BLOCK) {
1631 Rxbuflen = MAX_BLOCK;
1632 vfile("Rxbuflen=%d", Rxbuflen);
1633 }
1634 #endif
1635 /* Override to force shorter frame length */
1636 if (Tframlen && Rxbuflen > Tframlen)
1637 Rxbuflen = Tframlen;
1638 if ( !Rxbuflen)
1639 Rxbuflen = 1024;
1640 vfile("Rxbuflen=%d", Rxbuflen);
1641
1642 /* If using a pipe for testing set lower buf len */
1643 fstat(0, &f);
1644 #if defined(S_ISCHR)
1645 if (! (S_ISCHR(f.st_mode))) {
1646 #else
1647 if ((f.st_mode & S_IFMT) != S_IFCHR) {
1648 #endif
1649 Rxbuflen = MAX_BLOCK;
1650 }
1651 /*
1652 * If input is not a regular file, force ACK's to
1653 * prevent running beyond the buffer limits
1654 */
1655 if ( !command_mode) {
1656 fstat(fileno(input_f), &f);
1657 #if defined(S_ISREG)
1658 if (!(S_ISREG(f.st_mode))) {
1659 #else
1660 if ((f.st_mode & S_IFMT) != S_IFREG) {
1661 #endif
1662 Canseek = -1;
1663 /* return ERROR; */
1664 }
1665 }
1666 /* Set initial subpacket length */
1667 if (blklen < 1024) { /* Command line override? */
1668 if (Baudrate > 300)
1669 blklen = 256;
1670 if (Baudrate > 1200)
1671 blklen = 512;
1672 if (Baudrate > 2400)
1673 blklen = 1024;
1674 }
1675 if (Rxbuflen && blklen>Rxbuflen)
1676 blklen = Rxbuflen;
1677 if (blkopt && blklen > blkopt)
1678 blklen = blkopt;
1679 vfile("Rxbuflen=%d blklen=%d", Rxbuflen, blklen);
1680 vfile("Txwindow = %u Txwspac = %d", Txwindow, Txwspac);
1681 Rxtimeout=old_timeout;
1682 return (sendzsinit());
1683 case ZCAN:
1684 case TIMEOUT:
1685 if (timeouts++==0)
1686 continue; /* force one other ZRQINIT to be sent */
1687 return ERROR;
1688 case ZRQINIT:
1689 if (Rxhdr[ZF0] == ZCOMMAND)
1690 continue;
1691 default:
1692 zshhdr(ZNAK, Txhdr);
1693 continue;
1694 }
1695 }
1696 return ERROR;
1697 }
1698
1699 /* Send send-init information */
1700 static int
1701 sendzsinit(void)
1702 {
1703 int c;
1704
1705 if (Myattn[0] == '\0' && (!Zctlesc || (Rxflags & TESCCTL)))
1706 return OK;
1707 errors = 0;
1708 for (;;) {
1709 stohdr(0L);
1710 if (Zctlesc) {
1711 Txhdr[ZF0] |= TESCCTL; zshhdr(ZSINIT, Txhdr);
1712 }
1713 else
1714 zsbhdr(ZSINIT, Txhdr);
1715 ZSDATA(Myattn, 1+strlen(Myattn), ZCRCW);
1716 c = zgethdr(Rxhdr, 1,NULL);
1717 switch (c) {
1718 case ZCAN:
1719 return ERROR;
1720 case ZACK:
1721 return OK;
1722 default:
1723 if (++errors > 19)
1724 return ERROR;
1725 continue;
1726 }
1727 }
1728 }
1729
1730 /* Send file name and related info */
1731 static int
1732 zsendfile(struct zm_fileinfo *zi, const char *buf, size_t blen)
1733 {
1734 unsigned long crc;
1735 size_t rxpos;
1736
1737 /* we are going to send a ZFILE. There cannot be much useful
1738 * stuff in the line right now (*except* ZCAN?).
1739 */
1740 #if 0
1741 purgeline(io_mode_fd); /* might possibly fix stefan glasers problems */
1742 #endif
1743
1744 for (;;) {
1745 int gotblock;
1746 int gotchar;
1747 Txhdr[ZF0] = Lzconv; /* file conversion request */
1748 Txhdr[ZF1] = Lzmanag; /* file management request */
1749 if (Lskipnocor)
1750 Txhdr[ZF1] |= ZF1_ZMSKNOLOC;
1751 Txhdr[ZF2] = Lztrans; /* file transport request */
1752 Txhdr[ZF3] = 0;
1753 zsbhdr(ZFILE, Txhdr);
1754 ZSDATA(buf, blen, ZCRCW);
1755 again:
1756 gotblock = zgethdr(Rxhdr, 1, &rxpos);
1757 switch (gotblock) {
1758 case ZRINIT:
1759 while ((gotchar = READLINE_PF(50)) > 0)
1760 if (gotchar == ZPAD) {
1761 goto again;
1762 }
1763 /* **** FALL THRU TO **** */
1764 default:
1765 continue;
1766 case ZRQINIT: /* remote site is sender! */
1767 if (Verbose)
1768 vstringf(_("got ZRQINIT"));
1769 DO_SYSLOG((LOG_INFO, "%s/%s: got ZRQINIT - sz talks to sz",
1770 shortname,protname()));
1771 return ERROR;
1772 case ZCAN:
1773 if (Verbose)
1774 vstringf(_("got ZCAN"));
1775 DO_SYSLOG((LOG_INFO, "%s/%s: got ZCAN - receiver canceled",
1776 shortname,protname()));
1777 return ERROR;
1778 case TIMEOUT:
1779 DO_SYSLOG((LOG_INFO, "%s/%s: got TIMEOUT",
1780 shortname,protname()));
1781 return ERROR;
1782 case ZABORT:
1783 DO_SYSLOG((LOG_INFO, "%s/%s: got ZABORT",
1784 shortname,protname()));
1785 return ERROR;
1786 case ZFIN:
1787 DO_SYSLOG((LOG_INFO, "%s/%s: got ZFIN",
1788 shortname,protname()));
1789 return ERROR;
1790 case ZCRC:
1791 crc = 0xFFFFFFFFL;
1792 #ifdef HAVE_MMAP
1793 if (use_mmap && !mm_addr)
1794 {
1795 struct stat st;
1796 /* mmap on files of 0 length can give 0 as result .. under linux 2.2.9 at least */
1797 if (fstat (fileno (input_f), &st) == 0 && st.st_size!=0) {
1798 mm_size = st.st_size;
1799 mm_addr = mmap (0, mm_size, PROT_READ,
1800 MAP_SHARED, fileno (input_f), 0);
1801 if ((caddr_t) mm_addr == (caddr_t) - 1)
1802 mm_addr = NULL;
1803 else {
1804 fclose (input_f);
1805 input_f = NULL;
1806 }
1807 }
1808 }
1809 if (mm_addr) {
1810 size_t i;
1811 size_t count;
1812 char *p=mm_addr;
1813 count=(rxpos < mm_size && rxpos > 0)? rxpos: mm_size;
1814 for (i=0;i<count;i++,p++) {
1815 crc = UPDC32(*p, crc);
1816 }
1817 crc = ~crc;
1818 } else
1819 #endif
1820 if (Canseek >= 0) {
1821 if (rxpos==0) {
1822 struct stat st;
1823 if (0==fstat(fileno(input_f),&st)) {
1824 rxpos=st.st_size;
1825 } else
1826 rxpos=-1;
1827 }
1828 while (rxpos-- && ((gotchar = getc(input_f)) != EOF))
1829 crc = UPDC32(gotchar, crc);
1830 crc = ~crc;
1831 clearerr(input_f); /* Clear EOF */
1832 fseek(input_f, 0L, 0);
1833 }
1834 stohdr(crc);
1835 zsbhdr(ZCRC, Txhdr);
1836 goto again;
1837 case ZSKIP:
1838 if (input_f) {
1839 fclose(input_f);
1840 input_f=NULL;
1841 }
1842 #ifdef HAVE_MMAP
1843 else if (mm_addr) {
1844 munmap(mm_addr,mm_size);
1845 mm_addr=NULL;
1846 }
1847 #endif
1848
1849 vfile("receiver skipped");
1850 DO_SYSLOG((LOG_INFO, "%s/%s: receiver skipped",
1851 shortname, protname()));
1852 return ZSKIP;
1853 case ZRPOS:
1854 /*
1855 * Suppress zcrcw request otherwise triggered by
1856 * lastsync==bytcnt
1857 */
1858 #ifdef HAVE_MMAP
1859 if (!mm_addr)
1860 #endif
1861 if (rxpos && fseek(input_f, (long) rxpos, 0)) {
1862 int er=errno;
1863 vfile("fseek failed: %s", strerror(er));
1864 DO_SYSLOG((LOG_INFO, "%s/%s: fseek failed: %s",
1865 shortname, protname(), strerror(er)));
1866 return ERROR;
1867 }
1868 if (rxpos)
1869 zi->bytes_skipped=rxpos;
1870 bytcnt = zi->bytes_sent = rxpos;
1871 Lastsync = rxpos -1;
1872 return zsendfdata(zi);
1873 }
1874 }
1875 }
1876
1877 /* Send the data in the file */
1878 static int
1879 zsendfdata (struct zm_fileinfo *zi)
1880 {
1881 static int c;
1882 int newcnt;
1883 static int junkcount; /* Counts garbage chars received by TX */
1884 static size_t last_txpos = 0;
1885 static long last_bps = 0;
1886 static long not_printed = 0;
1887 static long total_sent = 0;
1888 static time_t low_bps=0;
1889
1890 #ifdef HAVE_MMAP
1891 if (use_mmap && !mm_addr)
1892 {
1893 struct stat st;
1894 if (fstat (fileno (input_f), &st) == 0 && st.st_size!=0) {
1895 mm_size = st.st_size;
1896 mm_addr = mmap (0, mm_size, PROT_READ,
1897 MAP_SHARED, fileno (input_f), 0);
1898 if ((caddr_t) mm_addr == (caddr_t) - 1)
1899 mm_addr = NULL;
1900 else {
1901 fclose (input_f);
1902 input_f = NULL;
1903 }
1904 }
1905 }
1906 #endif
1907
1908 if (play_with_sigint)
1909 signal (SIGINT, onintr);
1910
1911 Lrxpos = 0;
1912 junkcount = 0;
1913 Beenhereb4 = 0;
1914 somemore:
1915 if (setjmp (intrjmp)) {
1916 if (play_with_sigint)
1917 signal (SIGINT, onintr);
1918 waitack:
1919 junkcount = 0;
1920 c = getinsync (zi, 0);
1921 gotack:
1922 switch (c) {
1923 default:
1924 if (input_f) {
1925 fclose (input_f);
1926 input_f=NULL;
1927 }
1928 DO_SYSLOG((LOG_INFO, "%s/%s: got %d",
1929 shortname, protname(), c));
1930 return ERROR;
1931 case ZCAN:
1932 if (input_f) {
1933 fclose (input_f);
1934 input_f=NULL;
1935 }
1936 DO_SYSLOG((LOG_INFO, "%s/%s: got ZCAN",
1937 shortname, protname()));
1938 return ERROR;
1939 case ZSKIP:
1940 if (input_f) {
1941 fclose (input_f);
1942 input_f=NULL;
1943 }
1944 DO_SYSLOG((LOG_INFO, "%s/%s: got ZSKIP",
1945 shortname, protname()));
1946 return ZSKIP;
1947 case ZACK:
1948 case ZRPOS:
1949 break;
1950 case ZRINIT:
1951 return OK;
1952 }
1953 #ifdef READCHECK
1954 /*
1955 * If the reverse channel can be tested for data,
1956 * this logic may be used to detect error packets
1957 * sent by the receiver, in place of setjmp/longjmp
1958 * rdchk(fdes) returns non 0 if a character is available
1959 */
1960 while (rdchk (io_mode_fd)) {
1961 #ifdef READCHECK_READS
1962 switch (checked)
1963 #else
1964 switch (READLINE_PF (1))
1965 #endif
1966 {
1967 case CAN:
1968 case ZPAD:
1969 c = getinsync (zi, 1);
1970 goto gotack;
1971 case XOFF: /* Wait a while for an XON */
1972 case XOFF | 0200:
1973 READLINE_PF (100);
1974 }
1975 }
1976 #endif
1977 }
1978
1979 newcnt = Rxbuflen;
1980 Txwcnt = 0;
1981 stohdr (zi->bytes_sent);
1982 zsbhdr (ZDATA, Txhdr);
1983
1984 do {
1985 size_t n;
1986 int e;
1987 unsigned old = blklen;
1988 blklen = calc_blklen (total_sent);
1989 total_sent += blklen + OVERHEAD;
1990 if (Verbose > 2 && blklen != old)
1991 vstringf (_("blklen now %d\n"), blklen);
1992 #ifdef HAVE_MMAP
1993 if (mm_addr) {
1994 if (zi->bytes_sent + blklen < mm_size)
1995 n = blklen;
1996 else {
1997 n = mm_size - zi->bytes_sent;
1998 zi->eof_seen = 1;
1999 }
2000 } else
2001 #endif
2002 n = zfilbuf (zi);
2003 if (zi->eof_seen) {
2004 e = ZCRCE;
2005 if (Verbose>3)
2006 vstring("e=ZCRCE/eof seen");
2007 } else if (junkcount > 3) {
2008 e = ZCRCW;
2009 if (Verbose>3)
2010 vstring("e=ZCRCW/junkcount > 3");
2011 } else if (bytcnt == Lastsync) {
2012 e = ZCRCW;
2013 if (Verbose>3)
2014 vstringf("e=ZCRCW/bytcnt == Lastsync == %ld",
2015 (unsigned long) Lastsync);
2016 #if 0
2017 /* what is this good for? Rxbuflen/newcnt normally are short - so after
2018 * a few KB ZCRCW will be used? (newcnt is never incremented)
2019 */
2020 } else if (Rxbuflen && (newcnt -= n) <= 0) {
2021 e = ZCRCW;
2022 if (Verbose>3)
2023 vstringf("e=ZCRCW/Rxbuflen(newcnt=%ld,n=%ld)",
2024 (unsigned long) newcnt,(unsigned long) n);
2025 #endif
2026 } else if (Txwindow && (Txwcnt += n) >= Txwspac) {
2027 Txwcnt = 0;
2028 e = ZCRCQ;
2029 if (Verbose>3)
2030 vstring("e=ZCRCQ/Window");
2031 } else {
2032 e = ZCRCG;
2033 if (Verbose>3)
2034 vstring("e=ZCRCG");
2035 }
2036 if ((Verbose > 1 || min_bps || stop_time)
2037 && (not_printed > (min_bps ? 3 : 7)
2038 || zi->bytes_sent > last_bps / 2 + last_txpos)) {
2039 int minleft = 0;
2040 int secleft = 0;
2041 time_t now;
2042 last_bps = (zi->bytes_sent / timing (0,&now));
2043 if (last_bps > 0) {
2044 minleft = (zi->bytes_total - zi->bytes_sent) / last_bps / 60;
2045 secleft = ((zi->bytes_total - zi->bytes_sent) / last_bps) % 60;
2046 }
2047 if (min_bps) {
2048 if (low_bps) {
2049 if (last_bps<min_bps) {
2050 if (now-low_bps>=min_bps_time) {
2051 /* too bad */
2052 if (Verbose) {
2053 vstringf(_("zsendfdata: bps rate %ld below min %ld"),
2054 last_bps, min_bps);
2055 vstring("\r\n");
2056 }
2057 DO_SYSLOG((LOG_INFO, "%s/%s: bps rate low: %ld <%ld",
2058 shortname, protname(), last_bps, min_bps));
2059 return ERROR;
2060 }
2061 } else
2062 low_bps=0;
2063 } else if (last_bps < min_bps) {
2064 low_bps=now;
2065 }
2066 }
2067 if (stop_time && now>=stop_time) {
2068 /* too bad */
2069 if (Verbose) {
2070 vstring(_("zsendfdata: reached stop time"));
2071 vstring("\r\n");
2072 }
2073 DO_SYSLOG((LOG_INFO, "%s/%s: reached stop time",
2074 shortname, protname()));
2075 return ERROR;
2076 }
2077
2078 if (Verbose > 1) {
2079 vchar ('\r');
2080 vstringf (_("Bytes Sent:%7ld/%7ld BPS:%-8ld ETA %02d:%02d "),
2081 (long) zi->bytes_sent, (long) zi->bytes_total,
2082 last_bps, minleft, secleft);
2083 }
2084 last_txpos = zi->bytes_sent;
2085 } else if (Verbose)
2086 not_printed++;
2087 ZSDATA (DATAADR, n, e);
2088 bytcnt = zi->bytes_sent += n;
2089 if (e == ZCRCW)
2090 goto waitack;
2091 #ifdef READCHECK
2092 /*
2093 * If the reverse channel can be tested for data,
2094 * this logic may be used to detect error packets
2095 * sent by the receiver, in place of setjmp/longjmp
2096 * rdchk(fdes) returns non 0 if a character is available
2097 */
2098 fflush (stdout);
2099 while (rdchk (io_mode_fd)) {
2100 #ifdef READCHECK_READS
2101 switch (checked)
2102 #else
2103 switch (READLINE_PF (1))
2104 #endif
2105 {
2106 case CAN:
2107 case ZPAD:
2108 c = getinsync (zi, 1);
2109 if (c == ZACK)
2110 break;
2111 /* zcrce - dinna wanna starta ping-pong game */
2112 ZSDATA (txbuf, 0, ZCRCE);
2113 goto gotack;
2114 case XOFF: /* Wait a while for an XON */
2115 case XOFF | 0200:
2116 READLINE_PF (100);
2117 default:
2118 ++junkcount;
2119 }
2120 }
2121 #endif /* READCHECK */
2122 if (Txwindow) {
2123 size_t tcount = 0;
2124 while ((tcount = zi->bytes_sent - Lrxpos) >= Txwindow) {
2125 vfile ("%ld (%ld,%ld) window >= %u", tcount,
2126 (long) zi->bytes_sent, (long) Lrxpos,
2127 Txwindow);
2128 if (e != ZCRCQ)
2129 ZSDATA (txbuf, 0, e = ZCRCQ);
2130 c = getinsync (zi, 1);
2131 if (c != ZACK) {
2132 ZSDATA (txbuf, 0, ZCRCE);
2133 goto gotack;
2134 }
2135 }
2136 vfile ("window = %ld", tcount);
2137 }
2138 } while (!zi->eof_seen);
2139
2140
2141 if (play_with_sigint)
2142 signal (SIGINT, SIG_IGN);
2143
2144 for (;;) {
2145 stohdr (zi->bytes_sent);
2146 zsbhdr (ZEOF, Txhdr);
2147 switch (getinsync (zi, 0)) {
2148 case ZACK:
2149 continue;
2150 case ZRPOS:
2151 goto somemore;
2152 case ZRINIT:
2153 return OK;
2154 case ZSKIP:
2155 if (input_f) {
2156 fclose (input_f);
2157 input_f=NULL;
2158 }
2159 DO_SYSLOG((LOG_INFO, "%s/%s: got ZSKIP",
2160 shortname, protname()));
2161 return c;
2162 default:
2163 if (input_f) {
2164 fclose (input_f);
2165 input_f=NULL;
2166 }
2167 DO_SYSLOG((LOG_INFO, "%s/%s: got %d",
2168 shortname, protname(), c));
2169 return ERROR;
2170 }
2171 }
2172 }
2173
2174 static int
2175 calc_blklen(long total_sent)
2176 {
2177 static long total_bytes=0;
2178 static int calcs_done=0;
2179 static long last_error_count=0;
2180 static int last_blklen=0;
2181 static long last_bytes_per_error=0;
2182 unsigned long best_bytes=0;
2183 long best_size=0;
2184 long this_bytes_per_error;
2185 long d;
2186 unsigned int i;
2187 if (total_bytes==0)
2188 {
2189 /* called from countem */
2190 total_bytes=total_sent;
2191 return 0;
2192 }
2193
2194 /* it's not good to calc blklen too early */
2195 if (calcs_done++ < 5) {
2196 if (error_count && start_blklen >1024)
2197 return last_blklen=1024;
2198 else
2199 last_blklen/=2;
2200 return last_blklen=start_blklen;
2201 }
2202
2203 if (!error_count) {
2204 /* that's fine */
2205 if (start_blklen==max_blklen)
2206 return start_blklen;
2207 this_bytes_per_error=LONG_MAX;
2208 goto calcit;
2209 }
2210
2211 if (error_count!=last_error_count) {
2212 /* the last block was bad. shorten blocks until one block is
2213 * ok. this is because very often many errors come in an
2214 * short period */
2215 if (error_count & 2)
2216 {
2217 last_blklen/=2;
2218 if (last_blklen < 32)
2219 last_blklen = 32;
2220 else if (last_blklen > 512)
2221 last_blklen=512;
2222 if (Verbose > 3)
2223 vstringf(_("calc_blklen: reduced to %d due to error\n"),
2224 last_blklen);
2225 }
2226 last_error_count=error_count;
2227 last_bytes_per_error=0; /* force recalc */
2228 return last_blklen;
2229 }
2230
2231 this_bytes_per_error=total_sent / error_count;
2232 /* we do not get told about every error, because
2233 * there may be more than one error per failed block.
2234 * but one the other hand some errors are reported more
2235 * than once: If a modem buffers more than one block we
2236 * get at least two ZRPOS for the same position in case
2237 * *one* block has to be resent.
2238 * so don't do this:
2239 * this_bytes_per_error/=2;
2240 */
2241 /* there has to be a margin */
2242 if (this_bytes_per_error<100)
2243 this_bytes_per_error=100;
2244
2245 /* be nice to the poor machine and do the complicated things not
2246 * too often
2247 */
2248 if (last_bytes_per_error>this_bytes_per_error)
2249 d=last_bytes_per_error-this_bytes_per_error;
2250 else
2251 d=this_bytes_per_error-last_bytes_per_error;
2252 if (d<4)
2253 {
2254 if (Verbose > 3)
2255 {
2256 vstringf(_("calc_blklen: returned old value %d due to low bpe diff\n"),
2257 last_blklen);
2258 vstringf(_("calc_blklen: old %ld, new %ld, d %ld\n"),
2259 last_bytes_per_error,this_bytes_per_error,d );
2260 }
2261 return last_blklen;
2262 }
2263 last_bytes_per_error=this_bytes_per_error;
2264
2265 calcit:
2266 if (Verbose > 3)
2267 vstringf(_("calc_blklen: calc total_bytes=%ld, bpe=%ld, ec=%ld\n"),
2268 total_bytes,this_bytes_per_error,(long) error_count);
2269 for (i=32;i<=max_blklen;i*=2) {
2270 long ok; /* some many ok blocks do we need */
2271 long failed; /* and that's the number of blocks not transmitted ok */
2272 unsigned long transmitted;
2273 ok=total_bytes / i + 1;
2274 failed=((long) i + OVERHEAD) * ok / this_bytes_per_error;
2275 transmitted=total_bytes + ok * OVERHEAD
2276 + failed * ((long) i+OVERHEAD+OVER_ERR);
2277 if (Verbose > 4)
2278 vstringf(_("calc_blklen: blklen %d, ok %ld, failed %ld -> %lu\n"),
2279 i,ok,failed,transmitted);
2280 if (transmitted < best_bytes || !best_bytes)
2281 {
2282 best_bytes=transmitted;
2283 best_size=i;
2284 }
2285 }
2286 if (best_size > 2*last_blklen)
2287 best_size=2*last_blklen;
2288 last_blklen=best_size;
2289 if (Verbose > 3)
2290 vstringf(_("calc_blklen: returned %d as best\n"),
2291 last_blklen);
2292 return last_blklen;
2293 }
2294
2295 /*
2296 * Respond to receiver's complaint, get back in sync with receiver
2297 */
2298 static int
2299 getinsync(struct zm_fileinfo *zi, int flag)
2300 {
2301 size_t rxpos;
2302
2303 for (;;) {
2304 int gotblock;
2305 gotblock = zgethdr(Rxhdr, 0, &rxpos);
2306 switch (gotblock) {
2307 case ZCAN:
2308 case ZABORT:
2309 case ZFIN:
2310 case TIMEOUT:
2311 return ERROR;
2312 case ZRPOS:
2313 /* ************************************* */
2314 /* If sending to a buffered modem, you */
2315 /* might send a break at this point to */
2316 /* dump the modem's buffer. */
2317 if (input_f)
2318 clearerr(input_f); /* In case file EOF seen */
2319 #ifdef HAVE_MMAP
2320 if (!mm_addr)
2321 #endif
2322 if (fseek(input_f, (long) rxpos, 0))
2323 return ERROR;
2324 zi->eof_seen = 0;
2325 bytcnt = Lrxpos = zi->bytes_sent = rxpos;
2326 if (Lastsync == rxpos) {
2327 error_count++;
2328 }
2329 Lastsync = rxpos;
2330 return ZRPOS;
2331 case ZACK:
2332 Lrxpos = rxpos;
2333 if (flag || zi->bytes_sent == rxpos)
2334 return ZACK;
2335 continue;
2336 case ZRINIT:
2337 case ZSKIP:
2338 if (input_f) {
2339 fclose (input_f);
2340 input_f=NULL;
2341 }
2342 #ifdef HAVE_MMAP
2343 else if (mm_addr) {
2344 munmap(mm_addr,mm_size);
2345 mm_addr=NULL;
2346 }
2347 #endif
2348 return ZSKIP;
2349 case ERROR:
2350 default:
2351 error_count++;
2352 zsbhdr(ZNAK, Txhdr);
2353 continue;
2354 }
2355 }
2356 }
2357
2358
2359 /* Say "bibi" to the receiver, try to do it cleanly */
2360 static void
2361 saybibi(void)
2362 {
2363 for (;;) {
2364 stohdr(0L); /* CAF Was zsbhdr - minor change */
2365 zshhdr(ZFIN, Txhdr); /* to make debugging easier */
2366 switch (zgethdr(Rxhdr, 0,NULL)) {
2367 case ZFIN:
2368 sendline('O');
2369 sendline('O');
2370 flushmo();
2371 case ZCAN:
2372 case TIMEOUT:
2373 return;
2374 }
2375 }
2376 }
2377
2378 /* Send command and related info */
2379 static int
2380 zsendcmd(const char *buf, size_t blen)
2381 {
2382 int c;
2383 pid_t cmdnum;
2384 size_t rxpos;
2385
2386 cmdnum = getpid();
2387 errors = 0;
2388 for (;;) {
2389 stohdr((size_t) cmdnum);
2390 Txhdr[ZF0] = Cmdack1;
2391 zsbhdr(ZCOMMAND, Txhdr);
2392 ZSDATA(buf, blen, ZCRCW);
2393 listen:
2394 Rxtimeout = 100; /* Ten second wait for resp. */
2395 c = zgethdr(Rxhdr, 1, &rxpos);
2396
2397 switch (c) {
2398 case ZRINIT:
2399 goto listen; /* CAF 8-21-87 */
2400 case ERROR:
2401 case TIMEOUT:
2402 if (++errors > Cmdtries)
2403 return ERROR;
2404 continue;
2405 case ZCAN:
2406 case ZABORT:
2407 case ZFIN:
2408 case ZSKIP:
2409 case ZRPOS:
2410 return ERROR;
2411 default:
2412 if (++errors > 20)
2413 return ERROR;
2414 continue;
2415 case ZCOMPL:
2416 Exitcode = rxpos;
2417 saybibi();
2418 return OK;
2419 case ZRQINIT:
2420 vfile("******** RZ *******");
2421 system("rz");
2422 vfile("******** SZ *******");
2423 goto listen;
2424 }
2425 }
2426 }
2427
2428 /*
2429 * If called as lsb use YMODEM protocol
2430 */
2431 static void
2432 chkinvok (const char *s)
2433 {
2434 const char *p;
2435
2436 p = s;
2437 while (*p == '-')
2438 s = ++p;
2439 while (*p)
2440 if (*p++ == '/')
2441 s = p;
2442 if (*s == 'v') {
2443 Verbose = 1;
2444 ++s;
2445 }
2446 program_name = s;
2447 if (*s == 'l')
2448 s++; /* lsz -> sz */
2449 protocol = ZM_ZMODEM;
2450 if (s[0] == 's' && s[1] == 'x')
2451 protocol = ZM_XMODEM;
2452 if (s[0] == 's' && (s[1] == 'b' || s[1] == 'y')) {
2453 protocol = ZM_YMODEM;
2454 }
2455 }
2456
2457 static void
2458 countem (int argc, char **argv)
2459 {
2460 struct stat f;
2461
2462 for (Totalleft = 0, Filesleft = 0; --argc >= 0; ++argv) {
2463 f.st_size = -1;
2464 if (Verbose > 2) {
2465 vstringf ("\nCountem: %03d %s ", argc, *argv);
2466 }
2467 if (access (*argv, R_OK) >= 0 && stat (*argv, &f) >= 0) {
2468 #if defined(S_ISDIR)
2469 if (!S_ISDIR(f.st_mode) && !S_ISBLK(f.st_mode)) {
2470 #else
2471 int c;
2472 c = f.st_mode & S_IFMT;
2473 if (c != S_IFDIR && c != S_IFBLK) {
2474 #endif
2475 ++Filesleft;
2476 Totalleft += f.st_size;
2477 }
2478 } else if (strcmp (*argv, "-") == 0) {
2479 ++Filesleft;
2480 Totalleft += DEFBYTL;
2481 }
2482 if (Verbose > 2)
2483 vstringf (" %ld", (long) f.st_size);
2484 }
2485 if (Verbose > 2)
2486 vstringf (_("\ncountem: Total %d %ld\n"),
2487 Filesleft, Totalleft);
2488 calc_blklen (Totalleft);
2489 }
2490
2491 /* End of lsz.c */
2492
2493