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