/*
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>
#ifndef R_OK
# define R_OK 4
#endif
#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MMAP)
# include <sys/mman.h>
size_t mm_size;
void *mm_addr=NULL;
#else
# undef HAVE_MMAP
#endif
#include "timing.h"
#include "long-options.h"
#include "xstrtoul.h"
#include "error.h"
#ifndef STRICT_PROTOTYPES
extern time_t time();
extern char *strerror();
extern char *strstr();
#endif
#ifndef HAVE_ERRNO_DECLARATION
extern int errno;
#endif
unsigned Baudrate=2400; /* Default, should be set by first mode() call */
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 __P ((struct zm_fileinfo *zi, const char *buf, size_t blen));
static int getnak __P ((void));
static int wctxpn __P ((struct zm_fileinfo *));
static int wcs __P ((const char *oname, const char *remotename));
static size_t zfilbuf __P ((struct zm_fileinfo *zi));
static size_t filbuf __P ((char *buf, size_t count));
static int getzrxinit __P ((void));
static int calc_blklen __P ((long total_sent));
static int sendzsinit __P ((void));
static int wctx __P ((struct zm_fileinfo *));
static int zsendfdata __P ((struct zm_fileinfo *));
static int getinsync __P ((struct zm_fileinfo *, int flag));
static void countem __P ((int argc, char **argv));
static void chkinvok __P ((const char *s));
static void usage __P ((int exitcode, const char *what));
static int zsendcmd __P ((const char *buf, size_t blen));
static void saybibi __P ((void));
static int wcsend __P ((int argc, char *argp[]));
static int wcputsec __P ((char *buf, int sectnum, size_t cseclen));
static void usage1 __P ((int exitcode));
#ifdef ENABLE_SYSLOG
#define DO_SYSLOG(message) do { \
if (enable_syslog) { \
const char *shortname; \
if (!zi->fname) \
shortname="no.name"; \
else { \
shortname=strrchr(zi->fname,'/'); \
if (!shortname) \
shortname=zi->fname; \
else \
shortname++; \
} \
lsyslog message ; \
} \
} while(0)
#else
#define DO_SYSLOG(message) do { } while(0)
#endif
#define ZSDATA(x,y,z) \
do { if (Crc32t) {zsda32(x,y,z); } else {zsdata(x,y,z);}} while(0)
#ifdef HAVE_MMAP
#define DATAADR (mm_addr ? ((char *)mm_addr)+zi->bytes_sent : txbuf)
#else
#define DATAADR (txbuf)
#endif
int Filesleft;
long Totalleft;
size_t buffersize=16384;
#ifdef HAVE_MMAP
int use_mmap=1;
#endif
/*
* 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[MAX_BLOCK];
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 Dottoslash=0; /* Change foo.bar.baz to foo/bar/baz */
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 command_mode; /* Send a command, then exit. */
int Cmdtries = 11;
int Cmdack1; /* Rx ACKs command, then do it */
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 tcp_flag=0;
char *tcp_server_address=0;
int tcp_socket=-1;
int error_count;
#define OVERHEAD 18
#define OVER_ERR 20
#define MK_STRING(x) #x
#ifdef ENABLE_SYSLOG
# if defined(ENABLE_SYSLOG_FORCE) || defined(ENABLE_SYSLOG_DEFAULT)
int enable_syslog=TRUE;
# else
int enable_syslog=FALSE;
# endif
#endif
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 */
RETSIGTYPE
bibi (int n)
{
canit(STDOUT_FILENO);
fflush (stdout);
io_mode (io_mode_fd,0);
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 RETSIGTYPE
onintr(int n LRZSZ_ATTRIB_UNUSED)
{
signal(SIGINT, SIG_IGN);
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'},
{"cmdtries", required_argument, NULL, 'C'},
{"command", required_argument, NULL, 'c'},
{"immediate-command", required_argument, NULL, 'i'},
{"dot-to-slash", no_argument, NULL, 'd'},
{"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'},
{"mi
评论4
最新资源