home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku200.zip / ckcfns.c < prev    next >
C/C++ Source or Header  |  2001-11-15  |  210KB  |  6,867 lines

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