xref: /reactos/boot/freeldr/tools/rcopy.c (revision d6eebaa4)
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 
5 #if defined (__DJGPP__) || defined (__WIN32__)
6 #define DOS_PATHS
7 #else
8 #define UNIX_PATHS
9 #endif
10 
11 char* convert_path(char* origpath)
12 {
13    char* newpath;
14    int i;
15 
16    newpath = strdup(origpath);
17 
18    i = 0;
19    while (newpath[i] != 0)
20      {
21 #ifdef UNIX_PATHS
22     if (newpath[i] == '\\')
23       {
24          newpath[i] = '/';
25       }
26 #else
27 #ifdef DOS_PATHS
28     if (newpath[i] == '/')
29       {
30          newpath[i] = '\\';
31       }
32 #endif
33 #endif
34     i++;
35      }
36    return(newpath);
37 }
38 
39 #define TRANSFER_SIZE      (65536)
40 
41 int main(int argc, char* argv[])
42 {
43    char* path1;
44    char* path2;
45    FILE* in;
46    FILE* out;
47    char* buf;
48    int n_in;
49    int n_out;
50 
51    if (argc != 3)
52      {
53     fprintf(stderr, "Too many arguments\n");
54     exit(1);
55      }
56 
57    path1 = convert_path(argv[1]);
58    path2 = convert_path(argv[2]);
59 
60    in = fopen(path1, "rb");
61    if (in == NULL)
62      {
63     perror("Cannot open input file");
64     exit(1);
65      }
66 
67 
68 
69    out = fopen(path2, "wb");
70    if (out == NULL)
71      {
72     perror("Cannot open output file");
73     fclose(in);
74     exit(1);
75      }
76 
77    buf = malloc(TRANSFER_SIZE);
78 
79    while (!feof(in))
80      {
81     n_in = fread(buf, 1, TRANSFER_SIZE, in);
82     n_out = fwrite(buf, 1, n_in, out);
83     if (n_in != n_out)
84       {
85          perror("Failed to write to output file\n");
86          free(buf);
87          fclose(in);
88          fclose(out);
89          exit(1);
90       }
91      }
92    exit(0);
93 }
94