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
27int copyfile(Stream_t *Source, Stream_t *Target)
28{
29	char buffer[8*16384];
30	mt_off_t pos;
31	int ret, retw;
32/*	size_t len;*/
33	mt_size_t mt_len;
34
35	if (!Source){
36		fprintf(stderr,"Couldn't open source file\n");
37		return -1;
38	}
39
40	if (!Target){
41		fprintf(stderr,"Couldn't open target file\n");
42		return -1;
43	}
44
45	pos = 0;
46	GET_DATA(Source, 0, &mt_len, 0, 0);
47	while(1){
48		ret = READS(Source, buffer, (mt_off_t) pos, 8*16384);
49		if (ret < 0 ){
50			perror("file read");
51			return -1;
52		}
53		if(!ret)
54			break;
55		if(got_signal)
56			return -1;
57		if (ret == 0)
58			break;
59		if ((retw = force_write(Target, buffer, (mt_off_t) pos, ret)) != ret){
60			if(retw < 0 )
61				perror("write in copy");
62			else
63				fprintf(stderr,
64					"Short write %d instead of %d\n", retw,
65					ret);
66			if(errno == ENOSPC)
67				got_signal = 1;
68			return ret;
69		}
70		pos += ret;
71	}
72	return 0;
73}
74