12 #include <sys/types.h>
21 #define DIR_SEPARATOR_CHAR '/'
22 #define DIR_SEPARATOR_STRING "/"
24 #define DIR_SEPARATOR_CHAR '\\'
25 #define DIR_SEPARATOR_STRING "\\"
29 make_absolute(char *absolute
, char *path
)
32 if (path
[0] == DIR_SEPARATOR_CHAR
)
34 strcpy(absolute
, path
);
38 getcwd(absolute
, MAX_PATH
);
39 strcat(absolute
, DIR_SEPARATOR_STRING
);
40 strcat(absolute
, path
);
43 _fullpath(absolute
, path
, MAX_PATH
);
49 char* convert_path(char* origpath
)
54 //newpath = strdup(origpath);
55 newpath
= malloc(strlen(origpath
)+1);
56 strcpy(newpath
, origpath
);
59 while (newpath
[i
] != 0)
62 if (newpath
[i
] == '\\')
68 if (newpath
[i
] == '/')
79 #define TRANSFER_SIZE (65536)
82 copy_file(char* path1
, char* path2
)
89 struct stat st_buffer
;
90 struct utimbuf ut_buffer
;
92 in
= fopen(path1
, "rb");
95 perror("Cannot open input file");
99 out
= fopen(path2
, "wb");
102 perror("Cannot open output file");
107 buf
= malloc(TRANSFER_SIZE
);
110 n_in
= fread(buf
, 1, TRANSFER_SIZE
, in
);
111 n_out
= fwrite(buf
, 1, n_in
, out
);
114 perror("Failed to write to output file\n");
125 if (stat(path2
, &st_buffer
) >= 0)
127 ut_buffer
.actime
= st_buffer
.st_atime
;
129 if (stat(path1
, &st_buffer
) >= 0)
131 ut_buffer
.modtime
= st_buffer
.st_mtime
;
132 utime(path2
, &ut_buffer
);
141 copy_directory (char *path1
, char *path2
)
143 struct _finddata_t f
;
146 char tobuf
[MAX_PATH
];
149 if (path1
[strlen(path1
) - 1] != DIR_SEPARATOR_CHAR
)
150 strcat(buf
, DIR_SEPARATOR_STRING
);
152 findhandle
=_findfirst(buf
, &f
);
157 if ((f
.attrib
& _A_SUBDIR
) == 0 && f
.name
[0] != '.')
159 // Convert to absolute path
160 make_absolute(buf
, path1
);
161 if (path1
[strlen(path1
) - 1] != DIR_SEPARATOR_CHAR
)
162 strcat(buf
, DIR_SEPARATOR_STRING
);
165 //printf("copying file %s\n", buf);
166 if (path2
[strlen(path2
) - 1] == DIR_SEPARATOR_CHAR
)
168 strcpy(tobuf
, path2
);
169 strcat(tobuf
, f
.name
);
173 strcpy(tobuf
, path2
);
174 strcat(tobuf
, DIR_SEPARATOR_STRING
);
175 strcat(tobuf
, f
.name
);
177 copy_file(buf
, tobuf
);
181 //printf("skipping directory '%s'\n", f.name);
184 while (_findnext(findhandle
, &f
) == 0);
186 _findclose(findhandle
);
194 is_reg (char *path
, char *fn
)
201 if (buf
[strlen(buf
)-1] != '/')
205 make_absolute(buf2
, buf
);
207 if (stat(buf2
, &sbuf
) == -1)
210 if (S_ISREG(sbuf
.st_mode
))
218 copy_directory (char *path1
, char *path2
)
221 struct dirent
*entry
;
224 char tobuf
[MAX_PATH
];
227 dirp
= opendir(path1
);
231 while ((entry
= readdir (dirp
)) != NULL
)
233 if (strcmp(entry
->d_name
, ".") == 0 || strcmp(entry
->d_name
, "..") == 0)
234 continue; // skip self and parent
236 if (entry
->d_type
== DT_REG
|| is_reg(path1
, entry
->d_name
)) // normal file
238 // Convert to absolute path
239 make_absolute(buf
, path1
);
240 if (path1
[strlen(path1
) - 1] != DIR_SEPARATOR_CHAR
)
241 strcat(buf
, DIR_SEPARATOR_STRING
);
242 strcat(buf
, entry
->d_name
);
243 if (stat(buf
, &stbuf
) == -1)
245 sprintf(err
, "Can't access '%s' (%s)\n", buf
, strerror(errno
));
251 //printf("copying file '%s'\n", entry->d_name);
252 if (path2
[strlen(path2
) - 1] == DIR_SEPARATOR_CHAR
)
254 strcpy(tobuf
, path2
);
255 strcat(tobuf
, entry
->d_name
);
259 strcpy(tobuf
, path2
);
260 strcat(tobuf
, DIR_SEPARATOR_STRING
);
261 strcat(tobuf
, entry
->d_name
);
263 copy_file(buf
, tobuf
);
267 //printf("skipping directory '%s'\n", entry->d_name);
274 sprintf(err
, "Can't open %s\n", path1
);
284 is_directory(char *path
)
289 // Convert to absolute path
290 make_absolute(buf
, path
);
291 if (stat(buf
, &stbuf
) == -1)
293 /* Assume a destination file */
296 if (S_ISDIR(stbuf
.st_mode
))
302 int main(int argc
, char* argv
[])
311 fprintf(stderr
, "Wrong argument count\n");
315 path1
= convert_path(argv
[1]);
316 path2
= convert_path(argv
[2]);
318 dir1
= is_directory(path1
);
319 dir2
= is_directory(path2
);
321 if ((dir1
&& !dir2
) || (!dir1
&& dir2
))
323 perror("None or both paramters must be a directory\n");
329 copy_directory(path1
, path2
);
333 copy_file(path1
, path2
);