blob: cf0eeebc871c8cf7f55001ceb080d424fc37d4a4 [file] [log] [blame]
Alistair Delvabeaee832021-02-24 11:27:23 -08001/* 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
Yi Kong39bbd962022-01-09 19:41:38 +080027mt_off_t copyfile(Stream_t *Source, Stream_t *Target)
Alistair Delvabeaee832021-02-24 11:27:23 -080028{
29 char buffer[8*16384];
30 mt_off_t pos;
Yi Kong39bbd962022-01-09 19:41:38 +080031 ssize_t ret;
Alistair Delvabeaee832021-02-24 11:27:23 -080032 ssize_t retw;
Alistair Delvabeaee832021-02-24 11:27:23 -080033
34 if (!Source){
35 fprintf(stderr,"Couldn't open source file\n");
36 return -1;
37 }
38
39 if (!Target){
40 fprintf(stderr,"Couldn't open target file\n");
41 return -1;
42 }
43
44 pos = 0;
Alistair Delvabeaee832021-02-24 11:27:23 -080045 while(1){
Yi Kong39bbd962022-01-09 19:41:38 +080046 ret = READS(Source, buffer, 8*16384);
Alistair Delvabeaee832021-02-24 11:27:23 -080047 if (ret < 0 ){
48 perror("file read");
49 return -1;
50 }
51 if(!ret)
52 break;
53 if(got_signal)
54 return -1;
55 if (ret == 0)
56 break;
Yi Kong39bbd962022-01-09 19:41:38 +080057 if ((retw = force_write(Target, buffer, (size_t) ret)) != ret){
Alistair Delvabeaee832021-02-24 11:27:23 -080058 if(retw < 0 )
59 perror("write in copy");
60 else
61 fprintf(stderr,
Yi Kong39bbd962022-01-09 19:41:38 +080062 "Short write %zd instead of %zd\n",
63 retw, ret);
Alistair Delvabeaee832021-02-24 11:27:23 -080064 if(errno == ENOSPC)
65 got_signal = 1;
66 return ret;
67 }
68 pos += ret;
69 }
Yi Kong39bbd962022-01-09 19:41:38 +080070 return pos;
Alistair Delvabeaee832021-02-24 11:27:23 -080071}