home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / comm / rzsz.zip / SZ.C < prev    next >
C/C++ Source or Header  |  1993-05-16  |  34KB  |  1,570 lines

  1. #define VERSION "3.24 5-16-93"
  2. #define PUBDIR "/usr/spool/uucppublic"
  3.  
  4. /*
  5.  **************************************************************************
  6.  *
  7.  * sz.c By Chuck Forsberg,  Omen Technology INC
  8.  *    Copyright 1993 Omen Technology Inc All Rights Reserved
  9.  *
  10.  * 
  11.  *    This version implements numerous enhancements including ZMODEM
  12.  *    Run Length Encoding and variable length headers.  These
  13.  *    features were not funded by the original Telenet development
  14.  *    contract.
  15.  * 
  16.  *  This software may be freely used for educational (didactic
  17.  *  only) purposes.  This software may also be freely used to
  18.  *  support file transfer operations to or from licensed Omen
  19.  *  Technology products.  Use with other commercial or shareware
  20.  *  programs (Crosstalk, Procomm, etc.) REQUIRES REGISTRATION.
  21.  *
  22.  *  Any programs which use part or all of this software must be
  23.  *  provided in source form with this notice intact except by
  24.  *  written permission from Omen Technology Incorporated.
  25.  *
  26.  * 
  27.  * Use of this software for commercial or administrative purposes
  28.  * except when exclusively limited to interfacing Omen Technology
  29.  * products requires a per port license payment of $20.00 US per
  30.  * port (less in quantity, see mailer.rz).  Use of this code by
  31.  * inclusion, decompilation, reverse engineering or any other means
  32.  * constitutes agreement to these conditions and acceptance of
  33.  * liability to license the materials and payment of reasonable
  34.  * legal costs necessary to enforce this license agreement.
  35.  *
  36.  *
  37.  *        Omen Technology Inc
  38.  *        Post Office Box 4681
  39.  *        Portland OR 97208
  40.  *
  41.  *    This code is made available in the hope it will be useful,
  42.  *    BUT WITHOUT ANY WARRANTY OF ANY KIND OR LIABILITY FOR ANY
  43.  *    DAMAGES OF ANY KIND.
  44.  *
  45.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  46.  */
  47.  
  48. char *Copyrsz = "Copyright 1993 Omen Technology Inc All Rights Reserved";
  49.  
  50. char *substr(), *getenv();
  51.  
  52. #define LOGFILE "/tmp/szlog"
  53. #define LOGFILE2 "szlog"
  54. #include <stdio.h>
  55. #include <signal.h>
  56. #include <ctype.h>
  57. #include <errno.h>
  58. extern int errno;
  59. #define STATIC
  60.  
  61. #define PATHLEN 256
  62. #define OK 0
  63. #define FALSE 0
  64. #ifdef TRUE
  65. #undef TRUE
  66. #endif
  67. #define TRUE 1
  68. #define ERROR (-1)
  69. /* Ward Christensen / CP/M parameters - Don't change these! */
  70. #define ENQ 005
  71. #define CAN ('X'&037)
  72. #define XOFF ('s'&037)
  73. #define XON ('q'&037)
  74. #define SOH 1
  75. #define STX 2
  76. #define EOT 4
  77. #define ACK 6
  78. #define NAK 025
  79. #define CPMEOF 032
  80. #define WANTCRC 0103    /* send C not NAK to get crc not checksum */
  81. #define WANTG 0107    /* Send G not NAK to get nonstop batch xmsn */
  82. #define TIMEOUT (-2)
  83. #define RCDO (-3)
  84. #define GCOUNT (-4)
  85. #define RETRYMAX 10
  86.  
  87.  
  88. #define HOWMANY 2
  89. STATIC int Zmodem=0;        /* ZMODEM protocol requested by receiver */
  90. unsigned Baudrate = 9600;        /* Default, set by first mode() call */
  91. STATIC unsigned Effbaud = 9600;
  92. STATIC unsigned Txwindow;    /* Control the size of the transmitted window */
  93. STATIC unsigned Txwspac;    /* Spacing between zcrcq requests */
  94. STATIC unsigned Txwcnt;    /* Counter used to space ack requests */
  95. STATIC unsigned long Lrxpos;    /* Receiver's last reported offset */
  96. STATIC int errors;
  97. char endmsg[80] = {0};    /* Possible message to display on exit */
  98.  
  99. #include "rbsb.c"    /* most of the system dependent stuff here */
  100.  
  101. #include "crctab.c"
  102.  
  103. STATIC int Filesleft;
  104. STATIC unsigned long Totalleft;
  105.  
  106. /*
  107.  * Attention string to be executed by receiver to interrupt streaming data
  108.  *  when an error is detected.  A pause (0336) may be needed before the
  109.  *  ^C (03) or after it.
  110.  */
  111. #ifdef READCHECK
  112. STATIC char Myattn[] = { 0 };
  113. #else
  114. #ifdef USG
  115. STATIC char Myattn[] = { 03, 0336, 0 };
  116. #endif
  117. #endif
  118.  
  119. FILE *in;
  120.  
  121. STATIC int Canseek = 1;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  122.  
  123. #ifdef TXBSIZE
  124. #define TXBMASK (TXBSIZE-1)
  125. STATIC char Txb[TXBSIZE];        /* Circular buffer for file reads */
  126. STATIC char *txbuf = Txb;        /* Pointer to current file segment */
  127. #else
  128. STATIC char txbuf[1024];
  129. #endif
  130. STATIC unsigned long vpos = 0;        /* Number of bytes read from file */
  131.  
  132. STATIC char Lastrx;
  133. STATIC char Crcflg;
  134. STATIC int Modem2=0;        /* XMODEM Protocol - don't send pathnames */
  135. STATIC int Restricted=0;    /* restricted; no /.. or ../ in filenames */
  136. STATIC int Fullname=0;        /* transmit full pathname */
  137. STATIC int Unlinkafter=0;    /* Unlink file after it is sent */
  138. STATIC int Dottoslash=0;    /* Change foo.bar.baz to foo/bar/baz */
  139. STATIC int firstsec;
  140. STATIC int errcnt=0;        /* number of files unreadable */
  141. STATIC int blklen=128;        /* length of transmitted records */
  142. STATIC int Optiong;        /* Let it rip no wait for sector ACK's */
  143. STATIC int Eofseen;        /* EOF seen on input set by zfilbuf */
  144. STATIC int BEofseen;        /* EOF seen on input set by fooseek */
  145. STATIC int Totsecs;        /* total number of sectors this file */
  146. STATIC int Filcnt=0;        /* count of number of files opened */
  147. STATIC unsigned Rxbuflen=16384;    /* Receiver's max buffer length */
  148. STATIC int Tframlen = 0;    /* Override for tx frame length */
  149. STATIC int blkopt=0;        /* Override value for zmodem blklen */
  150. STATIC int Rxflags = 0;
  151. STATIC unsigned long bytcnt;
  152. STATIC int Wantfcs32 = TRUE;    /* want to send 32 bit FCS */
  153. STATIC char Lzconv;    /* Local ZMODEM file conversion request */
  154. STATIC char Lzmanag;    /* Local ZMODEM file management request */
  155. STATIC int Lskipnocor;
  156. STATIC char Lztrans;
  157. STATIC int Command;        /* Send a command, then exit. */
  158. STATIC char *Cmdstr;        /* Pointer to the command string */
  159. STATIC int Cmdtries = 11;
  160. STATIC int Cmdack1;        /* Rx ACKs command, then do it */
  161. STATIC int Exitcode;
  162. STATIC int Test;        /* 1= Force receiver to send Attn, etc with qbf. */
  163.             /* 2= Character transparency test */
  164. STATIC char *qbf=
  165.  "The quick brown fox jumped over the lazy dog's back 1234567890\r\n";
  166. STATIC unsigned long Lastsync;    /* Last offset to which we got a ZRPOS */
  167. STATIC int Beenhereb4;        /* How many times we've been ZRPOS'd here */
  168. STATIC int Ksendstr;        /* 1= Send esc-?-3-4-l to remote kermit */
  169. STATIC char *ksendbuf = "\033[?34l";
  170.  
  171. STATIC jmp_buf tohere;        /* For the interrupt on RX timeout */
  172. STATIC jmp_buf intrjmp;    /* For the interrupt on RX CAN */
  173.  
  174.  
  175. /* called by signal interrupt or terminate to clean things up */
  176. void
  177. bibi(n)
  178. {
  179.     canit(); fflush(stdout); mode(0);
  180.     fprintf(stderr, "sz: caught signal %d; exiting\n", n);
  181.     if (n == SIGQUIT)
  182.         abort();
  183.     if (n == 99)
  184.         fprintf(stderr, "mode(2) in rbsb.c not implemented!!\n");
  185.     exit(3);
  186. }
  187.  
  188. /* Called when ZMODEM gets an interrupt (^X) */
  189. void
  190. onintr(c)
  191. {
  192.     signal(SIGINT, SIG_IGN);
  193.     longjmp(intrjmp, -1);
  194. }
  195.  
  196. STATIC int Zctlesc;    /* Encode control characters */
  197. STATIC int Nozmodem = 0;    /* If invoked as "sb" */
  198. STATIC char *Progname = "sz";
  199. STATIC int Zrwindow = 1400;    /* RX window size (controls garbage count) */
  200.  
  201. /*
  202.  * Log an error
  203.  */
  204. /*VARARGS1*/
  205. void
  206. zperr(s,p,u)
  207. char *s, *p, *u;
  208. {
  209.     if (Verbose <= 0)
  210.         return;
  211.     fprintf(stderr, "Retry %d: ", errors);
  212.     fprintf(stderr, s, p, u);
  213.     fprintf(stderr, "\n");
  214. }
  215.  
  216. #include "zm.c"
  217. #include "zmr.c"
  218.  
  219. main(argc, argv)
  220. char *argv[];
  221. {
  222.     register char *cp;
  223.     register npats;
  224.     int dm;
  225.     char **patts;
  226.  
  227.     if ((cp = getenv("ZNULLS")) && *cp)
  228.         Znulls = atoi(cp);
  229.     if ((cp=getenv("SHELL")) && (substr(cp, "rsh") || substr(cp, "rksh")))
  230.         Restricted=TRUE;
  231.     inittty();
  232.     chkinvok(argv[0]);
  233.  
  234.     Rxtimeout = 600;
  235.     npats=0;
  236.     if (argc<2)
  237.         usage();
  238.     while (--argc) {
  239.         cp = *++argv;
  240.         if (*cp++ == '-' && *cp) {
  241.             while ( *cp) {
  242.                 switch(*cp++) {
  243.                 case '\\':
  244.                      *cp = toupper(*cp);  continue;
  245.                 case '+':
  246.                     Lzmanag = ZMAPND; break;
  247. #ifdef CSTOPB
  248.                 case '2':
  249.                     Twostop = TRUE; break;
  250. #endif
  251.                 case 'a':
  252.                     if (Nozmodem || Modem2)
  253.                         usage;
  254.                     Lzconv = ZCNL;  break;
  255.                 case 'b':
  256.                     Lzconv = ZCBIN; break;
  257.                 case 'C':
  258.                     if (--argc < 1) {
  259.                         usage();
  260.                     }
  261.                     Cmdtries = atoi(*++argv);
  262.                     break;
  263.                 case 'c':
  264.                     Lzmanag = ZMCHNG;  break;
  265.                 case 'd':
  266.                     ++Dottoslash;
  267.                     /* **** FALL THROUGH TO **** */
  268.                 case 'f':
  269.                     Fullname=TRUE; break;
  270.                         case 'g' :
  271.                     Ksendstr = TRUE; break;
  272.                 case 'e':
  273.                     Zctlesc = 1; break;
  274.                 case 'k':
  275.                     blklen=1024; break;
  276.                 case 'L':
  277.                     if (--argc < 1) {
  278.                         usage();
  279.                     }
  280.                     blkopt = atoi(*++argv);
  281.                     if (blkopt<24 || blkopt>1024)
  282.                         usage();
  283.                     break;
  284.                 case 'l':
  285.                     if (--argc < 1) {
  286.                         usage();
  287.                     }
  288.                     Tframlen = atoi(*++argv);
  289.                     if (Tframlen<32 || Tframlen>1024)
  290.                         usage();
  291.                     break;
  292.                 case 'N':
  293.                     Lzmanag = ZMNEWL;  break;
  294.                 case 'n':
  295.                     Lzmanag = ZMNEW;  break;
  296.                 case 'o':
  297.                     Wantfcs32 = FALSE; break;
  298.                 case 'p':
  299.                     Lzmanag = ZMPROT;  break;
  300.                 case 'r':
  301.                     if (Lzconv == ZCRESUM)
  302.                         Lzmanag = (Lzmanag & ZMMASK) | ZMCRC;
  303.                     Lzconv = ZCRESUM; break;
  304.                 case 't':
  305.                     if (--argc < 1) {
  306.                         usage();
  307.                     }
  308.                     Rxtimeout = atoi(*++argv);
  309.                     if (Rxtimeout<10 || Rxtimeout>1000)
  310.                         usage();
  311.                     break;
  312.                 case 'T':
  313.                     chartest(1); chartest(2);
  314.                     mode(0);  exit(0);
  315.                 case 'u':
  316.                     ++Unlinkafter; break;
  317.                 case 'v':
  318.                     ++Verbose; break;
  319.                 case 'w':
  320.                     if (--argc < 1) {
  321.                         usage();
  322.                     }
  323.                     Txwindow = atoi(*++argv);
  324.                     if (Txwindow < 256)
  325.                         Txwindow = 256;
  326.                     Txwindow = (Txwindow/64) * 64;
  327.                     Txwspac = Txwindow/4;
  328.                     if (blkopt > Txwspac
  329.                      || (!blkopt && Txwspac < 1024))
  330.                         blkopt = Txwspac;
  331.                     break;
  332.                 case 'Y':
  333.                     Lskipnocor = TRUE;
  334.                     /* **** FALLL THROUGH TO **** */
  335.                 case 'y':
  336.                     Lzmanag = ZMCLOB; break;
  337.                 case 'Z':
  338.                 case 'z':
  339.                     Lztrans = ZTRLE;  break;
  340.                 default:
  341.                     usage();
  342.                 }
  343.             }
  344.         }
  345.         else if (Command) {
  346.             if (argc != 1) {
  347.                 usage();
  348.             }
  349.             Cmdstr = *argv;
  350.         }
  351.         else if ( !npats && argc>0) {
  352.             if (argv[0][0]) {
  353.                 npats=argc;
  354.                 patts=argv;
  355.             }
  356.         }
  357.     }
  358.     if (npats < 1 && !Command && !Test) 
  359.         usage();
  360.     if (Verbose) {
  361.         if (freopen(LOGFILE, "a", stderr)==NULL)
  362.             if (freopen(LOGFILE2, "a", stderr)==NULL) {
  363.                 printf("Can't open log file!");
  364.                 exit(2);
  365.             }
  366.         setbuf(stderr, NULL);
  367.     }
  368.     vfile("%s %s for %s\n", Progname, VERSION, OS);
  369.  
  370.     mode(3);
  371.  
  372.     if (signal(SIGINT, bibi) == SIG_IGN) {
  373.         signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
  374.     } else {
  375.         signal(SIGINT, bibi); signal(SIGKILL, bibi);
  376.     }
  377. #ifdef SIGQUIT
  378.     signal(SIGQUIT, SIG_IGN);
  379. #endif
  380. #ifdef SIGTERM
  381.     signal(SIGTERM, bibi);
  382. #endif
  383.  
  384.     countem(npats, patts);
  385.  
  386.     if (!Modem2 && !Nozmodem) {
  387.         if (Ksendstr)
  388.             printf(ksendbuf);
  389.         printf("rz\r");  fflush(stdout);
  390.         stohdr(0L);
  391.         if (Command)
  392.             Txhdr[ZF0] = ZCOMMAND;
  393.         zshhdr(4, ZRQINIT, Txhdr);
  394.     }
  395.     fflush(stdout);
  396.  
  397.  
  398.     if (Command) {
  399.         if (getzrxinit()) {
  400.             Exitcode=1; canit();
  401.         }
  402.         else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  403.             Exitcode=1; canit();
  404.         }
  405.     } else if (wcsend(npats, patts)==ERROR) {
  406.         Exitcode=1;
  407.         canit();
  408.     }
  409.     if (endmsg[0]) {
  410.         printf("  %s: %s\r\n", Progname, endmsg);
  411.         if (Verbose)
  412.             fprintf(stderr, "%s\r\n", endmsg);
  413.     }
  414.     printf("%s %s finished.\r\n", Progname, VERSION);
  415.     fflush(stdout);
  416.     mode(0);
  417.     if(errcnt || Exitcode)
  418.         exit(1);
  419.  
  420. #ifndef REGISTERED
  421.     /* Removing or disabling this code without registering is theft */
  422.     if (!Usevhdrs)  {
  423.         printf("\n\n\nPlease read the License Agreement in sz.doc\n");
  424.         fflush(stdout);
  425.         sleep(10);
  426.     }
  427. #endif
  428.     exit(0);
  429.     /*NOTREACHED*/
  430. }
  431.  
  432. /* Say "bibi" to the receiver, try to do it cleanly */
  433. void
  434. saybibi()
  435. {
  436.     for (;;) {
  437.         stohdr(0L);        /* CAF Was zsbhdr - minor change */
  438.         zshhdr(4, ZFIN, Txhdr);    /*  to make debugging easier */
  439.         switch (zgethdr(Rxhdr, 0)) {
  440.         case ZFIN:
  441.             sendline('O'); sendline('O'); flushmo();
  442.         case ZCAN:
  443.         case TIMEOUT:
  444.             return;
  445.         }
  446.     }
  447. }
  448.  
  449. wcsend(argc, argp)
  450. char *argp[];
  451. {
  452.     register n;
  453.  
  454.     Crcflg=FALSE;
  455.     firstsec=TRUE;
  456.     bytcnt = -1;
  457.     if (Nozmodem) {
  458.         printf("Start your YMODEM receive. ");  fflush(stdout);
  459.     }
  460.     for (n=0; n<argc; ++n) {
  461.         Totsecs = 0;
  462.         if (wcs(argp[n])==ERROR)
  463.             return ERROR;
  464.     }
  465.     Totsecs = 0;
  466.     if (Filcnt==0) {    /* bitch if we couldn't open ANY files */
  467.         if (!Nozmodem && !Modem2) {
  468.             Command = TRUE;
  469.             Cmdstr = "echo \"sz: Can't open any requested files\"";
  470.             if (getnak()) {
  471.                 Exitcode=1; canit();
  472.             }
  473.             if (!Zmodem)
  474.                 canit();
  475.             else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  476.                 Exitcode=1; canit();
  477.             }
  478.             Exitcode = 1; return OK;
  479.         }
  480.         canit();
  481.         sprintf(endmsg, "Can't open any requested files");
  482.         return ERROR;
  483.     }
  484.     if (Zmodem)
  485.         saybibi();
  486.     else if ( !Modem2)
  487.         wctxpn("");
  488.     return OK;
  489. }
  490.  
  491. wcs(oname)
  492. char *oname;
  493. {
  494.     register c;
  495.     register char *p, *q;
  496.     struct stat f;
  497.     char name[PATHLEN];
  498.  
  499.     strcpy(name, oname);
  500.  
  501.     if (Restricted) {
  502.         /* restrict pathnames to current tree or uucppublic */
  503.         if ( substr(name, "../")
  504.          || (name[0]== '/' && strncmp(name, PUBDIR, strlen(PUBDIR))) ) {
  505.             canit();  sprintf(endmsg,"Security Violation");
  506.             return ERROR;
  507.         }
  508.     }
  509.  
  510.     in=fopen(oname, "r");
  511.  
  512.     if (in==NULL) {
  513.         ++errcnt;
  514.         return OK;    /* pass over it, there may be others */
  515.     }
  516.     BEofseen = Eofseen = 0;  vpos = 0;
  517.  
  518.     /* Check for directory */
  519.     fstat(fileno(in), &f);
  520. #ifdef POSIX
  521.     if (S_ISDIR(f.st_mode))
  522. #else
  523.     c = f.st_mode & S_IFMT;
  524.     if (c == S_IFDIR || c == S_IFBLK)
  525. #endif
  526.     {
  527.         fclose(in);
  528.         return OK;
  529.     }
  530.  
  531.     ++Filcnt;
  532.     switch (wctxpn(name)) {
  533.     case ZSKIP:
  534.     case ZFERR:
  535.         return OK;
  536.     case OK:
  537.         break;
  538.     default:
  539.         return ERROR;
  540.     }
  541.     if (!Zmodem && wctx(f.st_size))
  542.         return ERROR;
  543.  
  544.     if (Unlinkafter)
  545.         unlink(oname);
  546.  
  547.     return 0;
  548. }
  549.  
  550. /*
  551.  * generate and transmit pathname block consisting of
  552.  *  pathname (null terminated),
  553.  *  file length, mode time and file mode in octal
  554.  *  as provided by the Unix fstat call.
  555.  *  N.B.: modifies the passed name, may extend it!
  556.  */
  557. wctxpn(name)
  558. char *name;
  559. {
  560.     register char *p, *q;
  561.     char name2[PATHLEN];
  562.     struct stat f;
  563.  
  564.     if (Modem2) {
  565.         if (*name && fstat(fileno(in), &f)!= -1) {
  566.             fprintf(stderr, "Sending %s, %ld XMODEM blocks. ",
  567.               name, (127+f.st_size)>>7);
  568.         }
  569.         fprintf(stderr, "Give your local XMODEM receive command now.\r\n");
  570.         fflush(stderr);
  571.         return OK;
  572.     }
  573.     zperr("Awaiting pathname nak for %s", *name?name:"<END>");
  574.     if ( !Zmodem)
  575.         if (getnak())
  576.             return ERROR;
  577.  
  578.     q = (char *) 0;
  579.     if (Dottoslash) {        /* change . to . */
  580.         for (p=name; *p; ++p) {
  581.             if (*p == '/')
  582.                 q = p;
  583.             else if (*p == '.')
  584.                 *(q=p) = '/';
  585.         }
  586.         if (q && strlen(++q) > 8) {    /* If name>8 chars */
  587.             q += 8;            /*   make it .ext */
  588.             strcpy(name2, q);    /* save excess of name */
  589.             *q = '.';
  590.             strcpy(++q, name2);    /* add it back */
  591.         }
  592.     }
  593.  
  594.     for (p=name, q=txbuf ; *p; )
  595.         if ((*q++ = *p++) == '/' && !Fullname)
  596.             q = txbuf;
  597.     *q++ = 0;
  598.     p=q;
  599.     while (q < (txbuf + 1024))
  600.         *q++ = 0;
  601.     if (*name) {
  602.         if (fstat(fileno(in), &f)!= -1)
  603.             sprintf(p, "%lu %lo %o 0 %d %ld", f.st_size, f.st_mtime,
  604.               f.st_mode, Filesleft, Totalleft);
  605.         Totalleft -= f.st_size;
  606.     }
  607.     if (--Filesleft <= 0)
  608.         Filesleft = Totalleft = 0;
  609.     if (Totalleft < 0)
  610.         Totalleft = 0;
  611.  
  612.     /* force 1k blocks if name won't fit in 128 byte block */
  613.     if (txbuf[125])
  614.         blklen=1024;
  615.     else {        /* A little goodie for IMP/KMD */
  616.         txbuf[127] = (f.st_size + 127) >>7;
  617.         txbuf[126] = (f.st_size + 127) >>15;
  618.     }
  619.     if (Zmodem)
  620.         return zsendfile(txbuf, 1+strlen(p)+(p-txbuf));
  621.     if (wcputsec(txbuf, 0, 128)==ERROR)
  622.         return ERROR;
  623.     return OK;
  624. }
  625.  
  626. getnak()
  627. {
  628.     register firstch;
  629.  
  630.     Lastrx = 0;
  631.     for (;;) {
  632.         switch (firstch = readline(800)) {
  633.         case ZPAD:
  634.             if (getzrxinit())
  635.                 return ERROR;
  636.             return FALSE;
  637.         case TIMEOUT:
  638.             sprintf(endmsg, "Timeout waiting for ZRINIT");
  639.             return TRUE;
  640.         case WANTG:
  641. #ifdef MODE2OK
  642.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  643. #endif
  644.             Optiong = TRUE;
  645.             blklen=1024;
  646.         case WANTCRC:
  647.             Crcflg = TRUE;
  648.         case NAK:
  649.             return FALSE;
  650.         case CAN:
  651.             if ((firstch = readline(20)) == CAN && Lastrx == CAN) {
  652.                 sprintf(endmsg, "Got CAN waiting to send file");
  653.                 return TRUE;
  654.             }
  655.         default:
  656.             break;
  657.         }
  658.         Lastrx = firstch;
  659.     }
  660. }
  661.  
  662.  
  663. wctx(flen)
  664. unsigned long flen;
  665. {
  666.     register int thisblklen;
  667.     register int sectnum, attempts, firstch;
  668.     unsigned long charssent;
  669.  
  670.     charssent = 0;  firstsec=TRUE;  thisblklen = blklen;
  671.     vfile("wctx:file length=%ld", flen);
  672.  
  673.     while ((firstch=readline(Rxtimeout))!=NAK && firstch != WANTCRC
  674.       && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
  675.         ;
  676.     if (firstch==CAN) {
  677.         zperr("Receiver CANcelled");
  678.         return ERROR;
  679.     }
  680.     if (firstch==WANTCRC)
  681.         Crcflg=TRUE;
  682.     if (firstch==WANTG)
  683.         Crcflg=TRUE;
  684.     sectnum=0;
  685.     for (;;) {
  686.         if (flen <= (charssent + 896L))
  687.             thisblklen = 128;
  688.         if ( !filbuf(txbuf, thisblklen))
  689.             break;
  690.         if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)
  691.             return ERROR;
  692.         charssent += thisblklen;
  693.     }
  694.     fclose(in);
  695.     attempts=0;
  696.     do {
  697.         purgeline();
  698.         sendline(EOT);
  699.         flushmo();
  700.         ++attempts;
  701.     }
  702.         while ((firstch=(readline(Rxtimeout)) != ACK) && attempts < RETRYMAX);
  703.     if (attempts == RETRYMAX) {
  704.         zperr("No ACK on EOT");
  705.         return ERROR;
  706.     }
  707.     else
  708.         return OK;
  709. }
  710.  
  711. wcputsec(buf, sectnum, cseclen)
  712. char *buf;
  713. int sectnum;
  714. int cseclen;    /* data length of this sector to send */
  715. {
  716.     register checksum, wcj;
  717.     register char *cp;
  718.     unsigned oldcrc;
  719.     int firstch;
  720.     int attempts;
  721.  
  722.     firstch=0;    /* part of logic to detect CAN CAN */
  723.  
  724.     if (Verbose>2)
  725.         fprintf(stderr, "Sector %3d %2dk\n", Totsecs, Totsecs/8 );
  726.     else if (Verbose>1)
  727.         fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 );
  728.     for (attempts=0; attempts <= RETRYMAX; attempts++) {
  729.         Lastrx= firstch;
  730.         sendline(cseclen==1024?STX:SOH);
  731.         sendline(sectnum);
  732.         sendline(-sectnum -1);
  733.         oldcrc=checksum=0;
  734.         for (wcj=cseclen,cp=buf; --wcj>=0; ) {
  735.             sendline(*cp);
  736.             oldcrc=updcrc((0377& *cp), oldcrc);
  737.             checksum += *cp++;
  738.         }
  739.         if (Crcflg) {
  740.             oldcrc=updcrc(0,updcrc(0,oldcrc));
  741.             sendline((int)oldcrc>>8);
  742.             sendline((int)oldcrc);
  743.         }
  744.         else
  745.             sendline(checksum);
  746.         flushmo();
  747.  
  748.         if (Optiong) {
  749.             firstsec = FALSE; return OK;
  750.         }
  751.         firstch = readline(Rxtimeout);
  752. gotnak:
  753.         switch (firstch) {
  754.         case CAN:
  755.             if(Lastrx == CAN) {
  756. cancan:
  757.                 zperr("Cancelled");  return ERROR;
  758.             }
  759.             break;
  760.         case TIMEOUT:
  761.             zperr("Timeout on sector ACK"); continue;
  762.         case WANTCRC:
  763.             if (firstsec)
  764.                 Crcflg = TRUE;
  765.         case NAK:
  766.             zperr("NAK on sector"); continue;
  767.         case ACK: 
  768.             firstsec=FALSE;
  769.             Totsecs += (cseclen>>7);
  770.             return OK;
  771.         case ERROR:
  772.             zperr("Got burst for sector ACK"); break;
  773.         default:
  774.             zperr("Got %02x for sector ACK", firstch); break;
  775.         }
  776.         for (;;) {
  777.             Lastrx = firstch;
  778.             if ((firstch = readline(Rxtimeout)) == TIMEOUT)
  779.                 break;
  780.             if (firstch == NAK || firstch == WANTCRC)
  781.                 goto gotnak;
  782.             if (firstch == CAN && Lastrx == CAN)
  783.                 goto cancan;
  784.         }
  785.     }
  786.     zperr("Retry Count Exceeded");
  787.     return ERROR;
  788. }
  789.  
  790. /* fill buf with count chars padding with ^Z for CPM */
  791. filbuf(buf, count)
  792. register char *buf;
  793. {
  794.     register c, m;
  795.  
  796.     m = read(fileno(in), buf, count);
  797.     if (m <= 0)
  798.         return 0;
  799.     while (m < count)
  800.         buf[m++] = 032;
  801.     return count;
  802. }
  803.  
  804. /* Fill buffer with blklen chars */
  805. zfilbuf()
  806. {
  807.     int n;
  808.  
  809. #ifdef TXBSIZE
  810.     vfile("zfilbuf: bytcnt =%lu vpos=%lu blklen=%d", bytcnt, vpos, blklen);
  811.     /* We assume request is within buffer, or just beyond */
  812.     txbuf = Txb + (bytcnt & TXBMASK);
  813.     if (vpos <= bytcnt) {
  814.         n = fread(txbuf, 1, blklen, in);
  815.  
  816.         vpos += n;
  817.         if (n < blklen)
  818.             Eofseen = 1;
  819.         vfile("zfilbuf: n=%d vpos=%lu Eofseen=%d", n, vpos, Eofseen);
  820.         return n;
  821.     }
  822.     if (vpos >= (bytcnt+blklen))
  823.         return blklen;
  824.     /* May be a short block if crash recovery etc. */
  825.     Eofseen = BEofseen;
  826.     return (vpos - bytcnt);
  827. #else
  828.     n = fread(txbuf, 1, blklen, in);
  829.     if (n < blklen)
  830.         Eofseen = 1;
  831.     return n;
  832. #endif
  833. }
  834.  
  835. #ifdef TXBSIZE
  836. /* Replacement for brain damaged fseek function.  Returns 0==success */
  837. fooseek(fptr, pos, whence)
  838. FILE *fptr;
  839. unsigned long pos;
  840. {
  841.     unsigned long m, n;
  842.  
  843.     vfile("fooseek: pos =%lu vpos=%lu Canseek=%d", pos, vpos, Canseek);
  844.     /* Seek offset < current buffer */
  845.     if (pos < (vpos -TXBSIZE +1024)) {
  846.         BEofseen = 0;
  847.         if (Canseek > 0) {
  848.             vpos = pos & ~TXBMASK;
  849.             if (vpos >= pos)
  850.                 vpos -= TXBSIZE;
  851.             if (fseek(fptr, vpos, 0))
  852.                 return 1;
  853.         }
  854.         else if (Canseek == 0) {
  855.             if (fseek(fptr, vpos = 0L, 0))
  856.                 return 1;
  857.         } else
  858.             return 1;
  859.         while (vpos < pos) {
  860.             n = fread(Txb, 1, TXBSIZE, fptr);
  861.             vpos += n;
  862.             vfile("n=%d vpos=%ld", n, vpos);
  863.             if (n < TXBSIZE) {
  864.                 BEofseen = 1;
  865.                 break;
  866.             }
  867.         }
  868.         vfile("vpos=%ld", vpos);
  869.         return 0;
  870.     }
  871.     /* Seek offset > current buffer (Crash Recovery, etc.) */
  872.     if (pos > vpos) {
  873.         if (Canseek)
  874.             if (fseek(fptr, vpos = (pos & ~TXBMASK), 0))
  875.                 return 1;
  876.         while (vpos <= pos) {
  877.             txbuf = Txb + (vpos & TXBMASK);
  878.             m = TXBSIZE - (vpos & TXBMASK);
  879.             vfile("m=%ld vpos=%ld", m,vpos);
  880.                 n = fread(txbuf, 1, m, fptr);
  881.             vfile("n=%ld vpos=%ld", n,vpos);
  882.             vpos += n;
  883.             vfile("bo=%d m=%ld vpos=%ld", txbuf-Txb,m,vpos);
  884.             if (n < m) {
  885.                 BEofseen = 1;
  886.                 break;
  887.             }
  888.         }
  889.         return 0;
  890.     }
  891.     /* Seek offset is within current buffer */
  892.     vfile("within buffer: vpos=%ld", vpos);
  893.     return 0;
  894. }
  895. #define fseek fooseek
  896. #endif
  897.  
  898.  
  899. /*
  900.  * substr(string, token) searches for token in string s
  901.  * returns pointer to token within string if found, NULL otherwise
  902.  */
  903. char *
  904. substr(s, t)
  905. register char *s,*t;
  906. {
  907.     register char *ss,*tt;
  908.     /* search for first char of token */
  909.     for (ss=s; *s; s++)
  910.         if (*s == *t)
  911.             /* compare token with substring */
  912.             for (ss=s,tt=t; ;) {
  913.                 if (*tt == 0)
  914.                     return s;
  915.                 if (*ss++ != *tt++)
  916.                     break;
  917.             }
  918.     return NULL;
  919. }
  920.  
  921. char *usinfo[] = {
  922.     "Send Files and Commands with ZMODEM/YMODEM/XMODEM Protocol\n",
  923.     "Usage:    sz [-2+abcdefgklLnNuvwyY] [-] file ...",
  924.     "\t    zcommand [-2Cegv] COMMAND",
  925.     "\t    zcommandi [-2Cegv] COMMAND",
  926.     "\t    sb [-2adfkuv] [-] file ...",
  927.     "\t    sx [-2akuv] [-] file",
  928.     ""
  929. };
  930.  
  931. usage()
  932. {
  933.     char **pp;
  934.  
  935.     fprintf(stderr, "\n%s %s for %s by Chuck Forsberg, Omen Technology INC\n",
  936.      Progname, VERSION, OS);
  937.     fprintf(stderr, "\t\t\042The High Reliability Software\042\n");
  938.     for (pp=usinfo; **pp; ++pp)
  939.         fprintf(stderr, "%s\n", *pp);
  940.     fprintf(stderr,"\nCopyright 1993 Omen Technology INC All Rights Reserved\n");
  941.     fprintf(stderr,
  942.      "See sz.doc for option descriptions and licensing information.\n\n");
  943.     exit(3);
  944. }
  945.  
  946. /*
  947.  * Get the receiver's init parameters
  948.  */
  949. getzrxinit()
  950. {
  951.     register n;
  952.     struct stat f;
  953.  
  954.     for (n=10; --n>=0; ) {
  955.         
  956.         switch (zgethdr(Rxhdr, 1)) {
  957.         case ZCHALLENGE:    /* Echo receiver's challenge numbr */
  958.             stohdr(Rxpos);
  959.             zshhdr(4, ZACK, Txhdr);
  960.             continue;
  961.         case ZCOMMAND:        /* They didn't see out ZRQINIT */
  962.             stohdr(0L);
  963.             zshhdr(4, ZRQINIT, Txhdr);
  964.             continue;
  965.         case ZRINIT:
  966.             Rxflags = 0377 & Rxhdr[ZF0];
  967.             Usevhdrs = Rxhdr[ZF1] & CANVHDR;
  968.             Txfcs32 = (Wantfcs32 && (Rxflags & CANFC32));
  969.             Zctlesc |= Rxflags & TESCCTL;
  970.             Rxbuflen = (0377 & Rxhdr[ZP0])+((0377 & Rxhdr[ZP1])<<8);
  971.             if ( !(Rxflags & CANFDX))
  972.                 Txwindow = 0;
  973.             vfile("Rxbuflen=%d Tframlen=%d", Rxbuflen, Tframlen);
  974.             signal(SIGINT, SIG_IGN);
  975. #ifdef MODE2OK
  976.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  977. #endif
  978.  
  979. #ifndef READCHECK
  980. #ifndef USG
  981.             /* Use 1024 byte frames if no sample/interrupt */
  982.             if (Rxbuflen < 32 || Rxbuflen > 1024) {
  983.                 Rxbuflen = 1024;
  984.                 vfile("Rxbuflen=%d", Rxbuflen);
  985.             }
  986. #endif
  987. #endif
  988.  
  989.             /* Override to force shorter frame length */
  990.             if (Rxbuflen && (Rxbuflen>Tframlen) && (Tframlen>=32))
  991.                 Rxbuflen = Tframlen;
  992.             if ( !Rxbuflen && (Tframlen>=32) && (Tframlen<=1024))
  993.                 Rxbuflen = Tframlen;
  994.             vfile("Rxbuflen=%d", Rxbuflen);
  995.  
  996.  
  997.             /*
  998.              * If input is not a regular file, force ACK's to
  999.              *  prevent running beyond the buffer limits
  1000.              */
  1001.             if ( !Command) {
  1002.                 fstat(fileno(in), &f);
  1003.                 if (
  1004. #ifdef POSIX
  1005.                     !S_ISREG(f.st_mode)
  1006. #else
  1007.                     (f.st_mode & S_IFMT) != S_IFREG
  1008. #endif
  1009.                     ) {
  1010.                     Canseek = -1;
  1011. #ifdef TXBSIZE
  1012.                     Txwindow = TXBSIZE - 1024;
  1013.                     Txwspac = TXBSIZE/4;
  1014. #else
  1015.                     return ERROR;
  1016. #endif
  1017.                 }
  1018.             }
  1019.  
  1020.             /* Set initial subpacket length */
  1021.             if (blklen < 1024) {    /* Command line override? */
  1022.                 if (Effbaud > 300)
  1023.                     blklen = 256;
  1024.                 if (Effbaud > 1200)
  1025.                     blklen = 512;
  1026.                 if (Effbaud > 2400)
  1027.                     blklen = 1024;
  1028.             }
  1029.             if (Rxbuflen && blklen>Rxbuflen)
  1030.                 blklen = Rxbuflen;
  1031.             if (blkopt && blklen > blkopt)
  1032.                 blklen = blkopt;
  1033.             vfile("Rxbuflen=%d blklen=%d", Rxbuflen, blklen);
  1034.             vfile("Txwindow = %u Txwspac = %d", Txwindow, Txwspac);
  1035.  
  1036.  
  1037.             if (Lztrans == ZTRLE && (Rxflags & CANRLE))
  1038.                 Txfcs32 = 2;
  1039.             else
  1040.                 Lztrans = 0;
  1041.  
  1042.             return (sendzsinit());
  1043.         case ZCAN:
  1044.         case TIMEOUT:
  1045.             return ERROR;
  1046.         case ZRQINIT:
  1047.             if (Rxhdr[ZF0] == ZCOMMAND)
  1048.                 continue;
  1049.         default:
  1050.             zshhdr(4, ZNAK, Txhdr);
  1051.             continue;
  1052.         }
  1053.     }
  1054.     return ERROR;
  1055. }
  1056.  
  1057. /* Send send-init information */
  1058. sendzsinit()
  1059. {
  1060.     register c;
  1061.  
  1062.     if (Myattn[0] == '\0' && (!Zctlesc || (Rxflags & TESCCTL)))
  1063.         return OK;
  1064.     errors = 0;
  1065.     for (;;) {
  1066.         stohdr(0L);
  1067. #ifdef ALTCANOFF
  1068.         Txhdr[ALTCOFF] = ALTCANOFF;
  1069. #endif
  1070.         if (Zctlesc) {
  1071.             Txhdr[ZF0] |= TESCCTL; zshhdr(4, ZSINIT, Txhdr);
  1072.         }
  1073.         else
  1074.             zsbhdr(4, ZSINIT, Txhdr);
  1075.         zsdata(Myattn, ZATTNLEN, ZCRCW);
  1076.         c = zgethdr(Rxhdr, 1);
  1077.         switch (c) {
  1078.         case ZCAN:
  1079.             return ERROR;
  1080.         case ZACK:
  1081.             return OK;
  1082.         default:
  1083.             if (++errors > 19)
  1084.                 return ERROR;
  1085.             continue;
  1086.         }
  1087.     }
  1088. }
  1089.  
  1090. /* Send file name and related info */
  1091. zsendfile(buf, blen)
  1092. char *buf;
  1093. {
  1094.     register c;
  1095.     register unsigned long crc;
  1096.     long lastcrcrq = -1;
  1097.     char *p;
  1098.  
  1099.     for (errors=0; ++errors<11;) {
  1100.         Txhdr[ZF0] = Lzconv;    /* file conversion request */
  1101.         Txhdr[ZF1] = Lzmanag;    /* file management request */
  1102.         if (Lskipnocor)
  1103.             Txhdr[ZF1] |= ZMSKNOLOC;
  1104.         Txhdr[ZF2] = Lztrans;    /* file transport request */
  1105.         Txhdr[ZF3] = 0;
  1106.         zsbhdr(4, ZFILE, Txhdr);
  1107.         zsdata(buf, blen, ZCRCW);
  1108. again:
  1109.         c = zgethdr(Rxhdr, 1);
  1110.         switch (c) {
  1111.         case ZRINIT:
  1112.             while ((c = readline(50)) > 0)
  1113.                 if (c == ZPAD) {
  1114.                     goto again;
  1115.                 }
  1116.             continue;
  1117.         case ZCAN:
  1118.         case TIMEOUT:
  1119.         case ZABORT:
  1120.         case ZFIN:
  1121.             sprintf(endmsg, "Got %s on pathname", frametypes[c+FTOFFSET]);
  1122.             return ERROR;
  1123.         default:
  1124.             sprintf(endmsg, "Got %d frame type on pathname", c);
  1125.             continue;
  1126.         case ERROR:
  1127.         case ZNAK:
  1128.             continue;
  1129.         case ZCRC:
  1130.             if (Rxpos != lastcrcrq) {
  1131.                 lastcrcrq = Rxpos;
  1132.                 crc = 0xFFFFFFFFL;
  1133.                 if (Canseek >= 0) {
  1134.                     fseek(in, 0L, 0);
  1135.                     while (((c = getc(in)) != EOF) && --lastcrcrq)
  1136.                         crc = UPDC32(c, crc);
  1137.                     crc = ~crc;
  1138.                     clearerr(in);    /* Clear possible EOF */
  1139.                     lastcrcrq = Rxpos;
  1140.                 }
  1141.             }
  1142.             stohdr(crc);
  1143.             zsbhdr(4, ZCRC, Txhdr);
  1144.             goto again;
  1145.         case ZFERR:
  1146.         case ZSKIP:
  1147.             sprintf(endmsg, "File skipped by receiver request");
  1148.             fclose(in); return c;
  1149.         case ZRPOS:
  1150.             /*
  1151.              * Suppress zcrcw request otherwise triggered by
  1152.              * lastyunc==bytcnt
  1153.              */
  1154.             if (fseek(in, Rxpos, 0))
  1155.                 return ERROR;
  1156.             Lastsync = (bytcnt = Txpos = Lrxpos = Rxpos) -1;
  1157.             return zsendfdata();
  1158.         }
  1159.     }
  1160.     fclose(in); return ERROR;
  1161. }
  1162.  
  1163. /* Send the data in the file */
  1164. zsendfdata()
  1165. {
  1166.     register c, e, n;
  1167.     register newcnt;
  1168.     register long tcount = 0;
  1169.     int junkcount;        /* Counts garbage chars received by TX */
  1170.     static int tleft = 6;    /* Counter for test mode */
  1171.  
  1172.     junkcount = 0;
  1173.     Beenhereb4 = FALSE;
  1174. somemore:
  1175.     if (setjmp(intrjmp)) {
  1176. waitack:
  1177.         junkcount = 0;
  1178.         c = getinsync(0);
  1179. gotack:
  1180.         switch (c) {
  1181.         default:
  1182.         case ZCAN:
  1183.             fclose(in);
  1184.             return ERROR;
  1185.         case ZRINIT:
  1186.             fclose(in);
  1187.             return ZSKIP;
  1188.         case ZSKIP:
  1189.             fclose(in);
  1190.             return c;
  1191.         case ZACK:
  1192.         case ZRPOS:
  1193.             break;
  1194.         }
  1195. #ifdef READCHECK
  1196.         /*
  1197.          * If the reverse channel can be tested for data,
  1198.          *  this logic may be used to detect error packets
  1199.          *  sent by the receiver, in place of setjmp/longjmp
  1200.          *  rdchk(Tty) returns non 0 if a character is available
  1201.          */
  1202.         while (rdchk(Tty)) {
  1203. #ifdef EATSIT
  1204.             switch (checked)
  1205. #else
  1206.             switch (readline(1))
  1207. #endif
  1208.             {
  1209.             case CAN:
  1210.             case ZPAD:
  1211.                 c = getinsync(1);
  1212.                 goto gotack;
  1213.             case XOFF:        /* Wait a while for an XON */
  1214.                 readline(100);
  1215.             }
  1216.         }
  1217. #endif
  1218.     }
  1219.  
  1220.     signal(SIGINT, onintr);
  1221.     newcnt = Rxbuflen;
  1222.     Txwcnt = 0;
  1223.     stohdr(Txpos);
  1224.     zsbhdr(4, ZDATA, Txhdr);
  1225.  
  1226.     /*
  1227.      * Special testing mode.  This should force receiver to Attn,ZRPOS
  1228.      *  many times.  Each time the signal should be caught, causing the
  1229.      *  file to be started over from the beginning.
  1230.      */
  1231.     if (Test) {
  1232.         if ( --tleft)
  1233.             while (tcount < 20000) {
  1234.                 printf(qbf); fflush(stdout);
  1235.                 tcount += strlen(qbf);
  1236. #ifdef READCHECK
  1237.                 while (rdchk(Tty)) {
  1238. #ifdef EATSIT
  1239.                     switch (checked)
  1240. #else
  1241.                     switch (readline(1))
  1242. #endif
  1243.                     {
  1244.                     case CAN:
  1245.                     case ZPAD:
  1246.                         goto waitack;
  1247.                     case XOFF:    /* Wait for XON */
  1248.                         readline(100);
  1249.                     }
  1250.                 }
  1251. #endif
  1252.             }
  1253.         signal(SIGINT, SIG_IGN); canit();
  1254.         sleep(3); purgeline(); mode(0);
  1255.         printf("\nsz: Tcount = %ld\n", tcount);
  1256.         if (tleft) {
  1257.             printf("ERROR: Interrupts Not Caught\n");
  1258.             exit(1);
  1259.         }
  1260.         exit(0);
  1261.     }
  1262.  
  1263.     do {
  1264.         n = zfilbuf();
  1265.         if (Eofseen)
  1266.             e = ZCRCE;
  1267.         else if (junkcount > 3)
  1268.             e = ZCRCW;
  1269.         else if (bytcnt == Lastsync)
  1270.             e = ZCRCW;
  1271.         else if (Rxbuflen && (newcnt -= n) <= 0)
  1272.             e = ZCRCW;
  1273.         else if (Txwindow && (Txwcnt += n) >= Txwspac) {
  1274.             Txwcnt = 0;  e = ZCRCQ;
  1275.         } else
  1276.             e = ZCRCG;
  1277.         if (Verbose>1)
  1278.             fprintf(stderr, "\r%7ld ZMODEM%s    ",
  1279.               Txpos, Crc32t?" CRC-32":"");
  1280.         zsdata(txbuf, n, e);
  1281.         bytcnt = Txpos += n;
  1282.         if (e == ZCRCW)
  1283.             goto waitack;
  1284. #ifdef READCHECK
  1285.         /*
  1286.          * If the reverse channel can be tested for data,
  1287.          *  this logic may be used to detect error packets
  1288.          *  sent by the receiver, in place of setjmp/longjmp
  1289.          *  rdchk(Tty) returns non 0 if a character is available
  1290.          */
  1291.         fflush(stdout);
  1292.         while (rdchk(Tty)) {
  1293. #ifdef EATSIT
  1294.             switch (checked)
  1295. #else
  1296.             switch (readline(1))
  1297. #endif
  1298.             {
  1299.             case CAN:
  1300.             case ZPAD:
  1301.                 c = getinsync(1);
  1302.                 if (c == ZACK)
  1303.                     break;
  1304.                 /* zcrce - dinna wanna starta ping-pong game */
  1305.                 zsdata(txbuf, 0, ZCRCE);
  1306.                 goto gotack;
  1307.             case XOFF:        /* Wait a while for an XON */
  1308.                 readline(100);
  1309.             default:
  1310.                 ++junkcount;
  1311.             }
  1312.         }
  1313. #endif    /* READCHECK */
  1314.         if (Txwindow) {
  1315.             while ((tcount = (Txpos - Lrxpos)) >= Txwindow) {
  1316.                 vfile("%ld window >= %u", tcount, Txwindow);
  1317.                 if (e != ZCRCQ)
  1318.                     zsdata(txbuf, 0, e = ZCRCQ);
  1319.                 c = getinsync(1);
  1320.                 if (c != ZACK) {
  1321.                     zsdata(txbuf, 0, ZCRCE);
  1322.                     goto gotack;
  1323.                 }
  1324.             }
  1325.             vfile("window = %ld", tcount);
  1326.         }
  1327.     } while (!Eofseen);
  1328.     signal(SIGINT, SIG_IGN);
  1329.  
  1330.     if (Lrxpos != Txpos)
  1331.         n = Unlinkafter | Txwindow;
  1332.     else
  1333.         n = 0;
  1334.     for (;;) {
  1335.         stohdr(Txpos);
  1336.         if (n) {
  1337.             zsbhdr(4, ZDATA, Txhdr);
  1338.             zsdata(txbuf, 0, ZCRCW);
  1339.         } else
  1340.             zsbhdr(4, ZEOF, Txhdr);
  1341.         switch (getinsync(0)) {
  1342.         case ZACK:
  1343.             n = 0;  continue;
  1344.         case ZNAK:
  1345.             continue;
  1346.         case ZRPOS:
  1347.             goto somemore;
  1348.         case ZRINIT:
  1349.             fclose(in);
  1350.             return OK;
  1351.         case ZSKIP:
  1352.             fclose(in);
  1353.             sprintf(endmsg, "File skipped by receiver request");
  1354.             return c;
  1355.         default:
  1356.             sprintf(endmsg, "Got %d trying to send end of file", c);
  1357.         case ERROR:
  1358.             fclose(in);
  1359.             return ERROR;
  1360.         }
  1361.     }
  1362. }
  1363.  
  1364. /*
  1365.  * Respond to receiver's complaint, get back in sync with receiver
  1366.  */
  1367. getinsync(flag)
  1368. {
  1369.     register c;
  1370.  
  1371.     for (;;) {
  1372.         if (Test) {
  1373.             printf("\r\n\n\n***** Signal Caught *****\r\n");
  1374.             Rxpos = 0; c = ZRPOS;
  1375.         } else
  1376.             c = zgethdr(Rxhdr, 0);
  1377.         switch (c) {
  1378.         case ZCAN:
  1379.         case ZABORT:
  1380.         case ZFIN:
  1381.         case TIMEOUT:
  1382.             sprintf(endmsg, "Got %s sending data", frametypes[c+FTOFFSET]);
  1383.             return ERROR;
  1384.         case ZRPOS:
  1385.             if (Rxpos > bytcnt) {
  1386.                 sprintf(endmsg, "Nonstandard Protocol");
  1387.                 return ZRPOS;
  1388.             }
  1389.             /* ************************************* */
  1390.             /*  If sending to a buffered modem, you  */
  1391.             /*   might send a break at this point to */
  1392.             /*   dump the modem's buffer.         */
  1393.             clearerr(in);    /* In case file EOF seen */
  1394.             if (fseek(in, Rxpos, 0)) {
  1395.                 sprintf(endmsg, "Bad Seek");
  1396.                 return ERROR;
  1397.             }
  1398.             Eofseen = 0;
  1399.             bytcnt = Lrxpos = Txpos = Rxpos;
  1400.             if (Lastsync == Rxpos) {
  1401.                 if (++Beenhereb4 > 12) {
  1402.                     sprintf(endmsg, "Can't send block");
  1403.                     return ERROR;
  1404.                 }
  1405.                 if (Beenhereb4 > 4)
  1406.                     if (blklen > 32)
  1407.                         blklen /= 2;
  1408.             }
  1409.             Lastsync = Rxpos;
  1410.             return c;
  1411.         case ZACK:
  1412.             Lrxpos = Rxpos;
  1413.             if (flag || Txpos == Rxpos)
  1414.                 return ZACK;
  1415.             continue;
  1416.         case ZRINIT:
  1417.             return c;
  1418.         case ZSKIP:
  1419.             sprintf(endmsg, "File skipped by receiver request");
  1420.             return c;
  1421.         case ERROR:
  1422.         default:
  1423.             zsbhdr(4, ZNAK, Txhdr);
  1424.             continue;
  1425.         }
  1426.     }
  1427. }
  1428.  
  1429.  
  1430. /* Send command and related info */
  1431. zsendcmd(buf, blen)
  1432. char *buf;
  1433. {
  1434.     register c;
  1435.     long cmdnum;
  1436.  
  1437.     cmdnum = getpid();
  1438.     errors = 0;
  1439.     for (;;) {
  1440.         stohdr(cmdnum);
  1441.         Txhdr[ZF0] = Cmdack1;
  1442.         zsbhdr(4, ZCOMMAND, Txhdr);
  1443.         zsdata(buf, blen, ZCRCW);
  1444. listen:
  1445.         Rxtimeout = 100;        /* Ten second wait for resp. */
  1446.         Usevhdrs = 0;        /* Allow rx to send fixed len headers */
  1447.         c = zgethdr(Rxhdr, 1);
  1448.  
  1449.         switch (c) {
  1450.         case ZRINIT:
  1451.             goto listen;    /* CAF 8-21-87 */
  1452.         case ERROR:
  1453.         case GCOUNT:
  1454.         case TIMEOUT:
  1455.             if (++errors > Cmdtries)
  1456.                 return ERROR;
  1457.             continue;
  1458.         case ZCAN:
  1459.         case ZABORT:
  1460.         case ZFIN:
  1461.         case ZSKIP:
  1462.         case ZRPOS:
  1463.             return ERROR;
  1464.         default:
  1465.             if (++errors > 20)
  1466.                 return ERROR;
  1467.             continue;
  1468.         case ZCOMPL:
  1469.             Exitcode = Rxpos;
  1470.             saybibi();
  1471.             return OK;
  1472.         case ZRQINIT:
  1473.             vfile("******** RZ *******");
  1474.             system("rz");
  1475.             vfile("******** SZ *******");
  1476.             goto listen;
  1477.         }
  1478.     }
  1479. }
  1480.  
  1481. /*
  1482.  * If called as sb use YMODEM protocol
  1483.  */
  1484. chkinvok(s)
  1485. char *s;
  1486. {
  1487.     register char *p;
  1488.  
  1489.     p = s;
  1490.     while (*p == '-')
  1491.         s = ++p;
  1492.     while (*p)
  1493.         if (*p++ == '/')
  1494.             s = p;
  1495.     if (*s == 'v') {
  1496.         Verbose=1; ++s;
  1497.     }
  1498.     Progname = s;
  1499.     if (s[0]=='z' && s[1] == 'c') {
  1500.         Command = TRUE;
  1501.         if (s[8] == 'i')
  1502.             Cmdack1 = ZCACK1;
  1503.     }
  1504.     if (s[0]=='s' && s[1]=='b') {
  1505.         Nozmodem = TRUE; blklen=1024;
  1506.     }
  1507.     if (s[0]=='s' && s[1]=='x') {
  1508.         Modem2 = TRUE;
  1509.     }
  1510. }
  1511.  
  1512. countem(argc, argv)
  1513. register char **argv;
  1514. {
  1515.     register c;
  1516.     struct stat f;
  1517.  
  1518.     for (Totalleft = 0, Filesleft = 0; --argc >=0; ++argv) {
  1519.         f.st_size = -1;
  1520.         if (Verbose>2) {
  1521.             fprintf(stderr, "\nCountem: %03d %s ", argc, *argv);
  1522.             fflush(stderr);
  1523.         }
  1524.         if (access(*argv, 04) >= 0 && stat(*argv, &f) >= 0) {
  1525.             ++Filesleft;  Totalleft += f.st_size;
  1526.         }
  1527.         if (Verbose>2)
  1528.             fprintf(stderr, " %ld", f.st_size);
  1529.     }
  1530.     if (Verbose>2)
  1531.         fprintf(stderr, "\ncountem: Total %d %ld\n",
  1532.           Filesleft, Totalleft);
  1533. }
  1534.  
  1535. chartest(m)
  1536. {
  1537.     register n;
  1538.  
  1539.     mode(m);
  1540.     printf("\r\n\nCharacter Transparency Test Mode %d\r\n", m);
  1541.     printf("If Pro-YAM/ZCOMM is not displaying ^M hit ALT-V NOW.\r\n");
  1542.     printf("Hit Enter.\021");  fflush(stdout);
  1543.     readline(500);
  1544.  
  1545.     for (n = 0; n < 256; ++n) {
  1546.         if (!(n%8))
  1547.             printf("\r\n");
  1548.         printf("%02x ", n);  fflush(stdout);
  1549.         sendline(n);    flushmo();
  1550.         printf("  ");  fflush(stdout);
  1551.         if (n == 127) {
  1552.             printf("Hit Enter.\021");  fflush(stdout);
  1553.             readline(500);
  1554.             printf("\r\n");  fflush(stdout);
  1555.         }
  1556.     }
  1557.     printf("\021\r\nEnter Characters, echo is in hex.\r\n");
  1558.     printf("Hit SPACE or pause 40 seconds for exit.\r\n");
  1559.  
  1560.     while (n != TIMEOUT && n != ' ') {
  1561.         n = readline(400);
  1562.         printf("%02x\r\n", n);
  1563.         fflush(stdout);
  1564.     }
  1565.     printf("\r\nMode %d character transparency test ends.\r\n", m);
  1566.     fflush(stdout);
  1567. }
  1568.  
  1569. /* End of sz.c */
  1570.