2424
2525#ifdef __WATCOMC__
2626#include <direct.h>
27- #include <stdlib.h>
2827#else
2928#include <dir.h>
3029#endif
30+ #include <stdlib.h>
3131#include <stdio.h>
3232#include <string.h>
3333#include <dos.h>
@@ -173,47 +173,54 @@ static int DelTree(const char* path)
173173/* Searchs through the source directory (and its subdirectories) and calls */
174174/* function "xcopy_file" for every found file. */
175175/*-------------------------------------------------------------------------*/
176- static int CopyTree (const char * src_pathname ,
176+ static int CopyTree (int depth , char * src_pathname ,
177177 const char * src_filename ,
178- const char * dest_pathname ,
178+ char * dest_pathname ,
179179 const char * dest_filename )
180180{
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 ],
184185 src_path_filename [MAXPATH ],
185186 dest_path_filename [MAXPATH ],
186187 tmp_filename [MAXFILE + MAXEXT ],
187188 tmp_pathname [MAXPATH ];
188- struct ffblk fileblock ;
189+ struct ffblk * fileblock ;
189190 unsigned fileattrib ;
190191 int done ;
191192
192193 /* copy files in subdirectories */
194+ if ((fileblock = malloc (sizeof (struct ffblk ))) == NULL ) {
195+ error (1 ,30 ,"Insufficient memory" );
196+ return 0 ;
197+ }
193198 strmcpy (filepattern , src_pathname , sizeof (filepattern ));
194199 strmcat (filepattern , src_filename , sizeof (filepattern ));
195- done = findfirst (filepattern , & fileblock , FA_DIREC );
200+ done = findfirst (filepattern , fileblock , FA_DIREC );
196201 while (!done )
197202 {
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 )
201206 {
202207 /* 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 );
206211
207212 /* 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' ;
214221 }
215222
216- done = findnext (& fileblock );
223+ done = findnext (fileblock );
217224 }
218225
219226 fileattrib = FA_RDONLY + FA_ARCH + FA_HIDDEN + FA_SYSTEM ;
@@ -240,18 +247,19 @@ static int CopyTree(const char *src_pathname,
240247 {
241248 /* build source filename including path */
242249 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 ));
244251
245252 /* build destination filename including path */
246253 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 );
248255 strmcat (dest_path_filename , tmp_filename , sizeof (dest_path_filename ));
249256
250257 if (!xcopy_file (src_path_filename , dest_path_filename ))
251258 return 0 ;
252259
253- done = findnext (& fileblock );
260+ done = findnext (fileblock );
254261 }
262+ free (fileblock );
255263
256264 return 1 ;
257265}
@@ -284,7 +292,7 @@ static int xcopy_file(const char *src_filename,
284292 disktable .df_sclus * disktable .df_bsec ;
285293
286294 /* check free space on destination disk */
287- if (src_statbuf .st_size > free_diskspace )
295+ if (( unsigned long ) src_statbuf .st_size > free_diskspace )
288296 {
289297 error (1 ,29 ,"Insufficient disk space in destination path" );
290298 return (0 );
@@ -333,7 +341,7 @@ int MoveDirectory(const char* src_filename, const char* dest_filename)
333341 if (!DelTree (dest_filename ))
334342 return 0 ;
335343
336- if (!CopyTree (src_path , src_file , dest_path , dest_file ))
344+ if (!CopyTree (0 , src_path , src_file , dest_path , dest_file ))
337345 {
338346 DelTree (dest_filename );
339347 return 0 ;
0 commit comments