home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource3
/
110_01
/
umodem27.c
< prev
next >
Wrap
Text File
|
1984-03-04
|
31KB
|
1,025 lines
#
/*
* UMODEM -- Implements the "CP/M User's Group XMODEM" protocol and
* the TERM II File Transfer Protocol (FTP) Number 1 for
* packetized file up/downloading.
*
* Note: UNIX System-Dependent values are indicated by the string [SD]
* in a comment field one the same line as the values.
*
* -- Lauren Weinstein, 6/81
* -- (Version 2.0) Modified for JHU/UNIX by Richard Conn, 8/1/81
* -- Version 2.1 Mods by Richard Conn, 8/2/81
* . File Size Included on Send Option
* -- Version 2.2 Mods by Richard Conn, 8/2/81
* . Log File Generation and Option Incorporated
* -- Version 2.3 Mods by Richard Conn, 8/3/81
* . TERM II FTP 1 Supported
* . Error Log Reports Enhanced
* . CAN Function Added to FTP 3
* . 'd' Option Added to Delete umodem.log File before starting
* -- Version 2.4 Mods by Richard Conn, 8/4/81
* . 16K-extent sector number check error corrected
* . Count of number of received sectors added
* -- Version 2.5 Mods by Richard Conn, 8/5/81
* . ARPA Net Flag added
* . ARPA Net parameter ('a') added to command line
* . ARPA Net BIS, BIE, BOS, BOE added
* . ARPA Net FFH escape added
* -- Version 2.6 Mods by Bennett Marks, 8/21/81 (Bucky @ CCA-UNIX)
* . mods for UNIX V7 (Note: for JHU compilation define
* the variable JHU during 'cc'
* . added 'mungmode' flag to protect from inadvertant
* overwrite on file receive
* . changed timeout handling prior to issuing checksum
* -- Version 2.7 Mods by Richard Conn, 8/25/81 (rconn @ BRL)
* . correct minor "ifndef" error in which ifndef had no arg
* . restructured "ifdef" references so that other versions
* of UNIX than Version 7 and JHU can be easily incorporated;
* previous ifdef references were for JHU/not JHU;
* to compile under Version 7 or JHU UNIX, the following
* command lines are recommended:
* "cc -7 umodem.c -o umodem -DVER7" for Version 7
* "cc -7 umodem.c -o umodem -DJHU" for JHU
* . added 'y' file status display option; this option gives
* the user an estimate of the size of the target file to
* send from the UNIX system in terms of CP/M records (128
* bytes) and Kbytes (1024 byte units)
* . added '7' option which modifies the transmission protocols
* for 7 significant bits rather than 8; modifies both FTP 1
* and FTP 3
*
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
/* JHU UNIX tty parameter file */
#ifdef JHU
#include <stty.h>
#endif
/* Version 7 UNIX tty parameter file */
#ifdef VER7
#include <sgtty.h>
#endif
#include <signal.h>
#define VERSION 27 /* Version Number */
#define TRUE 1
#define FALSE 0
#define SOH 001
#define STX 002
#define ETX 003
#define EOT 004
#define ENQ 005
#define ACK 006
#define LF 012 /* Unix LF/NL */
#define CR 015
#define NAK 025
#define SYN 026
#define CAN 030
#define ESC 033
#define CTRLZ 032 /* CP/M EOF for text (usually!) */
#define TIMEOUT -1
#define ERRORMAX 10 /* maximum errors tolerated */
#define RETRYMAX 10 /* maximum retries to be made */
#define BBUFSIZ 128 /* buffer size -- do not change! */
#define CREATMODE 0644 /* mode for created files */
/* ARPA Net Constants */
#define IAC 0377
#define DO 0375
#define DONT 0376
#define WILL 0373
#define WONT 0374
#define TRBIN 0
/* JHU UNIX structures */
#ifdef JHU
struct sttybuf ttys, ttysnew, ttystemp; /* for stty terminal mode calls */
#endif
/* Version 7 UNIX structures */
#ifdef VER7
struct sgttyb ttys, ttysnew, ttystemp; /* for stty terminal mode calls */
#endif
struct stat statbuf; /* for terminal message on/off control */
FILE *LOGFP, *fopen();
char buff[BBUFSIZ];
#ifdef JHU
int wason;
#endif
#ifdef VER7
int pagelen;
#endif
char *tty;
char XMITTYPE;
int ARPA, RECVFLAG, SENDFLAG, FTP1, PMSG, DELFLAG, LOGFLAG, MUNGMODE;
int STATDISP, BIT7, BITMASK;
int delay;
alarmfunc();
main(argc, argv)
int argc;
char **argv;
{
char *logfile;
int index;
char flag;
logfile = "umodem.log"; /* Name of LOG File */
printf("\nUMODEM Version %d.%d", VERSION/10, VERSION%10);
printf(" -- UNIX-Based Remote File Transfer Facility\n");
if (argc < 3 || *argv[1] != '-')
{ printf("\nUsage: \n\tumodem ");
printf("-[rb!rt!sb!st][p][l][1][a][m][d][y][7]");
printf(" filename\n");
printf("\n");
printf("\n\trb <-- Receive Binary");
printf("\n\trt <-- Receive Text");
printf("\n\tsb <-- Send Binary");
printf("\n\tst <-- Send Text");
printf("\n\tp <-- Turn ON Parameter Display");
printf("\n\tl <-- (ell) Turn OFF LOG File Entries");
printf("\n\t1 <-- (one) Employ TERM II FTP 1");
printf("\n\ta <-- Turn ON ARPA Net Flag");
printf("\n\tm <-- Allow file overwiting on receive");
printf("\n\td <-- Delete umodem.log File before starting");
printf("\n\ty <-- Display file status (size) information only");
printf("\n\t7 <-- Enable 7-bit transfer mask");
printf("\n");
exit(-1);
}
index = 1; /* set index for loop */
delay = 3; /* assume FTP 3 delay */
PMSG = FALSE; /* turn off flags */
FTP1 = FALSE; /* assume FTP 3 (CP/M UG XMODEM2) */
RECVFLAG = FALSE; /* not receive */
SENDFLAG = FALSE; /* not send either */
XMITTYPE = 't'; /* assume text */
DELFLAG = FALSE; /* do NOT delete log file before starting */
LOGFLAG = TRUE; /* assume log messages */
ARPA = FALSE; /* assume not on ARPA Net */
MUNGMODE = FALSE; /* protect files from overwriting */
STATDISP = FALSE; /* assume not a status display */
BIT7 = FALSE; /* assume 8-bit communication */
while ((flag = argv[1][index++]) != '\0')
switch (flag) {
case 'a' : ARPA = TRUE; /* set ARPA Net */
break;
case 'p' : PMSG = TRUE; /* print all messages */
break;
case '1' : FTP1 = TRUE; /* select FTP 1 */
delay = 5; /* FTP 1 delay constant */
printf("\nUMODEM: TERM II FTP 1 Selected\n");
break;
case 'd' : DELFLAG = TRUE; /* delete log file first */
break;
case 'l' : LOGFLAG = FALSE; /* turn off log report */
break;
case 'r' : RECVFLAG = TRUE; /* receive file */
XMITTYPE = gettype(argv[1][index++]); /* get t/b */
break;
case 's' : SENDFLAG = TRUE; /* send file */
XMITTYPE = gettype(argv[1][index++]);
break;
case 'm' : MUNGMODE = TRUE; /* allow overwriting of files */
break;
case 'y' : STATDISP = TRUE; /* display file status */
break;
case '7' : BIT7 = TRUE; /* transfer only 7 bits */
break;
default : error("Invalid Flag", FALSE);
}
if (BIT7 && (XMITTYPE == 'b'))
{ printf("\nUMODEM: Fatal Error -- Both 7-Bit Transfer and ");
printf("Binary Transfer Selected");
exit(-1); /* error exit to UNIX */
}
if (BIT7) /* set MASK value */
BITMASK = 0177; /* 7 significant bits */
else
BITMASK = 0377; /* 8 significant bits */
if (PMSG)
{ printf("\nSupported File Transfer Protocols:");
printf("\n\tTERM II FTP 1");
printf("\n\tCP/M UG XMODEM2 (TERM II FTP 3)");
printf("\n\n");
}
if (LOGFLAG)
{ if (!DELFLAG)
LOGFP = fopen(logfile, "a"); /* append to LOG file */
else
LOGFP = fopen(logfile, "w"); /* new LOG file */
fprintf(LOGFP,"\n\n++++++++\n");
fprintf(LOGFP,"\nUMODEM Version %d.%d\n", VERSION/10, VERSION%10);
printf("\nUMODEM: LOG File '%s' is Open\n", logfile);
}
if (STATDISP) yfile(argv[2]); /* status of a file */
if (RECVFLAG && SENDFLAG)
error("Both Send and Receive Functions Specified", FALSE);
if (!RECVFLAG && !SENDFLAG)
error("Neither Send nor Receive Functions Specified", FALSE);
if (RECVFLAG)
{ if(open(argv[2], 0) != -1) /* possible abort if file exists */
{ printf("\nUMODEM: Warning -- Target File Exists\n");
if( MUNGMODE == FALSE )
error