]> diplodocus.org Git - nmh/blob - uip/rmf.c
Escape literal leading full stop in man/new.man.
[nmh] / uip / rmf.c
1
2 /*
3 * rmf.c -- remove a folder
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12
13 #define RMF_SWITCHES \
14 X("interactive", 0, INTRSW) \
15 X("nointeractive", 0, NINTRSW) \
16 X("version", 0, VERSIONSW) \
17 X("help", 0, HELPSW) \
18
19 #define X(sw, minchars, id) id,
20 DEFINE_SWITCH_ENUM(RMF);
21 #undef X
22
23 #define X(sw, minchars, id) { sw, minchars, id },
24 DEFINE_SWITCH_ARRAY(RMF, switches);
25 #undef X
26
27 /*
28 * static prototypes
29 */
30 static int rmf(char *);
31 static void rma (char *);
32
33
34 int
35 main (int argc, char **argv)
36 {
37 int defolder = 0, interactive = -1;
38 char *cp, *folder = NULL, newfolder[BUFSIZ];
39 char buf[BUFSIZ], **argp, **arguments;
40
41 if (nmh_init(argv[0], 1)) { return 1; }
42
43 arguments = getarguments (invo_name, argc, argv, 1);
44 argp = arguments;
45
46 while ((cp = *argp++)) {
47 if (*cp == '-') {
48 switch (smatch (++cp, switches)) {
49 case AMBIGSW:
50 ambigsw (cp, switches);
51 done (1);
52 case UNKWNSW:
53 adios (NULL, "-%s unknown", cp);
54
55 case HELPSW:
56 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
57 invo_name);
58 print_help (buf, switches, 1);
59 done (0);
60 case VERSIONSW:
61 print_version(invo_name);
62 done (0);
63
64 case INTRSW:
65 interactive = 1;
66 continue;
67 case NINTRSW:
68 interactive = 0;
69 continue;
70 }
71 }
72 if (*cp == '+' || *cp == '@') {
73 if (folder)
74 adios (NULL, "only one folder at a time!");
75 else
76 folder = pluspath (cp);
77 } else {
78 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
79 }
80 }
81
82 if (!context_find ("path"))
83 free (path ("./", TFOLDER));
84 if (!folder) {
85 folder = getfolder (1);
86 defolder++;
87 }
88 if (strcmp (m_mailpath (folder), pwd ()) == 0)
89 adios (NULL, "sorry, you can't remove the current working directory");
90
91 if (interactive == -1)
92 interactive = defolder;
93
94 if (strchr (folder, '/') && (*folder != '/') && (*folder != '.')) {
95 for (cp = stpcpy(newfolder, folder); cp > newfolder && *cp != '/'; cp--)
96 continue;
97 if (cp > newfolder)
98 *cp = '\0';
99 else
100 strncpy (newfolder, getfolder(0), sizeof(newfolder));
101 } else {
102 strncpy (newfolder, getfolder(0), sizeof(newfolder));
103 }
104
105 if (interactive) {
106 cp = concat ("Remove folder \"", folder, "\"? ", NULL);
107 if (!read_yes_or_no_if_tty (cp))
108 done (0);
109 free (cp);
110 }
111
112 if (rmf (folder) == OK) {
113 char *cfolder = context_find(pfolder);
114 if (cfolder && strcmp (cfolder, newfolder)) {
115 printf ("[+%s now current]\n", newfolder);
116 context_replace (pfolder, newfolder); /* update current folder */
117 }
118 }
119 context_save (); /* save the context file */
120 done (0);
121 return 1;
122 }
123
124 static int
125 rmf (char *folder)
126 {
127 int i, others;
128 char *maildir;
129 char cur[BUFSIZ];
130 struct dirent *dp;
131 DIR *dd;
132
133 switch (i = chdir (maildir = m_maildir (folder))) {
134 case OK:
135 if (access (".", W_OK) != NOTOK && access ("..", W_OK) != NOTOK)
136 break;
137 /* FALLTHRU */
138
139 case NOTOK:
140 snprintf (cur, sizeof(cur), "atr-%s-%s",
141 current, m_mailpath (folder));
142 if (!context_del (cur)) {
143 printf ("[+%s de-referenced]\n", folder);
144 return OK;
145 }
146 advise (NULL, "you have no profile entry for the %s folder +%s",
147 i == NOTOK ? "unreadable" : "read-only", folder);
148 return NOTOK;
149 }
150
151 if ((dd = opendir (".")) == NULL)
152 adios (NULL, "unable to read folder +%s", folder);
153 others = 0;
154
155 /*
156 * Run the external delete hook program.
157 */
158
159 (void)ext_hook("del-hook", maildir, NULL);
160
161 while ((dp = readdir (dd))) {
162 switch (dp->d_name[0]) {
163 case '.':
164 if (strcmp (dp->d_name, ".") == 0
165 || strcmp (dp->d_name, "..") == 0)
166 continue;
167 /* FALLTHRU */
168
169 case ',':
170 break;
171
172 default:
173 if (m_atoi (dp->d_name))
174 break;
175 if (strcmp (dp->d_name, LINK) == 0
176 || has_prefix(dp->d_name, BACKUP_PREFIX))
177 break;
178
179 admonish (NULL, "file \"%s/%s\" not deleted",
180 folder, dp->d_name);
181 others++;
182 continue;
183 }
184 if (m_unlink (dp->d_name) == NOTOK) {
185 admonish (dp->d_name, "unable to unlink %s:", folder);
186 others++;
187 }
188 }
189
190 closedir (dd);
191
192 /*
193 * Remove any relevant private sequences
194 * or attributes from context file.
195 */
196 rma (folder);
197
198 if (chdir ("..") < 0) {
199 advise ("..", "chdir");
200 }
201 if (others == 0 && remdir (maildir))
202 return OK;
203
204 advise (NULL, "folder +%s not removed", folder);
205 return NOTOK;
206 }
207
208
209 /*
210 * Remove all the (private) sequence information for
211 * this folder from the profile/context list.
212 */
213
214 static void
215 rma (char *folder)
216 {
217 int alen, j, plen;
218 char *cp;
219 struct node *np, *pp;
220
221 alen = LEN("atr-");
222 plen = strlen (cp = m_mailpath (folder)) + 1;
223
224 /*
225 * Search context list for keys that look like
226 * "atr-something-folderpath", and remove them.
227 */
228 for (np = m_defs, pp = NULL; np; np = np->n_next) {
229 if (ssequal ("atr-", np->n_name)
230 && (j = strlen (np->n_name) - plen) > alen
231 && *(np->n_name + j) == '-'
232 && strcmp (cp, np->n_name + j + 1) == 0) {
233 if (!np->n_context)
234 admonish (NULL, "bug: context_del(key=\"%s\")", np->n_name);
235 if (pp) {
236 pp->n_next = np->n_next;
237 np = pp;
238 } else {
239 m_defs = np->n_next;
240 }
241 ctxflags |= CTXMOD;
242 } else {
243 pp = np;
244 }
245 }
246 }