home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv197.zip / ckcfns.c < prev    next >
C/C++ Source or Header  |  2000-01-03  |  198KB  |  6,553 lines

  1. char *fnsv = "C-Kermit functions, 7.0.187, 20 Dec 1999";
  2.  
  3. char *nm[] =  { "Disabled", "Local only", "Remote only", "Enabled" };
  4.  
  5. /*  C K C F N S  --  System-independent Kermit protocol support functions.  */
  6.  
  7. /*  ...Part 1 (others moved to ckcfn2,3 to make this module smaller) */
  8.  
  9. /*
  10.   Author: Frank da Cruz <fdc@columbia.edu>,
  11.   Columbia University Academic Information Systems, New York City.
  12.  
  13.   Copyright (C) 1985, 2000,
  14.     Trustees of Columbia University in the City of New York.
  15.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  16.     copyright text in the ckcmai.c module for disclaimer and permissions.
  17. */
  18. /*
  19.  System-dependent primitives defined in:
  20.  
  21.    ck?tio.c -- terminal (communications) i/o
  22.    cx?fio.c -- file i/o, directory structure
  23. */
  24. #include "ckcsym.h"            /* Needed for Stratus VOS */
  25. #include "ckcasc.h"            /* ASCII symbols */
  26. #include "ckcdeb.h"            /* Debug formats, typedefs, etc. */
  27. #include "ckcker.h"            /* Symbol definitions for Kermit */
  28. #include "ckcxla.h"            /* Character set symbols */
  29. #include "ckcnet.h"                     /* VMS definition of TCPSOCKET */
  30.  
  31. int docrc  = 0;                /* Accumulate CRC for \v(crc16) */
  32. long crc16 = 0L;            /* File CRC = \v(crc16) */
  33.  
  34. extern CHAR feol;
  35. extern int byteorder, xflg, remfile, what, fmask, cxseen, czseen;
  36. extern long ffc;
  37.  
  38. #ifndef NOXFER
  39.  
  40. /* (move these prototypes to the appropriate .h files...) */
  41.  
  42. #ifdef COMMENT
  43. /* Not used */
  44. #ifdef VMS
  45. _PROTOTYP( int getvnum, (char *) );
  46. #endif /* VMS */
  47. #endif /* COMMENT */
  48.  
  49. _PROTOTYP( static int bgetpkt, (int) );
  50. #ifndef NOCSETS
  51. _PROTOTYP( int lookup, (struct keytab[], char *, int, int *) );
  52. #endif /* NOCSETS */
  53. #ifndef NOSPL
  54. _PROTOTYP( int zzstring, (char *, char **, int *) );
  55. #endif /* NOSPL */
  56. #ifdef OS2ORUNIX
  57. _PROTOTYP( long zfsize, (char *) );
  58. #endif /* OS2ORUNIX */
  59.  
  60. #ifdef OS2
  61. #include <io.h>
  62. #endif /* OS2 */
  63.  
  64. #ifdef VMS
  65. #include <errno.h>
  66. #endif /* VMS */
  67.  
  68. /* Externals from ckcmai.c */
  69.  
  70. extern int srvcdmsg, srvidl, idletmo, bigendian;
  71. extern char * cdmsgfile[];
  72. extern int spsiz, spmax, rpsiz, timint, srvtim, rtimo, npad, ebq, ebqflg,
  73.  rpt, rptq, rptflg, capas, keep, fncact, pkttim, autopar, spsizr, xitsta;
  74. extern int pktnum, bctr, bctu, bctl, clfils, sbufnum, protocol,
  75.  size, osize, spktl, nfils, ckwarn, timef, spsizf, sndtyp, rcvtyp, success;
  76. extern int parity, turn, network, whatru, fsecs, justone, slostart,
  77.  ckdelay, displa, mypadn, moving, recursive, nettype;
  78. extern long filcnt, flci, flco, tlci, tlco, tfc, fsize, sendstart, rs_len;
  79. extern long filrej, oldcps, cps, peakcps, ccu, ccp, calibrate, filestatus;
  80. extern int fblksiz, frecl, frecfm, forg, fcctrl, fdispla, skipbup;
  81. extern int spackets, rpackets, timeouts, retrans, crunched, wmax, wcur;
  82. extern int hcflg, binary, fncnv, b_save, f_save, server;
  83. extern int nakstate, discard, rejection, local, xfermode, interrupted;
  84. extern int rq, rqf, sq, wslots, wslotn, wslotr, winlo, urpsiz, rln;
  85. extern int fnspath, fnrpath, eofmethod, diractive, whatru2, wearealike;
  86. extern int atcapr, atcapb, atcapu;
  87. extern int lpcapr, lpcapb, lpcapu;
  88. extern int swcapr, swcapb, swcapu;
  89. extern int lscapr, lscapb, lscapu;
  90. extern int rscapr, rscapb, rscapu;
  91. extern int rptena, rptmin;
  92. extern int sseqtbl[];
  93. extern int numerrs, nzxopts;
  94. extern long rptn;
  95. extern int maxtry;
  96. extern int stdouf;
  97. extern int sendmode;
  98. extern int carrier, ttprty;
  99. #ifdef TCPSOCKET
  100. extern int ttnproto;
  101. #endif /* TCPSOCKET */
  102.  
  103. #ifndef NOSPL
  104. extern int sndxin, sndxhi, sndxlo;
  105. #endif /* NOSPL */
  106.  
  107. extern int g_binary, g_fncnv;
  108.  
  109. #ifdef GFTIMER
  110. extern CKFLOAT fpfsecs;
  111. #endif /* GFTIMER */
  112.  
  113. #ifdef OS2
  114. extern struct zattr iattr;
  115. #endif /* OS2 */
  116.  
  117. #ifdef PIPESEND
  118. extern int pipesend, usepipes;
  119. #endif /* PIPESEND */
  120.  
  121. /* Per-file text/binary-mode recognition */
  122.  
  123. #ifdef PATTERNS
  124. extern int patterns;
  125. extern char *txtpatterns[];
  126. extern char *binpatterns[];
  127. #endif /* PATTERNS */
  128.  
  129. #ifdef STREAMING
  130. extern int streamrq, streaming, streamed, streamok;
  131. #endif /* STREAMING */
  132. extern int reliable, clearrq, cleared, urclear;
  133.  
  134. extern int
  135.   atenci, atenco, atdati, atdato, atleni, atleno, atblki, atblko,
  136.   attypi, attypo, atsidi, atsido, atsysi, atsyso, atdisi, atdiso;
  137.  
  138. extern int bigsbsiz, bigrbsiz;
  139. extern char *versio;
  140. extern char *filefile;
  141. extern char whoareu[], * cksysid;
  142.  
  143. #ifndef NOSERVER
  144. extern int ngetpath;
  145. extern char * getpath[];
  146. extern int fromgetpath;
  147. #endif /* NOSERVER */
  148.  
  149. extern int inserver;
  150. #ifdef CK_LOGIN
  151. extern int isguest;
  152. #endif /* CK_LOGIN */
  153.  
  154. extern CHAR *srvcmd, * epktmsg;
  155. extern CHAR padch, mypadc, eol, seol, ctlq, myctlq, sstate, myrptq;
  156. extern CHAR *data, padbuf[], stchr, mystch;
  157. extern CHAR *srvptr;
  158. extern CHAR *rdatap;
  159. extern char *cmarg, *cmarg2, **cmlist, filnam[];
  160. extern char fspec[], *sfspec, *rfspec;
  161. extern int fspeclen;
  162.  
  163. #ifndef NOMSEND
  164. extern struct filelist * filehead, * filenext;
  165. extern int addlist;
  166. #endif /* NOMSEND */
  167.  
  168. _PROTOTYP( int lslook, (unsigned int b) );    /* Locking Shift Lookahead */
  169. _PROTOTYP( int szeof, (CHAR *s) );
  170. _PROTOTYP( VOID fnlist, (void) );
  171. #endif /* NOXFER */
  172.  
  173. /* Character set Translation */
  174.  
  175. #ifndef NOCSETS
  176. extern int tcharset, fcharset;
  177. extern int ntcsets, xlatype, cseqtab[];
  178. extern struct csinfo tcsinfo[], fcsinfo[];
  179.  
  180. _PROTOTYP( CHAR ident, (CHAR) );    /* Identity translation function */
  181.  
  182. /* Arrays of and pointers to character translation functions */
  183.  
  184. #ifdef CK_ANSIC
  185. extern CHAR (*rx)(CHAR); /* Pointer to input character translation function */
  186. extern CHAR (*sx)(CHAR); /* Pointer to output character translation function */
  187. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Byte-to-Byte Send */
  188. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Byte-to-Byte Recv */
  189. #ifdef UNICODE
  190. extern int (*xut)(USHORT);    /* Translation function UCS to TCS */
  191. extern int (*xuf)(USHORT);    /* Translation function UCS to FCS */
  192. extern USHORT (*xtu)(CHAR);    /* Translation function TCS to UCS */
  193. extern USHORT (*xfu)(CHAR);    /* Translation function FCS to UCS */
  194. #endif /* UNICODE */
  195.  
  196. #else /* The same declarations again for non-ANSI comilers... */
  197.  
  198. extern CHAR (*rx)();
  199. extern CHAR (*sx)();
  200. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])();
  201. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])();
  202. #ifdef UNICODE
  203. extern int (*xut)();
  204. extern int (*xuf)();
  205. extern USHORT (*xtu)();
  206. extern USHORT (*xfu)();
  207. #endif /* UNICODE */
  208. #endif /* CK_ANSIC */
  209. #endif /* NOCSETS */
  210.  
  211. /* (PWP) external def. of things used in buffered file input and output */
  212.  
  213. #ifdef DYNAMIC
  214. extern char *zinbuffer, *zoutbuffer;
  215. #else
  216. extern char zinbuffer[], zoutbuffer[];
  217. #endif /* DYNAMIC */
  218. extern char *zinptr, *zoutptr;
  219. extern int zincnt, zoutcnt, zobufsize;
  220.  
  221. extern long crcta[], crctb[];        /* CRC-16 generation tables */
  222. extern int rseqtbl[];            /* Rec'd-packet sequence # table */
  223.  
  224. #ifndef NOXFER
  225.  
  226. /* Variables defined in this module but shared by other modules. */
  227.  
  228. int xfrbel = 1;
  229. char * ofperms = "";            /* Output file permissions */
  230. int autopath = 0;            /* SET RECEIVE PATHNAMES AUTO flag */
  231.  
  232. #ifdef CALIBRATE
  233. #define CAL_O 3
  234. #define CAL_M 253
  235.  
  236. int cal_j = 0;
  237.  
  238. CHAR
  239. cal_a[] = {
  240.  16, 45, 98,  3, 52, 41, 14,  7, 76,165,122, 11,104, 77,166, 15,
  241. 160, 93, 18, 19,112, 85, 54, 23,232,213, 90, 27, 12, 81,126, 31,
  242.   4,205, 34, 35,144, 73,110, 39, 28,133,218, 43,156, 65,102, 47,
  243.  84, 61, 50, 51,208,117, 86, 55,  8,245, 74, 59, 44,125,222, 63,
  244.  80,  1,162, 67,116,105,206, 71,120,  9,250, 75, 88, 97,  6, 79,
  245. 100,221, 82, 83, 36, 89, 94, 87, 40, 21,106, 91,236,145,150, 95,
  246. 228, 33,130, 99,148,137,198,103,108,169, 42,107,184,129, 78,111,
  247.   0, 49,114,115, 32,121,254,119,172, 57,138,123,152,177, 22,127,
  248. 240,193,  2,131,176,  5, 38,135,204,229, 10,139,200,161,174,143,
  249. 128, 17,146,147, 68,153, 30,151, 72,217,170,155, 24,209, 62,159,
  250.  64,225,194,163,244,201, 70,167,216,197,234,171,188,109,230,175,
  251. 212,113,178,179,132,185,190,183,136,249,202,187, 92,241,118,191,
  252.  48,237, 66,195, 96,233,142,199,248, 37, 58,203, 60, 13,134,207,
  253.  20, 29,210,211,164,149,182,215,220, 25, 26,219,124,157,246,223,
  254. 180,141,226,227,192,101,238,231, 56, 69,154,235,252,173, 46,239,
  255. 224,253,242,243,196, 53,214,247,168,181,186,251,140,189,158,255
  256. };
  257. #endif /* CALIBRATE */
  258.  
  259. char * rf_err = "Error receiving file";    /* rcvfil() error message */
  260.  
  261. #ifdef CK_SPEED
  262. short ctlp[256] = {        /* Control-Prefix table */
  263.   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  */
  264.   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  */
  265.   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,
  266.   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 */
  267.   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  */
  268.   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  */
  269.   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,
  270.   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 */
  271. };
  272. #endif /* CK_SPEED */
  273.  
  274. int sndsrc;        /* Flag for where to get names of files to send: */
  275.                     /* -1: znext() function */
  276.                     /*  0: stdin */
  277.                     /* >0: list in cmlist or other list */
  278.                     /* -9: calibrate */
  279.  
  280. int  memstr;                /* Flag for input from memory string */
  281. int  funcstr;                /* Flag for input from function */
  282. int  bestlen = 0;
  283. int  maxsend = 0;
  284.  
  285. int gnf_binary = 0;            /* Prevailing xfer mode for gnfile */
  286.  
  287. #ifdef pdp11
  288. #define MYINITLEN 32
  289. #else
  290. #define MYINITLEN 100
  291. #endif /* pdp11 */
  292. CHAR myinit[MYINITLEN];            /* Copy of my Send-Init data */
  293.  
  294. /* Variables local to this module */
  295.  
  296. #ifdef TLOG
  297. #ifndef XYZ_INTERNAL
  298. static
  299. #endif /* XYZ_INTERNAL */
  300. char *fncnam[] = {
  301.   "rename", "replace", "backup", "append", "discard", "ask", "update", ""
  302. };
  303. #endif /* TLOG */
  304.  
  305. static char *memptr;            /* Pointer for memory strings */
  306.  
  307. #ifdef VMS
  308. extern int batch;
  309. #else
  310. extern int backgrd;
  311. #endif /* VMS */
  312.  
  313. #ifdef CK_CTRLZ
  314. static int lastchar = 0;
  315. #endif /* CK_CTRLZ */
  316.  
  317. #ifdef CK_ANSIC
  318. static int (*funcptr)(void);        /* Pointer for function strings */
  319. #else
  320. static int (*funcptr)();
  321. #endif /* CK_ANSIC */
  322.  
  323. #ifdef pdp11
  324. #define CMDSTRL 50
  325. static char cmdstr[50];            /* System command string. */
  326. #else
  327. #ifdef BIGBUFOK
  328. #define CMDSTRL 1024
  329. #else
  330. #define CMDSTRL 256
  331. #endif /* BIGBUFOK */
  332. static char cmdstr[CMDSTRL+1];
  333. #endif /* pdp11 */
  334.  
  335. static int drain;            /* For draining stacked-up ACKs. */
  336.  
  337. static int first;            /* Flag for first char from input */
  338. static CHAR t;                /* Current character */
  339. #ifdef COMMENT
  340. static CHAR next;            /* Next character */
  341. #endif /* COMMENT */
  342.  
  343. static int ebqsent = 0;            /* 8th-bit prefix bid that I sent */
  344. static int lsstate = 0;            /* Locking shift state */
  345. static int lsquote = 0;            /* Locking shift quote */
  346.  
  347. #ifdef datageneral
  348. extern int quiet;
  349. #endif /* datageneral */
  350.  
  351. /*  E N C S T R  --  Encode a string from memory. */
  352.  
  353. /*
  354.   Call this instead of getpkt() if source is a string, rather than a file.
  355.   Note: Character set translation is never done in this case.
  356. */
  357.  
  358. #ifdef COMMENT
  359. #define ENCBUFL 200
  360. #ifndef pdp11
  361. CHAR encbuf[ENCBUFL];
  362. #else
  363. /* This is gross, but the pdp11 root segment is out of space */
  364. /* Will allocate it in ckuusr.c. */
  365. extern CHAR encbuf[];
  366. #endif /* pdp11 */
  367. #endif /* COMMENT */
  368.  
  369. /*
  370.   Encode packet data from a string in memory rather than from a file.
  371.   Returns the length of the encoded string on success, -1 if the string
  372.   could not be completely encoded into the currently negotiated data
  373.   field length.
  374. */
  375. int
  376. #ifdef CK_ANSIC
  377. encstr(CHAR *s)
  378. #else /* CK_ANSIC */
  379. encstr(s) CHAR* s;
  380. #endif /* CK_ANSIC */
  381. {
  382. /*
  383.   Recoded 30 Jul 94 to use the regular data buffer and the negotiated
  384.   maximum packet size.  Previously we were limited to the length of encbuf[].
  385.   Also, to return a failure code if the entire encoded string would not fit.
  386.   Modified 14 Jul 98 to return length of encoded string.
  387. */
  388.     int m, rc, slen; char *p;
  389.     if (!data) {            /* Watch out for null pointers. */
  390.     debug(F100,"SERIOUS ERROR: encstr data == NULL","",0);
  391.     return(-1);
  392.     }
  393.     if (!s) s = (CHAR *)"";        /* Ditto. */
  394.     slen = strlen((char *)s);        /* Length of source string. */
  395.     debug(F111,"encstr",s,slen);
  396.     rc = 0;                /* Return code. */
  397.     m = memstr; p = memptr;        /* Save these. */
  398.     memptr = (char *)s;            /* Point to the string. */
  399.     debug(F101,"encstr memptr 1","",memptr);
  400.     memstr = 1;                /* Flag memory string as source. */
  401.     first = 1;                /* Initialize character lookahead. */
  402.     *data = NUL;            /* In case s is empty */
  403.     debug(F101,"encstr spsiz","",spsiz);
  404.     rc = getpkt(spsiz,0);        /* Fill a packet from the string. */
  405.     debug(F101,"encstr getpkt rc","",rc);
  406.     if (rc > -1 && memptr < (char *)(s + slen)) { /* Means we didn't encode */
  407.     rc = -1;            /* the whole string. */
  408.     debug(F101,"encstr string too big","",size);
  409.     }
  410.     debug(F101,"encstr getpkt rc","",rc);
  411.     memstr = m;                /* Restore memory string flag */
  412.     memptr = p;                /* and pointer */
  413.     first = 1;                /* Put this back as we found it. */
  414.     return(rc);
  415. }
  416.  
  417. /*  Output functions passed to 'decode':  */
  418.  
  419. int                   /*  Put character in server command buffer  */
  420. #ifdef CK_ANSIC
  421. putsrv(char c)
  422. #else
  423. putsrv(c) register char c;
  424. #endif /* CK_ANSIC */
  425. /* putsrv */ {
  426.     *srvptr++ = c;
  427.     *srvptr = '\0';        /* Make sure buffer is null-terminated */
  428.     return(0);
  429. }
  430.  
  431. int                    /*  Output character to console.  */
  432. #ifdef CK_ANSIC
  433. puttrm(char c)
  434. #else
  435. puttrm(c) register char c;
  436. #endif /* CK_ANSIC */
  437. /* puttrm */ {
  438. #ifndef NOSPL
  439.     extern int cmdsrc();
  440.     extern char * qbufp;        /* If REMOTE QUERY active, */
  441.     extern int query, qbufn;        /* also store response in */
  442.     if (query && qbufn++ < 1024) {    /* query buffer. */
  443.     *qbufp++ = c;
  444.     *qbufp = NUL;
  445.     }
  446.     if (!query || !cmdsrc())
  447. #endif /* NOSPL */
  448.       conoc(c);
  449.     return(0);
  450. }
  451. #endif /* NOXFER */
  452.  
  453. int                    /*  Output char to file. */
  454. #ifdef CK_ANSIC
  455. putmfil(char c)                /* Just like putfil but to ZMFILE */
  456. #else                    /* rather than ZOFILE... */
  457. putmfil(c) register char c;
  458. #endif /* CK_ANSIC */
  459. /* putmfil */ {
  460.     debug(F000,"putfil","",c);
  461.     if (zchout(ZMFILE, (char) (c & fmask)) < 0) {
  462.     czseen = 1;
  463.     debug(F101,"putfil zchout write error, setting czseen","",1);
  464.     return(-1);
  465.     }
  466.     return(0);
  467. }
  468.  
  469. int                    /*  Output char to file. */
  470. #ifdef CK_ANSIC
  471. putfil(char c)
  472. #else
  473. putfil(c) register char c;
  474. #endif /* CK_ANSIC */
  475. /* putfil */ {
  476.     debug(F000,"putfil","",c);
  477.     if (zchout(ZOFILE, (char) (c & fmask)) < 0) {
  478.     czseen = 1;               /* If write error... */
  479.     debug(F101,"putfil zchout write error, setting czseen","",1);
  480.     return(-1);
  481.     }
  482.     return(0);
  483. }
  484.  
  485. /*
  486.   The following function is a wrapper for putfil().  The only reason for its
  487.   existence is to be passed as a function pointer to decode(), which treats
  488.   putfil() itself specially -- bypassing it and using an internal macro
  489.   instead to speed things up.  Use zputfil() instead of putfil() in cases where
  490.   we do not want this to happen, e.g. when we need to send output to the file
  491.   with a mixture of zchout() and zsout()/zsoutl() calls (as is the case with
  492.   incoming short-form REMOTE command replies redirected to a file), which would
  493.   otherwise result in data written to the file out of order.
  494. */
  495. int
  496. #ifdef CK_ANSIC
  497. zputfil(char c)
  498. #else
  499. zputfil(c) register char c;
  500. #endif /* CK_ANSIC */
  501. /* zputfil */ {
  502.     return(putfil(c));
  503. }
  504.  
  505. #ifndef NOXFER
  506.  
  507. /* D E C O D E  --  Kermit packet decoding procedure */
  508.  
  509. /*
  510.  Call with string to be decoded and an output function.
  511.  Returns 0 on success, -1 on failure (e.g. disk full).
  512.  
  513.  This is the "inner loop" when receiving files, and must be coded as
  514.  efficiently as possible.  Note some potential problems:  if a packet
  515.  is badly formed, having a prefixed sequence ending prematurely, this
  516.  function, as coded, could read past the end of the packet.  This has
  517.  never happened, thus the additional (time-consuming) tests have not
  518.  been added.
  519. */
  520.  
  521. static CHAR *xdbuf;    /* Global version of decode()'s buffer pointer */
  522.                         /* for use by translation functions. */
  523.  
  524. /* Function for pushing a character onto decode()'s input stream. */
  525.  
  526. VOID
  527. #ifdef CK_ANSIC
  528. zdstuff(CHAR c)
  529. #else
  530. zdstuff(c) CHAR c;
  531. #endif /* CK_ANSIC */
  532. /* zdstuff */ {
  533.     xdbuf--;                /* Back up the pointer. */
  534.     *xdbuf = c;                /* Stuff the character. */
  535. }
  536.  
  537. #ifdef CKTUNING
  538. /*
  539.   Trimmed-down packet decoder for binary-mode no-parity transfers.
  540.   decode() is the full version.
  541. */
  542. int
  543. #ifdef CK_ANSIC
  544. bdecode(CHAR *buf, int (*fn)(char))
  545. #else
  546. bdecode(buf,fn) register CHAR *buf; register int (*fn)();
  547. #endif /* CK_ANSIC */
  548. /* bdecode */ {
  549.     register unsigned int a, a7;    /* Various copies of current char */
  550.     int ccpflg;                /* For Ctrl-unprefixing stats */
  551.     int t;                /* Int version of character */
  552.     int len;
  553.     long z;                /* For CRC calculation */
  554.     CHAR c;                /* Current character */
  555.  
  556.     if (!binary || parity || fn != putfil) /* JUST IN CASE */
  557.       return(decode(buf,fn,1));
  558.     debug(F100,"BDECODE","",0);
  559.  
  560.     xdbuf = buf;            /* Global copy of source pointer. */
  561.  
  562.     len = rln;                /* Number of bytes in data field */
  563.     while (len > 0) {
  564.         a = *xdbuf++ & 0xff;        /* Get next character */
  565.     len--;
  566.     rpt = 0;            /* Initialize repeat count. */
  567.     if (a == rptq && rptflg) {    /* Got a repeat prefix? */
  568.         rpt = xunchar(*xdbuf++ & 0xFF); /* Yes, get the repeat count, */
  569.         rptn += rpt;
  570.         a = *xdbuf++ & 0xFF;    /* and get the prefixed character. */
  571.         len -= 2;
  572.     }
  573.     ccpflg = 0;            /* Control prefix flag. */
  574.     if (a == ctlq) {        /* If control prefix, */
  575.         a  = *xdbuf++ & 0xFF;    /* get its operand */
  576.         len--;
  577.         a7 = a & 0x7F;        /* and its low 7 bits. */
  578.         if ((a7 >= 0100 && a7 <= 0137) || a7 == '?') { /* Controllify */
  579.         a = ctl(a);        /* if in control range. */
  580.         a7 = a & 0x7F;
  581.         ccpflg = 1;        /* Note that we did this */
  582.         ccp++;            /* Count for stats */
  583.         }
  584.     } else a7 = a & 0x7f;        /* Not control quote */
  585.     if (a7 < 32 || a7 == 127)    /* A bare control character? */
  586.       if (!ccpflg) ccu++;        /* Count it */
  587.     if (!rpt) rpt = 1;
  588.     for (; rpt > 0; rpt--) {    /* Output the char RPT times */
  589. #ifdef CALIBRATE
  590.         if (calibrate) {
  591.         ffc++;
  592.         continue;
  593.         }
  594. #endif /* CALIBRATE */
  595. #ifdef OS2
  596.             if (xflg && !remfile) {        /* Write to virtual screen */
  597.                 char _a;
  598.                 _a = a & fmask;
  599.                 t = conoc(_a);
  600.                 if (t < 1)
  601.                     t = -1;
  602.             } else
  603. #endif /* OS2 */
  604.           t = zmchout(a & fmask);    /* zmchout is a macro */
  605.         if (t < 0) {
  606.         debug(F101,"bdecode write error - errno","",errno);
  607.         return(-1);
  608.         }
  609.         ffc++;            /* Count the character */
  610.         if (docrc && !remfile) {    /* Update file CRC */
  611.         c = a;            /* Force conversion to unsigned char */
  612.         z = crc16 ^ (long)c;
  613.         crc16 = (crc16 >> 8) ^
  614.           (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
  615.         }
  616.     }
  617. #ifdef CK_CTRLZ
  618.     lastchar = a;
  619. #endif /* CK_CTRLZ */
  620.     }
  621.     return(0);
  622. }
  623. #endif /* CKTUNING */
  624. #endif /* NOXFER */
  625.  
  626. /*  P N B Y T E  --  Output next byte to file or other destination  */
  627.  
  628. static long offc = 0L;
  629.  
  630. static int
  631. #ifdef CK_ANSIC
  632. pnbyte(CHAR c, int (*fn)(char))
  633. #else
  634. pnbyte(c,fn) CHAR c; int (*fn)();
  635. #endif /* CK_ANSIC */
  636. /* pnbyte */ {
  637.     int rc;
  638.     long z;
  639.  
  640. #ifdef OS2
  641.     if (xflg && !remfile) {        /* Write to virtual screen */
  642.     char _a;
  643.     _a = c & fmask;
  644.     rc = conoc(_a);
  645.     if (rc < 1)
  646.       return(-1);
  647.     } else {
  648. #endif /* OS2 */
  649.     if (fn == putfil)        /* Execute output function */
  650.       rc = zmchout(c);        /* to-file macro (fast) */
  651.     else if (!fn)
  652.       rc = putchar(c);        /* to-screen macro (fast) */
  653.         else
  654.       rc = (*fn)(c);        /* function call (not as fast) */
  655.     if (rc < 0)
  656.       return(rc);
  657. #ifdef OS2
  658.     }
  659. #endif /* OS2 */
  660. /*
  661.   Both xgnbyte() and xpnbyte() increment ffc (the file byte counter).
  662.   During file transfer, only one of these functions is called.  However,
  663.   the TRANSLATE command is likely to call them both.  offc, therefore,
  664.   contains the output byte count, necessary for handling the UCS-2 BOM.
  665. */
  666.     offc++;                /* Count the byte */
  667.     ffc++;                /* Count the byte */
  668. #ifndef NOXFER
  669.     if (docrc && !xflg && !remfile) {    /* Update file CRC */
  670.     z = crc16 ^ (long)c;
  671.     crc16 = (crc16 >> 8) ^
  672.       (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
  673.     }
  674. #endif /* NOXFER */
  675.     return(1);
  676. }
  677.  
  678. /*
  679.   X P N B Y T E  --  Translate and put next byte to file.
  680.  
  681.   Only for Unicode.  Call with next untranslated byte from incoming
  682.   byte stream, which can be in any Transfer Character Set: UCS-2, UTF-8,
  683.   Latin-1, Latin-Hebrew, etc.  Translates to the file character set and
  684.   writes bytes to the output file.  Call with character as int to translate
  685.   as an int, plus the transfer character set (to translate from) and the
  686.   file character set (to translate to), or -1,0,0 to reset the UCS-2 byte
  687.   number (which should be done at the beginning of a file).  Returns:
  688.    -1: On error
  689.     0: Nothing to write (mid-sequence)
  690.    >0: Number of bytes written.
  691. */
  692. #ifdef KANJI
  693. static int jstate = 0, jx = 0;        /* For outputting JIS-7 */
  694. static char jbuf[16] = { NUL, NUL };
  695. #endif /* KANJI */
  696.  
  697. int
  698. #ifdef CK_ANSIC
  699. xpnbyte(int a, int tcs, int fcs, int (*fn)(char))
  700. #else
  701. xpnbyte(a,tcs,fcs,fn) int a, tcs, fcs; int (*fn)();
  702. #endif /* CK_ANSIC */
  703. /* xpnbyte */ {
  704. #ifdef UNICODE
  705.     extern int ucsorder, ucsbom;    /* Byte order */
  706. #endif /* UNICODE */
  707.     CHAR c;                /* Unsigned char worker */
  708.     static union ck_short uc, eu, sj;    /* UCS-2, EUC, and Shift-JIS workers */
  709.     USHORT ch;                /* ditto... */
  710.     USHORT * us = NULL;            /* ditto... */
  711.     int c7, rc, haveuc = 0;        /* Return code and UCS-2 flag */
  712.     int utferror = 0;            /* UTF-8 error */
  713.     static int bn = 0;            /* UCS-2 byte number */
  714.     int swapping = 0;            /* Swapping UCS bytes to output? */
  715.                     /* swapping must be 0 or 1 */
  716.     if (a == -1 && (tcs | fcs) == 0) {    /* Reset in case previous run */
  717.     bn = 0;                /* left bn at 1... */
  718.     offc = 0L;
  719.     debug(F101,"xpnbyte RESET","",bn);
  720.     return(0);
  721.     }
  722.     debug(F001,"xpnbyte a","",a);
  723.  
  724. #ifdef UNICODE
  725.     if (ucsorder != 1 && ucsorder != 0)    /* Also just in case... */
  726.       ucsorder = 0;
  727.     if ((byteorder && !ucsorder) || (!byteorder && ucsorder))
  728.       swapping = 1;            /* Swapping bytes to output */
  729.     if (tcs == TC_UTF8) {        /* 'a' is from a UTF-8 stream */
  730.     ch = a;
  731.     if (fcs == TC_UTF8)        /* Output is UTF-8 too */
  732.       return(pnbyte(ch,fn));    /* so just copy. */
  733.     rc = utf8_to_ucs2(ch,&us);    /* Otherwise convert to UCS-2 */
  734.     if (rc == 0) {            /* Done with this sequence */
  735.         uc.x_short = *us;        /* We have a Unicode */
  736.         haveuc = 1;
  737.     } else if (rc < 0) {        /* Error */
  738.         debug(F101,"xpnbyte UTF-8 conversion error","",rc);
  739.         haveuc = 1;            /* Replace by U+FFFD */
  740.         uc.x_short = *us;
  741.         utferror = 1;
  742.     } else                /* Sequence incomplete */
  743.       return(0);
  744.     } else if (tcs == TC_UCS2) {    /* 'a' is UCS-2 */
  745.     /* Here we have incoming UCS-2 in guaranteed Big Endian order */
  746.     /* so we must exchange bytes if local machine is Little Endian. */
  747.     switch (bn) {            /* Which byte? */
  748.       case 0:            /* High... */
  749.         uc.x_char[byteorder] = (unsigned)a & 0xff;
  750.         bn++;
  751.         return(0);            /* Wait for next */
  752.       case 1:            /* Low... */
  753.         uc.x_char[1-byteorder] = (unsigned)a & 0xff;
  754.         bn = 0;            /* Done with sequence */
  755.         haveuc = 1;            /* Have a Unicode */
  756.     }
  757.  
  758.     } else
  759. #endif /* UNICODE */
  760.  
  761. #ifdef KANJI                /* Whether UNICODE is defined or not */
  762.       if (tcs == TC_JEUC) {        /* Incoming Japanese EUC */
  763.     int bad = 0;
  764.     static int kanji = 0;        /* Flags set in case 0 for case 1 */
  765.     static int kana = 0;
  766.     switch (bn) {            /* Byte number */
  767.       case 0:            /* Byte 0 */
  768.         eu.x_short = 0;
  769.         if ((a & 0x80) == 0) {
  770.         sj.x_short = (unsigned)a & 0xff; /* Single byte */
  771.         kanji = kana = 0;
  772.         } else {            /* Double byte */
  773.         c7 = a & 0x7f;
  774.         if (c7 > 0x20 && c7 < 0x7f) { /* Kanji */
  775.             eu.x_char[byteorder] = (CHAR) a;  /* Store first byte */
  776.             bn++;              /* Set up for second byte */
  777.             kanji = 1;
  778.             kana = 0;
  779.             return(0);
  780.         } else if (a == 0x8e) {    /* SS2 -- Katakana prefix */
  781.             eu.x_char[byteorder] = (CHAR) a; /* Save it */
  782.             bn++;
  783.             kana = 1;
  784.             kanji = 0;
  785.             return(0);
  786.         } else {
  787.             bad++;
  788.         }
  789.         }
  790.         break;
  791.       case 1:            /* Byte 1 */
  792.         bn = 0;
  793.         if (kanji) {
  794.         eu.x_char[1-byteorder] = (CHAR) a;
  795.         sj.x_short = eu_to_sj(eu.x_short);
  796.         break;
  797.         } else if (kana) {
  798.         sj.x_short = (CHAR) (a | 0x80);
  799.         break;
  800.         } else {            /* (shouldn't happen) */
  801.         bad++;
  802.         }
  803.     }
  804.     /* Come here with one Shift-JIS character */
  805.  
  806. #ifdef UNICODE
  807.     if (bad) {
  808.         uc.x_short = 0xfffd;
  809.     } else {
  810.         uc.x_short = sj_to_un(sj.x_short); /* Convert to Unicode */
  811.     }
  812.     haveuc = 1;
  813. #endif /* UNICODE */
  814.     } else
  815. #endif /* KANJI */
  816.  
  817. #ifdef UNICODE
  818.     uc.x_short = (unsigned)a & 0xff; /* Latin-1 or whatever... */
  819.  
  820.     /* Come here with uc = the character to be translated. */
  821.     /* If (haveuc) it's UCS-2 in native order, otherwise it's a byte. */
  822.  
  823.     debug(F101,"xpnbyte haveuc","",haveuc);
  824.  
  825.     if (haveuc) {            /* If we have a Unicode... */
  826.     debug(F101,"xpnbyte uc.x_short A","",uc.x_short);
  827.     debug(F101,"xpnbyte feol","",feol);
  828.     if (feol && uc.x_short == CR) {
  829.         return(0);
  830.     } else if (feol && uc.x_short == LF) {
  831.         uc.x_short = feol;
  832.     }
  833.     debug(F101,"xpnbyte uc.x_short B","",uc.x_short);
  834.     if (fcs == FC_UCS2) {        /* And FCS is UCS-2 */
  835.         /* Write out the bytes in the appropriate byte order */
  836.         int count = 0;
  837.         if (offc == 0L && ucsbom) {    /* Beginning of file? */
  838.         if ((rc = pnbyte((ucsorder ? 0xff : 0xfe),fn)) < 0) /* BOM */
  839.           return(rc);
  840.         if ((rc = pnbyte((ucsorder ? 0xfe : 0xff),fn)) < 0)
  841.           return(rc);
  842.         count += 2;
  843.         }
  844.         if (utferror) {
  845.         if ((rc = pnbyte((ucsorder ? 0xfd : 0xff),fn)) < 0)
  846.           return(rc);
  847.         if ((rc = pnbyte((ucsorder ? 0xff : 0xfd),fn)) < 0)
  848.           return(rc);
  849.         count += 2;
  850.         }
  851.         if ((rc = pnbyte(uc.x_char[swapping],fn)) < 0)
  852.           return(rc);
  853.         if ((rc = pnbyte(uc.x_char[1-swapping],fn)) < 0)
  854.           return(rc);
  855.         count += 2;
  856.         return(count);
  857.     } else if (fcs == FC_UTF8) {    /* Convert to UTF-8 */
  858.         CHAR * buf = NULL;
  859.         int i, count;
  860.         if (utferror) {
  861.         if ((rc = pnbyte((ucsorder ? 0xbd : 0xff),fn)) < 0)
  862.           return(rc);
  863.         if ((rc = pnbyte((ucsorder ? 0xff : 0xbd),fn)) < 0)
  864.           return(rc);
  865.         }
  866.         if ((count = ucs2_to_utf8(uc.x_short,&buf)) < 1)
  867.           return(-1);
  868.         debug(F011,"xpnbyte buf",buf,count);
  869.         for (i = 0; i < count; i++)
  870.           if ((rc = pnbyte(buf[i],fn)) < 0)
  871.         return(rc);
  872.         if (utferror)
  873.           count += 2;
  874.         return(count);
  875.     } else {            /* Translate UCS-2 to byte */
  876.         if (uc.x_short == 0x2028 || uc.x_short == 0x2029) {
  877.         if (utferror)
  878.           pnbyte(UNK,fn);
  879.         if (feol)
  880.           return(pnbyte((CHAR)feol,fn));
  881.         if ((rc = pnbyte((CHAR)CR,fn)) < 0)
  882.           return(rc);
  883.         if ((rc = pnbyte((CHAR)LF,fn)) < 0)
  884.           return(rc);
  885.         else
  886.           return(utferror ? 3 : 2);
  887.         } else if (xuf) {        /* UCS-to-FCS function */
  888.         int x = 0;
  889.         if (utferror)
  890.           pnbyte(UNK,fn);
  891.         if ((rc = (*xuf)(uc.x_short)) < 0) /* These can fail... */
  892.           ch = UNK;
  893.         else
  894.           ch = (unsigned)((unsigned)rc & 0xffff);
  895.         x = pnbyte(ch,fn);
  896.         if (x < 0)
  897.           return(x);
  898.         else if (utferror)
  899.           x++;
  900.         return(x);
  901. #ifdef KANJI
  902.  
  903. /*  Also see the non-Unicode Kanji section further down in this function. */
  904.  
  905.         } else if (fcsinfo[fcs].alphabet == AL_JAPAN) {
  906.  
  907.         /* Translate UCS-2 to Japanese set */
  908.         debug(F001,"xpnbyte uc","",uc.x_short);
  909.         sj.x_short = un_to_sj(uc.x_short); /* First to Shift-JIS */
  910.         debug(F001,"xpnbyte sj","",sj.x_short);
  911.  
  912.         switch (fcs) {        /* File character set */
  913.           case FC_SHJIS:    /* Shift-JIS -- just output it */
  914.             if (sj.x_char[byteorder]) /* But not high byte if zero */
  915.               if ((rc = pnbyte((CHAR)sj.x_char[byteorder],fn)) < 0)
  916.             return(rc);
  917.             if ((rc = pnbyte((CHAR)sj.x_char[1-byteorder],fn)) < 0)
  918.               return(rc);
  919.             return(2);
  920.           case FC_JEUC:        /* EUC-JP */
  921.             eu.x_short = sj_to_eu(sj.x_short); /* Shift-JIS to EUC */
  922.             debug(F001,"xpnbyte eu","",eu.x_short);
  923.             if (eu.x_short == 0xffff) { /* Bad */
  924.             if ((rc = pnbyte(UNK,fn)) < 0)
  925.               return(rc);
  926.             return(1);
  927.             } else {        /* Good */
  928.             int count = 0;    /* Write high byte if not zero */
  929.             if (eu.x_char[byteorder]) {
  930.                 if ((rc=pnbyte((CHAR)eu.x_char[byteorder],fn)) < 0)
  931.                   return(rc);
  932.                 count++;
  933.             }
  934.             /* Always write low byte */
  935.             if ((rc = pnbyte((CHAR)eu.x_char[1-byteorder],fn)) < 0)
  936.               return(rc);
  937.             count++;
  938.             return(count);
  939.             }
  940.             break;
  941.  
  942.           case FC_JIS7:        /* JIS-7 */
  943.           case FC_JDEC:        /* DEC Kanji */
  944.             eu.x_short = sj_to_eu(sj.x_short); /* Shift-JIS to EUC */
  945.             if (eu.x_short == 0xffff) { /* Bad */
  946.             debug(F001,"xpnbyte bad eu","",eu.x_short);
  947.             if ((rc = pnbyte(UNK,fn)) < 0)
  948.               return(rc);
  949.             return(1);
  950.             } else {        /* Good */
  951.             int i;
  952.             /* ^^^ Use another name - 'a' hides parameter */
  953.             /* It's OK as is but causes compiler warnings */
  954.             char a = eu.x_char[1-byteorder]; /* Low byte */
  955.             debug(F001,"xpnbyte eu","",eu.x_short);
  956.             if (eu.x_char[byteorder] == 0) { /* Roman */
  957.                 switch (jstate) {
  958.                   case 1:    /* Current state is Katakana */
  959.                 jbuf[0] = 0x0f;    /* SI */
  960.                 jbuf[1] = a;
  961.                 jx = 2;
  962.                 break;
  963.                   case 2:    /* Current state is Kanji */
  964.                 jbuf[0] = 0x1b;    /* ESC */
  965.                 jbuf[1] = 0x28;    /* ( */
  966.                 jbuf[2] = 0x4a;    /* J */
  967.                 jbuf[3] = a;
  968.                 jx = 4;
  969.                 break;
  970.                   default:    /* Current state is Roman */
  971.                 jbuf[0] = a;
  972.                 jx = 1;
  973.                 break;
  974.                 }
  975.                 jstate = 0;    /* New state is Roman */
  976.             } else if (eu.x_char[byteorder] == 0x8e) { /* Kana */
  977.                 jx = 0;
  978.                 switch (jstate) {
  979.                   case 2:           /* from Kanji */
  980.                 jbuf[jx++] = 0x1b; /* ESC */
  981.                 jbuf[jx++] = 0x28; /* ( */
  982.                 jbuf[jx++] = 0x4a; /* J */
  983.                   case 0:           /* from Roman */
  984.                 jbuf[jx++] = 0x0e; /* SO */
  985.                   default:           /* State is already Kana*/
  986.                 jbuf[jx++] = (a & 0x7f); /* and the char */
  987.                 break;
  988.                 }
  989.                 jstate = 1;    /* New state is Katakana */
  990.             } else {    /* Kanji */
  991.                 jx = 0;
  992.                 switch (jstate) {
  993.                   case 1:    /* Current state is Katakana */
  994.                 jbuf[jx++] = 0x0f; /* SI  */
  995.                   case 0:    /* Current state is Roman */
  996.                 jbuf[jx++] = 0x1b; /* ESC */
  997.                 jbuf[jx++] = 0x24; /* $   */
  998.                 jbuf[jx++] = 0x42; /* B   */
  999.                   default:    /* Current state is already Kanji */
  1000.                 jbuf[jx++] = eu.x_char[byteorder] & 0x7f;
  1001.                 jbuf[jx++] = eu.x_char[1-byteorder] & 0x7f;
  1002.                 break;
  1003.                 }
  1004.                 jstate = 2;    /* Set state to Kanji */
  1005.             }
  1006.             for (i = 0; i < jx; i++) /* Output the result */
  1007.               if ((rc = pnbyte(jbuf[i],fn)) < 0)
  1008.                 return(rc);
  1009.             return(jx);    /* Return its length */
  1010.             }
  1011.         }
  1012. #endif /* KANJI */
  1013.         } else {            /* No translation function */
  1014.         int count = 0;
  1015.         if (utferror) {
  1016.             if ((rc = pnbyte((ucsorder ? 0xfd : 0xff),fn)) < 0)
  1017.               return(rc);
  1018.             if ((rc = pnbyte((ucsorder ? 0xff : 0xfd),fn)) < 0)
  1019.               return(rc);
  1020.             count += 2;
  1021.         }
  1022.         if ((rc = pnbyte(uc.x_char[swapping],fn)) < 0)
  1023.           return(rc);
  1024.         if ((rc = pnbyte(uc.x_char[1-swapping],fn)) < 0)
  1025.           return(rc);
  1026.         count += 2;
  1027.         return(count);
  1028.         }
  1029.     }
  1030.     } else {                /* Byte to Unicode */
  1031.     if (xtu) {            /* TCS-to-UCS function */
  1032.         if (((tcsinfo[tcs].size > 128) && (uc.x_short & 0x80)) ||
  1033.         tcsinfo[tcs].size <= 128)
  1034.           uc.x_short = (*xtu)(uc.x_short);
  1035.     }
  1036.     if (fcs == FC_UCS2) {        /* And FCS is UCS-2 */
  1037.         /* Write out the bytes in the appropriate byte order */
  1038.         if (offc == 0 && ucsbom) {    /* Beginning of file? */
  1039.         if ((rc = pnbyte((ucsorder ? 0xff : 0xfe),fn)) < 0) /* BOM */
  1040.           return(rc);
  1041.         if ((rc = pnbyte((ucsorder ? 0xfe : 0xff),fn)) < 0)
  1042.           return(rc);
  1043.         }
  1044.         if ((rc = pnbyte(uc.x_char[swapping],fn)) < 0)
  1045.           return(rc);
  1046.         if ((rc = pnbyte(uc.x_char[1-swapping],fn)) < 0)
  1047.           return(rc);
  1048.         return(2);
  1049.     } else if (fcs == FC_UTF8) {    /* Convert to UTF-8 */
  1050.         CHAR * buf = NULL;
  1051.         int i, count;
  1052.         if ((count = ucs2_to_utf8(uc.x_short,&buf)) < 1)
  1053.           return(-1);
  1054.         for (i = 0; i < count; i++)
  1055.           if ((rc = pnbyte(buf[i],fn)) < 0)
  1056.         return(rc);
  1057.         return(count);
  1058.     } else {
  1059.         debug(F100,"xpnbyte impossible combination","",0);
  1060.         return(-1);
  1061.     }
  1062.     }
  1063. #else
  1064. #ifdef KANJI
  1065. /*
  1066.   This almost, but not quite, duplicates the Kanji section above.
  1067.   There is no doubt a way to combine the sections more elegantly,
  1068.   but probably only at the expense of additional execution overhead.
  1069.   As matters stand, be careful to reflect any changes in this section
  1070.   to the other Kanji section above.
  1071. */
  1072.     if (tcs == TC_JEUC) {        /* Incoming Japanese EUC */
  1073.     int count = 0;
  1074.     switch (fcs) {            /* File character set */
  1075.       case FC_SHJIS:        /* Shift-JIS -- just output it */
  1076.         if (sj.x_char[byteorder])    /* But not high byte if zero */
  1077.           if ((rc = pnbyte((CHAR)sj.x_char[byteorder],fn)) < 0)
  1078.         return(rc);
  1079.         count++;
  1080.         if ((rc = pnbyte((CHAR)sj.x_char[1-byteorder],fn)) < 0)
  1081.           return(rc);
  1082.         count++;
  1083.         return(count);
  1084.       case FC_JEUC:            /* EUC-JP */
  1085.         eu.x_short = sj_to_eu(sj.x_short); /* Shift-JIS to EUC */
  1086.         debug(F001,"xpnbyte FC_JEUC eu","",eu.x_short);
  1087.         if (eu.x_short == 0xffff) { /* Bad */
  1088.         if ((rc = pnbyte(UNK,fn)) < 0)
  1089.           return(rc);
  1090.         return(1);
  1091.         } else {            /* Good */
  1092.         int count = 0;        /* Write high byte if not zero */
  1093.         if (eu.x_char[byteorder]) {
  1094.             if ((rc = pnbyte((CHAR)eu.x_char[byteorder],fn)) < 0)
  1095.               return(rc);
  1096.             count++;
  1097.         }
  1098.         /* Always write low byte */
  1099.         if ((rc = pnbyte((CHAR)eu.x_char[1-byteorder],fn)) < 0)
  1100.           return(rc);
  1101.         count++;
  1102.         return(count);
  1103.         }
  1104.         break;
  1105.  
  1106.       case FC_JIS7:            /* JIS-7 */
  1107.       case FC_JDEC:            /* DEC Kanji */
  1108.         eu.x_short = sj_to_eu(sj.x_short); /* Shift-JIS to EUC */
  1109.         if (eu.x_short == 0xffff) { /* Bad */
  1110.         debug(F001,"xpnbyte FC_JIS7 bad eu","",eu.x_short);
  1111.         if ((rc = pnbyte(UNK,fn)) < 0)
  1112.           return(rc);
  1113.         return(1);
  1114.         } else {            /* Good */
  1115.         int i;
  1116.         char a = eu.x_char[1-byteorder]; /* Low byte */
  1117.         debug(F001,"xpnbyte FC_JIS7 eu","",eu.x_short);
  1118.         if (eu.x_char[byteorder] == 0) { /* Roman */
  1119.             switch (jstate) {
  1120.               case 1:        /* Current state is Katakana */
  1121.             jbuf[0] = 0x0f;    /* SI */
  1122.             jbuf[1] = a;
  1123.             jx = 2;
  1124.             break;
  1125.               case 2:        /* Current state is Kanji */
  1126.             jbuf[0] = 0x1b;    /* ESC */
  1127.             jbuf[1] = 0x28;    /* ( */
  1128.             jbuf[2] = 0x4a;    /* J */
  1129.             jbuf[3] = a;
  1130.             jx = 4;
  1131.             break;
  1132.               default:        /* Current state is Roman */
  1133.             jbuf[0] = a;
  1134.             jx = 1;
  1135.             break;
  1136.             }
  1137.             jstate = 0;        /* New state is Roman */
  1138.         } else if (eu.x_char[byteorder] == 0x8e) { /* Kana */
  1139.             jx = 0;
  1140.             switch (jstate) {
  1141.               case 2:           /* from Kanji */
  1142.             jbuf[jx++] = 0x1b; /* ESC */
  1143.             jbuf[jx++] = 0x28; /* ( */
  1144.             jbuf[jx++] = 0x4a; /* J */
  1145.               case 0:           /* from Roman */
  1146.             jbuf[jx++] = 0x0e; /* SO */
  1147.               default:           /* State is already Kana*/
  1148.             jbuf[jx++] = (a & 0x7f); /* and the char */
  1149.             break;
  1150.             }
  1151.             jstate = 1;        /* New state is Katakana */
  1152.         } else {        /* Kanji */
  1153.             jx = 0;
  1154.             switch (jstate) {
  1155.               case 1:        /* Current state is Katakana */
  1156.             jbuf[jx++] = 0x0f; /* SI  */
  1157.               case 0:        /* Current state is Roman */
  1158.             jbuf[jx++] = 0x1b; /* ESC */
  1159.             jbuf[jx++] = 0x24; /* $   */
  1160.             jbuf[jx++] = 0x42; /* B   */
  1161.               default:        /* Current state is already Kanji */
  1162.             jbuf[jx++] = eu.x_char[byteorder] & 0x7f;
  1163.             jbuf[jx++] = eu.x_char[1-byteorder] & 0x7f;
  1164.             break;
  1165.             }
  1166.             jstate = 2;        /* Set state to Kanji */
  1167.         }
  1168.         for (i = 0; i < jx; i++) /* Output the result */
  1169.           if ((rc = pnbyte(jbuf[i],fn)) < 0)
  1170.             return(rc);
  1171.         return(jx);        /* Return its length */
  1172.         }
  1173.       default:
  1174.         if (sj.x_short < 0x80)
  1175.           return(sj.x_short);
  1176.         else
  1177.           return('?');
  1178.     }
  1179.     }
  1180. #endif /* KANJI */
  1181. #endif /* UNICODE */
  1182.     debug(F100,"xpnbyte BAD FALLTHRU","",0);
  1183.     return(-1);
  1184. }
  1185.  
  1186. #ifndef NOXFER
  1187.  
  1188. /*  D E C O D E  --  Kermit Data-packet decoder  */
  1189.  
  1190. int
  1191. #ifdef CK_ANSIC
  1192. decode(CHAR *buf, int (*fn)(char), int xlate)
  1193. #else
  1194. decode(buf,fn,xlate) register CHAR *buf; register int (*fn)(); int xlate;
  1195. #endif /* CK_ANSIC */
  1196. /* decode */ {
  1197.     register unsigned int a, a7, a8, b8; /* Various copies of current char */
  1198.     int t;                /* Int version of character */
  1199.     int ssflg;                /* Character was single-shifted */
  1200.     int ccpflg;                /* For Ctrl-unprefixing stats */
  1201.     int len;
  1202.     long z;
  1203.     CHAR c;
  1204. /*
  1205.   Catch the case in which we are asked to decode into a file that is not open,
  1206.   for example, if the user interrupted the transfer, but the other Kermit
  1207.   keeps sending.
  1208. */
  1209.     if ((cxseen || czseen || discard) && (fn == putfil))
  1210.       return(0);
  1211.  
  1212. #ifdef COMMENT
  1213. #ifdef CKTUNING
  1214.     if (binary && !parity)
  1215.       return(bdecode(buf,fn));
  1216. #endif /* CKTUNING */
  1217. #endif /* COMMENT */
  1218.     debug(F100,"DECODE","",0);
  1219.  
  1220.     xdbuf = buf;            /* Make global copy of pointer. */
  1221.     rpt = 0;                /* Initialize repeat count. */
  1222.  
  1223.     len = rln;                /* Number of bytes in data field */
  1224.     while (len > 0) {            /* Loop for each byte */
  1225.         a = *xdbuf++ & 0xff;        /* Get next character */
  1226.     len--;
  1227.     if (a == rptq && rptflg) {    /* Got a repeat prefix? */
  1228.         rpt = xunchar(*xdbuf++ & 0xFF); /* Yes, get the repeat count, */
  1229.         rptn += rpt;
  1230.         a = *xdbuf++ & 0xFF;    /* and get the prefixed character. */
  1231.         len -= 2;
  1232.     }
  1233.     b8 = lsstate ? 0200 : 0;    /* 8th-bit value from SHIFT-STATE */
  1234.     if (ebqflg && a == ebq) {    /* Have 8th-bit prefix? */
  1235.         b8 ^= 0200;            /* Yes, invert the 8th bit's value, */
  1236.         ssflg = 1;            /* remember we did this, */
  1237.         a = *xdbuf++ & 0xFF;    /* and get the prefixed character. */
  1238.         len--;
  1239.     } else ssflg = 0;
  1240.     ccpflg = 0;
  1241.     if (a == ctlq) {        /* If control prefix, */
  1242.         a  = *xdbuf++ & 0xFF;    /* get its operand */
  1243.         len--;
  1244.         a7 = a & 0x7F;        /* and its low 7 bits. */
  1245.         if ((a7 >= 0100 && a7 <= 0137) || a7 == '?') { /* Controllify */
  1246.         a = ctl(a);        /* if in control range. */
  1247.         a7 = a & 0x7F;
  1248.         ccpflg = 1;        /* Note that we did this */
  1249.         ccp++;            /* Count for stats */
  1250.         }
  1251.     } else a7 = a & 0x7f;        /* Not control quote */
  1252.     if (a7 < 32 || a7 == 127) {    /* Control character? */
  1253.         if (!ccpflg) ccu++;        /* A bare one, count it */
  1254.         if (lscapu) {        /* If doing locking shifts... */
  1255.         if (lsstate)        /* If SHIFTED */
  1256.           a8 = (a & ~b8) & 0xFF; /* Invert meaning of 8th bit */
  1257.         else            /* otherwise */
  1258.           a8 = a | b8;        /* OR in 8th bit */
  1259.         /* If we're not in a quoted sequence */
  1260.         if (!lsquote && (!lsstate || !ssflg)) {
  1261.             if (a8 == DLE) {    /* Check for DLE quote */
  1262.             lsquote = 1;    /* prefixed by single shift! */
  1263.             continue;
  1264.             } else if (a8 == SO) { /* Check for Shift-Out */
  1265.             lsstate = 1;    /* SHIFT-STATE = SHIFTED */
  1266.             continue;
  1267.             } else if (a8 == SI) { /* or Shift-In */
  1268.             lsstate = 0;    /* SHIFT-STATE = UNSHIFTED */
  1269.             continue;
  1270.             }
  1271.         } else lsquote = 0;
  1272.         }
  1273.     }
  1274.     a |= b8;            /* OR in the 8th bit */
  1275.     if (rpt == 0) rpt = 1;        /* If no repeats, then one */
  1276. #ifndef NOCSETS
  1277.     if (!binary) {            /* If in text mode, */
  1278.         if (tcharset != TC_UCS2) {
  1279.         if (feol && a == CR)    /* Convert CRLF to newline char */
  1280.           continue;
  1281.         if (feol && a == LF)
  1282.           a = feol;
  1283.         }
  1284.         if (xlatype == XLA_BYTE)    /* Byte-for-byte - do it now */
  1285.           if (xlate && rx) a = (*rx)((CHAR) a);
  1286.         }
  1287. #endif /* NOCSETS */
  1288.     /* (PWP) Decoding speedup via buffered output and a macro... */
  1289.     if (fn == putfil) {
  1290.         for (; rpt > 0; rpt--) {    /* Output the char RPT times */
  1291. #ifdef CALIBRATE
  1292.         if (calibrate) {
  1293.             ffc++;
  1294.             continue;
  1295.         }
  1296. #endif /* CALIBRATE */
  1297.  
  1298. /* Note: The Unicode and Kanji sections can probably be combined now; */
  1299. /* the Unicode method (xpnbyte()) covers Kanji too. */
  1300.  
  1301. #ifdef UNICODE
  1302.         if (!binary && xlatype == XLA_UNICODE)
  1303.           t = xpnbyte((unsigned)((unsigned)a & 0xff),
  1304.                   tcharset,
  1305.                   fcharset,
  1306.                   fn
  1307.                   );
  1308.         else
  1309. #endif /* UNICODE */
  1310. #ifdef KANJI
  1311.         if (!binary && tcharset == TC_JEUC &&
  1312.             fcharset != FC_JEUC) { /* Translating from J-EUC */
  1313.             if (ffc == 0L) xkanjf();
  1314.             if (xkanji(a,fn) < 0)  /* to something else? */
  1315.               return(-1);
  1316.             else t = 1;
  1317.         } else
  1318. #endif /* KANJI */
  1319.         {
  1320. #ifdef OS2
  1321.               if (xflg && !remfile) { /* Write to virtual screen */
  1322.               char _a;
  1323.               _a = a & fmask;
  1324.               t = conoc(_a);
  1325.               if (t < 1)
  1326.                 t = -1;
  1327.               } else
  1328. #endif /* OS2 */
  1329.             t = zmchout(a & fmask); /* zmchout is a macro */
  1330.         }
  1331.         if (t < 0) {
  1332.             debug(F101,"decode write errno","",errno);
  1333.             return(-1);
  1334.         }
  1335. #ifdef UNICODE
  1336.         if (xlatype != XLA_UNICODE || binary) {
  1337.             ffc++;        /* Count the character */
  1338.             if (docrc && !xflg && !remfile) { /* Update file CRC */
  1339.             c = a;        /* Force conversion to unsigned char */
  1340.             z = crc16 ^ (long)c;
  1341.             crc16 = (crc16 >> 8) ^
  1342.               (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
  1343.             }
  1344.         }
  1345. #endif /* UNICODE */
  1346.         }
  1347.     } else {            /* Output to something else. */
  1348.         a &= fmask;            /* Apply file mask */
  1349.         for (; rpt > 0; rpt--) {    /* Output the char RPT times */
  1350. #ifdef CALIBRATE
  1351.         if (calibrate) {
  1352.             ffc++;
  1353.             continue;
  1354.         }
  1355. #endif /* CALIBRATE */
  1356.         if ((*fn)((char) a) < 0) return(-1); /* Send to output func. */
  1357.         }
  1358.     }
  1359. #ifdef CK_CTRLZ
  1360.     lastchar = a;
  1361. #endif /* CK_CTRLZ */
  1362.     }
  1363.     return(0);
  1364. }
  1365.  
  1366. /*  G E T P K T -- Fill a packet data field  */
  1367.  
  1368. /*
  1369.   Gets characters from the current source -- file or memory string.
  1370.   Encodes the data into the packet, filling the packet optimally.
  1371.   Set first = 1 when calling for the first time on a given input stream
  1372.   (string or file).
  1373.  
  1374.   Call with:
  1375.     bufmax -- current send-packet size
  1376.     xlate  -- flag: 0 to skip character-set translation, 1 to translate
  1377.  
  1378.   Uses global variables:
  1379.     t     -- current character.
  1380.     first -- flag: 1 to start up, 0 for input in progress, -1 for EOF.
  1381.     next  -- next character (not used any more).
  1382.     data  -- pointer to the packet data buffer.
  1383.     size  -- number of characters in the data buffer.
  1384.     memstr - flag that input is coming from a memory string instead of a file.
  1385.     memptr - pointer to string in memory.
  1386.     (*sx)()  character set translation function
  1387.  
  1388.   Returns:
  1389.     The size as value of the function, and also sets global "size",
  1390.     and fills (and null-terminates) the global data array.
  1391.     Returns:
  1392.       0 on EOF.
  1393.      -1 on fatal (internal) error.
  1394.      -3 on timeout (e.g. when reading data from a pipe).
  1395.  
  1396.   Rewritten by Paul W. Placeway (PWP) of Ohio State University, March 1989.
  1397.   Incorporates old getchx() and encode() inline to reduce function calls,
  1398.   uses buffered input for much-improved efficiency, and clears up some
  1399.   confusion with line termination (CRLF vs LF vs CR).
  1400.  
  1401.   Rewritten again by Frank da Cruz to incorporate locking shift mechanism,
  1402.   May 1991.  And again in 1998 for efficiency, etc, with a separate
  1403.   bgetpkt() split out for binary-mode no-parity transfers.
  1404. */
  1405.  
  1406. /*
  1407.   Note: Separate Kanji support dates from circa 1991 and now (1999) can most
  1408.   likely be combined with the the Unicode support: the xgnbyte()/xpnbyte()
  1409.   mechanism works for both Unicode and Kanji.
  1410. */
  1411. #ifdef KANJI
  1412. int
  1413. kgetf(
  1414. #ifdef CK_ANSIC
  1415.       VOID
  1416. #endif /* CK_ANSIC */
  1417.       ) {
  1418.     if (funcstr)
  1419.       return((*funcptr)());
  1420.     else
  1421.       return(zminchar());
  1422. }
  1423.  
  1424. int
  1425. kgetm(
  1426. #ifdef CK_ANSIC
  1427.       VOID
  1428. #endif /* CK_ANSIC */
  1429.       ) {
  1430.     int x;
  1431.     if (x = *memptr++) return(x);
  1432.     else return(-1);
  1433. }
  1434. #endif /* KANJI */
  1435.  
  1436. /*
  1437.   Lookahead function to decide whether locking shift is worth it.  Looks at
  1438.   the next four input characters to see if all of their 8th bits match the
  1439.   argument.  Call with 0 or 0200.  Returns 1 on match, 0 if they don't match.
  1440.   If we don't happen to have at least 4 more characters waiting in the input
  1441.   buffer, returns 1.  Note that zinptr points two characters ahead of the
  1442.   current character because of repeat-count lookahead.
  1443. */
  1444. int
  1445. lslook(b) unsigned int b; {        /* Locking Shift Lookahead */
  1446.     int i;
  1447.     if (zincnt < 3)            /* If not enough chars in buffer, */
  1448.       return(1);            /* force shift-state switch. */
  1449.     b &= 0200;                /* Force argument to proper form. */
  1450.     for (i = -1; i < 3; i++)        /* Look at next 5 characters to */
  1451.       if (((*(zinptr+i)) & 0200) != b)    /* see if all their 8th bits match.  */
  1452.     return(0);            /* They don't. */
  1453.     return(1);                /* They do. */
  1454. }
  1455.  
  1456. /* Routine to compute maximum data length for packet to be filled */
  1457.  
  1458. int
  1459. maxdata() {                /* Get maximum data length */
  1460.     int n, len;
  1461.     debug(F101,"maxdata spsiz 1","",spsiz);
  1462.     if (spsiz < 0)            /* How could this happen? */
  1463.       spsiz = DSPSIZ;
  1464.     debug(F101,"maxdata spsiz 2","",spsiz);
  1465.     n = spsiz - 5;            /* Space for Data and Checksum */
  1466.     if (n > 92 && n < 96) n = 92;    /* "Short" Long packets don't pay */
  1467.     if (n > 92 && lpcapu == 0)        /* If long packets needed, */
  1468.       n = 92;                /* make sure they've been negotiated */
  1469.     len = n - bctl;            /* Space for data */
  1470.     if (n > 92) len -= 3;        /* Long packet needs header chksum */
  1471.     debug(F101,"maxdata len 1","",len);
  1472.     if (len < 0) len = 10;
  1473.     debug(F101,"maxdata len 2","",len);
  1474.     return(len);
  1475. }
  1476.  
  1477. static CHAR leftover[9] = { '\0','\0','\0','\0','\0','\0','\0','\0','\0' };
  1478. static int nleft = 0;
  1479.  
  1480. #ifdef CKTUNING
  1481. /*
  1482.   When CKTUNING is defined we use this special trimmed-down version of getpkt
  1483.   to speed up binary-mode no-parity transfers.  When CKTUNING is not defined,
  1484.   or for text-mode or parity transfers, we use the regular getpkt() function.
  1485.   Call just like getpkt() but test first for transfer mode and parity.  NOTE:
  1486.   This routine is only to be called when sending a real file -- not for
  1487.   filenames, server responses, etc, because it only reads from the input file.
  1488.   See getpkt() for more detailed commentary.
  1489. */
  1490. static int
  1491. bgetpkt(bufmax) int bufmax; {
  1492.     register CHAR rt = t, rnext;
  1493.     register CHAR *dp, *odp, *p1, *p2;
  1494.     register int x = 0, a7;
  1495.  
  1496.     CHAR xxrc, xxcq;            /* Pieces of prefixed sequence */
  1497.  
  1498.     long z;                /* A long worker (for CRC) */
  1499.  
  1500.     if (!binary || parity || memstr)    /* JUST IN CASE caller didn't test */
  1501.       return(getpkt(bufmax,!binary));
  1502.  
  1503.     if (!data) {
  1504.     debug(F100,"SERIOUS ERROR: bgetpkt data == NULL","",0);
  1505.     return(-1);
  1506.     }
  1507.     dp = data;                /* Point to packet data buffer */
  1508.     size = 0;                /* And initialize its size */
  1509.     bufmax = maxdata();            /* Get maximum data length */
  1510.  
  1511. #ifdef DEBUG
  1512.     if (deblog)
  1513.       debug(F101,"bgetpkt bufmax","",bufmax);
  1514. #endif /* DEBUG */
  1515.  
  1516.     if (first == 1) {            /* If first character of this file.. */
  1517.     ffc = 0L;            /* reset file character counter */
  1518. #ifdef COMMENT
  1519. /* Moved to below */
  1520.     first = 0;            /* Next character won't be first */
  1521. #endif /* COMMENT */
  1522.     *leftover = '\0';        /* Discard any interrupted leftovers */
  1523.     nleft = 0;
  1524.  
  1525.     /* Get first character of file into rt, watching out for null file */
  1526.  
  1527. #ifdef CALIBRATE
  1528.     if (calibrate) {
  1529. #ifdef NORANDOM
  1530.         rt = 17;
  1531. #else
  1532.         rt = cal_a[rand() & 0xff];
  1533. #endif /* NORANDOM */
  1534.         first = 0;
  1535.     } else
  1536. #endif /* CALIBRATE */
  1537.  
  1538.     if ((x = zminchar()) < 0) {    /* EOF or error */
  1539.         if (x == -3) {        /* Timeout. */
  1540.         size = (dp - data);
  1541.         debug(F101,"bgetpkt timeout size","",size);
  1542.         return((size == 0) ? x : size);
  1543.         }
  1544.         first = -1;
  1545.         size = 0;
  1546.         if (x == -2) {        /* Error */
  1547.         debug(F100,"bgetpkt: input error","",0);
  1548.         cxseen = 1;        /* Interrupt the file transfer */
  1549.         } else {
  1550.         debug(F100,"bgetpkt empty file","",0);
  1551.         }
  1552.         return(0);
  1553.     }
  1554.     first = 0;            /* Next char will not be the first */
  1555.     ffc++;                /* Count a file character */
  1556.     rt = (CHAR) x;            /* Convert int to char */
  1557.     if (docrc && what == W_SEND) {    /* Accumulate file crc */
  1558.         z = crc16 ^ (long)rt;
  1559.         crc16 = (crc16 >> 8) ^
  1560.           (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
  1561.     }
  1562.     rt &= fmask;            /* Apply SET FILE BYTESIZE mask */
  1563.  
  1564.     } else if (first == -1 && nleft == 0) { /* EOF from last time */
  1565.  
  1566.         return(size = 0);
  1567.     }
  1568. /*
  1569.   Here we handle characters that were encoded for the last packet but
  1570.   did not fit, and so were saved in the "leftover" array.
  1571. */
  1572.     if (nleft) {
  1573.     for (p1 = leftover; nleft > 0; nleft--) /* Copy leftovers */
  1574.       *dp++ = *p1++;
  1575.     *leftover = '\0';        /* Delete leftovers */
  1576.     nleft = 0;
  1577.     }
  1578.     if (first == -1)            /* Handle EOF */
  1579.       return(size = (dp - data));
  1580.  
  1581. /* Now fill up the rest of the packet. */
  1582.  
  1583.     rpt = 0;                /* Initialize character repeat count */
  1584.  
  1585.     while (first > -1) {        /* Until EOF... */
  1586. #ifdef CALIBRATE
  1587.     if (calibrate) {        /* We generate our own "file" */
  1588.         if (ffc >= calibrate) {    /* EOF */
  1589.         first = -1;
  1590.         ffc--;
  1591.         } else {            /* Generate next character */
  1592.         if (cal_j > CAL_M * ffc)
  1593.           cal_j = cal_a[ffc & 0xff];
  1594.         x = (unsigned)cal_a[(cal_j & 0xff)];
  1595.         if (x == rt) x ^= 2;
  1596.         }
  1597.         ffc++;
  1598.         cal_j += (unsigned int)(ffc + CAL_O);
  1599.     } else
  1600. #endif /* CALIBRATE */
  1601.     if ((x = zminchar()) < 0) {    /* Check for EOF */
  1602.         if (x == -3) {        /* Timeout. */
  1603.         t = rt;
  1604.         size = (dp-data);
  1605.         debug(F101,"bgetpkt timeout size","",size);
  1606.         return((size == 0) ? x : size);
  1607.         }
  1608.         first = -1;            /* Flag eof for next time. */
  1609.         if (x == -2) cxseen = 1;    /* If error, cancel this file. */
  1610.     } else {
  1611.         ffc++;            /* Count the character */
  1612.         if (docrc && what == W_SEND) { /* Accumulate file crc */
  1613.         z = crc16 ^ (long)((CHAR)x & 0xff);
  1614.         crc16 = (crc16 >> 8) ^
  1615.           (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
  1616.         }
  1617.     }
  1618.     rnext = (CHAR) (x & fmask);    /* Apply file mask */
  1619. /*
  1620.   At this point, the character we just read is in rnext,
  1621.   and the character we are about to encode into the packet is in rt.
  1622. */
  1623.     odp = dp;            /* Remember where we started. */
  1624.     xxrc = xxcq = NUL;        /* Clear these. */
  1625. /*
  1626.   Now encode the character according to the options that are in effect:
  1627.     ctlp[]: whether this control character needs prefixing.
  1628.     rptflg: repeat counts enabled.
  1629.     Other options don't apply in this routine.
  1630. */
  1631.     if (rptflg && rt == rnext && first == 0) { /* Got a run... */
  1632.         if (++rpt < 94) {        /* Below max, just count */
  1633.         continue;        /* go back and get another */
  1634.         } else if (rpt == 94) {    /* Reached max, must dump */
  1635.         xxrc = (CHAR) tochar(rpt); /* Put the repeat count here */
  1636.         rptn += rpt;        /* Accumulate it for statistics */
  1637.         rpt = 0;        /* And reset it */
  1638.         }
  1639.     } else if (rpt > 0) {        /* End of run */
  1640.         xxrc = (CHAR)tochar(++rpt); /* The count */
  1641.         rptn += rpt;        /* For stats */
  1642.         rpt = 0;            /* Reset repeat count */
  1643.     }
  1644.     a7 = rt & 0177;            /* Get low 7 bits of character */
  1645.     if (
  1646. #ifdef CK_SPEED
  1647.         ctlp[(unsigned)(rt & 0xff)]    /* Lop off any "sign" extension */
  1648. #else
  1649.         (a7 < SP) || (a7 == DEL)
  1650. #endif /* CK_SPEED */
  1651.         ) {                /* Do control prefixing if necessary */
  1652.         xxcq = myctlq;        /* The prefix */
  1653.         ccp++;            /* Count it */
  1654.         rt = (CHAR) ctl(rt);    /* Uncontrollify the character */
  1655.     }
  1656. #ifdef CK_SPEED
  1657.     else if ((a7 < SP) || (a7 == DEL)) /* Count an unprefixed one */
  1658.       ccu++;
  1659. #endif /* CK_SPEED */
  1660.  
  1661.     if (a7 == myctlq)        /* Always prefix the control prefix */
  1662.       xxcq = myctlq;
  1663.  
  1664.     if ((rptflg) && (a7 == rptq))    /* If it's the repeat prefix, */
  1665.       xxcq = myctlq;        /* prefix it if doing repeat counts */
  1666.  
  1667. /* Now construct the prefixed sequence */
  1668.  
  1669.     if (xxrc) {            /* Repeat count */
  1670.         if (xxrc == (CHAR) '"' && !xxcq) { /* 2 in a row & not prefixed */
  1671.         *dp++ = rt;        /* So just do this */
  1672.         } else {            /* More than two or prefixed */
  1673.         *dp++ = (CHAR) rptq; *dp++ = xxrc; /* Emit repeat sequence */
  1674.         }
  1675.     }
  1676.     if (xxcq) { *dp++ = myctlq; }    /* Control prefix */
  1677.     *dp++ = rt;            /* Finally, the character itself */
  1678.     rt = rnext;            /* Next character is now current. */
  1679.  
  1680. /* Done encoding the character.  Now take care of packet buffer overflow. */
  1681.  
  1682.     size = dp - data;        /* How many bytes we put in buffer. */
  1683.     if (size >= bufmax) {        /* If too big, save some for next. */
  1684.         *dp = '\0';            /* Mark the end. */
  1685.         if (size > bufmax) {    /* if packet is overfull */
  1686.         /* Copy the part that doesn't fit into the leftover buffer, */
  1687.         /* taking care not to split a prefixed sequence. */
  1688.         int i;
  1689.         nleft = dp - odp;
  1690.         p1 = leftover;
  1691.         p2 = odp;
  1692.         for (i = 0; i < nleft; i++)
  1693.           *p1++ = *p2++;
  1694.         size = odp - data;    /* Return truncated packet. */
  1695.         *odp = '\0';        /* Mark the new end */
  1696.         }
  1697.         t = rt;            /* Save for next time */
  1698.         return(size);
  1699.     }
  1700.     }                    /* Otherwise, keep filling. */
  1701.     size = dp - data;            /* End of file */
  1702.     *dp = '\0';                /* Mark the end of the data. */
  1703.     return(size);             /* Return partially filled last packet. */
  1704. }
  1705. #endif /* CKTUNING */
  1706.  
  1707. VOID
  1708. dofilcrc(c) int c; {            /* Accumulate file crc */
  1709.     long z;
  1710.     z = crc16 ^ (long)c;
  1711.     crc16 = (crc16 >> 8) ^
  1712.       (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
  1713. }
  1714.  
  1715. /* For SENDing from an array... */
  1716.  
  1717. int
  1718. agnbyte() {                /* Get next byte from array */
  1719. #ifndef NOSPL
  1720.     char c;
  1721.     static int save = 0;        /* For CRLF */
  1722.     static char ** ap = NULL;        /* Array pointer */
  1723.     static char * p = NULL;        /* Character pointer */
  1724.     static int i = 0, n = 0;        /* Array index and limit */
  1725.     extern int a_dim[];            /* Array dimension */
  1726.     int x = 1;
  1727.     if (!ap) {                /* First time thru */
  1728.     ap = sndarray;            /* Set up array pointers */
  1729.     if (!ap || (i = sndxlo) > a_dim[sndxin]) {
  1730.         sndarray = NULL;
  1731.         ap = NULL;
  1732.         return(-1);
  1733.     }
  1734.     p = ap[i];            /* Point to first element in range */
  1735.     n = sndxhi;            /* Index of last element in range */
  1736.     if (sndxhi > a_dim[sndxin])    /* Adjust if necessary */
  1737.       n = a_dim[sndxin];
  1738.     }
  1739.     if (save) {                /* If anything saved */
  1740.     c = save;            /* unsave it */
  1741.     save = 0;            /* and return it */
  1742.     return(c & 0xff);
  1743.     }
  1744.     if (i > n) {            /* No more elements */
  1745.     sndarray = NULL;
  1746.     ap = NULL;
  1747.     return(-1);
  1748.     }
  1749.     if (!p)                /* Source pointer is NULL */
  1750.       c = NUL;                /* this means an empty line */
  1751.     else                /* Source pointer not NULL */
  1752.       c = *p++;                /* Next char */
  1753.     if (!c) {                /* Char is empty? */
  1754.     if (!binary) {            /* Text: end of line. */
  1755.         if (feol) {            /* Supply NL */
  1756.         c = feol;
  1757.         } else {            /* or CRLF */
  1758.         save = LF;
  1759.         c = CR;
  1760.         }
  1761.         p = ap[++i];
  1762.         return(c & 0xff);
  1763.     }
  1764.     while (i++ < n) {        /* Binary - get next element */
  1765.         p = ap[i];
  1766.         if (!p)            /* Empty line? */
  1767.           continue;            /* Ignore it and get another */
  1768.         c = *p++;            /* Get next char */
  1769.         if (!c)            /* Emtpy char? */
  1770.           continue;            /* Ignore it and get another */
  1771.         return(c & 0xff);        /* Not empty - return it */
  1772.     }
  1773.     sndarray = NULL;
  1774.     ap = NULL;
  1775.     return(-1);            /* Done */
  1776.     }
  1777.     return(c & 0xff);            /* Char is not empty */
  1778. #else
  1779.     sndarray = NULL;
  1780.     return(-1);
  1781. #endif /* NOSPL */
  1782. }
  1783. #endif /* NOXFER */
  1784.  
  1785. #ifndef NOCSETS
  1786. static CHAR xlabuf[32] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  1787. static int xlacount = 0;
  1788. static int xlaptr = 0;
  1789. static USHORT lastucs2 = 0;
  1790. /*
  1791.   X G N B Y T E --  Get next translated byte from the input file.
  1792.  
  1793.   Returns the next byte that is to be put into the packet, already translated.
  1794.   This isolates getpkt() from having to know anything about translation,
  1795.   single- vs multibyte character sets, one-to-many vs many-to-one, etc, but it
  1796.   has rather high overhead, so don't call it unless you know translation is
  1797.   needed to or from Unicode, Japanese, or other multibyte character set.
  1798.  
  1799.   Call with:
  1800.     fcs:  File character set (source, file we are reading from)
  1801.     tcs:  Target character set (use an FC_xxx code, not a TC_xxx code)
  1802.   Returns:
  1803.     >= 0: A translated byte suitable for writing.
  1804.     <  0: Fatal error (such as EOF on input source).
  1805. */
  1806. int
  1807. xgnbyte(tcs,fcs) int tcs, fcs; {
  1808.     _PROTOTYP( int (*xx), (USHORT) ) = NULL;
  1809. #ifdef UNICODE
  1810.     extern int ucsorder;        /* SET FILE UCS BYTE-ORDER */
  1811. #endif /* UNICODE */
  1812.     int haveuc = 0;            /* Flag for have Unicode character */
  1813. #ifdef KANJI
  1814.     int havesj = 0;            /* Have Shift-JIS character */
  1815.     int haveeu = 0;            /* Have EUC-JP character */
  1816. #endif /* KANJI */
  1817.     int rc = -1, x = 0, flag = 0;
  1818.     int utferror = 0;
  1819.     int eolflag = 0;
  1820.     unsigned int xc, thischar;
  1821.     static int swapping = 0;
  1822.     CHAR rt;
  1823.     USHORT ch;
  1824. #ifdef UNICODE
  1825.     union ck_short uc;
  1826. #endif /* UNICODE */
  1827. #ifdef KANJI
  1828.     union ck_short sj, eu;        /* Shift-JIS character */
  1829. #endif /* KANJI */
  1830.  
  1831. #ifdef KANJI
  1832.     sj.x_short = 0;
  1833. #endif /* KANJI */
  1834.  
  1835.     if (xlacount-- > 0) {        /* We already have some */
  1836.     x = xlabuf[xlaptr++];
  1837.     debug(F001,"xgnbyte SEND from buf","",x);
  1838.     return(x);
  1839.     }
  1840.     if (xlatype != XLA_NONE) {        /* Not not translating... */
  1841.     haveuc = 0;
  1842. #ifdef UNICODE
  1843.     if (fcs == FC_UCS2) {        /* UCS-2: Read two bytes */
  1844.         if (ffc == 0) {        /* Beginning of file? */
  1845.         swapping = 0;        /* Reset byte-swapping flag */
  1846.         }
  1847.         uc.x_short = 0;
  1848.       bomskip:
  1849.         x = zminchar();        /* Get first byte */
  1850.         flag = 1;            /* Remember we called zminchar() */
  1851.         if (x > -1) {        /* Didn't fail */
  1852.         ffc++;            /* Count a file byte */
  1853.         uc.x_char[swapping] = x & 0xff;
  1854. #ifndef NOXFER
  1855.         if (docrc && what == W_SEND)
  1856.           dofilcrc(x);
  1857. #endif /* NOXFER */
  1858.         if ((x = zminchar()) > -1) { /* If didn't fail */
  1859.             ffc++;        /* count another file byte */
  1860.             uc.x_char[1-swapping] = x & 0xff;
  1861.             haveuc = 1;    /* And remember we have Unicode */
  1862. #ifndef NOXFER
  1863.             if (docrc && what == W_SEND)
  1864.               dofilcrc(x);
  1865. #endif /* NOXFER */
  1866.             if (ffc == 2) {    /* Second char of file */
  1867.             debug(F001,"xgnbyte 1st UCS2","",uc.x_short);
  1868.             if (uc.x_short == (USHORT)0xfeff) {
  1869.                 swapping = 0;
  1870.                 debug(F101,
  1871.                   "xgnbyte UCS2 goodbom swap","",swapping);
  1872.                 goto bomskip;
  1873.             } else if (uc.x_short == (USHORT)0xfffe) {
  1874.                 swapping = 1;
  1875.                 debug(F101,
  1876.                   "xgnbyte UCS2 badbom swap","",swapping);
  1877.                 goto bomskip;
  1878.             } else if ((byteorder && !ucsorder) ||
  1879.                    (!byteorder && ucsorder)) {
  1880.                 CHAR c;
  1881.                 c = uc.x_char[0];
  1882.                 uc.x_char[0] = uc.x_char[1];
  1883.                 uc.x_char[1] = c;
  1884.                 swapping = 1;
  1885.                 debug(F101,
  1886.                   "xgnbyte UCS2 no BOM X swap","",swapping);
  1887.             } else {
  1888.                 swapping = 0;
  1889.                 debug(F101,
  1890.                   "xgnbyte UCS2 no BOM Y swap","",swapping);
  1891.             }
  1892.             }
  1893.         } else
  1894.           return(x);
  1895.         } else
  1896.           return(x);
  1897.         debug(F001,"xgnbyte UCS2","",uc.x_short);
  1898.  
  1899.     } else if (fcs == FC_UTF8) {    /* File is UTF-8 */
  1900.         CHAR ch = 0;        /* Data types needed for API... */
  1901.         USHORT * us = NULL;
  1902.         uc.x_short = 0;
  1903.         flag = 1;            /* We (will) have called zminchar() */
  1904.         while ((x = zminchar()) > -1) { /* Read source bytes */
  1905.         ffc++;            /* Got a byte - count it */
  1906. #ifndef NOXFER
  1907.         if (docrc && what == W_SEND)
  1908.           dofilcrc(x);
  1909. #endif /* NOXFER */
  1910.         ch = x;
  1911.         rc = utf8_to_ucs2(ch,&us); /* Convert to UCS-2 */
  1912.         if (rc == 0) {        /* Done */
  1913.             uc.x_short = *us;
  1914.             haveuc = 1;
  1915.             break;
  1916.         } else if (rc < 0) {    /* Error */
  1917.             utferror = 1;
  1918.             debug(F101,"xgnbyte UTF-8 input error","",rc);
  1919.             haveuc = 1;
  1920.             uc.x_short = *us;
  1921.             break;
  1922.         }
  1923.         }
  1924.         if (x < 0)
  1925.           return(x);
  1926.         debug(F001,"xgnbyte UTF8->UTF2","",uc.x_short);
  1927.     }
  1928. #endif /* UNICODE */
  1929.  
  1930. #ifdef KANJI
  1931. #ifdef UNICODE
  1932.     else
  1933. #endif /* UNICODE */
  1934.       if (fcsinfo[fcs].alphabet == AL_JAPAN) { /* Japanese source file */
  1935.         int c7, x, y, done = 0;
  1936.         if (fcs == FC_JIS7) {    /* If file charset is JIS-7 */
  1937.         if (ffc == 0L)        /* If first byte of file */
  1938.           j7init();        /* Initialize JIS-7 parser */
  1939.         x = getj7();        /* Get a JIS-7 byte */
  1940.         } else            /* Otherwise */
  1941.           x = zminchar();        /* Just get byte */
  1942.         if (x < 0) {        /* Propogate EOF or error */
  1943.         debug(F100,"XGNBYTE EOF","",0);
  1944.         return(x);
  1945.         }
  1946.         debug(F001,"XGNBYTE x","",x);
  1947.         ffc++;            /* Count */
  1948. #ifndef NOXFER
  1949.         if (docrc && what == W_SEND) dofilcrc(x); /* Do CRC */
  1950. #endif /* NOXFER */
  1951.         switch (fcs) {        /* What next depends on charset */
  1952.           case FC_SHJIS:        /* Shift-JIS */
  1953.         if ((x <= 0x80) ||    /* Any 7-bit char... */
  1954.             (x >= 0xa0 && x <= 0xdf)) { /* or halfwidth Katakana */
  1955.             sj.x_short = (USHORT) x;    /* we read one byte. */
  1956.         } else {        /* Anything else */
  1957.             if ((y = zminchar()) < 0) /* get another */
  1958.               return(y);
  1959. #ifndef NOXFER
  1960.             if (docrc && what == W_SEND) dofilcrc(y);
  1961. #endif /* NOXFER */
  1962.             ffc++;
  1963.             sj.x_char[byteorder] = (CHAR) x;
  1964.             sj.x_char[1-byteorder] = (CHAR) y;
  1965.         }
  1966.         break;
  1967.  
  1968.           case FC_JIS7:        /* JIS-7 */
  1969.           case FC_JDEC:        /* DEC Kanji */
  1970.           case FC_JEUC:        /* EUC-JP */
  1971.         if ((x & 0x80) == 0) {    /* Convert to Shift-JIS */
  1972.             sj.x_short = (USHORT) x; /* C0 or G0: one byte */
  1973.             eu.x_short = (USHORT) x;
  1974.             haveeu = 1;
  1975.         } else {
  1976.             c7 = x & 0x7f;
  1977.             if (c7 > 0x20 && c7 < 0x7f) { /* Kanji: two bytes */
  1978.             if ((y = (fcs == FC_JEUC) ? zminchar() : getj7()) < 0)
  1979.               return(y);
  1980.             ffc++;
  1981. #ifndef NOXFER
  1982.             if (docrc && what == W_SEND) dofilcrc(y);
  1983. #endif /* NOXFER */
  1984.             eu.x_char[byteorder] = (CHAR) x;
  1985.             eu.x_char[1-byteorder] = (CHAR) y;
  1986.             sj.x_short = eu_to_sj(eu.x_short);
  1987.             haveeu = 1;
  1988.             } else if (x == 0x8e) { /* SS2 Katakana prefix: 2 bytes */
  1989.             if ((y = (fcs == FC_JIS7) ? getj7() : zminchar()) < 0)
  1990.               return(y);
  1991.             ffc++;
  1992. #ifndef NOXFER
  1993.             if (docrc && what == W_SEND) dofilcrc(y);
  1994. #endif /* NOXFER */
  1995.             sj.x_short = y | 0x80;
  1996.             debug(F001,"XGNBYTE KANA SJ","",sj.x_short);
  1997.             } else {
  1998.             /* Something that translates to U+FFFD */
  1999.             sj.x_short = UNKSJIS;
  2000.             }
  2001.         }
  2002.         break;
  2003.         }
  2004.         havesj = 1;            /* Have Shift-JIS */
  2005. #ifdef UNICODE
  2006.         uc.x_short = sj_to_un(sj.x_short); /* Translate to UCS-2 */
  2007.         haveuc = 1;            /* Have Unicode */
  2008. #endif /* UNICODE */
  2009.         flag = 1;            /* Have a char */
  2010.     }
  2011. #endif /* KANJI */
  2012.     }
  2013.     if (!flag) {            /* If no character was read yet... */
  2014.     if ((x = zminchar()) > -1)    /* read one now */
  2015.       ffc++;
  2016.     debug(F101,"xgnbyte zminchar 1","",x);
  2017.     if (x < 0)
  2018.       return(x);
  2019.     haveuc = 0;
  2020.     }
  2021. #ifdef UNICODE
  2022.     if (haveuc) {
  2023.     thischar = uc.x_short;
  2024.     lastucs2 = uc.x_short;
  2025.     } else
  2026. #endif /* UNICODE */
  2027.       thischar = x;
  2028.     debug(F001,"xgnbyte thischar",haveuc ? "[UNICODE]" : "[other]",thischar);
  2029.  
  2030. #ifdef CK_CTRLZ                /* SET EOF CTRLZ */
  2031.     if (eofmethod == XYEOF_Z && !binary && thischar == 26) {
  2032.     debug(F100,"xgnbyte EOF on Ctrl-Z 1","",0);
  2033.     return(-1);
  2034.     }
  2035. #endif /* CK_CTRLZ */
  2036.  
  2037. #ifdef UNICODE
  2038.     if (!haveuc)            /* If not Unicode... */
  2039. #endif /* UNICODE */
  2040.       x &= fmask;            /* Apply SET FILE BYTESIZE mask */
  2041.  
  2042.     switch (xlatype) {            /* Translation type... */
  2043. #ifdef UNICODE
  2044.       case XLA_UNICODE: {        /* Unicode is involved */
  2045.       xc = 0;
  2046. /*
  2047.   Here we must choose the appropriate translation function.  If we are being
  2048.   called by getpkt() (i.e. when transferring a file), we are translating from
  2049.   Unicode to the Transfer Character Set and therefore must use the function
  2050.   pointed to by xut.  Otherwise, e.g. during TRANSLATE, CONNECT, TRANSMIT, etc,
  2051.   we are translating from Unicode to the File Character Set and so must call
  2052.   the function pointed to by xuf.  There might be a cleaner way to set this
  2053.   up but I don't think so.  For example, setxlatype() might have been called
  2054.   too soon and so might not have known whether it was a file transfer or a
  2055.   local operation.
  2056. */
  2057.       xx = (what == W_SEND) ? xut : xuf;
  2058.       eolflag = 0;
  2059.       if (haveuc) {            /* File is Unicode */
  2060.           /* See Unicode TR13, "Converting to Other Character Sets" */
  2061.           if (uc.x_short == 0x2028 || /* Line Separator? */
  2062.           uc.x_short == 0x2029 || /* Paragraph Separator */
  2063.           (feol && (uc.x_short == (USHORT)feol))
  2064.           ) {
  2065.           debug(F001,"xgnbyte uc eol","",uc.x_short);
  2066.           rc = 0;
  2067.           eolflag = 1;        /* Don't translate and handle later */
  2068.           }
  2069.           if (xx && !eolflag) {    /* UCS-to-TCS function (UCS->byte) */
  2070.           rc = (*xx)(uc.x_short); /* These can fail... */
  2071.           debug(F101,"xgnbyte xx rc","",rc);
  2072.           if (rc < 0)        /* If it can't be translated */
  2073.             uc.x_short = UNK;    /* Put unknown-character symbol */
  2074.           else
  2075.             uc.x_short = (unsigned)((unsigned)rc & 0xffff);
  2076.           debug(F101,"xgnbyte xx uc","",uc.x_short);
  2077.           }
  2078. #ifdef KANJI
  2079.           if (tcs == FC_JEUC) {    /* Translating to EUC-JP */
  2080.           USHORT sj;
  2081.           union ck_short eu;
  2082.           debug(F001,"xgnbyte UCS->EUC UCS","",uc.x_short);
  2083.           if (!havesj)        /* If we don't already have it */
  2084.             sj = un_to_sj(uc.x_short); /* convert to Shift-JIS */
  2085.           eu.x_short = sj_to_eu(sj);
  2086.           debug(F001,"xgnbyte UCS->EUC EUC","",eu.x_short);
  2087.           xlaptr = 0;
  2088.           xlacount = 0;
  2089.           if (eolflag) {
  2090.               if (what == W_SEND) {
  2091.               xlabuf[xlacount++] = LF;
  2092.               return(CR);
  2093.               } else {
  2094.               return(feol);
  2095.               }
  2096.           }
  2097.           if (eu.x_char[byteorder]) {    /* Two bytes */
  2098.               rc = eu.x_char[byteorder];
  2099.               xlabuf[xlacount++] = eu.x_char[1-byteorder];
  2100.               debug(F001,"xgnbyte UCS->EUC xlabuf[0]","",xlabuf[0]);
  2101.           } else {        /* One byte */
  2102.               rc = eu.x_char[1-byteorder];
  2103.           }
  2104.           debug(F101,"xgnbyte UCS->EUC xlacount","",xlacount);
  2105.           debug(F001,"xgnbyte UCS->EUC rc","",rc);
  2106.           return(rc);
  2107.           } else
  2108. #endif /* KANJI */
  2109.           if (tcs != FC_UCS2 && tcs != FC_UTF8) {
  2110.           if (uc.x_short & 0xff00) {    /* Decoding error */
  2111.               debug(F001,"xgnbyte decoding error","",uc.x_short);
  2112.               return(-2);
  2113.           } else
  2114.             return((unsigned int)(uc.x_short & 0xff));
  2115.           }
  2116.           xc = uc.x_short;
  2117.  
  2118.       } else {            /* File is not Unicode */
  2119.           USHORT ch;
  2120.           /* Translate from single FCS byte to UCS-2 */
  2121. /*
  2122.   This is a bit nonobvious...  The blah_u() (Blah-to-Unicode) routines are
  2123.   called only with pieces of character sets, in the ISO 2022 sense.  So,
  2124.   for example, if ch is a Latin-1 character, we call the translation
  2125.   routine only if it is in the right half; if it's in the left half, it
  2126.   isn't translated, and in fact will give the wrong result if sent to the
  2127.   translation function.  That's because those functions were designed for
  2128.   use with the ISO 2022 G0..G3 sets, not for file transfer.  On the other
  2129.   hand, if it's a 7-bit character set, we *do* call the translation
  2130.   function.  (To put it another way, the left half of any 8-bit character
  2131.   set is ASCII and therefore doesn't need to be translated but 7-bit sets
  2132.   such as ISO 646 German do need translation).
  2133. */
  2134.           ch = (unsigned)(thischar & 0xff);
  2135.           if (((fcsinfo[fcs].size > 128) && (ch & 0x80)) ||
  2136.           fcsinfo[fcs].size <= 128) {
  2137.           if (xfu) {         /* FCS-to-UCS function */
  2138.               ch = (*xfu)(ch);
  2139.           }
  2140.           }
  2141.           xc = ch;
  2142.       }
  2143.       /* At this point we have a UCS-2 character in native format */
  2144.       /* (Big Endian or Little Endian) in xc, which is an unsigned int. */
  2145.  
  2146.       debug(F001,"xgnbyte xc","",xc);
  2147.  
  2148.       if (tcs == FC_UTF8) {        /* Now convert to UTF-8 */
  2149.           USHORT c;            /* NOTE: this is FC_UTF8 on purpose! */
  2150.           CHAR * buf = NULL;
  2151.           int i, k = 0, x;
  2152.  
  2153.           xlaptr = 0;
  2154.           if (utferror) {
  2155.           xlabuf[k++] = 0xff;
  2156.           xlabuf[k++] = 0xbd;
  2157.           }
  2158.           if (eolflag) {        /* We detected EOL in source file */
  2159.           if (what == W_SEND) {    /* Convert to CRLF */
  2160.               xlabuf[k++] = LF;
  2161.               xlacount = k;
  2162.               return((unsigned int)CR);
  2163.           } else {        /* Or to local line-end */
  2164.               xlacount = k;
  2165.               return((unsigned int)feol);
  2166.           }
  2167.           }
  2168.           c = xc;
  2169.           if ((x = ucs2_to_utf8(c,&buf)) < 1) {
  2170.           debug(F101,"xgnbyte ucs2_to_utf8 error","",c);
  2171.           return(-2);
  2172.           }
  2173.           debug(F101,"xgnbyte UTF8 buf[0]","",buf[0]);
  2174.           for (i = 1; i < x; i++) {
  2175.           xlabuf[k+i-1] = buf[i];
  2176.           debug(F111,"xgnbyte UTF8 xlabuf",ckitoa(i-1),buf[i]);
  2177.           }
  2178.           xlaptr = 0;
  2179.           xlacount = x - 1;
  2180.           debug(F101,"xgnbyte UTF8 xlacount","",xlacount);
  2181.           return((unsigned int)buf[0]);
  2182.       } else {            /* Or keep it as UCS-2 */
  2183.           int k = 0;
  2184.           CHAR c;
  2185.           xlaptr = 0;
  2186.           if (utferror) {
  2187.           xlabuf[k++] = 0xff;
  2188.           xlabuf[k++] = 0xfd;
  2189.           debug(F101,"xgnbyte error","",k);
  2190.           }
  2191.           if (eolflag) {        /* We detected EOL in source file */
  2192.           if (what == W_SEND) {    /* Convert to CRLF */
  2193.               xlabuf[k++] = CR;
  2194.               xlabuf[k++] = NUL;
  2195.               xlabuf[k++] = LF;
  2196.               xlacount = k;
  2197.               debug(F101,"xgnbyte send CRLF","",k);
  2198.               return(0);    /* Return NUL */
  2199.           } else {        /* Or to local line-end */
  2200.               xlabuf[k++] = (CHAR)feol;
  2201.               xlacount = k;
  2202.               debug(F101,"xgnbyte send feol","",k);
  2203.               return(0);    /* Return NUL */
  2204.           }
  2205.           }
  2206.           if (what == W_SEND || !ucsorder) {
  2207.           xlabuf[k++] = (xc >> 8) & 0xff; /* Big Endian */
  2208.           xlabuf[k++] = xc & 0xff;
  2209.           debug(F110,"xgnbyte to UCS2 BE",ckitoa(xlabuf[0]),0);
  2210.           } else {            /* Little Endian */
  2211.           xlabuf[k++] = xc & 0xff;
  2212.           xlabuf[k++] = (xc >> 8) & 0xff;
  2213.           debug(F110,"xgnbyte to UCS2 LE",ckitoa(xlabuf[0]),0);
  2214.           }
  2215.           c = xlabuf[0];
  2216.           xlaptr = 1;
  2217.           xlacount = k-1;
  2218.           debug(F101,"xgnbyte c","",c);
  2219.           debug(F101,"xgnbyte xlaptr","",xlaptr);
  2220.           debug(F101,"xgnbyte xlacount","",xlacount);
  2221.           return((unsigned int)c);
  2222.       }
  2223.       }
  2224. #endif /* UNICODE */
  2225.       case XLA_NONE:
  2226.     return(zminchar());
  2227.       case XLA_BYTE:            /* Byte-for-Byte translation */
  2228.     rt = x;
  2229.     if (sx)
  2230.       rt = (*sx)(rt);
  2231. #ifdef UNICODE
  2232.     if (utferror) {
  2233.         xlaptr = 0;
  2234.         xlacount = 1;
  2235.         xlabuf[0] = rt;
  2236.         return(UNK);
  2237.     } else
  2238. #endif /* UNICODE */
  2239.       return((unsigned int)rt);
  2240.  
  2241. #ifdef KANJI
  2242.       case XLA_JAPAN:            /* Come here with Shift-JIS */
  2243.     if (tcs == FC_JEUC) {        /* It better be... */
  2244.         xlaptr = 0;
  2245.         xlacount = 0;
  2246.         if (!havesj) {
  2247.         printf("BAD BAD\n");
  2248.         return(-2);
  2249.         }
  2250.         if (!haveeu)        /* We might already have EUC too */
  2251.           eu.x_short = sj_to_eu(sj.x_short);
  2252.         if (eu.x_char[byteorder]) {
  2253.         xlabuf[xlacount++] = eu.x_char[1-byteorder];
  2254.         return(eu.x_char[byteorder]);
  2255.         } else {
  2256.         return(eu.x_char[1-byteorder]);
  2257.         }
  2258.         break;
  2259.     }
  2260. #endif /* KANJI */
  2261.  
  2262.       default:
  2263.     debug(F101,"xgnbyte bad xlatype","",xlatype);
  2264.     return(-2);
  2265.     }
  2266.     /* NOTREACHED */
  2267.     /* Some compilers complain if this is not here, others if it is. */
  2268.     debug(F100,"xgnbyte switch failure","",0);
  2269.     return(-2);
  2270. }
  2271. #endif /* NOCSETS */
  2272.  
  2273. #ifndef NOXFER
  2274.  
  2275. /*  G E T P K T  --  Fill a packet data field from the indicated source.  */
  2276.  
  2277. /*
  2278.   Parameters:
  2279.     bufmax: Maximum length of entire packet.
  2280.     xlate:  Flag for whether to translate charsets when in text mode.
  2281.   Returns:  Number of characters written to packet data field, 0 or more,
  2282.             Or -1 on failure (internal error),
  2283.             or -3 on timeout (e.g. when reading from a pipe).
  2284.  
  2285.   This is the full version allowing for parity and text-mode conversions;
  2286.   i.e. it works in all cases.   Also see bgetpkt(), a special lean/mean/fast
  2287.   packet encoder that works only for binary-mode no-parity transfers.
  2288. */
  2289. static int uflag = 0;
  2290.  
  2291. int
  2292. getpkt(bufmax,xlate) int bufmax, xlate; { /* Fill one packet buffer */
  2293.     register CHAR rt = t, rnext = NUL;      /* Register shadows of the globals */
  2294.     register CHAR *dp, *odp, *odp2, *p1, *p2; /* pointers... */
  2295.     register int x;            /* Loop index. */
  2296.     register int a7;            /* Low 7 bits of character */
  2297.     int thischar = 0;            /* Might be byte or wide */
  2298.  
  2299.     CHAR xxls, xxdl, xxrc, xxss, xxcq;    /* Pieces of prefixed sequence */
  2300.  
  2301.     if (binary) xlate = 0;        /* We don't translate if binary */
  2302.  
  2303.     if (!data) {
  2304.     debug(F100,"SERIOUS ERROR: getpkt data == NULL","",0);
  2305.     return(-1);
  2306.     }
  2307.     dp = data;                /* Point to packet data buffer */
  2308.     size = 0;                /* And initialize its size */
  2309. /*
  2310.   Assume bufmax is the receiver's total receive-packet buffer length.
  2311.   Our whole packet has to fit into it, so we adjust the data field length.
  2312.   We also decide optimally whether it is better to use a short-format or
  2313.   long-format packet when we're near the borderline.
  2314. */
  2315.     bufmax = maxdata();            /* Get maximum data length */
  2316.  
  2317.     if (first == 1) {            /* If first character of this file.. */
  2318. #ifdef UNICODE
  2319.     /* Special end-of-line handling for Unicode */
  2320.     if (tcharset == TC_UCS2 || tcharset == TC_UTF8)
  2321.       uflag = 1;
  2322. #endif /* UNICODE */
  2323.     debug(F101,"getpkt first uflag","",uflag);
  2324.     debug(F101,"getpkt first rt","",rt);
  2325.     if (!memstr && !funcstr)    /* and real file... */
  2326.       ffc = 0L;            /* reset file character counter */
  2327. #ifdef COMMENT
  2328.     /* Moved to below... */
  2329.     first = 0;            /* Next character won't be first */
  2330. #endif /* COMMENT */
  2331.     *leftover = '\0';        /* Discard any interrupted leftovers */
  2332.     nleft = 0;
  2333. #ifndef NOCSETS
  2334.     setxlatype(tcharset,fcharset);    /* Set up charset translations */
  2335. #endif /* NOCSETS */
  2336.  
  2337.     /* Get first character of file into rt, watching out for null file */
  2338.  
  2339. #ifdef CALIBRATE
  2340.     if (calibrate && !memstr) {
  2341. #ifdef NORANDOM
  2342.         x = rt = 53;
  2343. #else
  2344.         x = rt = cal_a[rand() & 0xff];
  2345. #endif /* NORANDOM */
  2346.         first = 0;
  2347.         ffc++;
  2348.     } else
  2349. #endif /* CALIBRATE */
  2350. #ifdef KANJI
  2351.     if (xlate && tcharset == TC_JEUC) { /* Kanji text */
  2352.         x = zkanjf();
  2353.         if ((x = zkanji(memstr ? kgetm : kgetf)) < 0) {
  2354.             first = -1;
  2355.             size = 0;
  2356.             if (x == -2) {
  2357.                 debug(F100,"getpkt zkanji: input error","",0);
  2358.                 cxseen = 1;
  2359.             } else debug(F100,"getpkt zkanji: empty string/file","",0);
  2360.             return(0);
  2361.         }
  2362.         rt = x;
  2363.         first = 0;
  2364.         if (!memstr) {
  2365.         ffc++;
  2366.         if (docrc && what == W_SEND) /* Accumulate file crc */
  2367.           dofilcrc((int)rt);
  2368.         }
  2369.     } else {            /* Not Kanji text */
  2370. #endif /* KANJI */
  2371.         if (memstr) {        /* Reading data from memory string */
  2372.         /* This will not be Unicode */
  2373.         if ((rt = *memptr++) == '\0') { /* end of string ==> EOF */
  2374.             first = -1;
  2375.             size = 0;
  2376.             debug(F100,"getpkt: empty string","",0);
  2377.             return(0);
  2378.         }
  2379.         first = 0;
  2380.         } else if (funcstr) {    /* Reading data from a function */
  2381.         /* This will not be Unicode */
  2382.         if ((x = (*funcptr)()) < 0) { /* End of input  */
  2383.             first = -1;
  2384.             size = 0;        /* Empty */
  2385.             return(0);
  2386.         }
  2387.         ffc++;            /* Count a file character */
  2388.         rt = (CHAR) x;        /* Convert int to char */
  2389.         first = 0;
  2390.         debug(F000,"getpkt funcstr","",rt);
  2391.  
  2392.         } else {            /* Reading data from a file */
  2393. #ifndef NOCSETS
  2394.         if (xlate && !binary) {    /* Could be Unicode */
  2395.             if (xlatype == XLA_UNICODE) {
  2396.             /* Get next translated byte */
  2397.             x = xgnbyte(cseqtab[tcharset],fcharset);
  2398.             debug(F101,"getpkt xgnbyte","",x);
  2399.             } else {        /* Not Unicode */
  2400.             x = zminchar();    /* Get next byte, translate below */
  2401.             debug(F101,"getpkt zminchar A","",x);
  2402.             }
  2403.         } else {        /* Just get next byte */
  2404. #endif /* NOCSETS */
  2405.             x = zminchar();
  2406.             debug(F101,"getpkt zminchar B","",x);
  2407. #ifndef NOCSETS
  2408.         }
  2409. #endif /* NOCSETS */
  2410.         if (x < 0) {        /* End of file or input error */
  2411.             if (x == -3) {    /* Timeout. */
  2412.             size = (dp-data);
  2413.             debug(F101,"getpkt timeout size","",size);
  2414.             return((size == 0) ? x : size);
  2415.             }
  2416.             first = -1;
  2417.             size = 0;
  2418.             if (x == -2) {    /* Error */
  2419.             debug(F100,"getpkt: input error","",0);
  2420.             cxseen = 1;    /* Interrupt the file transfer */
  2421.             } else {
  2422.             debug(F100,"getpkt empty file","",0);
  2423.             }
  2424.             return(0);
  2425.         }
  2426.         first = 0;        /* Next character won't be first */
  2427.         rt = (CHAR) x;        /* Convert int to char */
  2428. #ifndef NOCSETS
  2429.         if (xlatype != XLA_UNICODE || binary) {
  2430.             ffc++;
  2431.             if (sx)
  2432.               rt = (*sx)(rt);
  2433.             if (docrc && what == W_SEND)
  2434.               dofilcrc(x);
  2435.         }
  2436. #endif /* NOCSETS */
  2437. #ifdef DEBUG
  2438.         if (deblog)
  2439.           debug(F101,"getpkt 1st char","",rt);
  2440. #endif /* DEBUG */
  2441.         if (/* !haveuc && */ docrc && what == W_SEND)    /* File CRC */
  2442.           dofilcrc(x);
  2443.         }
  2444. #ifdef KANJI
  2445.     }
  2446. #endif /* KANJI */
  2447.     /* PWP: handling of feol is done later (in the while loop)... */
  2448.  
  2449.     } else if ((first == -1) && (nleft == 0)) { /* EOF from last time */
  2450. #ifdef DEBUG
  2451.     if (deblog) {
  2452.         debug(F101,"getpkt eof crc16","",crc16);
  2453.         debug(F101,"getpkt eof ffc","",ffc);
  2454.     }
  2455. #endif /* DEBUG */
  2456.         return(size = 0);
  2457.     }
  2458. /*
  2459.   Here we handle characters that were encoded for the last packet but
  2460.   did not fit, and so were saved in the "leftover" array.
  2461. */
  2462.     debug(F101,"getpkt nleft","",nleft);
  2463.     if (nleft) {
  2464.     for (p1 = leftover; nleft > 0; nleft--) /* Copy leftovers */
  2465.       *dp++ = *p1++;
  2466.     *leftover = '\0';            /* Delete leftovers */
  2467.     nleft = 0;
  2468.     }
  2469.     if (first == -1)            /* Handle EOF */
  2470.       return(size = (dp - data));
  2471.  
  2472. /* Now fill up the rest of the packet. */
  2473.  
  2474.     rpt = 0;                /* Initialize character repeat count */
  2475.  
  2476.     while (first > -1) {        /* Until EOF... */
  2477. #ifdef CALIBRATE
  2478.     if (calibrate && !memstr) {    /* We generate our own "file" */
  2479.         if (ffc >= calibrate) {    /* EOF */
  2480.         first = -1;
  2481.         ffc--;
  2482.         } else {            /* Generate next character */
  2483.         if (cal_j > CAL_M * ffc)
  2484.           cal_j = cal_a[ffc & 0xff];
  2485.         x = (unsigned)cal_a[(cal_j & 0xff)];
  2486.         if (x == rt) x ^= 2;
  2487.         }
  2488.         cal_j += (unsigned int)(ffc + CAL_O);
  2489.         ffc++;
  2490.     } else
  2491. #endif /* CALIBRATE */
  2492. #ifdef KANJI
  2493.       if (xlate && tcharset == TC_JEUC) {
  2494.           if ((x = zkanji(memstr ? kgetm : kgetf)) < 0) {
  2495.           first = -1;
  2496.           if (x == -2) cxseen = 1;
  2497.           } else if (!memstr) ffc++;
  2498.           rnext = (CHAR) (x & fmask);
  2499.       } else {
  2500. #endif /* KANJI */
  2501.         if (memstr) {        /* Get next char from memory string */
  2502.         if ((x = *memptr++) == '\0') /* End of string means EOF */
  2503.           first = -1;        /* Flag EOF for next time. */
  2504.         rnext = (CHAR) (x & fmask); /* Apply file mask */
  2505.         } else if (funcstr) {    /* Get next char from function */
  2506.         if ((x = (*funcptr)()) < 0) /* End of string means EOF */
  2507.           first = -1;        /* Flag EOF for next time. */
  2508.         rnext = (CHAR) (x & fmask); /* Apply file mask */
  2509.         } else {            /* From file... */
  2510. #ifndef NOCSETS
  2511.         if (xlate && !binary) {    /* Could be Unicode */
  2512.             if (xlatype == XLA_UNICODE) {
  2513.             /* Get next translated byte */
  2514.             x = xgnbyte(cseqtab[tcharset],fcharset);
  2515.             } else {        /* Not Unicode */
  2516.             x = zminchar(); /* Get next byte, translate below */
  2517.             debug(F101,"xgnbyte B zminchar","",x);
  2518.             }
  2519.         } else {        /* Just get next byte */
  2520. #endif /* NOCSETS */
  2521.             x = zminchar();
  2522.             debug(F101,"xgnbyte C zminchar","",x);
  2523. #ifndef NOCSETS
  2524.         }
  2525. #endif /* NOCSETS */
  2526.         if (x < 0) {        /* Check for EOF */
  2527.             if (x == -3) {    /* Timeout reading from pipe */
  2528.             t = rt;
  2529.             size = (dp-data);
  2530.             debug(F101,"getpkt timeout size","",size);
  2531.             return((size == 0) ? x : size);
  2532.             }
  2533.             first = -1;        /* Flag eof for next time. */
  2534.             if (x == -2) cxseen = 1; /* If error, cancel this file. */
  2535.         }
  2536.         rnext = (CHAR) (x & fmask); /* Apply file mask */
  2537. #ifndef NOCSETS
  2538.         if (xlatype != XLA_UNICODE) {
  2539. #endif /* NOCSETS */
  2540.             ffc++;
  2541. #ifndef NOCSETS
  2542.             if (sx)
  2543.               rt = (*sx)(rt);
  2544. #endif /* NOCSETS */
  2545.             if (docrc && what == W_SEND)
  2546.               dofilcrc(x);
  2547.  
  2548. #ifndef NOCSETS
  2549.         }
  2550. #endif /* NOCSETS */
  2551.         }
  2552. #ifdef KANJI
  2553.     }
  2554. #endif /* KANJI */
  2555. /*
  2556.   At this point, the character we just read is in rnext,
  2557.   and the character we are about to encode into the packet is in rt.
  2558. */
  2559.     odp = dp;            /* Remember where we started. */
  2560.      xxls = xxdl = xxrc = xxss = xxcq = NUL;    /* Clear these. */
  2561. /*
  2562.   Now encode the character according to the options that are in effect:
  2563.     ctlp[]: whether this control character needs prefixing.
  2564.     binary: text or binary mode.
  2565.     rptflg: repeat counts enabled.
  2566.     ebqflg: 8th-bit prefixing enabled.
  2567.     lscapu: locking shifts enabled.
  2568. */
  2569.     if (rptflg) {            /* Repeat processing is on? */
  2570.         if (!uflag &&
  2571.         /*
  2572.          * If the next char is really CRLF, then we cannot
  2573.          * be doing a repeat (unless CR,CR,LF which becomes
  2574.          * "~ <n-1> CR CR LF", which is OK but not most efficient).
  2575.          * I just plain don't worry about this case.  The actual
  2576.          * conversion from NL to CRLF is done after the rptflg if...
  2577.          */
  2578.         (!feol || binary || (feol && (rnext != feol))) &&
  2579.         (rt == rnext) && (first == 0)) { /* Got a run... */
  2580.         if (++rpt < 94) {    /* Below max, just count */
  2581.             continue;        /* go back and get another */
  2582.         } else if (rpt == 94) {    /* Reached max, must dump */
  2583.             xxrc = (CHAR) tochar(rpt); /* Put the repeat count here */
  2584.             rptn += rpt;    /* Accumulate it for statistics */
  2585.             rpt = 0;        /* And reset it */
  2586.         }
  2587.         } else if (rpt > 1) {    /* More than two */
  2588.         xxrc = (CHAR) tochar(++rpt); /* and count. */
  2589.         rptn += rpt;
  2590.         rpt = 0;        /* Reset repeat counter. */
  2591.         }
  2592.         /*
  2593.           If (rpt == 1) we must encode exactly two characters.
  2594.           This is done later, after the first character is encoded.
  2595.         */
  2596.     }
  2597.     /* If it's the newline character... */
  2598.     if (!uflag && !binary && feol && (rt == feol)) {
  2599.         if (lscapu && lsstate) {    /* If SHIFT-STATE is SHIFTED */
  2600.         if (ebqflg) {        /* If single shifts enabled, */
  2601.             *dp++ = (CHAR) ebq;    /* insert a single shift. */
  2602.         } else {        /* Otherwise must shift in. */
  2603.             *dp++ = myctlq;    /* Insert shift-out code */
  2604.             *dp++ = 'O';
  2605.             lsstate = 0;    /* Change shift state */
  2606.         }
  2607.         }
  2608. #ifdef CK_SPEED
  2609.         if (ctlp[CR]) {
  2610.         *dp++ = myctlq;        /* Insert carriage return directly */
  2611.         *dp++ = 'M';
  2612.         ccp++;
  2613.         } else {
  2614.         *dp++ = CR;        /* Perhaps literally */
  2615.         ccu++;
  2616.         }
  2617. #else /* !CK_SPEED */
  2618.         *dp++ = myctlq;        /* Insert carriage return directly */
  2619.         *dp++ = 'M';
  2620.         ccp++;
  2621. #endif /* CK_SPEED */
  2622.         rt = LF;            /* Now make next char be linefeed. */
  2623.     }
  2624. /*
  2625.   Now handle the 8th bit of the file character.  If we have an 8-bit
  2626.   connection, we preserve the 8th bit.  If we have a 7-bit connection,
  2627.   we employ either single or locking shifts (if they are enabled).
  2628. */
  2629.     a7 = rt & 0177;            /* Get low 7 bits of character */
  2630.     if (rt & 0200) {        /* 8-bit character? */
  2631.         if (lscapu) {        /* Locking shifts enabled? */
  2632.         if (!lsstate) {        /* Not currently shifted? */
  2633.             x = lslook(0200);    /* Look ahead */
  2634.             if (x != 0 || ebqflg == 0) { /* Locking shift decision */
  2635.             xxls = 'N';       /* Need locking shift-out */
  2636.             lsstate = 1;       /* and change to shifted state */
  2637.             } else if (ebqflg) {   /* Not worth it */
  2638.             xxss = (CHAR) ebq; /* Use single shift */
  2639.             }
  2640.         }
  2641.         rt = (CHAR) a7;        /* Replace character by 7-bit value */
  2642.         } else if (ebqflg) {    /* 8th bit prefixing is on? */
  2643.         xxss = (CHAR) ebq;    /* Insert single shift */
  2644.         rt = (CHAR) a7;        /* Replace character by 7-bit value */
  2645.         }
  2646. /*
  2647.   In case we have a 7-bit connection and this is an 8-bit character, AND
  2648.   neither locking shifts nor single shifts are enabled, then the character's
  2649.   8th bit will be destroyed in transmission, and a block check error will
  2650.   occur.
  2651. */
  2652.     } else if (lscapu) {        /* 7-bit character */
  2653.  
  2654.         if (lsstate) {        /* Comes while shifted out? */
  2655.         x = lslook(0);        /* Yes, look ahead */
  2656.         if (x || ebqflg == 0) {    /* Time to shift in. */
  2657.             xxls = 'O';        /* Set shift-in code */
  2658.             lsstate = 0;    /* Exit shifted state */
  2659.         } else if (ebqflg) {    /* Not worth it, stay shifted out */
  2660.             xxss = (CHAR) ebq;    /* Insert single shift */
  2661.         }
  2662.         }
  2663.     }
  2664.     /* If data character is significant to locking shift protocol... */
  2665.     if (lscapu && (a7 == SO || a7 == SI || a7 == DLE))
  2666.       xxdl = 'P';            /* Insert datalink escape */
  2667.  
  2668.     if (
  2669. #ifdef CK_SPEED
  2670.         /*
  2671.           Thwart YET ANOTHER unwanted, unneeded, and unloved sign
  2672.           extension.  This one was particularly nasty because it prevented
  2673.           255 (Telnet IAC) from being prefixed on some platforms -- e.g.
  2674.           VMS with VAX C -- but not others, thus causing file transfers to
  2675.           fail on Telnet connections by sending bare IACs.  Not to mention
  2676.           the stray memory reference.  Signed chars are a BAD idea.
  2677.         */
  2678.         ctlp[(unsigned)(rt & 0xff)]    /* Lop off any "sign" extension */
  2679. #else
  2680.         (a7 < SP) || (a7 == DEL)
  2681. #endif /* CK_SPEED */
  2682.         ) {                /* Do control prefixing if necessary */
  2683.         xxcq = myctlq;        /* The prefix */
  2684.         ccp++;            /* Count it */
  2685.         rt = (CHAR) ctl(rt);    /* Uncontrollify the character */
  2686.     }
  2687. #ifdef CK_SPEED
  2688.     else if ((a7 < SP) || (a7 == DEL)) /* Count an unprefixed one */
  2689.       ccu++;
  2690. #endif /* CK_SPEED */
  2691.  
  2692.     if (a7 == myctlq)        /* Always prefix the control prefix */
  2693.       xxcq = myctlq;
  2694.  
  2695.     if ((rptflg) && (a7 == rptq))    /* If it's the repeat prefix, */
  2696.       xxcq = myctlq;        /* prefix it if doing repeat counts */
  2697.  
  2698.     if ((ebqflg) && (a7 == ebq))    /* Prefix the 8th-bit prefix */
  2699.       xxcq = myctlq;        /* if doing 8th-bit prefixes */
  2700.  
  2701. /* Now construct the entire sequence */
  2702.  
  2703.     if (xxls) { *dp++ = myctlq; *dp++ = xxls; } /* Locking shift */
  2704.     odp2 = dp;                    /* (Save this place) */
  2705.     if (xxdl) { *dp++ = myctlq; *dp++ = xxdl; } /* Datalink escape */
  2706.     if (xxrc) { *dp++ = (CHAR) rptq; *dp++ = xxrc; } /* Repeat count */
  2707.     if (xxss) { *dp++ = (CHAR) ebq; }           /* Single shift */
  2708.     if (xxcq) { *dp++ = myctlq; }                /* Control prefix */
  2709.     *dp++ = rt;            /* Finally, the character itself */
  2710.  
  2711.     if (rpt == 1) {            /* Exactly two copies? */
  2712.         rpt = 0;
  2713.         p2 = dp;            /* Save place temporarily */
  2714.         for (p1 = odp2; p1 < p2; p1++) /* Copy the old chars over again */
  2715.           *dp++ = *p1;
  2716.         if ((p2-data) <= bufmax) odp = p2; /* Check packet bounds */
  2717.         if ((p2-data) < bufmax) odp = p2; /* Check packet bounds */
  2718.     }
  2719.     rt = rnext;            /* Next character is now current. */
  2720.  
  2721. /* Done encoding the character.  Now take care of packet buffer overflow. */
  2722.  
  2723.     if ((dp-data) >= bufmax) {    /* If too big, save some for next. */
  2724.  
  2725.         debug(F000,"getpkt EOP","",rt);
  2726.  
  2727.         size = (dp-data);        /* Calculate the size. */
  2728.         *dp = '\0';            /* Mark the end. */
  2729.         if (memstr) {        /* No leftovers for memory strings */
  2730.         if (rt)            /* Char we didn't encode yet */
  2731.           memptr--;        /* (for encstr()) */
  2732.         return(size);
  2733.         }
  2734.         if ((dp-data) > bufmax) {    /* if packet is overfull */
  2735.         /* copy the part that doesn't fit into the leftover buffer, */
  2736.         /* taking care not to split a prefixed sequence. */
  2737.         int i;
  2738.         nleft = dp - odp;
  2739.         for (i = 0, p1 = leftover, p2 = odp; i < nleft; i++) {
  2740.             *p1++ = *p2++;
  2741.             if (memstr) memptr--; /* (for encstr) */
  2742.         }
  2743.         debug(F111,"getpkt leftover",leftover,size);
  2744.         debug(F101,"getpkt osize","",(odp-data));
  2745.         size = (odp-data);    /* Return truncated packet. */
  2746.         *odp = '\0';        /* Mark the new end */
  2747.         }
  2748.         t = rt;            /* Save for next time */
  2749.         return(size);
  2750.     }
  2751.     }                    /* Otherwise, keep filling. */
  2752.     size = (dp-data);            /* End of file */
  2753.     *dp = '\0';                /* Mark the end of the data. */
  2754.     debug(F111,"getpkt eof/eot",data,size); /* Fell thru before packet full, */
  2755.     return(size);             /* return partially filled last packet. */
  2756. }
  2757.  
  2758. /*  T I N I T  --  Initialize a transaction  */
  2759.  
  2760. int epktrcvd = 0, epktsent = 0;
  2761.  
  2762. /*
  2763.   Call with 1 to reset everything before S/I/Y negotiation, or 0 to
  2764.   reset only the things that are not set in the S/I/Y negotiation.
  2765.   Returns -1 on failure (e.g. to create packet buffers), 0 on success.
  2766. */
  2767. int
  2768. tinit(flag) int flag; {
  2769.     int x;
  2770. #ifdef CK_TIMERS
  2771.     extern int rttflg;
  2772. #else
  2773.     extern int rcvtimo;
  2774. #endif /* CK_TIMERS */
  2775.     extern int fatalio;
  2776.  
  2777.     debug(F101,"tinit flag","",flag);
  2778.  
  2779.     *epktmsg = NUL;
  2780.     epktrcvd = 0;
  2781.     epktsent = 0;
  2782.     ofperms = "";
  2783.     diractive = 0;            /* DIR / REMOTE DIR not active */
  2784.     interrupted = 0;            /* Not interrupted */
  2785.     fatalio = 0;            /* No fatal i/o error */
  2786.     if (server) {
  2787.     moving  = 0;
  2788. #ifdef PIPESEND
  2789.     pipesend = 0; /* This takes care of multiple GETs sent to a server. */
  2790. #endif /* PIPESEND */
  2791.     }
  2792.     bestlen = 0;            /* For packet length optimization */
  2793.     maxsend = 0;            /* Biggest data field we can send */
  2794. #ifdef STREAMING
  2795.     streamok = 0;            /* Streaming negotiated */
  2796.     streaming = 0;            /* Streaming being done now */
  2797. #endif /* STREAMING */
  2798.  
  2799.     binary = b_save;            /* ... */
  2800.     gnf_binary = binary;        /* Per-file transfer mode */
  2801.     retrans = 0;            /* Packet retransmission count */
  2802.     sndtyp = 0;                /* No previous packet */
  2803.     xflg = 0;                /* Reset x-packet flag */
  2804.     memstr = 0;                /* Reset memory-string flag */
  2805.     memptr = NULL;            /*  and buffer pointer */
  2806.     funcstr = 0;            /* Reset "read from function" flag */
  2807.     funcptr = NULL;            /*  and function pointer */
  2808.     autopar = 0;            /* Automatic parity detection flag */
  2809.  
  2810.     /* This stuff is only for BEFORE S/I/Y negotiation, not after */
  2811.  
  2812.     if (flag) {
  2813.     bctu = bctl = 1;        /* Reset block check type to 1 */
  2814.     myinit[0] = '\0';        /* Haven't sent init string yet */
  2815.     rqf = -1;            /* Reset 8th-bit-quote request flag */
  2816.     ebq = MYEBQ;            /* Reset 8th-bit quoting stuff */
  2817.     ebqflg = 0;            /* 8th bit quoting not enabled */
  2818.     ebqsent = 0;            /* No 8th-bit prefix bid sent yet */
  2819.     sq = 'Y';            /* 8th-bit prefix bid I usually send */
  2820.     spsiz = spsizr;            /* Initial send-packet size */
  2821.     debug(F101,"tinit spsiz","",spsiz);
  2822.     wslots = 1;            /* One window slot */
  2823.     wslotn = 1;            /* No window negotiated yet */
  2824.     justone = 0;            /* (should this be zero'd here?) */
  2825.     what = W_INIT;            /* Doing nothing so far... */
  2826.     }
  2827.     fncnv = f_save;            /* Back to what user last said */
  2828.     pktnum = 0;                /* Initial packet number to send */
  2829.     cxseen = czseen = discard = 0;    /* Reset interrupt flags */
  2830.     *filnam = '\0';            /* Clear file name */
  2831.     spktl = 0;                /* And its length */
  2832.     nakstate = 0;            /* Assume we're not in a NAK state */
  2833.     numerrs = 0;            /* Transmission error counter */
  2834.     idletmo = 0;            /* No idle timeout yet */
  2835.     if (server) {             /* If acting as server, */
  2836.     if (srvidl > 0)            /* If an idle timeout is given */
  2837.       timint = srvidl;
  2838.     else
  2839.       timint = srvtim;        /* use server timeout interval. */
  2840.     } else {                /* Otherwise */
  2841.     timint = chktimo(rtimo,timef);    /* and use local timeout value */
  2842.     }
  2843.     debug(F101,"tinit timint","",timint);
  2844.  
  2845. #ifdef CK_TIMERS
  2846.     if (rttflg && timint > 0)        /* Using round-trip timers? */
  2847.       rttinit();
  2848. #else
  2849.     rcvtimo = timint;
  2850. #endif /* CK_TIMERS */
  2851.  
  2852.     winlo = 0;                /* Packet 0 is at window-low */
  2853.     debug(F101,"tinit winlo","",winlo);
  2854.     x = mksbuf(1);            /* Make a 1-slot send-packet buffer */
  2855.     if (x < 0) return(x);
  2856.     x = getsbuf(0);            /* Allocate first send-buffer. */
  2857.     debug(F101,"tinit getsbuf","",x);
  2858.     if (x < 0) return(x);
  2859.     dumpsbuf();
  2860.     x = mkrbuf(wslots);            /* & a 1-slot receive-packet buffer. */
  2861.     if (x < 0) return(x);
  2862.     lsstate = 0;            /* Initialize locking shift state */
  2863.     if (autopath) {            /* SET RECEIVE PATHNAMES AUTO fixup */
  2864.     fnrpath = PATH_AUTO;
  2865.     autopath = 0;
  2866.     }
  2867.     return(0);
  2868. }
  2869.  
  2870. VOID
  2871. pktinit() {                /* Initialize packet sequence */
  2872.     pktnum = 0;                /* number & window low. */
  2873.     winlo = 0;
  2874.     debug(F101,"pktinit winlo","",winlo);
  2875. }
  2876.  
  2877. /*  R I N I T  --  Respond to S or I packet  */
  2878.  
  2879. VOID
  2880. rinit(d) CHAR *d; {
  2881.     char *tp;
  2882.     ztime(&tp);
  2883.     tlog(F110,"Transaction begins",tp,0L); /* Make transaction log entry */
  2884.     tlog(F110,"Global file mode:", binary ? "binary" : "text", 0L);
  2885.     tlog(F110,"Collision action:", fncnam[fncact],0);
  2886.     tlog(F100,"","",0);
  2887.     debug(F101,"rinit fncact","",fncact);
  2888.     filcnt = filrej = 0;        /* Init file counters */
  2889.     spar(d);
  2890.     ack1(rpar());
  2891. #ifdef datageneral
  2892.     if ((local) && (!quiet))            /* Only do this if local & not quiet */
  2893.         consta_mt();                    /* Start the asynch read task */
  2894. #endif /* datageneral */
  2895. }
  2896.  
  2897.  
  2898. /*  R E S E T C  --  Reset per-transaction character counters */
  2899.  
  2900. VOID
  2901. resetc() {
  2902.     rptn = 0;                /* Repeat counts */
  2903.     fsecs = flci = flco = 0L;        /* File chars in and out */
  2904. #ifdef GFTIMER
  2905.     fpfsecs = 0.0;
  2906. #endif /* GFTIMER */
  2907.     tfc = tlci = tlco = 0L;        /* Total file, line chars in & out */
  2908.     ccu = ccp = 0L;            /* Control-char statistics */
  2909. #ifdef COMMENT
  2910.     fsize = -1L;            /* File size */
  2911. #else
  2912.     if (what != W_SEND)
  2913.       fsize = -1L;
  2914.     debug(F101,"resetc fsize","",fsize);
  2915. #endif /* COMMENT */
  2916.     timeouts = retrans = 0;        /* Timeouts, retransmissions */
  2917.     spackets = rpackets = 0;        /* Packet counts out & in */
  2918.     crunched = 0;            /* Crunched packets */
  2919.     wcur = 0;                /* Current window size */
  2920.     wmax = 0;                /* Maximum window size used */
  2921.     peakcps = 0;                        /* Peak chars per second */
  2922. }
  2923.  
  2924. /*  S I N I T  --  Get & verify first file name, then send Send-Init packet */
  2925. /*
  2926.  Returns:
  2927.    1 if send operation begins successfully
  2928.    0 if send operation fails
  2929. */
  2930. #ifdef DYNAMIC
  2931. char *cmargbuf = NULL;
  2932. #else
  2933. char cmargbuf[CKMAXPATH+1];
  2934. #endif /* DYNAMIC */
  2935. char *cmargp[2];
  2936.  
  2937. VOID
  2938. fnlist() {
  2939.     if (!calibrate)
  2940.       sndsrc = (nfils < 0) ? -1 : nfils; /* Source for filenames */
  2941. #ifdef DYNAMIC
  2942.     if (!cmargbuf && !(cmargbuf = malloc(CKMAXPATH+1)))
  2943.       fatal("fnlist: no memory for cmargbuf");
  2944. #endif /* DYNAMIC */
  2945.     cmargbuf[0] = NUL;            /* Initialize name buffer */
  2946.  
  2947.     debug(F101,"fnlist nfils","",nfils);
  2948.     debug(F110,"fnlist cmarg",cmarg,0);
  2949.     debug(F110,"fnlist cmarg2",cmarg2,0);
  2950.     if (!cmarg2) cmarg2 = "";
  2951.     if (nfils == 0) {            /* Sending from stdin or memory. */
  2952.     if ((cmarg2 != NULL) && (*cmarg2)) {
  2953.         cmarg = cmarg2;        /* If F packet, "as-name" is used */
  2954.         cmarg2 = "";        /* if provided */
  2955.     } else
  2956.       cmarg = "stdin";        /* otherwise just use "stdin" */
  2957.     ckstrncpy(cmargbuf,cmarg,CKMAXPATH+1);
  2958.     cmargp[0] = cmargbuf;
  2959.     cmargp[1] = "";
  2960.     cmlist = cmargp;
  2961.     nfils = 1;
  2962.     }
  2963. }
  2964.  
  2965. int
  2966. sinit() {
  2967.     int x;                /* Worker int */
  2968.     char *tp, *xp, *m;            /* Worker string pointers */
  2969.  
  2970.     filcnt = filrej = 0;        /* Initialize file counters */
  2971.  
  2972.     fnlist();
  2973.  
  2974.     xp = "";
  2975.     if (nfils < 0) {
  2976. #ifdef PIPESEND
  2977.     if (usepipes && protocol == PROTO_K && *cmarg == '!') {
  2978.         pipesend = 1;
  2979.         cmarg++;
  2980.     }
  2981. #endif /* PIPESEND */
  2982.     xp = cmarg;
  2983.     } else {
  2984. #ifndef NOMSEND
  2985.     if (addlist)
  2986.       xp = filehead->fl_name;
  2987.     else
  2988. #endif /* NOMSEND */
  2989.       if (filefile)
  2990.         xp = filefile;
  2991.       else if (calibrate)
  2992.         xp = "Calibration";
  2993.       else
  2994.         xp = *cmlist;
  2995.     }
  2996.     debug(F110,"sinit xp",xp,0);
  2997.     x = gnfile();            /* Get first filename. */
  2998.     m = NULL;                /* Error message pointer */
  2999.     debug(F101,"sinit gnfil","",x);
  3000.     switch (x) {
  3001.       case -5: m = "Too many files match wildcard"; break;
  3002.       case -4: m = "Cancelled"; break;
  3003.       case -3: m = "Read access denied"; break;
  3004.       case -2: m = "File is not readable"; break;
  3005. #ifdef COMMENT
  3006.       case -1: m = iswild(filnam) ? "No files match" : "File not found";
  3007.     break;
  3008.       case  0: m = "No filespec given!"; break;
  3009. #else
  3010.       case  0:
  3011.       case -1: m = iswild(filnam) ? "No files match" : "File not found";
  3012.     break;
  3013. #endif /* COMMENT */
  3014.       default:
  3015.     break;
  3016.     }
  3017.     debug(F101,"sinit nfils","",nfils);
  3018.     debug(F110,"sinit filnam",filnam,0);
  3019.     if (x < 1) {            /* Didn't get a file. */
  3020.     if (server)            /* Doing GET command */
  3021.       errpkt((CHAR *)m);        /* so send Error packet. */
  3022.     else                /* Doing SEND command */
  3023.       xxscreen(SCR_EM,0,0l,m);    /* so print message. */
  3024.     tlog(F110,xp,m,0L);        /* Make transaction log entry. */
  3025.     freerbuf(rseqtbl[0]);        /* Free the buffer the GET came in. */
  3026.     return(0);            /* Return failure code */
  3027.     }
  3028.     if (!local && !server && ckdelay > 0) /* OS-9 sleep(0) == infinite */
  3029.       sleep(ckdelay);            /* Delay if requested */
  3030. #ifdef datageneral
  3031.     if ((local) && (!quiet))            /* Only do this if local & not quiet */
  3032.         consta_mt();                    /* Start the asynch read task */
  3033. #endif /* datageneral */
  3034.     freerbuf(rseqtbl[0]);        /* Free the buffer the GET came in. */
  3035.     sipkt('S');                /* Send the Send-Init packet. */
  3036.     ztime(&tp);                /* Get current date/time */
  3037.     tlog(F110,"Transaction begins",tp,0L); /* Make transaction log entry */
  3038.     tlog(F110,"Global file mode:", binary ? "binary" : "text", 0L);
  3039.     tlog(F100,"","",0);
  3040.     debug(F111,"sinit ok",filnam,0);
  3041.     return(1);
  3042. }
  3043.  
  3044. int
  3045. #ifdef CK_ANSIC
  3046. sipkt(char c)                /* Send S or I packet. */
  3047. #else
  3048. sipkt(c) char c;
  3049. #endif
  3050. /* sipkt */ {
  3051.     CHAR *rp; int k, x;
  3052.     debug(F101,"sipkt pktnum","",pktnum);
  3053.     k = sseqtbl[pktnum];        /* Find slot for this packet */
  3054.     debug(F101,"sipkt k","",k);
  3055.     if (k < 0) {            /* No slot? */
  3056.     k = getsbuf(winlo = pktnum);    /* Make one. */
  3057.     debug(F101,"sipkt getsbuf","",k);
  3058.     }
  3059.     ttflui();                /* Flush pending input. */
  3060.     rp = rpar();            /* Get protocol parameters. */
  3061.     x = spack(c,pktnum,(int)strlen((char *)rp),rp); /* Send them. */
  3062.     return(x);
  3063. }
  3064.  
  3065. /*  X S I N I T  --  Retransmit S-packet  */
  3066. /*
  3067.   For use in the GET-SEND sequence, when we start to send, but receive another
  3068.   copy of the GET command because the receiver didn't get our S packet.
  3069.   This retransmits the S packet and frees the receive buffer for the ACK.
  3070.   This special case is necessary because packet number zero is being re-used.
  3071. */
  3072. VOID
  3073. xsinit() {
  3074.     int k;
  3075.     k = rseqtbl[0];
  3076.     debug(F101,"xsinit k","",k);
  3077.     if (k > -1)
  3078.     freerbuf(k);
  3079.     resend(0);
  3080. }
  3081.  
  3082. /*  R C V F I L -- Receive a file  */
  3083.  
  3084. /*
  3085.   Incoming filename is in data field of F packet.
  3086.   This function decodes it into the srvcmd buffer, substituting an
  3087.   alternate "as-name", if one was given.
  3088.   Then it does any requested transformations (like converting to
  3089.   lowercase), and finally if a file of the same name already exists,
  3090.   takes the desired collision action.
  3091.   Returns:
  3092.     1 on success.
  3093.     0 on failure.
  3094. */
  3095. char ofn1[CKMAXPATH+4];            /* Buffer for output file name */
  3096. char * ofn2;                /* Pointer to backup file name */
  3097. int ofn1x;                /* Flag output file already exists */
  3098. int opnerr;                /* Flag for open error */
  3099.  
  3100. int                    /* Returns success ? 1 : 0 */
  3101. rcvfil(n) char *n; {
  3102.     extern int en_cwd;
  3103.     extern char * rcvexcept[];
  3104.     int i, skipthis;
  3105.     char * n2;
  3106. #ifdef OS2ONLY
  3107.     char *zs, *longname, *newlongname, *pn; /* OS/2 long name items */
  3108. #endif /* OS2ONLY */
  3109. #ifdef DTILDE
  3110.     char *dirp;
  3111. #endif /* DTILDE */
  3112.     int dirflg, x, y;
  3113. #ifdef PIPESEND
  3114.     extern char * rcvfilter;
  3115. #endif /* PIPESEND */
  3116.     extern char * rrfspec;
  3117. #ifdef CALIBRATE
  3118.     extern int dest;
  3119.     int csave;
  3120.     csave = calibrate;            /* So we can decode filename */
  3121.     calibrate = 0;
  3122. #endif /* CALIBRATE */
  3123.  
  3124.     ofperms = "";            /* Reset old-file permissions */
  3125.     opnerr = 0;                /* No open error (yet) */
  3126.     ofn2 = NULL;            /* No new name (yet) */
  3127.     lsstate = 0;            /* Cancel locking-shift state */
  3128.     srvptr = srvcmd;            /* Decode file name from packet. */
  3129.  
  3130. #ifdef UNICODE
  3131.     xpnbyte(-1,0,0,NULL);        /* Reset UCS-2 byte counter. */
  3132. #endif /* UNICODE */
  3133.  
  3134.     debug(F110,"rcvfil rdatap",rdatap,0);
  3135.     decode(rdatap,putsrv,0);        /* Don't xlate charsets. */
  3136. #ifdef CALIBRATE
  3137.     calibrate = csave;
  3138.     if (dest == DEST_N) {
  3139.     calibrate = 1;
  3140.     cmarg2 = "CALIBRATE";
  3141.     }
  3142. #endif /* CALIBRATE */
  3143.     if (*srvcmd == '\0')        /* Watch out for null F packet. */
  3144.       strcpy((char *)srvcmd,"NONAME");
  3145.     makestr(&rrfspec,(char *)srvcmd);
  3146. #ifdef DTILDE
  3147.     if (*srvcmd == '~') {
  3148.     dirp = tilde_expand((char *)srvcmd); /* Expand tilde, if any. */
  3149.     if (*dirp != '\0') strcpy((char *)srvcmd,dirp);
  3150.     }
  3151. #else
  3152. #ifdef OS2
  3153.     if (isalpha(*srvcmd) && srvcmd[1] == ':' && srvcmd[2] == '\0')
  3154.       strcat((char *)srvcmd,"NONAME");
  3155. #endif /* OS2 */
  3156. #endif /* DTILDE */
  3157.  
  3158.     if (!ENABLED(en_cwd)) {        /* CD is disabled */
  3159.     zstrip((char *)(srvcmd+2),&n2); /* and they included a pathname, */
  3160.     if (strcmp((char *)(srvcmd+2),n2)) { /* so refuse. */
  3161.         rf_err = "Access denied";
  3162.         return(0);
  3163.     }
  3164.     }
  3165. #ifdef COMMENT
  3166.     /* Wrong place for this -- handle cmarg2 first -- see below...  */
  3167.  
  3168.     if (zchko((char *)srvcmd) < 0) {    /* Precheck for write access */
  3169.     debug(F110,"rcvfil access denied",srvcmd,0);
  3170.     rf_err = "Write access denied";
  3171.     discard = opnerr = 1;
  3172.     return(0);
  3173.     }
  3174.     xxscreen(SCR_FN,0,0l,(char *)srvcmd); /* Put it on screen if local */
  3175.     debug(F110,"rcvfil srvcmd 1",srvcmd,0);
  3176.     tlog(F110,"Receiving",(char *)srvcmd,0L); /* Transaction log entry */
  3177. #endif /* COMMENT */
  3178.  
  3179.     skipthis = 0;            /* This file in our exception list? */
  3180.     for (i = 0; i < 8; i++) {
  3181.     if (!rcvexcept[i]) {
  3182.         break;
  3183.     }
  3184.     if (ckmatch(rcvexcept[i],(char *)srvcmd,filecase,1)) {
  3185.         skipthis = 1;
  3186.         break;
  3187.     }
  3188.     }
  3189.  
  3190. #ifdef DEBUG
  3191.     if (deblog && skipthis) {
  3192.     debug(F111,"rcvfil rcvexcept",rcvexcept[i],i);
  3193.     debug(F110,"rcvfil skipping",srvcmd,0);
  3194.     }
  3195. #endif /* DEBUG */
  3196.  
  3197.     if (skipthis) {            /* Skipping this file */
  3198.     discard = 1;
  3199.     rejection = 1;
  3200.     rf_err = "Exception list";
  3201.     debug(F101,"rcvfil discard","",discard);
  3202.     tlog(F100," refused: exception list","",0);
  3203.     return(1);
  3204.     }
  3205.  
  3206.     /* File is not in exception list */
  3207.  
  3208.     if (!cmarg2)            /* No core dumps please */
  3209.       cmarg2 = "";
  3210.     debug(F110,"rcvfil cmarg2",cmarg2,0);
  3211.  
  3212.     if (*cmarg2) {            /* Check for alternate name */
  3213. #ifndef NOSPL
  3214.     int y; char *s;            /* Pass it thru the evaluator */
  3215.     extern int cmd_quoting;
  3216.     if (cmd_quoting) {
  3217.         y = MAXRP;
  3218.         ckstrncpy(ofn1,(char *)srvcmd,CKMAXPATH+1); /* for \v(filename) */
  3219.         s = (char *)srvcmd;
  3220.         zzstring(cmarg2,&s,&y);
  3221.     } else
  3222.       *srvcmd = NUL;
  3223.     if (!*srvcmd)            /* If we got something */
  3224. #endif /* NOSPL */
  3225.       strcpy((char *)srvcmd,cmarg2);
  3226.     }
  3227.     debug(F110,"rcvfil srvcmd 2",srvcmd,0);
  3228.  
  3229. #ifdef PIPESEND
  3230.     /* If it starts with "bang", it's a pipe, not a file. */
  3231.     if (usepipes && protocol == PROTO_K && *srvcmd == '!' && !rcvfilter) {
  3232.     CHAR *s;
  3233.     s = srvcmd+1;            /* srvcmd[] is not a pointer. */
  3234.     while (*s) {            /* So we have to slide the contents */
  3235.         *(s-1) = *s;        /* over 1 space to the left. */
  3236.         s++;
  3237.     }
  3238.     *(s-1) = NUL;
  3239.     pipesend = 1;
  3240.     }
  3241. #endif /* PIPESEND */
  3242.  
  3243. #ifdef COMMENT
  3244. /*
  3245.   This is commented out because we need to know whether the name we are
  3246.   using was specified by the local user as an override, or came from the
  3247.   incoming packet.  In the former case, we don't do stuff to it (like
  3248.   strip the pathname) that we might do to it in the latter.
  3249. */
  3250.     cmarg2 = "";            /* Done with alternate name */
  3251. #endif /* COMMENT */
  3252.  
  3253.     if ((int)strlen((char *)srvcmd) > CKMAXPATH) /* Watch out for overflow */
  3254.       *(srvcmd + CKMAXPATH - 1) = NUL;
  3255.  
  3256.     /* At this point, srvcmd[] contains the incoming filename or as-name. */
  3257.     /* So NOW we check for write access. */
  3258.  
  3259.     if (zchko((char *)srvcmd) < 0) {    /* Precheck for write access */
  3260.     debug(F110,"rcvfil access denied",srvcmd,0);
  3261.     rf_err = "Write access denied";
  3262.     discard = opnerr = 1;
  3263.     return(0);
  3264.     }
  3265.     xxscreen(SCR_FN,0,0l,(char *)srvcmd); /* Put it on screen if local */
  3266.     debug(F110,"rcvfil srvcmd 1",srvcmd,0);
  3267.     tlog(F110,"Receiving",(char *)srvcmd,0L); /* Transaction log entry */
  3268.  
  3269. #ifdef CK_LABELED
  3270. #ifdef VMS
  3271. /*
  3272.   If we have an as-name, this overrides the internal name if we are doing
  3273.   a labeled-mode transfer.
  3274. */
  3275.     if (*cmarg2) {
  3276.     extern int lf_opts;
  3277.     lf_opts &= ~LBL_NAM;
  3278.     }
  3279. #endif /* VMS */
  3280. #endif /* CK_LABELED */
  3281.  
  3282. #ifdef PIPESEND
  3283.     /* Skip all the filename manipulation and collision actions */
  3284.     if (pipesend) {
  3285.     dirflg = 0;
  3286.     sprintf(ofn1,"!%s",(char *)srvcmd);
  3287.     ckstrncpy(n,ofn1,CKMAXPATH+1);
  3288.     ckstrncpy(fspec,ofn1,CKMAXPATH+1);
  3289.     makestr(&rfspec,fspec);
  3290.     debug(F110,"rcvfil pipesend",ofn1,0);
  3291.     goto rcvfilx;
  3292.     }
  3293. #endif /* PIPESEND */
  3294.  
  3295. #ifdef NZLTOR
  3296.     if (*cmarg2)
  3297.       ckstrncpy((char *)ofn1,(char *)srvcmd,CKMAXPATH+1);
  3298.     else
  3299.       nzrtol((char *)srvcmd,        /* Filename from packet */
  3300.          (char *)ofn1,        /* Where to put result */
  3301.          fncnv,            /* Filename conversion */
  3302.          fnrpath,            /* Pathname handling */
  3303.          CKMAXPATH            /* Size of result buffer */
  3304.          );
  3305. #else
  3306.     debug(F101,"rcvfil fnrpath","",fnrpath); /* Handle pathnames */
  3307.     if (fnrpath == PATH_OFF && !*cmarg2) { /* RECEIVE PATHNAMES OFF? */
  3308.     char *t;            /* Yes. */
  3309.     zstrip((char *)srvcmd,&t);    /* If there is a pathname, strip it */
  3310.     debug(F110,"rcvfil PATH_OFF zstrip",t,0);
  3311.     if (!t)                /* Be sure we didn't strip too much */
  3312.       sprintf(ofn1,"FILE%02ld",filcnt);
  3313.     else if (*t == '\0')
  3314.       sprintf(ofn1,"FILE%02ld",filcnt);
  3315.     else
  3316.       strcpy(ofn1,t);
  3317.     strcpy((char *)srvcmd,ofn1);    /* Now copy it back. */
  3318.     }
  3319. /*
  3320.   SET RECEIVE PATHNAMES RELATIVE...
  3321.   The following doesn't belong here but doing it right would require
  3322.   defining and implementing a new file routine for all ck?fio.c modules.
  3323.   So for now...
  3324. */
  3325. #ifdef UNIXOROSK
  3326.     else if (fnrpath == PATH_REL && !*cmarg2) {
  3327.     if (isabsolute((char *)srvcmd)) {
  3328.         sprintf(ofn1,".%s",(char *)srvcmd);
  3329.         strcpy((char *)srvcmd,ofn1);
  3330.         debug(F110,"rcvfil PATH_REL",ofn1,0);
  3331.     }
  3332.     }
  3333. #else
  3334. #ifdef OS2
  3335.     else if (fnrpath == PATH_REL && !*cmarg2) {
  3336.     if (isabsolute((char *)srvcmd)) {
  3337.         char *p = (char *)srvcmd;
  3338.         if (isalpha(*p) && *(p+1) == ':')
  3339.           p += 2;
  3340.         if (*p == '\\' || *p == '/')
  3341.           p++;
  3342.         ckstrncpy(ofn1,p,CKMAXPATH+1);
  3343.         strcpy((char *)srvcmd,ofn1);
  3344.         debug(F110,"rcvfil OS2 PATH_REL",ofn1,0);
  3345.     }
  3346.     }
  3347. #endif /* OS2 */
  3348. #endif /* UNIXOROSK */
  3349.  
  3350.     /* Now srvcmd contains incoming filename with path possibly stripped */
  3351.  
  3352.     if (fncnv)                /* FILE NAMES CONVERTED? */
  3353.       zrtol((char *)srvcmd,(char *)ofn1); /* Yes, convert to local form */
  3354.     else
  3355.       ckstrncpy(ofn1,(char *)srvcmd,CKMAXPATH+1); /* No, copy literally. */
  3356. #endif /* NZLTOR */
  3357.  
  3358. #ifdef PIPESEND
  3359.     if (rcvfilter) {
  3360.     char * s, * p = NULL, * q;
  3361.     int nn = MAXRP;
  3362.     pipesend = 1;
  3363.     debug(F110,"rcvfil rcvfilter ",rcvfilter,0);
  3364. #ifndef NOSPL
  3365.     if (p = (char *) malloc(nn + 1)) {
  3366.         q = p;
  3367. #ifdef COMMENT
  3368.             /* We have already processed srvcmd and placed it into ofn1 */
  3369.             ckstrncpy(ofn1,(char *)srvcmd,CKMAXPATH+1); /* For \v(filename) */
  3370. #endif /* COMMENT */
  3371.         debug(F110,"rcvfile pipesend filter",rcvfilter,0);
  3372.         zzstring(rcvfilter,&p,&nn);
  3373.         debug(F111,"rcvfil pipename",q,nn);
  3374.         if (nn <= 0) {
  3375.         printf(
  3376.                "?Sorry, receive filter + filename too long, %d max.\n",
  3377.                CKMAXPATH
  3378.                );
  3379.         rf_err = "Name too long";
  3380.         free(q);
  3381.         return(0);
  3382.         }
  3383.         ckstrncpy((char *)srvcmd,q,MAXRP);
  3384.         free(q);
  3385.     }
  3386. #endif /* NOSPL */
  3387.     }
  3388. #endif /* PIPESEND */
  3389.  
  3390.     /* Now the incoming filename, possibly converted, is in ofn1[]. */
  3391.  
  3392. #ifdef OS2
  3393.     /* Don't refuse the file just because the name is illegal. */
  3394.     if (!IsFileNameValid(ofn1)) {    /* Name is OK for OS/2? */
  3395. #ifdef OS2ONLY
  3396.     char *zs = NULL;
  3397.     zstrip(ofn1, &zs);        /* Not valid, strip unconditionally */
  3398.     if (zs) {
  3399.         if (iattr.longname.len &&    /* Free previous longname, if any */
  3400.         iattr.longname.val)
  3401.           free(iattr.longname.val);
  3402.         iattr.longname.len = strlen(zs); /* Store in attribute structure */
  3403.         iattr.longname.val = (char *) malloc(iattr.longname.len + 1);
  3404.         if (iattr.longname.val)    /* Remember this (illegal) name */
  3405.           strcpy(iattr.longname.val, zs);
  3406.     }
  3407. #endif /* OS2ONLY */
  3408.     debug(F110,"rcvfil: invalid file name",ofn1,0);
  3409.     ChangeNameForFAT(ofn1);    /* Change to an acceptable name */
  3410.     debug(F110,"rcvfil: FAT file name",ofn1,0);
  3411.  
  3412.     } else {                /* Name is OK. */
  3413.  
  3414.     debug(F110,"rcvfil: valid file name",ofn1,0);
  3415. #ifdef OS2ONLY
  3416.     if (iattr.longname.len &&
  3417.          iattr.longname.val)    /* Free previous longname, if any */
  3418.       free(iattr.longname.val);
  3419.     iattr.longname.len = 0;
  3420.     iattr.longname.val = NULL;    /* This file doesn't need a longname */
  3421. #endif /* OS2ONLY */
  3422.     }
  3423. #endif /* OS2 */
  3424.     debug(F110,"rcvfil as",ofn1,0);
  3425.  
  3426. /* Filename collision action section. */
  3427.  
  3428.     dirflg =                /* Is it a directory name? */
  3429. #ifdef CK_TMPDIR
  3430.         isdir(ofn1)
  3431. #else
  3432.     0
  3433. #endif /* CK_TMPDIR */
  3434.       ;
  3435.     debug(F101,"rcvfil dirflg","",dirflg);
  3436.     ofn1x = (zchki(ofn1) != -1);    /* File already exists? */
  3437.     debug(F101,"rcvfil ofn1x",ofn1,ofn1x);
  3438.  
  3439.     if ( (
  3440. #ifdef UNIX
  3441.     strcmp(ofn1,"/dev/null") &&    /* It's not the null device? */
  3442. #else
  3443. #ifdef OSK
  3444.     strcmp(ofn1,"/nil") &&
  3445. #endif /* OSK */
  3446. #endif /* UNIX */
  3447.     !stdouf ) &&            /* Not copying to standard output? */
  3448.     ofn1x ||            /* File of same name exists? */
  3449.     dirflg ) {            /* Or file is a directory? */
  3450.         debug(F111,"rcvfil exists",ofn1,fncact);
  3451. #ifdef CK_PERMS
  3452.     ofperms = zgperm((char *)ofn1);    /* Get old file's permissions */
  3453.     debug(F110,"rcvfil perms",ofperms,0);
  3454. #endif /* CK_PERMS */
  3455.  
  3456.     debug(F101,"rcvfil fncact","",fncact);
  3457.     switch (fncact) {        /* Yes, do what user said. */
  3458.       case XYFX_A:            /* Append */
  3459.         ofperms = "";
  3460.         debug(F100,"rcvfil append","",0);
  3461.         if (dirflg) {
  3462.         rf_err = "Can't append to a directory";
  3463.         tlog(F100," error - can't append to directory","",0);
  3464.         discard = opnerr = 1;
  3465.         return(0);
  3466.         }
  3467.         tlog(F110," appending to",ofn1,0);
  3468.         break;
  3469. #ifdef COMMENT
  3470.       case XYFX_Q:            /* Query (Ask) */
  3471.         break;            /* not implemented */
  3472. #endif /* COMMENT */
  3473.       case XYFX_B:            /* Backup (rename old file) */
  3474.         if (dirflg) {
  3475.         rf_err = "Can't rename existing directory";
  3476.         tlog(F100," error - can't rename directory","",0);
  3477.         discard = opnerr = 1;
  3478.         return(0);
  3479.         }
  3480.         znewn(ofn1,&ofn2);        /* Get new unique name */
  3481.         tlog(F110," backup:",ofn2,0);
  3482.         debug(F110,"rcvfil backup ofn1",ofn1,0);
  3483.         debug(F110,"rcvfil backup ofn2",ofn2,0);
  3484. #ifdef CK_LABELED
  3485. #ifdef OS2ONLY
  3486. /*
  3487.   In case this is a FAT file system, we can't change only the FAT name, we
  3488.   also have to change the longname from the extended attributes block.
  3489.   Otherwise, we'll have many files with the same longname and if we copy them
  3490.   to an HPFS volume, only one will survive.
  3491. */
  3492.         if (os2getlongname(ofn1, &longname) > -1) {
  3493.         if (strlen(longname)) {
  3494.             char tmp[10];
  3495.             extern int ck_znewn;
  3496.             sprintf(tmp,".~%d~",ck_znewn);
  3497.             newlongname =
  3498.               (char *) malloc(strlen(longname) + strlen(tmp) + 1);
  3499.             if (newlongname) {
  3500.             strcpy(newlongname, longname);
  3501.             strcat(newlongname, tmp);
  3502.             os2setlongname(ofn1, newlongname);
  3503.             free(newlongname);
  3504.             newlongname = NULL;
  3505.             }
  3506.         }
  3507.         } else debug(F100,"rcvfil os2getlongname failed","",0);
  3508. #endif /* OS2ONLY */
  3509. #endif /* CK_LABELED */
  3510.  
  3511. #ifdef COMMENT
  3512.         /* Do this later, in opena()... */
  3513.         if (zrename(ofn1,ofn2) < 0) {
  3514.         rf_err = "Can't transform filename";
  3515.         debug(F110,"rcvfil rename fails",ofn1,0);
  3516.         discard = opnerr = 1;
  3517.         return(0);
  3518.         }
  3519. #endif /* COMMENT */
  3520.         break;
  3521.  
  3522.       case XYFX_D:            /* Discard (refuse new file) */
  3523.         ofperms = "";
  3524.         discard = 1;
  3525.         rejection = 1;        /* Horrible hack: reason = name */
  3526.         debug(F101,"rcvfil discard","",discard);
  3527.         tlog(F100," refused: name","",0);
  3528.         break;
  3529.  
  3530.       case XYFX_R:            /* Rename incoming file */
  3531.         znewn(ofn1,&ofn2);        /* Make new name for it */
  3532. #ifdef OS2ONLY
  3533.         if (iattr.longname.len) {
  3534.         char tmp[10];
  3535.         extern int ck_znewn;
  3536.         sprintf(tmp,".~%d~",ck_znewn);
  3537.         newlongname =
  3538.           (char *) malloc(iattr.longname.len + strlen(tmp) + 1);
  3539.         if (newlongname) {
  3540.             strcpy(newlongname, iattr.longname.val);
  3541.             strcat(newlongname, tmp);
  3542.             debug(F110,
  3543.               "Rename Incoming: newlongname",newlongname,0);
  3544.  
  3545.             if (iattr.longname.len &&
  3546.             iattr.longname.val)
  3547.               free(iattr.longname.val);
  3548.             iattr.longname.len = strlen(newlongname);
  3549.             iattr.longname.val = newlongname;
  3550.         }
  3551.         }
  3552. #endif /* OS2ONLY */
  3553.         break;
  3554.       case XYFX_X:            /* Replace old file */
  3555.         debug(F100,"rcvfil overwrite","",0);
  3556.         if (dirflg) {
  3557.         rf_err = "Can't overwrite existing directory";
  3558.         tlog(F100," error - can't overwrite directory","",0);
  3559.         discard = opnerr = 1;
  3560. #ifdef COMMENT
  3561.         return(0);
  3562. #else
  3563.         break;
  3564. #endif /* COMMENT */
  3565.         }
  3566.         tlog(F110,"overwriting",ofn1,0);
  3567.         break;
  3568.       case XYFX_U:            /* Refuse if older */
  3569.         debug(F110,"rcvfil update",ofn1,0);
  3570.         if (dirflg) {
  3571.         rf_err = "File has same name as existing directory";
  3572.         tlog(F110," error - directory exists:",ofn1,0);
  3573.         discard = opnerr = 1;
  3574. #ifdef COMMENT
  3575.         /* Don't send an error packet, just refuse the file */
  3576.         return(0);
  3577. #endif /* COMMENT */
  3578.         }
  3579.         break;            /* Not here, we don't have */
  3580.                     /* the attribute packet yet. */
  3581.       default:
  3582.         ofperms = "";
  3583.         debug(F101,"rcvfil bad collision action","",fncact);
  3584.         break;
  3585.     }
  3586.     }
  3587.     debug(F110,"rcvfil ofn1",ofn1,0);
  3588.     debug(F110,"rcvfil ofn2",ofn2,0);
  3589.     debug(F110,"rcvfil ofperms",ofperms,0);
  3590.     if (fncact == XYFX_R && ofn1x && ofn2) { /* Renaming incoming file? */
  3591.     xxscreen(SCR_AN,0,0l,ofn2);    /* Display renamed name */
  3592.     ckstrncpy(n, ofn2, CKMAXPATH+1); /* Return it */
  3593.     } else {                /* No */
  3594.     xxscreen(SCR_AN,0,0l,ofn1);    /* Display regular name */
  3595.     ckstrncpy(n, ofn1, CKMAXPATH+1); /* and return it. */
  3596.     }
  3597.  
  3598. #ifdef CK_MKDIR
  3599. /*  Create directory(s) if necessary.  */
  3600.     if (!discard && fnrpath != PATH_OFF) { /* RECEIVE PATHAMES ON? */
  3601.     int x;
  3602.     debug(F110,"rcvfil calling zmkdir",ofn1,0); /* Yes */
  3603.     if ((x = zmkdir(ofn1)) < 0) {
  3604.         debug(F100,"zmkdir fails","",0);
  3605.         tlog(F110," error - directory creation failure:",ofn1,0);
  3606.         rf_err = "Directory creation failure.";
  3607.         discard = 1;
  3608.         return(0);
  3609.     }
  3610. #ifdef TLOG
  3611.     else if (x > 0)
  3612.       tlog(F110," path created:",ofn1,0);
  3613. #endif /* TLOG */
  3614.     }
  3615. #else
  3616.     debug(F110,"rcvfil CK_MKDIR not defined",ofn1,0);
  3617. #endif /* CK_MKDIR */
  3618.  
  3619.     if (calibrate)
  3620.       ckstrncpy(fspec,ofn1,CKMAXPATH+1);
  3621.     else
  3622.       zfnqfp(ofn1,fspeclen,fspec);
  3623.     debug(F110,"rcvfil fspec",fspec,0);
  3624.  
  3625. #ifdef COMMENT
  3626.     /* See comments with VMS zfnqfp()... */
  3627. #ifdef VMS
  3628.     /* zfnqfp() does not return the version number */
  3629.     if (!calibrate) {
  3630.     int x, v;
  3631.     x = strlen(ofn1);
  3632.     if (x > 0) {
  3633.         if (ofn1[x-1] == ';') {
  3634.         v = getvnum(ofn1);
  3635.         strcpy(&ofn1[x],ckitoa(v));
  3636.         }
  3637.     }
  3638.     }
  3639. #endif /* VMS */
  3640. #endif /* COMMENT */
  3641.     fspec[fspeclen] = NUL;
  3642.     makestr(&rfspec,fspec);
  3643.  
  3644. #ifdef PIPESEND
  3645.   rcvfilx:
  3646. #endif /* PIPESEND */
  3647.  
  3648.     debug(F110,"rcvfilx: n",n,0);
  3649.     debug(F110,"rcvfilx: ofn1",ofn1,0);
  3650.     ffc = 0L;                /* Init per-file counters */
  3651.     cps = oldcps = 0L;
  3652.     rs_len = 0L;
  3653.     rejection = -1;
  3654.     fsecs = gtimer();            /* Time this file started */
  3655. #ifdef GFTIMER
  3656.     fpfsecs = gftimer();
  3657.     debug(F101,"rcvfil fpfsecs","",fpfsecs);
  3658. #endif /* GFTIMER */
  3659.     filcnt++;
  3660.     intmsg(filcnt);
  3661.     return(1);                /* Successful return */
  3662. }
  3663.  
  3664.  
  3665. /*  R E O F  --  Receive End Of File packet for incoming file */
  3666.  
  3667. /*
  3668.   Closes the received file.
  3669.   Returns:
  3670.     0 on success.
  3671.    -1 if file could not be closed.
  3672.     2 if disposition was mail, mail was sent, but temp file not deleted.
  3673.     3 if disposition was print, file was printed, but not deleted.
  3674.    -2 if disposition was mail and mail could not be sent
  3675.    -3 if disposition was print and file could not be printed
  3676. */
  3677. int
  3678. reof(f,yy) char *f; struct zattr *yy; {
  3679.     extern char * rcv_move, * rcv_rename;
  3680.     int x;
  3681.     char *p;
  3682.     char c;
  3683.  
  3684.     debug(F111,"reof fncact",f,fncact);
  3685.     debug(F101,"reof discard","",discard);
  3686.     success = 1;            /* Assume status is OK */
  3687.     lsstate = 0;            /* Cancel locking-shift state */
  3688.     if (discard) {            /* Handle attribute refusals, etc. */
  3689.     debug(F101,"reof discarding","",0);
  3690.     success = 0;            /* Status = failed. */
  3691.     if (rejection == '#' ||        /* Unless rejection reason is */
  3692.         rejection ==  1  ||        /* date or name (SET FILE COLLISION */
  3693.         rejection == '?')        /* UPDATE or DISCARD) */
  3694.       success = 1;
  3695.     debug(F101,"reof success","",success);
  3696.     filrej++;            /* Count this rejection. */
  3697.     discard = 0;            /* We never opened the file, */
  3698.     return(0);            /* so we don't close it. */
  3699.     }
  3700. #ifdef DEBUG
  3701.     if (deblog) {
  3702.     debug(F101,"reof cxseen","",cxseen);
  3703.     debug(F101,"reof czseen","",czseen);
  3704.     debug(F110,"reof rdatap",rdatap,0);
  3705.     }
  3706. #endif /* DEBUG */
  3707.  
  3708.     if (cxseen == 0)            /* Got cancel directive? */
  3709.       cxseen = (*rdatap == 'D');
  3710.     if (cxseen || czseen)        /* (for hints) */
  3711.       interrupted = 1;
  3712.     success = (cxseen || czseen) ? 0 : 1; /* Set SUCCESS flag appropriately */
  3713.     if (!success) filrej++;        /* "Uncount" this file */
  3714.     debug(F101,"reof success","",success);
  3715.  
  3716. #ifdef CK_CTRLZ
  3717.     if (success) {
  3718.     debug(F101,"reof lastchar","",lastchar);
  3719.     if (!binary && eofmethod == XYEOF_Z && lastchar != 26 &&
  3720.         (!xflg || (xflg && remfile)))
  3721.       pnbyte((char)26,putfil);
  3722.     }
  3723. #endif /* CK_CTRLZ */
  3724.  
  3725.     x = clsof(cxseen || czseen);    /* Close the file (resets cxseen) */
  3726.     debug(F101,"reof closf","",x);
  3727.     if (x < 0) {            /* If failure to close, FAIL */
  3728.     if (success) filrej++;
  3729.     success = 0;
  3730.     }
  3731.     if (!calibrate) {
  3732.     /* Set file modification date and/or permissions */
  3733.     if (success)
  3734.       zstime(f,yy,0);
  3735. #ifdef OS2ONLY
  3736. #ifdef CK_LABELED
  3737.     if (success && yy->longname.len)
  3738.       os2setlongname(f, yy->longname.val);
  3739. #endif /* CK_LABELED */
  3740. #endif /* OS2ONLY */
  3741.     }
  3742.     if (success == 0) xitsta |= W_RECV;    /* And program return code */
  3743.  
  3744. /* Handle dispositions from attribute packet... */
  3745.  
  3746.     c = NUL;
  3747. #ifndef NOFRILLS
  3748.     if (!calibrate && yy->disp.len != 0) {
  3749.     p = yy->disp.val;
  3750.     c = *p++;
  3751. #ifndef UNIX
  3752. /*
  3753.   See ckcpro.w.  In UNIX we don't use temp files any more -- we pipe the
  3754.   stuff right into mail or lpr.
  3755. */
  3756.     if (c == 'M') {            /* Mail to user. */
  3757.         x = zmail(p,filnam);    /* Do the system's mail command */
  3758.         if (x < 0) success = 0;    /* Remember status */
  3759.         tlog(F110,"mailed",filnam,0L);
  3760.         tlog(F110," to",p,0L);
  3761.         zdelet(filnam);        /* Delete the file */
  3762.     } else if (c == 'P') {        /* Print the file. */
  3763.         x = zprint(p,filnam);    /* Do the system's print command */
  3764.         if (x < 0) success = 0;    /* Remember status */
  3765.         tlog(F110,"printed",filnam,0L);
  3766.         tlog(F110," with options",p,0L);
  3767. #ifndef VMS
  3768. #ifndef STRATUS
  3769.         /* spooler will delete file after print complete in VOS & VMS */
  3770.         if (zdelet(filnam) && x == 0) x = 3; /* Delete the file */
  3771. #endif /* STRATUS */
  3772. #endif /* VMS */
  3773.     }
  3774. #endif /* UNIX */
  3775.     }
  3776. #endif /* NOFRILLS */
  3777.  
  3778.     if (success &&
  3779. #ifdef PIPESEND
  3780.     !pipesend &&
  3781. #endif /* PIPESEND */
  3782.     !calibrate && c != 'M' && c != 'P') {
  3783.     if (rcv_move) {            /* If /MOVE-TO was given... */
  3784.         int x;
  3785.         tlog(F110," moving received file to",rcv_move,0);
  3786.         x = zrename(filnam,rcv_move);
  3787.         debug(F111,"reof MOVE zrename",rcv_move,x);
  3788.     } else if (rcv_rename) {    /* Or /RENAME-TO: */
  3789.         int x;
  3790.         char *s = rcv_rename;    /* This is the renaming string */
  3791. #ifndef NOSPL
  3792.         char tmpnam[CKMAXPATH+16];
  3793.         int y;            /* Pass it thru the evaluator */
  3794.         extern int cmd_quoting;    /* for \v(filename) */
  3795.         if (cmd_quoting) {        /* But only if cmd_quoting is on */
  3796.         y = MAXRP;
  3797.         s = (char *)tmpnam;
  3798.         zzstring(rcv_rename,&s,&y);
  3799.         s = (char *)tmpnam;
  3800.         }
  3801. #endif /* NOSPL */
  3802.         if (s) if (*s) {
  3803.         x = zrename(filnam,s);
  3804.         debug(F111,"reof RENAME zrename",s,x);
  3805.         if (x > -1)
  3806.           tlog(F110," renaming received file to",s,0);
  3807.         }
  3808.     }
  3809.     }
  3810.     debug(F101,"reof returns","",x);
  3811.     *filnam = '\0';
  3812.     return(x);
  3813. }
  3814.  
  3815. /*  R E O T  --  Receive End Of Transaction  */
  3816.  
  3817. VOID
  3818. reot() {
  3819.     cxseen = czseen = discard = 0;    /* Reset interruption flags */
  3820.     tstats();
  3821. }
  3822.  
  3823. /*  S F I L E -- Send File header or teXt header packet  */
  3824.  
  3825. /*
  3826.   Call with x nonzero for X packet, zero for F packet.
  3827.   If X == 0, filename to send is in filnam[], and if cmarg2 is not null
  3828.   or empty, the file should be sent under this name rather than filnam[].
  3829.   If sndfilter not NULL, it is the name of a send filter.
  3830.   Returns 1 on success, 0 on failure.
  3831. */
  3832. int
  3833. sfile(x) int x; {
  3834. #ifdef pdp11
  3835. #define PKTNL 64
  3836. #else
  3837. #define PKTNL 256
  3838. #endif /* pdp11 */
  3839.     char pktnam[PKTNL+1];        /* Local copy of name */
  3840.     char *s;
  3841.     int rc;
  3842. #ifdef PIPESEND
  3843.     extern char * sndfilter;
  3844.  
  3845.     debug(F101,"sfile x","",x);
  3846.     if (sndfilter) {
  3847.     pipesend = 1;
  3848.     debug(F110,"sfile send filter ",sndfilter,0);
  3849.     }
  3850. #else
  3851.     debug(F101,"sfile x","",x);
  3852. #endif /* PIPESEND */
  3853.  
  3854.     /* cmarg2 or filnam (with that precedence) have the file's name */
  3855.  
  3856.     lsstate = 0;            /* Cancel locking-shift state */
  3857.     if (nxtpkt() < 0) return(0);    /* Bump packet number, get buffer */
  3858.     pktnam[0] = NUL;            /* Buffer for name we will send */
  3859.     if (x == 0) {            /* F-Packet setup */
  3860.     if (!cmarg2) cmarg2 = "";
  3861. #ifdef DEBUG
  3862.     if (deblog) {
  3863.         debug(F111,"sfile cmarg2",cmarg2,cmarg2);
  3864.         debug(F101,"sfile binary 1","",binary);
  3865.         debug(F101,"sfile wearealike","",wearealike);
  3866.         debug(F101,"sfile xfermode","",xfermode);
  3867. #ifndef NOCSETS
  3868.         debug(F101,"sfile tcharset","",tcharset);
  3869. #endif /* NOCSETS */
  3870.     }
  3871. #endif /* DEBUG */
  3872.     if (xfermode == XMODE_A        /* TRANSFER MODE AUTOMATIC */
  3873. #ifndef NOMSEND
  3874.         && !addlist            /* And not working from a SEND-LIST */
  3875. #endif /* NOMSEND */
  3876.         ) {
  3877.         /* Other Kermit is on a like system and no charset translation */
  3878.         if (wearealike
  3879. #ifndef NOCSETS
  3880.         && tcharset == TC_TRANSP
  3881. #endif /* NOCSETS */
  3882.         ) {
  3883. #ifdef VMS
  3884.         if (binary != XYFT_I)
  3885. #endif /* VMS */
  3886. #ifdef CK_LABELED
  3887.           if (binary != XYFT_L)
  3888. #endif /* CK_LABELED */
  3889.             binary = XYFT_B;    /* Send all files in binary mode */
  3890.         }
  3891. #ifdef PATTERNS
  3892.  
  3893.         /* Otherwise select transfer mode based on filename patterns */
  3894.  
  3895.         else if (patterns        /* PATTERNS are ON */
  3896. #ifdef PIPESEND
  3897.              && !pipesend    /* But not if sending from pipe */
  3898. #endif /* PIPESEND */
  3899. #ifdef CK_LABELED
  3900.              && binary != XYFT_L /* And not if FILE TYPE LABELED */
  3901. #endif /* CK_LABELED */
  3902. #ifdef VMS
  3903.              && binary != XYFT_I /* or FILE TYPE IMAGE */
  3904. #endif /* VMS */
  3905.              ) {
  3906.         if (binary != XYFT_T && txtpatterns[0]) {
  3907.             int i;
  3908.             for (i = 0; i < FTPATTERNS && txtpatterns[i]; i++) {
  3909.             if (ckmatch(txtpatterns[i],filnam,filecase,1)) {
  3910.                 binary = XYFT_T;
  3911.                 break;
  3912.             }
  3913.             }
  3914.         }
  3915.         if (binary != XYFT_B && binpatterns[0]) {
  3916.             int i;
  3917.             for (i = 0; i < FTPATTERNS && binpatterns[i]; i++) {
  3918.             if (ckmatch(binpatterns[i],filnam,filecase,1)) {
  3919.                 binary = XYFT_B;
  3920.                 break;
  3921.             }
  3922.             }
  3923.         }
  3924.         }
  3925. #endif /* PATTERNS */
  3926.     }
  3927.     debug(F101,"sfile binary 2","",binary);
  3928.     debug(F101,"sfile sendmode","",sendmode);
  3929.  
  3930.         if (*cmarg2) {            /* If we have a send-as name... */
  3931.         int y; char *s;
  3932. #ifndef NOSPL                /* and a script programming language */
  3933.         extern int cmd_quoting;
  3934.         if (cmd_quoting) {        /* and it's not turned off */
  3935.         y = PKTNL;        /* pass as-name thru the evaluator */
  3936.         s = pktnam;
  3937.         zzstring(cmarg2,&s,&y);
  3938. #ifdef COMMENT
  3939. /* This ruins macros like BSEND */
  3940.         if (!pktnam[0])        /* and make sure result is not empty */
  3941.           sprintf(pktnam,"FILE%02ld",filcnt);
  3942. #endif /* COMMENT */
  3943.         } else
  3944. #endif /* NOSPL */
  3945.           ckstrncpy(pktnam,cmarg2,PKTNL); /* copy it literally, */
  3946.  
  3947.         debug(F110,"sfile pktnam",pktnam,0);
  3948. #ifdef COMMENT
  3949. /* We don't do this any more because now we have filename templates */
  3950.         cmarg2 = "";        /* and blank it out for next time. */
  3951. #endif /* COMMENT */
  3952.         }
  3953.     if (!*pktnam) {            /* No as-name... */
  3954. #ifdef NZLTOR
  3955.         int xfncnv, xpath;
  3956.         debug(F101,"sfile fnspath","",fnspath);
  3957.         debug(F101,"sfile fncnv","",fncnv);
  3958.         debug(F101,"sfile calibrate","",calibrate);
  3959.         xfncnv = fncnv;
  3960.         xpath = fnspath;
  3961. #ifdef CALIBRATE
  3962.         if (calibrate) {
  3963.         xfncnv = 0;        /* Don't convert */
  3964.         xpath = PATH_OFF;    /* Remove path */
  3965.         }
  3966. #endif /* CALIBRATE */
  3967. #ifdef PIPESEND
  3968.         debug(F101,"sfile pipesend","",pipesend);
  3969.         debug(F101,"sfile sndfilter","",sndfilter);
  3970.         if (pipesend || sndfilter) {
  3971.         xfncnv = 0;        /* Don't convert */
  3972.         xpath = PATH_ABS;    /* Leave or put path on */
  3973.         }
  3974. #endif /* PIPESEND */
  3975.         debug(F101,"sfile xpath","",xpath);
  3976.         debug(F101,"sfile xfncnv","",xfncnv);
  3977.         nzltor(filnam,pktnam,xfncnv,xpath,PKTNL);
  3978.  
  3979. #else  /* Not NZLTOR */
  3980.  
  3981.         debug(F101,"sfile fnspath","",fnspath);
  3982.         if (fnspath == PATH_OFF    /* Stripping path names? */
  3983. #ifdef PIPESEND
  3984.         && (!pipesend || !sndfilter) /* does this make sense? */
  3985. #endif /* PIPESEND */
  3986.         ) {
  3987.         char *t;        /* Yes. */
  3988.         zstrip(filnam,&t);    /* Strip off the path. */
  3989.         debug(F110,"sfile zstrip",t,0);
  3990.         if (!t) t = "UNKNOWN";    /* Be cautious... */
  3991.         else if (*t == '\0')
  3992.           t = "UNKNOWN";
  3993.         ckstrncpy(pktnam,t,PKTNL); /* Copy stripped name literally. */
  3994.         } else if (fnspath == PATH_ABS && !calibrate) {
  3995.         /* Converting to absolute form */
  3996.         zfnqfp(filnam,PKTNL,pktnam);
  3997.         } else
  3998.         ckstrncpy(pktnam,filnam,PKTNL);
  3999.  
  4000.         /* pktnam[] has the packet name, filnam[] has the original name. */
  4001.         /* But we still need to convert pktnam if FILE NAMES CONVERTED.  */
  4002.  
  4003.         debug(F101,"sfile fncnv","",fncnv);
  4004.         if (fncnv
  4005. #ifdef PIPESEND
  4006.         && (!pipesend || !sndfilter)
  4007. #endif /* PIPESEND */
  4008. #ifdef CALIBRATE
  4009.         && !calibrate
  4010. #endif /* CALIBRATE */
  4011.         ) {            /* If converting names, */
  4012.         zltor(pktnam,(char *)srvcmd); /* convert it to common form, */
  4013.         ckstrncpy(pktnam,(char *)srvcmd,PKTNL);
  4014.         *srvcmd = NUL;
  4015.         }
  4016. #endif /* NZLTOR */
  4017.         }
  4018.     if (!*pktnam)            /* Failsafe... */
  4019.       sprintf(pktnam,"FILE%02ld",filcnt);
  4020.     debug(F110,"sfile filnam 1",filnam,0);
  4021.     debug(F110,"sfile pktnam 1",pktnam,0);
  4022. #ifdef PIPESEND
  4023. /* If we have a send filter, substitute the current filename into it */
  4024.  
  4025.     if (sndfilter) {
  4026.         char * p = NULL, * q;
  4027.         int n = CKMAXPATH;
  4028. #ifndef NOSPL
  4029.         if (p = (char *) malloc(n + 1)) {
  4030.         q = p;
  4031.         debug(F110,"sfile pipesend filter",sndfilter,0);
  4032.         zzstring(sndfilter,&p,&n);
  4033.         debug(F111,"sfile pipename",q,n);
  4034.         if (n <= 0) {
  4035.             printf(
  4036.               "?Sorry, send filter + filename too long, %d max.\n",
  4037.                CKMAXPATH
  4038.                );
  4039.             free(q);
  4040.             return(0);
  4041.         }
  4042.         ckstrncpy(filnam,q,CKMAXPATH+1);
  4043.         free(q);
  4044.         }
  4045. #endif /* NOSPL */
  4046.     }
  4047. #endif /* PIPESEND */
  4048.  
  4049.         debug(F110,"sfile filnam 2",filnam,0); /* Log debugging info */
  4050.         debug(F110,"sfile pktnam 2",pktnam,0);
  4051.         if (openi(filnam) == 0)     /* Try to open the input file */
  4052.       return(0);
  4053.  
  4054. #ifdef CK_RESEND
  4055. /*
  4056.   The following check is done after openi() is called, since openi() itself
  4057.   can change the transfer mode (as in VMS).
  4058. */
  4059.         if ((binary == XYFT_T
  4060. #ifdef VMS
  4061.          || binary == XYFT_L
  4062. #endif /* VMS */
  4063.          ) && sendmode == SM_RESEND) {
  4064.             /* Trying to RESEND/REGET a file first sent in TEXT mode. */
  4065.         debug(F111,"sfile error - Recover vs Text",filnam,binary);
  4066.             /* Set appropriate error messages and make log entries here */
  4067. #ifdef VMS
  4068.         if (binary == XYFT_L)
  4069.           sprintf((char *) epktmsg,
  4070.               "Recovery attempted in LABELED mode: %s",filnam);
  4071.         else
  4072. #endif /* VMS */
  4073.             sprintf((char *)epktmsg,
  4074.             "Recovery attempted in TEXT mode: %s",filnam);
  4075.             return(0);
  4076.         }
  4077.     if (sendmode == SM_PSEND)    /* PSENDing? */
  4078.       if (sendstart > 0L)        /* Starting position */
  4079.         if (zfseek(sendstart) < 0)    /* seek to it... */
  4080.           return(0);
  4081. #endif /* CK_RESEND */
  4082.         s = pktnam;            /* Name for packet data field */
  4083. #ifdef OS2
  4084.     /* Never send a disk letter. */
  4085.     if (isalpha(*s) && (*(s+1) == ':'))
  4086.       s += 2;
  4087. #endif /* OS2 */
  4088.  
  4089.     } else {                /* X-packet setup, not F-packet. */
  4090.     binary = XYFT_T;        /* Text always */
  4091.         debug(F110,"sfile X packet",cmdstr,0); /* Log debugging info */
  4092.         s = cmdstr;            /* Name for data field */
  4093.     }
  4094.  
  4095.     /* Now s points to the string that goes in the packet data field. */
  4096.  
  4097.     debug(F101,"sfile binary","",binary); /* Log debugging info */
  4098.     encstr((CHAR *)s);            /* Encode the name. */
  4099.                     /* Send the F or X packet */
  4100.     /* If the encoded string did not fit into the packet, it was truncated. */
  4101.  
  4102.     rc = spack((char) (x ? 'X' : 'F'), pktnum, size, data);
  4103.     if (rc < 0)
  4104.       return(rc);
  4105.  
  4106. #ifndef NOCSETS
  4107.     setxlatype(tcharset,fcharset);    /* Set up charset translations */
  4108. #endif /* NOCSETS */
  4109.  
  4110.     if (x == 0) {            /* Display for F packet */
  4111.         if (displa) {            /* Screen */
  4112.         xxscreen(SCR_FN,'F',(long)pktnum,filnam);
  4113.         xxscreen(SCR_AN,0,0L,pktnam);
  4114.         xxscreen(SCR_FS,0,calibrate ? calibrate : fsize,"");
  4115.         }
  4116. #ifdef pdp11
  4117.         tlog(F110,"Sending",filnam,0L); /* Transaction log entry */
  4118.     makestr(&sfspec,filnam);
  4119. #else
  4120. #ifndef ZFNQFP
  4121.         tlog(F110,"Sending",filnam,0L);
  4122.     makestr(&sfspec,filnam);
  4123. #else
  4124.     if (sndarray) {            /* Log fully qualified filename */
  4125.         tlog(F110,"Sending", filnam, 0L);
  4126.     } else {
  4127. #ifdef COMMENT
  4128.         /* This section generates bad code in SCO 3.2v5.0.5's cc */
  4129.         char *p = NULL, *q = filnam;
  4130.         debug(F101,"sfile CKMAXPATH","",CKMAXPATH);
  4131.         if ((p = malloc(CKMAXPATH+1))) {
  4132.         debug(F111,"sfile calling zfnqfp",filnam,strlen(filnam));
  4133.         if (zfnqfp(filnam, CKMAXPATH, p)) {
  4134.             debug(F111,"sfile zfnqfp ok",p,strlen(p));
  4135.             q = p;
  4136.         }
  4137.         }
  4138. #else
  4139.         char tmpbuf[CKMAXPATH+1];
  4140.         char *p = tmpbuf, *q = filnam;
  4141.         if (zfnqfp(filnam, CKMAXPATH, p))
  4142.           q = p;
  4143. #endif /* COMMENT */
  4144.         debug(F111,"sfile q",q,strlen(q));
  4145.         tlog(F110,"Sending",q,0L);
  4146.         makestr(&sfspec,q);
  4147. #ifdef COMMENT
  4148.         if (p) free(p);
  4149. #endif /* COMMENT */
  4150.     }
  4151. #endif /* ZFNQFP */
  4152. #endif /* pdp11 */
  4153.         tlog(F110," as",pktnam,0L);
  4154.     if (binary) {            /* Log file mode in transaction log */
  4155.         tlog(F101," mode: binary","",(long) binary);
  4156.     } else {            /* If text mode, check character set */
  4157.         tlog(F100," mode: text","",0L);
  4158. #ifndef NOCSETS
  4159.         tlog(F110," file character set",fcsinfo[fcharset].name,0L);
  4160.         if (tcharset == TC_TRANSP)
  4161.           tlog(F110," xfer character set","transparent",0L);
  4162.         else
  4163.           tlog(F110," xfer character set",tcsinfo[tcharset].name,0L);
  4164. #endif /* NOCSETS */
  4165.     }
  4166.     } else {                /* Display for X-packet */
  4167.  
  4168.         xxscreen(SCR_XD,'X',(long)pktnum,cmdstr); /* Screen */
  4169.         tlog(F110,"Sending from:",cmdstr,0L);    /* Transaction log */
  4170.     }
  4171.     intmsg(++filcnt);            /* Count file, give interrupt msg */
  4172.     first = 1;                /* Init file character lookahead. */
  4173.     ffc = 0L;                /* Init file character counter. */
  4174.     cps = oldcps = 0L;            /* Init cps statistics */
  4175.     rejection = -1;
  4176.     fsecs = gtimer();            /* Time this file started */
  4177. #ifdef GFTIMER
  4178.     fpfsecs = gftimer();
  4179.     debug(F101,"SFILE fpfsecs","",fpfsecs);
  4180. #endif /* GFTIMER */
  4181.     debug(F101,"SFILE fsecs","",fsecs);
  4182.     return(1);
  4183. }
  4184.  
  4185. /*  S D A T A -- Send a data packet */
  4186.  
  4187. /*
  4188.   Returns -1 if no data to send (end of file), -2 if connection is broken.
  4189.   If there is data, a data packet is sent, and sdata() returns 1.
  4190.  
  4191.   In the streaming case, the window is regarded as infinite and we keep
  4192.   sending data packets until EOF or there appears to be a Kermit packet on the
  4193.   reverse channel.  When not streaming and the window size is greater than 1,
  4194.   we keep sending data packets until window is full or characters start to
  4195.   appear from the other Kermit.
  4196.  
  4197.   In the windowing or streaming case, when there is no more data left to send
  4198.   (or when sending has been interrupted), sdata() does nothing and returns 0
  4199.   each time it is called until the acknowledgement sequence number catches up
  4200.   to the last data packet that was sent.
  4201. */
  4202. int
  4203. sdata() {
  4204.     int i, x, len;
  4205.     char * s;
  4206.  
  4207.     debug(F101,"sdata entry, first","",first);
  4208.     debug(F101,"sdata drain","",drain);
  4209. /*
  4210.   The "drain" flag is used with window size > 1.  It means we have sent
  4211.   our last data packet.  If called and drain is not zero, then we return
  4212.   0 as if we had sent an empty data packet, until all data packets have
  4213.   been ACK'd, then then we can finally return -1 indicating EOF, so that
  4214.   the protocol can switch to seof state.  This is a kludge, but at least
  4215.   it's localized...
  4216. */
  4217.     if (first == 1) drain = 0;        /* Start of file, init drain flag. */
  4218.  
  4219.     if (drain) {            /* If draining... */
  4220.     debug(F101,"sdata draining, winlo","",winlo);
  4221.     if (winlo == pktnum)        /* If all data packets are ACK'd */
  4222.       return(-1);            /* return EOF indication */
  4223.     else                /* otherwise */
  4224.       return(0);            /* pretend we sent a data packet. */
  4225.     }
  4226.     debug(F101,"sdata sbufnum","",sbufnum);
  4227.     for (i = sbufnum;
  4228.      i > 0
  4229. #ifdef STREAMING
  4230.      || streaming
  4231. #endif /* STREAMING */
  4232.      ;
  4233.      i--) {
  4234.         debug(F101,"sdata countdown","",i);
  4235.     if (streaming) {
  4236.         pktnum = (pktnum + 1) % 64;
  4237.         winlo = pktnum;
  4238.         debug(F101,"sdata streaming pktnum","",pktnum);
  4239.     } else {
  4240.         x = nxtpkt();        /* Get next pkt number and buffer */
  4241.         debug(F101,"sdata nxtpkt pktnum","",pktnum);
  4242.         if (x < 0) return(0);
  4243.     }
  4244.     debug(F101,"sdata packet","",pktnum);
  4245.     if (chkint() < 0)        /* Especially important if streaming */
  4246.       return(-9);
  4247.     if (cxseen || czseen) {        /* If interrupted, done. */
  4248.         if (wslots > 1) {
  4249.         drain = 1;
  4250.         debug(F100,"sdata cx/zseen windowing","",0);
  4251.         return(0);
  4252.         } else {
  4253.         debug(F100,"sdata cx/zseen nonwindowing","",0);
  4254.         return(-1);
  4255.         }
  4256.     }
  4257. #ifdef DEBUG
  4258.     if (deblog) {
  4259.         debug(F101,"sdata spsiz","",spsiz);
  4260.         debug(F101,"sdata binary","",binary);
  4261.         debug(F101,"sdata parity","",parity);
  4262.     }
  4263. #endif /* DEBUG */
  4264. #ifdef CKTUNING
  4265.     if (binary && !parity && !memstr && !funcstr)
  4266.       len = bgetpkt(spsiz);
  4267.     else
  4268.       len = getpkt(spsiz,1);
  4269. #else
  4270.     len = getpkt(spsiz,1);
  4271. #endif /* CKTUNING */
  4272.     s = (char *)data;
  4273.     if (len == -3) {        /* Timed out */
  4274.         s = "";            /* Send an empty data packet. */
  4275.         len = 0;
  4276.     } else if (len == 0) {        /* Done if no data. */
  4277.         if (pktnum == winlo)
  4278.           return(-1);
  4279.         drain = 1;            /* But can't return -1 until all */
  4280.         debug(F101,"sdata eof, drain","",drain);
  4281.         return(0);            /* ACKs are drained. */
  4282.     }
  4283.     debug(F101,"sdata pktnum","",pktnum);
  4284.     debug(F101,"sdata len","",len);
  4285.     debug(F011,"sdata data",data,len);
  4286.  
  4287.     x = spack('D',pktnum,len,(CHAR *)s); /* Send the data packet. */
  4288.     if (x < 0)
  4289.       return(x);
  4290.     if (streaming)            /* What an ACK would do. */
  4291.       winlo = pktnum;
  4292.     x = ttchk();            /* Peek at input buffer. */
  4293.     debug(F101,"sdata ttchk","",x);    /* ACKs waiting, maybe?  */
  4294.     if (x < 0)            /* Or connection broken? */
  4295.       return(x);
  4296. /*
  4297.   Here we check to see if any ACKs or NAKs have arrived, in which case we
  4298.   break out of the D-packet-sending loop and return to the state switcher
  4299.   to process them.  This is what makes our windows slide instead of lurch.
  4300. */
  4301.     if (
  4302. #ifdef GEMDOS
  4303. /*
  4304.   In the Atari ST version, ttchk() can only return 0 or 1.  But note: x will
  4305.   probably always be > 0, since the as-yet-unread packet terminator from the
  4306.   last packet is probably still in the buffer, so sliding windows will
  4307.   probably never happen when the Atari ST is the file sender.  The alternative
  4308.   is to say "if (0)", in which case the ST will always send a window full of
  4309.   packets before reading any ACKs or NAKs.
  4310. */
  4311.         x > 0
  4312.  
  4313. #else /* !GEMDOS */
  4314. /*
  4315.   In most other versions, ttchk() returns the actual count.
  4316.   It can't be a Kermit packet if it's less than five bytes long.
  4317. */
  4318.         x > 4 + bctu
  4319.  
  4320. #endif /* GEMDOS */
  4321.         )
  4322.       return(1);            /* Yes, stop sending data packets */
  4323.     }                    /* and go try to read the ACKs. */
  4324.     return(1);
  4325. }
  4326.  
  4327.  
  4328. /*  S E O F -- Send an End-Of-File packet */
  4329.  
  4330. /*  Call with a string pointer to character to put in the data field, */
  4331. /*  or else a null pointer or "" for no data.  */
  4332.  
  4333. /*
  4334.   There are two "send-eof" functions.  seof() is used to send the normal eof
  4335.   packet at the end of a file's data (even if the file has no data), or when
  4336.   a file transfer is interrupted.  sxeof() is used to send an EOF packet that
  4337.   occurs because of attribute refusal.  The difference is purely a matter of
  4338.   buffer allocation and packet sequence number management.  Both functions
  4339.   act as "front ends" to the common send-eof function, szeof().
  4340. */
  4341.  
  4342. /* Code common to both seof() and sxeof() */
  4343.  
  4344. int
  4345. szeof(s) CHAR *s; {
  4346.     int x;
  4347.     lsstate = 0;            /* Cancel locking-shift state */
  4348.     if (!s) s = (CHAR *)"";
  4349.     debug(F111,"szeof",s,pktnum);
  4350.     if (*s) {
  4351.     x = spack('Z',pktnum,1,s);
  4352.     xitsta |= W_SEND;
  4353. #ifdef COMMENT
  4354.     tlog(F100," *** interrupted, sending discard request","",0L);
  4355. #endif /* COMMENT */
  4356.     filrej++;
  4357.     } else {
  4358.     x = spack('Z',pktnum,0,(CHAR *)"");
  4359.     }
  4360.     if (x < 0)
  4361.       return(x);
  4362.     discard = 0;            /* Turn off per-file discard flag */
  4363. #ifdef PIPESEND
  4364. /* If we were sending from a pipe, we're not any more... */
  4365.     pipesend = 0;
  4366. #endif /* PIPESEND */
  4367.     return(0);
  4368. }
  4369.  
  4370. int
  4371. seof(x) int x; {
  4372.     char * s;
  4373. /*
  4374.   ckcpro.w, before calling seof(), sets window size back to 1 and then calls
  4375.   window(), which clears out the old buffers.  This is OK because the final
  4376.   data packet for the file has been ACK'd.  However, sdata() has already
  4377.   called nxtpkt(), which set the new value of pktnum which seof() will use.
  4378.   So all we need to do here is is allocate a new send-buffer.
  4379. */
  4380.     s = x ? "D" : "";
  4381.     debug(F111,"seof",s,pktnum);
  4382.     if (getsbuf(pktnum) < 0) {    /* Get a buffer for packet n */
  4383.     debug(F101,"seof can't get s-buffer","",pktnum);
  4384.     return(-1);
  4385.     }
  4386.     return(szeof((CHAR *)s));
  4387. }
  4388.  
  4389. /*
  4390.   Version of seof() to be called when sdata() has not been called before.  The
  4391.   difference is that this version calls nxtpkt() to allocate a send-buffer and
  4392.   get the next packet number.
  4393. */
  4394. int
  4395. sxeof(x) int x; {
  4396.     char * s;
  4397.     s = x ? "D" : "";
  4398.     if (nxtpkt() < 0)            /* Get next pkt number and buffer */
  4399.       debug(F101,"sxeof nxtpkt fails","",pktnum);
  4400.     else
  4401.       debug(F101,"sxeof packet","",pktnum);
  4402.     return(szeof((CHAR *)s));
  4403. }
  4404.  
  4405. /*  S E O T -- Send an End-Of-Transaction packet */
  4406.  
  4407. int
  4408. seot() {
  4409.     int x;
  4410.     x = nxtpkt();
  4411.     debug(F101,"seot nxtpkt","",x);
  4412.     if (x < 0) return(-1);        /* Bump packet number, get buffer */
  4413.     x = spack('B',pktnum,0,(CHAR *)"");    /* Send the EOT packet */
  4414.     if (x < 0)
  4415.       return(x);
  4416.     cxseen = czseen = discard = 0;    /* Reset interruption flags */
  4417.     tstats();                /* Log timing info */
  4418.     return(0);
  4419. }
  4420.  
  4421.  
  4422. /*   R P A R -- Fill the data array with my send-init parameters  */
  4423.  
  4424. int q8flag = 0;
  4425.  
  4426. CHAR dada[32];                /* Use this instead of data[]. */
  4427.                     /* To avoid some kind of wierd */
  4428.                     /* addressing foulup in spack()... */
  4429.                     /* (which might be fixed now...) */
  4430.  
  4431. CHAR *
  4432. rpar() {
  4433.     char *p;
  4434.     int i, x, max;
  4435.     extern int sprmlen;
  4436.  
  4437.     max = maxdata();            /* Biggest data field I can send */
  4438.     debug(F101, "rpar max 1","",max);
  4439.     debug(F101, "rpar sprmlen","",sprmlen);
  4440.     if (sprmlen > 1 && sprmlen < max)    /* User override */
  4441.       max = sprmlen;
  4442.     debug(F101, "rpar max 2","",max);
  4443.  
  4444.     if (rpsiz > MAXPACK)        /* Biggest normal packet I want. */
  4445.       dada[0] = (char) tochar(MAXPACK);    /* If > 94, use 94, but specify */
  4446.     else                /* extended packet length below... */
  4447.       dada[0] = (char) tochar(rpsiz);    /* else use what the user said. */
  4448.     dada[1] = (char) tochar(chktimo(pkttim,0)); /* When to time me out */
  4449.     dada[2] = (char) tochar(mypadn);    /* How much padding I need (none) */
  4450.     dada[3] = (char) ctl(mypadc);    /* Padding character I want */
  4451.     dada[4] = (char) tochar(eol);    /* End-Of-Line character I want */
  4452.     dada[5] = myctlq;            /* Control-Quote character I send */
  4453.  
  4454.     if (max < 6) { dada[6] = NUL; rqf = 0; ebq = sq = NUL; return(dada); }
  4455.  
  4456.     switch (rqf) {            /* 8th-bit prefix (single-shift) */
  4457.       case -1:                /* I'm opening the bids */
  4458.       case  1:                /* Other Kermit already bid 'Y' */
  4459.     if (parity) ebq = sq = MYEBQ;    /* So I reply with '&' if parity */
  4460.     break;                /*  otherwise with 'Y'. */
  4461.       case  2:                /* Other Kermit sent a valid prefix */
  4462.     if (q8flag)
  4463.       sq = ebq;
  4464.       case  0:                /* Other Kermit bid nothing */
  4465.     break;                /* So I reply with 'Y'. */
  4466.     }
  4467.     debug(F000,"rpar 8bq sq","",sq);
  4468.     debug(F000,"rpar 8bq ebq","",ebq);
  4469.     if (lscapu == 2)            /* LOCKING-SHIFT FORCED */
  4470.       dada[6] = 'N';            /* requires no single-shift */
  4471.     else                /* otherwise send prefix or 'Y' */
  4472.       dada[6] = (char) sq;
  4473.     ebqsent = dada[6];            /* And remember what I really sent */
  4474.  
  4475.     if (max < 7) { dada[7] = NUL; bctr = 1; return(dada); }
  4476.  
  4477.     dada[7] = (char) (bctr == 4) ? 'B' : bctr + '0'; /* Block check type */
  4478.  
  4479.     if (max < 8) { dada[8] = NUL; rptflg = 0; return(dada); }
  4480.  
  4481.     if (rptena) {
  4482.     if (rptflg)            /* Run length encoding */
  4483.       dada[8] = (char) rptq;    /* If receiving, agree */
  4484.     else                /* by replying with same character. */
  4485.       dada[8] = (char) (rptq = myrptq); /* When sending use this. */
  4486.     } else dada[8] = SP;        /* Not enabled, put a space here. */
  4487.  
  4488.     /* CAPAS mask */
  4489.  
  4490.     if (max < 9) {
  4491.     dada[9] = NUL;
  4492.     atcapr = 0;
  4493.     lpcapr = 0;
  4494.     swcapr = 0;
  4495.     rscapr = 0;
  4496.     return(dada);
  4497.     }
  4498.     dada[9] = (char) tochar((lscapr ? lscapb : 0) | /* Locking shifts */
  4499.              (atcapr ? atcapb : 0) | /* Attribute packets */
  4500.              (lpcapr ? lpcapb : 0) | /* Long packets */
  4501.              (swcapr ? swcapb : 0) | /* Sliding windows */
  4502.              (rscapr ? rscapb : 0)); /* RESEND */
  4503.     if (max < 10) { wslotr = 1; return(dada); }
  4504.     dada[10] = (char) tochar(swcapr ? wslotr : 1); /* CAPAS+1 = Window size */
  4505.  
  4506.     if (max < 12) { rpsiz = 80; return(dada); }
  4507.     if (urpsiz > 94)
  4508.       rpsiz = urpsiz - 1;        /* Long packets ... */
  4509.     dada[11] = (char) tochar(rpsiz / 95); /* Long packet size, big part */
  4510.     dada[12] = (char) tochar(rpsiz % 95); /* Long packet size, little part */
  4511.  
  4512.     if (max < 16) return(dada);
  4513.     dada[13] = '0';            /* CAPAS+4 = WONT CHKPNT */
  4514.     dada[14] = '_';            /* CAPAS+5 = CHKINT (reserved) */
  4515.     dada[15] = '_';            /* CAPAS+6 = CHKINT (reserved) */
  4516.     dada[16] = '_';            /* CAPAS+7 = CHKINT (reserved) */
  4517.     if (max < 17) return(dada);
  4518. #ifndef WHATAMI
  4519.     dada[17] = ' ';
  4520. #else
  4521.     x = 0;
  4522.     if (server) x |= WMI_SERVE;        /* Whether I am a server */
  4523.     if (binary) x |= WMI_FMODE;        /* My file transfer mode is ... */
  4524.     if (fncnv)  x |= WMI_FNAME;        /* My filename conversion is ... */
  4525. #ifdef STREAMING
  4526.     if (streamrq == SET_ON)
  4527.       x |= WMI_STREAM;
  4528.     else if (streamrq == SET_AUTO && reliable == SET_ON)
  4529.       x |= WMI_STREAM;
  4530.     /*
  4531.       Always offer to stream when in remote mode and STREAMING is AUTO
  4532.       and RELIABLE is not OFF (i.e. is ON or AUTO).
  4533.     */
  4534.     else if (!local && streamrq == SET_AUTO && reliable != SET_OFF)
  4535.       x |= WMI_STREAM;
  4536. #endif /* STREAMING */
  4537. #ifdef TCPSOCKET
  4538.     if (clearrq == SET_ON)
  4539.       x |= WMI_CLEAR;
  4540.     else if (clearrq == SET_AUTO &&    /* SET CLEAR-CHANNEL AUTO */
  4541.          ((network && nettype == 1    /* We have a NET_TCPB connection */
  4542. #ifdef RLOGCODE
  4543.                 && ttnproto != NP_RLOGIN/* Rlogin is not clear */
  4544.                 && !(ttnproto >= NP_K4LOGIN && ttnproto <= NP_EK5LOGIN)
  4545. #endif /* RLOGCODE */
  4546.            )
  4547. #ifdef IKSD
  4548.           || inserver        /* We are IKSD */
  4549. #endif /* IKSD */
  4550.           ))
  4551.       x |= WMI_CLEAR;
  4552. #endif /* TCPSOCKET */
  4553.     x |= WMI_FLAG;
  4554.     dada[17] = (char) tochar(x);
  4555. #endif /* WHATAMI */
  4556.     i = 18;                /* Position of next field */
  4557.     p = cksysid;            /* WHOAMI (my system ID) */
  4558.     x = strlen(p);
  4559.     if (max - i < x + 1) return(dada);
  4560.     if (x > 0) {
  4561.     dada[i++] = (char) tochar(x);
  4562.     while (*p)
  4563.       dada[i++] = *p++;
  4564.     }
  4565.  
  4566.     if (max < i+1) return(dada);
  4567. #ifndef WHATAMI                /* WHATAMI2 */
  4568.     dada[i++] = ' ';
  4569. #else
  4570.     debug(F101,"rpar xfermode","",xfermode);
  4571.     x = WMI2_FLAG;            /* Is-Valid flag */
  4572.     if (xfermode != XMODE_A)        /* If TRANSFER MODE is MANUAL */
  4573.       x |= WMI2_XMODE;            /* set the XFERMODE bit */
  4574.     if (recursive > 0)            /* If this is a recursive transfer */
  4575.       x |= WMI2_RECU;            /* set the RECURSIVE bit */
  4576.     dada[i++] = tochar(x);
  4577.     debug(F101,"rpar whatami2","",x);
  4578. #endif /* WHATAMI */
  4579.  
  4580.     dada[i] = '\0';            /* Terminate the init string */
  4581.  
  4582. #ifdef DEBUG
  4583.     if (deblog) {
  4584.     debug(F110,"rpar",dada,0);
  4585.     rdebu(dada,(int)strlen((char *)dada));
  4586.     }
  4587. #endif /* DEBUG */
  4588.     ckstrncpy((char *)myinit,(char *)dada,MYINITLEN);
  4589.     return(dada);            /* Return pointer to string. */
  4590. }
  4591.  
  4592. int
  4593. spar(s) CHAR *s; {            /* Set parameters */
  4594.     int x, y, lpsiz, biggest;
  4595.     extern int rprmlen, lastspmax;
  4596.     extern int sysindex;
  4597.     extern struct sysdata sysidlist[];
  4598.  
  4599.     whatru = 0;
  4600.     whoareu[0] = NUL;
  4601. #ifdef STREAMING
  4602.     streamok = 0;
  4603.     streaming = 0;
  4604. #endif /* STREAMING */
  4605.     biggest = rln;
  4606.  
  4607.     debug(F101, "spar biggest 1","",biggest);
  4608.     debug(F101, "spar rprmlen","",rprmlen);
  4609.     if (rprmlen > 1 && rprmlen < biggest)
  4610.       biggest = rprmlen;
  4611.     debug(F101, "rpar biggest 2","",biggest);
  4612.     debug(F110,"spar packet",s,0);
  4613.  
  4614.     s--;                /* Line up with field numbers. */
  4615.  
  4616. /* Limit on size of outbound packets */
  4617.     x = (biggest >= 1) ? xunchar(s[1]) : 80;
  4618.     lpsiz = spsizr;            /* Remember what they SET. */
  4619.     if (spsizf) {            /* SET-command override? */
  4620.     if (x < spsizr) spsiz = x;    /* Ignore LEN unless smaller */
  4621.     } else {                /* otherwise */
  4622.     spsiz = (x < 10) ? 80 : x;    /* believe them if reasonable */
  4623.     }
  4624.     spmax = spsiz;            /* Remember maximum size */
  4625.  
  4626. /* Timeout on inbound packets */
  4627.     if (timef) {
  4628.     timint = rtimo;            /* SET SEND TIMEOUT value overrides */
  4629.     } else {                /* Otherwise use requested value, */
  4630.     x = (biggest >= 2) ? xunchar(s[2]) : rtimo; /* if it is legal. */
  4631.     timint = (x < 0) ? rtimo : x;
  4632.     }
  4633.     timint = chktimo(timint,timef);    /* Adjust if necessary */
  4634.  
  4635. /* Outbound Padding */
  4636.     npad = 0; padch = '\0';
  4637.     if (biggest >= 3) {
  4638.     npad = xunchar(s[3]);
  4639.     if (biggest >= 4) padch = (CHAR) ctl(s[4]); else padch = 0;
  4640.     }
  4641.     if (npad) {
  4642.     int i;
  4643.     for (i = 0; i < npad; i++) padbuf[i] = dopar(padch);
  4644.     }
  4645.  
  4646. /* Outbound Packet Terminator */
  4647.     seol = (CHAR) (biggest >= 5) ? xunchar(s[5]) : CR;
  4648.     if ((seol < 1) || (seol > 31)) seol = CR;
  4649.  
  4650. /* Control prefix that the other Kermit is sending */
  4651.     x = (biggest >= 6) ? s[6] : '#';
  4652.     ctlq = (CHAR) (((x > 32 && x < 63) || (x > 95 && x < 127)) ? x : '#');
  4653.  
  4654. /* 8th-bit prefix */
  4655. /*
  4656.   NOTE: Maybe this could be simplified using rcvtyp.
  4657.   If rcvtyp == 'Y' then we're reading the ACK,
  4658.   otherwise we're reading the other Kermit's initial bid.
  4659.   But his horrendous code has been working OK for years, so...
  4660. */
  4661.     rq = (biggest >= 7) ? s[7] : 0;
  4662.     if (rq == 'Y') rqf = 1;
  4663.       else if ((rq > 32 && rq < 63) || (rq > 95 && rq < 127)) rqf = 2;
  4664.         else rqf = 0;
  4665.     debug(F000,"spar 8bq rq","",rq);
  4666.     debug(F000,"spar 8bq sq","",sq);
  4667.     debug(F000,"spar 8bq ebq","",ebq);
  4668.     debug(F101,"spar 8bq rqf","",rqf);
  4669.     switch (rqf) {
  4670.       case 0:                /* Field is missing from packet. */
  4671.     ebqflg = 0;            /* So no 8th-bit prefixing. */
  4672.     break;
  4673.       case 1:                /* Other Kermit sent 'Y' = Will Do. */
  4674.     /*
  4675.           When I am the file receiver, ebqsent is 0 because I didn't send a
  4676.           negotiation yet.  If my parity is set to anything other than NONE,
  4677.           either because my user SET PARITY or because I detected parity bits
  4678.           on this packet, I reply with '&', otherwise 'Y'.
  4679.  
  4680.       When I am the file sender, ebqsent is what I just sent in rpar(),
  4681.           which can be 'Y', 'N', or '&'.  If I sent '&', then this 'Y' means
  4682.           the other Kermit agrees to do 8th-bit prefixing.
  4683.  
  4684.           If I sent 'Y' or 'N', but then detected parity on the ACK packet
  4685.           that came back, then it's too late: there is no longer any way for
  4686.           me to tell the other Kermit that I want to do 8th-bit prefixing, so
  4687.           I must not do it, and in that case, if there is any 8-bit data in
  4688.           the file to be transferred, the transfer will fail because of block
  4689.           check errors.
  4690.  
  4691.           The following clause covers all of these situations:
  4692.     */
  4693.     if (parity && (ebqsent == 0 || ebqsent == '&')) {
  4694.         ebqflg = 1;
  4695.         ebq = MYEBQ;
  4696.     }
  4697.     break;
  4698.       case 2:                /* Other Kermit sent a valid prefix */
  4699.     ebqflg = (ebq == sq || sq == 'Y');
  4700.     if (ebqflg) {
  4701.         ebq = rq;
  4702.         debug(F101,"spar setting parity to space","",ebq);
  4703.         if (!parity) parity = ttprty = 's';
  4704.     }
  4705.     }
  4706.     if (lscapu == 2) {     /* But no single-shifts if LOCKING-SHIFT FORCED */
  4707.     ebqflg = 0;
  4708.     ebq = 'N';
  4709.     }
  4710.  
  4711. /* Block check */
  4712.     x = 1;
  4713.     if (biggest >= 8) {
  4714.     if (s[8] == 'B') x = 4;
  4715.     else x = s[8] - '0';
  4716.     if ((x < 1) || (x > 4)) x = 1;
  4717.     }
  4718.     bctr = x;
  4719.  
  4720. /* Repeat prefix */
  4721.  
  4722.     rptflg = 0;                /* Assume no repeat-counts */
  4723.     if (biggest >= 9) {            /* Is there a repeat-count field? */
  4724.     char t;                /* Yes. */
  4725.     t = s[9];            /* Get its contents. */
  4726. /*
  4727.   If I'm sending files, then I'm reading these parameters from an ACK, and so
  4728.   this character must agree with what I sent.
  4729. */
  4730.     if (rptena) {            /* If enabled ... */
  4731.         if ((char) rcvtyp == 'Y') {    /* Sending files, reading ACK. */
  4732.         if (t == myrptq) rptflg = 1;
  4733.         } else {            /* I'm receiving files */
  4734.         if ((t > 32 && t < 63) || (t > 95 && t < 127)) {
  4735.             rptflg = 1;
  4736.             rptq = t;
  4737.         }
  4738.         }
  4739.     } else rptflg = 0;
  4740.     }
  4741.  
  4742. /* Capabilities */
  4743.  
  4744.     atcapu = lpcapu = swcapu = rscapu = 0; /* Assume none of these. */
  4745.     if (lscapu != 2) lscapu = 0;    /* Assume no LS unless forced. */
  4746.     y = 11;                /* Position of next field, if any */
  4747.     if (biggest >= 10) {
  4748.         x = xunchar(s[10]);
  4749.     debug(F101,"spar capas","",x);
  4750.         atcapu = (x & atcapb) && atcapr; /* Attributes */
  4751.     lpcapu = (x & lpcapb) && lpcapr; /* Long packets */
  4752.     swcapu = (x & swcapb) && swcapr; /* Sliding windows */
  4753.     rscapu = (x & rscapb) && rscapr; /* RESEND */
  4754.     debug(F101,"spar lscapu","",lscapu);
  4755.     debug(F101,"spar lscapr","",lscapr);
  4756.     debug(F101,"spar ebqflg","",ebqflg);
  4757.     if (lscapu != 2) lscapu = ((x & lscapb) && lscapr && ebqflg) ? 1 : 0;
  4758.     debug(F101,"spar swcapr","",swcapr);
  4759.     debug(F101,"spar swcapu","",swcapu);
  4760.     debug(F101,"spar lscapu","",lscapu);
  4761.     for (y = 10; (xunchar(s[y]) & 1) && (biggest >= y); y++);
  4762.     debug(F101,"spar y","",y);
  4763.     }
  4764.  
  4765. /* Long Packets */
  4766.     debug(F101,"spar lpcapu","",lpcapu);
  4767.     if (lpcapu) {
  4768.         if (biggest > y+1) {
  4769.         x = xunchar(s[y+2]) * 95 + xunchar(s[y+3]);
  4770.         debug(F101,"spar lp len","",x);
  4771.         if (spsizf) {        /* If overriding negotiations */
  4772.         spsiz = (x < lpsiz) ? x : lpsiz; /* do this, */
  4773.         } else {                     /* otherwise */
  4774.         spsiz = (x > MAXSP) ? MAXSP : x; /* do this. */
  4775.         }
  4776.         if (spsiz < 10) spsiz = 80;    /* Be defensive... */
  4777.     }
  4778.     }
  4779.     /* (PWP) save current send packet size for optimal packet size calcs */
  4780.     spmax = spsiz;            /* Maximum negotiated length */
  4781.     lastspmax = spsiz;            /* For stats */
  4782.     if (slostart && spsiz > 499)    /* Slow start length */
  4783.       spsiz = 244;
  4784.     debug(F101,"spar slow-start spsiz","",spsiz);
  4785.     debug(F101,"spar lp spmax","",spmax);
  4786.     timint = chktimo(timint,timef);    /* Recalculate the packet timeout */
  4787.  
  4788. /* Sliding Windows... */
  4789.  
  4790.     if (swcapr) {            /* Only if requested... */
  4791.         if (biggest > y) {        /* See what other Kermit says */
  4792.         x = xunchar(s[y+1]);
  4793.         debug(F101,"spar window","",x);
  4794.         wslotn = (x > MAXWS) ? MAXWS : x;
  4795. /*
  4796.   wslotn = negotiated size (from other Kermit's S or I packet).
  4797.   wslotr = requested window size (from this Kermit's SET WINDOW command).
  4798. */
  4799.         if (wslotn > wslotr)    /* Use the smaller of the two */
  4800.           wslotn = wslotr;
  4801.         if (wslotn < 1)        /* Watch out for bad negotiation */
  4802.           wslotn = 1;
  4803.         if (wslotn > 1) {
  4804.         swcapu = 1;        /* We do windows... */
  4805.         if (wslotn > maxtry)    /* Retry limit must be greater */
  4806.           maxtry = wslotn + 1;    /* than window size. */
  4807.         }
  4808.         debug(F101,"spar window after adjustment","",x);
  4809.     } else {            /* No window size specified. */
  4810.         wslotn = 1;            /* We don't do windows... */
  4811.         debug(F101,"spar window","",x);
  4812.         swcapu = 0;
  4813.         debug(F101,"spar no windows","",wslotn);
  4814.     }
  4815.     }
  4816.  
  4817. /* Now recalculate packet length based on number of windows.   */
  4818. /* The nogotiated number of window slots will be allocated,    */
  4819. /* and the maximum packet length will be reduced if necessary, */
  4820. /* so that a windowful of packets can fit in the big buffer.   */
  4821.  
  4822.     if (wslotn > 1) {            /* Shrink to fit... */
  4823.     x = adjpkl(spmax,wslotn,bigsbsiz);
  4824.     if (x < spmax) {
  4825.         spmax = x;
  4826.         lastspmax = spsiz;
  4827.         if (slostart && spsiz > 499) spsiz = 244; /* Slow start again */
  4828.         debug(F101,"spar sending, redefine spmax","",spmax);
  4829.     }
  4830.     }
  4831. #ifdef WHATAMI
  4832.     debug(F101,"spar biggest","",biggest);
  4833.     if (biggest > y+7) {        /* Get WHATAMI info if any */
  4834.     whatru = xunchar(s[y+8]);
  4835.     debug(F101,"spar whatru","",whatru);
  4836.     }
  4837.     if (whatru & WMI_FLAG) {        /* Only valid if this bit is set */
  4838. #ifdef STREAMING
  4839.     if (whatru & WMI_STREAM) {
  4840.         if (streamrq == SET_ON ||
  4841.         (streamrq == SET_AUTO &&
  4842.          (reliable == SET_ON || (reliable == SET_AUTO && !local)
  4843. #ifdef IKSD
  4844.                    || inserver
  4845. #endif /* IKSD */
  4846.                    ))) {
  4847.         streamok = 1;        /* Streaming negotiated */
  4848.         slostart = 0;        /* Undo slow-start machinations */
  4849.         spsiz = lastspmax;
  4850.         }
  4851.     }
  4852.     streamed = streamok;
  4853.     debug(F101,"spar streamok","",streamok);
  4854.     debug(F101,"spar clearrq","",clearrq);
  4855.     if (clearrq == SET_ON ||
  4856.              (clearrq == SET_AUTO &&
  4857.                ((network && nettype == 1
  4858. #ifdef RLOGCODE
  4859.                 && ttnproto != NP_RLOGIN/* Rlogin is not clear */
  4860.                 && !(ttnproto >= NP_K4LOGIN && ttnproto <= NP_EK5LOGIN)
  4861. #endif /* RLOGCODE */
  4862.                   )
  4863. #ifdef IKSD
  4864.                  || inserver
  4865. #endif /* IKSD */
  4866.                  )))
  4867.           urclear = (whatru & WMI_CLEAR);
  4868.     debug(F101,"spar urclear","",urclear);
  4869. #ifdef CK_SPEED
  4870.     if (urclear)
  4871.       setprefix(PX_NON);
  4872. #endif /* CK_SPEED */
  4873.     cleared = urclear;
  4874. #endif /* STREAMING */
  4875.     }
  4876. #endif /* WHATAMI */
  4877.  
  4878.     if (biggest > y+8) {        /* Get WHOAREYOU info if any */
  4879.     int x, z;
  4880.     x = xunchar(s[y+9]);        /* Length of it */
  4881.     z = y;
  4882.     y += (9 + x);
  4883.     debug(F101,"spar sysindex x","",x);
  4884.     debug(F101,"spar sysindex y","",y);
  4885.     debug(F101,"spar sysindex biggest","",biggest);
  4886.  
  4887.     if (x > 0 && x < 16 && biggest >= y) {
  4888.         strncpy(whoareu,(char *)s+z+10,x); /* Other Kermit's system ID */
  4889.         debug(F111,"spar whoareyou",whoareu,whoareu[0]);
  4890.         if (whoareu[0]) {        /* Got one? */
  4891.         sysindex = getsysix((char *)whoareu);
  4892.         debug(F101,"spar sysindex",whoareu,sysindex);
  4893.         }
  4894.     }
  4895.     } else
  4896.       goto xspar;
  4897.  
  4898. #ifdef WHATAMI
  4899.     y++;                /* Advance pointer */
  4900.     if (biggest >= y) {
  4901.     whatru2 = xunchar(s[y]);    /* Next field is WHATAMI2 */
  4902.     debug(F101,"spar whatru2","",whatru2);
  4903.     if (whatru2 & WMI2_FLAG) {    /* Valid only if this bit is set */
  4904.         if (server) {        /* Server obeys client's xfer mode */
  4905.         xfermode = (whatru2 & WMI2_XMODE) ? XMODE_M : XMODE_A;
  4906.         debug(F101,"spar whatru2 xfermode","",xfermode);
  4907.         }
  4908.         if (whatru2 & WMI2_RECU) {    /* RECURSIVE transfer */
  4909.         if (fnrpath == PATH_AUTO) { /* If REC PATH AUTO */
  4910.             fnrpath = PATH_REL;    /* Set it to RELATIVE */
  4911.             autopath = 1;    /* and remember we did this */
  4912.         }
  4913.         }
  4914.     }
  4915.     }
  4916. #endif /* WHATAMI */
  4917.  
  4918.   xspar:
  4919.     if (sysindex > -1) {
  4920.     char * p;
  4921.     p = sysidlist[sysindex].sid_name;
  4922.     tlog(F110,"Remote system type: ",p,0L);
  4923.     if (sysindex > 0) {        /* If partnet's system type known */
  4924.         whoarewe();            /* see if we are a match. */
  4925. #ifdef CK_SPEED
  4926. /* Never unprefix XON and XOFF when sending to VMS */
  4927.         debug(F111,"proto whoareu",whoareu,sysindex);
  4928.         if (!strcmp((char *)whoareu,"D7")) {
  4929.         debug(F111,"proto special VMS prefixing","",0);
  4930.         ctlp[XON] = ctlp[XOFF] = 1;
  4931.         ctlp[XON+128] = ctlp[XOFF+128] = 1;
  4932.         ctlp[3] = 1;        /* Ctrl-C might be dangerous too */
  4933.         ctlp[14] = ctlp[15] = 1; /* And SO/SI */
  4934.         ctlp[24] = ctlp[25] = 1; /* And ^X/^Y */
  4935.         ctlp[141] = 1;        /* And CR+128 */
  4936.         }
  4937. #endif /* CK_SPEED */
  4938.     }
  4939.     }
  4940.  
  4941. #ifdef PATTERNS
  4942. /* If attributes not negotiated and FILE PATTERNS is AUTO... */
  4943.     if (patterns == SET_AUTO && atcapu == 0) /* Turn them OFF. */
  4944.       patterns = SET_OFF;
  4945. #endif /* PATTERNS */
  4946.  
  4947. /* Record parameters in debug log */
  4948. #ifdef DEBUG
  4949.     if (deblog) sdebu(biggest);
  4950. #endif /* DEBUG */
  4951.     numerrs = 0;            /* Start counting errors here. */
  4952.     return(0);
  4953. }
  4954.  
  4955. /* Criteria used by gnfile()... */
  4956.  
  4957. char sndafter[19]   = { NUL, NUL };
  4958. char sndbefore[19]  = { NUL, NUL };
  4959. char sndnafter[19]  = { NUL, NUL };
  4960. char sndnbefore[19] = { NUL, NUL };
  4961. char *sndexcept[8]  = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  4962. char *rcvexcept[8]  = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  4963. long sndsmaller = -1L;
  4964. long sndlarger  = -1L;
  4965.  
  4966. /*  G N F I L E  --  Get name of next file to send  */
  4967. /*
  4968.   Expects global sndsrc to be:
  4969.    -9: if we are generating a file internally for calibration.
  4970.    -1: next filename to be obtained by calling znext().
  4971.     0: no next file name
  4972.     1: (or greater) next filename to be obtained from **cmlist,
  4973.        or if addlist != 0, from the "filehead" linked list,
  4974.        or if filefile pointer not null from that file (which is already open).
  4975.   Returns:
  4976.     1, with name of next file in filnam.
  4977.     0, no more files, with filnam set to empty string.
  4978.    -1, file not found
  4979.    -2, file is not readable (but then we just skip to the next one if any)
  4980.    -3, read access denied
  4981.    -4, cancelled
  4982.    -5, too many files match wildcard
  4983. */
  4984. int
  4985. gnfile() {
  4986.     int i = 0, x = 0; long y = 0L;
  4987.     int retcode = 0;
  4988.     char fullname[CKMAXPATH+1];
  4989.  
  4990.     debug(F101,"gnfile sndsrc","",sndsrc);
  4991.     debug(F101,"gnfile filcnt","",filcnt);
  4992.     debug(F101,"gnfile what","",what);
  4993.     fsize = -1L;            /* Initialize file size */
  4994.     fullname[0] = NUL;
  4995.     if (what != W_REMO && xfermode == XMODE_A
  4996. #ifndef NOMSEND
  4997.     && !addlist
  4998. #endif /* NOMSEND */
  4999.     ) {
  5000. #ifdef WHATAMI
  5001.     /* We don't do this in server mode because it undoes WHATAMI */
  5002.     if (!server || (server && (whatru & WMI_FLAG == 0)))
  5003. #endif /* WHATAMI */
  5004.       binary = gnf_binary;        /* Restore prevailing transfer mode */
  5005.     debug(F101,"gnfile binary = gnf_binary","",gnf_binary);
  5006.     }
  5007. #ifdef PIPESEND
  5008.     debug(F101,"gnfile pipesend","",pipesend);
  5009.     if (pipesend) {            /* First one */
  5010.     if (filcnt == 0) {
  5011.         ckstrncpy(filnam,cmarg,CKMAXPATH+1);
  5012.         return(1);
  5013.     } else {            /* There's only one... */
  5014.         *filnam = NUL;
  5015.         pipesend = 0;
  5016.         return(0);
  5017.     }
  5018.     }
  5019. #endif /* PIPESEND */
  5020.  
  5021. #ifdef CALIBRATE
  5022.     if (sndsrc == -9) {
  5023.     strcpy(filnam,"CALIBRATION");
  5024.     nfils = 0;
  5025.     cal_j = 0;
  5026.     fsize = calibrate;
  5027.     sndsrc = 0;            /* For next time */
  5028.     nfils = 0;
  5029.     return(1);
  5030.     }
  5031. #endif /* CALIBRATE */
  5032.  
  5033. #ifndef NOSPL
  5034.     if (sndarray) {            /* Sending from an array */
  5035.     extern char sndxnam[];        /* Pseudo filename */
  5036.     nfils = 0;
  5037.     fsize = -1L;            /* Size unknown */
  5038.     sndsrc = 0;
  5039.     ckstrncpy(filnam,sndxnam,CKMAXPATH);
  5040.     return(1);
  5041.     }
  5042. #endif /* NOSPL */
  5043.  
  5044.     if (sndsrc == 0) {            /* It's not really a file */
  5045.     if (nfils > 0) {        /* It's a pipe, or stdin */
  5046.         ckstrncpy(filnam,*cmlist,CKMAXPATH+1); /* Copy its "name" */
  5047.         nfils = 0;            /* There is no next file */
  5048.         return(1);            /* OK this time */
  5049.     } else return(0);        /* but not next time */
  5050.     }
  5051.  
  5052. /* If file group interruption (C-Z) occurred, fail.  */
  5053.  
  5054.     if (czseen) {
  5055.     tlog(F100,"Transaction cancelled","",0L);
  5056.         debug(F100,"gnfile czseen","",0);
  5057.     return(-4);
  5058.     }
  5059.  
  5060. /* Loop through file list till we find a readable, sendable file */
  5061.  
  5062.     y = -1L;                /* Loop exit (file size) variable */
  5063.     while (y < 0L) {            /* Keep trying till we get one... */
  5064.     retcode = 0;
  5065.     if (sndsrc > 0) {        /* File list in cmlist or file */
  5066.         if (filefile) {        /* Reading list from file... */
  5067.         if (zsinl(ZMFILE,filnam,CKMAXPATH) < 0) { /* Read a line */
  5068.             zclose(ZMFILE);                      /* Failed */
  5069.             debug(F110,"gnfile filefile EOF",filefile,0);
  5070.             makestr(&filefile,NULL);
  5071.             return(0);
  5072.         }
  5073.         debug(F110,"gnfile filefile filnam",filnam,0);
  5074.         }
  5075.         debug(F101,"gnfile nfils","",nfils);
  5076.         if (nfils-- > 0 || filefile) { /* Still some left? */
  5077. #ifndef NOMSEND
  5078.         if (addlist) {
  5079.             if (filenext && filenext->fl_name) {
  5080.             ckstrncpy(filnam,filenext->fl_name,CKMAXPATH+1);
  5081.             cmarg2 =
  5082.               filenext->fl_alias ?
  5083.                 filenext->fl_alias :
  5084.                   "";
  5085.             binary = filenext->fl_mode;
  5086.             } else {
  5087.             printf("?Internal error expanding ADD list\n");
  5088.             return(-5);
  5089.             }
  5090.             filenext = filenext->fl_next;
  5091.             debug(F111,"gnfile addlist filnam",filnam,nfils);
  5092.         } else if (sndsrc > 0 && !filefile) {
  5093. #endif /* NOMSEND */
  5094.             ckstrncpy(filnam,*cmlist++,CKMAXPATH+1);
  5095.             debug(F111,"gnfile cmlist filnam",filnam,nfils);
  5096. #ifndef NOMSEND
  5097.         }
  5098. #endif /* NOMSEND */
  5099.         i = 0;
  5100. #ifndef NOSERVER
  5101.         debug(F101,"gnfile ngetpath","",ngetpath);
  5102. #endif /* NOSERVER */
  5103. nextinpath:
  5104. #ifndef NOSERVER
  5105.         fromgetpath = 0;
  5106.         if (server && !isabsolute(filnam) && (ngetpath > i)) {
  5107.             ckstrncpy(fullname,getpath[i],CKMAXPATH+1);
  5108.             strncat(fullname,filnam,CKMAXPATH);
  5109.             debug(F111,"gnfile getpath",fullname,i);
  5110.             fromgetpath = 1;
  5111.             i++;
  5112.         } else {
  5113.             i = ngetpath + 1;
  5114. #else
  5115.             i = 1;        /* ? */
  5116. #endif /* NOSERVER */
  5117.             ckstrncpy(fullname,filnam,CKMAXPATH+1);
  5118.             debug(F110,"gnfile absolute",fullname,0);
  5119. #ifndef NOSERVER
  5120.         }
  5121. #endif /* NOSERVER */
  5122.         if (iswild(fullname)
  5123. #ifdef RECURSIVE
  5124.             || recursive > 0 || !strcmp(fullname,".")
  5125. #endif /* RECURSIVE */
  5126.             ) {    /* It looks wild... */
  5127.             /* First check if a file with this name exists */
  5128.             debug(F110,"gnfile wild",fullname,0);
  5129.             if (zchki(fullname) > -1) {
  5130.             /*
  5131.                Here we have a file whose name actually
  5132.                contains wildcard characters.
  5133.             */
  5134.             goto gotnam;
  5135.             }
  5136.             nzxopts = 0;
  5137. #ifdef UNIXOROSK
  5138.             if (matchdot) nzxopts |= ZX_MATCHDOT;
  5139. #endif /* UNIXOROSK */
  5140.             if (recursive) nzxopts |= ZX_RECURSE;
  5141.             x = nzxpand(fullname,nzxopts); /* Expand wildcards */
  5142.             debug(F101,"gnfile nzxpand","",x);
  5143.             if (x == 1) {
  5144.             znext(fullname);
  5145.             debug(F110,"gnfile znext",fullname,0);
  5146.             goto gotnam;
  5147.             }
  5148.             if (x == 0) {    /* None match */
  5149. #ifndef NOSERVER
  5150.             if (server && ngetpath > i)
  5151.               goto nextinpath;
  5152. #endif /* NOSERVER */
  5153.             retcode = -1;
  5154.             continue;
  5155.             }
  5156.             if (x < 0) {    /* Too many to expand */
  5157.             return(-5);
  5158.             }
  5159.             sndsrc = -1;    /* Change send-source to znext() */
  5160.         }
  5161.         } else {            /* We're out of files. */
  5162.         debug(F101,"gnfile done","",nfils);
  5163.         *filnam = '\0';
  5164.         return(0);
  5165.         }
  5166.     }
  5167.  
  5168. /* Otherwise, step to next element of internal wildcard expansion list. */
  5169.  
  5170.     if (sndsrc == -1) {
  5171.         while (1) {
  5172.         znext(filnam);
  5173.         if (!filnam[0])
  5174.           break;
  5175.         if (!isdir(filnam))
  5176.           break;
  5177.         }
  5178.         debug(F111,"gnfile znext",filnam,x);
  5179.         if (!filnam[0]) {        /* If no more, */
  5180.         sndsrc = 1;        /* go back to previous list */
  5181.         debug(F101,"gnfile setting sndsrc back","",sndsrc);
  5182.         continue;
  5183.         } else
  5184.           ckstrncpy(fullname,filnam,CKMAXPATH+1);
  5185.     }
  5186.  
  5187. /* Get here with a filename. */
  5188.  
  5189. gotnam:
  5190.     if (fullname[0]) {
  5191. #ifdef DTILDE
  5192.         char * dirp = "";
  5193.         if (fullname[0] == '~') {
  5194.         dirp = tilde_expand((char *)fullname);
  5195.         if (*dirp) ckstrncpy(fullname,dirp,CKMAXPATH+1);
  5196.         }
  5197. #endif /* DTILDE */
  5198.         y = zchki(fullname);    /* Check if file readable */
  5199.         retcode = (int) y;        /* Possible return code */
  5200.         if (y == -1L) {        /* If not found */
  5201. #ifndef NOSERVER
  5202.         if (server && ngetpath > i)
  5203.           goto nextinpath;
  5204. #endif /* NOSERVER */
  5205.         debug(F110,"gnfile skipping:",fullname,0);
  5206.         tlog(F111,fullname,"not sent",0);
  5207.         xxscreen(SCR_ST,ST_SKIP,0l,fullname);
  5208. #ifdef TLOG
  5209.         if (tralog && !tlogfmt)
  5210.           doxlog(what,fullname,fsize,binary,1,"Skipped");
  5211. #endif /* TLOG */
  5212.         continue;
  5213.         } else if (y < 0) {
  5214.         if (y == -3) {        /* Exists but not readable */
  5215.             filrej++;        /* Count this one as not sent */
  5216.             tlog(F110,"Read access denied",fullname,0); /* Log this */
  5217.             xxscreen(SCR_ST,ST_SKIP,0l,fullname); /* Display message */
  5218. #ifdef TLOG
  5219.             if (tralog && !tlogfmt)
  5220.               doxlog(what,fullname,fsize,binary,1,"Skipped");
  5221. #endif /* TLOG */
  5222.         }
  5223.         continue;
  5224.         } else {
  5225.         fsize = y;
  5226.         if (!fileselect(fullname,
  5227.                 sndafter, sndbefore,
  5228.                 sndnafter,sndnbefore,
  5229.                 sndsmaller,sndlarger,
  5230.                 skipbup,
  5231.                 8,sndexcept)) {
  5232.             y = -1L;
  5233.             continue;
  5234.         }
  5235.         ckstrncpy(filnam,fullname,CKMAXPATH+1);
  5236.         return(1);
  5237.         }
  5238.     } else {            /* sndsrc is 0... */
  5239.         if (!fileselect(fullname,
  5240.                 sndafter, sndbefore,
  5241.                 sndnafter,sndnbefore,
  5242.                 sndsmaller,sndlarger,
  5243.                 skipbup,
  5244.                 8,sndexcept)) {
  5245.         y = -1L;
  5246.         continue;
  5247.         }
  5248.         ckstrncpy(filnam,fullname,CKMAXPATH+1);
  5249.         return(1);
  5250.     }
  5251.     }
  5252.     *filnam = '\0';            /* Should never get here */
  5253.     return(0);
  5254. }
  5255.  
  5256. /*
  5257.   The following bunch of routines feed internally generated data to the server
  5258.   to send to the client in response to REMOTE commands like DIRECTORY, DELETE,
  5259.   and so on.  We have to write these lines in the format appropriate to our
  5260.   platform, so they can be converted to generic (CRLF) text format by the
  5261.   packetizer.
  5262. */
  5263. #ifdef UNIX
  5264. char * endline = "\12";
  5265. #else
  5266. #ifdef datageneral
  5267. char * endline = "\12";
  5268. #else
  5269. #ifdef MAC
  5270. char * endline = "\15";
  5271. #else
  5272. #ifdef OSK
  5273. char * endline = "\15";
  5274. #else
  5275. char * endline = "\15\12";
  5276. #endif /* OSK */
  5277. #endif /* MAC */
  5278. #endif /* datageneral */
  5279. #endif /* UNIX */
  5280.  
  5281.  
  5282.  
  5283.  
  5284. #ifdef CKSYMLINK
  5285. #define FNCBUFL (CKMAXPATH + CKMAXPATH + 64)
  5286. #else
  5287. #define FNCBUFL (CKMAXPATH + 64)
  5288. #endif /* CKSYMLINK */
  5289. static CHAR funcbuf[FNCBUFL];
  5290. static int funcnxt = 0;
  5291. static int funclen = 0;
  5292. static int nxpnd = - 1;
  5293. static long ndirs =   0;
  5294. static long nfiles =  0;
  5295. static long nbytes =  0;
  5296.  
  5297. int
  5298. sndstring(p) char * p; {
  5299. #ifndef NOSERVER
  5300.     nfils = 0;                /* No files, no lists. */
  5301.     xflg = 1;                /* Flag we must send X packet. */
  5302.     ckstrncpy(cmdstr,versio,CMDSTRL);    /* Data for X packet. */
  5303.     first = 1;                /* Init getchx lookahead */
  5304.     memstr = 1;                /* Just set the flag. */
  5305.     memptr = p;                /* And the pointer. */
  5306.     binary = XYFT_T;            /* Text mode for this. */
  5307.     return(sinit());
  5308. #else
  5309.     return(0);
  5310. #endif /* NOSERVER */
  5311. }
  5312.  
  5313. /*  S N D H L P  --  Routine to send builtin help  */
  5314.  
  5315. static int srvhlpnum = 0;
  5316.  
  5317. #ifdef IKSD
  5318. static char *nmx[] =  { "Disabled", "Disabled", "Enabled", "Enabled" };
  5319. #endif /* IKSD */
  5320.  
  5321. static char *
  5322. xnm(x) int x; {
  5323. #ifdef IKSD
  5324.     if (inserver)
  5325.       return(nmx[x]);
  5326.     else
  5327. #endif /* IKSD */
  5328.       return(nm[x]);
  5329. }
  5330.  
  5331. static int
  5332. nxthlp(
  5333. #ifdef CK_ANSIC
  5334.        void
  5335. #endif /* CK_ANSIC */
  5336.        ) {
  5337.     int x = 0;
  5338.     extern int
  5339.       en_cpy, en_cwd, en_del, en_dir, en_fin, en_get, en_bye, en_mai,
  5340.       en_pri, en_hos, en_ren, en_sen, en_spa, en_set, en_typ, en_who,
  5341.       en_ret, en_mkd, en_rmd, en_asg, en_que, en_xit, x_login, x_logged,
  5342.       xfinish;
  5343.     extern char * ckxsys;
  5344.  
  5345.     if (funcnxt < funclen)
  5346.       return (funcbuf[funcnxt++]);
  5347.  
  5348.     switch (srvhlpnum++) {
  5349.       case 0:
  5350.         sprintf((char *)funcbuf,
  5351.         "Client Command     Status        Description\n");
  5352.     x = strlen((char *)funcbuf);
  5353.     if (x_login && !x_logged) {
  5354.         sprintf((char *)(funcbuf+x)," REMOTE LOGIN       required\n");
  5355.         x = strlen((char *)funcbuf);
  5356.     }
  5357.     sprintf((char *)(funcbuf+x)," GET                %-14s%s\n",
  5358.         xnm(en_get),
  5359.         "Transfer file(s) from server to client."
  5360.         );
  5361.     break;
  5362.  
  5363.       case 1:
  5364.     sprintf((char *)funcbuf," SEND               %-14s%s\n",
  5365.         xnm(en_sen),
  5366.         "Transfer file(s) from client to server."
  5367.         );
  5368.     break;
  5369.  
  5370.       case 2:
  5371.     sprintf((char *)funcbuf," MAIL               %-14s%s\n",
  5372.         xnm(inserver ? 0 : en_mai),
  5373.         "Send file(s) as e-mail."
  5374.         );
  5375.     break;
  5376.  
  5377.       case 3:
  5378. #ifndef NOSPL
  5379.     sprintf((char *)funcbuf," REMOTE ASSIGN      %-14s%s\n",
  5380.         xnm(en_asg),
  5381.         "Assign value to server variable or macro."
  5382.         );
  5383. #else
  5384.     sprintf((char *)funcbuf," REMOTE ASSIGN      not configured\n");
  5385. #endif /* NOSPL */
  5386.  
  5387.     break;
  5388.       case 4:
  5389.     sprintf((char *)funcbuf," REMOTE CD          %-14s%s\n",
  5390.         xnm(en_cwd),
  5391.         "Change server's directory."
  5392.         );
  5393.     break;
  5394.  
  5395.       case 5:
  5396. #ifdef ZCOPY
  5397.     sprintf((char *)funcbuf," REMOTE COPY        %-14s%s\n",
  5398.         xnm(en_cpy),
  5399.         "Copy a file on the server."
  5400.         );
  5401. #else
  5402.     sprintf((char *)funcbuf," REMOTE COPY        not configured\n");
  5403. #endif /* ZCOPY */
  5404.  
  5405.     break;
  5406.       case 6:
  5407.     sprintf((char *)funcbuf," REMOTE DELETE      %-14s%s\n",
  5408.         xnm(en_del),
  5409.         "Delete a file on the server."
  5410.         );
  5411.     break;
  5412.  
  5413.       case 7:
  5414.     sprintf((char *)funcbuf," REMOTE DIRECTORY   %-14s%s\n",
  5415.         xnm(en_dir),
  5416.         "List files on the server."
  5417.         );
  5418.     break;
  5419.  
  5420.       case 8:
  5421.     sprintf((char *)funcbuf," REMOTE EXIT        %-14s%s\n",
  5422.         xnm(en_xit),
  5423.         "Exit from Kermit server program."
  5424.         );
  5425.     break;
  5426.  
  5427.       case 9:
  5428.     sprintf((char *)funcbuf," REMOTE HOST        %-14s%s\n",
  5429.         xnm(inserver ? 0 : en_hos),
  5430. #ifdef datageneral
  5431.         "Execute a CLI command on the server."
  5432. #else
  5433. #ifdef VMS
  5434.         "Execute a DCL command on the server."
  5435. #else
  5436.         "Execute a shell command on the server."
  5437. #endif /* VMS */
  5438. #endif /* datageneral */
  5439.         );
  5440.     break;
  5441.  
  5442.       case 10:
  5443.     sprintf((char *)funcbuf," REMOTE PRINT       %-14s%s\n",
  5444.         xnm(inserver ? 0 : en_pri),
  5445.         "Send a file to the server for printing."
  5446.         );
  5447.     break;
  5448.  
  5449.       case 11:
  5450. #ifndef NOSPL
  5451.     sprintf((char *)funcbuf," REMOTE QUERY       %-14s%s\n",
  5452.         xnm(en_que),
  5453.         "Get value of server variable or macro."
  5454.         );
  5455.  
  5456. #else
  5457.     sprintf((char *)funcbuf," REMOTE QUERY       not configured\n");
  5458. #endif /* NOSPL */
  5459.  
  5460.     break;
  5461.       case 12:
  5462.     sprintf((char *)funcbuf," REMOTE MKDIR       %-14s%s\n",
  5463.         xnm(en_mkd),
  5464.         "Create a directory on the server."
  5465.         );
  5466.     break;
  5467.  
  5468.       case 13:
  5469.     sprintf((char *)funcbuf," REMOTE RMDIR       %-14s%s\n",
  5470.         xnm(en_rmd),
  5471.         "Remove a directory on the server."
  5472.         );
  5473.     break;
  5474.  
  5475.       case 14:
  5476.     sprintf((char *)funcbuf," REMOTE RENAME      %-14s%s\n",
  5477.         xnm(en_ren),
  5478.         "Rename a file on the server."
  5479.         );
  5480.     break;
  5481.  
  5482.       case 15:
  5483.     sprintf((char *)funcbuf," REMOTE SET         %-14s%s\n",
  5484.         xnm(en_set),
  5485.         "Set a parameter on the server"
  5486.         );
  5487.     break;
  5488.  
  5489.       case 16:
  5490.     sprintf((char *)funcbuf," REMOTE SPACE       %-14s%s\n",
  5491.         xnm(en_spa),
  5492.         "Inquire about disk space on the server."
  5493.         );
  5494.     break;
  5495.  
  5496.       case 17:
  5497.     sprintf((char *)funcbuf," REMOTE TYPE        %-14s%s\n",
  5498.         xnm(en_typ),
  5499.         "Display a server file on your screen."
  5500.         );
  5501.     break;
  5502.  
  5503.       case 18:
  5504.     sprintf((char *)funcbuf," REMOTE WHO         %-14s%s\n",
  5505.         xnm(inserver ? 0 : en_who),
  5506.         "List who is logged in to the server."
  5507.         );
  5508.     break;
  5509.  
  5510.       case 19:
  5511.     sprintf((char *)funcbuf," FINISH             %-14s%s\n",
  5512.         xnm(en_fin),
  5513.         xfinish ?
  5514.         "Exit from Kermit server program." :
  5515.         "Return the server to its command prompt."
  5516.         );
  5517.     break;
  5518.  
  5519.       case 20:
  5520.     sprintf((char *)funcbuf," BYE                %-14s%s\n\n",
  5521.         xnm(en_bye),
  5522.         "Log the server out and disconnect."
  5523.         );
  5524.     break;
  5525.  
  5526.       default:
  5527.     return(-1);
  5528.     }
  5529.     funcnxt = 0;
  5530.     funclen = strlen((char *)funcbuf);
  5531.     return(funcbuf[funcnxt++]);
  5532. }
  5533.  
  5534. int
  5535. sndhlp() {
  5536. #ifndef NOSERVER
  5537.     extern char * ckxsys;
  5538.  
  5539.     first = 1;                /* Init getchx lookahead */
  5540.     nfils = 0;                /* No files, no lists. */
  5541.     xflg = 1;                /* Flag we must send X packet. */
  5542.     strcpy(cmdstr,"REMOTE HELP");    /* Data for X packet. */
  5543.     sprintf((char *)funcbuf, "C-Kermit %s,%s\n\n", versio, ckxsys);
  5544.     funclen = strlen((char *)funcbuf);
  5545. #ifdef IKSD
  5546.     if (inserver) {
  5547.     sprintf((char *)(funcbuf+funclen),
  5548.         "Internet Kermit Service (EXPERIMENTAL)\n\n");
  5549.     funclen = strlen((char *)funcbuf);
  5550.     }
  5551. #endif /* IKSD */
  5552.     funcnxt = 0;
  5553.     funcptr = nxthlp;
  5554.     funcstr = 1;
  5555.     srvhlpnum = 0;
  5556.     binary = XYFT_T;            /* Text mode for this. */
  5557.     return(sinit());
  5558. #else
  5559.     return(0);
  5560. #endif /* NOSERVER */
  5561. }
  5562.  
  5563. /*
  5564.    Returns the next available character,
  5565.   -1 if no more data.
  5566. */
  5567. static int
  5568. nxttype(
  5569. #ifdef CK_ANSIC
  5570.        void
  5571. #endif /* CK_ANSIC */
  5572.     ) {
  5573.     int c;
  5574.     if (zchin(ZIFILE,&c) < 0) {
  5575.         zclose(ZIFILE);
  5576.         return(-1);
  5577.     } else {
  5578.     return((unsigned)c);
  5579.     }
  5580. }
  5581.  
  5582. /*  S N D T Y P -- TYPE a file to remote client */
  5583.  
  5584. int
  5585. sndtype(file) char * file; {
  5586. #ifndef NOSERVER
  5587.     char * p = NULL, name[CKMAXPATH+1];
  5588. #ifdef OS2
  5589.     if (*file) {
  5590.         ckstrncpy(name, file, CKMAXPATH+1);
  5591.         /* change / to \. */
  5592.         p = name;
  5593.         while (*p) {            /* Change them back to \ */
  5594.             if (*p == '/') *p = '\\';
  5595.             p++;
  5596.         }
  5597.     } else
  5598.       return(0);
  5599. #else
  5600.     ckstrncpy(name, file, CKMAXPATH+1);
  5601. #endif /* OS2 */
  5602.  
  5603.     funcnxt = 0;
  5604.     funclen = strlen((char *)funcbuf);
  5605.     if (zchki(name) == -2) {
  5606.         /* Found a directory */
  5607.         return(0);
  5608.     }
  5609.     if (!zopeni(ZIFILE,name))
  5610.       return(0);
  5611.  
  5612.     nfils = 0;                /* No files, no lists. */
  5613.     xflg = 1;                /* Flag we must send X packet. */
  5614.     strcpy(cmdstr,"type");        /* Data for X packet. */
  5615.     first = 1;                /* Init getchx lookahead */
  5616.     funcstr = 1;            /* Just set the flag. */
  5617.     funcptr = nxttype;            /* And the pointer. */
  5618.     binary = XYFT_T;            /* Text mode for this */
  5619.     return(sinit());
  5620. #else
  5621.     return(0);
  5622. #endif /* NOSERVER */
  5623. }
  5624.  
  5625. /*
  5626.    N X T D I R  --  Provide data for senddir()
  5627.  
  5628.    Returns the next available character or -1 if no more data.
  5629. */
  5630. #ifndef NOICP
  5631. /* Directory listing parameters set by the user interface, if any. */
  5632. extern int dir_head, dir_dots, dir_back;
  5633. #endif /* NOICP */
  5634. static int sd_hdg, sd_bkp, sd_dot;    /* Local listing parameters */
  5635.  
  5636. static int
  5637. nxtdir(
  5638. #ifdef CK_ANSIC
  5639.        void
  5640. #endif /* CK_ANSIC */
  5641.        ) {
  5642.     char name[CKMAXPATH+1], dbuf[24], *p = NULL;
  5643.     char *dstr = NULL, * lnk = "";
  5644.     CHAR c, * linebuf = funcbuf;
  5645. #ifdef VMSORUNIX
  5646.     extern int zgfs_dir;
  5647. #ifdef CKSYMLINK
  5648.     extern int zgfs_link;
  5649. #endif /* CKSYMLINK */
  5650. #endif /* VMSUNIX */
  5651. #ifdef OSK
  5652.     /* Work around bugs in OSK compiler */
  5653.     char *dirtag = "directories";
  5654.     char *filetag = "files";
  5655.     char *bytetag = "bytes";
  5656. #endif /* OSK */
  5657.     long len = 0;
  5658.     int x, itsadir = 0, gotone = 0;
  5659.  
  5660. #ifdef DEBUG
  5661.     if (deblog) {
  5662.     debug(F101,"nxtdir funcnxt","",funcnxt);
  5663.     debug(F101,"nxtdir funclen","",funclen);
  5664.     debug(F110,"nxtdir funcbuf",funcbuf+funcnxt,0);
  5665.     }
  5666. #endif /* DEBUG */
  5667.     if (funcnxt < funclen) {        /* Return next character from buffer */
  5668.     c = funcbuf[funcnxt++];
  5669.     debug(F000,"nxtdir return 1","",(unsigned)(c & 0xff));
  5670.     return((unsigned)(c & 0xff));
  5671.     }
  5672.     while (nxpnd > 0) {            /* Buffer needs refill */
  5673.         nxpnd--;
  5674.     znext(name);            /* Get next filename */
  5675.         if (!name[0]) {            /* None left - done */
  5676.             nxpnd = 0;
  5677.             return(nxtdir());
  5678.         }
  5679.     if (sd_bkp) {            /* Showing backup files? */
  5680.         gotone = 1;            /* Yes, no need to check. */
  5681.         break;
  5682.     }
  5683.     x = ckmatch(            /* No - see if this is one */
  5684. #ifdef CKREGEX
  5685.             "*.~[0-9]*~"    /* Not perfect but close enough. */
  5686. #else
  5687.             "*.~*~"        /* Less close. */
  5688. #endif /* CKREGEX */
  5689.             ,name,filecase,1);
  5690.     debug(F111,"nxtdir ckmatch",name,x);
  5691.     if (x) {
  5692.         continue;            /* It's a backup file - skip it */
  5693.     } else {
  5694.         gotone = 1;            /* It's not, break from loop. */
  5695.         break;
  5696.     }
  5697.     }
  5698.     if (gotone) {
  5699.         len = zgetfs(name);        /* Get file size */
  5700.     debug(F111,"nxtdir zgetfs",name,len);
  5701. #ifdef VMSORUNIX
  5702.     itsadir = zgfs_dir;        /* See if it's a directory */
  5703. #else
  5704.     itsadir = (len == -2 || isdir(name));
  5705. #endif /* VMSORUNIX */
  5706.         dstr = zfcdat(name);
  5707.     debug(F111,"nxtdir zcfdat",dstr,0);
  5708.     if (!dstr)
  5709.       dstr = "";
  5710.     if (!*dstr) {
  5711.         dstr = "0000-00-00 00:00:00";
  5712.     } else {
  5713.         dbuf[0] = dstr[0];
  5714.         dbuf[1] = dstr[1];
  5715.         dbuf[2] = dstr[2];
  5716.         dbuf[3] = dstr[3];
  5717.         dbuf[4] = '-';
  5718.         dbuf[5] = dstr[4];
  5719.         dbuf[6] = dstr[5];
  5720.         dbuf[7] = '-';
  5721.         dbuf[8] = dstr[6];
  5722.         dbuf[9] = dstr[7];
  5723.         strcpy(dbuf+10,dstr+8);
  5724.         dstr = dbuf;
  5725.     }
  5726. #ifdef CK_PERMS
  5727. #ifdef VMSORUNIX
  5728.     p = ziperm(name);        /* Get permissions */
  5729. #else
  5730.     p = zgperm(name);
  5731. #endif /* VMSORUNIX */
  5732. #else
  5733.     p = NULL;
  5734. #endif /* CK_PERMS */
  5735.     debug(F110,"domydir perms",p,0);
  5736.  
  5737. #ifdef VMS
  5738.     /* Make name relative */
  5739.     ckstrncpy(name,zrelname(name,zgtdir()),CKMAXPATH+1);
  5740. #endif /* VMS */
  5741.  
  5742.     if (itsadir) {
  5743.         ndirs++;
  5744.     } else {
  5745.         nfiles++;
  5746.         nbytes += len;
  5747.     }
  5748.     lnk = "";
  5749. #ifdef UNIX
  5750. #ifdef CKSYMLINK
  5751.     if (zgfs_link) {
  5752.         extern char linkname[];
  5753.         lnk = linkname;
  5754.     }
  5755.     debug(F111,"nxtdir linkname",lnk,zgfs_link);
  5756. #endif /* CKSYMLINK */
  5757. #endif /* UNIX */
  5758.  
  5759.     if (itsadir && len < 0) {    /* Directory */
  5760. #ifdef VMS
  5761.         sprintf((char *)linebuf,
  5762.             "%-22s%-10s  %s  %s\n",p,"<DIR>",dstr,name);
  5763. #else
  5764.         if (p)
  5765.           sprintf((char *)linebuf,
  5766.               "%10s%-10s  %s  %s\n",p,"<DIR>",dstr,name);
  5767.         else
  5768.           sprintf((char *)linebuf,
  5769.               "%-10s  %s  %s\n", "<DIR>", dstr, name);
  5770. #endif /* VMS */
  5771.     } else {            /* Regular file */
  5772. #ifdef VMS
  5773.         sprintf((char *)linebuf,
  5774.             "%-22s%10ld  %s  %s\n", p, len, dstr, name);
  5775. #else
  5776.         if (p)
  5777.           sprintf((char *)linebuf,
  5778.               "%10s%10ld  %s  %s%s%s\n",
  5779.               p, len, dstr, name,
  5780.               *lnk ? " -> " : "",
  5781.               lnk
  5782.               );
  5783.         else
  5784.           sprintf((char *)linebuf,
  5785.               "%10ld  %s  %s%s%s\n",
  5786.               len, dstr, name,
  5787.               *lnk ? " -> " : "",
  5788.               lnk
  5789.               );
  5790. #endif /* VMS */
  5791.     }
  5792.         funcnxt = 0;
  5793.         funclen = strlen((char *)funcbuf);
  5794.     } else if (sd_hdg && nxpnd == 0) {    /* Done, send summary */
  5795.     char *blankline = "";        /* At beginning of summary */
  5796. /*
  5797.   The idea is to prevent (a) unnecessary multiple blanklines, and (b)
  5798.   prompt-stomping.  Preventing (b) is practically impossible, because it
  5799.   depends on the client so for now always include that final CRLF.
  5800. */
  5801.     if (!ndirs || !nbytes || !nfiles)
  5802.       blankline = endline;
  5803. #ifdef OSK
  5804. /* Workaround bugs in OS-9 compiler... */
  5805.         if (ndirs == 1)
  5806.            dirtag = "directory";
  5807.         if (nfiles == 1)
  5808.            filetag = "file";
  5809.         if (nbytes == 1)
  5810.            bytetag = "byte";
  5811.         sprintf((char *)funcbuf,
  5812.            "%sSummary: %ld %s, %ld %s, %ld %s%s",
  5813.            blankline,
  5814.            ndirs,
  5815.            dirtag,
  5816.            nfiles,
  5817.            filetag,
  5818.            nbytes,
  5819.            bytetag,
  5820.            endline);
  5821. #else
  5822.         sprintf((char *)funcbuf,
  5823.         "%sSummary: %ld director%s, %ld file%s, %ld byte%s%s",
  5824.         blankline,
  5825.         ndirs,
  5826.         (ndirs == 1) ? "y" : "ies",
  5827.         nfiles,
  5828.         (nfiles == 1) ? "" : "s",
  5829.         nbytes,
  5830.         (nbytes == 1) ? "" : "s",
  5831.         endline
  5832.         );
  5833. #endif /* OSK */
  5834.         nxpnd--;
  5835.         funcnxt = 0;
  5836.         funclen = strlen((char *)funcbuf);
  5837.     } else {
  5838.         funcbuf[0] = '\0';
  5839.         funcnxt = 0;
  5840.         funclen = 0;
  5841.     }
  5842.     debug(F101,"nxtdir funclen","",funclen);
  5843.  
  5844.     if (funcnxt < funclen) {        /* If we have data to send... */
  5845.     c = funcbuf[funcnxt++];
  5846.     debug(F000,"nxtdir return 2","",(unsigned)(c & 0xff));
  5847.     return((unsigned)(c & 0xff));
  5848.     } else
  5849.       return(-1);            /* Nothing left, done. */
  5850. }
  5851.  
  5852. /*  S N D D I R -- send directory listing  */
  5853.  
  5854. int
  5855. snddir(spec) char * spec; {
  5856. #ifndef NOSERVER
  5857.     char * p = NULL, name[CKMAXPATH+1];
  5858.     int t = 0, rc = 0;
  5859.     char fnbuf[CKMAXPATH+1];
  5860.  
  5861. #ifndef NOICP
  5862.     sd_hdg = dir_head > 0;        /* Import listing parameters if any */
  5863.     sd_bkp = dir_back > 0;
  5864.     sd_dot = dir_dots > 0;
  5865. #else
  5866.     sd_hdg = 1;                /* Or use hardwired defaults */
  5867.     sd_bkp = 1;
  5868.     sd_dot = 0;
  5869. #endif /* NOICP */
  5870.  
  5871.     if (!spec) spec = "";
  5872.     debug(F110,"snddir",spec,0);
  5873.     if (*spec) {
  5874. #ifdef COMMENT
  5875.     zfnqfp(spec,CKMAXPATH,name);
  5876.     debug(F110,"snddir zfnqfp",name,0);
  5877. #else
  5878.     ckstrncpy(name,spec,CKMAXPATH+1);
  5879.     debug(F110,"snddir name",name,0);
  5880. #endif /* COMMENT */
  5881.     } else {
  5882. #ifdef OS2
  5883.     strcpy(name, "*");
  5884. #else
  5885. #ifdef UNIXOROSK
  5886.     strcpy(name, "./*");
  5887. #else
  5888. #ifdef VMS
  5889.     strcpy(name, "*.*");
  5890. #else
  5891. #ifdef datageneral
  5892.     strcpy(name, "+");
  5893. #else
  5894.     debug(F101,"snddir quit (no filespec)","",0);
  5895.     return(0);
  5896. #endif /* datageneral */
  5897. #endif /* VMS */
  5898. #endif /* UNIX */
  5899. #endif /* OS2 */
  5900.     }
  5901.     debug(F110,"snddir name 1",name,0);
  5902.     ndirs = 0L;
  5903.     nfiles = 0L;
  5904.     nbytes = 0L;
  5905.  
  5906.     if (zfnqfp(name,CKMAXPATH,fnbuf))
  5907.  
  5908.     debug(F110,"snddir name 2",name,0);
  5909.     p = name + strlen(name);        /* Move it to end of list */
  5910.  
  5911.     if (sd_hdg) {
  5912.     sprintf((char *)funcbuf,"Listing files: %s%s%s",fnbuf,endline,endline);
  5913.     funcnxt = 0;
  5914.     funclen = strlen((char *)funcbuf);
  5915.     }
  5916.     diractive = 1;
  5917.  
  5918. #ifdef OS2
  5919.     if (zchki(name) == -2) {        /* Found a directory */
  5920.         p--;
  5921.         if (*p == '\\' || *p == '/')
  5922.           strcat(name, "*");
  5923.         else if (*p == ':')
  5924.           strcat(name, ".");
  5925.         else
  5926.           strcat(name, "\\*");
  5927.     debug(F110,"snddir directory",name,0);
  5928.     }
  5929. #else
  5930.     if (!iswild(name) && isdir(name)) {
  5931.     char * s = name;
  5932.     p--;
  5933. #ifdef UNIXOROSK
  5934.     if (*p == '/')            /* So append wildcard to it */
  5935.       strcat(s, "*");
  5936.     else
  5937.       strcat(s, "/*");
  5938. #else
  5939. #ifdef VMS
  5940.     if (*p == ']' || *p == '>' || *p == ':')
  5941.       strcat(s, "*.*");
  5942. #else
  5943. #ifdef datageneral
  5944.     if (*p == ':')
  5945.       strcat(s, "+");
  5946.     else
  5947.       strcat(s, ":+");
  5948. #else
  5949. #ifdef VOS
  5950.     if (*p == '>')
  5951.       strcat(s, "*");
  5952.     else
  5953.       strcat(s, ">*");
  5954. #endif /* VOS */
  5955. #endif /* datageneral */
  5956. #endif /* VMS */
  5957. #endif /* UNIXOROSK */
  5958.     debug(F110,"snddir directory",name,0);
  5959.     }
  5960. #endif /* OS2 */
  5961.  
  5962.     nzxopts = 0;
  5963. #ifdef UNIX
  5964.     {
  5965.     extern char ** mtchs;
  5966.     if (sd_dot) nzxopts |= ZX_MATCHDOT;
  5967.     if (recursive) nzxopts |= ZX_RECURSE;
  5968.     nxpnd = nzxpand(name,nzxopts);    /* Get the array of names */
  5969.     sh_sort(mtchs,NULL,nxpnd,0,0,1); /* Sort the array */
  5970.     }
  5971. #else
  5972.     if (recursive) nzxopts |= ZX_RECURSE;
  5973.     nxpnd = nzxpand(name,nzxopts);
  5974. #endif /* UNIX */
  5975.  
  5976.     debug(F101,"snddir nzxpand nxpnd","",nxpnd);
  5977.     if (nxpnd < 1)
  5978.       return(-1);
  5979.     nfils = 0;                /* No files, no lists. */
  5980.     xflg = 1;                /* Flag we must send X packet. */
  5981.     if ((int)strlen(name) < CMDSTRL - 11) /* Data for X packet. */
  5982.       sprintf(cmdstr,"DIRECTORY %s",name);
  5983.     else
  5984.       strcpy(cmdstr,"DIRECTORY");
  5985.     first = 1;                /* Init getchx lookahead */
  5986.     funcstr = 1;            /* Just set the flag. */
  5987.     funcptr = nxtdir;            /* And the pointer. */
  5988.     rc = sinit();
  5989.     debug(F111,"snddir","sinit()",rc);
  5990.     return(rc);
  5991. #else
  5992.     return(0);
  5993. #endif /* NOSERVER */
  5994. }
  5995.  
  5996. /*  N X T D E L -- provide data for delete   */
  5997.  
  5998. /*  Returns the next available character or -1 if no more data  */
  5999.  
  6000. static int
  6001. nxtdel(
  6002. #ifdef CK_ANSIC
  6003.        void
  6004. #endif /* CK_ANSIC */
  6005.        ) {
  6006.     char name[257], *p = NULL;
  6007.     int len = 0;
  6008.  
  6009.     if (funcnxt < funclen)
  6010.       return ((unsigned)funcbuf[funcnxt++]);
  6011.  
  6012.     if (nxpnd > 0) {
  6013.         nxpnd--;
  6014.     znext(name);
  6015.         if (!name[0]) {
  6016.             nxpnd = 0;
  6017.             return(nxtdel());
  6018.         }
  6019.         len = zchki(name);
  6020.  
  6021.         /* Find just the name of the file */
  6022.  
  6023.         for (p = name + strlen(name); p != name && *p != '/' ; p--) ;
  6024.         if (*p == '/') p++;
  6025.  
  6026.         if (len > -1L) {
  6027.         if (zdelet(name)) {
  6028.         sprintf((char *)funcbuf," %10s: %s%s","skipping",p,endline);
  6029.         } else {
  6030.         nfiles++;
  6031.         nbytes += len;
  6032.         sprintf((char *)funcbuf," %10s: %s%s","deleted",p,endline);
  6033.         }
  6034.         } else
  6035.       sprintf((char *)funcbuf," directory: %s%s", p, endline);
  6036.         funcnxt = 0;
  6037.         funclen = strlen((char *)funcbuf);
  6038.     } else
  6039.  
  6040.     /* If done processing the expanded entries send a summary statement */
  6041.  
  6042.       if (nxpnd == 0) {
  6043.       sprintf((char *)funcbuf,
  6044.           "%s%ld file%s deleted, %ld byte%s freed%s",
  6045.           endline,
  6046.           nfiles,
  6047.           (nfiles == 1) ? "" : "s",
  6048.           nbytes,
  6049.           (nbytes == 1) ? "" : "s",
  6050.           endline
  6051.           );
  6052.       nxpnd--;
  6053.       funcnxt = 0;
  6054.       funclen = strlen((char *)funcbuf);
  6055.       } else {
  6056.       funcbuf[0] = '\0';
  6057.       funcnxt = 0;
  6058.       funclen = 0;
  6059.       }
  6060.  
  6061.     /* If we have data to send */
  6062.  
  6063.     if (funcnxt < funclen)
  6064.       return ((unsigned)funcbuf[funcnxt++]); /* Return a character */
  6065.     else
  6066.       return(-1);            /* No more input */
  6067. }
  6068.  
  6069. /*  S N D D E L  --  Send delete message  */
  6070.  
  6071. int
  6072. snddel(spec) char * spec; {
  6073. #ifndef NOSERVER
  6074.     char * p = NULL, name[CKMAXPATH+1];
  6075.  
  6076.     if (!*spec)
  6077.       return(0);
  6078.  
  6079.     ckstrncpy(name, spec, CKMAXPATH+1);
  6080.  
  6081. #ifdef OS2
  6082.     /* change / to \. */
  6083.     p = name;
  6084.     while (*p) {            /* Change them back to \ */
  6085.         if (*p == '/') *p = '\\';
  6086.         p++;
  6087.     }
  6088. #endif /* OS2 */
  6089.  
  6090.     nfiles = nbytes = 0L;
  6091.     sprintf((char *)funcbuf,"Deleting \"%s\"%s",name,endline);
  6092.     funcnxt = 0;
  6093.     funclen = strlen((char *)funcbuf);
  6094.  
  6095.     nzxopts = ZX_FILONLY;        /* Files only */
  6096. #ifdef UNIXOROSK
  6097.     if (matchdot) nzxopts |= ZX_MATCHDOT;
  6098. #endif /* UNIXOROSK */
  6099. #ifdef COMMENT
  6100.     /* Recursive deleting not supported yet */
  6101.     if (recursive) nzxopts |= ZX_RECURSE;
  6102. #endif /* COMMENT */
  6103.     nxpnd = nzxpand(name,nzxopts);
  6104.     if (nxpnd < 1)
  6105.       return(-1);
  6106.     nfils = 0;                /* No files, no lists. */
  6107.     xflg = 1;                /* Flag we must send X packet. */
  6108.     strcpy(cmdstr,"REMOTE DELETE");    /* Data for X packet. */
  6109.     first = 1;                /* Init getchx lookahead */
  6110.     funcstr = 1;            /* Just set the flag. */
  6111.     funcptr = nxtdel;            /* And the pointer. */
  6112.     binary = XYFT_T;            /* Use text mode for this, */
  6113.     return(sinit());
  6114. #else
  6115.     return(0);
  6116. #endif /* NOSERVER */
  6117. }
  6118.  
  6119. #ifdef OS2
  6120. /*  S N D S P A C E -- send disk space message  */
  6121. int
  6122. sndspace(drive) int drive; {
  6123. #ifndef NOSERVER
  6124.     static char spctext[64];
  6125.     if (drive)
  6126.       sprintf(spctext,
  6127.           " Drive %c: %ldK free%s",
  6128.           drive,
  6129.           zdskspace(drive - 'A' + 1) / 1024L,
  6130.           endline
  6131.           );
  6132.     else
  6133.       sprintf(spctext, " Free space: %ldK%s", zdskspace(0) / 1024L, endline);
  6134.     nfils = 0;                /* No files, no lists. */
  6135.     xflg = 1;                /* Flag we must send X packet. */
  6136.     strcpy(cmdstr,"free space");    /* Data for X packet. */
  6137.     first = 1;                /* Init getchx lookahead */
  6138.     memstr = 1;                /* Just set the flag. */
  6139.     memptr = spctext;            /* And the pointer. */
  6140.     binary = XYFT_T;            /* Text mode for this. */
  6141.     return(sinit());
  6142. #else
  6143.     return(0);
  6144. #endif /* NOSERVER */
  6145. }
  6146.  
  6147. /*  S N D W H O -- send who message  */
  6148. int
  6149. sndwho(who) char * who; {
  6150. #ifndef NOSERVER
  6151.     nfils = 0;                /* No files, no lists. */
  6152.     xflg = 1;                /* Flag we must send X packet. */
  6153.     strcpy(cmdstr,"who");        /* Data for X packet. */
  6154.     first = 1;                /* Init getchx lookahead */
  6155.     memstr = 1;                /* Just set the flag. */
  6156. #ifdef NT
  6157.     memptr = "\15\12K95 SERVER\15\12";    /* And the pointer. */
  6158. #else
  6159.     memptr = "\15\12K/2 SERVER\15\12";
  6160. #endif /* NT */
  6161.     binary = XYFT_T;            /* Use text mode */
  6162.     return(sinit());
  6163. #else
  6164.     return(0);
  6165. #endif /* NOSERVER */
  6166. }
  6167. #endif /* OS2 */
  6168.  
  6169. /*  C W D  --  Change server's working directory  */
  6170.  
  6171. /*
  6172.  String passed has first byte as length of directory name, rest of string
  6173.  is name.  Returns:
  6174.   0 on failure.
  6175.   1 on success after sending short-form response (ACK with name).
  6176.   2 on success if a CD Message file is to be sent.
  6177. */
  6178. int
  6179. cwd(vdir) char *vdir; {
  6180.     char *cdd, *dirp;
  6181.  
  6182.     vdir[xunchar(*vdir) + 1] = '\0';    /* Terminate string with a null */
  6183.     dirp = vdir+1;
  6184.     tlog(F110,"Directory requested: ",dirp,0L);
  6185.     if (zchdir(dirp)) {            /* Try to change */
  6186.     cdd = zgtdir();            /* Get new working directory. */
  6187.     debug(F110,"cwd",cdd,0);
  6188.     if (srvcdmsg) {            /* Send orientation file? */
  6189.         int i;
  6190.         for (i = 0; i < 8; i++) {
  6191.         if (zchki(cdmsgfile[i]) > -1) {
  6192.             xxscreen(SCR_CD,0,0l,cdd);
  6193.             tlog(F110,"Changed directory to",cdd,0L);
  6194.             return(2);
  6195.         }
  6196.         }
  6197.     }
  6198.     encstr((CHAR *)cdd);        /* Send short-form reply */
  6199.     ack1(data);            /* containing directory name. */
  6200.     xxscreen(SCR_CD,0,0l,cdd);
  6201.     tlog(F110,"Changed directory to",cdd,0L);
  6202.     return(1);
  6203.     } else {
  6204.     debug(F110,"cwd failed",dirp,0);
  6205.     tlog(F110,"Failed to change directory to",dirp,0L);
  6206.     return(0);
  6207.     }
  6208. }
  6209.  
  6210.  
  6211. /*  S Y S C M D  --  Do a system command  */
  6212.  
  6213. /*  Command string is formed by concatenating the two arguments.  */
  6214.  
  6215. int
  6216. syscmd(prefix,suffix) char *prefix, *suffix; {
  6217.     extern int i_isopen;
  6218. #ifndef NOPUSH
  6219.     char *cp;
  6220.  
  6221.     i_isopen = 0;
  6222.     if (!prefix)
  6223.       return(0);
  6224.     if (!*prefix)
  6225.       return(0);
  6226.     for (cp = cmdstr; *prefix != '\0'; *cp++ = *prefix++);
  6227.     while (*cp++ = *suffix++)
  6228. #ifdef OS2
  6229.         /* This takes away more than we gain in convenience
  6230.         if (*(cp-1) == '/') *(cp-1) = '\\' */
  6231. #endif /* OS2 */
  6232.       ;                    /* Copy suffix */
  6233.  
  6234.     debug(F110,"syscmd",cmdstr,0);
  6235.  
  6236.     if (zxcmd(ZIFILE,cmdstr) > 0) {
  6237.         debug(F110,"syscmd zxcmd ok",cmdstr,0);
  6238.     nfils = sndsrc = 0;        /* Flag that input is from stdin */
  6239.         xflg = hcflg = 1;        /* And special flags for pipe */
  6240.     binary = XYFT_T;        /* Go to text mode */
  6241.     i_isopen = 1;
  6242.         return (sinit());        /* Send S packet */
  6243.     } else {
  6244.         debug(F100,"syscmd zxcmd failed",cmdstr,0);
  6245.     i_isopen = 0;
  6246.         return(0);
  6247.     }
  6248. #else
  6249.     debug(F100,"syscmd zxcmd NOPUSH",cmdstr,0);
  6250.     i_isopen = 0;
  6251.     return(0);
  6252. #endif /* NOPUSH */
  6253. }
  6254.  
  6255. /*  R E M S E T  --  Remote Set  */
  6256. /*  Called by server to set variables as commanded in REMOTE SET packets.  */
  6257. /*  Returns 1 on success, 0 on failure.  */
  6258.  
  6259. int
  6260. remset(s) char *s; {
  6261.     extern int c_save, en_del;
  6262.     int len, i, x, y;
  6263.     char *p;
  6264.  
  6265.     len = xunchar(*s++);        /* Length of first field */
  6266.     p = s + len;            /* Pointer to second length field */
  6267.     *p++ = '\0';            /* Zero out second length field */
  6268.     x = atoi(s);            /* Value of first field */
  6269.     debug(F111,"remset",s,x);
  6270.     debug(F110,"remset",p,0);
  6271.     switch (x) {            /* Do the right thing */
  6272.       case 132:                /* Attributes (all, in) */
  6273.     atcapr = atoi(p);
  6274.     return(1);
  6275.       case 133:                /* File length attributes */
  6276.       case 233:                /* IN/OUT combined */
  6277.       case 148:                /* Both kinds of lengths */
  6278.       case 248:
  6279.     atleni = atleno = atoi(p);
  6280.     return(1);
  6281.       case 134:                /* File Type (text/binary) */
  6282.       case 234:
  6283.     attypi = attypo = atoi(p);
  6284.     return(1);
  6285.       case 135:                /* File creation date */
  6286.       case 235:
  6287.     atdati = atdato = atoi(p);
  6288.     return(1);
  6289.       case 139:                /* File Blocksize */
  6290.       case 239:
  6291.     atblki = atblko = atoi(p);
  6292.     return(1);
  6293.       case 141:                /* Encoding / Character Set */
  6294.       case 241:
  6295.     atenci = atenco = atoi(p);
  6296.     return(1);
  6297.       case 142:                /* Disposition */
  6298.       case 242:
  6299.     atdisi = atdiso = atoi(p);
  6300.     return(1);
  6301.       case 145:                /* System ID */
  6302.       case 245:
  6303.     atsidi = atsido = atoi(p);
  6304.     return(1);
  6305.       case 147:                /* System-Dependent Info */
  6306.       case 247:
  6307.     atsysi = atsyso = atoi(p);
  6308.     return(1);
  6309.       case 232:                /* Attributes (all, out) */
  6310.     atcapr = atoi(p);
  6311.     return(1);
  6312.       case 300:                /* File type (text, binary) */
  6313.     binary = atoi(p);
  6314.     b_save = binary;
  6315. #ifndef NOICP
  6316.     g_binary = -1;
  6317. #endif /* NOICP */
  6318.     return(1);
  6319.       case 301:                /* File name conversion */
  6320.     fncnv = 1 - atoi(p);        /* (oops) */
  6321.     f_save = fncnv;
  6322. #ifndef NOICP
  6323.     g_fncnv = -1;
  6324. #endif /* NOICP */
  6325.     return(1);
  6326.       case 302:                /* File name collision */
  6327. #ifdef IKSD
  6328. #ifdef CK_LOGIN
  6329.     if (inserver && isguest)    /* May not be changed by guest */
  6330.       return(0);
  6331. #endif /* CK_LOGIN */
  6332. #endif /* IKSD */
  6333.     x = atoi(p);
  6334.     if (!ENABLED(en_del) && (x == XYFX_X || x == XYFX_U))
  6335.       return(0);
  6336.     if (x == XYFX_R) ckwarn = 1;    /* Rename */
  6337.     if (x == XYFX_X) ckwarn = 0;    /* Replace */
  6338.     fncact = x;
  6339.     return(1);
  6340.       case 310:                /* Incomplete File Disposition */
  6341.     keep = atoi(p);            /* Keep, Discard, Auto */
  6342.     return(1);
  6343.       case 311:                /* Blocksize */
  6344.     fblksiz = atoi(p);
  6345.     return(1);
  6346.       case 312:                /* Record Length */
  6347.     frecl = atoi(p);
  6348.     return(1);
  6349.       case 313:                /* Record format */
  6350.     frecfm = atoi(p);
  6351.     return(1);
  6352.       case 314:                /* File organization */
  6353.     forg = atoi(p);
  6354.     return(1);
  6355.       case 315:                /* File carriage control */
  6356.     fcctrl = atoi(p);
  6357.     return(1);
  6358.       case 400:                /* Block check */
  6359.     y = atoi(p);
  6360.     if (y < 5 && y > 0) {
  6361.         bctr = y;
  6362.         c_save = -1;
  6363.         return(1);
  6364.     } else if (*p == 'B') {
  6365.         bctr = 4;
  6366.         c_save = -1;
  6367.         return(1);
  6368.     }
  6369.     return(0);
  6370.       case 401:                /* Receive packet-length */
  6371.     rpsiz = urpsiz = atoi(p);
  6372.     if (urpsiz > MAXRP) urpsiz = MAXRP; /* Max long-packet length */
  6373.     if (rpsiz > 94) rpsiz = 94;        /* Max short-packet length */
  6374.     urpsiz = adjpkl(urpsiz,wslots,bigrbsiz);
  6375.     return(1);
  6376.       case 402:                /* Receive timeout */
  6377.     y = atoi(p);            /* Client is telling us */
  6378.     if (y > -1 && y < 999) {    /* the timeout that it wants */
  6379.         pkttim = chktimo(y,timef);    /* us to tell it to use. */
  6380.         return(1);
  6381.     } else return(0);
  6382.       case 403:                /* Retry limit */
  6383.     y = atoi(p);
  6384.     if (y > -1 && y < 95) {
  6385.         maxtry = y;
  6386.         return(1);
  6387.     } else return(0);
  6388.       case 404:                /* Server timeout */
  6389.     y = atoi(p);
  6390.     if (y < 0) return(0);
  6391.     srvtim = y;
  6392.     return(1);
  6393.  
  6394. #ifndef NOCSETS
  6395.       case 405:                /* Transfer character set */
  6396.     for (i = 0; i < ntcsets; i++) {
  6397.         if (!strcmp(tcsinfo[i].designator,p)) break;
  6398.     }
  6399.     debug(F101,"remset xfer charset lookup","",i);
  6400.     if (i == ntcsets) return(0);
  6401.     tcharset = tcsinfo[i].code;    /* if known, use it */
  6402.     if (tcharset == TC_TRANSP)
  6403.       rx = NULL;
  6404.     else
  6405.       rx = xlr[tcharset][fcharset];    /* translation function */
  6406.     return(1);
  6407.  
  6408.       case 320: {            /* File character set */
  6409.       extern struct keytab fcstab[];
  6410.       extern int nfilc, s_cset, r_cset;
  6411.       x = lookup(fcstab,p,nfilc,&y);
  6412.       debug(F111,"RSET FILE CHAR name",p,x);
  6413.       if (x < 0)
  6414.         return(0);
  6415.       s_cset = XMODE_M;        /* No automatic charset switching */
  6416.       r_cset = XMODE_M;
  6417.       fcharset = x;            /* Set file charset  */
  6418.       rx = xlr[tcharset][fcharset];    /* translation function */
  6419.       return(1);
  6420.       }
  6421. #endif /* NOCSETS */
  6422.  
  6423.       case 406:                /* Window slots */
  6424.     y = atoi(p);
  6425.     if (y == 0) y = 1;
  6426.     if (y < 1 || y > MAXWS) return(0);
  6427.     wslotr = y;
  6428.     swcapr = 1;
  6429.     urpsiz = adjpkl(urpsiz,wslotr,bigrbsiz);
  6430.     return(1);
  6431.  
  6432.       case 410:                /* Transfer mode */
  6433.     y = atoi(p);            /* 0 = automatic, nonzero = manual */
  6434.     if (y != 0) y = 1;
  6435.     xfermode = y;
  6436.     debug(F101,"REMOTE SET xfermode","",xfermode);
  6437.     return(1);
  6438.  
  6439.       case 420:                /* SERVER CD-MESSAGE { ON, OFF } */
  6440.     y = atoi(p);            /* 0 = automatic, nonzero = manual */
  6441.     srvcdmsg = y;
  6442.     return(1);
  6443.  
  6444.       default:                /* Anything else... */
  6445.     return(0);
  6446.     }
  6447. }
  6448.  
  6449. /* Adjust packet length based on number of window slots and buffer size */
  6450.  
  6451. int
  6452. adjpkl(pktlen,slots,bufsiz) int pktlen, slots, bufsiz; {
  6453.     if (protocol != PROTO_K) return(pktlen);
  6454.     debug(F101,"adjpkl len","",pktlen);
  6455.     debug(F101,"adjpkl slots","",slots);
  6456.     debug(F101,"adjpkl bufsiz","",bufsiz);
  6457.     if (((pktlen + 6) * slots) > bufsiz)
  6458.       pktlen = (bufsiz / slots) - 6;
  6459.     debug(F101,"adjpkl new len","",pktlen);
  6460.     return(pktlen);
  6461. }
  6462.  
  6463. /* Set transfer mode and file naming based on comparison of system types */
  6464.  
  6465.  
  6466. VOID
  6467. whoarewe() {
  6468. #ifndef NOICP
  6469.     extern int g_xfermode;
  6470. #endif /* NOICP */
  6471.  
  6472.     wearealike = 0;
  6473.  
  6474.     debug(F101,"whoarewe xfermode","",xfermode);
  6475. #ifndef NOICP
  6476.     debug(F101,"whoarewe g_xfermode","",g_xfermode);
  6477. #endif /* NOICP */
  6478.     if (whoareu[0]) {            /* If we know partner's system type */
  6479.     char * p = (char *)whoareu;
  6480.     debug(F110,"whoarewe remote sysid",whoareu,0);
  6481.     if (!strcmp(p,cksysid))        /* Other system same as us */
  6482.       wearealike = 1;
  6483.  
  6484. #ifdef UNIX
  6485.     else if (!strcmp(p,"L3"))    /* UNIX is sort of like AmigaDOS */
  6486.       wearealike = 1;        /* (same directory separator) */
  6487.     else if (!strcmp(p,"N3"))    /* UNIX like Aegis */
  6488.       wearealike = 1;
  6489. #else
  6490. #ifdef AMIGA
  6491. /* Like UNIX, but case distinctions are ignored and can begin with device:. */
  6492.     else if (!strcmp(p,"U1"))    /* Amiga is sort of like UNIX */
  6493.       wearealike = 1;
  6494.     else if (!strcmp(p,"N3"))    /* Amiga is sort of like Aegis */
  6495.       wearealike = 1;
  6496. #else
  6497. #ifdef OS2                /* (Includes Windows 95/NT) */
  6498.  
  6499.     /* DOS, GEMDOS, Windows 3.x, Windows 95, Windows NT */
  6500.     /* All "the same" for FAT partitions but all bets off otherwise */
  6501.     /* so this part needs some refinement ...  */
  6502.  
  6503.     else if (!strcmp(p,"U8"))    /* MS-DOS */
  6504.       wearealike = 1;
  6505.     else if (!strcmp(p,"UO"))    /* OS/2 */
  6506.       wearealike = 1;
  6507.     else if (!strcmp(p,"UN"))    /* Windows NT or 95 */
  6508.       wearealike = 1;
  6509.     else if (!strcmp(p,"K2"))    /* GEMDOS */
  6510.       wearealike = 1;
  6511. #else
  6512. #ifdef GEMDOS
  6513.     else if (!strcmp(p,"U8"))
  6514.       wearealike = 1;
  6515.     else if (!strcmp(p,"UO"))
  6516.       wearealike = 1;
  6517.     else if (!strcmp(p,"UN"))
  6518.       wearealike = 1;
  6519.     else if (!strcmp(p,"K2"))
  6520.       wearealike = 1;
  6521. #endif /* GEMDOS */
  6522. #endif /* OS2 */
  6523. #endif /* AMIGA */
  6524. #endif /* UNIX */
  6525.  
  6526.     /* Get here with wearealike == 1 if system types match */
  6527.  
  6528.     debug(F101,"whoarewe wearealike","",wearealike);
  6529.     if (!wearealike)        /* Not alike */
  6530.       return;
  6531.  
  6532.     fncnv = XYFN_L;            /* Alike, so literal filenames */
  6533.     debug(F101,"whoarewe setting fncnv","",fncnv);
  6534.  
  6535.     if (xfermode == XMODE_A) {    /* Current xfer mode is auto */
  6536. #ifdef VMS
  6537.         binary = XYFT_L;        /* For VMS-to-VMS, use labeled */
  6538. #else
  6539. #ifdef OS2
  6540.         /* OS/2 but not Windows */
  6541.         if (!strcmp(cksysid,"UO") && !strcmp((char *)whoareu,"UO"))
  6542.           binary = XYFT_L;        /* For OS/2-to-OS/2, use labeled */
  6543. #else
  6544.         binary = XYFT_B;        /* For all others use binary */
  6545. #endif /* OS2 */
  6546. #endif /* VMS */
  6547.         gnf_binary = binary;    /* Prevailing type for gnfile() */
  6548.         debug(F101,"whoarewe setting binary","",binary);
  6549.     }
  6550.     }
  6551. }
  6552. #endif /* NOXFER */
  6553.