24
24
25
25
#ifdef __WATCOMC__
26
26
#include <direct.h>
27
- #include <stdlib.h>
28
27
#else
29
28
#include <dir.h>
30
29
#endif
30
+ #include <stdlib.h>
31
31
#include <stdio.h>
32
32
#include <string.h>
33
33
#include <dos.h>
@@ -173,47 +173,54 @@ static int DelTree(const char* path)
173
173
/* Searchs through the source directory (and its subdirectories) and calls */
174
174
/* function "xcopy_file" for every found file. */
175
175
/*-------------------------------------------------------------------------*/
176
- static int CopyTree (const char * src_pathname ,
176
+ static int CopyTree (int depth , char * src_pathname ,
177
177
const char * src_filename ,
178
- const char * dest_pathname ,
178
+ char * dest_pathname ,
179
179
const char * dest_filename )
180
180
{
181
- char filepattern [MAXPATH ],
182
- new_src_pathname [MAXPATH ],
183
- new_dest_pathname [MAXPATH ],
181
+ char * old_new_src_pathname = src_pathname + strlen (src_pathname );
182
+ char * old_new_dest_pathname = dest_pathname + strlen (dest_pathname );
183
+ /* Warning: these are overwritten during recursive calls */
184
+ static char filepattern [MAXPATH ],
184
185
src_path_filename [MAXPATH ],
185
186
dest_path_filename [MAXPATH ],
186
187
tmp_filename [MAXFILE + MAXEXT ],
187
188
tmp_pathname [MAXPATH ];
188
- struct ffblk fileblock ;
189
+ struct ffblk * fileblock ;
189
190
unsigned fileattrib ;
190
191
int done ;
191
192
192
193
/* copy files in subdirectories */
194
+ if ((fileblock = malloc (sizeof (struct ffblk ))) == NULL ) {
195
+ error (1 ,30 ,"Insufficient memory" );
196
+ return 0 ;
197
+ }
193
198
strmcpy (filepattern , src_pathname , sizeof (filepattern ));
194
199
strmcat (filepattern , src_filename , sizeof (filepattern ));
195
- done = findfirst (filepattern , & fileblock , FA_DIREC );
200
+ done = findfirst (filepattern , fileblock , FA_DIREC );
196
201
while (!done )
197
202
{
198
- if (fileblock . ff_attrib == FA_DIREC &&
199
- strcmp (fileblock . ff_name , "." ) != 0 &&
200
- strcmp (fileblock . ff_name , ".." ) != 0 )
203
+ if (fileblock -> ff_attrib == FA_DIREC &&
204
+ strcmp (fileblock -> ff_name , "." ) != 0 &&
205
+ strcmp (fileblock -> ff_name , ".." ) != 0 )
201
206
{
202
207
/* build source pathname */
203
- strmcpy (new_src_pathname , src_pathname , sizeof (new_src_pathname ));
204
- strmcat (new_src_pathname , fileblock . ff_name , sizeof ( new_src_pathname ) );
205
- strmcat (new_src_pathname , DIR_SEPARATOR , sizeof ( new_src_pathname ) );
208
+ /* strmcpy(new_src_pathname, src_pathname, sizeof(new_src_pathname)); */
209
+ strmcat (old_new_src_pathname , fileblock -> ff_name , MAXPATH );
210
+ strmcat (old_new_src_pathname , DIR_SEPARATOR , MAXPATH );
206
211
207
212
/* build destination pathname */
208
- strmcpy (new_dest_pathname , dest_pathname , sizeof (new_dest_pathname ));
209
- strmcat (new_dest_pathname , fileblock .ff_name , sizeof (new_dest_pathname ));
210
- strmcat (new_dest_pathname , DIR_SEPARATOR , sizeof (new_dest_pathname ));
211
-
212
- if (!CopyTree (new_src_pathname , "*.*" ,
213
- new_dest_pathname , "*.*" )) return 0 ;
213
+ /* strmcpy(new_dest_pathname, dest_pathname, sizeof(new_dest_pathname)); */
214
+ strmcat (old_new_dest_pathname , fileblock -> ff_name , MAXPATH );
215
+ strmcat (old_new_dest_pathname , DIR_SEPARATOR , MAXPATH );
216
+
217
+ if (!CopyTree (depth + 1 , src_pathname , "*.*" ,
218
+ dest_pathname , "*.*" )) return 0 ;
219
+ * old_new_src_pathname = '\0' ;
220
+ * old_new_dest_pathname = '\0' ;
214
221
}
215
222
216
- done = findnext (& fileblock );
223
+ done = findnext (fileblock );
217
224
}
218
225
219
226
fileattrib = FA_RDONLY + FA_ARCH + FA_HIDDEN + FA_SYSTEM ;
@@ -240,18 +247,19 @@ static int CopyTree(const char *src_pathname,
240
247
{
241
248
/* build source filename including path */
242
249
strmcpy (src_path_filename , src_pathname , sizeof (src_path_filename ));
243
- strmcat (src_path_filename , fileblock . ff_name , sizeof (src_path_filename ));
250
+ strmcat (src_path_filename , fileblock -> ff_name , sizeof (src_path_filename ));
244
251
245
252
/* build destination filename including path */
246
253
strmcpy (dest_path_filename , dest_pathname , sizeof (dest_path_filename ));
247
- build_filename (tmp_filename , fileblock . ff_name , dest_filename );
254
+ build_filename (tmp_filename , fileblock -> ff_name , dest_filename );
248
255
strmcat (dest_path_filename , tmp_filename , sizeof (dest_path_filename ));
249
256
250
257
if (!xcopy_file (src_path_filename , dest_path_filename ))
251
258
return 0 ;
252
259
253
- done = findnext (& fileblock );
260
+ done = findnext (fileblock );
254
261
}
262
+ free (fileblock );
255
263
256
264
return 1 ;
257
265
}
@@ -284,7 +292,7 @@ static int xcopy_file(const char *src_filename,
284
292
disktable .df_sclus * disktable .df_bsec ;
285
293
286
294
/* check free space on destination disk */
287
- if (src_statbuf .st_size > free_diskspace )
295
+ if (( unsigned long ) src_statbuf .st_size > free_diskspace )
288
296
{
289
297
error (1 ,29 ,"Insufficient disk space in destination path" );
290
298
return (0 );
@@ -333,7 +341,7 @@ int MoveDirectory(const char* src_filename, const char* dest_filename)
333
341
if (!DelTree (dest_filename ))
334
342
return 0 ;
335
343
336
- if (!CopyTree (src_path , src_file , dest_path , dest_file ))
344
+ if (!CopyTree (0 , src_path , src_file , dest_path , dest_file ))
337
345
{
338
346
DelTree (dest_filename );
339
347
return 0 ;
0 commit comments