home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / old / misc / ck5a189 / ckcfn2.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  53KB  |  1,627 lines

  1. /*  C K C F N 2  --  System-independent Kermit protocol support functions... */
  2.  
  3. /*  ...Part 2 (continued from ckcfns.c)  */
  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, 1993, 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.  Note -- if you change this file, please amend the version number and date at
  19.  the top of ckcfns.c accordingly.
  20. */
  21.  
  22. #include "ckcsym.h"            /* Compilation options */
  23. #include "ckcdeb.h"            /* Debugging and other symbols */
  24. #include "ckcasc.h"            /* ASCII symbols */
  25. #include "ckcker.h"            /* Kermit symbols */
  26. #include "ckcxla.h"            /* Translation */
  27.  
  28. #ifdef TCPSOCKET            /* For TELNET business in spack() */
  29. #ifndef NP_TELNET
  30. #define NP_TELNET 1
  31. extern int tn_nlm, ttnproto;
  32. #endif /* NP_TELNET */
  33. #endif /* TCPSOCKET */
  34.  
  35. #ifdef DYNAMIC
  36. extern struct pktinfo *s_pkt;        /* array of pktinfo structures */
  37. extern struct pktinfo *r_pkt;        /* array of pktinfo structures */
  38. #else
  39. extern struct pktinfo s_pkt[];        /* array of pktinfo structures */
  40. extern struct pktinfo r_pkt[];        /* array of pktinfo structures */
  41. #endif /* DYNAMIC */
  42.  
  43. extern int sseqtbl[], rseqtbl[], sbufuse[], sacktbl[], wslots, winlo,
  44.   sbufnum, rbufnum;
  45.  
  46. extern int ttprty;            /* from ckutio.c */
  47. extern int autopar;
  48.  
  49. extern int spsiz, spmax, rpsiz, timint, timef, npad;
  50. extern int rpt, rptq, rptflg, capas, spsizf, en_fin, tsecs, network, flow;
  51. extern int pktnum, sndtyp, rcvtyp, bctr, bctu, bctl, rsn, rln, maxtry, size;
  52. extern int osize, maxsize, spktl, rpktl, nfils, stdouf, warn, parity;
  53. extern int turn, turnch,  delay, displa, pktlog, tralog, seslog, xflg, mypadn;
  54. extern int hcflg, local, server, cxseen, czseen;
  55. extern int nakstate, quiet, success, xitsta, what;
  56. extern int spackets, rpackets, timeouts, retrans, crunched, urpsiz;
  57.  
  58. extern long filcnt, ffc, flci, flco, tlci, tlco, tfc, speed;
  59.  
  60. extern char *cmarg, *cmarg2, filnam[], *hlptxt;
  61.  
  62. extern CHAR padch, mypadc, eol, seol, ctlq, sstate;
  63. extern CHAR *recpkt, *data, encbuf[], myinit[];
  64. extern CHAR *srvptr, stchr, mystch, *rdatap;
  65. extern CHAR padbuf[];
  66. #ifdef DYNAMIC
  67.   extern CHAR *srvcmd;
  68. #else
  69.   extern CHAR srvcmd[];
  70. #endif /* DYNAMIC */
  71.  
  72. int numerrs = 0;        /* Number of packet errors so far */
  73.  
  74. static CHAR partab[] = {        /* Even parity table for dopar(). */
  75.     (CHAR) '\000',            /* ANSI C casts '\ooo' constants  */
  76.     (CHAR) '\201',            /* to signed char, so we have to  */
  77.     (CHAR) '\202',            /* cast back to unsigned char...  */
  78.     (CHAR) '\003',
  79.     (CHAR) '\204',
  80.     (CHAR) '\005',
  81.     (CHAR) '\006',
  82.     (CHAR) '\207',
  83.     (CHAR) '\210',
  84.     (CHAR) '\011',
  85.     (CHAR) '\012',
  86.     (CHAR) '\213',
  87.     (CHAR) '\014',
  88.     (CHAR) '\215',
  89.     (CHAR) '\216',
  90.     (CHAR) '\017',
  91.     (CHAR) '\220',
  92.     (CHAR) '\021',
  93.     (CHAR) '\022',
  94.     (CHAR) '\223',
  95.     (CHAR) '\024',
  96.     (CHAR) '\225',
  97.     (CHAR) '\226',
  98.     (CHAR) '\027',
  99.     (CHAR) '\030',
  100.     (CHAR) '\231',
  101.     (CHAR) '\232',
  102.     (CHAR) '\033',
  103.     (CHAR) '\234',
  104.     (CHAR) '\035',
  105.     (CHAR) '\036',
  106.     (CHAR) '\237',
  107.     (CHAR) '\240',
  108.     (CHAR) '\041',
  109.     (CHAR) '\042',
  110.     (CHAR) '\243',
  111.     (CHAR) '\044',
  112.     (CHAR) '\245',
  113.     (CHAR) '\246',
  114.     (CHAR) '\047',
  115.     (CHAR) '\050',
  116.     (CHAR) '\251',
  117.     (CHAR) '\252',
  118.     (CHAR) '\053',
  119.     (CHAR) '\254',
  120.     (CHAR) '\055',
  121.     (CHAR) '\056',
  122.     (CHAR) '\257',
  123.     (CHAR) '\060',
  124.     (CHAR) '\261',
  125.     (CHAR) '\262',
  126.     (CHAR) '\063',
  127.     (CHAR) '\264',
  128.     (CHAR) '\065',
  129.     (CHAR) '\066',
  130.     (CHAR) '\267',
  131.     (CHAR) '\270',
  132.     (CHAR) '\071',
  133.     (CHAR) '\072',
  134.     (CHAR) '\273',
  135.     (CHAR) '\074',
  136.     (CHAR) '\275',
  137.     (CHAR) '\276',
  138.     (CHAR) '\077',
  139.     (CHAR) '\300',
  140.     (CHAR) '\101',
  141.     (CHAR) '\102',
  142.     (CHAR) '\303',
  143.     (CHAR) '\104',
  144.     (CHAR) '\305',
  145.     (CHAR) '\306',
  146.     (CHAR) '\107',
  147.     (CHAR) '\110',
  148.     (CHAR) '\311',
  149.     (CHAR) '\312',
  150.     (CHAR) '\113',
  151.     (CHAR) '\314',
  152.     (CHAR) '\115',
  153.     (CHAR) '\116',
  154.     (CHAR) '\317',
  155.     (CHAR) '\120',
  156.     (CHAR) '\321',
  157.     (CHAR) '\322',
  158.     (CHAR) '\123',
  159.     (CHAR) '\324',
  160.     (CHAR) '\125',
  161.     (CHAR) '\126',
  162.     (CHAR) '\327',
  163.     (CHAR) '\330',
  164.     (CHAR) '\131',
  165.     (CHAR) '\132',
  166.     (CHAR) '\333',
  167.     (CHAR) '\134',
  168.     (CHAR) '\335',
  169.     (CHAR) '\336',
  170.     (CHAR) '\137',
  171.     (CHAR) '\140',
  172.     (CHAR) '\341',
  173.     (CHAR) '\342',
  174.     (CHAR) '\143',
  175.     (CHAR) '\344',
  176.     (CHAR) '\145',
  177.     (CHAR) '\146',
  178.     (CHAR) '\347',
  179.     (CHAR) '\350',
  180.     (CHAR) '\151',
  181.     (CHAR) '\152',
  182.     (CHAR) '\353',
  183.     (CHAR) '\154',
  184.     (CHAR) '\355',
  185.     (CHAR) '\356',
  186.     (CHAR) '\157',
  187.     (CHAR) '\360',
  188.     (CHAR) '\161',
  189.     (CHAR) '\162',
  190.     (CHAR) '\363',
  191.     (CHAR) '\164',
  192.     (CHAR) '\365',
  193.     (CHAR) '\366',
  194.     (CHAR) '\167',
  195.     (CHAR) '\170',
  196.     (CHAR) '\371',
  197.     (CHAR) '\372',
  198.     (CHAR) '\173',
  199.     (CHAR) '\374',
  200.     (CHAR) '\175',
  201.     (CHAR) '\176',
  202.     (CHAR) '\377'
  203. };
  204.  
  205. /* CRC generation tables */
  206.  
  207. static long crcta[16] = { 0L, 010201L, 020402L, 030603L, 041004L,
  208.   051205L, 061406L, 071607L, 0102010L, 0112211L, 0122412L, 0132613L, 0143014L,
  209.   0153215L, 0163416L, 0173617L };
  210.  
  211. static long crctb[16] = { 0L, 010611L, 021422L, 031233L, 043044L,
  212.   053655L, 062466L, 072277L, 0106110L, 0116701L, 0127532L, 0137323L, 0145154L,
  213.   0155745L, 0164576L, 0174367L };
  214.  
  215.  
  216. /*  I N P U T  --  Attempt to read packet number 'pktnum'.  */
  217.  
  218. /*
  219.  This is the function that feeds input to Kermit's finite state machine,
  220.  in the form of a character in the range 32-126, normally a packet type
  221.  (uppercase letter) or pseudo-packet-type (lowercase letter).
  222.  
  223.  If a special start state is in effect, that state is returned as if it were
  224.  the type of an incoming packet.
  225. */
  226. int
  227. input() {
  228.     int type;                /* Received packet type */
  229.     int x = 0, y, k;            /* Workers */
  230.     int z, pi, nf;            /* Worker, packet index, NAK flag */
  231.  
  232.     debug(F101,"input sstate","",sstate);
  233.     debug(F101," nakstate","",nakstate);
  234.     debug(F000," sndtyp","",sndtyp);
  235.  
  236.     while (1) {                /* Big loop... */
  237.  
  238.     if (sstate != 0) {        /* If a start state is in effect, */
  239.         type = sstate;        /* return it like a packet type, */
  240.         sstate = 0;            /* and then nullify it. */
  241.         numerrs = 0;        /* (PWP) no errors so far */
  242.         return(type);
  243.     }
  244.     
  245.     if (nakstate) {            /* This section for file receiver. */
  246.  
  247.         if (wslots > 1) {        /* If we're doing windows, */
  248.         x = rseqtbl[winlo];    /* see if desired packet already in. */
  249.         debug(F101," winlo","",winlo);
  250.         debug(F101," rseqtbl[winlo]","",rseqtbl[winlo]);
  251.         if (x > -1) {        /* Already there? */
  252.             if (r_pkt[x].pk_seq == winlo) { /* (double check) */
  253.             rsn = winlo;                /* Yes, return its info */
  254.             debug(F101,"input return pre-stashed packet","",rsn);
  255.             dumprbuf();
  256.             rdatap = r_pkt[x].pk_adr;   /* like rpack would do. */
  257.             rln = (int)strlen((char *) rdatap);
  258.             type = r_pkt[x].pk_typ;
  259.             break;
  260.             }
  261.         }
  262.         }
  263.         type = rpack();            /* Try to read a packet. */
  264.         debug(F111,"input recv",(char *) rdatap,(int) type);
  265.         while (type == 'e') {    /* Handle echoes */
  266.         debug(F000,"echo discarded","",type);
  267.         type = rpack();    
  268.         }        
  269.         if (type < -1) return('q'); /* Ctrl-C */
  270.         if (type < 0) {        /* Receive window full */
  271.         /* Another thing to do here would be to delete */
  272.         /* the highest packet and NAK winlo.  But that */
  273.         /* shouldn't be necessary since the other Kermit */
  274.         /* should not have sent a packet outside the window. */
  275.         debug(F101,"rpack receive window full","",0);
  276.         dumprbuf();
  277.         errpkt((CHAR *)"Receive window full.");
  278.         strcpy((char *)recpkt,"Receive window full.");
  279.         type = 'E';
  280.         break;
  281.         }
  282.         dumprbuf();
  283.  
  284.         if (chkint() < 0) {        /* Check for console interrupts. */
  285.         errpkt((CHAR *)"User cancelled.");
  286.         strcpy((char *)recpkt,"User cancelled.");
  287.         type = 'E';
  288.         break;
  289.         }
  290.         if (type == 'E') {
  291.         debug(F101,"input got E, nakstate","",nakstate);
  292.         break;            /* Error packet */
  293.         }
  294.         if (type == 'Q') {        /* Crunched packet. */
  295.         crunched++;
  296.         numerrs++;
  297. /*
  298.   Packet arrived damaged.  It was most likely the packet we were expecting
  299.   next, so we send a NAK for that packet.  Prior to 5A(189), we always
  300.   NAK'd winlo here, but that was bad because if two (or more) different
  301.   packets were damaged, we would keep NAKing the first one and never NAK the
  302.   other ones, which could result in a lengthy series of timeouts.  Now we
  303.   NAK the oldest as-yet-unNAK'd missing packet.
  304. */
  305.         z = (winlo + wslots) % 64;  /* Search from winlo to z */
  306.         debug(F101,"ZZZ crunched z","",z);
  307.         nf = 0;                /* NAK flag not set yet */
  308.         for (x = winlo; x != z; x = (x + 1) % 64) {
  309.             debug(F101,"ZZZ x","",x);
  310.             if (rseqtbl[x] > -1) /* Have I received packet x? */
  311.               continue;         /* Yes, go on. */
  312.             debug(F101,"ZZZ x not recd yet","",x);
  313.             pi = sseqtbl[x];     /* No, have I NAK'd it yet? */
  314.             if (pi < 0 || s_pkt[pi].pk_rtr == 0) {
  315.             debug(F101,"ZZZ x not NAK'd yet","",x);
  316.             nack(x);     /* No, NAK it now. */
  317.             nf = 1;         /* Flag that I did. */
  318.             break;
  319.             }
  320.         }
  321.         if (!nf) {        /* If we didn't NAK anything above, */
  322.             debug(F101,"ZZZ NAKing winlo","",winlo);
  323.             if (nack(winlo) < 0) { /* we have to NAK winlo (again) */
  324.             errpkt((CHAR *)"Too many retries."); /* Too many */
  325.             strcpy((char *)recpkt,"Timed out."); /* Give up */
  326.             type = 'E';
  327.             break;
  328.             }
  329.         } 
  330.         continue;
  331.         }
  332.  
  333.         if (type == 'T') {        /* Timeout */
  334.         timeouts++;
  335.         debug(F101,"input receive-state timeout, winlo","",winlo);
  336.         /* NAK only the packet at window-low */
  337.         debug(F101,"input sending NAK for winlo","",winlo);
  338.         if (nack(winlo) < 0) {
  339.             debug(F101,"input sent too many naks","",winlo);
  340.             errpkt((CHAR *)"Too many retries.");
  341.             strcpy((char *)recpkt,"Sent too many NAKs.");
  342.             type = 'E';
  343.             break;
  344.         } else continue;
  345.         }
  346.  
  347.         if (rsn == winlo) {        /* Got the packet we want, done. */
  348.         debug(F101,"input rsn=winlo","",rsn);
  349.         break;
  350.         }
  351.  
  352.         /* Got a packet out of order. */
  353.  
  354.         debug(F101,"input recv got packet out of order","",rsn);
  355.         k = rseqtbl[rsn];        /* Get window slot of this packet. */
  356.         debug(F101,"input recv rseqtbl[rsn]","",k);
  357.         if (k < 0) {
  358.         debug(F101,"input recv can't find index for rcvd pkt","",rsn);
  359.         errpkt((CHAR *)"internal error number 21");
  360.         strcpy((char *)recpkt,"Sliding windows protocol error.");
  361.         type = 'E';
  362.         break;
  363.         }        
  364.         y = chkwin(rsn,winlo,wslots); /* See what window it's in. */
  365.         debug(F101,"input recv chkwin","",y);
  366.         if (y == 1) {        /* From previous window. */
  367.         resend(rsn);        /* Resend the ACK (might have data) */
  368.         freerpkt(rsn);        /* Get rid of received packet */
  369.         continue;
  370.         } else {            /* In this window or out of range */
  371.         if (y < 0)        /* If out of range entirely, */
  372.           freerpkt(rsn);    /* release its buffer */
  373.  
  374. #ifdef COMMENT /* Ignore this and read what comes afterwards... */
  375. /*
  376.   We have received a packet, but not the one we want.  It would seem to
  377.   make sense to always send a NAK for the most desired packet (winlo).  But
  378.   consider this scenario: a packet arrived damaged so we NAK'd it above;
  379.   then packets winlo+1, winlo+2, ... winlo+n arrive, each one making us
  380.   send another NAK for winlo, so the other Kermit gets n NAKs for winlo, and
  381.   either would have to resend it n times, or if n > retry limit, give up
  382.   because of too many retries.  So we compromise: If a packet arrives
  383.   that is not the most desired packet (winlo), we NAK winlo, BUT ONLY IF
  384.   it has not been NAK'd before.
  385. */
  386.         x = sseqtbl[winlo];    /* Get index of most desired packet */
  387.         if (s_pkt[x].pk_rtr == 0 ||   /* Not NAK'd before? */
  388.             rbufnum < 1) {          /* Or receive window full? */
  389.             if (nack(winlo) < 0) {    /* One or both, so NAK it now. */
  390.             errpkt((CHAR *)"Too many retries."); /* Too many */
  391.             strcpy((char *)recpkt,"Timed out."); /* Give up */
  392.             type = 'E';
  393.             break;
  394.             } else continue;
  395.         } else continue;
  396. #endif /* COMMENT */
  397.  
  398. /*
  399.   In version 5A(189), the strategy was revised to send NAKs for the oldest
  400.   missing packet that had not been NAK'd before, which requires a search.
  401.   Thus, if winlo was already NAK'd, instead of doing nothing, we send a NAK
  402.   for the "lowest" as-yet-unNAK'd missing packet.
  403.  
  404.   If our receive window is full, however, we have no choice but to NAK winlo:
  405. */
  406.         debug(F101,"XXX checking rbufnum","",rbufnum);
  407.         if (rbufnum < 1) {          /* Receive window full? */
  408.             debug(F101,"XXX out of buffers","",rbufnum);
  409.             if (nack(winlo) < 0) {    /* No choice, must NAK winlo. */
  410.             errpkt((CHAR *)"Too many retries."); /* Too many */
  411.             strcpy((char *)recpkt,"Timed out."); /* Give up */
  412.             type = 'E';
  413.             break;
  414.             } else continue;
  415.         }
  416. /*
  417.   Receive window not full.  This is a packet in the current window but it is
  418.   not the desired packet at winlo.  So therefore there are gaps before this
  419.   packet.  So we find the "lowest" unNAK'd missing packet, if any, between
  420.   winlo and this one, and NAK it.  If there are no as-yet-unNAK'd missing
  421.   packets in the window, then we send nothing and go wait for another packet.
  422.   In theory, this could result in a timeout, but in practice it is likely that
  423.   the already-NAK'd missing packets are already on their way.  Note, we do not
  424.   NAK ahead of ourselves, as that only creates unnecessary retransmissions.
  425. */
  426.         debug(F101,"XXX winlo","",winlo);
  427.         for (x = winlo; x != rsn; x = (x + 1) % 64) {
  428.             debug(F101,"XXX x","",x);
  429.             if (rseqtbl[x] > -1) /* Have I received packet x? */
  430.               continue;         /* Yes, check next sequence number. */
  431.             debug(F101,"XXX missing pkt","",x);
  432.             pi = sseqtbl[x];     /* No, have I NAK'd it yet? */
  433.             if (pi < 0 || s_pkt[pi].pk_rtr == 0) {
  434.             nack(x);     /* No, NAK it now. */
  435.             debug(F101,"XXX nak","",x);
  436.             break;
  437.             }
  438.         }
  439.         }
  440. /*!!!*/
  441.     } else {            /* Otherwise file sender... */
  442.  
  443.         if (wslots > 1) {        /* Packet at winlo already ACK'd? */
  444.         if (sacktbl[winlo]) {    /* If so,  */
  445.             sacktbl[winlo] = 0;       /* Turn off the ACK'd flag */
  446.             winlo = (winlo + 1) % 64; /* Rotate the window */
  447.             type = 'Y';        /* And return ACK */
  448.             debug(F101,
  449.               "input send returning pre-stashed ACK","",
  450.               winlo-1);
  451.             break;
  452.         }
  453.         }
  454.         type = rpack();        /* Try to read an acknowledgement */
  455.         debug(F111,"input send",(char *) rdatap,(int) type);
  456.         while (type == 'e') {    /* Handle echoes */
  457.         debug(F000,"echo discarded","",type);
  458.         type = rpack();    
  459.         }        
  460.         if (type == -2) return('q');
  461.         if (type == -1) {
  462.         errpkt((CHAR *)"Internal error number 18");
  463.         debug(F101," wslots","",wslots);
  464.         debug(F101," winlo","",winlo);
  465.         debug(F101," pktnum","",pktnum);
  466.         dumprbuf();
  467.         strcpy((char *)recpkt,"Can't allocate receive buffer");
  468.         type = 'E';
  469.         break;
  470.         }
  471.         dumprbuf();            /* debugging */
  472.  
  473.         if (chkint() < 0) {        /* Check for console interrupts. */
  474.         errpkt((CHAR *)"User cancelled.");
  475.         strcpy((char *)recpkt,"User cancelled.");
  476.         return(type = 'E');
  477.         }
  478.  
  479.         /* got a packet */
  480.  
  481.         if (type == 'E') {
  482.         debug(F101,"input send got E, nakstate","",nakstate);
  483.         break;            /* Error packet */
  484.         }
  485.         if (type == 'Q') {        /* Crunched packet */ 
  486.         crunched++;        /* For statistics */
  487.         numerrs++;        /* For packet resizing */
  488.         x = resend(winlo);    /* Resend window-low */
  489.         if (x < 0) {
  490.             type = 'E';
  491.             errpkt(recpkt);
  492.             break;
  493.         }
  494.         continue;
  495.         }
  496.         if (type == 'T') {        /* Timeout waiting for ACKs. */
  497.         timeouts++;        /* Count it */
  498.         numerrs++;        /* Count an error too */
  499.         debug(F101,"input send state timeout, winlo","",winlo);
  500.  
  501.         /* Retransmit the oldest un-ACK'd packet. */
  502.  
  503.         debug(F101,"input send resending winlo","",winlo);
  504.         if (resend(winlo) < 0) { /* Check retries */
  505.             debug(F101,"input send too many resends","",maxtry);
  506.             errpkt(recpkt);
  507.             return(type = 'E');
  508.         }
  509.         continue;
  510.         }
  511.  
  512.         /* Got an actual normal packet */
  513.  
  514.         y = chkwin(rsn,winlo,wslots); /* Is it in the window? */
  515.         debug(F101,"input send rsn","",rsn);
  516.         debug(F101,"input send winlo","",winlo);
  517.         debug(F101,"input send chkwin","",y);
  518.         if (type == 'Y') {        /* Got an ACK */
  519.         if (y == 0) {        /* In current window */
  520.             x = sseqtbl[rsn];    /* Mark the packet as ACK'd */
  521.             if (x > -1) s_pkt[x].pk_flg++;  /* (old way) */
  522.             sacktbl[rsn]++;                /* (new way) */
  523. /*
  524.   NOTE: The following statement frees the buffer of the ACK we just got.
  525.   But the upper layers still need the data, like if it's the ACK to an I,
  526.   S, F, D, Z, or just about any kind of packet.  So for now, freerbuf()
  527.   deallocates the buffer, but does not erase the data or destroy the pointer
  528.   to it.  There's no other single place where these receive buffers can be
  529.   correctly freed (?) ...
  530. */
  531.             freerpkt(rsn);    /* Free the ACK's buffer */
  532.             freesbuf(rsn);    /* *** Free the sent packet's buffer */
  533.             if (rsn == winlo) {    /* Got the one we want */
  534.             sacktbl[winlo] = 0;
  535.             winlo = (winlo + 1) % 64;
  536.             debug(F101,"input send rotated send window","",winlo);
  537.             break;        /* Return the ACK */
  538.             } else {
  539.             debug(F101,"input send mark pkt","",rsn);
  540.             continue;    /* Otherwise go read another packet */
  541.             }
  542.         } else {        /* ACK not in window, ignore */
  543.             debug(F101,"input send ACK out of window","",rsn);
  544.             freerpkt(rsn);
  545.             continue;
  546.         }
  547.         }
  548.         if (type == 'N') {        /* NAK */
  549.         numerrs++;        /* Count an error */
  550.         debug(F101,"input send NAK","",rsn);
  551.         freerpkt(rsn);        /* Free buffer where NAK lies. */
  552.         if (y == 0) {        /* In current window */        
  553.             debug(F100," in window","",0);
  554.             k = sseqtbl[rsn];    /* Get pointer to NAK'd packet. */
  555.             x = 0;
  556.             if (k < 0 || (k > -1 && s_pkt[k].pk_typ == ' ')) {
  557.             x = resend(winlo); /* Packet we haven't sent yet. */
  558.             } else {
  559.             x = resend(rsn); /* Resend requested packet. */
  560.             }
  561.             if (x < 0) {    /* Resend error is fatal.  */
  562.             type = 'E';
  563.             errpkt(recpkt);
  564.             break;
  565.             } else continue;    /* Resend ok, go read another packet */
  566.         } else if ((rsn == (pktnum + 1) % 64)) { /* NAK for next pkt */
  567.             if (wslots > 1) {
  568.             debug( F101,"NAK for next packet, windowing","",rsn);
  569.             x = resend(winlo); /* Resend window-low */
  570.             if (x < 0) {
  571.                 type = 'E';
  572.                 errpkt(recpkt);
  573.                 break;
  574.             }
  575.             continue;    /* Go back and read another pkt */
  576.             }
  577.             debug(F101,"NAK for next packet, no windowing","",rsn);
  578.             x = (rsn == 0) ? 63 : rsn - 1;
  579.             if (x == 0 && (sndtyp == 'S' || sndtyp == 'I')) {
  580.             resend(0);    /* ACK for S or I packet missing */
  581.             continue;    /* so resend it. */
  582.             }            /* Else, treat NAK(n+1) as ACK(n) */
  583.             if ((x = sseqtbl[x]) > -1) {
  584.             sacktbl[x]++;       /* (new way) */
  585.             s_pkt[x].pk_flg++; /* (old way) */
  586.             }
  587.             type = 'Y';        /* Treat it as ACK for last pkt */
  588.             break;
  589.         } else if (y > 0) {    /* NAK for pkt we can't resend */
  590.             debug(F101," NAK out of window","",rsn); /* bad... */
  591.             type = 'E';
  592.             errpkt((CHAR *)"NAK out of window");
  593.             strcpy((char *)recpkt,"NAK out of window.");
  594.             break;
  595.         } else continue;    /* Ignore other NAKs */
  596.         }                /* End of file-sender NAK handler */
  597.  
  598.             if (rsn == winlo) {        /* Not ACK, NAK, timeout, etc. */
  599.         debug(F000,"input send unexpected type","",type);
  600.         break;
  601.         }
  602.     }                /* End of file-sender section */
  603.     }                    /* End of input() while() loop */
  604.     if (wslots == 1) {
  605.     debug(F100,"input about to flush","",0);
  606.     ttflui();        /* Got what we want, clear input buffer. */
  607.     }
  608.     if (!nakstate)        /* When sending */
  609.       rcalcpsz();        /* recalculate size every packet */
  610.     debug(F000,"input returning type","",type);
  611.     return(rcvtyp = type);    /* Success, return packet type. */
  612. }
  613.  
  614. /*  D O P A R  --  Add an appropriate parity bit to a character  */
  615.  
  616. /*
  617.   (PWP) this is still used in the Mac terminal emulator, so we have to keep it
  618. */
  619. CHAR
  620. #ifdef CK_ANSIC
  621. dopar(register CHAR ch)
  622. #else
  623. dopar(ch) register CHAR ch;
  624. #endif /* CK_ANSIC */
  625.     {
  626.     register unsigned int a;
  627.     if (!parity) return((CHAR) (ch & 255)); else a = ch & 127;
  628.     switch (parity) {
  629.     case 'e':  return(partab[a]);                 /* Even */
  630.     case 'm':  return((CHAR) (a | 128));         /* Mark */
  631.     case 'o':  return((CHAR) (partab[a] ^ 128)); /* Odd */
  632.     case 's':  return((CHAR) a);                 /* Space */
  633.     default:   return((CHAR) a);                 /* Something illegal */
  634.     }
  635. }
  636.  
  637. #ifdef PARSENSE
  638. /*  P A R C H K  --  Check if Kermit packet has parity  */
  639.  
  640. /*
  641.   Call with s = pointer to packet, start = packet start character, n = length.
  642.   Returns 0 if packet has no parity, -1 on error, or, if packet has parity:
  643.     'e' for even, 'o' for odd, 'm' for mark.  Space parity cannot be sensed.
  644.   So a return value of 0 really means either space or none.
  645.   Returns -2 if parity has already been checked during this protocol operation.
  646. */
  647. int
  648. #ifdef CK_ANSIC
  649. parchk(CHAR *s, CHAR start, int n)
  650. #else
  651. parchk(s,start,n) CHAR *s, start; int n;
  652. #endif /* CK_ANSIC */
  653. /* parchk */ {
  654.     CHAR s0, s1, s2, s3;
  655.  
  656.     debug(F101,"parchk n","",n);
  657.     debug(F101,"parchk start","",start);
  658.  
  659.     s0 = s[0] & 0x7f;            /* Mark field (usually Ctrl-A) */
  660.  
  661.     if (s0 != start || n < 5) return(-1); /* Not a valid packet */
  662.  
  663. /* Look at packet control fields, which never have 8th bit set */
  664. /* First check for no parity, most common case. */
  665.  
  666.     if (((s[0] | s[1] | s[2] | s[3]) & 0x80) == 0)
  667.       return(0);            /* No parity or space parity */
  668.  
  669. /* Check for mark parity */
  670.  
  671.     if (((s[0] & s[1] & s[2] & s[3]) & 0x80) == 0x80)
  672.       return('m');            /* Mark parity */
  673.  
  674. /* Packet has some kind of parity */
  675. /* Make 7-bit copies of control fields */
  676.  
  677.     s1 = s[1] & 0x7f;            /* LEN */
  678.     s2 = s[2] & 0x7f;            /* SEQ */
  679.     s3 = s[3] & 0x7f;            /* TYPE */
  680.  
  681. /* Check for even parity */
  682.  
  683.     if ((s[0] == partab[s0]) &&
  684.         (s[1] == partab[s1]) &&
  685.         (s[2] == partab[s2]) &&
  686.     (s[3] == partab[s3]))
  687.       return('e');
  688.  
  689. /* Check for odd parity */
  690.  
  691.     if ((s[0] != partab[s0]) &&
  692.         (s[1] != partab[s1]) &&
  693.         (s[2] != partab[s2]) &&
  694.     (s[3] != partab[s3]))
  695.       return('o');
  696.  
  697. /* Otherwise it's probably line noise.  Let checksum calculation catch it. */
  698.  
  699.     return(-1);
  700. }
  701. #endif /* PARSENSE */
  702.  
  703. /*
  704.   Check to make sure timeout intervals are long enough to allow maximum
  705.   length packets to get through before the timer goes off.  If not, the
  706.   timeout interval is adjusted upwards.
  707.  
  708.   This routine is called at the beginning of a transaction, before we
  709.   know anything about the delay characteristics of the line.  It works
  710.   only for serial communication devices; it trusts the speed reported by
  711.   the operating system.
  712.  
  713.   Call with a timout interval.  Returns it, adjusted if necessary.
  714. */  
  715. int
  716. chktimo(timo,flag) int timo, flag; {
  717.     long cps, z; int x, y;
  718.     debug(F101,"chktimo timo","",timo); /* Timeout before adjustment */
  719.     debug(F101,"chktimo flag","",flag);
  720.  
  721.     if (flag)                /* Don't change timeout if user */
  722.       return(timo);            /* gave SET SEND TIMEOUT command. */
  723.     debug(F101,"chktimo spmax","",spmax);
  724.     debug(F101,"chktimo urpsiz","",urpsiz);
  725.  
  726.     speed = ttgspd();            /* Get current speed. */
  727.     if (speed > 0L && !network) {
  728.     cps = speed / 10L;        /* Convert to chars per second */
  729.     if (cps > 0L) {
  730.         long plen;            /* Maximum of send and rcv pkt size */
  731.         z = cps * (long) timo;    /* Chars per timeout interval */
  732.         z -= z / 10L;        /* Less 10 percent */
  733.         plen = spmax;
  734.         if (urpsiz > spmax) plen = urpsiz;
  735.         debug(F101,"chktimo plen","",plen);
  736.         if (z < plen) {        /* Compare with packet size */
  737.         x = (int) ((long) plen / cps); /* Adjust if necessary */
  738.         y = x / 10;        /* Add 10 percent for safety */
  739.         if (y < 2) y = 2;    /* Or 2 seconds, whichever is more */
  740.         x += y;
  741.         if (x > timo)        /* If this is greater than current */
  742.           timo = x;        /* timeout, change the timeout */
  743.         debug(F101,"chktimo new timo","",timo);
  744.         }
  745.     }
  746.     }
  747.     return(timo);
  748. }
  749.  
  750. /*  S P A C K  --  Construct and send a packet  */
  751.  
  752. /*
  753.   spack() sends a packet of the given type, sequence number n, with len data
  754.   characters pointed to by d, in either a regular or extended- length packet,
  755.   depending on len.  Returns the number of bytes actually sent, or else -1
  756.   upon failure.  Uses global npad, padch, mystch, bctu, data.  Leaves packet 
  757.   fully built and null-terminated for later retransmission by resend().
  758.   Updates global sndpktl (send-packet length).
  759.  
  760.   NOTE: The global pointer "data" is assumed to point into the 7th position
  761.   of a character array (presumably in packet buffer for the current packet).
  762.   It was used by getpkt() to build the packet data field.  spack() fills in
  763.   the header to the left of the data pointer (the data pointer is defined
  764.   in getsbuf() in ckcfn3.c).  If the address "d" is the same as "data", then
  765.   the packet's data field has been built "in place" and need not be copied.
  766. */
  767. int
  768. #ifdef CK_ANSIC
  769. spack(char pkttyp, int n, int len, CHAR *d)
  770. #else
  771. spack(pkttyp,n,len,d) char pkttyp; int n, len; CHAR *d;
  772. #endif /* CK_ANSIC */
  773. /* spack */ {
  774.     register int i;
  775.     int j, k, lp, longpkt, copy;
  776.     register CHAR *cp, *mydata;
  777.     unsigned crc;
  778.  
  779.     debug(F101,"spack n","",n);
  780.     debug(F111," data",data,data);
  781.     debug(F101," d","",d);
  782.     debug(F101," len","",len);
  783.  
  784.     copy = (d != data);            /* Flag whether data must be copied  */
  785.     longpkt = (len + bctl + 2) > 94;    /* Decide whether it's a long packet */
  786.     mydata = data - 7 + (longpkt ? 0 : 3); /* Starting position of header */
  787.     debug(F101," mydata","",mydata);
  788.  
  789.     k = sseqtbl[n];            /* Packet structure info for pkt n */ 
  790.     debug(F101," sseqtbl[n]","",k);
  791.     if (k < 0) {
  792.     debug(F101,"spack sending packet out of window","",n);
  793.     } else {                /* Record packet info */
  794.     s_pkt[k].pk_adr = mydata;    /* Remember address of packet. */
  795.     s_pkt[k].pk_seq = n;        /* Record sequence number */
  796.     s_pkt[k].pk_typ = pkttyp;    /* Record packet type */
  797.     }
  798.  
  799.     spktl = 0;                /* Initialize length of this packet */
  800.     i = 0;                /* and position in packet. */
  801.  
  802. /* Now fill the packet */
  803.  
  804.     mydata[i++] = mystch;        /* MARK */
  805.     lp = i++;                /* Position of LEN, fill in later */
  806.  
  807.     mydata[i++] = tochar(n);        /* SEQ field */
  808.     mydata[i++] = pkttyp;        /* TYPE field */
  809.     j = len + bctl;            /* Length of data + block check */
  810.     if (longpkt) {            /* Long packet? */
  811.     int x;                /* Work around SCO Xenix/286 */
  812.     x = 95;                /* compiler bug... */
  813.     x = j / 95;
  814.         mydata[lp] = tochar(0);        /* Yes, set LEN to zero */
  815.         mydata[i++] = tochar(x);    /* High part */
  816.         mydata[i++] = tochar(j % 95);    /* Low part */
  817.         mydata[i] = '\0';        /* Header checksum */
  818.         mydata[i++] = tochar(chk1(mydata+lp));
  819.     } else mydata[lp] = tochar(j+2);    /* Normal LEN */
  820.  
  821.     if (copy)                /* Data field built in place? */
  822.       for ( ; len--; i++) mydata[i] = *d++; /* No, must copy. */
  823.     else                /* Otherwise, */
  824.       i += len;                /* Just skip past data field. */
  825.     mydata[i] = '\0';            /* Null-terminate for checksum calc. */
  826.  
  827.     switch (bctu) {            /* Block check */
  828.     case 1:                /* 1 = 6-bit chksum */
  829.         mydata[i++] = tochar(chk1(mydata+lp));
  830.         break;
  831.     case 2:                /* 2 = 12-bit chksum */
  832.         j = chk2(mydata+lp);
  833.         mydata[i++] = (unsigned)tochar((j >> 6) & 077);
  834.            mydata[i++] = (unsigned)tochar(j & 077);
  835.         break;
  836.         case 3:                /* 3 = 16-bit CRC */
  837.         crc = chk3(mydata+lp);
  838.         mydata[i++] = (unsigned)tochar(((crc & 0170000)) >> 12);
  839.         mydata[i++] = (unsigned)tochar((crc >> 6) & 077);
  840.         mydata[i++] = (unsigned)tochar(crc & 077);
  841.         break;
  842.     case 4:                /* 2 = 12-bit chksum, blank-free */
  843.         j = chk2(mydata+lp);    
  844.         mydata[i++] =
  845.           (unsigned)(tochar((unsigned)(((j >> 6) & 077) + 1)));
  846.            mydata[i++] = (unsigned)(tochar((unsigned)((j & 077) + 1)));
  847.         break;
  848.     }
  849.     mydata[i++] = seol;            /* End of line (packet terminator) */
  850. #ifdef TCPSOCKET
  851. /*
  852.   If TELNET connection and packet terminator is carriage return,
  853.   we must stuff either LF or NUL, according to SET TELNET NEWLINE-MODE
  854.   (tn_nlm), to meet the TELNET specification.
  855. */
  856.     if (network && ttnproto == NP_TELNET && seol == CR)
  857.       mydata[i++] = tn_nlm ? LF : NUL;
  858. #endif /* TCPSOCKET */
  859.     mydata[i] = '\0';            /* Terminate string */
  860.     logpkt('s',n,mydata);        /* Log packet */
  861.  
  862.     /* (PWP) add the parity quickly at the end */
  863.     switch (parity) {
  864.       case 'e':                /* Even */
  865.     for (cp = &mydata[i-1]; cp >= mydata; cp--)
  866.       *cp = partab[*cp];
  867.     break;
  868.       case 'm':                /* Mark */
  869.     for (cp = &mydata[i-1]; cp >= mydata; cp--)
  870.       *cp |= 128;
  871.     break;
  872.       case 'o':                /* Odd */
  873.     for (cp = &mydata[i-1]; cp >= mydata; cp--)
  874.       *cp = partab[*cp] ^ 128;
  875.     break;
  876.       case 's':                /* Space */
  877.     for (cp = &mydata[i-1]; cp >= mydata; cp--)
  878.       *cp &= 127;
  879.     break;
  880.     }
  881.     if (npad) ttol(padbuf,npad);    /* Send any padding */
  882.     spktl = i;                /* Remember packet length */
  883.     s_pkt[k].pk_len = spktl;        /* also in packet info structure */
  884.     if (ttol(mydata,spktl) < 0) return(-1); /* Send the packet */
  885.     sndtyp = pkttyp;            /* Remember packet type for echos */
  886.     spackets++;                /* Count it. */
  887.     flco += spktl;            /* Count the characters */
  888.     tlco += spktl;            /* for statistics... */
  889.     dumpsbuf();                /* Dump send buffers to debug log */
  890.     screen(SCR_PT,pkttyp,(long)n,(char *)mydata); /* Update screen */
  891.     return(spktl);            /* Return length */
  892. }
  893.  
  894. /*  C H K 1  --  Compute a type-1 Kermit 6-bit checksum.  */
  895.  
  896. int
  897. chk1(pkt) register CHAR *pkt; {
  898.     register unsigned int chk;
  899.     chk = chk2(pkt);
  900.     chk = (((chk & 0300) >> 6) + chk) & 077;
  901.     return((int) chk);
  902. }
  903.  
  904. /*  C H K 2  --  Compute the numeric sum of all the bytes in the packet.  */
  905.  
  906. unsigned int
  907. chk2(pkt) register CHAR *pkt; {
  908.     register long chk; register unsigned int m;
  909.     m = (parity) ? 0177 : 0377;
  910.     for (chk = 0; *pkt != '\0'; pkt++)
  911.       chk += *pkt & m;
  912.     return((unsigned int) (chk & 07777));
  913. }
  914.  
  915.  
  916. /*  C H K 3  --  Compute a type-3 Kermit block check.  */
  917. /*
  918.  Calculate the 16-bit CRC-CCITT of a null-terminated string using a lookup 
  919.  table.  Assumes the argument string contains no embedded nulls.
  920. */
  921. unsigned int
  922. chk3(pkt) register CHAR *pkt; {
  923.     register long c, crc;
  924.     register unsigned int m;
  925.     m = (parity) ? 0177 : 0377;
  926.     for (crc = 0; *pkt != '\0'; pkt++) {
  927.     c = crc ^ (long)(*pkt & m);
  928.     crc = (crc >> 8) ^ (crcta[(c & 0xF0) >> 4] ^ crctb[c & 0x0F]);
  929.     }
  930.     return((unsigned int) (crc & 0xFFFF));
  931. }
  932.  
  933. int
  934. nxtpkt() {                /* Called by file sender */
  935.     int j, n;
  936.  
  937.     debug(F101,"nxtpkt pktnum","",pktnum);
  938.     debug(F101,"nxtpkt winlo ","",winlo);
  939.     n = (pktnum + 1) % 64;        /* Increment packet number mod 64 */
  940. #ifdef COMMENT 
  941. /*
  942.   Suggested by Alan Grieg.  A packet can be sent out of window in 
  943.   circumstances involving acks received out of order, ...  Have to think
  944.   about this...
  945. */
  946.     if (chkwin(n,winlo,wslots)) {
  947.     debug(F101,"nxtpkt n not in window","",n);
  948.     return(-1);
  949.     }
  950. #endif
  951.     j = getsbuf(n);            /* Get a buffer for packet n */
  952.     if (j < 0) {
  953.     debug(F101,"nxtpkt can't getsbuf","",j);
  954.     return(-1);
  955.     }
  956.     pktnum = n;          
  957.     debug(F101,"nxtpkt bumped pktnum to","",pktnum);
  958.     return(0);
  959. }
  960.  
  961. /* Functions for sending ACKs and NAKs */
  962.  
  963. /* Note, we should only ACK the packet at window-low (winlo) */
  964. /* However, if an old packet arrives again (e.g. because the ACK we sent */
  965. /* earlier was lost), we ACK it again. */
  966.  
  967. int
  968. ack() {                    /* Acknowledge the current packet. */
  969.     return(ackns(winlo,(CHAR *)""));
  970. }
  971.  
  972. int
  973. ackns(n,s) int n; CHAR *s; {        /* Acknowledge packet n */
  974.     int j, k;
  975.     debug(F111,"ackns",s,n);
  976.  
  977.     k = rseqtbl[n];            /* First find received packet n. */
  978.     debug(F101,"ackns k","",k);
  979. #ifdef COMMENT
  980. /* No need to set ACK'd bit, because we're gonna free the buffer now */
  981.     if (k > -1)                /* If in window */
  982.       s_pkt[k].pk_flg++;        /* mark the ack'd bit. */
  983.     else
  984.       debug(F101,"ackns can't set ack'd bit","",k);
  985. #endif
  986.     freesbuf(n);            /* Free current send-buffer, if any */
  987.     if ((j = getsbuf(n)) < 0) {
  988.     /* This can happen if we have to re-ACK an old packet that has */
  989.         /* already left the window.  It does no harm. */
  990.     debug(F101,"ackns can't getsbuf","",n);
  991.     }
  992.     spack('Y',n,(int)strlen((char *)s),s); /* Now send it. */
  993.     debug(F101,"ackns winlo","",winlo);
  994.     debug(F101,"ackns n","",n);
  995.     if (n == winlo) {            /* If we're acking winlo */
  996.     if (k > -1)
  997.       freerbuf(k);            /* don't need it any more */
  998.     if (j > -1)
  999.       freesbuf(j);            /* and don't need to keep ACK either */
  1000.     winlo = (winlo + 1) % 64;
  1001.     }
  1002.     return(0);
  1003. }
  1004.  
  1005. int
  1006. ackn(n) int n; {            /* Send ACK for packet number n */
  1007.     return(ackns(n,(CHAR *)""));
  1008. }
  1009.  
  1010. int
  1011. ack1(s) CHAR *s; {            /* Send an ACK with data. */
  1012.     debug(F110,"ack1",(char *) s,0);
  1013.     return(ackns(winlo, s));
  1014. }
  1015.  
  1016. /* N A C K  --   Send a Negative ACKnowledgment. */
  1017. /*
  1018.  Call with the packet number, n, to be NAK'd.
  1019.  Returns -1 if that packet has been NAK'd too many times, otherwise 0.
  1020.  Btw, it is not right to return 0 under error conditions.  This is
  1021.  done because the -1 code is used for cancelling the file transfer.
  1022.  More work is needed here.
  1023. */
  1024. int
  1025. nack(n) int n; {
  1026.     int i;
  1027.  
  1028.     if (n < 0 || n > 63) {
  1029.     debug(F101,"nack bad pkt num","",n);
  1030.     return(0);
  1031.     } else debug(F101,"nack","",n);
  1032.     if ((i = sseqtbl[n]) < 0) {        /* If necessary */
  1033.     if (getsbuf(n) < 0) {        /* get a buffer for this NAK */
  1034.         debug(F101,"nack can't getsbuf","",n);
  1035.         return(0);
  1036.     } else i = sseqtbl[n];        /* New slot number */
  1037.     }
  1038.     if (s_pkt[i].pk_rtr++ > maxtry)    /* How many times have we done this? */
  1039.       return(-1);            /* Too many... */
  1040.  
  1041. /* Note, don't free this buffer.  Eventually an ACK will come, and that */
  1042. /* will set it free.  If not, well, it's back to ground zero anyway...  */
  1043.  
  1044.     spack('N',n,0,(CHAR *) "");        /* NAKs never have data. */
  1045.     return(0);
  1046. }
  1047.  
  1048. /*
  1049.  * (PWP) recalculate the optimal packet length in the face of errors.
  1050.  * This is a modified version of the algorithm by John Chandler in Kermit/370, 
  1051.  * see "Dynamic Packet Size Control", Kermit News, V2 #1, June 1988.
  1052.  *
  1053.  * This implementation minimizes the total overhead equation, which is
  1054.  *
  1055.  *   Total chars = file_chars + (header_len * num_packs)
  1056.  *                            + (errors * (header_len + packet_len))
  1057.  *
  1058.  * Differentiate with respect to number of chars, solve for packet_len, get:
  1059.  *
  1060.  *   packet_len = sqrt (file_chars * header_len / errors)
  1061.  */
  1062.  
  1063. /*
  1064.  (FDC) New super-simple algorithm.  If there was an error in the most recent
  1065.  packet exchange, cut the send-packet size in half, down to a minimum of 20.
  1066.  If there was no error, increase the size by 5/4, up to the maximum negotiated
  1067.  length.  Seems to be much more responsive than previous algorithm, which took
  1068.  forever to recover the original packet length, and it also went crazy under
  1069.  certain conditions.
  1070.  
  1071.  Here's another idea for packet length resizing that keeps a history of the
  1072.  last n packets.  Push a 1 into the left end of an n-bit shift register if the
  1073.  current packet is good, otherwise push a zero.  The current n-bit value, w, of
  1074.  this register is a weighted sum of the noise hits for the last n packets, with
  1075.  the most recent weighing the most.  The current packet length is some function
  1076.  of w and the negotiated packet length, like:
  1077.  
  1078.    (2^n - w) / (2^n) * (negotiated length)
  1079.  
  1080.  If the present resizing method causes problems, think about this one a little
  1081.  more.
  1082. */
  1083. VOID
  1084. rcalcpsz() {
  1085.  
  1086. #ifdef COMMENT
  1087. /* Old way */
  1088.     register long x, q;
  1089.     if (numerrs == 0) return;    /* bounds check just in case */
  1090.  
  1091.     /* overhead on a data packet is npad+5+bctr, plus 3 if extended packet */
  1092.     /* an ACK is 5+bctr */
  1093.  
  1094.     /* first set x = per packet overhead */
  1095.     if (wslots > 1)
  1096.     x = (long) (npad+5+bctr);    /* only the packet, don't count the ack */
  1097.     else
  1098.     x = (long) (npad+5+3+bctr+5+bctr);
  1099.  
  1100.     /* then set x = packet length ** 2 */
  1101.     x = x * ( ffc / (long) numerrs);    /* careful of overflow */
  1102.     
  1103.     /* calculate the long integer sqrt(x) quickly */
  1104.     q = 500;
  1105.     q = (q + x/q) >> 1;
  1106.     q = (q + x/q) >> 1;
  1107.     q = (q + x/q) >> 1;
  1108.     q = (q + x/q) >> 1;        /* should converge in about 4 steps */
  1109.     if ((q > 94) && (q < 130))    /* break-even point for long packets */
  1110.     q = 94;
  1111.     if (q > spmax) q = spmax;    /* maximum bounds */
  1112.     if (q < 10) q = 10;        /* minimum bounds */
  1113.     spsiz = q;            /* set new send packet size */
  1114.     debug(F101,"rcalcpsiz","",q);
  1115. #else
  1116. /* New way */
  1117.     if (spackets < 3) return;
  1118.     debug(F101,"rcalcpsiz numerrs","",numerrs);
  1119.     debug(F101,"rcalcpsiz spsiz","",spsiz);
  1120.     if (numerrs)
  1121.       spsiz = spsiz / 2;
  1122.     else
  1123.       spsiz = (spsiz / 4) * 5;
  1124.     if (spsiz < 20) spsiz = 20;
  1125.     if (spsiz > spmax) spsiz = spmax;
  1126.     debug(F101,"rcalcpsiz new spsiz","",spsiz);
  1127.     numerrs = 0;
  1128.     return;
  1129. #endif
  1130. }
  1131.  
  1132. /*  R E S E N D  --  Retransmit packet n.  */
  1133.  
  1134. /*
  1135.   Returns 0 or positive on success (the number of retries for packet n).
  1136.   On failure, returns a negative number, and an error message is placed
  1137.   in recpkt.
  1138. */
  1139. int
  1140. resend(n) int n; {            /* Send packet n again. */
  1141.     int j, k;
  1142.  
  1143.     debug(F101,"resend seq","",n);
  1144.  
  1145.     k = chkwin(n,winlo,wslots);        /* See if packet in current window */
  1146.     j = -1;                /* Assume it's lost */
  1147.     if (k == 0) j = sseqtbl[n];        /* See if we still have a copy of it */
  1148.     if (k != 0 || j < 0) {        /* If not.... */
  1149.     if (nakstate && k == 1) {
  1150. /*
  1151.   Packet n is in the previous window and we are the file receiver.
  1152.   We already sent the ACK and deallocated its buffer so we can't just 
  1153.   retransmit the ACK.  Rather than give up, we try some tricks...
  1154. */
  1155.         if (n == 0 && spackets < 63 && myinit[0]) { /* ACK to Send-Init */
  1156. /*
  1157.   If the packet number is 0, and we're at the beginning of a protocol
  1158.   operation (spackets < 63), then we have to resend the ACK to an I or S
  1159.   packet, complete with parameters in the data field.  So we take a chance and
  1160.   send a copy of the parameters in an ACK packet with block check type 1.
  1161. */
  1162.         int bctlsav;        /* Temporary storage */
  1163.         int bctusav;
  1164.         bctlsav = bctl;        /* Save current block check length */
  1165.         bctusav = bctu;        /* and type */
  1166.         bctu = bctl = 1;    /* Set block check to 1 */
  1167.         spack('Y',0,(int)strlen((char *)myinit),(CHAR *)myinit);
  1168.         logpkt('#',n,(CHAR *)"<reconstructed>"); /* Log it */
  1169.         bctu = bctusav;        /* Restore block check type */
  1170.         bctl = bctlsav;        /* and length */
  1171.  
  1172.         } else {            /* Not the first packet */
  1173. /*
  1174.   It's not the first packet of the protocol operation.  It's some other packet
  1175.   that we have already ACK'd and forgotten about.  So we take a chance and
  1176.   send an empty ACK using the current block-check type.  Usually this will
  1177.   work out OK (like when acking Data packets), and no great harm will be done
  1178.   if it was some other kind of packet (F, etc).  If we are requesting an
  1179.   interruption of the file transfer, the flags are still set, so we'll catch
  1180.   up on the next packet.
  1181. */
  1182.         spack('Y',n,0,(CHAR *) "");
  1183. #ifdef COMMENT
  1184. /* It's already logged by spack(). */
  1185.         logpkt('#',n,(CHAR *)"<faith>"); /* Log it */
  1186. #endif /* COMMENT */
  1187.         }
  1188.         retrans++;
  1189.         screen(SCR_PT,'%',(long)pktnum,"(resend)");
  1190.         return(0);
  1191.     } else {
  1192. /*
  1193.   Packet number is not in current or previous window.  We seem to hit this
  1194.   code occasionally at the beginning of a transaction, for apparently no good
  1195.   reason.  Let's just log it for debugging, send nothing, and try to proceed
  1196.   with the protocol rather than killing it.
  1197. */
  1198.         debug(F101,"RESEND PKT NOT IN WINDOW","",n);
  1199.         debug(F101,"RESEND k","",k);
  1200. #ifdef COMMENT
  1201.         sprintf((char *)recpkt,
  1202.             "   resend error: NIW, n=%d, k=%d.",n,k);
  1203.         return(-2);
  1204. #else
  1205.         return(0);
  1206. #endif /* COMMENT */
  1207.     }
  1208.     }
  1209.  
  1210. /* OK, it's in the window and it's not lost. */
  1211.  
  1212.     debug(F101,"resend pktinfo index","",k);
  1213.  
  1214.     if (s_pkt[j].pk_rtr++ > maxtry) {    /* Found it but over retry limit */
  1215.     strcpy((char *)recpkt,"Too many retries.");
  1216.     return(-1);
  1217.     }
  1218.     debug(F101," retry","",s_pkt[j].pk_rtr); /* OK so far */
  1219.     dumpsbuf();                /* (debugging) */
  1220.     if (s_pkt[j].pk_typ == ' ') {    /* Incompletely formed packet */
  1221.     if (nakstate) {            /* (This shouldn't happen any more) */
  1222.         nack(n);
  1223.         retrans++;
  1224.         screen(SCR_PT,'%',(long)pktnum,"(resend)");
  1225.         return(s_pkt[j].pk_rtr);
  1226.     } else {            /* No packet to resend! */
  1227. #ifdef COMMENT
  1228. /*
  1229.   This happened (once) while sending a file with 2 window slots and typing
  1230.   X to the sender to cancel the file.  But since we're cancelling anyway,
  1231.   no need to give a scary message.
  1232. */
  1233.         sprintf((char *)recpkt,
  1234.             "resend logic error: NPS, n=%d, j=%d.",n,j); 
  1235.         return(-2);
  1236. #else
  1237. /* Just ignore it. */
  1238.         return(0);
  1239. #endif /* COMMENT */
  1240.     }
  1241.     }
  1242.     ttol(s_pkt[j].pk_adr,s_pkt[j].pk_len); /* Everything ok, send the packet */
  1243.     retrans++;                /* Count a retransmission */
  1244.     screen(SCR_PT,'%',(long)pktnum,"(resend)");    /* Tell user about resend */
  1245.     logpkt('S',n,s_pkt[j].pk_adr);    /* Log the resent packet */
  1246.     return(s_pkt[j].pk_rtr);        /* Return the number of retries. */
  1247. }
  1248.  
  1249. int
  1250. errpkt(reason) CHAR *reason; {        /* Send an error packet. */
  1251.     int x, y;
  1252.     encstr(reason);
  1253.     y = spack('E',pktnum,size,encbuf+7);
  1254.     x = quiet; quiet = 1;         /* Close files silently. */
  1255.     clsif(); clsof(1);
  1256.     quiet = x;
  1257. #ifdef COMMENT
  1258.     screen(SCR_TC,0,0l,"");
  1259. #endif /* COMMENT */
  1260.     if (what < W_CONNECT)
  1261.       xitsta |= what;            /* Remember what failed. */
  1262.     success = 0;
  1263.     return(y);
  1264. }
  1265.  
  1266. /* scmd()  --  Send a packet of the given type */
  1267.  
  1268. int
  1269. #ifdef CK_ANSIC
  1270. scmd(char t, CHAR *dat)
  1271. #else
  1272. scmd(t,dat) char t; CHAR *dat;
  1273. #endif /* CK_ANSIC */
  1274. /* scmd */ {
  1275.     encstr(dat);            /* Encode the command string */
  1276.     spack(t,pktnum,size,(CHAR *)(encbuf+7));
  1277.     return(0);
  1278. }
  1279.  
  1280. VOID
  1281. srinit() {                /* Send R (GET) packet */
  1282.     encstr((CHAR *)cmarg);        /* Encode the filename. */
  1283.     spack('R',pktnum,size,encbuf+7);    /* Send the packet. */
  1284. }
  1285.  
  1286. /* R P A C K  --  Read a Packet */
  1287.  
  1288. /*
  1289.  rpack reads a packet and returns the packet type, or else Q if the
  1290.  packet was invalid, or T if a timeout occurred.  Upon successful return, sets
  1291.  the values of global rsn (received sequence number),  rln (received
  1292.  data length), and rdatap (pointer to null-terminated data field).
  1293. */
  1294. int
  1295. rpack() {
  1296.     register int i, j, x, lp;        /* Local variables */
  1297.     int k, type, chklen;
  1298.     unsigned crc;
  1299.     CHAR pbc[4];            /* Packet block check */
  1300.     CHAR *sohp;                /* Pointer to SOH */
  1301.     CHAR e;                /* Packet end character */
  1302.  
  1303.     debug(F101,"entering rpack, pktnum","",pktnum);
  1304.     k = getrbuf();            /* Get a new packet input buffer. */
  1305.     debug(F101,"rpack getrbuf","",k);
  1306.     if (k < 0) return(-1);        /* Return like this if none free. */
  1307.     recpkt = r_pkt[k].bf_adr;
  1308.     *recpkt = '\0';            /* Clear receive buffer. */
  1309.     sohp = recpkt;            /* Initialize pointers to it. */
  1310.     rdatap = recpkt;
  1311.     rsn = rln = -1;            /* In case of failure. */
  1312.     e = (turn) ? turnch : eol;        /* Use any handshake char for eol */
  1313.  
  1314. /* Try to get a "line". */
  1315.  
  1316. #ifdef PARSENSE
  1317. #ifdef UNIX
  1318. /*
  1319.   So far the final turn argument is only for ck[uvd]tio.c.  Should be added
  1320.   to the others too.  (turn == handshake character.)
  1321. */
  1322.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,timint,e,stchr,turn);
  1323. #else
  1324. #ifdef VMS
  1325.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,timint,e,stchr,turn);
  1326. #else
  1327. #ifdef datageneral
  1328.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,timint,e,stchr,turn);
  1329. #else
  1330.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,timint,e,stchr);
  1331. #endif /* datageneral */
  1332. #endif /* VMS */
  1333. #endif /* UNIX */
  1334.     if (parity != ttprty) autopar = 1;
  1335.     parity = ttprty; 
  1336. #else /* !PARSENSE */
  1337.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,timint,e);
  1338. #endif /* PARSENSE */
  1339.     if (j < 0) {
  1340.     debug(F101,"rpack: ttinl fails","",j); /* Otherwise, */
  1341.     freerbuf(k);            /* Free this buffer */
  1342.     if (j < -1) {            /* Bail out if ^C^C typed. */
  1343.         debug(F101,"rpack ^C server","",server);
  1344.         debug(F101,"rpack ^C en_fin","",en_fin);
  1345.         if (server == 0) return(j);    /* But not if in server mode */
  1346.         else if (en_fin) return(j);    /* with DISABLE FINISH */
  1347.     }
  1348.     if (nakstate)                   /* call it a timeout. */
  1349.       screen(SCR_PT,'T',(long)winlo,"");
  1350.     else
  1351.       screen(SCR_PT,'T',(long)pktnum,"");
  1352.     logpkt('r',-1,(CHAR *)"<timeout>");
  1353.     if (flow == 1) ttoc(XON);    /* In case of Xoff blockage. */
  1354.     return('T');
  1355.     }
  1356.     rpktl = j;
  1357.     tlci += j;                /* All OK, Count the characters. */
  1358.     flci += j;
  1359.  
  1360. #ifndef PARSENSE
  1361. /* THEN eliminate this loop... */
  1362.     for (i = 0; (recpkt[i] != stchr) && (i < j); i++)
  1363.       sohp++;                /* Find mark */
  1364.     if (i++ >= j) {            /* Didn't find it. */
  1365.     logpkt('r',-1,"<timeout>");
  1366.     freerbuf(k);
  1367.     return('T');
  1368.     }    
  1369. #else
  1370.     i = 1;
  1371. #endif /* PARSENSE */
  1372.  
  1373.     rpackets++;
  1374.     lp = i;                /* Remember LEN position. */
  1375.     if ((j = xunchar(recpkt[i++])) == 0) {
  1376.         if ((j = lp+5) > MAXRP) return('Q'); /* Long packet */
  1377.     x = recpkt[j];            /* Header checksum. */
  1378.     recpkt[j] = '\0';        /* Calculate & compare. */
  1379.     if (xunchar(x) != chk1(recpkt+lp)) {
  1380.         freerbuf(k);
  1381.         logpkt('r',-1,(CHAR *)"<crunched:hdr>");
  1382.         return('Q');
  1383.     }
  1384.     recpkt[j] = x;            /* Checksum ok, put it back. */
  1385.     rln = xunchar(recpkt[j-2]) * 95 + xunchar(recpkt[j-1]) - bctl;
  1386.     j = 3;                /* Data offset. */
  1387.     } else if (j < 3) {
  1388.     debug(F101,"rpack packet length less than 3","",j);
  1389.     freerbuf(k);
  1390.     logpkt('r',-1,(CHAR *)"<crunched:len>");
  1391.     return('Q');
  1392.     } else {
  1393.     rln = j - bctl - 2;        /* Regular packet */
  1394.     j = 0;                /* No extended header */
  1395.     }
  1396.     rsn = xunchar(recpkt[i++]);        /* Sequence number */
  1397.     logpkt('r',rsn,sohp);
  1398.     if (rsn < 0 || rsn > 63) {
  1399.     debug(F101,"rpack bad sequence number","",rsn);
  1400.     freerbuf(k);
  1401.     logpkt('r',rsn,(CHAR *)"<crunched:seq>");
  1402.     return('Q');
  1403.     }
  1404. /*
  1405.   If this packet has the same type as the packet just sent, assume it is
  1406.   an echo and ignore it.  Don't even bother with the block check calculation:
  1407.   even if the packet is corrupted, we don't want to NAK an echoed packet.
  1408.   (And we certainly don't want to NAK an ACK or NAK!)
  1409. */
  1410.     type = recpkt[i++];            /* Get packet's TYPE field */
  1411.     if (type == sndtyp || (nakstate && (type == 'N' /* || type == 'Y' */ ))) {
  1412.     debug(F000,"rpack echo","",type); /* If it's an echo */
  1413.     freerbuf(k);            /* Free this buffer */
  1414.     logpkt('#',rsn,(CHAR *)"<echo:ignored>");
  1415.     return('e');            /* return special (lowercase) code */
  1416.     }
  1417. /*
  1418.   Separate the data from the block check, accounting for the case where    
  1419.   a packet was retransmitted after the block check switched.
  1420. */
  1421.     if (type == 'I' || type == 'S') {    /* I & S packets always have type 1 */
  1422.     chklen = 1;
  1423.     rln = rln + bctl - 1;
  1424.     } else if (type == 'N') {        /* A NAK packet never has data */
  1425.     chklen = xunchar(recpkt[lp]) - 2;
  1426.     rln = rln + bctl - chklen;
  1427.     } else chklen = bctl;
  1428.     debug(F101,"rpack bctl","",bctl);
  1429.     debug(F101,"rpack chklen","",chklen);
  1430.  
  1431.     i += j;                /* Buffer index of DATA field */
  1432.     rdatap = recpkt+i;            /* Pointer to DATA field */
  1433.     if ((j = rln + i) > r_pkt[k].bf_len ) { /* Make sure it fits */
  1434.     debug(F101,"packet sticks out too far","",j);
  1435.     freerbuf(k);
  1436.     logpkt('r',rsn,(CHAR *)"<overflow>");
  1437.     return('Q');
  1438.     }
  1439.  
  1440.     for (x = 0; x < chklen; x++)    /* Copy the block check */
  1441.       pbc[x] = recpkt[j+x];
  1442.     pbc[x] = '\0';            /* Null-terminate block check string */
  1443.     recpkt[j] = '\0';            /*  and the packet DATA field. */
  1444.     
  1445.     if (chklen == 2 && bctu == 4) {    /* Adjust for Blank-Free-2 */
  1446.     chklen = 4;            /* (chklen is now a misnomer...) */
  1447.     debug(F100,"rpack block check B","",0);
  1448.     }
  1449.     switch (chklen) {            /* Check the block check */
  1450.     case 1:                /* Type 1, 6-bit checksum */
  1451.         if (xunchar(*pbc) != chk1(recpkt+lp)) {
  1452.         debug(F110,"checked chars",recpkt+lp,0);
  1453.             debug(F101,"block check","",(int) xunchar(*pbc));
  1454.         debug(F101,"should be","",chk1(recpkt+lp));
  1455.         freerbuf(k);
  1456.         logpkt('r',-1,(CHAR *)"<crunched:chk1>");
  1457.         return('Q');
  1458.          }
  1459.         break;
  1460.     case 2:                /* Type 2, 12-bit checksum */
  1461.         x = xunchar(*pbc) << 6 | xunchar(pbc[1]);
  1462.         if (x != chk2(recpkt+lp)) {    /* No match */
  1463.         if (type == 'E') {    /* Allow E packets to have type 1 */
  1464.             recpkt[j++] = pbc[0];    
  1465.             recpkt[j] = '\0';
  1466.             if (xunchar(pbc[1]) == chk1(recpkt+lp))
  1467.               break;
  1468.             else
  1469.               recpkt[--j] = '\0';
  1470.         }
  1471.         debug(F110,"checked chars",recpkt+lp,0);
  1472.             debug(F101,"block check","", x);
  1473.         debug(F101,"should be","", (int) chk2(recpkt+lp));
  1474.         freerbuf(k);
  1475.         logpkt('r',-1,(CHAR *)"<crunched:chk2>");
  1476.         return('Q');
  1477.         }
  1478.         break;
  1479.     case 3:                /* Type 3, 16-bit CRC */
  1480.         crc = (xunchar(pbc[0]) << 12)
  1481.             | (xunchar(pbc[1]) << 6)
  1482.         | (xunchar(pbc[2]));
  1483.         if (crc != chk3(recpkt+lp)) {
  1484.         if (type == 'E') {    /* Allow E packets to have type 1 */
  1485.             recpkt[j++] = pbc[0];    
  1486.             recpkt[j++] = pbc[1];
  1487.             recpkt[j] = '\0';
  1488.             if (xunchar(pbc[2]) == chk1(recpkt+lp))
  1489.               break;
  1490.             else { j -=2; recpkt[j] = '\0'; }
  1491.         }
  1492.         debug(F110,"checked chars",recpkt+lp,0);
  1493.             debug(F101,"block check","",xunchar(*pbc));
  1494.         debug(F101,"should be","",(int) chk3(recpkt+lp));
  1495.         freerbuf(k);
  1496.         logpkt('r',-1,(CHAR *)"<crunched:chk3>");
  1497.         return('Q');
  1498.         }
  1499.         break;
  1500.     case 4:                /* Type 4 = Type 2, no blanks. */
  1501.         x = (unsigned)((xunchar(*pbc) - 1) << 6) |
  1502.             (unsigned)(xunchar(pbc[1]) - 1);
  1503.         if (x != chk2(recpkt+lp)) {
  1504.         if (type == 'E') {    /* Allow E packets to have type 1 */
  1505.             recpkt[j++] = pbc[0];    
  1506.             recpkt[j] = '\0';
  1507.             if (xunchar(pbc[1]) == chk1(recpkt+lp))
  1508.               break;
  1509.             else
  1510.               recpkt[--j] = '\0';
  1511.         }
  1512.         debug(F101,"bad type B block check","",x);
  1513.         freerbuf(k);
  1514.         logpkt('r',-1,(CHAR *)"<crunched:chkb>");
  1515.         return('Q');
  1516.         }
  1517.         break;
  1518.     default:            /* Shouldn't happen... */
  1519.         freerbuf(k);
  1520.         logpkt('r',-1,(CHAR *)"<crunched:chkx>");
  1521.         return('Q');
  1522.     }
  1523.     debug(F101,"rpack block check OK","",rsn);
  1524.  
  1525. /* Now we can believe the sequence number, and other fields. */
  1526. /* Here we violate strict principles of layering, etc, and look at the  */
  1527. /* packet sequence number.  If there's already a packet with the same   */
  1528. /* number in the window, we remove this one so that the window will not */
  1529. /* fill up. */
  1530.  
  1531.     if ((x = rseqtbl[rsn]) != -1) {    /* Already a packet with this number */
  1532.     retrans++;            /* Count it for statistics */
  1533.     debug(F101,"rpack got dup","",rsn);
  1534.     logpkt('r',rsn,(CHAR *)"<duplicate>");
  1535.     freerbuf(x);            /* Free old buffer, keep new packet. */
  1536.     r_pkt[k].pk_rtr++;        /* Count this as a retransmission. */
  1537.     }
  1538.  
  1539. /* New packet, not seen before, enter it into the receive window. */
  1540.  
  1541.     rseqtbl[rsn] = k;            /* Make back pointer */
  1542.     r_pkt[k].pk_seq = rsn;        /* Record in packet info structure */
  1543.     r_pkt[k].pk_typ = type;        /* Sequence, type,... */
  1544.     r_pkt[k].pk_adr = rdatap;        /* pointer to data buffer */
  1545.     screen(SCR_PT,(char)type,(long)rsn,(char *)sohp); /* Update screen */
  1546.     return(type);            /* Return packet type */
  1547. }
  1548.  
  1549. /*  L O G P K T  --  Log packet number n, pointed to by s.  */
  1550.  
  1551. /* c = 's' (send) or 'r' (receive) */
  1552.  
  1553. VOID
  1554. #ifdef CK_ANSIC
  1555. logpkt(char c,int n, CHAR *s)
  1556. #else
  1557. logpkt(c,n,s) char c; int n; CHAR *s;
  1558. #endif /* CK_ANSIC */
  1559. /* logpkt */ {
  1560.     char plog[20];
  1561.     if (pktlog && *s) {
  1562.     if (n < 0)
  1563.       sprintf(plog,"%c-xx-%02d-",c,(gtimer()%60));
  1564.     else
  1565.       sprintf(plog,"%c-%02d-%02d-",c,n,(gtimer()%60));
  1566.     if (zsout(ZPFILE,plog) < 0) pktlog = 0;
  1567.     else if (zsoutl(ZPFILE,(char *)s) < 0) pktlog = 0;
  1568.     }
  1569. }
  1570.  
  1571. #ifdef TLOG
  1572.  
  1573. /*  T S T A T S  --  Record statistics in transaction log  */
  1574.  
  1575. VOID
  1576. tstats() {
  1577.     char *tp;
  1578.     ztime(&tp);                /* Get time stamp */
  1579.     tlog(F110,"End of transaction",tp,0L);  /* Record it */
  1580.  
  1581.     if (filcnt < 1) return;        /* If no files, done. */
  1582.  
  1583. /* If multiple files, record character totals for all files */
  1584.  
  1585.     if (filcnt > 1) {
  1586.     tlog(F101," files","",filcnt);
  1587.     tlog(F101," total file characters   ","",tfc);
  1588.     tlog(F101," communication line in   ","",tlci);
  1589.     tlog(F101," communication line out  ","",tlco);
  1590.     }
  1591.  
  1592. /* Record timing info for one or more files */
  1593.  
  1594.     tlog(F101," elapsed time (seconds)  ","",(long) tsecs);
  1595.     if (tsecs > 0) {
  1596.     long lx;
  1597.     lx = (tfc * 10L) / (long) tsecs;
  1598.     tlog(F101," effective data rate     ","",lx/10L);
  1599.     if (speed <= 0L) speed = ttgspd();
  1600.     if (speed > 0L && speed != 8880L && network == 0) {
  1601.         lx = (lx * 100L) / speed;
  1602.         tlog(F101," efficiency (percent)    ","",lx);
  1603.     }
  1604.     }
  1605.     tlog(F100,"","",0L);        /* Leave a blank line */
  1606. }
  1607.  
  1608. /*  F S T A T S  --  Record file statistics in transaction log  */
  1609.  
  1610. VOID
  1611. fstats() {
  1612.     tfc += ffc;
  1613.     tlog(F100," end of file","",0L);
  1614.     tlog(F101,"  file characters        ","",ffc);
  1615.     tlog(F101,"  communication line in  ","",flci);
  1616.     tlog(F101,"  communication line out ","",flco);
  1617. }
  1618. #else /* NOTLOG */
  1619. VOID
  1620. tstats() {}
  1621.  
  1622. VOID
  1623. fstats() {
  1624.     tfc += ffc;
  1625. }
  1626. #endif /* TLOG */
  1627.