home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / ckcfns.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  211KB  |  7,114 lines

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