11 #include <sys/types.h>
18 #define DIR_SEPARATOR_CHAR '/'
19 #define DIR_SEPARATOR_STRING "/"
21 #define DIR_SEPARATOR_CHAR '\\'
22 #define DIR_SEPARATOR_STRING "\\"
25 char* convert_path(char* origpath)
30 newpath = strdup(origpath);
33 while (newpath[i] != 0)
36 if (newpath[i] == '\\')
42 if (newpath[i] == '/')
53 #define TRANSFER_SIZE (65536)
56 copy_file(char* path1, char* path2)
64 in = fopen(path1, "rb");
67 perror("Cannot open input file");
71 out = fopen(path2, "wb");
74 perror("Cannot open output file");
79 buf = malloc(TRANSFER_SIZE);
82 n_in = fread(buf, 1, TRANSFER_SIZE, in);
83 n_out = fwrite(buf, 1, n_in, out);
86 perror("Failed to write to output file\n");
98 copy_directory (char *path1, char *path2)
100 struct _finddata_t f;
103 char tobuf[MAX_PATH];
106 if (path1[strlen(path1) - 1] != DIR_SEPARATOR_CHAR)
107 strcat(buf, DIR_SEPARATOR_STRING);
109 findhandle =_findfirst(buf, &f);
114 if ((f.attrib & _A_SUBDIR) == 0 && f.name[0] != '.')
116 // Check for an absolute path
117 if (path1[0] == DIR_SEPARATOR_CHAR)
120 strcat(buf, DIR_SEPARATOR_STRING);
125 getcwd(buf, sizeof(buf));
126 strcat(buf, DIR_SEPARATOR_STRING);
128 if (path1[strlen(path1) - 1] != DIR_SEPARATOR_CHAR)
129 strcat(buf, DIR_SEPARATOR_STRING);
133 //printf("copying file %s\n", buf);
134 if (path2[strlen(path2) - 1] == DIR_SEPARATOR_CHAR)
136 strcpy(tobuf, path2);
137 strcat(tobuf, f.name);
141 strcpy(tobuf, path2);
142 strcat(tobuf, DIR_SEPARATOR_STRING);
143 strcat(tobuf, f.name);
145 copy_file(buf, tobuf);
149 //printf("skipping directory '%s'\n", f.name);
152 while (_findnext(findhandle, &f) == 0);
154 _findclose(findhandle);
162 copy_directory (char *path1, char *path2)
165 struct dirent *entry;
166 char *old_end_source;
169 char tobuf[MAX_PATH];
172 dirp = opendir(path1);
176 while ((entry = readdir (dirp)) != NULL)
178 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
179 continue; // skip self and parent
181 if (entry->d_type == DT_REG) // normal file
183 // Check for an absolute path
184 if (path1[0] == DIR_SEPARATOR_CHAR)
187 strcat(buf, DIR_SEPARATOR_STRING);
188 strcat(buf, entry->d_name);
192 getcwd(buf, sizeof(buf));
193 strcat(buf, DIR_SEPARATOR_STRING);
195 if (path1[strlen(path1) - 1] != DIR_SEPARATOR_CHAR)
196 strcat(buf, DIR_SEPARATOR_STRING);
197 strcat(buf, entry->d_name);
199 if (stat(buf, &stbuf) == -1)
201 sprintf(err, "Can't access '%s' (%s)\n", buf, strerror(errno));
207 //printf("copying file '%s'\n", entry->d_name);
208 if (path2[strlen(path2) - 1] == DIR_SEPARATOR_CHAR)
210 strcpy(tobuf, path2);
211 strcat(tobuf, entry->d_name);
215 strcpy(tobuf, path2);
216 strcat(tobuf, DIR_SEPARATOR_STRING);
217 strcat(tobuf, entry->d_name);
219 copy_file(buf, tobuf);
223 //printf("skipping directory '%s'\n", entry->d_name);
230 sprintf(err, "Can't open %s\n", path1);
240 is_directory(char *path)
246 // Check for an absolute path
247 if (path[0] == DIR_SEPARATOR_CHAR)
253 getcwd(buf, sizeof(buf));
254 strcat(buf, DIR_SEPARATOR_STRING);
257 if (stat(buf, &stbuf) == -1)
259 /* Assume a destination file */
262 if (S_ISDIR(stbuf.st_mode))
268 int main(int argc, char* argv[])
277 fprintf(stderr, "Wrong argument count\n");
281 path1 = convert_path(argv[1]);
282 path2 = convert_path(argv[2]);
284 dir1 = is_directory(path1);
285 dir2 = is_directory(path2);
287 if ((dir1 && !dir2) || (!dir1 && dir2))
289 perror("None or both paramters must be a directory\n");
295 copy_directory(path1, path2);
299 copy_file(path1, path2);