home *** CD-ROM | disk | FTP | other *** search
/ Share Gallery 1 / share_gal_1.zip / share_gal_1 / CO / CO004.ZIP / SZ.ZIP / SZ.C < prev    next >
Text File  |  1990-01-08  |  33KB  |  1,489 lines

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