home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / comm / tcp / ftpdaemon / source / ftpsubr.c < prev    next >
C/C++ Source or Header  |  1993-12-26  |  2KB  |  108 lines

  1. #include <stdio.h>
  2. #include <exec/types.h>
  3.  
  4. #include "ftp.h"
  5.  
  6. #include <proto/socket.h>
  7.  
  8. extern LONG server_socket;
  9.  
  10. static char buf[BLKSIZE];
  11.  
  12. /* Send a file (opened by caller) on a network socket */
  13. long sendfile(FILE *fp,LONG s,int mode,int hash)
  14. {
  15.     int c;
  16.     long total = 0;
  17.     long cnt;
  18.  
  19.     switch(mode) {
  20.         default:
  21.         case LOGICAL_TYPE:
  22.         case IMAGE_TYPE:
  23.             for(;;)
  24.             {
  25.                 if((cnt = fread(buf,1,BLKSIZE,fp)) == 0) break;
  26.                 total += cnt;
  27.                 if(send(s,buf,cnt,0) == -1) return -1;
  28.             }
  29.             break;
  30.         case ASCII_TYPE:
  31.             /* Let the newline mapping code in usputc() do the work */
  32.             while((c = fgetc(fp)) != EOF)
  33.             {
  34.                 usputc(s,(char)c);
  35.                 total++;
  36.             }
  37.             break;
  38.     }
  39.     return total;
  40. }
  41.  
  42. long recvfile(FILE *fp,LONG s,int mode,int hash)
  43. {
  44.     int c;
  45.     long total = 0;
  46.     long cnt;
  47.  
  48.     switch(mode) {
  49.         default:
  50.         case LOGICAL_TYPE:
  51.         case IMAGE_TYPE:
  52.             while((cnt=recv(s,buf,sizeof(buf),0))!=0)
  53.             {
  54.                 if(cnt == -1) return -1;
  55.                 total += cnt;
  56.                 if(fp != NULL)
  57.                 {
  58.                     if(fwrite(buf,cnt,1,fp)!=1) return -1;
  59.                 } 
  60.                 else send(server_socket,buf,cnt,0);
  61.             }
  62.             break;
  63.         case ASCII_TYPE:
  64.             while((c = recvchar(s)) != EOF)
  65.             {
  66.                 if(fp != NULL)
  67.                 {
  68.                     if(fputc(c,fp) == EOF)
  69.                     {
  70.                         total = -1;
  71.                         break;
  72.                     }
  73.                 }
  74.                 else tputc((char)c);
  75.                 total++;
  76.             }
  77.             break;
  78.     }
  79.     return total;
  80. }
  81.  
  82. /* Determine if a file appears to be binary (i.e., non-text).
  83.  * Return 1 if binary, 0 if ascii text after rewinding the file pointer.
  84.  *
  85.  * Used by FTP to warn users when transferring a binary file in text mode.
  86.  */
  87. int isbinary(FILE *fp)
  88. {
  89.     int c,i;
  90.     int rval;
  91.  
  92.     rval = 0;
  93.     for(i=0;i<512;i++)
  94.     {
  95.         if((c = getc(fp)) == EOF) break;
  96.         if(c & 0x80)
  97.         {
  98.             /* High bit is set, probably not text */
  99.             rval = 1;
  100.             break;
  101.         }
  102.     }
  103.     /* Assume it was at beginning */
  104.     rewind(fp);
  105.     
  106.     return rval;
  107. }
  108.