home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / FTPSUBR.C < prev    next >
C/C++ Source or Header  |  1992-05-03  |  3KB  |  133 lines

  1. /* Routines common to both the FTP client and server
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "socket.h"
  8. #include "proc.h"
  9. #include "ftp.h"
  10.  
  11. /* Send a file (opened by caller) on a network socket.
  12.  * Normal return: count of bytes sent
  13.  * Error return: -1
  14.  */
  15. long
  16. sendfile(fp,network,mode,hash)
  17. FILE *fp;    /* File to be sent */
  18. FILE *network;    /* Network stream to be sent on */
  19. int mode;    /* Transfer mode */
  20. int hash;    /* Print hash marks every BLKSIZE bytes */
  21. {
  22.     long total = 0;
  23.     long hmark = 0;
  24.     char *buf;
  25.     int cnt;
  26.  
  27.     switch(mode){
  28.     default:
  29.     case LOGICAL_TYPE:
  30.     case IMAGE_TYPE:
  31.         fmode(network,STREAM_BINARY);
  32.         break;
  33.     case ASCII_TYPE:
  34.         fmode(network,STREAM_ASCII);
  35.         break;
  36.     }
  37.     buf = mallocw(BLKSIZE);
  38.     for(;;){
  39.         if((cnt = fread(buf,1,BLKSIZE,fp)) == 0){
  40.             break;
  41.         }
  42.         total += cnt;
  43.         if(fwrite(buf,1,cnt,network) != cnt){
  44.             total = -1;
  45.             break;
  46.         }
  47.         while(hash && total >= hmark+1000){
  48.             putchar('#');
  49.             hmark += 1000;
  50.         }
  51.     }
  52.     free(buf);
  53.     if(hash)
  54.         putchar('\n');
  55.     return total;
  56. }
  57. /* Receive a file (opened by caller) from a network stream
  58.  * Normal return: count of bytes received
  59.  * Error return: -1
  60.  */
  61. long
  62. recvfile(fp,network,mode,hash)
  63. FILE *fp;
  64. FILE *network;
  65. int mode;
  66. int hash;
  67. {
  68.     int cnt;
  69.     long total = 0;
  70.     long hmark = 0;
  71.     char *buf;
  72.  
  73.     if(fp == NULLFILE)
  74.         fp = stdout;    /* Default */
  75.     switch(mode){
  76.     default:
  77.     case LOGICAL_TYPE:
  78.     case IMAGE_TYPE:
  79.         fmode(network,STREAM_BINARY);
  80.         break;
  81.     case ASCII_TYPE:
  82.         fmode(network,STREAM_ASCII);
  83.         break;
  84.     }
  85.     buf = mallocw(BUFSIZ);
  86.     while((cnt = fread(buf,1,BUFSIZ,network)) != 0){
  87.         total += cnt;
  88.         while(hash && total >= hmark+1000){
  89.             putchar('#');
  90.             hmark += 1000;
  91.         }
  92.         if(fwrite(buf,1,cnt,fp) != cnt){
  93.             total = -1;
  94.             break;
  95.         }
  96.         /* Detect an abnormal close */
  97.         if(socklen(fileno(network),0) == -1){
  98.             total = -1;
  99.             break;
  100.         }
  101.     }
  102.     if(hash)
  103.         putchar('\n');
  104.     return total;
  105. }
  106. /* Determine if a file appears to be binary (i.e., non-text).
  107.  * Return 1 if binary, 0 if ascii text after rewinding the file pointer.
  108.  *
  109.  * Used by FTP to warn users when transferring a binary file in text mode.
  110.  */
  111. int
  112. isbinary(fp)
  113. FILE *fp;
  114. {
  115.     int c,i;
  116.     int rval;
  117.  
  118.     rval = 0;
  119.     for(i=0;i<512;i++){
  120.         if((c = getc(fp)) == EOF)
  121.             break;
  122.         if(c & 0x80){
  123.             /* High bit is set, probably not text */
  124.             rval = 1;
  125.             break;
  126.         }
  127.     }
  128.     /* Assume it was at beginning */
  129.     fseek(fp,0L,SEEK_SET);
  130.     return rval;
  131. }
  132.  
  133.