1 /* Copyright 1986-1992 Emmet P. Gray.
2 * Copyright 1996-2002,2005,2008,2009 Alain Knaff.
3 * This file is part of mtools.
5 * Mtools is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * Mtools is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
19 * Delete an MSDOS file
23 #include "sysincludes.h"
30 #include "file_name.h"
32 typedef struct Arg_t {
38 * Wiped the given entry
40 void wipeEntry(direntry_t *entry)
42 direntry_t longNameEntry;
44 initializeDirentry(&longNameEntry, entry->Dir);
45 for(i=entry->beginSlot; i< entry->endSlot; i++) {
47 longNameEntry.entry=i;
48 dir_read(&longNameEntry, &error);
51 longNameEntry.dir.name[0] = (char) DELMARK;
52 dir_write(&longNameEntry);
54 entry->dir.name[0] = (char) DELMARK;
58 static int del_entry(direntry_t *entry, MainParam_t *mp)
60 Arg_t *arg=(Arg_t *) mp->arg;
65 if(entry->entry == -3) {
66 fprintf(stderr, "Cannot remove root directory\n");
71 fprintf(stderr,"Removing ");
72 fprintPwd(stderr, entry,0);
76 if (entry->dir.attr & (ATTR_READONLY | ATTR_SYSTEM)) {
77 char tmp[4*MAX_VNAMELEN+1];
78 wchar_to_native(entry->name,tmp,MAX_VNAMELEN);
79 if (ask_confirmation("%s: \"%s\" is read only, erase anyway (y/n) ? ",
83 if (fatFreeWithDirentry(entry))
90 static int del_file(direntry_t *entry, MainParam_t *mp)
95 Arg_t *arg = (Arg_t *) mp->arg;
106 SubDir = OpenFileByDirentry(entry);
107 initializeDirentry(&subEntry, SubDir);
109 while((r=vfat_lookup(&subEntry, "*", 1,
110 ACCEPT_DIR | ACCEPT_PLAIN,
111 shortname, NULL)) == 0 ){
112 if(shortname[0] != DELMARK &&
114 shortname[0] != '.' ){
115 if(arg->deltype != 2){
118 fprintPwd(stderr, entry,0);
119 fprintf(stderr," non empty\n");
127 ret = del_file(&subEntry, &sonmp);
139 return del_entry(entry, mp);
142 static void usage(int ret) NORETURN;
143 static void usage(int ret)
146 "Mtools version %s, dated %s\n", mversion, mdate);
148 "Usage: %s [-v] msdosfile [msdosfiles...]\n", progname);
152 void mdel(int argc, char **argv, int deltype)
159 if(helpFlag(argc, argv))
161 while ((c = getopt(argc, argv, "i:vh")) != EOF) {
164 set_cmd_line_image(optarg);
180 mp.callback = del_file;
181 mp.arg = (void *) &arg;
182 mp.openflags = O_RDWR;
183 arg.deltype = deltype;
186 mp.lookupflags = ACCEPT_PLAIN; /* mdel */
189 mp.lookupflags = ACCEPT_DIR; /* mrd */
192 mp.lookupflags = ACCEPT_DIR | ACCEPT_PLAIN; /* mdeltree */
195 mp.lookupflags |= NO_DOTS;
196 for(i=optind;i<argc;i++) {
198 if(argv[i][0] && argv[i][1] == ':')
202 l = strlen(argv[i]+b);
203 if(l > 1 && argv[i][b+l-1] == '/')
204 argv[i][b+l-1] = '\0';
207 exit(main_loop(&mp, argv + optind, argc - optind));