rmf.c
4.63 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2002, 2005, 2006, 2007, 2008, 2009, 2010 Free Software
Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
/* MH rmf command */
#include <mh.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
static char doc[] = N_("GNU MH rmf")"\v"
N_("Use -help to obtain the list of traditional MH options.");
static char args_doc[] = N_("[+folder]");
/* GNU options */
static struct argp_option options[] = {
{"folder", ARG_FOLDER, N_("FOLDER"), 0,
N_("specify the folder to delete")},
{"interactive", ARG_INTERACTIVE, N_("BOOL"), OPTION_ARG_OPTIONAL,
N_("interactive mode: ask for confirmation before removing each folder")},
{"nointeractive", ARG_NOINTERACTIVE, NULL, OPTION_HIDDEN, ""},
{"recursive", ARG_RECURSIVE, NULL, 0,
N_("recursively delete all subfolders")},
{"norecursive", ARG_NORECURSIVE, NULL, OPTION_HIDDEN, ""},
{ 0 }
};
/* Traditional MH options */
struct mh_option mh_option[] = {
{ "interactive", MH_OPT_BOOL },
{ 0 }
};
int explicit_folder; /* Was the folder explicitly given */
int interactive; /* Ask for confirmation before deleting */
int recurse; /* Recursively process all the sub-directories */
static char *cur_folder_path; /* Full pathname of the current folder */
static char *folder_name; /* Name of the (topmost) folder to be
deleted */
static error_t
opt_handler (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case ARG_FOLDER:
explicit_folder = 1;
folder_name = arg;
break;
case ARG_INTERACTIVE:
interactive = is_true (arg);
break;
case ARG_NOINTERACTIVE:
interactive = 0;
break;
case ARG_RECURSIVE:
recurse = is_true (arg);
break;
case ARG_NORECURSIVE:
recurse = 0;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static char *
current_folder_path ()
{
mu_mailbox_t mbox = mh_open_folder (mh_current_folder (), 0);
mu_url_t url;
char *p;
mu_mailbox_get_url (mbox, &url);
p = (char*) mu_url_to_string (url);
if (strncmp (p, "mh:", 3) == 0)
p += 3;
return p;
}
static int
rmf (const char *name)
{
DIR *dir;
struct dirent *entry;
int failures = 0;
dir = opendir (name);
if (!dir)
{
mu_error (_("cannot scan folder %s: %s"), name, strerror (errno));
return 1;
}
if (interactive && !mh_getyn (_("Remove folder %s"), name))
exit (0);
while ((entry = readdir (dir)))
{
char *p;
struct stat st;
if (strcmp (entry->d_name, ".") == 0
|| strcmp (entry->d_name, "..") == 0)
continue;
p = mh_safe_make_file_name (name, entry->d_name);
if (stat (p, &st) < 0)
{
mu_diag_funcall (MU_DIAG_ERROR, "stat", p, errno);
}
else if (S_ISDIR (st.st_mode))
{
if (recurse)
failures += rmf (p);
else
{
printf ("%s: file `%s' not deleted, continuing...\n",
mu_program_name, p);
failures++;
}
}
else
{
if (unlink (p))
{
mu_diag_funcall (MU_DIAG_ERROR, "unlink", p, errno);
failures++;
}
}
free (p);
}
closedir (dir);
if (failures == 0)
failures += rmdir (name);
else
printf ("%s: folder `%s' not removed\n",
mu_program_name, name);
if (failures == 0)
{
if (cur_folder_path && strcmp (name, cur_folder_path) == 0)
{
mh_set_current_folder ("inbox");
mh_global_sequences_drop ();
mh_global_save_state ();
printf ("[+inbox now current]\n");
}
}
return failures;
}
int
main (int argc, char **argv)
{
char *name;
/* Native Language Support */
MU_APP_INIT_NLS ();
mh_argp_init ();
mh_argp_parse (&argc, &argv, 0, options, mh_option, args_doc, doc,
opt_handler, NULL, NULL);
cur_folder_path = current_folder_path ();
if (!explicit_folder)
{
interactive = 1;
name = cur_folder_path;
}
else
name = mh_expand_name (NULL, folder_name, 0);
rmf (name);
return 0;
}