@@ -34,7 +34,7 @@ int rmdir_withfiles(const char * fullname, char * path, int maxlen)
34
34
int rmdir_withfiles (char * path , int maxlen )
35
35
#endif
36
36
{
37
- struct dos_ffblk f ;
37
+ struct dos_ffblk * f ;
38
38
int ret ;
39
39
int len = strlen (path ); /* Warning: we assume path is a buffer is large enough for longest file path */
40
40
char * p = path + len ;
@@ -52,59 +52,69 @@ int rmdir_withfiles(char * path, int maxlen)
52
52
53
53
/* cycle through removing directories first */
54
54
stpcpy (p , "*.*" );
55
- if (!dos_findfirst (path , & f , FA_DIREC )) {
55
+ /* allocate our findfirst/next structure on heap to avoid exhausting stack */
56
+ f = (struct dos_ffblk * )malloc (sizeof (struct dos_ffblk ));
57
+ if (f == NULL ) {
58
+ error_out_of_memory ();
59
+ return E_NoMem ;
60
+ }
61
+ if (!dos_findfirst (path , f , FA_DIREC )) {
56
62
ret = 0 ;
57
63
do {
58
64
/* skip . and .. directory entries */
59
- if ((strcmp (f . ff_name , "." )== 0 ) ||
60
- (strcmp (f . ff_name , ".." )== 0 ))
65
+ if ((strcmp (f -> ff_name , "." )== 0 ) ||
66
+ (strcmp (f -> ff_name , ".." )== 0 ))
61
67
continue ;
62
68
63
- if (f . ff_attrib & FA_DIREC ) {
64
- dprintf (("name=%s\n" , f . ff_name ));
69
+ if (f -> ff_attrib & FA_DIREC ) {
70
+ dprintf (("name=%s\n" , f -> ff_name ));
65
71
/* avoid overflowing our buffer */
66
- if ((len + strlen (f . ff_name )) > maxlen ) {
72
+ if ((len + strlen (f -> ff_name )) > maxlen ) {
67
73
error_filename_too_long (p );
68
74
ret = E_Other ;
69
75
continue ;
70
76
}
71
77
72
- strcpy (p , f . ff_name ); /* Make the full path */
78
+ strcpy (p , f -> ff_name ); /* Make the full path */
73
79
/* recursively delete subdirectory */
74
80
#ifdef DBCS
75
81
ret = rmdir_withfiles (fullname , path , maxlen );
76
82
#else
77
83
ret = rmdir_withfiles (path , maxlen );
78
84
#endif
79
85
}
80
- } while (!ret && (dos_findnext (& f ) == 0 ));
81
- dos_findclose (& f );
86
+ } while (!ret && (dos_findnext (f ) == 0 ));
87
+ dos_findclose (f );
82
88
/* return early on error */
83
- if (ret ) return ret ;
89
+ if (ret ) {
90
+ free (f );
91
+ return ret ;
92
+ }
84
93
}
85
94
86
95
/* remove any files in current directory */
87
96
stpcpy (p , "*.*" );
88
- if (!dos_findfirst (path , & f , FA_NORMAL )) {
97
+ if (!dos_findfirst (path , f , FA_NORMAL )) {
89
98
ret = 0 ;
90
99
do {
91
100
/* avoid overflowing our buffer */
92
- if ((len + strlen (f . ff_name )) > maxlen ) {
101
+ if ((len + strlen (f -> ff_name )) > maxlen ) {
93
102
error_filename_too_long (p );
94
103
ret = E_Other ;
95
104
continue ;
96
105
}
97
106
98
- strcpy (p , f . ff_name ); /* Make the full path */
107
+ strcpy (p , f -> ff_name ); /* Make the full path */
99
108
dprintf (("deleting [%s]\n" , path ));
100
109
/* try to delete the file */
101
110
if (unlink (path ) != 0 ) {
102
111
myperror (path ); /* notify the user */
103
112
/* could exit here, but we just let rmdir fail */
104
113
}
105
- } while (!ret && (dos_findnext (& f ) == 0 ));
106
- dos_findclose (& f );
114
+ } while (!ret && (dos_findnext (f ) == 0 ));
115
+ dos_findclose (f );
107
116
}
117
+ free (f );
108
118
109
119
/* finally actually remove the directory */
110
120
p [-1 ] = '\0' ;
@@ -115,7 +125,8 @@ int recursive_rmdir(const char * path, int recursiveMode, int quiet)
115
125
{
116
126
if (recursiveMode ) {
117
127
struct dos_ffblk f ;
118
- char fullname [MAXPATH + sizeof (f .ff_name ) + 2 ], * p ;
128
+ char * p ;
129
+ static char fullname [MAXPATH + sizeof (f .ff_name ) + 2 ];
119
130
int len ;
120
131
/* Get the pattern fully-qualified */
121
132
/* Note: An absolute path always contains:
0 commit comments