home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / fido / pup_v2b.zip / XMODEM.C < prev   
C/C++ Source or Header  |  1987-12-25  |  19KB  |  762 lines

  1. #include <puppy.h>
  2. #include <ascii.h>
  3. #include <pupmem.h>
  4.  
  5. /*
  6.     XMODEM, TELINK drivers
  7.  
  8.     T. Jennings 20 Sep 87
  9.  
  10. */
  11.  
  12. /* Protocol driver return codes */
  13.  
  14. #define OK 0        /* alls well */
  15. #define ERROR -1    /* generic error */
  16. #define ABORT -2    /* manually aborted */
  17. #define DISKFULL -3    /* disk full error */
  18.  
  19. /* Global statistics */
  20.  
  21. extern int totl_files;    /* how many sent/received */
  22. extern int totl_blocks;    /* 128 bytes blocks */
  23. extern int totl_errors;    /* block retries, etc */
  24.  
  25. static FLAG diverter;    /* packeter/unpacketer byte diverter */
  26.  
  27. /* Transmit one or more files. The list fn contains one or more file names;
  28. if explicit is true, then they should be used as is else the system path
  29. name should be used. */
  30.  
  31. transmit(fn,namelist,listlen)
  32. char *fn;        /* filename for XMODEM */
  33. char *namelist;        /* list of sent names */
  34. int listlen;        /* max. len of the list */
  35. {
  36. char fspec[SS];        /* full file to search for */
  37. char fname[SS];        /* full filename to send */
  38. int i,f,files;
  39. int error;
  40. struct _fileinfo fileinfo;
  41. FLAG dofn;        /* 1 == do modem7 filename */
  42. FLAG dotb;        /* 1 == do telink block */
  43.  
  44.     switch (filemode) {
  45.         case TELINK:    diverter= 0; dotb= 1; dofn= 1; break;
  46.         case MODEM7:    diverter= 0; dotb= 0; dofn= 1; break;
  47.         case XMODEM:    diverter= 0; dotb= 0; dofn= 0; break;
  48.         case FIDONET:    diverter= 1; dotb= 0; dofn= 0; break;
  49.     }
  50.  
  51.     while (*fn) {
  52.         cpyarg(fspec,fn);            /* get an atom, */
  53.         fn= next_arg(fn);            /* for next time */
  54.         i= 0; 
  55.         while (getinfo(fspec,i++,&fileinfo)) {
  56.             strip_path(fname,fspec);    /* get the prefix, */
  57.             strcat(fname,fileinfo.name);    /* add the filename */
  58.             xferstat(1,"File: %s",fname);    /* display it, */
  59.  
  60.             if (dofn) error= sendfname(fileinfo.name); else error= OK;
  61.             if (error != OK) break;        /* ERROR or ABORT */
  62.  
  63.             f= open(fname,0);        /* open it, */
  64.             if (f == -1) {
  65.                 xferstat(2,"Can't open \"%s\"",fname);
  66.                 continue;
  67.             }
  68.             error= sendfile(f,dotb,&fileinfo);
  69.             close(f);            /* close the file, */
  70.             if (error != OK) break;        /* check errors */
  71.             if (strlen(fname) < listlen) {    /* keep a list */
  72.                 strcat(namelist,fname);
  73.                 strcat(namelist," ");
  74.                 listlen -= strlen(fname);
  75.             }
  76.             ++totl_files;            /* count another */
  77.             ++files;
  78.         }
  79.         if (error != OK) break;
  80.     }
  81.     xmitdone(files);                /* complete transmission */
  82.     return(error);
  83. }
  84.  
  85. /* Complete transmission. */
  86.  
  87. xmitdone(files)
  88. int files;
  89. {
  90. int i;
  91.  
  92.     if ((filemode != XMODEM) && files) {
  93.         xferstat(1,"Last file");
  94.         do {i= modin(100);}
  95.         while ((i != -1) && (i != NAK) && (i != ETX));
  96.         if (i == NAK) {
  97.             xferstat(0,"NAK");
  98.             modout(ACK);        /* name ACK, */
  99.             modout(EOT);        /* no more, */
  100.         }
  101.         xferstat(1,"No More Files EOT");
  102.         modout(EOT);
  103.     }
  104. }
  105.  
  106. /* Receive files: the filename fn is used for XMODEM only; for TELINK
  107. it is a dummy. If explicit is true, the path from fn is used, else
  108. the system path is used. */
  109.  
  110. receive(fn,namelist,listlen)
  111. char *fn;        /* filename for XMODEM */
  112. char *namelist;        /* list of received names */
  113. int listlen;        /* max. len of the list */
  114. {
  115. int f,oops;
  116. char path[SS];        /* path/drive from input filename */
  117. char name[SS];        /* plain filename only */
  118. char fname[SS];        /* fully filename with prefix */
  119.  
  120. FLAG dofn;        /* 1 == do modem7 filename */
  121. FLAG dotb;        /* 1 == do telink block */
  122.  
  123.     cpyarg(name,strip_path(path,fn));        /* seperate filename/path */
  124.  
  125.     switch (filemode) {
  126.         case TELINK:    diverter= 0; dotb= 1; dofn= 1; break;
  127.         case MODEM7:    diverter= 0; dotb= 0; dofn= 1; break;
  128.         case XMODEM:    diverter= 0; dotb= 0; dofn= 0; break;
  129.         case FIDONET:    diverter= 1; dotb= 0; dofn= 0; break;
  130.     }
  131.  
  132.     while (1) {
  133.         if (dofn) {                /* for TELINK/MODEM7 */
  134.             oops= getfname(name);        /* get the filename */
  135.             if (oops != OK) {        /* remotely */
  136.                 xferstat(1,"Can't get a filename");
  137.                 break;            /* error */
  138.             }
  139.             if (badname(name)) name[1]= '$'; /* reserved filenames */
  140.         }
  141.         cpyarg(fname,path);            /* build the full */
  142.         strcat(fname,name);            /* filename */
  143.         xferstat(1,"File: %s",fname);        /* display it */
  144.  
  145.         f= open(fname,0);            /* make sure it does NOT */
  146.         if (f != -1) {                /* exist */
  147.             close(f);
  148.             xferstat(2,"File already exists!");
  149.             oops= ERROR;
  150.             break;
  151.         }
  152.         f= creat(fname,2);            /* create it, */
  153.         if (f == -1) {
  154.             xferstat(2,"Cannot create file");
  155.             break;
  156.         }
  157.         oops= getfile(f);            /* fill it, */
  158.         close(f);                /* close it, */
  159.  
  160.         if (oops != OK) break;            /* oops, ABORT or EOT */
  161.         if (strlen(fname) < listlen) {        /* keep a list */
  162.             strcat(namelist,fname);
  163.             strcat(namelist," ");
  164.             listlen -= strlen(fname);
  165.         }
  166.         ++totl_files;                /* another ... */
  167.         if (! dofn) break;            /* only one */
  168.     }
  169.     flush(0);                    /* flush garbage, */
  170.     return(oops);
  171. }        
  172.  
  173. /* Receive a MODEM7 filename, put into the passed array; return ERROR if error,
  174. OK if we get a filename, EOT if no more files. */
  175.  
  176. getfname(name)
  177. char *name;
  178. {
  179. int i;
  180. char aborts;
  181.  
  182.     aborts= 0;
  183.     for (i= 30; i-- > 0; ) {
  184.         switch (getfn(name)) {
  185.             case OK: return(OK);    /* got a name, */
  186.             case EOT: return(EOT);     /* no more files */
  187.             case ETX: if (++aborts > 2) return(ABORT);
  188.                 break;
  189.         }
  190.     }
  191.     return(ERROR);
  192. }
  193.  
  194. /* Get a single file from the remote computer. Return ERROR if something
  195. went wrong, SOH if the remote is not in batch mode, or 0 if transfer 
  196. complete. If batch mode, the name is a prefix we should stick on the 
  197. beginning of the disk file we create. Does CRC mode if that mode is set,
  198. or a newer TELINK is used, that transmits the CRC flag. */
  199.  
  200. getfile(f)
  201. int f;        /* open file */
  202. {
  203. int i;
  204. int blknum;                /* file block number, */
  205. BYTE sector;                /* XMODEM block number */
  206. BYTE buff[128];                /* XMODEM data block */
  207. int errors;                /* errors 0 - 9 */
  208. BYTE ackchar;                /* ACK/NAK */
  209. BYTE aborts;                /* # of Control-Cs in a row */
  210.  
  211. long fsize;                /* file size or -1 */
  212. long ftime;                /* creation time/date */
  213.  
  214.     blknum= 0;            /* file block number */
  215.     aborts= 0;            /* no Control-Cs yet */
  216.     errors= 0;            /* no errors */
  217.     fsize= -1L;            /* unknown so far */
  218.     ftime= -1L;
  219.     sector= 1;            /* XMODEM block, first time */
  220.     
  221.     ackchar= NAK;
  222.     if (crcmode) ackchar= 'C';    /* use right initial char, */
  223.  
  224.     while (errors < 10) {                /* retry count */
  225.         if (ackchar != ACK) xferstat(0,"NAK");    /* status report */
  226.         modout(ackchar);            /* previous ACK/NAK */
  227.         ++errors;                /* assume an error */
  228.         i= modin(400);                /* get initial character */
  229.         switch (i) {                /* see what it is */
  230.             case SOH:            /* XMODEM data */
  231.                 i= getblock(crcmode,buff); /* get sect number */
  232.                 ackchar= NAK;        /* assume bad, */
  233.                 if (i < 0) break;    /* check for error */
  234.  
  235.                 if (i < sector) {    /* duplicate */
  236.                     xferstat(1,"Duplicate");
  237.                     goto goodblk;    /* ACK it */
  238.  
  239.                 } else if (i > sector) {/* out of sync */
  240.                     xferstat(1,"Block Sync Error");
  241.                     break;
  242.                 }
  243.                 i= 128;            /* set bytes in buffer */
  244.                 if (fsize > 0L) {    /* correct if known */
  245.                     i= (fsize > 128L) ? 128 : fsize;
  246.                     fsize -= i;
  247.                 }
  248.                 if (put_buff(f,buff,i) != i) {
  249.                     xferstat(2,"DISK FULL!");
  250.                     ++totl_errors;
  251.                     return(DISKFULL);
  252.                 }
  253.                 ++totl_blocks;
  254.                 ++sector;
  255.                 ++blknum;
  256.  
  257. goodblk: ;            aborts= 0;
  258.                 errors= 0;
  259.                 ackchar= ACK;
  260.                 break;
  261.  
  262.             case SYN:            /* TELINK block */
  263.                 ackchar= NAK;        /* assume bad */
  264.                 if (getblock(0,buff) != 0) break;
  265.                 fsize= _ctol(&buff[0]);    /* FSC001-8 */
  266.                 ftime= _ctol(&buff[4]);    /* FSC001-8 */
  267.                 xferstat(1,"Telink Block");
  268.                 goto goodblk;
  269.  
  270.             case EOT:            /* end of file */
  271.                 xferstat(1,"End of File EOT");
  272.                 modout(ACK);        /* ACK the EOT, */
  273.                 if (ftime != -1L)    /* set filetime */
  274.                     _ftime(1,f,&ftime);
  275.                 return(OK);
  276.  
  277.             case ETX:            /* Control-C */
  278.                 xferstat(1,"Control-C");
  279.                 if (++aborts > 2) return(ABORT);
  280.                 break;
  281.  
  282.             case -1:            /* timeout */
  283.                 break;
  284.  
  285.             default:            /* garbage */
  286.                 flush(10);        /* quiet line */
  287.                 break;
  288.         }
  289.         xferstat(0,"Blk %u",blknum);
  290.         if (errors) flush(1);        /* flush garbage if error */
  291.     }
  292.     if (errors) {                /* if errors */
  293.         ++totl_errors;            /* flush the line */
  294.         flush(1);            /* was 100 */
  295.     }
  296.     return(ERROR);
  297. }
  298.  
  299. /* Send a filename, MODEM7 style. */
  300.  
  301. sendfname(name)
  302. char *name;
  303. {
  304. int i,ci,aborts;
  305.  
  306.     aborts= 0;
  307.     for (i= 10; i-- > 0; ) {
  308.         switch (sendfn(name)) {
  309.             case ETX: if (++aborts > 2) return(ABORT);
  310.             case OK: return(OK);
  311.         }
  312.     }
  313.     return(ERROR);
  314. }
  315.  
  316. /* Send a file in XMODEM. */
  317.  
  318. sendfile(file,statflg,fileinfo)
  319. int file;            /* open file */
  320. int statflg;            /* 1 == send TELINK block */
  321. struct _fileinfo *fileinfo;    /* file info struct */
  322. {
  323. char c;
  324. int i;
  325. int blknum;                /* file block number, */
  326. BYTE sector;                /* >>> 8 BIT <<< XMODEM block number */
  327. BYTE buff[128];                /* XMODEM data block */
  328. char errors;                /* errors 0 - 9 */
  329. char aborts;                /* # of Control-Cs */
  330.  
  331.     blknum= 0;            /* file block number */
  332.     sector= 1;            /* first XMODEM block number, */
  333.     aborts= 0;            /* no Control-Cs yet */
  334.  
  335.     crcmode= waitnak();            /* get initial NAK/CRC mode */
  336.     if (crcmode == ERROR) return(ERROR);    /* if not NAK/CRC, error */
  337.     if (statflg) sendtel(fileinfo);        /* file statistics */
  338.  
  339.     for (errors= 0; errors < 10; ) {        /* (no, not a mistake ...) */
  340.         i= get_buff(file,buff,128);        /* read some file data, */
  341.         if (! i) break;                /* end of file */
  342.         while (i < 128) buff[i++]= 26;        /* pad last block */
  343.  
  344.         for (errors= 0; errors < 10; ++errors) { /* (... its really OK) */
  345.             xferstat(0,"Block %u",blknum);    /* stats for the nosy sysop */
  346.             modout(SOH);            /* tell rcvr a block comes */
  347.             sendblock(sector,crcmode,buff);
  348.             i= getack();            /* get an acknowledge */
  349.             if (i == ACK) {
  350.                 ++sector;
  351.                 ++blknum;
  352.                 ++totl_blocks;
  353.                 errors= 0;
  354.                 break;
  355.  
  356.             } else {
  357.                 ++totl_errors;        /* another error, */
  358.                 flush(0);        /* flush garbage */
  359.                 if (i != NAK) errors= 10;
  360.             }
  361.         }
  362.     }
  363.  
  364. /* All blocks sent, or too many errors. Tell the remote no more. */
  365.  
  366.     xferstat(1,"");
  367.     if (! errors) {
  368.         for (i= 5; --i > 0; ) {
  369.             xferstat(0,"File EOT");
  370.             modout(EOT);
  371.             if (modin(1000) == ACK) break;
  372.             flush(0);
  373.         }
  374.         return(OK);
  375.     }
  376.     return(ERROR);
  377. }
  378.  
  379. /* Get an acknowledge character or timeout. Return the character
  380. received, or TIMEOUT for a timeout, ABORT for 3 Control-Cs in a row. */
  381.  
  382. getack() {
  383.  
  384. char aborts;
  385. int c;
  386.  
  387.     aborts= 0;
  388.  
  389.     while (1) {
  390.         c= modin(1000);
  391.         switch (c) {
  392.             case ACK: return(c);
  393.  
  394.             case NAK: xferstat(0,"NAK"); return(c);
  395.  
  396.             case ETX: xferstat(1,"Control-C");
  397.                 if (++aborts > 2) return(ABORT); 
  398.                 break;
  399.  
  400.             case -1: return(ERROR); break;
  401.         }
  402.     }
  403. }
  404.  
  405. /* Wait for an initial NAK or CRC; return 0 for NAK, 1 for CRC, or ERROR
  406. for anything else. */
  407.  
  408. waitnak() {
  409.  
  410. char aborts,errors,c;
  411.  
  412.     aborts= 0;
  413.     xferstat(0,"Waiting NAK");
  414.     for (errors= 60; errors-- > 0; ) {    /* 60 seconds max */
  415.  
  416.         switch (modin(100)) {
  417.             case NAK:
  418.                 xferstat(0,"NAK      ");
  419.                 return(0);
  420.  
  421.             case 'C':
  422.                 xferstat(0,"CRC      ");
  423.                 return(1);
  424.  
  425.             case ETX:
  426.                 xferstat(1,"Control-C ");
  427.                 if (++aborts > 2) return(ABORT);
  428.                 break;
  429.         }
  430.         flush(0);
  431.     }
  432.     return(ERROR);
  433. }
  434.  
  435. /* Attempt to send the TELINK data block. Do only 4 attempts; if not received 
  436. by then, assume the other end is not capable of receiving it. If not received, 
  437. then the file will be handled like XMODEM or MODEM7; time and date lost, 
  438. filesize rounded up to the nearest 128 bytes. */
  439.  
  440. sendtel(fileinfo)
  441. struct _fileinfo *fileinfo;
  442. {
  443. int i;
  444. char buffer[128],aborts;
  445.  
  446.     for (i= 0; i < sizeof(buffer); i++)
  447.         buffer[i]= 0;        /* clear data block, */
  448.  
  449.     cpyarg(&buffer[8],fileinfo-> name);/* install filename, */
  450.     strcpy(&buffer[25],"Pup sez: Hi");
  451.     buffer[41]= crcmode;        /* send the CRC flag, acknowledge it, */
  452.  
  453.     aborts= 0;
  454.     for (i= 4; i-- > 0; ) {
  455.         xferstat(0,"TELINK block");
  456.         modout(SYN);
  457.         sendblock(0,0,buffer);    /* send in checksum, */
  458.         switch (modin(1000)) {
  459.             case ACK: return(OK);
  460.             case ETX: if (++aborts > 2) return(ABORT); break;
  461.         }
  462.     }
  463.     return(ERROR);
  464. }
  465.  
  466. /* Get an XMODEM block, return its sequence number, or -1 if error. This
  467. gets the sector number, its 1's complement, the 128 data bytes and does
  468. the checksum or CRC. */
  469.  
  470. getblock(crcflag,buffer)
  471. int crcflag;
  472. char *buffer;
  473. {
  474. int i,v;
  475. int sector;
  476. int chksum;
  477.  
  478.     sector= modin(100);        /* get the sector number, */
  479.     chksum= modin(100);        /* 1's compl (use chksum as temp) */
  480.     if (sector + chksum != 255) return(-1); /* bad sector! */
  481.  
  482.     chksum= 0;            /* initialize check sum and CRC, */
  483.     clrcrc();            /* we maintain both in parallel, */
  484.     for (i= 0; i < 128; i++) {
  485.         v= modin(100);
  486.         if (v == -1) return(-1);
  487.         buffer[i]= v;
  488.         chksum += v;
  489.         updcrc(v);
  490.     }
  491.     if (crcflag) {            /* if CRC mode, get the two */
  492.         v= modin(100);        /* CRC bytes and do those, */
  493.         if (v == -1) return(-1);
  494.         updcrc(v);
  495.         v= modin(100);
  496.         if (v == -1) return(-1);
  497.         updcrc(v);
  498.         if (chkcrc()) return(-1); /* -1 if bad CRC */
  499.  
  500.     } else {            /* checksum */
  501.         v= modin(100);        /* get the checksum, */
  502.         if (v == -1) return(-1);
  503.         if (v != (chksum & 255)) return(-1);
  504.     }
  505.     return(sector);            /* good block */
  506. }
  507.  
  508. /* Transmit an XMODEM block with the specified sector number and checksum/CRC
  509. mode. No return value. */
  510.  
  511. sendblock(sector,crcflag,buffer)
  512. BYTE sector;
  513. int crcflag;
  514. char *buffer;
  515. {
  516. int i;
  517. WORD crc;
  518. BYTE chksum;
  519.  
  520.     modout(sector);            /* send the sector number */
  521.     modout(~sector);        /* and its complement */
  522.  
  523.     chksum= 0;            /* maintain both CRC and checksum, */
  524.     clrcrc();
  525.  
  526.     for (i= 0; i < 128; i++) modout(buffer[i]);
  527.     for (i= 0; i < 128; i++) {    /* output data then do CRC/checksum */
  528.         chksum += buffer[i];
  529.         updcrc(buffer[i]);
  530.     }
  531.  
  532.     flush(0);            /* flush out garbage, */
  533.     if (crcflag) {
  534.         crc= fincrc();        /* get CRC bytes, */
  535.         modout(crc >> 8);    /* MS byte first, */
  536.         modout(crc);        /* then LS byte, */
  537.  
  538.     } else modout(chksum);        /* send checksum, */
  539. }
  540.  
  541. /* Get data to send; if the diverter is on, get them from the packeter. 
  542. Return the number of bytes actually read. */
  543.  
  544. get_buff(f,buff,n)
  545. int f;            /* file handle, or dummy */
  546. char *buff;        /* buffer to put data in, */
  547. int n;            /* requested byte count */
  548. {
  549. int i;
  550.  
  551.     if (diverter) while (n--) {
  552.         i= get_pkt();        /* get a byte from the packeter, */
  553.         if (i == -1) break;    /* stop if "EOF" */
  554.         *buff++= i;        /* stuff it */
  555.  
  556.     } else n= read(f,buff,n);    /* else from a disk file */
  557.  
  558.     return(n);
  559. }
  560.  
  561. /* Output the buffer to the disk file or unpacketer. Return the number of bytes
  562. sucessfully written. */
  563.  
  564. put_buff(f,buff,n)
  565. int f;            /* file handle, or dummy */
  566. char *buff;        /* buffer to put data in, */
  567. int n;            /* requested byte count */
  568. {
  569. int i;
  570.  
  571.     if (diverter) while (n--) put_pkt(*buff++);    /* to the unpacketer, */
  572.     else n= write(f,buff,n);            /* or to the file */
  573.     return(n);                    /* say what we did */
  574. }
  575.  
  576. /* Transmit a filename for batch mode tranmission. Return ERROR if cant do it,
  577. or OK if sent properly. It is assumed that the receiver is ready to receive
  578. the filename we have to send. */
  579.  
  580. sendfn(name)
  581. char *name;
  582. {
  583. BYTE chksum;
  584. char c,localname[SS];
  585. int i;
  586.  
  587.     chksum= 0;                /* mis-use as Control-C counter */
  588.     for (i= 60; i-- > 0; ) {        /* get initial character */
  589.         c= modin(100);
  590.         if (c == NAK) break;        /* got name NAK */
  591.  
  592.         if (c == ETX) {            /* if Control-C */
  593.             if (++chksum > 2) return(ABORT);
  594.         }
  595.         flush(0);
  596.     }
  597.     if (! i) return(ERROR);            /* timeout */
  598.  
  599.     cvt_to_fcb(name,localname);        /* convert name, */
  600.     chksum= 0;                /* start checksum, */
  601.     modout(ACK);                /* all ready, */
  602.     for (i= 0; i < 11; i++) {
  603.         c= localname[i];        /* get name char, */
  604.         chksum += c;            /* maintain checksum, */
  605.         modout(c);            /* send name char, */
  606.         if (modin(200) != ACK) break;    /* if not an ACK */
  607.     }
  608.     if (i >= 11) {                /* if all sent OK */    
  609.         modout(SUB);            /* end of name, */
  610.         chksum += SUB;            /* stupid protocol */
  611.         if (modin(100) == (chksum & 0xff)) { /* get recvr's checksum, */
  612.             modout(ACK);        /* if good, say so, */
  613.             return(0);        /* return happy :-), */
  614.         }
  615.     }
  616.     modout('u');                /* else not sent OK, */
  617.     return(ERROR);                /* return sad :-( */
  618. }
  619.  
  620. /* Get a filename from the sender. */
  621.  
  622. getfn(name)
  623. char *name;
  624. {
  625. int i;
  626. int c;            /* NOTE: 'c' is an integer !!! */
  627. BYTE chksum;
  628. char newname[SS];
  629.  
  630.     modout(NAK);                    /* sync sender, */
  631.     c= modin(200);                    /* get sync character, */
  632.     if (c != ACK) return(c);
  633.  
  634.     chksum= 0;
  635.     for (i= 0; i < 11; i++) {            /* 11 char max, */
  636.         c= modin(100);                /* get a char, */
  637.         switch (c) {
  638.             case -1: return(ERROR);        /* timeout */
  639.             case 'u': return(ERROR);    /* error */
  640.             case EOT: return(EOT);        /* no more files */
  641.             case SUB: break;        /* end of name */
  642.             default:
  643.                 chksum += c;        /* file name char I guess */
  644.                 newname[i]= c;        /* stash it */
  645.                 modout(ACK);        /* say "ok" */
  646.                 break;
  647.         }
  648.     }
  649.     if (modin(200) == SUB) {            /* if end of name, */
  650.         modout(chksum & 0xff);            /* send check sum, */
  651.         if (modin(200) == ACK) {
  652.             cvt_from_fcb(newname,name);    /* fix name, */
  653.             return(OK);
  654.         }
  655.     }
  656.     return(ERROR);                    /* filename too long */
  657. }
  658.  
  659. /* Convert a CP/M like filename to a normal ASCIZ name. */
  660.  
  661. cvt_from_fcb(inname,outname)
  662. char *inname,*outname;
  663. {
  664. int i;
  665. char c;
  666.  
  667.     for (i= 8; --i > 0; ) {
  668.         c= fnc(*inname++);
  669.         if (c != ' ') *outname++= c;    /* ignore spaces */
  670.     }                    /* but do all 8 */
  671.     *outname++= '.';            /* add the dot, */
  672.  
  673.     for (i= 3; --i > 0; ) {
  674.         c= fnc(*inname++);
  675.         if (c == ' ') break;
  676.         *outname++= c;
  677.     }
  678.     *outname= NUL;                /* terminate it, */
  679. }
  680.  
  681. /* Fix this filename character */
  682.  
  683. fnc(c)
  684. char c;
  685. {
  686.     if ((c < ' ') || (c == '\\') || (c == '/')) c= '$';
  687.     return(c);
  688. }
  689.  
  690.  
  691. /* Convert a normal asciz string to MSDOS/CPM FCB format. Make the filename
  692. portion 8 characters, extention 3 maximum. Supports wildcards, skips drive
  693. specs. */
  694.  
  695. cvt_to_fcb(inname,outname)
  696. char *inname;
  697. char *outname;
  698. {
  699. char c;
  700. int i;
  701.  
  702.     if (inname[1] == ':') inname= &inname[2];
  703.     for (i= 0; i < 11; i++)
  704.         outname[i]= ' ';        /* clear out name, */
  705.     _cvt2(inname,outname,8);        /* do name portion, */
  706.     while (*inname) {            /* skip to dot, if any */
  707.         if (*inname++ == '.') break;
  708.     }
  709.     _cvt2(inname,&outname[8],3);        /* do extention, */
  710. }
  711. /* Do part of a name. */
  712.  
  713. _cvt2(inname,outname,n)
  714. char *inname,*outname;
  715. int n;
  716. {
  717. int i;
  718.     for (i= 0; i < n; i++) {        /* NAME PART */
  719.         if (*inname == '\0')        /* if null, */
  720.             break;            /* quit, */
  721.         else if (*inname == '*')    /* if *, fill with ?, */
  722.             outname[i]= '?';
  723.         else if (*inname == '.')    /* if a dot, */
  724.             break;            /* skip to extention, */
  725.         else {
  726.             outname[i]= toupper(*inname);
  727.             ++inname;
  728.         }
  729.     }
  730. }
  731.  
  732. /* Display a file transfer status message. N is how to handle it:
  733.  
  734.     0    CR only; stay on same line
  735.     1    CR/LF after text
  736.     2    CR/LF before & after text
  737.  */
  738.  
  739. xferstat(n,s)
  740. char *s;
  741. {
  742. char buff[SS * 4];
  743.  
  744.     if (n == 2) puts("\r\n");
  745.     switch (filemode) {
  746.         case XMODEM:    puts("-X"); break;
  747.         case TELINK:    puts("-T"); break;
  748.         case MODEM7:    puts("-B"); break;
  749.         case KERMIT:    puts("-K"); break;
  750.         case FIDONET:    puts("-F"); break;
  751.         case SEALINK:    puts("-S"); break;
  752.         case IFNA:    puts("-I"); break;
  753.     }
  754.     if (crcmode) puts("C");
  755.     puts(": ");    
  756.  
  757.     _spr(buff,&s);
  758.     puts(buff);
  759.  
  760.     puts("\r"); if (n) puts("\n");
  761. }
  762.