~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to ftp_lite/src/stream.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
 
3
Copyright (C) 2005- The University of Notre Dame
 
4
This software is distributed under the GNU General Public License.
 
5
See the file COPYING for details.
 
6
*/
 
7
 
 
8
#include "ftp_lite.h"
 
9
 
 
10
#include <stdio.h>
 
11
#include <stdlib.h>
 
12
 
 
13
#define BUFFER_SIZE 32768
 
14
 
 
15
ftp_lite_size_t ftp_lite_stream_to_stream( FILE *input, FILE *output )
 
16
{
 
17
        char buffer[BUFFER_SIZE];
 
18
        int actual_read=0, actual_write=0;
 
19
        ftp_lite_size_t total=0;
 
20
 
 
21
        while(1) {
 
22
                actual_read = fread(buffer,1,BUFFER_SIZE,input);
 
23
                if(actual_read<=0) break;
 
24
 
 
25
                actual_write = fwrite(buffer,1,actual_read,output);
 
26
                if(actual_write!=actual_read) break;
 
27
 
 
28
                total+=actual_write;
 
29
        }
 
30
 
 
31
        if( ( (actual_read<0) || (actual_write<0) ) && total==0 ) {
 
32
                return -1;
 
33
        } else {
 
34
                return total;
 
35
        }
 
36
}
 
37
 
 
38
ftp_lite_size_t ftp_lite_stream_to_buffer( FILE *input, char **buffer )
 
39
{
 
40
        int buffer_size = 8192;
 
41
        int actual;
 
42
        ftp_lite_size_t total=0;
 
43
        char *newbuffer;
 
44
 
 
45
        *buffer = malloc(buffer_size);
 
46
        if(!*buffer) return -1;
 
47
 
 
48
        while(1) {
 
49
                actual = fread(&(*buffer)[total],1,buffer_size-total,input);
 
50
                if(actual<=0) break;
 
51
 
 
52
                total += actual;
 
53
 
 
54
                if( (buffer_size-total)<1 ) {
 
55
                        buffer_size *= 2;
 
56
                        newbuffer = realloc(*buffer,buffer_size);
 
57
                        if(!newbuffer) {
 
58
                                free(*buffer);
 
59
                                return -1;
 
60
                        }
 
61
                        *buffer = newbuffer;
 
62
                }
 
63
        }
 
64
 
 
65
        return total;
 
66
}