home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc190.zip / ckcfn3.c < prev    next >
C/C++ Source or Header  |  1994-10-26  |  55KB  |  1,712 lines

  1. /*  C K C F N 3  --  Packet buffer management for C-Kermit  */
  2.  
  3. /* (plus assorted functions tacked on at the end) */
  4.  
  5. /*
  6.   Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  7.   Columbia University Academic Information Systems, New York City.
  8.  
  9.   Copyright (C) 1985, 1994, Trustees of Columbia University in the City of New
  10.   York.  The C-Kermit software may not be, in whole or in part, licensed or
  11.   sold for profit as a software product itself, nor may it be included in or
  12.   distributed with commercial products or otherwise distributed by commercial
  13.   concerns to their clients or customers without written permission of the
  14.   Office of Kermit Development and Distribution, Columbia University.  This
  15.   copyright notice must not be removed, altered, or obscured.
  16. */
  17.  
  18. #include "ckcsym.h"
  19. #include "ckcdeb.h"
  20. #include "ckcasc.h"
  21. #include "ckcker.h"
  22. #include "ckcxla.h"
  23.  
  24. #ifndef NODISPO
  25. #ifdef pdp11
  26. #define NODISPO
  27. #endif /* pdpd11 */
  28. #endif /* NODISPO */
  29.  
  30. extern int unkcs, wmax, wcur, discard, bctu, bctl, local, fdispla, what,
  31.   sendmode, opnerr;
  32. extern long sendstart;
  33.  
  34. extern char * ofn2;
  35. extern char ofn1[];
  36. extern int ofn1x;
  37.  
  38. #ifdef CK_LABELED
  39. extern int xflg;
  40. #endif /* CK_LABELED */
  41. extern CHAR *data;
  42. extern char filnam[];
  43. #ifndef NOFRILLS
  44. extern int rprintf, rmailf;        /* REMOTE MAIL, PRINT */
  45. extern char optbuf[];            /* Options buffer for mail, print */
  46. #endif /* NOFRILLS */
  47. extern int wslots;
  48. extern int fblksiz, frecl, forg, frecfm, fncact, fncsav, fcctrl, lf_opts;
  49. #ifdef pdp11
  50.   extern CHAR srvcmd[];
  51.   extern char tmpbuf[];
  52.   CHAR *pktmsg = (CHAR *) tmpbuf;
  53. #else
  54. #ifdef DYNAMIC
  55.   extern CHAR *srvcmd;
  56.   extern CHAR *pktmsg;
  57. #else
  58.   extern CHAR srvcmd[];
  59.   extern CHAR pktmsg[];
  60. #endif /* DYNAMIC */
  61. #endif /* pdp11 */
  62.  
  63. extern int binary, spsiz;
  64. extern int pktnum, cxseen, czseen, bsave, bsavef, nfils, stdinf;
  65. extern int memstr, stdouf, keep, sndsrc, hcflg;
  66. extern int server, en_cwd;
  67.  
  68. extern int
  69.   atenci, atenco, atdati, atdato, atleni, atleno, atblki, atblko,
  70.   attypi, attypo, atsidi, atsido, atsysi, atsyso, atdisi, atdiso; 
  71.  
  72. #ifdef STRATUS
  73. extern int atfrmi, atfrmo, atcrei, atcreo, atacti, atacto;
  74. #endif /* STRATUS */
  75.  
  76. #ifdef datageneral
  77. extern int quiet;
  78. #endif /* datageneral */
  79.  
  80. extern long fsize, filcnt, ffc, tfc;
  81.  
  82. #ifndef NOCSETS
  83. extern int tcharset, fcharset;
  84. extern int ntcsets;
  85. extern struct csinfo tcsinfo[], fcsinfo[];
  86.  
  87. /* Pointers to translation functions */
  88. #ifdef CK_ANSIC
  89. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Character set */
  90. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* translation functions */
  91. extern CHAR (*rx)(CHAR); /* Pointer to input character translation function */
  92. extern CHAR (*sx)(CHAR); /* Pointer to output character translation function */
  93. #else
  94. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])();    /* Character set */
  95. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])();    /* translation functions. */
  96. extern CHAR (*rx)();    /* Pointer to input character translation function */
  97. extern CHAR (*sx)();    /* Pointer to output character translation function */
  98. #endif /* CK_ANSIC */
  99. #endif /* NOCSETS */
  100.  
  101. /* Variables global to Kermit that are defined in this module */
  102.  
  103. int winlo;                /* packet number at low window edge  */
  104.  
  105. int sbufnum;                /* number of free buffers */
  106. int dum001 = 1234;            /* protection... */
  107. int sbufuse[MAXWS];            /* buffer in-use flag */
  108. int dum003 = 1111;
  109. int rbufnum;                /* number of free buffers */
  110. int dum002 = 4321;            /* more protection */
  111. int rbufuse[MAXWS];            /* buffer in-use flag */
  112. int sseqtbl[64];            /* sequence # to buffer # table */
  113. int rseqtbl[64];            /* sequence # to buffer # table */
  114. int sacktbl[64];            /* sequence # ack table */
  115.  
  116. #ifdef DYNAMIC
  117. struct pktinfo *s_pkt = NULL;        /* array of pktinfo structures */
  118. struct pktinfo *r_pkt = NULL;        /* array of pktinfo structures */
  119. #else
  120. struct pktinfo s_pkt[MAXWS];        /* array of pktinfo structures */
  121. struct pktinfo r_pkt[MAXWS];        /* array of pktinfo structures */
  122. #endif /* DYNAMIC */
  123.  
  124. #ifdef DEBUG
  125. char xbuf[200];                /* For debug logging */
  126. #endif /* DEBUG */
  127.  
  128. #ifdef DYNAMIC
  129. CHAR *bigsbuf = NULL, *bigrbuf = NULL;
  130. #else
  131. char bigsbt[8];                /* Protection (shouldn't need this). */
  132.                     /* BUT DON'T REMOVE IT! */
  133. CHAR bigsbuf[SBSIZ + 5];        /* Send-packet buffer area */
  134. char bigrbt[8];                /* Safety padding */
  135. CHAR bigrbuf[RBSIZ + 5];        /* Receive-packet area */
  136. #endif
  137. int bigsbsiz = SBSIZ;            /* Sizes of big send & rcv buffers. */
  138. int bigrbsiz = RBSIZ;
  139.  
  140. /* FUNCTIONS */
  141.  
  142. /* For sanity, use "i" for buffer slots, "n" for packet numbers. */
  143.  
  144. /* I N I B U F S */
  145.  
  146. /*
  147.   Allocates the big send and receive buffers.
  148.   Call with size for big send buffer (s) and receive buffer (r).
  149.   These sizes can be different.
  150.   Attempts to allocate buffers of the requested size, but if it can't,
  151.   it will allocate smaller ones.
  152.   Sets global variables bigsbsiz and bigrbsiz to the actual sizes,
  153.   and bigsbuf and bigrbuf pointing to the actual buffers.
  154.   Designed to be called more than once.
  155.   Returns 0 on success, -1 on failure.
  156. */
  157.  
  158. CHAR *bigbufp = NULL;
  159.  
  160. int
  161. inibufs(s,r) int s, r; {
  162. #ifdef DYNAMIC
  163.     unsigned
  164.       int size;
  165. #ifdef OS2
  166.     unsigned        /* Don't you wish everybody had unsigned long... */
  167. #endif /* OS2 */  
  168.       long z;
  169.     int x;
  170.  
  171.     debug(F101,"inibufs s","",s);
  172.     debug(F101,"inibufs r","",r);
  173.  
  174.     if (s < 80 || r < 80) return(-1);    /* Validate arguments. */
  175.  
  176.     if (!s_pkt) {            /* Allocate packet info structures */
  177.     if (!(s_pkt = (struct pktinfo *) malloc(sizeof(struct pktinfo)*MAXWS)))
  178.       fatal("ini_pkts: no memory for s_pkt");
  179.     }
  180.     for (x = 0; x < MAXWS; x++)
  181.       s_pkt[x].pk_adr = NULL;        /* Initialize addresses */
  182.  
  183.     if (!r_pkt) {
  184.     if (!(r_pkt = (struct pktinfo *) malloc(sizeof(struct pktinfo)*MAXWS)))
  185.       fatal("ini_pkts: no memory for s_pkt");
  186.     }
  187.     for (x = 0; x < MAXWS; x++)
  188.       r_pkt[x].pk_adr = NULL;        /* Initialize addresses */
  189.  
  190.     if (!srvcmd) {            /* Allocate srvcmd buffer */
  191.     srvcmd = (CHAR *) malloc(r + 100);
  192.     if (!srvcmd) return(-1);
  193.     *srvcmd = NUL;
  194.     }
  195.     if (!pktmsg) {            /* Allocate pktmsg buffer */
  196.     pktmsg = (CHAR *) malloc(81);
  197.     if (!pktmsg) return(-1);
  198.     *pktmsg = NUL;
  199.     }
  200.  
  201.     if (bigbufp) {            /* Free previous buffers, if any. */
  202.     free(bigbufp);
  203.     bigbufp = NULL;
  204.     }
  205.     size = s + r + 40;            /* Combined requested size + padding */
  206.     z  = (unsigned) s + (unsigned) r + 40;
  207.     debug(F101,"inibufs size 1","",size);
  208.     debug(F101,"inibufs size z","",z);
  209.     if ((long) size != z) {
  210.     debug(F100,"inibufs overflow","",0);
  211.     size = 65535;
  212.     }    
  213.  
  214.     /* Try to get the space.  If malloc fails, try to get a little less. */
  215.     /* (Obviously, this algorithm can be refined.) */
  216.  
  217.     while (!(bigbufp = (CHAR *) malloc(size))) {
  218.     debug(F101,"inibufs bigbuf malloc failed","",size);
  219.     size = (size * 2) / 3;        /* Failed, cut size by 1/3. */
  220.     if (size < 200)            /* Try again until too small. */
  221.       return(-1);
  222.     }
  223.     debug(F101,"inibufs size 2","",size); /* OK, we got some space. */
  224.  
  225. /*
  226.   Now divide the allocated space between the send and receive buffers in the
  227.   requested proportion.  The natural formula would be (s / (s + r)) * size
  228.   (for the send buffer), but that doesn't work with integer arithmetic and we
  229.   can't use floating point because some machines don't have it.  This can be
  230.   rearranged as (s * size) / (s + r).  But (s * size) can be VERY large, too
  231.   large for 32 bits.  So let's do it this way.  This arithmetic works for
  232.   buffer sizes up to about 5,000,000.
  233. */
  234. #define FACTOR 20L
  235.     z = ( (long) s * FACTOR ) / ( (long) s + (long) r );
  236.     x = ( z * ( (long) size / FACTOR ) );
  237.     if (x < 0) return(-1);        /* Catch overflow */
  238.  
  239.     bigsbsiz = x - 5;            /* Size of send buffer */
  240.     bigsbuf = bigbufp;            /* Address of send buffer */
  241.     debug(F101,"inibufs bigsbsiz","",bigsbsiz);
  242.  
  243.     bigrbsiz = size - x - 5;        /* Size of receive buffer */
  244.     bigrbuf = bigbufp + x;        /* Addresss of receive buffer */
  245.     debug(F101,"inibufs bigrbsiz","",bigrbsiz);
  246.  
  247.     return(0);                /* Success */
  248. #else                    /* No dynamic allocation */
  249.     bigsbsiz = SBSIZ;            /* Just use the symbols */
  250.     bigrbsiz = RBSIZ;            /* ... */
  251.     return(0);                /* Success. */
  252. #endif /* DYNAMIC */
  253. }
  254.  
  255.  
  256. /* M A K E B U F  --  Makes and clears a new buffers.  */
  257.  
  258. /* Call with: */
  259. /*  slots:  number of buffer slots to make, 1 to 32 */
  260. /*  bufsiz: size of the big buffer */
  261. /*  buf:    address of the big buffer */
  262. /*  xx:     pointer to array of pktinfo structures for these buffers */
  263.  
  264. /* Subdivides the big buffer into "slots" buffers. */
  265.  
  266. /* Returns: */
  267. /*  -1 if too many or too few slots requested,     */
  268. /*  -2 if slots would be too small.      */
  269. /*   n (positive) on success = size of one buffer. */
  270. /*   with pktinfo structure initialized for this set of buffers. */
  271.  
  272. int
  273. makebuf(slots,bufsiz,buf,xx)
  274. /* makebuf */ int slots, bufsiz; CHAR buf[]; struct pktinfo *xx; {
  275.  
  276.     CHAR *a;
  277.     int i, size;
  278.  
  279.     debug(F101,"makebuf","",slots);
  280.     debug(F101,"makebuf bufsiz","",bufsiz);
  281.     debug(F101,"makebuf MAXWS","",MAXWS);
  282.  
  283.     if (slots > MAXWS || slots < 1) return(-1);
  284.     if (bufsiz < slots * 10 ) return(-2);
  285.  
  286.     size = bufsiz / slots;        /* Divide up the big buffer. */
  287.     a = buf;                /* Address of first piece. */
  288.  
  289.     for (i = 0; i < slots; i++) {
  290.     struct pktinfo *x = &xx[i];
  291.     x->bf_adr = a;            /* Address of this buffer */
  292.     x->bf_len = size;        /* Length of this buffer */
  293.     x->pk_len = 0;            /* Length of data field */
  294.         x->pk_typ = ' ';        /* packet type */
  295.     x->pk_seq = -1;            /* packet sequence number */
  296.         x->pk_flg = 0;            /* ack'd bit */
  297.         x->pk_rtr = 0;            /* retransmissions */
  298.     *a = '\0';            /* Clear the buffer */
  299.     a += size;            /* Position to next buffer slot */
  300.     }
  301.     return(size);
  302. }
  303.  
  304. /*  M A K S B U F  --  Makes the send-packet buffer  */
  305.  
  306. int
  307. mksbuf(slots) int slots; {
  308.     int i, x;
  309.     sbufnum = 0;
  310.     if ((x = makebuf(slots,bigsbsiz,bigsbuf,s_pkt)) < 0) {
  311.     debug(F101,"mksbuf makebuf return","",x);
  312.     return(x);
  313.     }
  314.     debug(F101,"mksbuf makebuf return","",x);
  315.     for (i = 0; i < 64; i++) {        /* Initialize sequence-number- */
  316.     sseqtbl[i] = -1;        /* to-buffer-number table. */
  317.         sacktbl[i] = 0;
  318.     }
  319.     for (i = 0; i < MAXWS; i++)
  320.       sbufuse[i] = 0;            /* Mark each buffer as free */
  321.     sbufnum = slots;
  322.     wcur = 0;
  323.     return(x);
  324. }
  325.  
  326. /*  M A K R B U F  --  Makes the receive-packet buffer  */
  327.  
  328. int
  329. mkrbuf(slots) int slots; {
  330.     int i, x;
  331.     rbufnum = 0;
  332.     if ((x = makebuf(slots,bigrbsiz,bigrbuf,r_pkt)) < 0) {
  333.     debug(F101,"mkrbuf makebuf return","",x);
  334.     return(x);
  335.     }
  336.     debug(F101,"mkrbuf makebuf return","",x);
  337.     for (i = 0; i < 64; i++) {        /* Initialize sequence-number- */
  338.     rseqtbl[i] = -1;        /* to-buffer-number table. */
  339.     }
  340.     for (i = 0; i < MAXWS; i++)
  341.       rbufuse[i] = 0;            /* Mark each buffer as free */
  342.     rbufnum = slots;
  343.     wcur = 0;
  344.     return(x);
  345. }
  346.  
  347. /*  W I N D O W  --  Resize the window to n  */
  348.  
  349. int
  350. window(n) int n; {
  351.     debug(F101,"window","",n);
  352.     if (n < 1 || n > MAXWS) return(-1);
  353.     if (mksbuf(n) < 0) return(-1);
  354.     if (mkrbuf(n) < 0) return(-1);
  355.     wslots = n;
  356. #ifdef DEBUG
  357.     if (deblog) dumpsbuf();
  358.     if (deblog) dumprbuf();
  359. #endif /* DEBUG */
  360.     return(0);
  361. }
  362.  
  363. /*  G E T S B U F  --  Allocate a send-buffer.  */
  364.  
  365. /*  Call with packet sequence number to allocate buffer for. */
  366. /*  Returns: */
  367. /*   -4 if argument is invalid (negative, or greater than 63) */
  368. /*   -3 if buffers were thought to be available but really weren't (bug!) */
  369. /*   -2 if the number of free buffers is negative (bug!) */
  370. /*   -1 if no free buffers. */
  371. /*   0 or positive, packet sequence number, with buffer allocated for it. */
  372.  
  373. int
  374. getsbuf(n) int n; {            /* Allocate a send-buffer */
  375.     int i;
  376.     if (n < 0 || n > 63) {
  377.     debug(F101,"getsbuf bad arg","",n);
  378.     return(-4);    /* Bad argument */
  379.     }
  380.     debug(F101,"getsbuf, packet","",n);
  381.     debug(F101,"getsbuf, sbufnum","",sbufnum);
  382.     if (sbufnum == 0) return(-1);    /* No free buffers. */
  383.     if (sbufnum < 0) return(-2);    /* Shouldn't happen. */
  384.     for (i = 0; i < wslots; i++)    /* Find the first one not in use. */
  385.       if (sbufuse[i] == 0) {        /* Got one? */
  386.       sbufuse[i] = 1;        /* Mark it as in use. */
  387.       sbufnum--;            /* One less free buffer. */
  388.       *s_pkt[i].bf_adr = '\0';    /* Zero the buffer data field */
  389.       s_pkt[i].pk_seq = n;        /* Put in the sequence number */
  390.           sseqtbl[n] = i;        /* Back pointer from sequence number */
  391.           sacktbl[n] = 0;        /* ACK flag */
  392.       s_pkt[i].pk_len = 0;        /* Data field length now zero. */
  393.       s_pkt[i].pk_typ = ' ';    /* Blank the packet type too. */
  394.       s_pkt[i].pk_flg = 0;        /* Zero the flags */
  395.       s_pkt[i].pk_rtr = 0;        /* Zero the retransmission count */
  396.       data = s_pkt[i].bf_adr + 7;    /* Set global "data" address. */
  397.       if ((what & (W_SEND|W_REMO)) && (++wcur > wmax))
  398.         wmax = wcur;        /* For statistics. */
  399.       return(n);            /* Return its index. */
  400.       }
  401.     sbufnum = 0;            /* Didn't find one. */
  402.     return(-3);                /* Shouldn't happen! */
  403. }
  404.  
  405. int
  406. getrbuf() {                /* Allocate a receive buffer */
  407.     int i;
  408.     debug(F101,"getrbuf rbufnum","",rbufnum);
  409.     debug(F101,"getrbuf wslots","",wslots);
  410.     debug(F101,"getrbuf dum002","",dum002);
  411.     debug(F101,"getrbuf dum003","",dum003);
  412.     if (rbufnum == 0) return(-1);    /* No free buffers. */
  413.     if (rbufnum < 0) return(-2);    /* Shouldn't happen. */
  414.     for (i = 0; i < wslots; i++)    /* Find the first one not in use. */
  415.       if (rbufuse[i] == 0) {        /* Got one? */
  416.       rbufuse[i] = 1;        /* Mark it as in use. */
  417.       *r_pkt[i].bf_adr = '\0';    /* Zero the buffer data field */
  418.       rbufnum--;            /* One less free buffer. */
  419.       debug(F101,"getrbuf new rbufnum","",rbufnum);
  420.       if ((what & W_RECV) && (++wcur > wmax))
  421.         wmax = wcur;        /* For statistics. */
  422.       return(i);            /* Return its index. */
  423.       }
  424.     debug(F101,"getrbuf foulup","",i);
  425.     rbufnum = 0;            /* Didn't find one. */
  426.     return(-3);                /* Shouldn't happen! */
  427. }
  428.  
  429. /*  F R E E S B U F  --  Free send-buffer for given packet sequence number */
  430.  
  431. /*  Returns:  */
  432. /*   1 upon success  */
  433. /*  -1 if specified buffer does not exist */
  434.  
  435. int
  436. freesbuf(n) int n; {            /* Release send-buffer for packet n. */
  437.     int i;
  438.  
  439.     debug(F101,"freesbuf","",n);
  440.     if (n < 0 || n > 63)        /* No such packet. */
  441.       return(-1);
  442.     i = sseqtbl[n];            /* Get the window slot number. */
  443.     if (i > -1 && i <= wslots) {
  444.     sseqtbl[n] = -1;        /* If valid, remove from seqtbl */
  445.      sbufnum++;            /* and count one more free buffer */
  446.     sbufuse[i] = 0;            /* and mark it as free, */
  447.     if (what & (W_SEND|W_REMO))    /* decrement active slots */
  448.       wcur--;            /* for statistics and display. */
  449.     } else {
  450.     debug(F101," sseqtbl[n]","",sseqtbl[n]);
  451.     return(-1);
  452.     }
  453.  
  454. /* The following is done only so dumped buffers will look right. */
  455.  
  456.     if (1) {
  457.     *s_pkt[i].bf_adr = '\0';    /* Zero the buffer data field */
  458.     s_pkt[i].pk_seq = -1;        /* Invalidate the sequence number */
  459.     s_pkt[i].pk_len = 0;        /* Data field length now zero. */
  460.     s_pkt[i].pk_typ = ' ';        /* Blank the packet type too. */
  461.     s_pkt[i].pk_flg = 0;        /* And zero the flag */
  462.     s_pkt[i].pk_rtr = 0;        /* And the retries field. */
  463.     }
  464.     return(1);
  465. }
  466.  
  467. int
  468. freerbuf(i) int i; {            /* Release receive-buffer slot "i". */
  469.     int n;
  470.  
  471. /* NOTE !! Currently, this function frees the indicated buffer, but */
  472. /* does NOT erase the data.  The program counts on this.  Will find a */
  473. /* better way later.... */
  474.  
  475.     debug(F101,"freerbuf, slot","",i);
  476.     if (i < 0 || i >= wslots) {        /* No such slot. */
  477.     debug(F101,"freerbuf no such slot","",i);
  478.     return(-1);
  479.     }
  480.     n = r_pkt[i].pk_seq;        /* Get the packet sequence number */
  481.     debug(F101,"freerbuf, packet","",n);
  482.     if (n > -1 && n < 64)        /* If valid, remove from seqtbl */
  483.       rseqtbl[n] = -1;
  484.     if (rbufuse[i] != 0) {        /* If really allocated, */
  485.     rbufuse[i] = 0;            /* mark it as free, */
  486.     rbufnum++;            /* and count one more free buffer. */
  487.     if (what & W_RECV)        /* Keep track of current slots */
  488.       wcur--;            /*  for statistics and display */
  489.     debug(F101,"freerbuf, new rbufnum","",rbufnum);
  490.     }
  491.  
  492. /* The following is done only so dumped buffers will look right. */
  493.  
  494.     if (1) {
  495.      /* *r_pkt[i].bf_adr = '\0'; */    /* Zero the buffer data field */
  496.     r_pkt[i].pk_seq = -1;        /* And from packet list */
  497.     r_pkt[i].pk_len = 0;        /* Data field length now zero. */
  498.     r_pkt[i].pk_typ = ' ';        /* Blank the packet type too. */
  499.     r_pkt[i].pk_flg = 0;        /* And zero the flag */
  500.     r_pkt[i].pk_rtr = 0;        /* And the retries field. */
  501.     }
  502.     return(1);
  503. }
  504.  
  505. /* This is like freerbuf, except it's called with a packet sequence number */
  506. /* rather than a packet buffer index. */
  507.  
  508. VOID
  509. freerpkt(seq) int seq; {
  510.     int k;
  511.     debug(F101,"freerpkt seq","",seq);
  512.     k = rseqtbl[seq];
  513.     debug(F101,"freerpkt k","",k);
  514.     if (k > -1) {
  515.     k = freerbuf(k);
  516.     debug(F101,"freerpkt freerbuf","",k);
  517.     }
  518. }
  519.  
  520.  
  521. /*  C H K W I N  --  Check if packet n is in window. */
  522.  
  523. /*  Returns: */
  524. /*    0 if it is in the current window,  */
  525. /*   +1 if it would have been in previous window (e.g. if ack was lost), */
  526. /*   -1 if it is outside any window (protocol error),   */
  527. /*   -2 if either of the argument packet numbers is out of range.  */
  528.  
  529. /* Call with packet number to check (n), lowest packet number in window */
  530. /* (bottom), and number of slots in window (slots).  */
  531.  
  532. int
  533. chkwin(n,bottom,slots) int n, bottom, slots; {
  534.     int top, prev;
  535.  
  536.     debug(F101,"chkwin packet","",n);
  537.     debug(F101,"chkwin winlo","",bottom);
  538.     debug(F101,"chkwin slots","",slots);
  539.  
  540. /* First do the easy and common cases, where the windows are not split. */
  541.  
  542.     if (n < 0 || n > 63 || bottom < 0 || bottom > 63)
  543.       return(-2);
  544.  
  545.     if (n == bottom) return(0);        /* In a perfect world... */
  546.  
  547.     top = bottom + slots;        /* Calculate window top. */
  548.     if (top < 64 && n < top && n >= bottom)
  549.       return(0);            /* In current window. */
  550.  
  551.     prev = bottom - slots;        /* Bottom of previous window. */
  552.     if (prev > -1 && n < bottom && n > prev)
  553.       return(1);            /* In previous. */
  554.  
  555. /* Now consider the case where the current window is split. */
  556.  
  557.     if (top > 63) {            /* Wraparound... */
  558.     top -= 64;            /* Get modulo-64 sequence number */
  559.     if (n < top || n >= bottom) {
  560.         return(0);            /* In current window. */
  561.     } else {            /* Not in current window. */
  562.         if (n < bottom && n >= prev) /* Previous window can't be split. */
  563.           return(1);        /* In previous window. */
  564.         else
  565.           return(-1);        /* Not in previous window. */
  566.     }
  567.     }
  568.  
  569. /* Now the case where current window not split, but previous window is. */ 
  570.  
  571.     if (prev < 0) {            /* Is previous window split? */
  572.     prev += 64;            /* Yes. */
  573.     if (n < bottom || n >= prev)
  574.       return(1);            /* In previous window. */
  575.     } else {                /* Previous window not split. */
  576.     if (n < bottom && n >= prev)
  577.       return(1);            /* In previous window. */
  578.     }
  579.     
  580. /* It's not in the current window, and not in the previous window... */
  581.  
  582.     return(-1);                /* So it's not in any window. */
  583. }
  584.  
  585. int
  586. dumpsbuf() {                /* Dump send-buffers */
  587. #ifdef DEBUG
  588.     int j, x;                /* to debug log. */
  589.  
  590.     if (! deblog) return(0);
  591.     x = zsoutl(ZDFILE,"SEND BUFFERS:");
  592.     if (x < 0) {
  593.     deblog = 0;
  594.     return(0);
  595.     }
  596.     x=zsoutl(ZDFILE,"buffer inuse address length data type seq flag retries");
  597.     if (x < 0) {
  598.     deblog = 0;
  599.     return(0);
  600.     }
  601.     for ( j = 0; j < wslots; j++ ) {
  602.     sprintf(xbuf,"%4d%6d%10d%5d%6d%4c%5d%5d%6d\n",
  603.            j,
  604.            sbufuse[j],
  605.            s_pkt[j].bf_adr, 
  606.            s_pkt[j].bf_len,
  607.            s_pkt[j].pk_len,
  608.            s_pkt[j].pk_typ,
  609.            s_pkt[j].pk_seq,
  610.            s_pkt[j].pk_flg,
  611.            s_pkt[j].pk_rtr
  612.            );
  613.     if (zsout(ZDFILE,xbuf) < 0)  {
  614.         deblog = 0;
  615.         return(0);
  616.     }
  617.     if (s_pkt[j].pk_adr) {
  618.         x = (int)strlen((char *) s_pkt[j].pk_adr);
  619.         if (x)
  620.           sprintf(xbuf,"[%.72s%s]\n",s_pkt[j].pk_adr, x > 72 ? "..." : "");
  621.         else
  622.           sprintf(xbuf,"[(empty string)]\n");
  623.     } else {
  624.         sprintf(xbuf,"[(null pointer)]\n");
  625.     }
  626.     if (zsout(ZDFILE,xbuf) < 0) {
  627.         deblog = 0;
  628.         return(0);
  629.     }
  630.     }
  631.     sprintf(xbuf,"free: %d, winlo: %d\n", sbufnum, winlo);
  632.     if (zsout(ZDFILE,xbuf) < 0) {
  633.     deblog = 0;
  634.     return(0);
  635.     }
  636. #endif /* DEBUG */
  637.     return(0);
  638. }
  639. int
  640. dumprbuf() {                /* Dump receive-buffers */
  641. #ifdef DEBUG
  642.     int j, x;
  643.     if (! deblog) return(0);
  644.     if (zsoutl(ZDFILE,"RECEIVE BUFFERS:") < 0) {
  645.     deblog = 0;
  646.     return(0);
  647.     }
  648.     x=zsoutl(ZDFILE,"buffer inuse address length data type seq flag retries");
  649.     if (x < 0) {
  650.     deblog = 0;
  651.     return(0);
  652.     }
  653.     for ( j = 0; j < wslots; j++ ) {
  654.     sprintf(xbuf,"%4d%6d%10d%5d%6d%4c%5d%5d%6d\n",
  655.            j,
  656.            rbufuse[j],
  657.            r_pkt[j].bf_adr, 
  658.            r_pkt[j].bf_len,
  659.            r_pkt[j].pk_len,
  660.            r_pkt[j].pk_typ,
  661.            r_pkt[j].pk_seq,
  662.            r_pkt[j].pk_flg,
  663.            r_pkt[j].pk_rtr
  664.            );
  665.     if (zsout(ZDFILE,xbuf) < 0) {
  666.         deblog = 0;
  667.         return(0);
  668.     }
  669.     x = (int)strlen((char *)r_pkt[j].bf_adr);
  670.     sprintf(xbuf,"[%.72s%s]\n",r_pkt[j].bf_adr, x > 72 ? "..." : "");
  671.     if (zsout(ZDFILE,xbuf) < 0)  {
  672.         deblog = 0;
  673.         return(0);
  674.     }
  675.     }
  676.     sprintf(xbuf,"free: %d, winlo: %d\n", rbufnum, winlo);
  677.     if (zsout(ZDFILE,xbuf) < 0)  {
  678.     deblog = 0;
  679.     return(0);
  680.     }
  681. #endif /* DEBUG */
  682.     return(0);
  683. }
  684.  
  685. /*** Some misc functions also moved here from the other ckcfn*.c modules ***/
  686. /*** to even out the module sizes. ***/
  687.  
  688. /* Attribute Packets. */
  689.  
  690. /*
  691.   Call with xp == 0 if we're sending a real file (F packet),
  692.   or xp != 0 for screen data (X packet).
  693.   Returns 0 on success, -1 on failure.
  694. */
  695. int
  696. sattr(xp) int xp; {            /* Send Attributes */
  697.     int i, j, aln;
  698.     char *tp;
  699.     struct zattr x;
  700.  
  701.     if (zsattr(&x) < 0) return(-1);    /* Get attributes or die trying */
  702.     if (nxtpkt() < 0) return(-1);    /* Next packet number */
  703.     i = 0;                /* i = Data field character number */
  704.     if (atsido) {            /* System type */
  705.     data[i++] = '.';
  706.     data[i++] = tochar(x.systemid.len); /*  Copy from attr structure */
  707.     for (j = 0; j < x.systemid.len; j++)
  708.       data[i++] = x.systemid.val[j];
  709.     }
  710. #ifdef STRATUS
  711.     if (atcreo) {            /* File creator */
  712.     data[i++] = '$';
  713.     data[i++] = tochar(x.creator.len); /*  Copy from attr structure */
  714.     for (j = 0; j < x.creator.len; j++)
  715.       data[i++] = x.creator.val[j];
  716.     }
  717.     if (atacto) {            /* File account */
  718.     data[i++] = '%';
  719.     data[i++] = tochar(x.account.len); /*  Copy from attr structure */
  720.     for (j = 0; j < x.account.len; j++)
  721.          data[i++] = x.account.val[j];
  722.     }
  723.     if (atfrmo) {            /* Packet data format */
  724.     data[i++] = '/';
  725.     data[i++] = tochar(x.recfm.len); /*  Copy from attr structure */
  726.     for (j = 0; j < x.recfm.len; j++)
  727.       data[i++] = x.recfm.val[j];
  728.     }
  729. #endif /* STRATUS */
  730.     if (attypo) {            /* File type */
  731.     data[i++] = '"';
  732.     if (
  733. #ifdef VMS
  734.     binary == XYFT_I || binary == XYFT_L || /* IMAGE or LABELED */
  735.     !strncmp(x.recfm.val,"F",1)    /* or RECFM=Fxxxxxx */
  736. #else
  737.     binary                /* User said SET FILE TYPE BINARY  */
  738. #endif /* VMS */
  739.         ) {                /* Binary */
  740.         data[i++] = tochar(2);    /*  Two characters */
  741.         data[i++] = 'B';        /*  B for Binary */
  742.         data[i++] = '8';        /*  8-bit bytes (note assumption...) */
  743. #ifdef CK_LABELED
  744.         if (binary != XYFT_L
  745. #ifdef VMS
  746.         && binary != XYFT_I
  747. #endif /* VMS */
  748.         )
  749.         binary = XYFT_B;
  750. #endif /* CK_LABELED */
  751.     } else {            /* Text */
  752.         data[i++] = tochar(3);    /*  Three characters */
  753.         data[i++] = 'A';        /*  A = (extended) ASCII with CRLFs */
  754.         data[i++] = 'M';        /*  M for carriage return */
  755.         data[i++] = 'J';        /*  J for linefeed */
  756. #ifdef VMS
  757.         binary = XYFT_T;        /* We automatically detected text */
  758. #endif /* VMS */
  759.  
  760. #ifdef NOCSETS
  761.         data[i++] = '*';        /* Encoding */
  762.         data[i++] = tochar(1);    /* Length of value is 1 */
  763.         data[i++] = 'A';        /* A for ASCII */
  764. #else
  765.         if (tcharset == TC_TRANSP) { /* Transfer character set */
  766.         data[i++] = '*';    /* Encoding */
  767.         data[i++] = tochar(1);    /* Length of value is 1 */
  768.         data[i++] = 'A';    /* A for ASCII */
  769.         } else {
  770.         tp = tcsinfo[tcharset].designator;
  771.         if ((tp != NULL) && (aln = (int)strlen(tp)) > 0) {
  772.             data[i++] = '*';    /* Encoding */
  773.             data[i++] = tochar(aln+1); /* Length of designator. */
  774.             data[i++] = 'C';           /* Text in specified charset. */
  775.             for (j = 0; j < aln; j++)  /* Copy designator */
  776.               data[i++] = *tp++;       /*  Example: *&I6/100 */
  777.         }
  778.         }
  779. #endif /* NOCSETS */
  780.     }
  781.     }
  782.     if ((xp == 0) && (x.length > -1L)) { /* If it's a real file */
  783.  
  784.     if (atdato && (aln = x.date.len) > 0) {    /* Creation date, if any */
  785.         data[i++] = '#';
  786.         data[i++] = tochar(aln);
  787.         for (j = 0; j < aln; j++)
  788.           data[i++] = x.date.val[j];
  789.     }
  790.     if (atleno) {
  791.         data[i] = '!';            /* File length in K */
  792.         sprintf((char *) &data[i+2],"%ld",x.lengthk);
  793.         aln = (int)strlen((char *)(data+i+2));
  794.         data[i+1] = tochar(aln);
  795.         i += aln + 2;
  796.  
  797.         data[i] = '1';            /* File length in bytes */
  798.         sprintf((char *) &data[i+2],"%ld",x.length);
  799.         aln = (int)strlen((char *)(data+i+2));
  800.         data[i+1] = tochar(aln);
  801.         i += aln + 2;
  802.     }
  803.     if (atblko && fblksiz) {        /* Blocksize, if set */
  804.         data[i] = '(';
  805.         sprintf((char *) &data[i+2],"%d",fblksiz);
  806.         aln = (int)strlen((char *)(data+i+2));
  807.         data[i+1] = tochar(aln);
  808.         i += aln + 2;
  809.     }
  810.     }
  811. #ifndef NOFRILLS
  812.     if ((rprintf || rmailf) && atdiso) { /* MAIL, or REMOTE PRINT?  */
  813.     data[i++] = '+';        /* Disposition */
  814.         data[i++] = tochar((int)strlen(optbuf) + 1); /* Options, if any */
  815.     if (rprintf)
  816.       data[i++] = 'P';        /* P for Print */
  817.     else
  818.       data[i++] = 'M';        /* M for Mail */
  819.     for (j = 0; optbuf[j]; j++)    /* Copy any options */
  820.       data[i++] = optbuf[j];
  821.     }
  822. #endif /* NOFRILLS */
  823. #ifdef CK_RESEND
  824.     if (sendmode == SM_RESEND) {
  825.     data[i++] = '+';        /* Disposition */
  826.         data[i++] = tochar(1);
  827.     data[i++] = 'R';        /* is RESEND */
  828.     }
  829. #endif /* CK_RESEND */
  830.     data[i++] = '@';            /* End of Attributes */
  831.     data[i++] = SP;            /* Length 0 */
  832.     data[i] = '\0';            /* Make sure it's null-terminated */
  833.     aln = (int)strlen((char *)data);    /* Get overall length of attributes */
  834.  
  835. /* Change this code to break attribute data up into multiple packets! */
  836.  
  837.     j = (spsiz < 95) ? (92 - bctl) : (spsiz - 2 - bctl);
  838.     if (aln > j) {            /* Check length of result */
  839.     spack('A',pktnum,0,(CHAR *)"");    /* send an empty attribute packet */
  840.     debug(F101,"sattr pkt too long","",aln); /* if too long */
  841.     debug(F101,"sattr spsiz","",spsiz);
  842.     } else {                /* Otherwise */
  843.     spack('A',pktnum,aln,data);    /* send the real thing. */
  844.     debug(F111,"sattr",data,aln);
  845.     }
  846.  
  847.     return(0);
  848. }
  849.  
  850. static char *refused = "";
  851.  
  852. static char *reason[] = {
  853.     "size", "type", "date", "creator", "account", "area", "password",
  854.     "blocksize", "access", "encoding", "disposition", "protection",
  855.     "protection", "origin", "format",
  856.     "sys-dependent",            /* 0 */
  857.     "size",                /* 1 */
  858.     "2",                /* 2 */
  859.     "3",                /* 3 */
  860.     "4",                /* 4 */
  861.     "5",                /* 5 */
  862.     "6",                /* 6 */
  863.     "7",                /* 7 */
  864.     "8",                /* 8 */
  865.     "9",                /* 9 */
  866.     ":",                /* : */
  867.     ";",                /* ; */
  868.     "<",                /* < */
  869.     "=",                /* = */
  870.     ">",                /* > */
  871.     "name",                /* ? */
  872.     "@"
  873. };
  874. static int nreason = sizeof(reason) / sizeof(char *);
  875. int rejection = -1;
  876.  
  877. char *
  878. getreason(s) char *s; {            /* Decode attribute refusal reason */
  879.     char c, *p;
  880.     if (rejection == 1)            /* Kludge for SET FIL COLL DISCARD */
  881.       return("name");            /* when other Kermit doesn't... */
  882.     p = s;
  883.     if (*p++ != 'N') return("");    /* Should start with N */
  884.     else if ((c = *p) > SP) {        /* get reason, */
  885.     rejection = c;            /* remember it, */
  886.     c -= '!';            /* get offset */
  887.     p = ((unsigned int) ((CHAR) c) <= nreason) ? reason[c] : "unknown";
  888.     }
  889.     return(p);
  890. }
  891.  
  892. int
  893. rsattr(s) CHAR *s; {            /* Read response to attribute packet */
  894.     debug(F111,"rsattr: ",s,*s);
  895.     if (*s == 'N') {            /* If it's 'N' followed by anything, */
  896.     refused = getreason((char *)s);    /* they are refusing, get reason. */
  897.     debug(F110," refused",refused,0);
  898.     tlog(F110," refused:",refused,0L);
  899.     return(-1);    
  900.     }
  901. #ifdef CK_RESEND
  902.     if (sendmode == SM_RESEND && *s == '1') { /* RESEND length */
  903.     int n; long z; CHAR *p;
  904.     p = s + 1;
  905.     n = xunchar(*p++);
  906.     debug(F101,"rsattr RESEND n","",n);
  907.     z = 0L;
  908.     while (n-- > 0)            /* We assume the format is good. */
  909.       z = 10L * z + (long) (*p++ - '0');
  910.     debug(F101,"rsattr RESEND z","",z);
  911.     if (z > 0L) sendstart = z;
  912.     debug(F101,"rsattr RESEND sendstart","",sendstart);
  913.     if (sendstart > 0L)
  914.       if (zfseek(sendstart) < 0)    /* Input file is already open. */
  915.         return(0);
  916. #ifdef CK_CURSES
  917.     if (fdispla == XYFD_C)
  918.       screen(SCR_FS,0,fsize,"");    /* Refresh file transfer display */
  919. #endif /* CK_CURSES */
  920.     }
  921. #endif /* CK_RESEND */
  922.     refused = "";
  923.     return(0);
  924. }
  925.  
  926. long rs_len = 0L;            /* Length of file being resent to */
  927.  
  928. int
  929. gattr(s, yy) CHAR *s; struct zattr *yy; { /* Read incoming attribute packet */
  930.     char c, d;
  931.     char *ff;
  932.     int aln, i;
  933.  
  934. #define ABUFL 40            /* Temporary buffer for conversions */
  935.     char abuf[ABUFL];
  936. #define FTBUFL 10            /* File type buffer */
  937.     static char ftbuf[FTBUFL];
  938. #define DTBUFL 40            /* File creation date */
  939.     static char dtbuf[DTBUFL];
  940. #define TSBUFL 10            /* Transfer syntax */
  941.     static char tsbuf[TSBUFL];
  942. #define IDBUFL 10            /* System ID */
  943.     static char idbuf[IDBUFL];
  944. #ifndef DYNAMIC
  945. #define DSBUFL 100            /* Disposition */
  946.     static char dsbuf[DSBUFL];
  947. #define SPBUFL 512            /* System-dependent parameters */
  948.     static char spbuf[SPBUFL];
  949. #else
  950. #define DSBUFL 100            /* Disposition */
  951.     static char *dsbuf = NULL;
  952. #define SPBUFL 512            /* System-dependent parameters */
  953.     static char *spbuf = NULL;
  954. #endif /* DYNAMIC */
  955. #define RPBUFL 20            /* Attribute reply */
  956.     static char rpbuf[RPBUFL];
  957.  
  958.     char *rp;                /* Pointer to reply buffer */
  959.     int retcode;            /* Return code */
  960.  
  961.     d = SP;                /* Initialize disposition */
  962.     ff = filnam;            /* Filename returned by rcvfil */
  963.     if (fncact == XYFX_R && ofn1x && ofn1) /* But watch out for FC=RENAME */
  964.       ff = ofn1;            /* because we haven't renamed it yet */
  965.  
  966. /* Fill in the attributes we have received */
  967.  
  968.     rp = rpbuf;                /* Initialize reply buffer */
  969.     *rp++ = 'N';            /* for negative reply. */
  970.     *rp = NUL;
  971.     retcode = 0;            /* Initialize return code. */
  972.  
  973.     while (c = *s++) {            /* Get attribute tag */
  974.     aln = xunchar(*s++);        /* Length of attribute string */
  975.     switch (c) {
  976.       case '!':            /* File length in K */
  977.         for (i = 0; (i < aln) && (i < ABUFL); i++) /* Copy it */
  978.           abuf[i] = *s++;
  979.         abuf[i] = '\0';        /* Terminate with null */
  980.         if (i < aln) s += (aln - i); /* If field was too long for buffer */
  981.         yy->lengthk = atol(abuf);    /* Convert to number */
  982.         break;
  983.  
  984.       case '"':            /* file type */
  985.         for (i = 0; (i < aln) && (i < FTBUFL); i++)
  986.           ftbuf[i] = *s++;        /* Copy it into a static string */
  987.         ftbuf[i] = '\0';
  988.         if (i < aln) s += (aln - i);
  989.         if (attypi) {        /* TYPE attribute is enabled? */
  990.         yy->type.val = ftbuf;    /* Pointer to string */
  991.         yy->type.len = i;    /* Length of string */
  992.         debug(F111,"gattr file type",ftbuf,i);
  993.         debug(F101,"gattr binary","",binary);
  994.         if (            /* Unknown type? */
  995.             (*ftbuf != 'A' && *ftbuf != 'B' && *ftbuf != 'I')
  996. #ifdef CK_LABELED
  997. /* ... Or our FILE TYPE is LABELED and the incoming file is text... */
  998.             || (binary == XYFT_L && *ftbuf == 'A' && !xflg)
  999. #endif /* CK_LABELED */            
  1000.             ) {
  1001.             retcode = -1;    /* Reject the file */
  1002.             *rp++ = c;
  1003.             if (!opnerr) tlog(F100," refused: type","",0);
  1004.             break;
  1005.         }
  1006. /*
  1007.   The following code moved here from opena() so we set binary mode
  1008.   as soon as requested by the attribute packet.  That way when the first
  1009.   data packet comes, the mode of transfer can be displayed correctly
  1010.   before opena() is called.
  1011. */
  1012.         if (bsavef) {        /* If somehow file mode */
  1013.             binary = bsave;    /* was saved but not restored, */
  1014.             bsavef = 0;        /* restore it. */
  1015.             debug(F101,"gattr restoring binary","",binary);
  1016.         }
  1017.         if (yy->type.val[0] == 'A') { /* Check received attributes. */
  1018.             bsave = binary;    /* ASCII.  Save global file type */
  1019.             bsavef = 1;        /* ( restore it in clsof() ) */
  1020.             binary = XYFT_T;    /* Set current type to Text. */
  1021.             debug(F100,"gattr attribute A = text","",binary); /*  */
  1022.         } else if (yy->type.val[0] == 'B') {
  1023.             bsave = binary;    /* Binary.  Save global file type */
  1024.             bsavef = 1;
  1025. #ifdef CK_LABELED
  1026.             if (binary != XYFT_L
  1027. #ifdef VMS
  1028.             && binary != XYFT_U /* VMS special case */
  1029. #endif /* VMS */
  1030.             )
  1031. #endif /* CK_LABELED */
  1032. #ifdef MAC
  1033.             if (binary != XYFT_M) /* If not MacBinary... */
  1034. #endif /* MAC */
  1035.               binary = XYFT_B;
  1036.             debug(F101,"gattr attribute B = binary","",binary);
  1037.         }
  1038.         }
  1039.         break;
  1040.  
  1041.       case '#':            /* File creation date */
  1042.         for (i = 0; (i < aln) && (i < DTBUFL); i++)
  1043.           dtbuf[i] = *s++;        /* Copy it into a static string */
  1044.         if (i < aln) s += (aln - i);
  1045.         dtbuf[i] = '\0';
  1046.         if (atdati) {
  1047.         yy->date.val = dtbuf;    /* Pointer to string */
  1048.         yy->date.len = i;    /* Length of string */
  1049.         if (fncact == XYFX_U) {    /* Receiving in update mode? */
  1050.             if (zstime(ff,yy,1) > 0) { /* Compare dates */
  1051.             *rp++ = c;    /* Discard if older, reason = date. */
  1052.             if (!opnerr) tlog(F100," refused: date","",0);
  1053.             retcode = -1;    /* Rejection notice. */
  1054.             }
  1055.         }                
  1056.         }
  1057.         break;
  1058.  
  1059.       case '(':            /* File Block Size */
  1060.         for (i = 0; (i < aln) && (i < ABUFL); i++) /* Copy it */
  1061.           abuf[i] = *s++;
  1062.         abuf[i] = '\0';        /* Terminate with null */
  1063.         if (i < aln) s += (aln - i);
  1064.         if (atblki)
  1065.           yy->blksize = atol(abuf); /* Convert to number */
  1066.         break;
  1067.  
  1068.       case '*':            /* Encoding (transfer syntax) */
  1069.         for (i = 0; (i < aln) && (i < TSBUFL); i++)
  1070.           tsbuf[i] = *s++;        /* Copy it into a static string */
  1071.         if (i < aln) s += (aln - i);
  1072.         tsbuf[i] = '\0';
  1073.         if (atenci) {
  1074.         yy->encoding.val = tsbuf; /* Pointer to string */
  1075.         yy->encoding.len = i;    /* Length of string */
  1076.         debug(F101,"gattr encoding",tsbuf,i);
  1077.         switch (*tsbuf) {
  1078. #ifndef NOCSETS
  1079.           case 'A':        /* Normal, nothing special */
  1080.             tcharset = TC_TRANSP; /* Transparent chars untranslated */
  1081.             break;
  1082.           case 'C':        /* Specified character set */
  1083.             for (i = 0; i < ntcsets; i++) { 
  1084.             if (!strcmp(tcsinfo[i].designator,tsbuf+1)) break;
  1085.             }
  1086.             debug(F101,"gattr xfer charset lookup","",i);
  1087.             if (i == ntcsets) {    /* If unknown character set, */
  1088.             debug(F110,"gattr: xfer charset unknown",tsbuf+1,0);
  1089.             if (!unkcs) {    /* and SET UNKNOWN DISCARD, */
  1090.                 retcode = -1; /* reject the file. */
  1091.                 *rp++ = c;
  1092.                 if (!opnerr)
  1093.                   tlog(F100," refused: character set","",0);
  1094.             }
  1095.             } else {
  1096.             tcharset = tcsinfo[i].code; /* if known, use it */
  1097.             rx = xlr[tcharset][fcharset]; /* xlation function */
  1098.             }
  1099.             debug(F101,"gattr tcharset","",tcharset);
  1100.         break;
  1101. #endif /* NOCSETS */
  1102.           default:            /* Something else. */
  1103.         debug(F110,"gattr unk encoding attribute",tsbuf,0);
  1104.         if (!unkcs) {        /* If SET UNK DISC */
  1105.             retcode = -1;
  1106.             *rp++ = c;
  1107.             if (!opnerr) tlog(F100," refused: encoding","",0);
  1108.         }
  1109.         break;
  1110.         }
  1111.         }
  1112.         break;
  1113.  
  1114.       case '+':            /* Disposition */
  1115. #ifdef DYNAMIC
  1116.         if (!dsbuf)
  1117.         if ((dsbuf = malloc(DSBUFL+1)) == NULL)
  1118.             fatal("gtattr: no memory for dsbuf");
  1119. #endif /* DYNAMIC */
  1120.         for (i = 0; (i < aln) && (i < DSBUFL); i++)
  1121.           dsbuf[i] = *s++;        /* Copy it into a separate string */
  1122.         dsbuf[i] = '\0';
  1123.         if (i < aln) s += (aln - i);
  1124.         rs_len = 0;
  1125.         if (atdisi) {
  1126.         yy->disp.val = dsbuf;    /* Pointer to string */
  1127.         yy->disp.len = i;    /* Length of string */
  1128.         d = *dsbuf;
  1129. #ifndef NODISPO
  1130. /*
  1131.   Define NODISPO to disable receipt of mail or print files and of RESEND.
  1132. */
  1133.         if (
  1134. #ifndef datageneral            /* MAIL supported only for */
  1135. #ifndef OS2                /* UNIX, VMS, and OS-9 */
  1136. #ifndef MAC
  1137. #ifndef GEMDOS
  1138. #ifndef AMIGA
  1139.             d != 'M' &&        /* MAIL */
  1140. #endif /* AMIGA */
  1141. #endif /* GEMDOS */
  1142. #endif /* MAC */
  1143. #endif /* OS/2 */
  1144. #endif /* datageneral */
  1145. #ifdef CK_RESEND
  1146.             d != 'R' &&        /* RESEND */
  1147. #endif /* CK_RESEND */
  1148.             d != 'P') {        /* PRINT */
  1149.             retcode = -1;
  1150.             *rp++ = c;
  1151.             if (!opnerr) tlog(F100," refused: disposition","",0);
  1152.         }
  1153.         if (d == 'R') {        /* File is being resent */
  1154. #ifdef CK_RESEND
  1155.             rs_len = zchki(ff); /* Get length of file */
  1156.             debug(F111,"gattr RESEND",ff,rs_len);
  1157. #ifdef VMS
  1158.             rs_len &= (long) -512; /* Ensure block boundary if VMS */
  1159.             rs_len -= 512;      /* In case last block not complete */
  1160.             debug(F111,"gattr rs_len",ff,rs_len);
  1161. #endif /* VMS */
  1162.             if (rs_len < 0L) rs_len = 0L;
  1163.             if (rs_len > 0L) {
  1164.             fncsav = fncact; /* Save collision action */
  1165.             fncact = XYFX_A; /* Switch to APPEND */
  1166.             }
  1167. #else
  1168.             retcode = -1;    /* This shouldn't happen */
  1169.             *rp++ = c;        /* 'cause it wasn't negotiated. */
  1170.             if (!opnerr) tlog(F100," refused: resend","",0);
  1171. #endif /* CK_RESEND */
  1172.         }
  1173. #else  /* NODISPO */
  1174.         retcode = -1;
  1175.         *rp++ = c;
  1176.         if (!opnerr) tlog(F100," refused: resend","",0);
  1177. #endif /* NODISPO */
  1178.         }
  1179.         break;
  1180.  
  1181.       case '.':            /* Sender's system ID */
  1182.         for (i = 0; (i < aln) && (i < IDBUFL); i++)
  1183.           idbuf[i] = *s++;        /* Copy it into a static string */
  1184.         idbuf[i] = '\0';
  1185.         if (i < aln) s += (aln - i);
  1186.         if (atsidi) {
  1187.         yy->systemid.val = idbuf; /* Pointer to string */
  1188.         yy->systemid.len = i;    /* Length of string */
  1189.         }
  1190.         break;
  1191.  
  1192.       case '0':            /* System-dependent parameters */
  1193. #ifdef DYNAMIC
  1194.         if (!spbuf && !(spbuf = malloc(SPBUFL)))
  1195.         fatal("gattr: no memory for spbuf");
  1196. #endif /* DYNAMIC */
  1197.         for (i = 0; (i < aln) && (i < SPBUFL); i++)
  1198.           spbuf[i] = *s++;        /* Copy it into a static string */
  1199.         spbuf[i] = '\0';
  1200.         if (i < aln) s += (aln - i);
  1201.         if (atsysi) {
  1202.         yy->sysparam.val = spbuf; /* Pointer to string */
  1203.         yy->sysparam.len = i;    /* Length of string */
  1204.         }
  1205.         break;
  1206.  
  1207.       case '1':            /* File length in bytes */
  1208.         for (i = 0; (i < aln) && (i < ABUFL); i++) /* Copy it */
  1209.           abuf[i] = *s++;
  1210.         abuf[i] = '\0';        /* Terminate with null */
  1211.         if (i < aln) s += (aln - i);
  1212.         yy->length = atol(abuf);    /* Convert to number */
  1213.         debug(F111,"gattr length",abuf,(int) yy->length);
  1214.         break;
  1215.  
  1216. #ifdef STRATUS
  1217.       case '/':            /* Format of data in packets */
  1218.         /* We just copy it into the record and let the fio module */
  1219.         /* figure out what to do with it. */
  1220.         for (i = 0; (i < aln) && (i < ABUFL); i++) /* Copy it */
  1221.           abuf[i] = *s++;
  1222.         abuf[i] = '\0';        /* Terminate with null */
  1223.         if (atsysi) {        /* same switch as OS-DEPENDENT */
  1224.         yy->recfm.val = spbuf;    /* Pointer to string */
  1225.         yy->recfm.len = i;    /* Length of string */
  1226.         }
  1227.         break;
  1228. #endif /* STRATUS */
  1229.  
  1230.       default:            /* Unknown attribute */
  1231.         s += aln;            /* Just skip past it */
  1232.         break;
  1233.     }
  1234.     }
  1235.  
  1236.     /* Check file length now, because we also need to know the file type */
  1237.     /* in case zchkspa() differentiates text and binary (VMS version does) */
  1238.  
  1239.     if (atleni) {            /* Length attribute enabled? */
  1240.     if (yy->length > -1L) {        /* Length-in-bytes attribute rec'd? */
  1241.         if (!zchkspa(ff,(yy->length))) { /* Check space */
  1242.         retcode = -1;             /* Not enuf */
  1243.         *rp++ = '1';
  1244.         if (!opnerr) tlog(F100," refused: length","",0);
  1245.         }
  1246.     } else if (yy->lengthk > -1L) {    /* Length in K attribute rec'd? */
  1247.         if (!zchkspa(ff,(yy->lengthk * 1024))) {
  1248.         retcode = -1;        /* Check space */
  1249.         *rp++ = '!';
  1250.         if (!opnerr) tlog(F100," refused: length","",0);
  1251.         }
  1252.     }
  1253.     }
  1254.     if (yy->length > -1L) {        /* Remember the file size */
  1255.     fsize = yy->length;        
  1256.     } else if (yy->lengthk > -1L) {
  1257.     fsize = yy->lengthk * 1024L;
  1258.     } else fsize = -1L;
  1259.  
  1260. #ifdef DEBUG
  1261.     if (deblog) {
  1262.     sprintf(abuf,"%ld",fsize);
  1263.     debug(F110,"gattr fsize",abuf,0);
  1264.     }
  1265. #endif /* DEBUG */
  1266.     if (retcode == 0) rp = rpbuf;    /* Null reply string if accepted */
  1267.     *rp = '\0';                /* End of reply string */
  1268. #ifdef CK_RESEND
  1269.     if (d == 'R') {            /* Receiving a RESEND? */
  1270.     debug(F101,"gattr RESEND","",retcode);
  1271.     /* We ignore retcodes because this overrides */
  1272.     if (binary != XYFT_B) {        /* Reject if not binary */
  1273.         retcode = -1;        /* in case type field came */
  1274.         strcpy(rpbuf,"N+");        /* after the disposition field */
  1275.         debug(F111,"gattr RESEND not binary",rpbuf,binary);
  1276.     } else {            /* Binary mode */
  1277.         retcode = 0;        /* Accept the file */
  1278.         sprintf(rpbuf+2,"%ld",rs_len); /* Reply with length of file */
  1279.         rpbuf[0] = '1';        /* '1' means Length in Bytes */
  1280.         rpbuf[1] = tochar((int)strlen(rpbuf+2)); /* Length of length */
  1281.         debug(F111,"gattr RESEND OK",rpbuf,retcode);
  1282.     }
  1283.     }
  1284. #endif /* CK_RESEND */
  1285.     if (retcode == 0 && discard != 0) {    /* Do we still have a discard flag? */
  1286.     strcpy(rpbuf,"N?");        /* Yes, must be filename collision */
  1287.     retcode = -1;            /* "?" = name (reply-only code) */
  1288.     }
  1289.     yy->reply.val = rpbuf;        /* Add it to attribute structure */
  1290.     yy->reply.len = (int)strlen(rpbuf);
  1291.     if (retcode < 0) {            /* If we are rejecting */
  1292.     discard = 1;            /* remember to discard the file */
  1293.     rejection = rpbuf[1];        /* and use the first reason given. */
  1294.     }
  1295.     debug(F111,"gattr return",rpbuf,retcode);
  1296.     return(retcode);
  1297. }
  1298.  
  1299. /*  I N I T A T T R  --  Initialize file attribute structure  */
  1300.  
  1301. int
  1302. initattr(yy) struct zattr *yy; {
  1303.     yy->lengthk = yy->length = -1L;
  1304.     yy->type.val = "";
  1305.     yy->type.len = 0;
  1306.     yy->date.val = "";
  1307.     yy->date.len = 0;
  1308.     yy->encoding.val = "";
  1309.     yy->encoding.len = 0;
  1310.     yy->disp.val = "";
  1311.     yy->disp.len = 0;
  1312.     yy->systemid.val = "";
  1313.     yy->systemid.len = 0;
  1314.     yy->sysparam.val = "";
  1315.     yy->sysparam.len = 0;
  1316.     yy->creator.val = "";
  1317.     yy->creator.len = 0;
  1318.     yy->account.val = "";
  1319.     yy->account.len = 0;
  1320.     yy->area.val = "";
  1321.     yy->area.len = 0;
  1322.     yy->password.val = "";
  1323.     yy->password.len = 0;
  1324.     yy->blksize = -1L;
  1325.     yy->access.val = "";
  1326.     yy->access.len = 0;
  1327.     yy->lprotect.val = "";
  1328.     yy->lprotect.len = 0;
  1329.     yy->gprotect.val = "";
  1330.     yy->gprotect.len = 0;
  1331.     yy->recfm.val = "";
  1332.     yy->recfm.len = 0;
  1333.     yy->reply.val = "";
  1334.     yy->reply.len = 0;
  1335.     return(0);
  1336. }
  1337.  
  1338. /*  A D E B U -- Write attribute packet info to debug log  */
  1339.  
  1340. int
  1341. adebu(f,zz) char *f; struct zattr *zz; {
  1342. #ifdef DEBUG
  1343.     if (deblog == 0) return(0);
  1344.     debug(F110,"Attributes for incoming file ",f,0);
  1345.     debug(F101," length in K","",(int) zz->lengthk);
  1346.     debug(F111," file type",zz->type.val,zz->type.len);
  1347.     debug(F111," creation date",zz->date.val,zz->date.len);
  1348.     debug(F111," creator",zz->creator.val,zz->creator.len);
  1349.     debug(F111," account",zz->account.val,zz->account.len);
  1350.     debug(F111," area",zz->area.val,zz->area.len);
  1351.     debug(F111," password",zz->password.val,zz->password.len);
  1352.     debug(F101," blksize","",(int) zz->blksize);
  1353.     debug(F111," access",zz->access.val,zz->access.len);
  1354.     debug(F111," encoding",zz->encoding.val,zz->encoding.len);
  1355.     debug(F111," disposition",zz->disp.val,zz->disp.len);
  1356.     debug(F111," lprotection",zz->lprotect.val,zz->lprotect.len);
  1357.     debug(F111," gprotection",zz->gprotect.val,zz->gprotect.len);
  1358.     debug(F111," systemid",zz->systemid.val,zz->systemid.len);
  1359.     debug(F111," recfm",zz->recfm.val,zz->recfm.len);
  1360.     debug(F111," sysparam",zz->sysparam.val,zz->sysparam.len);
  1361.     debug(F101," length","",(int) zz->length);
  1362.     debug(F110," reply",zz->reply.val,0);
  1363. #endif /* DEBUG */
  1364.     return(0);
  1365. }
  1366.  
  1367. /*  O P E N A -- Open a file, with attributes.  */
  1368. /*
  1369.   This function tries to open a new file to put the arriving data in.  The
  1370.   filename is the one in the srvcmd buffer.  File collision actions are:
  1371.   OVERWRITE (the existing file is overwritten), RENAME (the new file is
  1372.   renamed), BACKUP (the existing file is renamed), DISCARD (the new file is
  1373.   refused), UPDATE (the incoming file replaces the existing file only if the
  1374.   incoming file has a newer creation date).
  1375.  
  1376.   Returns 0 on failure, nonzero on success.
  1377. */
  1378. extern char *rf_err;
  1379.  
  1380. int
  1381. opena(f,zz) char *f; struct zattr *zz; {
  1382.     int x, dispos = 0;
  1383.     static struct filinfo fcb;        /* Must be static! */
  1384.  
  1385.     debug(F110,"opena f",f,0);
  1386.     debug(F101,"opena discard","",discard);
  1387.  
  1388.     adebu(f,zz);            /* Write attributes to debug log */
  1389.  
  1390.     ffc = 0L;                /* Init file-character counter */
  1391.  
  1392.     /* Set up file control structure */
  1393.  
  1394.     fcb.bs = fblksiz;            /* Blocksize */
  1395. #ifndef NOCSETS
  1396.     fcb.cs = fcharset;            /* Character set */
  1397. #else
  1398.     fcb.cs = 0;                /* Character set */
  1399. #endif /* NOCSETS */
  1400.     fcb.rl = frecl;            /* Record Length */
  1401.     fcb.fmt = frecfm;            /* Record Format */
  1402.     fcb.org = forg;            /* Organization */
  1403.     fcb.cc = fcctrl;            /* Carriage control */
  1404.     fcb.typ = binary;            /* Type */
  1405.     fcb.dsp = (fncact == XYFX_A) ? XYFZ_A : XYFZ_N; /* Disposition */
  1406.     fcb.os_specific = '\0';        /* OS-specific info */
  1407. #ifdef CK_LABELED
  1408.     fcb.lblopts = lf_opts;        /* Labeled file options */
  1409. #else
  1410.     fcb.lblopts = 0;
  1411. #endif /* CK_LABELED */
  1412.  
  1413.     if (zz->disp.len > 0) {        /* Incoming file has a disposition? */
  1414.     debug(F111,"open disposition",zz->disp.val,zz->disp.len);
  1415.     dispos = (int) (*(zz->disp.val));
  1416.     }
  1417.     debug(F101,"opena dispos","",dispos);
  1418.  
  1419.     if (!dispos) {                     /* No special disposition? */
  1420.     if (fncact == XYFX_B && ofn1x && ofn2) { /* File collision = BACKUP? */
  1421.         if (zrename(ofn1,ofn2) < 0) {        /* Rename existing file. */
  1422.         debug(F110,"opena rename fails",ofn1,0);
  1423.         rf_err = "Can't create backup file";
  1424.         return(0);
  1425.         } else debug(F110,"opena rename ok",ofn2,0);
  1426.     }
  1427.     } else if (dispos == 'R') {        /* Receiving a RESEND */
  1428. #ifdef COMMENT
  1429.         if (fncact == XYFX_R)        /* and file collision = RENAME */
  1430.       if (ofn1x)
  1431. #endif
  1432.         f = ofn1;            /* use original name. */
  1433.     }
  1434.     debug(F111,"opena [file]=mode: ",f,fcb.dsp);
  1435.     if (x = openo(f,zz,&fcb)) {        /* Try to open the file. */
  1436.     tlog(F110," local name:",f,0L);    /* OK, open, record name. */
  1437.     if (binary) {            /* Log file mode in transaction log */
  1438.         tlog(F101," mode: binary","",(long) binary);
  1439.     } else {            /* If text mode, check character set */
  1440.         tlog(F100," mode: text","",0L);
  1441. #ifndef NOCSETS
  1442.         tlog(F110," file character-set",fcsinfo[fcharset].name,0L);
  1443.         if (tcharset == TC_TRANSP)
  1444.           tlog(F110," xfer character-set","transparent",0L);
  1445.         else
  1446.           tlog(F110," xfer character-set",tcsinfo[tcharset].name,0L);
  1447. #endif /* NOCSETS */
  1448.         debug(F111," opena charset",zz->encoding.val,zz->encoding.len);
  1449.     }
  1450.     if (fsize > -1L) screen(SCR_FS,0,fsize,"");
  1451.  
  1452. #ifdef datageneral
  1453. /* Need to turn on multi-tasking console interrupt task here, since multiple */
  1454. /* files may be received. */
  1455.         if ((local) && (!quiet))        /* Only do this if local & not quiet */
  1456.             consta_mt();                /* Start the asynch read task */
  1457. #endif /* datageneral */
  1458.  
  1459.     } else {                /* Did not open file OK. */
  1460. #ifdef ATTSV
  1461.     extern int errno;
  1462. #ifdef BSD44
  1463. #ifdef __386BSD__
  1464. #ifndef NDSYSERRLIST
  1465.     extern char *sys_errlist[];
  1466. #endif /* NDSYSERRLIST */
  1467. #else
  1468. #ifndef __bsdi__
  1469. #ifndef NDSYSERRLIST
  1470.     extern const char *const sys_errlist[];
  1471. #endif /* NDSYSERRLIST */
  1472. #endif /* __bsdi__ */
  1473. #endif /* __386BSD__ */
  1474.     char *e;
  1475.     e = (char *) sys_errlist[errno];
  1476.     screen(SCR_EM,0,0l,e);
  1477. #else /* !BSD44 */
  1478. #ifndef NDSYSERRLIST
  1479.     extern char *sys_errlist[];
  1480. #endif /* NDSYSERRLIST */
  1481.     screen(SCR_EM,0,0l,sys_errlist[errno]);
  1482. #endif /* BSD44 */
  1483.  
  1484. #else /* !ATTSV */
  1485.  
  1486. #ifdef BSD4
  1487. #ifndef NDSYSERRLIST
  1488.     extern char *sys_errlist[];
  1489. #endif /* NDSYSERRLIST */
  1490.     extern int errno;
  1491.     screen(SCR_EM,0,0l,sys_errlist[errno]);
  1492. #else
  1493.     screen(SCR_EM,0,0l,"Can't open output file");
  1494. #endif /* BSD4 */
  1495. #endif /* ATTSV */
  1496.  
  1497.         tlog(F110,"Failure to open",f,0L);
  1498.     }
  1499.     return(x);                /* Pass on return code from openo */
  1500. }
  1501.  
  1502. /*  C A N N E D  --  Check if current file transfer cancelled */
  1503.  
  1504. int
  1505. canned(buf) CHAR *buf; {
  1506.     if (*buf == 'X') cxseen = 1;
  1507.     if (*buf == 'Z') czseen = 1;
  1508.     debug(F101,"canned: cxseen","",cxseen);
  1509.     debug(F101," czseen","",czseen);
  1510.     return((czseen || cxseen) ? 1 : 0);
  1511. }
  1512.  
  1513.  
  1514. /*  O P E N I  --  Open an existing file for input  */
  1515.  
  1516. int
  1517. openi(name) char *name; {
  1518.     int x, filno;
  1519.     char *name2;
  1520.  
  1521.     if (memstr) return(1);        /* Just return if file is memory. */
  1522.  
  1523.     debug(F110,"openi",name,0);
  1524.     debug(F101," sndsrc","",sndsrc);
  1525.  
  1526.     filno = (sndsrc == 0) ? ZSTDIO : ZIFILE;    /* ... */
  1527.  
  1528.     debug(F101," file number","",filno);
  1529.  
  1530.     if (server && !en_cwd) {        /* If running as server */
  1531.     zstrip(name,&name2);        /* and CWD is disabled... */
  1532.     if (                /* check if pathname was included. */
  1533. #ifdef VMS
  1534.     zchkpath(name)
  1535. #else
  1536.     strcmp(name,name2)
  1537. #endif /* VMS */
  1538.         ) {
  1539.         tlog(F110,name,"authorization failure",0L);
  1540.         debug(F110," openi authorization failure",name,0);
  1541.         return(0);
  1542.     } else name = name2;
  1543.     }
  1544.     if (zopeni(filno,name)) {        /* Otherwise, try to open it. */
  1545.     debug(F110," ok",name,0);
  1546.         return(1);
  1547.     } else {                /* If not found, */
  1548.     char xname[100];        /* convert the name */
  1549.     zrtol(name,xname);        /* to local form and then */
  1550.     x = zopeni(filno,xname);    /* try opening it again. */
  1551.     debug(F101," zopeni","",x);
  1552.     if (x) {
  1553.         debug(F110," ok",xname,0);
  1554.         return(1);            /* It worked. */
  1555.         } else {
  1556. #ifdef COMMENT
  1557.         screen(SCR_EM,0,0l,"Can't open file");  /* It didn't work. */
  1558. #endif /* COMMENT */
  1559.         tlog(F110,xname,"could not be opened",0L);
  1560.         debug(F110," openi failed",xname,0);
  1561.         return(0);
  1562.         }
  1563.     }
  1564. }
  1565.  
  1566. /*  O P E N O  --  Open a new file for output.  */
  1567.  
  1568. static int isopen = 0;
  1569.  
  1570. int
  1571. openo(name,zz,fcb) char *name; struct zattr *zz; struct filinfo *fcb; {
  1572.     char *name2;
  1573.  
  1574.     if (stdouf)                /* Receiving to stdout? */
  1575.       return(zopeno(ZSTDIO,"",zz,NULL));
  1576.  
  1577.     debug(F110,"openo: name",name,0);
  1578.  
  1579.     if (cxseen || czseen || discard) {    /* If interrupted, get out before */
  1580.     debug(F100," open cancelled","",0); /* destroying existing file. */
  1581.     return(1);            /* Pretend to succeed. */
  1582.     }
  1583.     if (server && !en_cwd) {        /* If running as server */
  1584.     zstrip(name,&name2);        /* and CWD is disabled, */
  1585.     if (strcmp(name,name2)) {    /* check if pathname was included. */
  1586.         tlog(F110,name,"authorization failure",0L);
  1587.         debug(F110," openo authorization failure",name,0);
  1588.         return(0);
  1589.     } else name = name2;
  1590.     }
  1591.  
  1592.     if (zopeno(ZOFILE,name,zz,fcb) == 0) { /* Try to open the file */
  1593.     isopen = 0;
  1594.     debug(F110,"openo failed",name,0);
  1595.     tlog(F110,"Failure to open",name,0L);
  1596.     return(0);
  1597.     } else {
  1598.     isopen = 1;
  1599.     debug(F110,"openo ok, name",name,0);
  1600.     return(1);
  1601.     }
  1602. }
  1603.  
  1604. /*  O P E N T  --  Open the terminal for output, in place of a file  */
  1605.  
  1606. int
  1607. opent(zz) struct zattr *zz; {
  1608.     ffc = tfc = 0L;
  1609.     if (bsavef) {            /* If somehow file mode */
  1610.     binary = bsave;            /* was saved but not restored, */
  1611.     bsavef = 0;            /* restore it. */
  1612.     debug(F101,"opena restoring binary","",binary);
  1613.     }
  1614.     bsave = binary;
  1615.     bsavef = 1;
  1616.     binary = XYFT_T;
  1617.     return(zopeno(ZCTERM,"",zz,NULL));
  1618. }
  1619.  
  1620. /*  C L S I F  --  Close the current input file. */
  1621.  
  1622. int
  1623. clsif() {
  1624.     int x = 0;
  1625. #ifdef datageneral
  1626.     if ((local) && (!quiet))    /* Only do this if local & not quiet */
  1627.         if (nfils < 1)          /* More files to send ... leave it on! */
  1628.             connoi_mt();
  1629. #endif /* datageneral */
  1630.     if (memstr) {            /* If input was memory string, */
  1631.     memstr = 0;            /* indicate no more. */
  1632.     } else x = zclose(ZIFILE);        /* else close input file. */
  1633.     if (cxseen || czseen)        /* If interrupted */
  1634.       screen(SCR_ST,ST_INT,0l,"");    /* say so */
  1635.     else if (discard)            /* If I'm refusing */
  1636.       screen(SCR_ST,ST_REFU,0l,refused); /* say why */
  1637.     else {                /* Otherwise */
  1638.     fstats();            /* update statistics */
  1639.     screen(SCR_ST,ST_OK,0l,"");    /* and say transfer was OK */
  1640.     }
  1641.     hcflg = 0;                /* Reset flags */
  1642.     sendstart = 0L;            /* Don't do this again! */
  1643. #ifdef COMMENT
  1644. /*
  1645.   This prevents a subsequent call to clsof() from deleting the file
  1646.   when given the discard flag.
  1647. */
  1648.     *filnam = '\0';            /* and current file name */
  1649. #endif /* COMMENT */
  1650.     return(x);
  1651. }
  1652.  
  1653.  
  1654. /*  C L S O F  --  Close an output file.  */
  1655.  
  1656. /*  Call with disp != 0 if file is to be discarded.  */
  1657. /*  Returns -1 upon failure to close, 0 or greater on success. */
  1658.  
  1659. int
  1660. clsof(disp) int disp; {
  1661.     int x;
  1662.  
  1663.     debug(F101,"clsof disp","",disp);
  1664.     if (bsavef) {            /* If we saved global file type */
  1665.     debug(F101,"clsof restoring binary","",binary);
  1666.     binary = bsave;            /* restore it */
  1667.     bsavef = 0;            /* only this once. */
  1668.     }
  1669.     if (fncsav != -1) {            /* Saved file collision action... */
  1670.     fncact = fncsav;        /* Restore it. */
  1671.     fncsav = -1;            /* Unsave it. */
  1672.     }
  1673. #ifdef datageneral
  1674.     if ((local) && (!quiet))        /* Only do this if local & not quiet */
  1675.         connoi_mt();
  1676. #endif /* datageneral */
  1677.     if ((x = zclose(ZOFILE)) < 0) {    /* Try to close the file */
  1678.     tlog(F100,"Failure to close",filnam,0L);
  1679.     screen(SCR_ST,ST_ERR,0l,"");
  1680.     } else if (disp) {            /* Interrupted or refused */
  1681.     if (keep == 0) {        /* If not keeping incomplete files */
  1682.         if (isopen &&        /* AND the file is actually open */
  1683.         *filnam && (what & W_RECV)) /* AND we're receiving!!!... */
  1684.           zdelet(filnam);            /* ONLY THEN, delete it */
  1685.         debug(F100,"Incomplete: discarded","",0);
  1686.         tlog(F100," incomplete: discarded","",0L);
  1687.         screen(SCR_ST,ST_DISC,0l,"");
  1688.     } else {            /* Keep incomplete copy */
  1689.         fstats();
  1690.         if (!discard) { /* Unless discarding for other reason... */
  1691.         debug(F100,"Incomplete: Kept","",0);
  1692.         tlog(F100," incomplete: kept","",0L);
  1693.         }
  1694.         screen(SCR_ST,ST_INC,0l,"");
  1695.     }
  1696.     } else {                /* Nothing wrong, just keep it */
  1697.     debug(F100,"Closed","",0);    /* and give comforting messages. */
  1698.     fstats();
  1699.     screen(SCR_ST,ST_OK,0L,"");
  1700.     }
  1701.     rs_len = 0;
  1702.     isopen = 0;                /* It's not open any more. */
  1703.     cxseen = 0;                /* Reset per-file interruption flag */
  1704.     return(x);                /* Send back zclose() return code. */
  1705. }
  1706.  
  1707. #ifdef SUNOS4S5
  1708. tolower(c) char c; { return((c)-'A'+'a'); }
  1709. toupper(c) char c; { return((c)-'a'+'A'); }
  1710. #endif /* SUNOS4S5 */
  1711.  
  1712.