xref: /aosp_15_r20/external/mtools/file_read.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
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 "msdos.h"
21 #include "mtools.h"
22 #include "file.h"
23 
24 /*
25  * Read the clusters given the beginning FAT entry.  Returns 0 on success.
26  */
27 
file_read(FILE * fp,Stream_t * Source,int textmode,int stripmode)28 int file_read(FILE *fp, Stream_t *Source, int textmode, int stripmode)
29 {
30 	char buffer[16384];
31 	int pos;
32 	int ret;
33 
34 	if (!Source){
35 		fprintf(stderr,"Couldn't open source file\n");
36 		return -1;
37 	}
38 
39 	pos = 0;
40 	while(1){
41 		ret = Source->Class->read(Source, buffer, pos, 16384);
42 		if (ret < 0 ){
43 			perror("file read");
44 			return -1;
45 		}
46 		if ( ret == 0)
47 			break;
48 		if(!fwrite(buffer, 1, ret, fp)){
49 			perror("write");
50 			return -1;
51 		}
52 		pos += ret;
53 	}
54 	return 0;
55 }
56