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 >
Text File  |  1984-03-04  |  31KB  |  1,025 lines

  1. #
  2.  
  3. /*
  4.  *  UMODEM -- Implements the "CP/M User's Group XMODEM" protocol and 
  5.  *          the TERM II File Transfer Protocol (FTP) Number 1 for
  6.  *            packetized file up/downloading.    
  7.  *
  8.  *    Note: UNIX System-Dependent values are indicated by the string [SD]
  9.  *          in a comment field one the same line as the values.
  10.  *
  11.  *         -- Lauren Weinstein, 6/81
  12.  *       -- (Version 2.0) Modified for JHU/UNIX by Richard Conn, 8/1/81
  13.  *       -- Version 2.1 Mods by Richard Conn, 8/2/81
  14.  *        . File Size Included on Send Option
  15.  *       -- Version 2.2 Mods by Richard Conn, 8/2/81
  16.  *        . Log File Generation and Option Incorporated
  17.  *       -- Version 2.3 Mods by Richard Conn, 8/3/81
  18.  *        . TERM II FTP 1 Supported
  19.  *        . Error Log Reports Enhanced
  20.  *        . CAN Function Added to FTP 3
  21.  *        . 'd' Option Added to Delete umodem.log File before starting
  22.  *       -- Version 2.4 Mods by Richard Conn, 8/4/81
  23.  *        . 16K-extent sector number check error corrected
  24.  *        . Count of number of received sectors added
  25.  *       -- Version 2.5 Mods by Richard Conn, 8/5/81
  26.  *        . ARPA Net Flag added
  27.  *        . ARPA Net parameter ('a') added to command line
  28.  *        . ARPA Net BIS, BIE, BOS, BOE added
  29.  *        . ARPA Net FFH escape added
  30.  *       -- Version 2.6 Mods by Bennett Marks, 8/21/81 (Bucky @ CCA-UNIX)
  31.  *        . mods for UNIX V7 (Note: for JHU compilation define
  32.  *          the variable JHU  during 'cc'
  33.  *        . added 'mungmode' flag to protect from inadvertant
  34.  *          overwrite on file receive
  35.  *        . changed timeout handling prior to issuing checksum
  36.  *       -- Version 2.7 Mods by Richard Conn, 8/25/81 (rconn @ BRL)
  37.  *        . correct minor "ifndef" error in which ifndef had no arg
  38.  *        . restructured "ifdef" references so that other versions
  39.  *          of UNIX than Version 7 and JHU can be easily incorporated;
  40.  *          previous ifdef references were for JHU/not JHU;
  41.  *          to compile under Version 7 or JHU UNIX, the following
  42.  *          command lines are recommended:
  43.  *            "cc -7 umodem.c -o umodem -DVER7" for Version 7
  44.  *            "cc -7 umodem.c -o umodem -DJHU" for JHU
  45.  *        . added 'y' file status display option; this option gives
  46.  *          the user an estimate of the size of the target file to
  47.  *          send from the UNIX system in terms of CP/M records (128
  48.  *          bytes) and Kbytes (1024 byte units)
  49.  *        . added '7' option which modifies the transmission protocols
  50.  *          for 7 significant bits rather than 8; modifies both FTP 1
  51.  *          and FTP 3
  52.  *
  53.  */
  54.  
  55. #include <stdio.h>
  56. #include <sys/types.h>
  57. #include <sys/stat.h>
  58.  
  59. /*  JHU UNIX tty parameter file  */
  60. #ifdef JHU
  61. #include <stty.h>
  62. #endif
  63.  
  64. /*  Version 7 UNIX tty parameter file  */
  65. #ifdef VER7
  66. #include <sgtty.h>
  67. #endif
  68.  
  69. #include <signal.h>
  70.  
  71. #define         VERSION    27    /* Version Number */
  72. #define         TRUE    1        
  73. #define      FALSE      0
  74. #define      SOH      001 
  75. #define         STX    002
  76. #define         ETX    003
  77. #define      EOT    004
  78. #define         ENQ    005
  79. #define      ACK      006
  80. #define         LF        012   /* Unix LF/NL */
  81. #define         CR        015  
  82. #define      NAK      025
  83. #define         SYN    026
  84. #define         CAN    030
  85. #define         ESC    033
  86. #define         CTRLZ    032   /* CP/M EOF for text (usually!) */
  87. #define      TIMEOUT      -1
  88. #define      ERRORMAX      10    /* maximum errors tolerated */
  89. #define      RETRYMAX      10    /* maximum retries to be made */
  90. #define         BBUFSIZ    128   /* buffer size -- do not change! */
  91. #define      CREATMODE    0644  /* mode for created files */
  92.  
  93. /*  ARPA Net Constants  */
  94. #define         IAC    0377
  95. #define         DO        0375
  96. #define         DONT    0376
  97. #define         WILL    0373
  98. #define         WONT    0374
  99. #define         TRBIN    0
  100.  
  101. /*  JHU UNIX structures  */
  102. #ifdef JHU
  103. struct sttybuf ttys, ttysnew, ttystemp;    /* for stty terminal mode calls */
  104. #endif
  105.  
  106. /*  Version 7 UNIX structures  */
  107. #ifdef VER7
  108. struct sgttyb  ttys, ttysnew, ttystemp;    /* for stty terminal mode calls */
  109. #endif
  110.  
  111. struct stat statbuf;      /* for terminal message on/off control */
  112. FILE *LOGFP, *fopen();
  113. char buff[BBUFSIZ];
  114.  
  115. #ifdef JHU
  116. int wason;
  117. #endif
  118.  
  119. #ifdef VER7
  120. int pagelen;
  121. #endif
  122.  
  123. char *tty;
  124. char XMITTYPE;
  125. int ARPA, RECVFLAG, SENDFLAG, FTP1, PMSG, DELFLAG, LOGFLAG, MUNGMODE;
  126. int STATDISP, BIT7, BITMASK;
  127. int delay;
  128.  
  129. alarmfunc();
  130.  
  131. main(argc, argv)
  132. int argc;
  133. char **argv;
  134. {
  135.     char *logfile;
  136.     int index;
  137.     char flag;
  138.  
  139.     logfile = "umodem.log";  /* Name of LOG File */
  140.  
  141.     printf("\nUMODEM Version %d.%d", VERSION/10, VERSION%10);
  142.     printf(" -- UNIX-Based Remote File Transfer Facility\n");
  143.  
  144.     if (argc < 3 || *argv[1] != '-')
  145.     {  printf("\nUsage:  \n\tumodem ");
  146.         printf("-[rb!rt!sb!st][p][l][1][a][m][d][y][7]");
  147.         printf(" filename\n");
  148.        printf("\n");
  149.        printf("\n\trb <-- Receive Binary");
  150.        printf("\n\trt <-- Receive Text");
  151.        printf("\n\tsb <-- Send Binary");
  152.        printf("\n\tst <-- Send Text");
  153.        printf("\n\tp  <-- Turn ON Parameter Display");
  154.        printf("\n\tl  <-- (ell) Turn OFF LOG File Entries");
  155.        printf("\n\t1  <-- (one) Employ TERM II FTP 1");
  156.        printf("\n\ta  <-- Turn ON ARPA Net Flag");
  157.        printf("\n\tm  <-- Allow file overwiting on receive");
  158.        printf("\n\td  <-- Delete umodem.log File before starting");
  159.        printf("\n\ty  <-- Display file status (size) information only");
  160.        printf("\n\t7  <-- Enable 7-bit transfer mask");
  161.        printf("\n");
  162.         exit(-1);
  163.     }
  164.  
  165.     index = 1;  /* set index for loop */
  166.     delay = 3;  /* assume FTP 3 delay */
  167.     PMSG = FALSE;  /* turn off flags */
  168.     FTP1 = FALSE;  /* assume FTP 3 (CP/M UG XMODEM2) */
  169.     RECVFLAG = FALSE;  /* not receive */
  170.     SENDFLAG = FALSE;  /* not send either */
  171.     XMITTYPE = 't';  /* assume text */
  172.     DELFLAG = FALSE;  /* do NOT delete log file before starting */
  173.     LOGFLAG = TRUE;  /* assume log messages */
  174.     ARPA = FALSE;  /* assume not on ARPA Net */
  175.     MUNGMODE = FALSE; /* protect files from overwriting */
  176.     STATDISP = FALSE;  /* assume not a status display */
  177.     BIT7 = FALSE;  /* assume 8-bit communication */
  178.     while ((flag = argv[1][index++]) != '\0')
  179.         switch (flag) {
  180.         case 'a' : ARPA = TRUE;  /* set ARPA Net */
  181.                break;
  182.         case 'p' : PMSG = TRUE;  /* print all messages */
  183.                break;
  184.         case '1' : FTP1 = TRUE;  /* select FTP 1 */
  185.                delay = 5;  /* FTP 1 delay constant */
  186.                printf("\nUMODEM:  TERM II FTP 1 Selected\n");
  187.                break;
  188.         case 'd' : DELFLAG = TRUE;  /* delete log file first */
  189.                break;
  190.         case 'l' : LOGFLAG = FALSE;  /* turn off log report */
  191.                break;
  192.         case 'r' : RECVFLAG = TRUE;  /* receive file */
  193.                XMITTYPE = gettype(argv[1][index++]);  /* get t/b */
  194.                break;
  195.         case 's' : SENDFLAG = TRUE;  /* send file */
  196.                XMITTYPE = gettype(argv[1][index++]);
  197.                break;
  198.         case 'm' : MUNGMODE = TRUE; /* allow overwriting of files */
  199.                break;
  200.         case 'y' : STATDISP = TRUE;  /* display file status */
  201.                break;
  202.         case '7' : BIT7 = TRUE;  /* transfer only 7 bits */
  203.                break;
  204.         default  : error("Invalid Flag", FALSE);
  205.         }
  206.  
  207.     if (BIT7 && (XMITTYPE == 'b'))
  208.     {  printf("\nUMODEM:  Fatal Error -- Both 7-Bit Transfer and ");
  209.        printf("Binary Transfer Selected");
  210.        exit(-1);  /* error exit to UNIX */
  211.     }
  212.  
  213.     if (BIT7)  /* set MASK value */
  214.        BITMASK = 0177;  /* 7 significant bits */
  215.     else
  216.        BITMASK = 0377;  /* 8 significant bits */
  217.  
  218.     if (PMSG)
  219.        { printf("\nSupported File Transfer Protocols:");
  220.          printf("\n\tTERM II FTP 1");
  221.          printf("\n\tCP/M UG XMODEM2 (TERM II FTP 3)");
  222.          printf("\n\n");
  223.        }
  224.  
  225.     if (LOGFLAG)
  226.        { if (!DELFLAG)
  227.         LOGFP = fopen(logfile, "a");  /* append to LOG file */
  228.          else
  229.         LOGFP = fopen(logfile, "w");  /* new LOG file */
  230.          fprintf(LOGFP,"\n\n++++++++\n");
  231.          fprintf(LOGFP,"\nUMODEM Version %d.%d\n", VERSION/10, VERSION%10);
  232.          printf("\nUMODEM:  LOG File '%s' is Open\n", logfile);
  233.        }
  234.  
  235.     if (STATDISP) yfile(argv[2]);  /* status of a file */
  236.  
  237.     if (RECVFLAG && SENDFLAG)
  238.         error("Both Send and Receive Functions Specified", FALSE);
  239.     if (!RECVFLAG && !SENDFLAG)
  240.         error("Neither Send nor Receive Functions Specified", FALSE);
  241.  
  242.     if (RECVFLAG)
  243.     {  if(open(argv[2], 0) != -1)  /* possible abort if file exists */
  244.        {    printf("\nUMODEM:  Warning -- Target File Exists\n");
  245.         if( MUNGMODE == FALSE )
  246.             error