home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / rzsz_3_24_3_src.lzh / sz.c < prev    next >
Text File  |  1993-12-24  |  41KB  |  1,898 lines

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