1 mike 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 mike 1.1 **==============================================================================
23 */
24
25 #include <stdio.h>
26 #include "file.h"
27 #include "io.h"
28
29 int File_Copy(const char* src, const char* dest)
30 {
31 FILE* is = NULL;
32 FILE* os = NULL;
33 char buf[4096];
34
35 /* Open input file */
36 is = Fopen(src, "rb");
37 if (!is)
38 return -1;
39
40 #if defined(CONFIG_POSIX)
41
42 /* Unlink output file if it exists */
43 mike 1.1 if (access(dest, F_OK) == 0)
44 {
45 unlink(dest);
46 }
47
48 #endif
49
50 /* Open output file */
51 os = Fopen(dest, "wb");
52 if (!os)
53 {
54 fclose(is);
55 return -1;
56 }
57
58 /* Copy file */
59 for (;;)
60 {
61 #if defined(CONFIG_OS_WINDOWS)
62 long n = (long)fread(buf, 1, sizeof(buf), is);
63 long m;
64 mike 1.1 #else
65 ssize_t n = fread(buf, 1, sizeof(buf), is);
66 ssize_t m;
67 #endif
68
69 if (n <= 0)
70 break;
71
72 #if defined(CONFIG_OS_WINDOWS)
73 m = (long)fwrite(buf, 1, n, os);
74 #else
75 m = fwrite(buf, 1, n, os);
76 #endif
77
78 if (m != n)
79 {
80 fclose(is);
81 fclose(os);
82 return -1;
83 }
84 }
85 mike 1.1
86 fclose(is);
87 fclose(os);
88 return 0;
89 }
|