Category : UNIX Files
Archive   : U-XMODEM.ZIP
Filename : XR.C

 
Output of file : XR.C contained in archive : U-XMODEM.ZIP

/* xr.c - remote xmodem functions for xenix/unix
* copyright 1986 Maple Lawn Farm, Inc.
* usage: xr|xt [-ct] [-d errfile] file
* -c crc (instead of checksum), -t text mode (CR-NL <-> NL)
* compile: cc -i -O -s xr.c xmodem.c -o xr
* ln xr xt
* To avoid overwriting existing files, received file with same name
* as an existing file is stored as fname~. */

/* Modifications - Michael Grasso - Techni-Soft.
03/14/89 - MAG - Added pusage() to print usage information.
Used BSIZE for all block size references.
*/

#include
#include
#include
#include
#include
#include
#include

#define DEBUG 01
#define LF 02
#define CRC 04
#define BSIZE 128
#define errx(m,f) printf("%s: ", pname), \
printf(m, f), \
printf("\n"), \
pusage(), \
exit(1)

FILE *errf;

struct termio old,
new;

hangup()
{
resetline();
exit(1);
}

main(ac, av)
int ac;
char **av;
{
char *pname,
trans = (av[0][strlen(av[0]) -1] == 't'),
*fname;
int c, opts = 0;
struct stat stbuf;
extern int optind;
extern char *optarg;
FILE *fp;

pname = *av;
while ((c = getopt(ac, av, "ctd:?")) != EOF)
switch (c) {
case 't' :
opts |= LF;
break;
case 'c' :
opts |= CRC;
break;
case 'd' :
opts |= DEBUG;
if (!(errf = fopen(optarg, "w")))
errx("can't open %s", optarg);
setbuf(errf, NULL);
break;
case '?' :
pusage();
exit(1);
}
if (ac == 1 || ac == optind)
errx("need file name", NULL);
fname = av[optind];
if (trans && !(fp = fopen(fname, "r")))
errx("can't open %s", fname);
if (!trans) {
if (!access(fname, 0))
strcat(fname, "~");
if (!(fp = fopen(fname, "w")))
errx("can't write %s", fname);
}

printf("Ready to %s %s\n", (trans) ? "send":"receive", fname);
if (trans) {
stat(fname, &stbuf);
printf("%d blocks (%d bytes/block)\n",
stbuf.st_size/BSIZE+1, BSIZE);
}
printf("Ctrl-X to abort transfer\n");

signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGHUP, hangup);

ioctl(1, TCGETA, &old);
ioctl(1, TCGETA, &new);
fflush(stdin);
new.c_iflag = IGNBRK|IGNPAR;
new.c_oflag = 0;
new.c_lflag = 0;
new.c_cc[4] = 1;
new.c_cflag &= ~PARENB;
new.c_cflag |= CS8;
ioctl(1, TCSETAW, &new);

(trans) ? xput(fp, opts) : xget(fp, opts);
}


resetline()
{
ioctl(1, TCSETA, &old);
}

pusage()
{
printf("usage: xr|xt [-ct] [-d errfile] file\n");
printf(" xr = xmodem receive\n");
printf(" xt = xmodem transmit\n");
printf(" c = use crc (instead of checksum)\n");
printf(" t = text mode (CR-NL <-> NL)\n");
printf(" d = send results to error file\n");
}