home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc190.zip / ckcfns.c < prev    next >
C/C++ Source or Header  |  1994-10-01  |  86KB  |  2,575 lines

  1. char *fnsv = "C-Kermit functions, 5A(120) 18 Sep 94";
  2.  
  3. /*  C K C F N S  --  System-independent Kermit protocol support functions.  */
  4.  
  5. /*  ...Part 1 (others moved to ckcfn2,3 to make this module smaller) */
  6.  
  7. /*
  8.   Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  9.   Columbia University Academic Information Systems, New York City.
  10.  
  11.   Copyright (C) 1985, 1994, Trustees of Columbia University in the City of New
  12.   York.  The C-Kermit software may not be, in whole or in part, licensed or
  13.   sold for profit as a software product itself, nor may it be included in or
  14.   distributed with commercial products or otherwise distributed by commercial
  15.   concerns to their clients or customers without written permission of the
  16.   Office of Kermit Development and Distribution, Columbia University.  This
  17.   copyright notice must not be removed, altered, or obscured.
  18. */
  19. /*
  20.  System-dependent primitives defined in:
  21.  
  22.    ck?tio.c -- terminal i/o
  23.    cx?fio.c -- file i/o, directory structure
  24. */
  25. #include "ckcsym.h"            /* Needed for Stratus VOS */
  26. #include "ckcasc.h"            /* ASCII symbols */
  27. #include "ckcdeb.h"            /* Debug formats, typedefs, etc. */
  28. #include "ckcker.h"            /* Symbol definitions for Kermit */
  29. #include "ckcxla.h"            /* Character set symbols */
  30.  
  31. /* Externals from ckcmai.c */
  32. extern int spsiz, spmax, rpsiz, timint, srvtim, rtimo, npad, ebq, ebqflg,
  33.  rpt, rptq, rptflg, capas, keep, fncact, pkttim, autopar, spsizr, xitsta;
  34. extern int pktnum, bctr, bctu, bctl, fmask, clfils, sbufnum,
  35.  size, osize, spktl, nfils, warn, timef, spsizf, sndtyp, rcvtyp, success;
  36. extern int parity, turn, network, what, whatru, fsecs,
  37.  delay, displa, xflg, mypadn;
  38. extern long filcnt, ffc, flci, flco, tlci, tlco, tfc, fsize, sendstart, rs_len;
  39. extern long filrej, oldcps, cps ;
  40. extern int fblksiz, frecl, frecfm, forg, fcctrl;
  41. extern int spackets, rpackets, timeouts, retrans, crunched, wmax, wcur;
  42. extern int hcflg, binary, savmod, fncnv, local, server, cxseen, czseen;
  43. extern int nakstate, discard, rejection;
  44. extern int rq, rqf, sq, wslots, wslotn, wslotr, winlo, urpsiz, rln;
  45. extern int fnspath, fnrpath;
  46. extern int atcapr, atcapb, atcapu;
  47. extern int lpcapr, lpcapb, lpcapu;
  48. extern int swcapr, swcapb, swcapu;
  49. extern int lscapr, lscapb, lscapu;
  50. extern int rscapr, rscapb, rscapu;
  51. extern int bsave, bsavef, rptena, rptmin;
  52. extern int sseqtbl[];
  53. extern int numerrs;
  54. extern long rptn;
  55. extern int maxtry;
  56. extern int stdouf;
  57. extern int sendmode;
  58. #ifdef OS2
  59. extern struct zattr iattr ;
  60. #endif /* OS2 */
  61.  
  62. #ifndef NOCSETS
  63. extern int tcharset, fcharset;
  64. extern int ntcsets;
  65. extern struct csinfo tcsinfo[], fcsinfo[];
  66. #endif /* NOCSETS */
  67.  
  68. extern int
  69.   atenci, atenco, atdati, atdato, atleni, atleno, atblki, atblko,
  70.   attypi, attypo, atsidi, atsido, atsysi, atsyso, atdisi, atdiso; 
  71.  
  72. extern int bigsbsiz, bigrbsiz;
  73. extern char *versio;
  74.  
  75. #ifdef DYNAMIC
  76.   extern CHAR *srvcmd;
  77. #else
  78.   extern CHAR srvcmd[];
  79. #endif /* DYNAMIC */
  80. extern CHAR padch, mypadc, eol, seol, ctlq, myctlq, sstate, myrptq;
  81. extern CHAR *data, padbuf[], stchr, mystch;
  82. extern CHAR *srvptr;
  83. extern CHAR *rdatap;
  84. extern char *cmarg, *cmarg2, *hlptxt, **cmlist, filnam[], fspec[];
  85.  
  86. _PROTOTYP( CHAR *rpar, (void) );
  87. _PROTOTYP( int lslook, (unsigned int b) );    /* Locking Shift Lookahead */
  88. _PROTOTYP( int szeof, (CHAR *s) );
  89.  
  90. /* International character sets */
  91.  
  92. #ifndef NOCSETS
  93. /* Pointers to translation functions */
  94. #ifdef CK_ANSIC
  95. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Character set */
  96. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* translation functions */
  97. #else
  98. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])();    /* Character set */
  99. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])();    /* translation functions. */
  100. #endif /* CK_ANSIC */
  101. _PROTOTYP( CHAR (*rx), (CHAR) );    /* Input translation function */
  102. _PROTOTYP( CHAR (*sx), (CHAR) );    /* Output translation function */
  103. _PROTOTYP( CHAR ident, (CHAR) );    /* Identity translation function */
  104. #endif /* NOCSETS */
  105.  
  106. /* Windowing things */
  107.  
  108. extern int rseqtbl[];            /* Rec'd-packet sequence # table */
  109.  
  110. /* (PWP) external def. of things used in buffered file input and output */
  111.  
  112. #ifdef DYNAMIC
  113. extern char *zinbuffer, *zoutbuffer;
  114. #else
  115. extern char zinbuffer[], zoutbuffer[];
  116. #endif
  117. extern char *zinptr, *zoutptr;
  118. extern int zincnt, zoutcnt;
  119.  
  120. /*
  121.   Variables defined in this module but shared by other modules.
  122. */
  123.  
  124. char * rf_err = "Error receiving file";    /* rcvfil() error message */
  125.  
  126. #ifdef CK_SPEED
  127. short ctlp[256] = {        /* Control-Prefix table */
  128.   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* C0  */
  129.   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* G0  */
  130.   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  131.   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, /* DEL */
  132.   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* C1  */
  133.   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* G1  */
  134.   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  135.   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1  /* 255 */
  136. };
  137. #endif /* CK_SPEED */
  138.  
  139. int sndsrc;        /* Flag for where to get names of files to send: */
  140.                     /* -1: znext() function */
  141.                     /*  0: stdin */
  142.                     /* >0: list in cmlist */
  143.  
  144. int  memstr;                /* Flag for input from memory string */
  145.  
  146. #ifdef pdp11
  147. CHAR myinit[25];            /* Copy of my Send-Init data */
  148. #else
  149. CHAR myinit[100];            /* Copy of my Send-Init data */
  150. #endif /* pdp11 */
  151.  
  152. /* Variables local to this module */
  153.  
  154. #ifdef TLOG
  155. static char *fncnam[] = { 
  156.   "rename", "replace", "backup", "append", "discard", "ask", "update", ""
  157. };
  158. #endif /* TLOG */
  159.  
  160. static char *memptr;            /* Pointer for memory strings */
  161.  
  162. #ifdef pdp11
  163. static char cmdstr[50];            /* System command string. */
  164. #else
  165. static char cmdstr[256];
  166. #endif /* pdp11 */
  167.  
  168. static int drain;            /* For draining stacked-up ACKs. */
  169.  
  170. static int first;            /* Flag for first char from input */
  171. static CHAR t,                /* Current character */
  172.     next;                /* Next character */
  173.  
  174. static int ebqsent = 0;            /* 8th-bit prefix bid that I sent */
  175. static int lsstate = 0;            /* Locking shift state */
  176. static int lsquote = 0;            /* Locking shift quote */
  177.  
  178. #ifdef datageneral
  179. extern int quiet;
  180. #endif
  181.  
  182. /*  E N C S T R  --  Encode a string from memory. */
  183.  
  184. /*
  185.   Call this instead of getpkt() if source is a string, rather than a file.
  186.   Note: Character set translation is never done in this case.
  187. */
  188.  
  189. #ifdef COMMENT
  190. #define ENCBUFL 200
  191. #ifndef pdp11
  192. CHAR encbuf[ENCBUFL];
  193. #else
  194. /* This is gross, but the pdp11 root segment is out of space */
  195. /* Will allocate it in ckuusr.c. */
  196. extern CHAR encbuf[];
  197. #endif /* pdp11 */
  198. #endif /* COMMENT */
  199.  
  200. /*
  201.   Encode packet data from a string in memory rather than from a file.
  202.   Returns 0 on success, -1 if the string could not be completely encoded
  203.   into the currently negotiated data field length.
  204. */
  205. int
  206. encstr(s) CHAR* s; {
  207. #ifdef COMMENT
  208.     int m; char *p;
  209.     CHAR *dsave;
  210.  
  211.     if (!s) s = (CHAR *)"";
  212.     if ((m = (int)strlen((char *)s)) > ENCBUFL) {
  213.     debug(F111,"encstr string too long for buffer",s,ENCBUFL);
  214.     s[ENCBUFL] = '\0';
  215.     }
  216.     if (m > spsiz-bctl-3) {
  217.     debug(F111,"encstr string too long for packet",s,spsiz-bctl-3);
  218.     s[spsiz-bctl-3] = '\0';
  219.     }
  220.     m = memstr; p = memptr;        /* Save these. */
  221.  
  222.     memptr = (char *)s;            /* Point to the string. */
  223.     memstr = 1;                /* Flag memory string as source. */
  224.     first = 1;                /* Initialize character lookahead. */
  225.     dsave = data;            /* Boy is this ugly... */
  226.     data = encbuf + 7;            /* Why + 7?  See spack()... */
  227.     *data = NUL;            /* In case s is empty */
  228.     getpkt(spsiz,0);        /* Fill a packet from the string. */
  229.     data = dsave;            /* (sorry...) */
  230.     memstr = m;                /* Restore memory string flag */
  231.     memptr = p;                /* and pointer */
  232.     first = 1;                /* Put this back as we found it. */
  233.     return(0);
  234. #else
  235. /*
  236.   Recoded 30 Jul 94 to use the regular data buffer and the negotiated
  237.   maximum packet size.  Previously we were limited to the length of encbuf[].
  238.   Also, to return a failure code if the entire encoded string would not fit.
  239. */
  240.     int m, rc, slen; char *p;
  241.     if (!s) s = (CHAR *)"";        /* Watch out for null pointers. */
  242.     slen = strlen((char *)s);        /* Length of source string. */
  243.     rc = 0;                /* Return code. */
  244.     m = memstr; p = memptr;        /* Save these. */
  245.     memptr = (char *)s;            /* Point to the string. */
  246.     memstr = 1;                /* Flag memory string as source. */
  247.     first = 1;                /* Initialize character lookahead. */
  248.     *data = NUL;            /* In case s is empty */
  249.     getpkt(spsiz,0);            /* Fill a packet from the string. */
  250.  
  251.  
  252.     if (
  253. #ifdef COMMENT
  254.     *memptr
  255. #else
  256.     memptr < (char *)(s + slen)
  257. #endif /* COMMENT */
  258.     ) {                /* This means we didn't encode */
  259.     rc = -1;            /* the whole string. */
  260.     debug(F101,"encstr string too big","",size);
  261.     } else
  262.       debug(F101,"encstr fits OK, size","",size);
  263.     memstr = m;                /* Restore memory string flag */
  264.     memptr = p;                /* and pointer */
  265.     first = 1;                /* Put this back as we found it. */
  266.     return(rc);
  267. #endif /* COMMENT */
  268. }
  269.  
  270. #ifdef COMMENT
  271. /*
  272.   We don't use this routine any more -- the code has been incorporated
  273.   directly into getpkt() to reduce per-character function call overhead.
  274.   Also, watch out: it hasn't been updated since it was commented out a
  275.   long time ago.
  276. */
  277. /* E N C O D E - Kermit packet encoding procedure */
  278.  
  279. VOID
  280. encode(a) CHAR a; {            /* The current character */
  281.     int a7;                /* Low order 7 bits of character */
  282.     int b8;                /* 8th bit of character */
  283.  
  284. #ifndef NOCSETS
  285.     if (!binary && sx) a = (*sx)(a);    /* Translate. */
  286. #endif /* NOCSETS */
  287.  
  288.     if (rptflg)    {                   /* Repeat processing? */
  289.         if (a == next && (first == 0)) { /* Got a run... */
  290.         if (++rpt < 94)        /* Below max, just count */
  291.                 return;
  292.         else if (rpt == 94) {    /* Reached max, must dump */
  293.                 data[size++] = rptq;
  294.                 data[size++] = tochar(rpt);
  295.         rptn += rpt;        /* Count, for stats */
  296.                 rpt = 0;
  297.         }
  298.         } else if (rpt == 1) {        /* Run broken, only 2? */
  299.             rpt = 0;            /* Yes, reset repeat flag & count. */
  300.         encode(a);            /* Do the character twice. */
  301.         if (size <= maxsize) osize = size;
  302.         rpt = 0;
  303.         encode(a);
  304.         return;
  305.     } else if (rpt > 1) {        /* More than two */
  306.             data[size++] = rptq;    /* Insert the repeat prefix */
  307.             data[size++] = tochar(++rpt); /* and count. */
  308.         rptn += rpt;
  309.             rpt = 0;            /* Reset repeat counter. */
  310.         }
  311.     }
  312.     a7 = a & 0177;            /* Isolate ASCII part */
  313.     b8 = a & 0200;            /* and 8th (parity) bit. */
  314.  
  315.     if (ebqflg && b8) {            /* Do 8th bit prefix if necessary. */
  316.         data[size++] = ebq;
  317.         a = a7;
  318.     }
  319.     if ((a7 < SP) || (a7==DEL))    {    /* Do control prefix if necessary */
  320.         data[size++] = myctlq;
  321.     a = ctl(a);
  322.     }
  323.     if (a7 == myctlq)            /* Prefix the control prefix */
  324.         data[size++] = myctlq;
  325.  
  326.     if ((rptflg) && (a7 == rptq))    /* If it's the repeat prefix, */
  327.         data[size++] = myctlq;        /* quote it if doing repeat counts. */
  328.  
  329.     if ((ebqflg) && (a7 == ebq))    /* Prefix the 8th bit prefix */
  330.         data[size++] = myctlq;        /* if doing 8th-bit prefixes */
  331.  
  332.     data[size++] = a;            /* Finally, insert the character */
  333.     data[size] = '\0';            /* itself, and mark the end. */
  334. }
  335. #endif /* COMMENT */
  336.  
  337. /*  Output functions passed to 'decode':  */
  338.  
  339. int                   /*  Put character in server command buffer  */
  340. #ifdef CK_ANSIC
  341. putsrv(char c)
  342. #else
  343. putsrv(c) register char c;
  344. #endif /* CK_ANSIC */
  345. /* putsrv */ {
  346.     *srvptr++ = c;
  347.     *srvptr = '\0';        /* Make sure buffer is null-terminated */
  348.     return(0);
  349. }
  350.  
  351. int                    /*  Output character to console.  */
  352. #ifdef CK_ANSIC
  353. puttrm(char c)
  354. #else
  355. puttrm(c) register char c;
  356. #endif /* CK_ANSIC */
  357. /* puttrm */ {
  358. #ifndef NOSPL    
  359.     extern char * qbufp;        /* If REMOTE QUERY active, */
  360.     extern int query, qbufn;        /* also store response in */
  361.     if (query && qbufn++ < 1024) {    /* query buffer. */
  362.     *qbufp++ = c;
  363.     *qbufp = NUL;
  364.     } /* else */            /* else means don't display */
  365. #endif /* NOSPL */
  366.     conoc(c);
  367.     return(0);
  368. }
  369.  
  370. int                    /*  Output char to file. */
  371. #ifdef CK_ANSIC
  372. putfil(char c)
  373. #else
  374. putfil(c) register char c;
  375. #endif /* CK_ANSIC */
  376. /* putfil */ {
  377.     if (zchout(ZOFILE, (char) (c & fmask)) < 0) {
  378.     czseen = 1;               /* If write error... */
  379.     debug(F101,"putfil zchout write error, setting czseen","",1);
  380.     return(-1);
  381.     }
  382.     return(0);
  383. }
  384.  
  385. /* D E C O D E  --  Kermit packet decoding procedure */
  386.  
  387. /*
  388.  Call with string to be decoded and an output function.
  389.  Returns 0 on success, -1 on failure (e.g. disk full).
  390.  
  391.  This is the "inner loop" when receiving files, and must be coded as 
  392.  efficiently as possible.  Note some potential problems:  if a packet
  393.  is badly formed, having a prefixed sequence ending prematurely, this
  394.  function, as coded, could read past the end of the packet.  This has
  395.  never happened, thus the additional (time-consuming) tests have not
  396.  been added.
  397. */
  398.  
  399. static CHAR *xdbuf;    /* Global version of decode()'s buffer pointer */
  400.                         /* for use by translation functions. */
  401.  
  402. /* Function for pushing a character onto decode()'s input stream. */
  403.  
  404. VOID
  405. #ifdef CK_ANSIC
  406. zdstuff(CHAR c)
  407. #else
  408. zdstuff(c) CHAR c;
  409. #endif /* CK_ANSIC */
  410. /* zdstuff */ {    
  411.     xdbuf--;                /* Back up the pointer. */
  412.     *xdbuf = c;                /* Stuff the character. */
  413. }
  414.  
  415. int
  416. #ifdef CK_ANSIC
  417. decode(CHAR *buf, int (*fn)(char), int xlate)
  418. #else
  419. decode(buf,fn,xlate) register CHAR *buf; register int (*fn)(); int xlate;
  420. #endif /* CK_ANSIC */
  421. /* decode */ {
  422.     register unsigned int a, a7, a8, b8; /* Various copies of current char */
  423.     int t;                /* Int version of character */
  424.     int ssflg;                /* Character was single-shifted */
  425.  
  426. /*
  427.   Catch the case in which we are asked to decode into a file that is not open,
  428.   for example, if the user interrupted the transfer, but the other Kermit
  429.   keeps sending.
  430. */
  431.     if ((cxseen || czseen || discard) && (fn == putfil))
  432.       return(0);
  433.  
  434.     xdbuf = buf;            /* Make global copy of pointer. */
  435.     rpt = 0;                /* Initialize repeat count. */
  436.  
  437.     while ((a = *xdbuf++ & 0xFF) != '\0') { /* Get next character. */
  438.     if (a == rptq && rptflg) {    /* Got a repeat prefix? */
  439.         rpt = xunchar(*xdbuf++ & 0xFF); /* Yes, get the repeat count, */
  440.         rptn += rpt;
  441.         a = *xdbuf++ & 0xFF;    /* and get the prefixed character. */
  442.     }
  443.     b8 = lsstate ? 0200 : 0;    /* 8th-bit value from SHIFT-STATE */
  444.     if (ebqflg && a == ebq) {    /* Have 8th-bit prefix? */
  445.         b8 ^= 0200;            /* Yes, invert the 8th bit's value, */
  446.         ssflg = 1;            /* remember we did this, */
  447.         a = *xdbuf++ & 0xFF;    /* and get the prefixed character. */
  448.     } else ssflg = 0;
  449.  
  450.     if (a == ctlq) {        /* If control prefix, */
  451.         a  = *xdbuf++ & 0xFF;    /* get its operand */
  452.         a7 = a & 0x7F;        /* and its low 7 bits. */
  453.         if ((a7 >= 0100 && a7 <= 0137) || a7 == '?') { /* Controllify */
  454.         a = ctl(a);        /* if in control range. */
  455.         a7 = a & 0x7F;
  456.         }
  457.     } else a7 = a & 0x7f;        /* Not a control character */
  458.  
  459.     if (a7 < 32 || a7 == 127) {    /* Control character? */
  460.         if (lscapu) {        /* If doing locking shifts... */
  461.         if (lsstate)        /* If SHIFTED */
  462.           a8 = (a & ~b8) & 0xFF; /* Invert meaning of 8th bit */
  463.         else            /* otherwise */
  464.           a8 = a | b8;        /* OR in 8th bit */
  465.         /* If we're not in a quoted sequence */
  466.         if (!lsquote && (!lsstate || !ssflg)) {
  467.             if (a8 == DLE) {    /* Check for DLE quote */
  468.             lsquote = 1;    /* prefixed by single shift! */
  469.             continue;
  470.             } else if (a8 == SO) { /* Check for Shift-Out */
  471.             lsstate = 1;    /* SHIFT-STATE = SHIFTED */
  472.             continue;
  473.             } else if (a8 == SI) { /* or Shift-In */
  474.             lsstate = 0;    /* SHIFT-STATE = UNSHIFTED */
  475.             continue;
  476.             }
  477.         } else lsquote = 0;
  478.         }
  479.     }
  480.     a |= b8;            /* OR in the 8th bit */
  481.     if (rpt == 0) rpt = 1;        /* If no repeats, then one */
  482.     if (!binary) {            /* If in text mode, */
  483. #ifdef NLCHAR
  484.         if (a == CR) continue;    /* Discard carriage returns, */
  485.             if (a == LF) a = NLCHAR;     /* convert LF to system's newline. */
  486. #endif /* NLCHAR */
  487.  
  488. #ifndef NOCSETS                /* Character-set translation */
  489. #ifdef KANJI                /* For Kanji transfers, */
  490.         if (tcharset != TC_JEUC)    /* postpone translation. */
  491. #endif /* KANJI */
  492.           if (xlate && rx) a = (*rx)((CHAR) a); /* Translate charset */
  493. #endif /* NOCSETS */
  494.         }
  495.     if (fn == putfil) { /* (PWP) speedup via buffered output and a macro */
  496.         for (; rpt > 0; rpt--) {    /* Output the char RPT times */
  497. #ifndef NOCSETS
  498. #ifdef KANJI
  499.         if (!binary && tcharset == TC_JEUC &&
  500.             fcharset != FC_JEUC) { /* Translating from J-EUC */
  501.             if (ffc == 0L) xkanjf();
  502.             if (xkanji(a,fn) < 0)  /* to something else? */
  503.               return(-1);
  504.             else t = 1;
  505.         } else
  506. #endif /* KANJI */
  507. #endif /* NOCSETS */
  508. #ifdef OS2
  509.           if (xflg) {          /* For OS/2 only, use unbuffered */
  510.               char _a;
  511.               _a = a & fmask;
  512.               t = write(0,&_a,1); /* writes to console screen, to */
  513.               if (t < 1) t = -1;  /* reduce jerkiness. */
  514.           } else
  515. #endif /* OS2 */
  516.             t = zmchout(a & fmask); /* zmchout is a macro */
  517.         if (t < 0)
  518.           return(-1);
  519.         ffc++;            /* Count the character */
  520.         }
  521.     } else {            /* Output to something else. */
  522.         a &= fmask;            /* Apply file mask */
  523.         for (; rpt > 0; rpt--) {    /* Output the char RPT times */
  524.         if ((*fn)((char) a) < 0) return(-1); /* Send to output func. */
  525. #ifdef COMMENT
  526. /*
  527.   This was causing the server to count extra bytes that were part of
  528.   server command packets, like FINISH.
  529. */
  530.         ffc++;
  531. #endif /* COMMENT */
  532.         }
  533.     }
  534.     }
  535.     return(0);
  536. }
  537.  
  538. /*  G E T P K T -- Fill a packet data field  */
  539.  
  540. /*
  541.  Gets characters from the current source -- file or memory string.
  542.  Encodes the data into the packet, filling the packet optimally.
  543.  Set first = 1 when calling for the first time on a given input stream
  544.  (string or file).
  545.  
  546.  Call with:
  547.  bufmax -- current send-packet size
  548.  xlate  -- flag: 0 to skip character-set translation, 1 to translate
  549.  
  550.  Uses global variables:
  551.  t     -- current character.
  552.  first -- flag: 1 to start up, 0 for input in progress, -1 for EOF.
  553.  next  -- next character.
  554.  data  -- pointer to the packet data buffer.
  555.  size  -- number of characters in the data buffer.
  556.  memstr - flag that input is coming from a memory string instead of a file.
  557.  memptr - pointer to string in memory.
  558.  (*sx)()  character set translation function
  559.  
  560. Returns the size as value of the function, and also sets global "size",
  561. and fills (and null-terminates) the global data array.  Returns 0 upon eof.
  562.  
  563. Rewritten by Paul W. Placeway (PWP) of Ohio State University, March 1989.
  564. Incorporates old getchx() and encode() inline to reduce function calls,
  565. uses buffered input for much-improved efficiency, and clears up some
  566. confusion with line termination (CRLF vs LF vs CR).
  567.  
  568. Rewritten again by Frank da Cruz to incorporate locking shift mechanism,
  569. May 1991.
  570. */
  571.  
  572. /*
  573.   Lookahead function to decide whether locking shift is worth it.  Looks at
  574.   the next four input characters to see if all of their 8th bits match the
  575.   argument.  Call with 0 or 0200.  Returns 1 if so, 0 if not.  If we don't
  576.   happen to have at least 4 more characters waiting in the input buffer,
  577.   returns 1.  Note that zinptr points two characters ahead of the current
  578.   character because of repeat-count lookahead.
  579. */
  580.  
  581. #ifdef KANJI
  582. int
  583. kgetf() {
  584.     return(zminchar());
  585. }
  586.  
  587. int
  588. kgetm() {
  589.     int x;
  590.     if (x = *memptr++) return(x);
  591.     else return(-1);
  592. }
  593. #endif /* KANJI */
  594.  
  595. int
  596. lslook(b) unsigned int b; {        /* Locking Shift Lookahead */
  597.     int i;
  598.     if (zincnt < 3)            /* If not enough chars in buffer, */
  599.       return(1);            /* force shift-state switch. */
  600.     b &= 0200;                /* Force argument to proper form. */
  601.     for (i = -1; i < 3; i++)        /* Look at next 5 characters to */
  602.       if (((*(zinptr+i)) & 0200) != b)    /* see if all their 8th bits match.  */
  603.     return(0);            /* They don't. */
  604.     return(1);                /* They do. */
  605. }
  606.  
  607. int
  608. getpkt(bufmax,xlate) int bufmax, xlate; { /* Fill one packet buffer */
  609.     register CHAR rt = t, rnext;      /* Register shadows of the globals */
  610.     register CHAR *dp, *odp, *odp2, *p1, *p2; /* pointers... */
  611.     register int x;            /* Loop index. */
  612.     register int a7;            /* Low 7 bits of character */
  613.     static CHAR leftover[9] = { '\0','\0','\0','\0','\0','\0','\0','\0','\0' };
  614.     CHAR xxls, xxdl, xxrc, xxss, xxcq;    /* Pieces of prefixed sequence */
  615.     int n;                /* worker */
  616.  
  617. /*
  618.   Assume bufmax is the receiver's total receive-packet buffer length.
  619.   Our whole packet has to fit into it, so we adjust the data field length.
  620.   We also decide optimally whether it is better to use a short-format or
  621.   long-format packet when we're near the borderline.
  622. */
  623.     n = bufmax - 5;            /* Space for Data and Checksum */
  624.     if (n > 92 && n < 96) n = 92;    /* "Short" Long packets don't pay */
  625.     if (n > 92 && lpcapu == 0)        /* If long packets needed, */
  626.       n = 92;                /* make sure they've been negotiated */
  627.     bufmax = n - bctl;            /* Space for data */
  628.     if (n > 92) bufmax -= 3;        /* Long packet needs header chksum */
  629.  
  630.     if (first == 1) {        /* If first character of this file... */
  631.     if (!memstr) ffc = 0L;    /* Reset file character counter */
  632.     first = 0;        /* Next character won't be first */
  633.     *leftover = '\0';    /* Discard any interrupted leftovers, */
  634.  
  635.     /* get first character of file into rt, watching out for null file */
  636.  
  637. #ifndef NOCSETS
  638. #ifdef KANJI
  639.     if (!binary && tcharset == TC_JEUC && xlate) {
  640.         x = zkanjf();
  641.         if ((x = zkanji( memstr ? kgetm : kgetf )) < 0) {
  642.             first = -1;
  643.             size = 0;
  644.             if (x == -2) {
  645.                 debug(F100,"getpkt(zkanji): input error","",0);
  646.                 cxseen = 1;
  647.             } else debug(F100,"getpkt(zkanji): empty string/file","",0);
  648.             return (0);
  649.         }
  650.         if (!memstr) ffc++;
  651.         rt = x;
  652.     } else {
  653. #endif /* KANJI */
  654. #endif /* not NOCSETS */
  655.     if (memstr) {            /* Reading data from memory string */
  656.         if ((rt = *memptr++) == '\0') { /* end of string ==> EOF */
  657.         first = -1;
  658.             size = 0;
  659.         debug(F100,"getpkt: empty string","",0);
  660.         return (0);
  661.         }
  662.  
  663.     } else {            /* Reading data from a file */
  664.  
  665.         if ((x = zminchar()) < 0) { /* End of file or input error */
  666.         first = -1;
  667.             size = 0;
  668.         if (x == -2) {        /* Error */
  669.             debug(F100,"getpkt: input error","",0);
  670.             cxseen = 1;        /* Interrupt the file transfer */
  671.         } else debug(F100,"getpkt: empty file","",0); /* Empty file */
  672.         return(0);
  673.         }
  674.         ffc++;            /* Count a file character */
  675.         rt = x;            /* Convert int to char */
  676.         debug(F101,"getpkt zminchar","",rt);
  677.     }
  678. #ifndef NOCSETS
  679. #ifdef KANJI
  680.     }
  681. #endif /* KANJI */
  682. #endif /* not NOCSETS */
  683.  
  684.     rt &= fmask;            /* Apply SET FILE BYTESIZE mask */
  685.     debug(F101,"getpkt fmask","",fmask);
  686.     debug(F101,"getpkt new rt","",rt);
  687.  
  688. #ifndef NOCSETS
  689.     if (xlate) {
  690.         debug(F101,"getpkt about to call translate function","",rt);
  691.         debug(F101,"tcharset","",tcharset);
  692.         debug(F101,"fcharset","",fcharset);
  693.     }
  694. #ifdef KANJI
  695.     if (tcharset != TC_JEUC)
  696. #endif /* KANJI */
  697.       if (!binary && sx && xlate) {
  698.           rt = (*sx)(rt); /* Translate */
  699.           debug(F101," translate function returns","",rt);
  700.       }
  701. #endif /* not NOCSETS */
  702.  
  703.     /* PWP: handling of NLCHAR is done later (in the while loop)... */
  704.  
  705.     } else if ((first == -1) && (*leftover == '\0')) /* EOF from last time? */
  706.         return(size = 0);
  707. /*
  708.   Here we handle characters that were encoded for the last packet but
  709.   did not fit, and so were saved in the "leftover" array.
  710. */
  711.     dp = data;                /* Point to packet data buffer */
  712.     for (p1 = leftover; (*dp = *p1) != '\0'; p1++, dp++) /* Copy leftovers */
  713.         ;
  714.     *leftover = '\0';            /* Delete leftovers */
  715.     if (first == -1)            /* Handle EOF */
  716.       return(size = (dp - data));
  717.   
  718. /* Now fill up the rest of the packet. */
  719.  
  720.     rpt = 0;                /* Initialize character repeat count */
  721.  
  722.     while (first > -1) {        /* Until EOF... */
  723. #ifndef NOCSETS
  724. #ifdef KANJI
  725.     if (!binary && xlate && tcharset == TC_JEUC) {
  726.         if ((x = zkanji( memstr ? kgetm : kgetf )) < 0) {
  727.             first = -1;
  728.             if (x == -2) cxseen = 1;
  729.         } else if (!memstr) ffc++;
  730.         rnext = x & fmask;
  731.     } else {
  732. #endif /* KANJI */
  733. #endif /* not NOCSETS */
  734.     if (memstr) {            /* Get next char from memory string */
  735.         if ((x = *memptr++) == '\0') /* End of string means EOF */
  736.           first = -1;        /* Flag EOF for next time. */
  737.         rnext = x & fmask;        /* Apply file mask */
  738.     } else {
  739.         if ((x = zminchar()) < 0) { /* Real file, check for EOF */
  740.         first = -1;        /* Flag eof for next time. */
  741.         if (x == -2) cxseen = 1; /* If error, cancel this file. */
  742.         } else ffc++;        /* Count the character */
  743.         rnext = x & fmask;        /* Apply file mask */
  744.     } 
  745. #ifndef NOCSETS
  746. #ifdef KANJI
  747.     }
  748. #endif /* KANJI */
  749. #endif /* not NOCSETS */
  750.  
  751. #ifndef NOCSETS
  752. #ifdef KANJI
  753.     if (tcharset != TC_JEUC)
  754. #endif /* KANJI */
  755.         if (!binary && sx && xlate) {
  756.         rnext = (*sx)(rnext); /* Translate */
  757.         debug(F101,"getpkt xlated rnext to","",rnext);
  758.         }
  759. #endif /* not NOCSETS */
  760.  
  761.     odp = dp;            /* Remember where we started. */
  762.     xxls = xxdl = xxrc = xxss = xxcq = NUL;    /* Clear these. */
  763.  
  764. /*
  765.   Now encode the character according to the options that are in effect:
  766.     ctlp[]: whether this control character needs prefixing.
  767.     binary: text or binary mode.
  768.     rptflg: repeat counts enabled.
  769.     ebqflg: 8th-bit prefixing enabled.
  770.     lscapu: locking shifts enabled.
  771. */
  772.     if (rptflg) {            /* Repeat processing is on? */
  773.         if (
  774. #ifdef NLCHAR
  775.         /*
  776.          * If the next char is really CRLF, then we cannot
  777.          * be doing a repeat (unless CR,CR,LF which becomes
  778.          * "~ <n-1> CR CR LF", which is OK but not most efficient).
  779.          * I just plain don't worry about this case.  The actual
  780.          * conversion from NL to CRLF is done after the rptflg if...
  781.          */
  782.         (binary || (rnext != NLCHAR)) &&
  783. #endif /* NLCHAR */
  784.         (rt == rnext) && (first == 0)) { /* Got a run... */
  785.         if (++rpt < 94) {    /* Below max, just count */
  786.             continue;        /* go back and get another */
  787.         }
  788.         else if (rpt == 94) {    /* Reached max, must dump */
  789.             xxrc = tochar(rpt);    /* Put the repeat count here */
  790.             rptn += rpt;    /* Accumulate it for statistics */
  791.             rpt = 0;        /* And reset it */
  792.         }
  793.         } else if (rpt > 1) {    /* More than two */
  794.         xxrc = tochar(++rpt);    /* and count. */
  795.         rptn += rpt;
  796.         rpt = 0;        /* Reset repeat counter. */
  797.         }
  798.         /*
  799.           If (rpt == 1) we must encode exactly two characters.
  800.           This is done later, after the first character is encoded.
  801.         */
  802.     }
  803.  
  804. #ifdef NLCHAR
  805.     if (!binary && (rt == NLCHAR)) { /* It's the newline character */
  806.         if (lscapu && lsstate) {    /* If SHIFT-STATE is SHIFTED */
  807.         if (ebqflg) {        /* If single shifts enabled, */
  808.             *dp++ = ebq;    /* insert a single shift. */
  809.         } else {        /* Otherwise must shift in. */
  810.             *dp++ = myctlq;    /* Insert shift-out code */
  811.             *dp++ = 'O';
  812.             lsstate = 0;    /* Change shift state */
  813.         }
  814.         }
  815. #ifdef CK_SPEED
  816.         if (ctlp[CR]) {
  817.         *dp++ = myctlq;        /* Insert carriage return directly */
  818.         *dp++ = 'M';
  819.         } else *dp++ = CR;        /* Perhaps literally */
  820. #else /* !CK_SPEED */
  821.         *dp++ = myctlq;        /* Insert carriage return directly */
  822.         *dp++ = 'M';
  823. #endif /* CK_SPEED */
  824.         rt = LF;            /* Now make next char be linefeed. */
  825.     }
  826. #endif /* NLCHAR */
  827.  
  828. /*
  829.   Now handle the 8th bit of the file character.  If we have an 8-bit
  830.   connection, we preserve the 8th bit.  If we have a 7-bit connection,
  831.   we employ either single or locking shifts (if they are enabled).
  832. */
  833.     a7 = rt & 0177;            /* Get low 7 bits of character */
  834.     if (rt & 0200) {        /* 8-bit character? */
  835.         if (lscapu) {        /* Locking shifts enabled? */
  836.         if (!lsstate) {        /* Not currently shifted? */
  837.             x = lslook(0200);    /* Look ahead */
  838.             if (x != 0 || ebqflg == 0) { /* Locking shift decision */
  839.             xxls = 'N';       /* Need locking shift-out */
  840.             lsstate = 1;       /* and change to shifted state */
  841.             } else if (ebqflg) {   /* Not worth it */
  842.             xxss = ebq;       /* Use single shift */
  843.             }
  844.         }
  845.         rt = a7;        /* Replace character by 7-bit value */
  846.         } else if (ebqflg) {    /* 8th bit prefixing is on? */
  847.         xxss = ebq;        /* Insert single shift */
  848.         rt = a7;        /* Replace character by 7-bit value */
  849.         }
  850. /*
  851.   In case we have a 7-bit connection and this is an 8-bit character, AND
  852.   neither locking shifts nor single shifts are enabled, then the character's
  853.   8th bit will be destroyed in transmission, and a block check error will
  854.   occur.
  855. */
  856.     } else if (lscapu) {        /* 7-bit character */
  857.  
  858.         if (lsstate) {        /* Comes while shifted out? */
  859.         x = lslook(0);        /* Yes, look ahead */
  860.         if (x || ebqflg == 0) {    /* Time to shift in. */
  861.             xxls = 'O';        /* Set shift-in code */
  862.             lsstate = 0;    /* Exit shifted state */
  863.         } else if (ebqflg) {    /* Not worth it, stay shifted out */
  864.             xxss = ebq;        /* Insert single shift */
  865.         }
  866.         }
  867.     }
  868.     /* If data character is significant to locking shift protocol... */
  869.     if (lscapu && (a7 == SO || a7 == SI || a7 == DLE))
  870.       xxdl = 'P';            /* Insert datalink escape */
  871.  
  872.     if (
  873. #ifdef CK_SPEED
  874.         ctlp[rt]
  875. #else
  876.         (a7 < SP) || (a7 == DEL)
  877. #endif /* CK_SPEED */
  878.         ) {                /* Do control prefixing if necessary */
  879.         xxcq = myctlq;        /* The prefix */
  880.         rt = ctl(rt);        /* Uncontrollify the character */
  881.     }
  882.     if (a7 == myctlq)        /* Always prefix the control prefix */
  883.       xxcq = myctlq;
  884.  
  885.     if ((rptflg) && (a7 == rptq))    /* If it's the repeat prefix, */
  886.       xxcq = myctlq;        /* prefix it if doing repeat counts */
  887.  
  888.     if ((ebqflg) && (a7 == ebq))    /* Prefix the 8th-bit prefix */
  889.       xxcq = myctlq;        /* if doing 8th-bit prefixes */
  890.  
  891. /* Now construct the entire sequence */
  892.  
  893.     if (xxls) { *dp++ = myctlq; *dp++ = xxls; } /* Locking shift */
  894.     odp2 = dp;                    /* (Save this place) */
  895.     if (xxdl) { *dp++ = myctlq; *dp++ = xxdl; } /* Datalink escape */
  896.     if (xxrc) { *dp++ =   rptq; *dp++ = xxrc; } /* Repeat count */
  897.     if (xxss) { *dp++ = ebq; }            /* Single shift */
  898.     if (xxcq) { *dp++ = myctlq; }                /* Control prefix */
  899.     *dp++ = rt;            /* Finally, the character itself */
  900.  
  901.     if (rpt == 1) {            /* Exactly two copies? */
  902.         rpt = 0;
  903.         p2 = dp;            /* Save place temporarily */
  904.         for (p1 = odp2; p1 < p2; p1++) /* Copy the old chars over again */
  905.           *dp++ = *p1;
  906.         if ((p2-data) <= bufmax) odp = p2; /* Check packet bounds */
  907.     }
  908.     rt = rnext;            /* Next character is now current. */
  909.  
  910. /* Done encoding the character.  Now take care of packet buffer overflow. */
  911.  
  912.     if ((dp-data) >= bufmax) {    /* If too big, save some for next. */
  913.         size = (dp-data);        /* Calculate the size. */
  914.         *dp = '\0';            /* Mark the end. */
  915.         if ((dp-data) > bufmax) {    /* if packet is overfull */
  916.         /* copy the part that doesn't fit into the leftover buffer, */
  917.         /* taking care not to split a prefixed sequence. */
  918.         for (p1 = leftover, p2=odp; (*p1 = *p2) != '\0'; p1++,p2++)
  919.             ;
  920.         debug(F111,"getpkt leftover",leftover,size);
  921.         debug(F101," osize","",(odp-data));
  922.         size = (odp-data);    /* Return truncated packet. */
  923.         *odp = '\0';        /* Mark the new end */
  924.         }
  925.         t = rt; next = rnext;    /* save for next time */
  926.         return(size);
  927.     }
  928.     }                    /* Otherwise, keep filling. */
  929.  
  930.     size = (dp-data);            /* End of file */
  931.     *dp = '\0';                /* Mark the end of the data. */
  932.     debug(F111,"getpkt eof/eot",data,size); /* Fell thru before packet full, */
  933.     return(size);             /* return partially filled last packet. */
  934. }
  935.  
  936. /*  T I N I T  --  Initialize a transaction  */
  937.  
  938. int
  939. tinit() {
  940.     int x;
  941.  
  942. #ifndef NOCSETS
  943.     if (tcharset == TC_TRANSP) {    /* Character set translation */
  944.     rx = sx = NULL;            /* Transparent, no translation */
  945. #ifdef KANJI
  946.     } else if (tcharset == TC_JEUC) {
  947.     rx = sx = NULL;            /* Transparent, no translation */      
  948. #endif /* KANJI */
  949.     } else {                /* otherwise */
  950.     rx = xlr[tcharset][fcharset];    /* Input translation function */
  951.     sx = xls[tcharset][fcharset];    /* Output translation function */
  952.     }
  953.     debug(F101,"tinit tcharset","",tcharset);
  954.     debug(F101,"tinit fcharset","",fcharset);
  955. #ifdef COMMENT
  956.     debug(F101,"tinit sx   ","",sx);
  957.     debug(F101,"tinit rx   ","",rx);
  958. #endif /* COMMENT */
  959. #endif /* NOCSETS */
  960.     myinit[0] = '\0';            /* Haven't sent init string yet */
  961.     retrans = 0;            /* Packet retransmission count */
  962.     sndtyp = 0;                /* No previous packet */
  963.     xflg = 0;                /* Reset x-packet flag */
  964.     memstr = 0;                /* Reset memory-string flag */
  965.     memptr = NULL;            /*  and pointer */
  966.     bctu = bctl = 1;            /* Reset block check type to 1 */
  967.     autopar = 0;            /* Automatic parity detection flag */
  968.     rqf = -1;                /* Reset 8th-bit-quote request flag */
  969.     ebq = MYEBQ;            /* Reset 8th-bit quoting stuff */
  970.     ebqflg = 0;                /* 8th bit quoting not enabled */
  971.     ebqsent = 0;            /* No 8th-bit prefix bid sent yet */
  972.     sq = 'Y';                /* 8th-bit prefix bid I usually send */
  973.     if (savmod) {            /* If global file mode was saved, */
  974.         binary = savmod;        /*  restore it, */
  975.     savmod = 0;            /*  unsave it. */
  976.     }
  977.     pktnum = 0;                /* Initial packet number to send */
  978.     cxseen = czseen = discard = 0;    /* Reset interrupt flags */
  979.     *filnam = '\0';            /* Clear file name */
  980.     spktl = 0;                /* And its length */
  981.     nakstate = 0;            /* Assume not in a NAK'ing state */
  982.     numerrs = 0;            /* Transmission error counter */
  983.     if (server)             /* If acting as server, */
  984.       timint = srvtim;            /* Use server timeout interval. */
  985.     else                /* Otherwise */
  986.       timint = chktimo(rtimo,timef);    /* Begin by using local value */
  987.     spsiz = spsizr;            /* Initial send-packet size */
  988.     wslots = 1;                /* One window slot */
  989.     wslotn = 1;                /* No window negotiated yet */
  990.     winlo = 0;                /* Packet 0 is at window-low */
  991.     x = mksbuf(1);            /* Make a 1-slot send-packet buffer */
  992.     if (x < 0) return(x);
  993.     x = getsbuf(0);            /* Allocate first send-buffer. */
  994.     debug(F101,"tinit getsbuf","",x);
  995.     if (x < 0) return(x);
  996.     dumpsbuf();
  997.     x = mkrbuf(wslots);            /* & a 1-slot receive-packet buffer. */
  998.     if (x < 0) return(x);
  999.     what = W_NOTHING;            /* Doing nothing so far... */
  1000.     lsstate = 0;            /* Initialize locking shift state */
  1001.     whatru = 0;
  1002.     return(0);
  1003. }
  1004.  
  1005. VOID
  1006. pktinit() {                /* Initialize packet sequence */
  1007.     pktnum = 0;                /* number & window low. */
  1008.     winlo = 0;
  1009. }
  1010.  
  1011. /*  R I N I T  --  Respond to S or I packet  */
  1012.  
  1013. VOID
  1014. rinit(d) CHAR *d; {
  1015.     char *tp;
  1016.     ztime(&tp);
  1017.     tlog(F110,"Transaction begins",tp,0L); /* Make transaction log entry */
  1018.     tlog(F110,"Global file mode:", binary ? "binary" : "text", 0L);
  1019.     tlog(F110,"Collision action:", fncnam[fncact],0);
  1020.     tlog(F100,"","",0);
  1021.     filcnt = filrej = 0;        /* Init file counters */
  1022.     spar(d);
  1023.     ack1(rpar());
  1024. #ifdef datageneral
  1025.     if ((local) && (!quiet))            /* Only do this if local & not quiet */
  1026.         consta_mt();                    /* Start the asynch read task */
  1027. #endif /* datageneral */
  1028. }
  1029.  
  1030.  
  1031. /*  R E S E T C  --  Reset per-transaction character counters */
  1032.  
  1033. VOID
  1034. resetc() {
  1035.     rptn = 0;                /* Repeat counts */
  1036.     fsecs = flci = flco = 0L;        /* File chars in and out */
  1037.     tfc = tlci = tlco = 0L;        /* Total file, line chars in & out */
  1038. #ifdef COMMENT
  1039.     fsize = -1L;            /* File size */
  1040. #else
  1041.     if (what != W_SEND)
  1042.       fsize = -1L;
  1043.     debug(F101,"resetc fsize","",fsize);
  1044. #endif /* COMMENT */
  1045.     timeouts = retrans = 0;        /* Timeouts, retransmissions */
  1046.     spackets = rpackets = 0;        /* Packet counts out & in */
  1047.     crunched = 0;            /* Crunched packets */
  1048.     wcur = 0;                /* Current window size */
  1049.     wmax = 0;                /* Maximum window size used */
  1050. }
  1051.  
  1052. /*  S I N I T  --  Get & verify first file name, then send Send-Init packet */
  1053. /*
  1054.  Returns:
  1055.    1 if send operation begins successfully
  1056.    0 if send operation fails
  1057. */
  1058. #ifdef DYNAMIC
  1059. char *cmargbuf = NULL;
  1060. #else
  1061. char cmargbuf[256];
  1062. #endif /* DYNAMIC */
  1063. char *cmargp[2];
  1064.  
  1065. int
  1066. sinit() {
  1067.     int x;                /* Worker int */
  1068.     char *tp, *xp, *m;            /* Worker string pointers */
  1069.  
  1070.     filcnt = filrej = 0;        /* Initialize file counters */
  1071.     sndsrc = nfils;            /* Source for filenames */
  1072. #ifdef DYNAMIC
  1073.     if (!cmargbuf && !(cmargbuf = malloc(256)))
  1074.     fatal("sinit: no memory for cmargbuf");
  1075. #endif /* DYNAMIC */
  1076.     cmargbuf[0] = NUL;            /* Initialize name buffer */
  1077.  
  1078.     debug(F101,"sinit nfils","",nfils);
  1079.     debug(F110,"sinit cmarg",cmarg,0);
  1080.     debug(F110,"sinit cmarg2",cmarg2,0);
  1081.     if (nfils == 0) {            /* Sending from stdin or memory. */
  1082.     if ((cmarg2 != NULL) && (*cmarg2)) {
  1083.         cmarg = cmarg2;        /* If F packet, "as-name" is used */
  1084.         cmarg2 = "";        /* if provided */
  1085.     } else cmarg = "stdin";        /* otherwise just use "stdin" */
  1086.     strcpy(cmargbuf,cmarg);
  1087.     cmargp[0] = cmargbuf;
  1088.     cmargp[1] = "";
  1089.     cmlist = cmargp;
  1090.     nfils = 1;
  1091.     }
  1092. #ifdef COMMENT
  1093.     if (nfils < 1) {            /* Filespec pointed to by cmarg */
  1094.     if (nfils < 0) sndsrc = 1;
  1095.     nfils = 1;            /* Change it to cmlist */
  1096.     strcpy(cmargbuf,cmarg);        /* so we have a consistent way */
  1097.     cmargp[0] = cmargbuf;        /* of going thru the file list. */
  1098.     cmargp[1] = "";
  1099.     cmlist = cmargp;
  1100.     }
  1101.  
  1102. /* At this point, cmlist contains the list of filespecs to send */
  1103.  
  1104.     debug(F111,"sinit *cmlist",*cmlist,nfils);
  1105.  
  1106.     xp = *cmlist;            /* Save this for messages */
  1107. #else
  1108.     xp = (nfils < 0) ? cmarg : *cmlist;
  1109. #endif
  1110.  
  1111.     x = gnfile();            /* Get first filename. */
  1112.     m = NULL;                /* Error message pointer */
  1113.     debug(F101,"sinit gnfil","",x);
  1114.     switch (x) {
  1115.       case -5: m = "Too many files match wildcard"; break;
  1116.       case -4: m = "Cancelled"; break;
  1117.       case -3: m = "Read access denied"; break;
  1118.       case -2: m = "File is not readable"; break;
  1119.       case -1: m = iswild(filnam) ? "No files match" : "File not found";
  1120.     break;
  1121.       case  0: m = "No filespec given!" ; break;
  1122.       default:
  1123.     break;
  1124.     }
  1125.     debug(F101,"sinit nfils","",nfils);
  1126.     debug(F110,"sinit filnam",filnam,0);
  1127.     if (x < 1) {            /* Didn't get a file. */
  1128.     if (server)            /* Doing GET command */
  1129.       errpkt((CHAR *)m);        /* so send Error packet. */
  1130.     else                /* Doing SEND command */
  1131.       screen(SCR_EM,0,0l,m);    /* so print message. */
  1132.     tlog(F110,xp,m,0L);        /* Make transaction log entry. */
  1133.     freerbuf(rseqtbl[0]);        /* Free the buffer the GET came in. */
  1134.     return(0);            /* Return failure code */
  1135.     }
  1136.     if (!local && !server) sleep(delay); /* Delay if requested */
  1137. #ifdef datageneral
  1138.     if ((local) && (!quiet))            /* Only do this if local & not quiet */
  1139.         consta_mt();                    /* Start the asynch read task */
  1140. #endif /* datageneral */
  1141.     freerbuf(rseqtbl[0]);        /* Free the buffer the GET came in. */
  1142.     sipkt('S');                /* Send the Send-Init packet. */
  1143.     ztime(&tp);                /* Get current date/time */
  1144.     tlog(F110,"Transaction begins",tp,0L); /* Make transaction log entry */
  1145.     tlog(F110,"Global file mode:", binary ? "binary" : "text", 0L);
  1146.     tlog(F100,"","",0);
  1147.     debug(F111,"sinit ok",filnam,0);
  1148.     return(1);
  1149. }
  1150.  
  1151. int
  1152. #ifdef CK_ANSIC
  1153. sipkt(char c)                /* Send S or I packet. */
  1154. #else
  1155. sipkt(c) char c;
  1156. #endif
  1157. /* sipkt */ {
  1158.     CHAR *rp; int k;
  1159.     debug(F101,"sipkt pktnum","",pktnum);
  1160.     k = sseqtbl[pktnum];        /* Find slot for this packet */
  1161.     debug(F101,"sipkt k","",k);
  1162.     if (k < 0) {            /* No slot? */
  1163.     k = getsbuf(winlo = pktnum);    /* Make one. */
  1164.     debug(F101,"sipkt getsbuf","",k);    
  1165.     }
  1166.     ttflui();                /* Flush pending input. */
  1167.     rp = rpar();            /* Get protocol parameters. */
  1168.     return(spack(c,pktnum,(int)strlen((char *)rp),rp)); /* Send them. */
  1169. }
  1170.  
  1171. /*  X S I N I T  --  Retransmit S-packet  */
  1172. /*
  1173.   For use in the GET-SEND sequence, when we start to send, but receive another
  1174.   copy of the GET command because the receiver didn't get our S packet.
  1175.   This retransmits the S packet and frees the receive buffer for the ACK.
  1176.   This special case is necessary because packet number zero is being re-used.
  1177. */
  1178. VOID
  1179. xsinit() {
  1180.     int k;
  1181.     k = rseqtbl[0];
  1182.     debug(F101,"xsinit k","",k);
  1183.     if (k > -1)
  1184.       freerbuf(k);
  1185.     resend(0);
  1186. }
  1187.  
  1188. /*  R C V F I L -- Receive a file  */
  1189.  
  1190. /*
  1191.   Incoming filename is in data field of F packet.
  1192.   This function decodes it into the srvcmd buffer, substituting an
  1193.   alternate "as-name", if one was given.
  1194.   Then it does any requested transformations (like converting to
  1195.   lowercase), and finally if a file of the same name already exists, 
  1196.   takes the desired collision action.
  1197. */
  1198. #ifdef pdp11
  1199. #define XNAMLEN 65
  1200. #else
  1201. #define XNAMLEN 256
  1202. #endif /* pdp11 */
  1203.  
  1204. char ofn1[XNAMLEN];            /* Buffer for output file name */
  1205. char * ofn2;                /* Pointer to backup file name */
  1206. int ofn1x;                /* Flag output file already exists */
  1207. int opnerr;                /* Flag for open error */
  1208.  
  1209. int                    /* Returns success ? 1 : 0 */
  1210. rcvfil(n) char *n; {
  1211. #ifdef OS2
  1212. #ifdef __32BIT__
  1213.     char *zs, *longname, *newlongname, *pn; /* OS/2 long name items */
  1214. #endif /* __32BIT__ */
  1215. #endif /* OS2 */
  1216. #ifdef DTILDE
  1217.     char *dirp, *tilde_expand();
  1218. #endif /* DTILDE */
  1219.     int dirflg;
  1220.  
  1221.     opnerr = 0;
  1222.     ofn2 = NULL;            /* No new name (yet) */
  1223.     lsstate = 0;            /* Cancel locking-shift state */
  1224.     srvptr = srvcmd;            /* Decode file name from packet. */
  1225.     decode(rdatap,putsrv,0);        /* Don't xlate charsets. */
  1226.     if (*srvcmd == '\0')        /* Watch out for null F packet. */
  1227.       strcpy((char *)srvcmd,"NONAME");
  1228. #ifdef DTILDE
  1229.     if (*srvcmd == '~') {
  1230.     dirp = tilde_expand((char *)srvcmd); /* Expand tilde, if any. */
  1231.     if (*dirp != '\0') strcpy((char *)srvcmd,dirp);
  1232.     }
  1233. #else
  1234. #ifdef OS2
  1235.     if (isalpha(*srvcmd) && srvcmd[1] == ':' && srvcmd[2] == '\0')
  1236.       strcat((char *)srvcmd,"NONAME");
  1237. #endif /* OS2 */
  1238. #endif /* DTILDE */
  1239.     screen(SCR_FN,0,0l,(char *)srvcmd);    /* Put it on screen if local */
  1240.     debug(F110,"rcvfil",(char *)srvcmd,0); /* Debug log entry */
  1241.     debug(F110,"rcvfil cmarg2",cmarg2,0);
  1242.     tlog(F110,"Receiving",(char *)srvcmd,0L); /* Transaction log entry */
  1243.     if (cmarg2) {            /* Check for alternate name */
  1244.         if (*cmarg2) {
  1245.         debug(F110,"rcvfil substituting cmarg2",cmarg2,0);
  1246.         strcpy((char *)srvcmd,cmarg2); /* Got one, use it. */
  1247.     }
  1248.     }
  1249.     cmarg2 = "";            /* Done with alternate name */
  1250.  
  1251.     if ((int)strlen((char *)srvcmd) > XNAMLEN) /* Watch out for overflow */
  1252.       *(srvcmd + XNAMLEN - 1) = NUL;
  1253.  
  1254.     /* At this point, srvcmd[] contains the incoming filename or as-name */
  1255.  
  1256.     if (fnrpath) {            /* RECEIVE PATHNAMES OFF? */
  1257.     char *t;            /* Yes. */
  1258.     zstrip((char *)srvcmd,&t);    /* Off with it. */
  1259.     debug(F110,"rcvfil zstrip",t,0);
  1260.     if (!t)                /* Be sure we didn't strip too much */
  1261.       strcpy(ofn1,"UNKNOWN");
  1262.     else if (*t == '\0')
  1263.       strcpy(ofn1,"UNKNOWN");
  1264.     else strcpy(ofn1,t);
  1265.     strcpy((char *)srvcmd,ofn1);    /* Now copy it back. */
  1266.     }
  1267.  
  1268.     /* Now srvcmd contains incoming filename with path possibly stripped */
  1269.  
  1270.     if (fncnv)                /* FILE NAMES CONVERTED? */
  1271.       zrtol((char *)srvcmd,(char *)ofn1); /* Yes, convert to local form */
  1272.     else
  1273.       strcpy(ofn1,(char *)srvcmd);    /* No, copy literally. */
  1274.  
  1275.     /* Now the incoming filename, possibly converted, is in ofn1[]. */
  1276.  
  1277. #ifdef OS2
  1278.     /* Don't refuse the file just because the name is illegal. */
  1279.     if (!IsFileNameValid(ofn1)) {    /* Name is OK for OS/2? */
  1280. #ifdef __32BIT__
  1281.     char *zs = NULL;
  1282.     zstrip(ofn1, &zs);        /* Not valid, strip unconditionally */
  1283.     if (zs) {
  1284.         iattr.longname.len = strlen(zs); /* Store in attribute structure */
  1285.         if (iattr.longname.val)    /* Free previous longname, if any */
  1286.           free(iattr.longname.val);
  1287.         iattr.longname.val = (char *) malloc( iattr.longname.len + 1 ) ;
  1288.         if (iattr.longname.val)    /* Remember this (illegal) name */
  1289.           strcpy( iattr.longname.val, zs ) ;
  1290.     }
  1291. #endif /* __32BIT__ */
  1292.     debug(F110,"rcvfil: invalid file name",ofn1,0);
  1293.     ChangeNameForFAT(ofn1);    /* Change to an acceptable name */
  1294.     debug(F110,"rcvfil: FAT file name",ofn1,0);
  1295.  
  1296.     } else {                /* Name is OK. */
  1297.  
  1298.     debug(F110,"rcvfil: valid file name",ofn1,0);
  1299. #ifdef __32BIT__
  1300.     iattr.longname.len = 0;
  1301.     if (iattr.longname.val)        /* Free previous longname, if any */
  1302.       free(iattr.longname.val);
  1303.     iattr.longname.val = NULL;    /* This file doesn't need a longname */
  1304. #endif /* __32BIT__ */
  1305.     }
  1306. #endif /* OS2 */
  1307.     debug(F110,"rcvfil as",ofn1,0);
  1308.  
  1309. /* Filename collision action section. */
  1310.  
  1311.     dirflg =                /* Is it a directory name? */
  1312. #ifdef CK_TMPDIR
  1313.         isdir(ofn1)
  1314. #else
  1315.     0
  1316. #endif /* CK_TMPDIR */
  1317.       ;
  1318.     debug(F101,"rcvfil dirflg","",dirflg);
  1319.     ofn1x = (zchki(ofn1) != -1);    /* File already exists? */
  1320.     debug(F101,"rcvfil ofn1x",ofn1,ofn1x);
  1321.  
  1322.     if ( (
  1323. #ifdef UNIX
  1324.     strcmp(ofn1,"/dev/null") &&    /* It's not the null device? */
  1325. #endif /* UNIX */
  1326.     !stdouf ) &&            /* Not copying to standard output? */
  1327.     ofn1x ||            /* File of same name exists? */
  1328.     dirflg ) {            /* Or file is a directory? */
  1329.         debug(F111,"rcvfil exists",ofn1,fncact);
  1330.     switch (fncact) {        /* Yes, do what user said. */
  1331.       case XYFX_A:            /* Append */
  1332.         debug(F100,"rcvfil append","",0);
  1333.         if (dirflg) {
  1334.         rf_err = "Can't append to a directory";
  1335.         tlog(F100," error - can't append to directory","",0);
  1336.         discard = opnerr = 1;
  1337.         return(0);
  1338.         }
  1339.         tlog(F110," appending to",ofn1,0);
  1340.         break;
  1341.       case XYFX_Q:            /* Query (Ask) */
  1342.         break;            /* not implemented */
  1343.       case XYFX_B:            /* Backup (rename old file) */
  1344.         if (dirflg) {
  1345.         rf_err = "Can't rename existing directory";
  1346.         tlog(F100," error - can't rename directory","",0);
  1347.         discard = opnerr = 1;
  1348.         return(0);
  1349.         }
  1350.         znewn(ofn1,&ofn2);        /* Get new unique name */
  1351.         tlog(F110," backup:",ofn2,0);
  1352.         debug(F110,"rcvfil backup ofn1",ofn1,0);
  1353.         debug(F110,"rcvfil backup ofn2",ofn2,0);
  1354. #ifdef OS2
  1355. #ifdef __32BIT__
  1356. /*
  1357.   In case this is a FAT file system, we can't change only the FAT name, we
  1358.   also have to change the longname from the extended attributes block.
  1359.   Otherwise, we'll have many files with the same longname and if we copy them
  1360.   to an HPFS volume, only one will survive.
  1361. */
  1362.         if (os2getlongname(ofn1, &longname) > -1) {
  1363.         if (strlen(longname)) {
  1364.             char tmp[10];
  1365.             extern int ck_znewn;
  1366.             sprintf(tmp,".~%d~",ck_znewn);
  1367.             newlongname =
  1368.               (char *) malloc(strlen(longname) + strlen(tmp) + 1 ) ;
  1369.             if ( newlongname ) {
  1370.             strcpy( newlongname, longname ) ;
  1371.             strcat( newlongname, tmp ) ;
  1372.             os2setlongname(ofn1, newlongname);
  1373.             free(newlongname) ;
  1374.             }
  1375.         }
  1376.         } else debug(F100,"rcvfil os2getlongname failed","",0);
  1377. #endif /* __32BIT__ */
  1378. #endif /* OS2 */
  1379.  
  1380. #ifdef COMMENT
  1381.         /* Do this later, in opena()... */
  1382.         if (zrename(ofn1,ofn2) < 0) {
  1383.         rf_err = "Can't transform filename";
  1384.         debug(F110,"rcvfil rename fails",ofn1,0);
  1385.         discard = opnerr = 1;
  1386.         return(0);
  1387.         }
  1388. #endif /* COMMENT */
  1389.         break;
  1390.  
  1391.       case XYFX_D:            /* Discard (refuse new file) */
  1392.         discard = 1;
  1393.         rejection = 1;        /* Horrible hack: reason = name */
  1394.         debug(F101,"rcvfil discard","",discard);
  1395.         tlog(F100," refused: name","",0);
  1396.         break;
  1397.  
  1398.       case XYFX_R:            /* Rename incoming file */
  1399.         znewn(ofn1,&ofn2);        /* Make new name for it */
  1400. #ifdef OS2
  1401. #ifdef __32BIT__
  1402.         if (iattr.longname.len) {
  1403.             char tmp[10];
  1404.             extern int ck_znewn;
  1405.             sprintf(tmp,".~%d~",ck_znewn);
  1406.             newlongname =
  1407.               (char *) malloc(iattr.longname.len + strlen(tmp) + 1);
  1408.             if (newlongname) {
  1409.             strcpy( newlongname, iattr.longname.val);
  1410.             strcat( newlongname, tmp);
  1411.             debug(F110,
  1412.                   "Rename Incoming: newlongname",newlongname,0);
  1413.             iattr.longname.len = strlen(newlongname);
  1414.             if (iattr.longname.val)
  1415.               free(iattr.longname.val);
  1416.             iattr.longname.val = newlongname ;
  1417.             }
  1418.         }
  1419. #endif /* __32BIT__ */
  1420. #endif /* OS2 */
  1421.         break;
  1422.       case XYFX_X:            /* Replace old file */
  1423.         debug(F100,"rcvfil overwrite","",0);
  1424.         if (dirflg) {
  1425.         rf_err = "Can't overwrite existing directory";
  1426.         tlog(F100," error - can't overwrite directory","",0);
  1427.         discard = opnerr = 1;
  1428. #ifdef COMMENT
  1429.         return(0);
  1430. #else
  1431.         break;
  1432. #endif /* COMMENT */
  1433.         }
  1434.         tlog(F110,"overwriting",ofn1,0);
  1435.         break;
  1436.       case XYFX_U:            /* Refuse if older */
  1437.         debug(F100,"rcvfil update","",0);
  1438.         if (dirflg) {
  1439.         rf_err = "File has same name as existing directory";
  1440.         tlog(F110," error - directory exists:",ofn1,0);
  1441.         discard = opnerr = 1;
  1442. #ifdef COMMENT
  1443.         /* Don't send an error packet, just refuse the file */
  1444.         return(0);
  1445. #endif /* COMMENT */
  1446.         }
  1447.         break;            /* Not here, we don't have */
  1448.                     /* the attribute packet yet. */
  1449.       default:
  1450.         debug(F101,"rcvfil bad collision action","",fncact);
  1451.         break;
  1452.     }
  1453.     }
  1454.     debug(F110,"rcvfil ofn1",ofn1,0);
  1455.     debug(F110,"rcvfil ofn2",ofn2,0);
  1456.     if (fncact == XYFX_R && ofn1x && ofn2) { /* Renaming incoming file? */
  1457.     screen(SCR_AN,0,0l,ofn2);    /* Display renamed name */
  1458.     strcpy(n, ofn2);        /* Return it */
  1459.     } else {                /* No */
  1460.     screen(SCR_AN,0,0l,ofn1);    /* Display regular name */
  1461.     strcpy(n, ofn1);        /* and return it. */
  1462.     }
  1463.  
  1464. #ifdef CK_MKDIR
  1465. /*  Create directory(s) if necessary.  */
  1466.     if (!discard && !fnrpath) {        /* RECEIVE PATHAMES ON? */
  1467.     debug(F110,"rcvfil calling zmkdir",ofn1,0); /* Yes */
  1468.     if (zmkdir(ofn1) < 0) {
  1469.         debug(F100,"zmkdir fails","",0);
  1470.         tlog(F110," error - directory creation failure:",ofn1,0);
  1471.         rf_err = "Directory creation failure.";
  1472.         discard = 1;
  1473.         return(0);
  1474.     }
  1475.     }
  1476. #else
  1477.     debug(F110,"sfile CK_MKDIR not defined",ofn1,0);
  1478. #endif /* CK_MKDIR */
  1479.  
  1480. #ifndef NOICP
  1481. /* #ifndef MAC */
  1482. /* Why not Mac? */
  1483.     strcpy(fspec,ofn1);            /* Here too for \v(filespec) */
  1484. /* #endif */ 
  1485. #endif /* NOICP */
  1486.     debug(F110,"rcvfil: n",n,0);
  1487.     ffc = 0L;                /* Init per-file counters */
  1488.     cps = oldcps = 0L ;
  1489.     rs_len = 0L;
  1490.     rejection = -1;
  1491.     fsecs = gtimer();            /* Time this file started */
  1492.     filcnt++;
  1493.     intmsg(filcnt);
  1494.     return(1);                /* Successful return */
  1495. }
  1496.  
  1497.  
  1498. /*  R E O F  --  Receive End Of File packet for incoming file */
  1499.  
  1500. /*
  1501.   Closes the received file.
  1502.   Returns:
  1503.     0 on success.
  1504.    -1 if file could not be closed.
  1505.     2 if disposition was mail, mail was sent, but temp file not deleted.
  1506.     3 if disposition was print, file was printed, but not deleted.
  1507.    -2 if disposition was mail and mail could not be sent
  1508.    -3 if disposition was print and file could not be printed
  1509. */
  1510. int
  1511. reof(f,yy) char *f; struct zattr *yy; {
  1512.     int x;
  1513.     char *p;
  1514.     char c;
  1515.  
  1516.     debug(F111,"reof fncact",f,fncact);
  1517.     debug(F101,"reof discard","",discard);
  1518.     success = 1;            /* Assume status is OK */
  1519.     lsstate = 0;            /* Cancel locking-shift state */
  1520.     if (discard) {            /* Handle attribute refusals, etc. */
  1521.     debug(F101,"reof discarding","",0);
  1522.     success = 0;            /* Status = failed. */
  1523.     if (rejection == '#' ||        /* Unless rejection reason is */
  1524.         rejection ==  1  ||        /* date or name (SET FILE COLLISION */
  1525.         rejection == '?')        /* UPDATE or DISCARD) */
  1526.       success = 1;                
  1527.     debug(F101,"reof success","",success);
  1528.     filrej++;            /* Count this rejection. */
  1529.     discard = 0;            /* We never opened the file, */
  1530.     return(0);            /* so we don't close it. */
  1531.     }
  1532. #ifdef DEBUG
  1533.     if (deblog) {
  1534.     debug(F101,"reof cxseen","",cxseen);
  1535.     debug(F101,"reof czseen","",czseen);
  1536.     debug(F110,"reof rdatap",rdatap,0);
  1537.     }
  1538. #endif /* DEBUG */
  1539.     if (cxseen == 0) cxseen = (*rdatap == 'D');    /* Got cancel directive? */
  1540.     success = (cxseen || czseen) ? 0 : 1; /* Set SUCCESS flag appropriately */
  1541.     if (!success) filrej++;        /* "Uncount" this file */
  1542.     debug(F101,"reof success","",czseen);
  1543.     x = clsof(cxseen || czseen);    /* Close the file (resets cxseen) */
  1544.     if (x < 0) {            /* If failure to close, FAIL */
  1545.     if (success) filrej++;
  1546.     success = 0;
  1547.     }
  1548.     if (success && atcapu) zstime(f,yy,0); /* Set file creation date */
  1549. #ifdef OS2
  1550. #ifdef __32BIT__
  1551.     if (success && yy->longname.len)
  1552.       os2setlongname( f, yy->longname.val ) ;
  1553. #endif /* __32BIT__ */
  1554. #endif /* OS2 */
  1555.     if (success == 0) xitsta |= W_RECV;    /* And program return code */
  1556.  
  1557. /* Handle dispositions from attribute packet... */
  1558.  
  1559. #ifndef NOFRILLS
  1560.     if (yy->disp.len != 0) {
  1561.     p = yy->disp.val;
  1562.     c = *p++;
  1563.     if (c == 'M') {            /* Mail to user. */
  1564.         x = zmail(p,filnam);    /* Do the system's mail command */
  1565.         if (x < 0) success = 0;    /* Remember status */
  1566.         tlog(F110,"mailed",filnam,0L);
  1567.         tlog(F110," to",p,0L);
  1568.         zdelet(filnam);        /* Delete the file */
  1569.     } else if (c == 'P') {        /* Print the file. */
  1570.         x = zprint(p,filnam);    /* Do the system's print command */
  1571.         if (x < 0) success = 0;    /* Remember status */
  1572.         tlog(F110,"printed",filnam,0L);
  1573.         tlog(F110," with options",p,0L);
  1574. #ifndef VMS
  1575. #ifndef STRATUS
  1576.         /* spooler will delete file after print complete in VOS & VMS */
  1577.         if (zdelet(filnam) && x == 0) x = 3; /* Delete the file */
  1578. #endif /* STRATUS */
  1579. #endif /* VMS */
  1580.     }
  1581.     }
  1582. #endif /* NOFRILLS */
  1583.     debug(F101,"reof returns","",x);
  1584.     *filnam = '\0';
  1585.     return(x);
  1586. }
  1587.  
  1588. /*  R E O T  --  Receive End Of Transaction  */
  1589.  
  1590. VOID
  1591. reot() {
  1592.     cxseen = czseen = discard = 0;    /* Reset interruption flags */
  1593.     tstats();
  1594. }
  1595.  
  1596. /*  S F I L E -- Send File header or teXt header packet  */
  1597.  
  1598. /*  Call with x nonzero for X packet, zero for F packet  */
  1599. /*  Returns 1 on success, 0 on failure                   */
  1600.  
  1601. int
  1602. sfile(x) int x; {
  1603. #ifdef pdp11
  1604. #define PKTNL 64
  1605. #else
  1606. #define PKTNL 256
  1607. #endif /* pdp11 */
  1608.     char pktnam[PKTNL+1];        /* Local copy of name */
  1609.     char *s;
  1610.  
  1611.     /* cmarg2 or filnam (with that precedence) have the file's name */
  1612.  
  1613.     lsstate = 0;            /* Cancel locking-shift state */
  1614.     if (nxtpkt() < 0) return(0);    /* Bump packet number, get buffer */
  1615.     if (x == 0) {            /* F-Packet setup */
  1616.         if (cmarg2 && *cmarg2) {    /* If we have a send-as name, */
  1617.         debug(F111,"sfile cmarg2",cmarg2,cmarg2);
  1618.         strncpy(pktnam,cmarg2,PKTNL); /* copy it literally, */
  1619.         cmarg2 = "";        /* and blank it out for next time. */
  1620.         } else {            /* Otherwise... */
  1621.         debug(F101,"sfile fnspath","",fnspath);
  1622.         if (fnspath) {        /* Stripping path names? */
  1623.         char *t;        /* Yes. */
  1624.         zstrip(filnam,&t);    /* Strip off the path. */
  1625.         debug(F110,"sfile zstrip",t,0);
  1626.         if (!t) t = "UNKNOWN";    /* Be cautious... */
  1627.         else if (*t == '\0')
  1628.           t = "UNKNOWN";
  1629.         strncpy(pktnam,t,PKTNL); /* Copy stripped name literally. */
  1630.         } else {            /* No stripping. */
  1631.         strcpy(pktnam,filnam);    /* Copy whole name. */
  1632.         }
  1633.         /* pktnam[] has the packet name, filnam[] has the original name. */
  1634.         /* But we still need to convert pktnam if FILE NAMES CONVERTED.  */
  1635.  
  1636.         if (fncnv) {        /* If converting names, */
  1637.         zltor(pktnam,(char *)srvcmd); /* convert it to common form, */
  1638.         strcpy(pktnam,(char *)srvcmd); /* with srvcmd as temp buffer */
  1639.         *srvcmd = NUL;
  1640.         }
  1641.         }
  1642.         debug(F110,"sfile",filnam,0);    /* Log debugging info */
  1643.         debug(F110," pktnam",pktnam,0);
  1644.         if (openi(filnam) == 0)     /* Try to open the input file */
  1645.       return(0);         
  1646. #ifdef CK_RESEND
  1647.     if (sendmode == SM_PSEND)    /* PSENDing? */
  1648.       if (sendstart > 0L)        /* Starting position */
  1649.         if (zfseek(sendstart) < 0)    /* seek to it... */
  1650.           return(0);
  1651. #endif /* CK_RESEND */
  1652.         s = pktnam;            /* Name for packet data field */
  1653. #ifdef OS2
  1654.     /* Never send a disk letter. */
  1655.     if (isalpha(*s) && (*(s+1) == ':'))
  1656.       s += 2;
  1657. #endif /* OS2 */
  1658.  
  1659.     } else {                /* X-packet setup, not F-packet. */
  1660.  
  1661.         debug(F110,"sxpack",cmdstr,0);    /* Log debugging info */
  1662.         s = cmdstr;            /* Name for data field */
  1663.     }
  1664.  
  1665.     /* Now s points to the string that goes in the packet data field. */
  1666.  
  1667.     encstr((CHAR *)s);            /* Encode the name. */
  1668.                     /* Send the F or X packet */
  1669.     /* If the encoded string did not fit into the packet, it was truncated. */
  1670.    
  1671. #ifdef COMMENT
  1672.     spack((char) (x ? 'X' : 'F'), pktnum, size, encbuf+7);
  1673. #else
  1674.     spack((char) (x ? 'X' : 'F'), pktnum, size, data);
  1675. #endif
  1676.  
  1677.     if (x == 0) {            /* Display for F packet */
  1678.         if (displa) {            /* Screen */
  1679.         screen(SCR_FN,'F',(long)pktnum,filnam);
  1680.         screen(SCR_AN,0,0l,pktnam);
  1681.         screen(SCR_FS,0,fsize,"");
  1682.         }
  1683.         tlog(F110,"Sending",filnam,0L);    /* Transaction log entry */
  1684.         tlog(F110," as",pktnam,0L);
  1685.     if (binary) {            /* Log file mode in transaction log */
  1686.         tlog(F101," mode: binary","",(long) binary);
  1687.     } else {            /* If text mode, check character set */
  1688.         tlog(F100," mode: text","",0L);
  1689. #ifndef NOCSETS
  1690.         tlog(F110," file character set",fcsinfo[fcharset].name,0L);
  1691.         if (tcharset == TC_TRANSP)
  1692.           tlog(F110," xfer character set","transparent",0L);
  1693.         else
  1694.           tlog(F110," xfer character set",tcsinfo[tcharset].name,0L);
  1695. #endif /* NOCSETS */
  1696.     }
  1697.     } else {                /* Display for X-packet */
  1698.  
  1699.         screen(SCR_XD,'X',(long)pktnum,cmdstr);    /* Screen */
  1700.         tlog(F110,"Sending from:",cmdstr,0L);    /* Transaction log */
  1701.     }
  1702.     intmsg(++filcnt);            /* Count file, give interrupt msg */
  1703.     first = 1;                /* Init file character lookahead. */
  1704.     ffc = 0L;                /* Init file character counter. */
  1705.     cps = oldcps = 0L ; /* Init cps statistics */
  1706.     rejection = -1;
  1707.     fsecs = gtimer();            /* Time this file started */
  1708.     debug(F101,"SFILE fsecs","",fsecs);
  1709.     return(1);
  1710. }
  1711.  
  1712. /*  S D A T A -- Send a data packet */
  1713.  
  1714. /*
  1715.   Returns -1 if no data to send (end of file).  If there is data, a data
  1716.   packet is sent, and sdata() returns 1.
  1717.  
  1718.   For window size greater than 1, keep sending data packets until window
  1719.   is full or characters start to appear from the other Kermit, whichever
  1720.   happens first.
  1721.  
  1722.   In the windowing case, when there is no more data left to send (or when
  1723.   sending has been interrupted), sdata() does nothing and returns 0 each time
  1724.   it is called until the current packet number catches up to the last data
  1725.   packet that was sent.
  1726. */
  1727.  
  1728. int
  1729. sdata() {
  1730.     int i, x, len;
  1731.     
  1732.     debug(F101,"sdata entry, first","",first);
  1733.     debug(F101," drain","",drain);
  1734.  
  1735. /* The "drain" flag is used with window size > 1.  It means we have sent  */
  1736. /* our last data packet.  If called and drain is not zero, then we return */
  1737. /* 0 as if we had sent an empty data packet, until all data packets have  */
  1738. /* been ACK'd, then then we can finally return -1 indicating EOF, so that */
  1739. /* the protocol can switch to seof state.  This is a kludge, but at least */
  1740. /* it's localized...  */
  1741.  
  1742.     if (first == 1) drain = 0;        /* Start of file, init drain flag. */
  1743.  
  1744.     if (drain) {            /* If draining... */
  1745.     debug(F101,"sdata draining, winlo","",winlo);
  1746.     if (winlo == pktnum)        /* If all data packets are ACK'd */
  1747.       return(-1);            /* return EOF indication */
  1748.     else                /* otherwise */
  1749.       return(0);            /* pretend we sent a data packet. */
  1750.     }
  1751.     debug(F101,"sdata sbufnum","",sbufnum);
  1752.     for (i = sbufnum; i > 0; i--) {
  1753.         debug(F101,"sdata countdown","",i);
  1754.     x = nxtpkt();            /* Get next pkt number and buffer */
  1755.     debug(F101,"sdata packet","",pktnum);
  1756.     if (x < 0) return(0);
  1757. /***    dumpsbuf(); */
  1758.     if (cxseen || czseen) {        /* If interrupted, done. */
  1759.         if (wslots > 1) {
  1760.         drain = 1;
  1761.         debug(F101,"sdata cx/zseen, drain","",cxseen);
  1762.         return(0);
  1763.         } else {
  1764.         return(-1);
  1765.         }
  1766.     }
  1767. #ifdef COMMENT
  1768.     if (spsiz > 94)            /* Fill the packet's data buffer */
  1769.       len = getpkt(spsiz-bctl-6,1);    /* long packet */
  1770.     else                /*  or */
  1771.       len = getpkt(spsiz-bctl-3,1);    /* short packet */
  1772. #else
  1773.     len = getpkt(spsiz,1);
  1774. #endif /* COMMENT */
  1775.     if (len == 0) {            /* Done if no data. */
  1776.         if (pktnum == winlo) return(-1);
  1777.         drain = 1;            /* But can't return -1 until all */
  1778.         debug(F101,"sdata eof, drain","",drain);
  1779.         return(0);            /* ACKs are drained. */
  1780.     }
  1781.     spack('D',pktnum,len,data);    /* Send the data packet. */
  1782.     x = ttchk();            /* Peek at input buffer. */
  1783.     debug(F101,"sdata ttchk","",x);    /* ACKs waiting, maybe?  */
  1784. /*
  1785.   Here we check to see if any ACKs or NAKs have arrived, in which case we
  1786.   break out of the D-packet-sending loop and return to the state switcher
  1787.   to process them.  This is what makes our windows slide instead of lurch.
  1788. */
  1789.     if (
  1790. #ifdef GEMDOS
  1791. /*
  1792.   In the Atari ST version, ttchk() can only return 0 or 1.  But note: x will
  1793.   probably always be > 0, since the as-yet-unread packet terminator from the
  1794.   last packet is probably still in the buffer, so sliding windows will
  1795.   probably never happen when the Atari ST is the file sender.  The alternative
  1796.   is to say "if (0)", in which case the ST will always send a window full of
  1797.   packets before reading any ACKs or NAKs.
  1798. */
  1799.         x > 0
  1800.   
  1801. #else /* !GEMDOS */
  1802. /*
  1803.   In other versions, ttchk() returns the actual count.
  1804.   It can't be a Kermit packet if it's less than five bytes long.
  1805. */
  1806.         x > 4
  1807.   
  1808. #endif /* GEMDOS */
  1809.         )
  1810.       return(1);            /* Yes, stop sending data packets */
  1811.     }                    /* and go try to read the ACKs. */
  1812.     return(1);
  1813. }
  1814.  
  1815.  
  1816. /*  S E O F -- Send an End-Of-File packet */
  1817.  
  1818. /*  Call with a string pointer to character to put in the data field, */
  1819. /*  or else a null pointer or "" for no data.  */
  1820.  
  1821. /*
  1822.   There are two "send-eof" functions.  seof() is used to send the normal eof
  1823.   packet at the end of a file's data (even if the file has no data), or when
  1824.   a file transfer is interrupted.  sxeof() is used to send an EOF packet that
  1825.   occurs because of attribute refusal.  The difference is purely a matter of
  1826.   buffer allocation and packet sequence number management.  Both functions
  1827.   act as "front ends" to the common send-eof function, szeof().
  1828. */
  1829.  
  1830. /* Code common to both seof() and sxeof() */
  1831.  
  1832. int
  1833. szeof(s) CHAR *s; {
  1834.     lsstate = 0;            /* Cancel locking-shift state */
  1835.     if ((s != NULL) && (*s != '\0')) {
  1836.     spack('Z',pktnum,1,s);
  1837.     xitsta |= W_SEND;
  1838.     tlog(F100," *** interrupted, sending discard request","",0L);
  1839.     filrej++;
  1840.     } else {
  1841.     spack('Z',pktnum,0,(CHAR *)"");
  1842.     }
  1843.     discard = 0;            /* Turn off per-file discard flag */
  1844.     return(0);
  1845. }
  1846.  
  1847. int
  1848. seof(s) CHAR *s; {
  1849.  
  1850. /*
  1851.   ckcpro.w, before calling seof(), sets window size back to 1 and then calls
  1852.   window(), which clears out the old buffers.  This is OK because the final
  1853.   data packet for the file has been ACK'd.  However, sdata() has already
  1854.   called nxtpkt(), which set the new value of pktnum which seof() will use.
  1855.   So all we need to do here is is allocate a new send-buffer.
  1856. */
  1857.     if (getsbuf(pktnum) < 0) {    /* Get a buffer for packet n */
  1858.     debug(F101,"seof can't get s-buffer","",pktnum);
  1859.     return(-1);
  1860.     }
  1861.     return(szeof(s));
  1862. }
  1863.  
  1864. /*
  1865.   Version of seof() to be called when sdata() has not been called before.  The
  1866.   difference is that this version calls nxtpkt() to allocate a send-buffer and
  1867.   get the next packet number.
  1868. */
  1869. int
  1870. sxeof(s) CHAR *s; {
  1871.     int x;
  1872.     x = nxtpkt();            /* Get next pkt number and buffer */
  1873.     if (x < 0)
  1874.       debug(F101,"sxeof nxtpkt fails","",pktnum);
  1875.     else
  1876.       debug(F101,"sxeof packet","",pktnum);
  1877.     return(szeof(s));
  1878. }
  1879.  
  1880. /*  S E O T -- Send an End-Of-Transaction packet */
  1881.  
  1882. int
  1883. seot() {
  1884.     if (nxtpkt() < 0) return(-1);    /* Bump packet number, get buffer */
  1885.     spack('B',pktnum,0,(CHAR *)"");    /* Send the EOT packet */
  1886.     cxseen = czseen = discard = 0;    /* Reset interruption flags */
  1887.     tstats();                /* Log timing info */
  1888.     return(0);
  1889. }
  1890.  
  1891.  
  1892. /*   R P A R -- Fill the data array with my send-init parameters  */
  1893.  
  1894. CHAR dada[20];                /* Use this instead of data[]. */
  1895.                     /* To avoid some kind of wierd */
  1896.                     /* addressing foulup in spack()... */
  1897.                     /* (which might be fixed now...) */
  1898.  
  1899. CHAR *
  1900. rpar() {
  1901.     if (rpsiz > MAXPACK)        /* Biggest normal packet I want. */
  1902.       dada[0] = tochar(MAXPACK);    /* If > 94, use 94, but specify */
  1903.     else                /* extended packet length below... */
  1904.       dada[0] = tochar(rpsiz);        /* else use what the user said. */
  1905.     dada[1] = tochar(chktimo(pkttim,0)); /* When I want to be timed out */
  1906.     dada[2] = tochar(mypadn);        /* How much padding I need (none) */
  1907.     dada[3] = ctl(mypadc);        /* Padding character I want */
  1908.     dada[4] = tochar(eol);        /* End-Of-Line character I want */
  1909.     dada[5] = myctlq;            /* Control-Quote character I send */
  1910.  
  1911.     switch (rqf) {            /* 8th-bit prefix (single-shift) */
  1912.       case -1:                /* I'm opening the bids */
  1913.       case  1:                /* Other Kermit already bid 'Y' */
  1914.     if (parity) ebq = sq = MYEBQ ;    /* So I reply with '&' if parity */
  1915.     break;                /*  otherwise with 'Y'. */
  1916.       case  0:                /* Other Kermit bid nothing */
  1917.       case  2:                /* Other Kermit sent a valid prefix */
  1918.     break;                /* So I reply with 'Y'. */
  1919.     }
  1920.     debug(F000,"rpar 8bq sq","",sq);
  1921.     debug(F000,"rpar 8bq ebq","",ebq);
  1922.     if (lscapu == 2)            /* LOCKING-SHIFT FORCED */
  1923.       dada[6] = 'N';            /* requires no single-shift */
  1924.     else                /* otherwise send prefix or 'Y' */
  1925.       dada[6] = sq;
  1926.     ebqsent = dada[6];            /* And remember what I really sent */
  1927.  
  1928.     dada[7] = (bctr == 4) ? 'B' : bctr + '0'; /* Block check type */
  1929.     if (rptena) {
  1930.     if (rptflg)            /* Run length encoding */
  1931.       dada[8] = rptq;        /* If receiving, agree */
  1932.     else                /* by replying with same character. */
  1933.       dada[8] = rptq = myrptq;    /* When sending use this. */
  1934.     } else dada[8] = SP;        /* Not enabled, put a space here. */
  1935.  
  1936.     /* CAPAS mask */
  1937.  
  1938.     dada[9] = tochar((lscapr ? lscapb : 0) | /* Locking shifts */
  1939.              (atcapr ? atcapb : 0) | /* Attribute packets */
  1940.              (lpcapr ? lpcapb : 0) | /* Long packets */
  1941.              (swcapr ? swcapb : 0) | /* Sliding windows */
  1942.              (rscapr ? rscapb : 0)); /* RESEND */
  1943.     dada[10] = tochar(swcapr ? wslotr : 1);  /* CAPAS+1 = Window size */
  1944.     if (urpsiz > 94)
  1945.       rpsiz = urpsiz - 1;        /* Long packets ... */
  1946.     dada[11] = tochar(rpsiz / 95);    /* Long packet size, big part */
  1947.     dada[12] = tochar(rpsiz % 95);    /* Long packet size, little part */
  1948. #ifndef WHATAMI
  1949.     dada[13] = '\0';            /* Terminate the init string */
  1950. #else
  1951.     dada[13] = '0';            /* CAPAS+4 = WONT CHKPNT */
  1952.     dada[14] = '_';            /* CAPAS+5 = CHKINT (reserved) */
  1953.     dada[15] = '_';            /* CAPAS+6 = CHKINT (reserved) */
  1954.     dada[16] = '_';            /* CAPAS+7 = CHKINT (reserved) */
  1955.     dada[17] = tochar( WM_FLAG   |    /* CAPAS+8 = WHATAMI... */
  1956.       (server ? WM_SERVE : 0)    |    /* I am (not) a server */
  1957.     (binary ? WM_FMODE : 0)  |    /*  My file transfer mode is ... */
  1958.       (fncnv ? WM_FNAME : 0));     /*    My filename conversion is ... */
  1959.     dada[18] = NUL;            /* Terminate the init string */
  1960. #endif /* WHATAMI */
  1961.  
  1962. #ifdef DEBUG
  1963.     if (deblog) {
  1964.     debug(F110,"rpar",dada,0);
  1965.     rdebu(dada,(int)strlen((char *)dada));
  1966.     }
  1967. #endif /* DEBUG */
  1968.     strcpy((char *)myinit,(char *)dada);
  1969.     return(dada);            /* Return pointer to string. */
  1970. }
  1971.  
  1972. int
  1973. spar(s) CHAR *s; {            /* Set parameters */
  1974.     int x, y, lpsiz;
  1975.  
  1976.     debug(F110,"entering spar",s,0);
  1977.  
  1978.     s--;                /* Line up with field numbers. */
  1979.  
  1980. /* Limit on size of outbound packets */
  1981.     x = (rln >= 1) ? xunchar(s[1]) : 80;
  1982.     lpsiz = spsizr;            /* Remember what they SET. */
  1983.     if (spsizf) {            /* SET-command override? */
  1984.     if (x < spsizr) spsiz = x;    /* Ignore LEN unless smaller */
  1985.     } else {                /* otherwise */
  1986.     spsiz = (x < 10) ? 80 : x;    /* believe them if reasonable */
  1987.     }
  1988.     spmax = spsiz;            /* Remember maximum size */
  1989.  
  1990. /* Timeout on inbound packets */
  1991.     if (timef) {
  1992.     timint = rtimo;            /* SET SEND TIMEOUT value overrides */
  1993.     } else {                /* Otherwise use requested value, */
  1994.     x = (rln >= 2) ? xunchar(s[2]) : rtimo; /* if it is legal. */
  1995.     timint = (x < 0) ? rtimo : x;
  1996.     }
  1997.     timint = chktimo(timint,timef);    /* Adjust if necessary */
  1998.  
  1999. /* Outbound Padding */
  2000.     npad = 0; padch = '\0';
  2001.     if (rln >= 3) {
  2002.     npad = xunchar(s[3]);
  2003.     if (rln >= 4) padch = ctl(s[4]); else padch = 0;
  2004.     }
  2005.     if (npad) {
  2006.     int i;
  2007.     for (i = 0; i < npad; i++) padbuf[i] = dopar(padch);
  2008.     }
  2009.  
  2010. /* Outbound Packet Terminator */
  2011.     seol = (rln >= 5) ? xunchar(s[5]) : CR;
  2012.     if ((seol < 1) || (seol > 31)) seol = CR;
  2013.  
  2014. /* Control prefix that the other Kermit is sending */
  2015.     x = (rln >= 6) ? s[6] : '#';
  2016.     ctlq = ((x > 32 && x < 63) || (x > 95 && x < 127)) ? x : '#';
  2017.  
  2018. /* 8th-bit prefix */
  2019. /*
  2020.   NOTE: Maybe this could be simplified using rcvtyp.
  2021.   If rcvtyp == 'Y' then we're reading the ACK,
  2022.   otherwise we're reading the other Kermit's initial bid.
  2023.   But his horrendous code works, so leave it alone for now.
  2024. */
  2025.     rq = (rln >= 7) ? s[7] : 0;
  2026.     if (rq == 'Y') rqf = 1;
  2027.       else if ((rq > 32 && rq < 63) || (rq > 95 && rq < 127)) rqf = 2;
  2028.         else rqf = 0;
  2029.     debug(F000,"spar 8bq rq","",rq);
  2030.     debug(F000,"spar 8bq sq","",sq);
  2031.     debug(F000,"spar 8bq ebq","",ebq);
  2032.     debug(F101,"spar 8bq rqf","",rqf);
  2033.     switch (rqf) {
  2034.       case 0:                /* Field is missing from packet. */
  2035.     ebqflg = 0;            /* So no 8th-bit prefixing. */
  2036.     break;
  2037.       case 1:                /* Other Kermit sent 'Y' = Will Do. */
  2038.     /*
  2039.           When I am the file receiver, ebqsent is 0 because I didn't send a
  2040.           negotiation yet.  If my parity is set to anything other than NONE,
  2041.           either because my user SET PARITY or because I detected parity bits
  2042.           on this packet, I reply with '&', otherwise 'Y'.
  2043.  
  2044.       When I am the file sender, ebqsent is what I just sent in rpar(),
  2045.           which can be 'Y', 'N', or '&'.  If I sent '&', then this 'Y' means
  2046.           the other Kermit agrees to do 8th-bit prefixing.
  2047.  
  2048.           If I sent 'Y' or 'N', but then detected parity on the ACK packet
  2049.           that came back, then it's too late: there is no longer any way for
  2050.           me to tell the other Kermit that I want to do 8th-bit prefixing, so
  2051.           I must not do it, and in that case, if there is any 8-bit data in 
  2052.           the file to be transferred, the transfer will fail because of block
  2053.           check errors.
  2054.  
  2055.           The following clause covers all of these situations:
  2056.     */
  2057.     if (parity && (ebqsent == 0 || ebqsent == '&')) {
  2058.         ebqflg = 1;
  2059.         ebq = MYEBQ;
  2060.     }
  2061.     break;
  2062.       case 2:                /* Other Kermit send a valid prefix */
  2063.     if (ebqflg = (ebq == sq || sq == 'Y'))
  2064.       ebq = rq;
  2065.     }
  2066.     if (lscapu == 2) {     /* But no single-shifts if LOCKING-SHIFT FORCED */
  2067.     ebqflg = 0;
  2068.     ebq = 'N';
  2069.     }
  2070.  
  2071. /* Block check */
  2072.     x = 1;
  2073.     if (rln >= 8) {
  2074.     if (s[8] == 'B') x = 4;
  2075.     else x = s[8] - '0';
  2076.     if ((x < 1) || (x > 4)) x = 1;
  2077.     }
  2078.     bctr = x;
  2079.  
  2080. /* Repeat prefix */
  2081.  
  2082.     rptflg = 0;                /* Assume no repeat-counts */
  2083.     if (rln >= 9) {            /* Is there a repeat-count field? */
  2084.     char t;                /* Yes. */
  2085.     t = s[9];            /* Get its contents. */
  2086. /*
  2087.   If I'm sending files, then I'm reading these parameters from an ACK, and so
  2088.   this character must agree with what I sent.
  2089. */
  2090.     if (rptena) {            /* If enabled ... */
  2091.         if ((char) rcvtyp == 'Y') {    /* Sending files, reading ACK. */
  2092.         if (t == myrptq) rptflg = 1;
  2093.         } else {            /* I'm receiving files */
  2094.         if ((t > 32 && t < 63) || (t > 95 && t < 127)) {
  2095.             rptflg = 1;
  2096.             rptq = t;
  2097.         }
  2098.         }
  2099.     } else rptflg = 0;
  2100.     }
  2101.  
  2102. /* Capabilities */
  2103.  
  2104.     atcapu = lpcapu = swcapu = rscapu = 0; /* Assume none of these. */
  2105.     if (lscapu != 2) lscapu = 0;    /* Assume no LS unless forced. */
  2106.     y = 11;                /* Position of next field, if any */
  2107.     if (rln >= 10) {
  2108.         x = xunchar(s[10]);
  2109.     debug(F101,"spar capas","",x);
  2110.         atcapu = (x & atcapb) && atcapr; /* Attributes */
  2111.     lpcapu = (x & lpcapb) && lpcapr; /* Long packets */
  2112.     swcapu = (x & swcapb) && swcapr; /* Sliding windows */
  2113.     rscapu = (x & rscapb) && rscapr; /* RESEND */
  2114.     debug(F101,"spar lscapu","",lscapu);
  2115.     debug(F101,"spar lscapr","",lscapr);
  2116.     debug(F101,"spar ebqflg","",ebqflg);
  2117.     if (lscapu != 2) lscapu = ((x & lscapb) && lscapr && ebqflg) ? 1 : 0;
  2118.     debug(F101,"spar swcapr","",swcapr);
  2119.     debug(F101,"spar swcapu","",swcapu);
  2120.     debug(F101,"spar lscapu","",lscapu);
  2121.     for (y = 10; (xunchar(s[y]) & 1) && (rln >= y); y++) ;
  2122.     debug(F101,"spar y","",y);
  2123.     }
  2124.  
  2125. /* Long Packets */
  2126.     debug(F101,"spar lpcapu","",lpcapu);
  2127.     if (lpcapu) {
  2128.         if (rln > y+1) {
  2129.         x = xunchar(s[y+2]) * 95 + xunchar(s[y+3]);
  2130.         debug(F101,"spar lp len","",x);
  2131.         if (spsizf) {        /* If overriding negotiations */
  2132.         spsiz = (x < lpsiz) ? x : lpsiz; /* do this, */
  2133.         } else {                     /* otherwise */
  2134.         spsiz = (x > MAXSP) ? MAXSP : x; /* do this. */
  2135.         }
  2136.         if (spsiz < 10) spsiz = 80;    /* Be defensive... */
  2137.     }
  2138.     }
  2139.     /* (PWP) save current send packet size for optimal packet size calcs */
  2140.     spmax = spsiz;
  2141.     debug(F101,"spar lp spmax","",spmax);
  2142.     timint = chktimo(timint,timef);    /* Recalculate the packet timeout! */
  2143.     
  2144. /* Sliding Windows... */
  2145.  
  2146.     if (swcapr) {            /* Only if requested... */
  2147.         if (rln > y) {            /* See what other Kermit says */
  2148.         x = xunchar(s[y+1]);
  2149.         debug(F101,"spar window","",x);
  2150.         wslotn = (x > MAXWS) ? MAXWS : x;
  2151. /*
  2152.   wslotn = negotiated size (from other Kermit's S or I packet).
  2153.   wslotr = requested window size (from this Kermit's SET WINDOW command).
  2154. */
  2155.         if (wslotn > wslotr)    /* Use the smaller of the two */
  2156.           wslotn = wslotr;
  2157.         if (wslotn < 1)        /* Watch out for bad negotiation */
  2158.           wslotn = 1;
  2159.         if (wslotn > 1) {
  2160.         swcapu = 1;        /* We do windows... */
  2161.         if (wslotn > maxtry)    /* Retry limit must be greater */
  2162.           maxtry = wslotn + 1;    /* than window size. */
  2163.         }
  2164.         debug(F101,"spar window after adjustment","",x);
  2165.     } else {            /* No window size specified. */
  2166.         wslotn = 1;            /* We don't do windows... */
  2167.         debug(F101,"spar window","",x);
  2168.         swcapu = 0;
  2169.         debug(F101,"spar no windows","",wslotn);
  2170.     }
  2171.     }
  2172.  
  2173. /* Now recalculate packet length based on number of windows.   */
  2174. /* The nogotiated number of window slots will be allocated,    */
  2175. /* and the maximum packet length will be reduced if necessary, */
  2176. /* so that a windowful of packets can fit in the big buffer.   */
  2177.  
  2178.     if (wslotn > 1) {            /* Shrink to fit... */
  2179.     x = adjpkl(spsiz,wslotn,bigsbsiz);
  2180.     if (x < spsiz) {
  2181.         spsiz = spmax = x;
  2182.         debug(F101,"spar sending, redefine spsiz","",spsiz);
  2183.     }
  2184.     }
  2185. #ifdef WHATAMI
  2186.     if (rln > y+7)            /* Get WHATAMI info if any */
  2187.       whatru = xunchar(s[y+8]);
  2188. #endif /* WHATAMI */
  2189.  
  2190. /* Record parameters in debug log */
  2191. #ifdef DEBUG
  2192.     if (deblog) sdebu(rln);
  2193. #endif /* DEBUG */
  2194.     numerrs = 0;            /* Start counting errors here. */
  2195.     return(0);
  2196. }
  2197.  
  2198. /*  G N F I L E  --  Get name of next file to send  */
  2199. /*
  2200.   Expects global sndsrc to be:
  2201.    -1: next filename to be obtained by calling znext().
  2202.     0: no next file name
  2203.     1: (or greater) next filename to be obtained from **cmlist.
  2204.   Returns:
  2205.     1, with name of next file in filnam.
  2206.     0, no more files, with filnam set to empty string.
  2207.    -1, file not found
  2208.    -2, file is not readable
  2209.    -3, read access denied
  2210.    -4, cancelled
  2211.    -5, too many files match wildcard
  2212. */
  2213.  
  2214. int
  2215. gnfile() {
  2216.     int x; long y;
  2217.     int retcode = 0;
  2218.  
  2219.     debug(F101,"gnfile sndsrc","",sndsrc);
  2220.     fsize = -1L;            /* Initialize file size */
  2221.     if (sndsrc == 0) {            /* It's not really a file */
  2222.     if (nfils > 0) {        /* It's a pipe, or stdin */
  2223.         strcpy(filnam, *cmlist);    /* Copy its "name" */
  2224.         nfils = 0;            /* There is no next file */
  2225.         return(1);            /* OK this time */
  2226.     } else return(0);        /* but not next time */
  2227.     }
  2228.  
  2229. /* If file group interruption (C-Z) occurred, fail.  */
  2230.  
  2231.     if (czseen) {
  2232.     tlog(F100,"Transaction cancelled","",0L);
  2233.         debug(F100,"gnfile czseen","",0);
  2234.     return(-4);
  2235.     }
  2236.  
  2237. /* Loop through file list till we find a readable, sendable file */
  2238.  
  2239.     y = -1L;                /* Loop exit (file size) variable */
  2240.     while (y < 0L) {            /* Keep trying till we get one... */
  2241.     if (sndsrc > 0) {        /* File list in cmlist */
  2242.         debug(F101,"gnfile nfils","",nfils);
  2243.         if (nfils-- > 0) {        /* Still some left? */
  2244.         strcpy(filnam,*cmlist++);
  2245.         debug(F111,"gnfile cmlist filnam",filnam,nfils);
  2246.         if (!clfils) {        /* Expand only if not from cmdline */
  2247.             x = zxpand(filnam);
  2248.             debug(F101,"gnfile zxpand","",x);
  2249.             if (x == 1) {
  2250.             znext(filnam);
  2251.             goto gotnam;
  2252.             }
  2253.             if (x == 0) {
  2254.             retcode = -1; /* None match */
  2255.             continue;
  2256.             }
  2257.             if (x < 0) return(-5); /* Too many to expand */
  2258.             sndsrc = -1;    /* Change send-source to znext() */
  2259.         }
  2260.         } else {            /* We're out of files. */
  2261.         debug(F101,"gnfile done","",nfils);
  2262.         *filnam = '\0';
  2263.         return(retcode);
  2264.         }
  2265.     }
  2266.  
  2267. /* Otherwise, step to next element of internal wildcard expansion list. */
  2268.  
  2269.     if (sndsrc < 0) {
  2270.         x = znext(filnam);
  2271.         debug(F111,"gnfile znext",filnam,x);
  2272.         if (x == 0) {        /* If no more, */
  2273.         sndsrc = 1;        /* go back to list */
  2274.         continue;
  2275.         }
  2276.     }
  2277.  
  2278. /* Get here with a filename. */
  2279.  
  2280. gotnam:
  2281.     if (sndsrc) {
  2282.         y = zchki(filnam);        /* Check if file readable */
  2283.         retcode = (int) y;        /* Possible return code */
  2284.         if (y == -1L) {        /* If not found */
  2285.         debug(F110,"gnfile skipping:",filnam,0);
  2286.         tlog(F111,filnam,"not sent, reason",(long)y);
  2287.         screen(SCR_ST,ST_SKIP,0l,filnam);
  2288.         continue;
  2289.         } else if (y < 0) {
  2290.         continue;
  2291.         } else {
  2292.         fsize = y;
  2293.         return(1);
  2294.         }
  2295.     } else return(1);        /* sndsrc is 0... */
  2296.     }
  2297.     *filnam = '\0';            /* Should never get here */
  2298.     return(0);
  2299. }
  2300.  
  2301. /*  S N D H L P  --  Routine to send builtin help  */
  2302.  
  2303. int
  2304. sndhlp(p) char *p; {
  2305. #ifndef NOSERVER
  2306.     nfils = 0;                /* No files, no lists. */
  2307.     xflg = 1;                /* Flag we must send X packet. */
  2308.     strcpy(cmdstr,versio);        /* Data for X packet. */
  2309.     first = 1;                /* Init getchx lookahead */
  2310.     memstr = 1;                /* Just set the flag. */
  2311.     memptr = p;                /* And the pointer. */
  2312.     if (binary) {            /* If file mode is binary, */
  2313.     savmod = binary;        /*  remember to restore it later. */
  2314.     binary = XYFT_T;            /*  turn it back to text for this, */
  2315.     }
  2316.     return(sinit());
  2317. #else
  2318.     return(0);
  2319. #endif /* NOSERVER */
  2320. }
  2321.  
  2322. #ifdef OS2
  2323. /*  S N D S P A C E -- send disk space message  */
  2324. int
  2325. sndspace(int drive) {
  2326. #ifndef NOSERVER
  2327.     static char spctext[64];
  2328.     if (drive)
  2329.       sprintf(spctext, " Drive %c: %ldK free\n", drive, 
  2330.           zdskspace(drive - 'A' + 1) / 1024L);
  2331.     else
  2332.       sprintf(spctext, " Free space: %ldK\n", zdskspace(0)/1024L);
  2333.     nfils = 0;            /* No files, no lists. */
  2334.     xflg = 1;            /* Flag we must send X packet. */
  2335.     strcpy(cmdstr,"free space");/* Data for X packet. */
  2336.     first = 1;            /* Init getchx lookahead */
  2337.     memstr = 1;            /* Just set the flag. */
  2338.     memptr = spctext;        /* And the pointer. */
  2339.     if (binary) {        /* If file mode is binary, */
  2340.         savmod = binary;    /*  remember to restore it later. */
  2341.         binary = XYFT_T;        /*  turn it back to text for this, */
  2342.     }
  2343.     return(sinit());
  2344. #else
  2345.     return(0);
  2346. #endif /* NOSERVER */
  2347. }
  2348. #endif /* OS2 */
  2349.  
  2350. /*  C W D  --  Change current working directory  */
  2351.  
  2352. /*
  2353.  String passed has first byte as length of directory name, rest of string
  2354.  is name.  Fails if can't connect, else ACKs (with name) and succeeds. 
  2355. */
  2356.  
  2357. int
  2358. cwd(vdir) char *vdir; {
  2359.     char *cdd, *zgtdir(), *dirp;
  2360.  
  2361.     vdir[xunchar(*vdir) + 1] = '\0';    /* Terminate string with a null */
  2362.     dirp = vdir+1;
  2363.     tlog(F110,"Directory requested: ",dirp,0L);
  2364.     if (zchdir(dirp)) {        /* Try to change */
  2365.     cdd = zgtdir();        /* Get new working directory. */
  2366.     debug(F110,"cwd",cdd,0);
  2367.     encstr((CHAR *)cdd);
  2368. #ifdef COMMENT
  2369.     ack1((CHAR *)(encbuf+7));
  2370. #else
  2371.     ack1(data);
  2372. #endif /* COMMENT */
  2373.     screen(SCR_CD,0,0l,cdd);
  2374.     tlog(F110,"Changed directory to",cdd,0L);
  2375.     return(1); 
  2376.     } else {
  2377.     debug(F110,"cwd failed",dirp,0);
  2378.     tlog(F110,"Failed to change directory to",dirp,0L);
  2379.     return(0);
  2380.     }
  2381. }
  2382.  
  2383.  
  2384. /*  S Y S C M D  --  Do a system command  */
  2385.  
  2386. /*  Command string is formed by concatenating the two arguments.  */
  2387.  
  2388. int
  2389. syscmd(prefix,suffix) char *prefix, *suffix; {
  2390.     char *cp;
  2391.  
  2392.     if (!prefix)
  2393.       return(0);
  2394.     if (!*prefix)
  2395.       return(0);
  2396.     for (cp = cmdstr; *prefix != '\0'; *cp++ = *prefix++) ;
  2397.     while (*cp++ = *suffix++) ;        /* copy suffix */
  2398.  
  2399.     debug(F110,"syscmd",cmdstr,0);
  2400.     if (zxcmd(ZIFILE,cmdstr) > 0) {
  2401.     debug(F110,"syscmd zxcmd ok",cmdstr,0);
  2402.     nfils = sndsrc = 0;        /* Flag that input is from stdin */
  2403.     xflg = hcflg = 1;        /* And special flags for pipe */
  2404.     if (binary) {            /* If file mode is binary, */
  2405.         savmod = binary;        /*  remember to restore it later. */
  2406.         binary = XYFT_T;            /*  turn it back to text for this, */
  2407.     }
  2408.     return (sinit());        /* Send S packet */
  2409.     } else {
  2410.     debug(F100,"syscmd zxcmd failed",cmdstr,0);
  2411.     return(0);
  2412.     }
  2413. }
  2414.  
  2415. /*  R E M S E T  --  Remote Set  */
  2416. /*  Called by server to set variables as commanded in REMOTE SET packets.  */
  2417. /*  Returns 1 on success, 0 on failure.  */
  2418.  
  2419. int
  2420. remset(s) char *s; {
  2421.     int len, i, x, y;
  2422.     char *p;
  2423.  
  2424.     len = xunchar(*s++);        /* Length of first field */
  2425.     p = s + len;            /* Pointer to second length field */
  2426.     *p++ = '\0';            /* Zero out second length field */
  2427.     x = atoi(s);            /* Value of first field */
  2428.     debug(F111,"remset",s,x);
  2429.     debug(F110,"remset",p,0);
  2430.     switch (x) {            /* Do the right thing */
  2431.       case 132:                /* Attributes (all, in) */
  2432.     atcapr = atoi(p);
  2433.     return(1);
  2434.       case 133:                /* File length attributes */
  2435.       case 233:                /* IN/OUT combined */
  2436.       case 148:                /* Both kinds of lengths */
  2437.       case 248:
  2438.     atleni = atleno = atoi(p);
  2439.     return(1);
  2440.       case 134:                /* File Type (text/binary) */
  2441.       case 234:
  2442.     attypi = attypo = atoi(p);
  2443.     return(1);
  2444.       case 135:                /* File creation date */
  2445.       case 235:
  2446.     atdati = atdato = atoi(p);
  2447.     return(1);
  2448.       case 139:                /* File Blocksize */
  2449.       case 239:
  2450.     atblki = atblko = atoi(p);
  2451.     return(1);
  2452.       case 141:                /* Encoding / Character Set */
  2453.       case 241:
  2454.     atenci = atenco = atoi(p);
  2455.     return(1);
  2456.       case 142:                /* Disposition */
  2457.       case 242:
  2458.     atdisi = atdiso = atoi(p);
  2459.     return(1);
  2460.       case 145:                /* System ID */
  2461.       case 245:
  2462.     atsidi = atsido = atoi(p);
  2463.     return(1);
  2464.       case 147:                /* System-Dependent Info */
  2465.       case 247:
  2466.     atsysi = atsyso = atoi(p);
  2467.     return(1);
  2468.       case 232:                /* Attributes (all, out) */
  2469.     atcapr = atoi(p);
  2470.     return(1);
  2471.       case 300:                /* File type (text, binary) */
  2472.     binary = atoi(p);
  2473.     return(1);
  2474.       case 301:                /* File name conversion */
  2475.     fncnv = 1 - atoi(p);        /* (oops) */
  2476.     return(1);
  2477.       case 302:                /* File name collision */
  2478.     x = atoi(p);
  2479.     if (x == XYFX_R) warn = 1;    /* Rename */
  2480.     if (x == XYFX_X) warn = 0;    /* Replace */
  2481.     fncact = x;
  2482.     return(1);
  2483.       case 310:                /* Incomplete File Disposition */
  2484.     keep = atoi(p);            /* Keep, Discard */
  2485.     return(1);
  2486.       case 311:                /* Blocksize */
  2487.     fblksiz = atoi(p);
  2488.     return(1);
  2489.       case 312:                /* Record Length */
  2490.     frecl = atoi(p);
  2491.     return(1);
  2492.       case 313:                /* Record format */
  2493.     frecfm = atoi(p);
  2494.     return(1);
  2495.       case 314:                /* File organization */
  2496.     forg = atoi(p);
  2497.     return(1);
  2498.       case 315:                /* File carriage control */
  2499.     fcctrl = atoi(p);
  2500.     return(1);
  2501.       case 400:                /* Block check */
  2502.     y = atoi(p);
  2503.     if (y < 5 && y > 0) {
  2504.         bctr = y;
  2505.         return(1);
  2506.     } else if (*p == 'B') {
  2507.         bctr = 4;
  2508.         return(1);
  2509.     }
  2510.     return(0);
  2511.       case 401:                /* Receive packet-length */
  2512.     rpsiz = urpsiz = atoi(p);
  2513.     if (urpsiz > MAXRP) urpsiz = MAXRP; /* Max long-packet length */
  2514.     if (rpsiz > 94) rpsiz = 94;        /* Max short-packet length */
  2515.     urpsiz = adjpkl(urpsiz,wslots,bigrbsiz);
  2516.     return(1);
  2517.       case 402:                /* Receive timeout */
  2518.     y = atoi(p);            /* Client is telling us */
  2519.     if (y > -1 && y < 999) {    /* the timeout that it wants */
  2520.         pkttim = chktimo(y,timef);    /* us to tell it to use. */
  2521.         return(1);
  2522.     } else return(0);
  2523.       case 403:                /* Retry limit */
  2524.     y = atoi(p);
  2525.     if (y > -1 && y < 95) {
  2526.         maxtry = y;
  2527.         return(1);
  2528.     } else return(0);
  2529.       case 404:                /* Server timeout */
  2530.     y = atoi(p);
  2531.     if (y < 0) return(0);
  2532.     srvtim = y;
  2533.     return(1);
  2534.  
  2535. #ifndef NOCSETS
  2536.       case 405:                /* Transfer character set */
  2537.     for (i = 0; i < ntcsets; i++) { 
  2538.         if (!strcmp(tcsinfo[i].designator,p)) break;
  2539.     }
  2540.     debug(F101,"remset xfer charset lookup","",i);
  2541.     if (i == ntcsets) return(0);
  2542.     tcharset = tcsinfo[i].code;    /* if known, use it */
  2543.     if (tcharset == TC_TRANSP)
  2544.       rx = NULL;
  2545.     else
  2546.       rx = xlr[tcharset][fcharset];    /* translation function */
  2547.     return(1);
  2548. #endif /* NOCSETS */
  2549.  
  2550.       case 406:                /* Window slots */
  2551.     y = atoi(p);
  2552.     if (y == 0) y = 1;
  2553.     if (y < 1 && y > MAXWS) return(0);
  2554.     wslotr = y;
  2555.     swcapr = 1;
  2556.     urpsiz = adjpkl(urpsiz,wslots,bigrbsiz);
  2557.     return(1);
  2558.       default:                /* Anything else... */
  2559.     return(0);
  2560.     }
  2561. }
  2562.  
  2563. /* Adjust packet length based on number of window slots and buffer size */
  2564.  
  2565. int
  2566. adjpkl(pktlen,slots,bufsiz) int pktlen, slots, bufsiz; {
  2567.     debug(F101,"adjpkl len","",pktlen);
  2568.     debug(F101,"adjpkl slots","",slots);
  2569.     debug(F101,"adjpkl bufsiz","",bufsiz);
  2570.     if (((pktlen + 6) * slots) > bufsiz)
  2571.       pktlen = (bufsiz / slots) - 6;
  2572.     debug(F101,"adjpkl new len","",pktlen);
  2573.     return(pktlen);
  2574. }
  2575.