Imported Upstream version 4.0.43
[platform/upstream/mtools.git] / file_read.c
1 /*  Copyright 1986-1992 Emmet P. Gray.
2  *  Copyright 1996,1997,1999,2002,2009 Alain Knaff.
3  *  This file is part of mtools.
4  *
5  *  Mtools is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  Mtools is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "sysincludes.h"
20 #include "mtools.h"
21 #include "file.h"
22
23 /*
24  * Read the clusters given the beginning FAT entry.  Returns 0 on success.
25  */
26
27 int file_read(FILE *fp, Stream_t *Source, int textmode, int stripmode)
28 {
29         char buffer[16384];
30         int pos;
31         int ret;
32
33         if (!Source){
34                 fprintf(stderr,"Couldn't open source file\n");
35                 return -1;
36         }
37
38         pos = 0;
39         while(1){
40                 ret = Source->Class->read(Source, buffer, pos, 16384);
41                 if (ret < 0 ){
42                         perror("file read");
43                         return -1;
44                 }
45                 if ( ret == 0)
46                         break;
47                 if(!fwrite(buffer, 1, ret, fp)){
48                         perror("write");
49                         return -1;
50                 }
51                 pos += ret;
52         }
53         return 0;
54 }