Skip to content

Commit c56d243

Browse files
committed
reduce stack usage during recursive CopyTree
1 parent 1588523 commit c56d243

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

nls/move.en

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
1.27:Write error on destination file
5151
1.28:Unable to create directory
5252
1.29:Insufficient disk space in destination path
53+
1.30:Insufficient memory
5354

5455
# Y/N/All/None; Simple messages
5556

src/misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ static void build_name(char *dest, const char *src, const char *pattern)
237237
void build_filename(char *dest_filename,const char *src_filename,const char
238238
*filepattern)
239239
{
240-
char drive[MAXDRIVE], dir[MAXDIR], filename_file[MAXFILE],
240+
static char drive[MAXDRIVE], dir[MAXDIR], filename_file[MAXFILE],
241241
filename_ext[MAXEXT], filepattern_file[MAXFILE],
242242
filepattern_ext[MAXEXT], tmp_filename[MAXFILE], tmp_ext[MAXEXT];
243243

src/move.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ int found=0;
8282

8383
nl_catd cat;
8484

85+
/* extern unsigned _stklen = 12288U; */
86+
8587
/* P R O T O T Y P E S */
8688
/*------------------------------------------------------*/
8789

src/movedir.c

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
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

Comments
 (0)