1 /*  Copyright 1996-1999,2001,2002,2009 Alain Knaff.
2  *  This file is part of mtools.
3  *
4  *  Mtools is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  Mtools is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include "sysincludes.h"
18 #include "msdos.h"
19 #include "mtools.h"
20 #include "file.h"
21 #include "llong.h"
22 
23 /*
24  * Copy the data from source to target
25  */
26 
copyfile(Stream_t * Source,Stream_t * Target)27 int copyfile(Stream_t *Source, Stream_t *Target)
28 {
29 	char buffer[8*16384];
30 	mt_off_t pos;
31 	int ret;
32 	ssize_t retw;
33 /*	size_t len;*/
34 	mt_size_t mt_len;
35 
36 	if (!Source){
37 		fprintf(stderr,"Couldn't open source file\n");
38 		return -1;
39 	}
40 
41 	if (!Target){
42 		fprintf(stderr,"Couldn't open target file\n");
43 		return -1;
44 	}
45 
46 	pos = 0;
47 	GET_DATA(Source, 0, &mt_len, 0, 0);
48 	while(1){
49 		ret = READS(Source, buffer, (mt_off_t) pos, 8*16384);
50 		if (ret < 0 ){
51 			perror("file read");
52 			return -1;
53 		}
54 		if(!ret)
55 			break;
56 		if(got_signal)
57 			return -1;
58 		if (ret == 0)
59 			break;
60 		if ((retw = force_write(Target, buffer,
61 					(mt_off_t) pos, (size_t) ret)) != ret){
62 			if(retw < 0 )
63 				perror("write in copy");
64 			else
65 				fprintf(stderr,
66 					"Short write %lu instead of %d\n",
67 					(unsigned long) retw, ret);
68 			if(errno == ENOSPC)
69 				got_signal = 1;
70 			return ret;
71 		}
72 		pos += ret;
73 	}
74 	return 0;
75 }
76