-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathrmdir.c
154 lines (132 loc) · 3.44 KB
/
rmdir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* $Id$
* RD / RMDIR - makes a call to directory_handler to do its work
*/
#include "../config.h"
#include <assert.h>
#include <dos.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "suppl.h"
#include "dfn.h"
#include "../include/command.h"
#include "../include/misc.h"
#include "../include/lfnfuncs.h"
#include "../err_fcts.h"
#include "../strings.h"
#ifdef FEATURE_LONG_FILENAMES
#define abspath( x, y ) abspath( getshortfilename( x ), y )
#endif
/* recursively delete subdirs and files */
int rmdir_withfiles(char * path, int maxlen)
{
struct dos_ffblk f;
int ret;
int len = strlen(path); /* Warning: we assume path is a buffer is large enough for longest file path */
char *p = path+len;
/* ensure ends with \ */
if (p[-1] != '\\')
*p++ = '\\';
*p = '\0';
dprintf(("rmdir(%s)\n", path));
/* cycle through removing directories first */
stpcpy(p, "*.*");
if (!dos_findfirst(path, &f, FA_DIREC)) {
ret = 0;
do {
/* skip . and .. directory entries */
if ((strcmp(f.ff_name, ".")==0) ||
(strcmp(f.ff_name, "..")==0))
continue;
if (f.ff_attrib & FA_DIREC) {
dprintf(("name=%s\n", f.ff_name));
/* avoid overflowing our buffer */
if((len + strlen(f.ff_name)) > maxlen) {
error_filename_too_long(p);
ret = E_Other;
continue;
}
strcpy(p, f.ff_name); /* Make the full path */
/* recursively delete subdirectory */
ret = rmdir_withfiles(path, maxlen);
}
} while (!ret && (dos_findnext(&f) == 0));
dos_findclose(&f);
/* return early on error */
if (ret) return ret;
}
/* remove any files in current directory */
stpcpy(p, "*.*");
if (!dos_findfirst(path, &f, FA_NORMAL)) {
ret = 0;
do {
/* avoid overflowing our buffer */
if((len + strlen(f.ff_name)) > maxlen) {
error_filename_too_long(p);
ret = E_Other;
continue;
}
strcpy(p, f.ff_name); /* Make the full path */
dprintf(("deleting [%s]\n", path));
/* try to delete the file */
if(unlink(path) != 0) {
myperror(path); /* notify the user */
/* could exit here, but we just let rmdir fail */
}
} while (!ret && (dos_findnext(&f) == 0));
dos_findclose(&f);
}
/* finally actually remove the directory */
p[-1] = '\0';
return rmdir(path);
}
int recursive_rmdir(const char * path, int recursiveMode, int quiet)
{
if (recursiveMode) {
struct dos_ffblk f;
char fullname[MAXPATH + sizeof(f.ff_name) + 2], *p;
int len;
/* Get the pattern fully-qualified */
/* Note: An absolute path always contains:
A:\\
--> It's always three bytes long at minimum
and always contains a backslash */
p = abspath(path, 1);
if(!p)
return E_Other;
assert(strlen(p) >= 3);
if((len = strlen(p)) >= MAXPATH) {
error_filename_too_long(p);
free(p);
return E_Other;
}
strcpy(fullname, p);
free(p);
p = fullname + len;
/* validate path exists and is a directory */
if(!(dfnstat(fullname) & DFN_DIRECTORY)) {
/* not a directory */
return E_Other;
}
/* ensure ends with \ */
if (p[-1] != '\\')
*p++ = '\\';
*p = 0;
/* prompt user if they are sure, regardless if files or not */
if (!quiet) {
int r;
r = userprompt(PROMPT_DELETE_ALL, fullname); /* Are you sure? TODO fix me */
if (r != 1) {
return E_Other;
}
}
return rmdir_withfiles(fullname, sizeof(fullname));
} else {
return rmdir(path);
}
}
int cmd_rmdir(char *param)
{
return mk_rd_dir(param, recursive_rmdir, "RMDIR");
}