rmf.c
3.33 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2002 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 2, 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* 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>
const char *argp_program_version = "rmf (" PACKAGE_STRING ")";
static char doc[] = "GNU MH rmf";
static char args_doc[] = N_("[+folder]");
/* GNU options */
static struct argp_option options[] = {
{"folder", 'f', "FOLDER", 0, N_("Specify the folder to delete")},
{"interactive", 'i', "BOOL", OPTION_ARG_OPTIONAL,
N_("Interactive mode: ask for confirmation before removing each folder")},
{"recursive", 'r', NULL, 0, N_("Recursively delete all subfolders")},
{ N_("\nUse -help switch to obtain the list of traditional MH options. "), 0, 0, OPTION_DOC, "" },
{ 0 }
};
/* Traditional MH options */
struct mh_option mh_option[] = {
{"interactive", 1, 'i', MH_OPT_BOOL, NULL},
{ 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 int
opt_handler (int key, char *arg, void *unused)
{
switch (key)
{
case '+':
case 'f':
explicit_folder = 1;
current_folder = arg;
break;
case 'i':
interactive = is_true (arg);
break;
case 'r':
recurse = is_true (arg);
break;
default:
return 1;
}
return 0;
}
static void
rmf (const char *name)
{
DIR *dir;
struct dirent *entry;
dir = opendir (name);
if (!dir)
{
mh_error (_("can't scan folder %s: %s"), name, strerror (errno));
return;
}
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;
asprintf (&p, "%s/%s", name, entry->d_name);
if (stat (p, &st) < 0)
{
mh_error (_("can't stat %s: %s"), p, strerror (errno));
}
else if (S_ISDIR (st.st_mode))
{
if (recurse)
rmf (p);
}
else
{
if (unlink (p))
mh_error (_("can't unlink %s: %s"), p, strerror (errno));
}
free (p);
}
closedir (dir);
rmdir (name);
}
int
main (int argc, char **argv)
{
char *name;
/* Native Language Support */
mu_init_nls ();
mh_argp_parse (argc, argv, options, mh_option, args_doc, doc,
opt_handler, NULL, NULL);
if (!explicit_folder)
interactive = 1;
name = mh_expand_name (current_folder, 0);
rmf (name);
return 0;
}