home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d109 / uupc.lha / UUpc / Source / dcpxfer.c < prev    next >
C/C++ Source or Header  |  1987-10-28  |  7KB  |  368 lines

  1. /*            dcpxfer.c
  2.  
  3.             Revised edition of dcp
  4.  
  5.             Stuart Lynne May/87
  6.  
  7.             Copyright (c) Richard H. Lamb 1985, 1986, 1987
  8.             Changes Copyright (c) Stuart Lynne 1987
  9.  
  10. */
  11. /* "DCP" a uucp clone. Copyright Richard H. Lamb 1985,1986,1987 */
  12. /* file send routines */
  13. #include "dcp.h" 
  14. #include <ctype.h>
  15.  
  16. static unsigned char    rpacket[MAXPACK];
  17. static unsigned char    spacket[MAXPACK];
  18. /* */
  19. /***************SEND PROTOCOL***************************/
  20. /*
  21.  *  s d a t a
  22.  *
  23.  *  Send File Data
  24.  */
  25. sdata()
  26. {
  27.  
  28.     while( TRUE ) {
  29.     
  30.         if (( *sendpkt) ( spacket, size, 0 )) 
  31.             return (0 );     /* send data */
  32.         if (( size = bufill( spacket )) == 0 )  /* Get data from file */
  33.             return( 'Z' );                         /* If EOF set state to that */
  34.     }
  35.     
  36.     return('D');/* Got data, stay in state D */
  37.  
  38. }
  39.  
  40.  
  41. /*
  42.  *  b u f i l l
  43.  *
  44.  *  Get a bufferful of data from the file that's being sent.
  45.  *  Only control-quoting is done; 8-bit & repeat count prefixes are
  46.  *  not handled.
  47.  */
  48. bufill(buffer)
  49. char    *buffer;/* Buffer */
  50. {
  51.     return( read(fp, buffer, pktsize) );/* Handle partial buffer */
  52. }
  53.  
  54.  
  55. /*
  56.  *  s b r e a k
  57.  *
  58.  *  Send Break (EOT)
  59.  */
  60. sbreak()
  61. {
  62.     int    len, i;
  63.     sprintf(spacket, "H");
  64.     if ((*sendpkt)(spacket, 0, 1)) 
  65.         return(0);
  66.     if ((*getpkt)(spacket, &len)) 
  67.         return(0);
  68.     printmsg( 2, "Switch modes" );
  69.     if (spacket[1] == 'N') 
  70.         return('G');
  71.     return('Y');
  72. }
  73.  
  74.  
  75. /* */
  76. /*
  77.  *  s e o f
  78.  *
  79.  *  Send End-Of-File.
  80.  */
  81. seof()
  82. {
  83.     int    len, i;
  84.     if ((*sendpkt)(spacket, 0, 0)) 
  85.         return(0);
  86.     if ((*getpkt)(spacket, &len)) 
  87.         return(0); /* rec CY or CN */
  88.     if (strncmp(spacket, "CY", 2)) 
  89.         return(0); /* cant send file */
  90.     close(fp);
  91.     fp = (-1);
  92.      importpath( hostfile, fromfile );
  93.     unlink(fromfile);
  94.     printmsg( 0, "transfer %s complete,%d", fromfile, fp );
  95.  
  96.     /*
  97.     fprintf( syslog, "%s!%s (%d/%d-%d:%d:%d) -> %ld / %ld secs", host, id, size, secs );
  98.     */
  99.     
  100.     return('F');                    /* go get the next file to send */
  101. }
  102.  
  103.  
  104. /* */
  105. /*
  106.  *  s f i l e
  107.  *
  108.  *  Send File Header.
  109.  */
  110. sfile()
  111. {
  112.     int    i, len;
  113.     char * cp;
  114.     if (fp == -1) {/* If not already open, */
  115.         printmsg( 3, "looking for next file..." );
  116.         if (getfile()) { /* get next file from current work*/
  117.             fclose( fw );
  118.             unlink( cfile );/* close and delete completed workfile */
  119.             fw = (char *)NULL;
  120.             return('B'); /* end sending session */
  121.         }
  122.  
  123.          importpath( hostfile, fromfile );
  124.  
  125.         printmsg( 3, "   Opening %s for sending.", fromfile );
  126.         fp = open(fromfile, 0);/* open the file to be sent */
  127.         if (fp == -1) {/* If bad file pointer, give up */
  128.             printmsg( 0, "Cannot open file %s", fromfile );
  129.             return('A');
  130.         }
  131.     } else 
  132.         return('A'); /* If somethings already open. were in trouble*/
  133.     printmsg( 0, "Sending %s as %s", fromfile, tofile);
  134.     strcpy(spacket, tofile);
  135.     if ((*sendpkt)(spacket, 0, 1)) 
  136.         return(0);       /* send S fromfile tofile */
  137.     if ((*getpkt)(spacket, &len)) 
  138.         return(0);       /* user - tofile 0666. */
  139.     if (spacket[1] != 'Y') 
  140.         return('A');             /* If otherside says no-quit*/
  141.     size = bufill(spacket);
  142.     return('D');
  143. }
  144.  
  145.  
  146. /* */
  147. /*
  148.  *  s i n i t
  149.  *
  150.  *  Send Initiate: send this host's parameters and get other side's back.
  151.  */
  152. sinit()
  153. {
  154.     if ((*openpk)()) 
  155.         return('A');
  156.     return('B');
  157. }
  158.  
  159.  
  160. /* */
  161. /*
  162.  *
  163.  *
  164.  *  getfile
  165.  *
  166.  *  getfile reads the next line from the presently open workfile
  167.  *  (cfile) and determines from this the next file to be sent
  168.  *  (file). If there are no more TRUE is returned.
  169.  *  --A fix for "R from to 0666" should be done here to recieve files
  170.  *  --in addition to sending them. The appropriate "state letter"
  171.  *  --i.e. "R" should be returned to the send "master" or "slave"
  172.  *  --state switching table in "dcp.c"
  173.  *  --I did not implement this since the majority of uucp transactions
  174.  *  --appear to be "S from to 0666" type. RHLamb 1/87
  175.  *
  176.  */
  177. getfile()
  178. {
  179.     int    i;
  180.     char    line[132];
  181.     register char * cp;
  182.     
  183.     if ( fgets( line, BUFSIZ, fw ) == (char *)NULL )
  184.         return(TRUE);
  185.         
  186.     sscanf(&line[2], "%s ", fromfile);
  187.     for ( i = 0, cp = line; *cp!='\0'; i++, cp++ ) {
  188.         if ( strncmp( cp, "0666", 4 ) == 0) 
  189.             break;
  190.     }
  191.     cp+=4;
  192.     *cp = '\0';
  193.     strcpy(tofile, line);
  194.      printmsg(3, "   getfile: fromfile=%s, tofile=%s.", fromfile, tofile);
  195.     return(FALSE);
  196. }
  197.  
  198.  
  199. /* */
  200. /*********************** MISC SUB SUB PROTOCOL *************************/
  201. /*
  202. **
  203. **schkdir
  204. ** scan the dir
  205. */
  206. schkdir()
  207. {
  208.     char    c;
  209.     c = scandir();
  210.     if (c == 'Q') {
  211.         return('Y');
  212.     }
  213.     if (c == 'S') {
  214.         sprintf(rpacket, "HN");
  215.         if ((*sendpkt)(rpacket, 0, 1)) 
  216.             return(0);
  217.     }
  218.     return('B');
  219. }
  220.  
  221.  
  222. /* */
  223. /*
  224.  *
  225.  *      endp() end protocol
  226.  *
  227. */
  228. endp()
  229. {
  230.     sprintf(rpacket, "HY");
  231.     (*sendpkt)(rpacket, 0, 2); /* dont wait for ACK */
  232.     (*closepk)();
  233.     return('P');
  234. }
  235.  
  236.  
  237. /* */
  238. /***********************RECIEVE PROTOCOL**********************/
  239. /*
  240.  *  r d a t a
  241.  *
  242.  *  Receive Data
  243.  */
  244. rdata()
  245. {
  246.     int    len;
  247.     if ((*getpkt)(rpacket, &len)) 
  248.         return(0);
  249.     if (len == 0) {
  250.         close(fp);
  251.         sprintf(rpacket, "CY");
  252.         if ((*sendpkt)(rpacket, 0, 1)) 
  253.             return(0);
  254.         printmsg( 0, "transfer complete" );
  255.         return('F');
  256.     }
  257.     if ( write(fp, rpacket, len) == -1 ) /* Write the data to the file */
  258.        {
  259.         printmsg( 0, "Error Writing File" );
  260.         return 'A';
  261.        }
  262.     return('D');/* Remain in data state */
  263. }
  264.  
  265.  
  266. /* */
  267. /*
  268.  *  r f i l e
  269.  *
  270.  *  Receive File Header
  271.  */
  272. rfile()
  273. {
  274.  
  275.     char     buf[256];
  276.     char *    flds[10];
  277.     int        numflds;
  278.     
  279.     int        len, i;
  280.     char    tmpfilename[256]; /*Holds the converted file name */
  281.     char    *cp;
  282.     
  283.     printmsg( 3, "rfile entered" );
  284.  
  285.     cp = buf;
  286.     
  287.     while ( TRUE ) {
  288.         if ((*getpkt)( rpacket, &len )) 
  289.             return( 0 );
  290.         strncpy( cp, rpacket, len );
  291.         cp += len;
  292.         if ( *(cp - 1) == '\0' ) break;
  293.     }
  294.     if (( buf[0] & 0x7f ) == 'H' ) 
  295.         return( 'C' );
  296.  
  297.     printmsg( 3, "rfile: buf %d \"%s\"", len, buf );
  298.         
  299.     /* Convert upper case to lower */
  300.     for (cp = buf; *cp != '\0';cp++)
  301.         if (isupper(*cp)) tolower(*cp);
  302.  
  303.     numflds = getargs( buf, flds );
  304.  
  305.     cp = flds[2];
  306.     printmsg( 3, "rfile: receive file \"%s\"", cp );
  307.  
  308.     /* check for ~/ destination -> /usr/spool/uucppublic */
  309.     if ( strncmp( cp, "~/", 2 ) == SAME )
  310.         sprintf( tmpfilename, "%s%s", pubdir, cp+1);
  311.     else
  312.         strcpy( tmpfilename, cp );
  313.     printmsg( 3, "rfile: receive file \"%s\"", tmpfilename );    
  314.  
  315.     /* check for dirname only */
  316.     cp = tmpfilename + strlen( tmpfilename ) - 1;
  317.     if ( *cp == '\n' ) 
  318.         *cp-- = '\0';
  319.         
  320.     if ( *cp == '/' ) {
  321.  
  322.         fprintf( stderr, "rfile: fromfile %s\n", flds[1] );
  323.         cp = rindex( flds[1], '/' );
  324.         if ( cp == (char *) NULL )
  325.             cp = flds[1];
  326.         else
  327.             cp++;
  328.  
  329.         fprintf( stderr, "rfile: dironly add   %s\n", cp );
  330.         
  331.         strcat( tmpfilename, cp );
  332.     }
  333.     printmsg( 3, "rfile: receive file \"%s\"", tmpfilename );    
  334.  
  335.  
  336.     /* let host munge filename as appropriate */
  337.     importpath( tofile, tmpfilename );
  338.     printmsg( 3, "rfile: receive file \"%s\"", tofile );    
  339.     
  340.     if ((fp = CREAT( tofile, 0775, 'b' )) == -1) { /* Try to open a new file */
  341.         printmsg( 0, "cannot create %s", tofile ); /* Give up if can't */
  342.         return('A'); 
  343.     }
  344.  
  345.     printmsg( 0, "Receiving %s as %s", flds[1], tofile );
  346.     sprintf(rpacket, "SY");
  347.     if ((*sendpkt)(rpacket, 0, 1)) 
  348.         return(0);
  349.     return('D'); /* Switch to data state */
  350. }
  351.  
  352.  
  353. /* */
  354. /*
  355.  *  r i n i t
  356.  *
  357.  *  Receive Initialization
  358.  */
  359. rinit()
  360. {
  361.     if ((*openpk)()) 
  362.         return(0);
  363.     return('F');
  364. }
  365.  
  366.  
  367.  
  368.