2 ** rmf.c -- remove a folder
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
11 static struct swit switches[] = {
15 { "nointeractive", 0 },
26 static int rmf(char *);
27 static void rma(char *);
31 main(int argc, char **argv)
33 int defolder = 0, interactive = -1;
34 char *cp, *folder = NULL, newfolder[BUFSIZ];
35 char buf[BUFSIZ], **argp, **arguments;
38 setlocale(LC_ALL, "");
40 invo_name = mhbasename(argv[0]);
42 /* read user profile/context */
45 arguments = getarguments(invo_name, argc, argv, 1);
48 while ((cp = *argp++)) {
50 switch (smatch(++cp, switches)) {
52 ambigsw(cp, switches);
55 adios(NULL, "-%s unknown", cp);
58 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
59 print_help(buf, switches, 1);
62 print_version(invo_name);
73 if (*cp == '+' || *cp == '@') {
75 adios(NULL, "only one folder at a time!");
77 folder = pluspath(cp);
79 adios(NULL, "usage: %s [+folder] [switches]",
84 if (!context_find("path"))
85 free(path("./", TFOLDER));
87 folder = getfolder(FCUR);
90 if (strcmp(m_mailpath(folder), pwd()) == 0)
91 adios(NULL, "sorry, you can't remove the current working directory");
93 if (interactive == -1)
94 interactive = defolder;
96 if (strchr(folder, '/') && (*folder != '/') && (*folder != '.')) {
97 for (cp = copy(folder, newfolder);
98 cp > newfolder && *cp != '/'; cp--)
103 strncpy(newfolder, getfolder(FDEF), sizeof(newfolder));
105 strncpy(newfolder, getfolder(FDEF), sizeof(newfolder));
109 cp = concat("Remove folder \"", folder, "\"? ", NULL);
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 /* update current folder */
120 context_replace(pfolder, newfolder);
123 context_save(); /* save the context file */
132 register char *maildir;
134 register struct dirent *dp;
137 switch (i = chdir(maildir = m_maildir(folder))) {
139 if (access(".", W_OK) != NOTOK &&
140 access("..", W_OK) != NOTOK)
141 break; /* fall otherwise */
144 snprintf(cur, sizeof(cur), "atr-%s-%s",
145 current, m_mailpath(folder));
146 if (!context_del(cur)) {
147 printf("[+%s de-referenced]\n", folder);
150 advise(NULL, "you have no profile entry for the %s folder +%s", i == NOTOK ? "unreadable" : "read-only", folder);
154 if ((dd = opendir(".")) == NULL)
155 adios(NULL, "unable to read folder +%s", folder);
159 ** Run the external delete hook program.
162 (void)ext_hook("del-hook", maildir, (char *)0);
164 j = strlen(backup_prefix);
165 while ((dp = readdir(dd))) {
166 switch (dp->d_name[0]) {
168 if (strcmp(dp->d_name, ".") == 0 ||
169 strcmp(dp->d_name, "..") == 0)
170 continue; /* else fall */
183 if (m_atoi(dp->d_name))
185 if (strcmp(dp->d_name, altmsglink) == 0 ||
187 backup_prefix, j) == 0)
190 admonish(NULL, "file \"%s/%s\" not deleted",
195 if (unlink(dp->d_name) == NOTOK) {
196 admonish(dp->d_name, "unable to unlink %s:", folder);
204 ** Remove any relevant private sequences
205 ** or attributes from context file.
211 context_save(); /* Is this needed? meillo 2011-10 */
212 fflush(stdout); /* Is this needed? meillo 2011-10 */
213 if (rmdir(maildir) != -1) {
216 admonish(maildir, "unable to remove directory");
219 advise(NULL, "folder +%s not removed", folder);
225 ** Remove all the (private) sequence information for
226 ** this folder from the profile/context list.
232 register int alen, j, plen;
234 register struct node *np, *pp;
236 alen = strlen("atr-");
237 plen = strlen(cp = m_mailpath(folder)) + 1;
240 ** Search context list for keys that look like
241 ** "atr-something-folderpath", and remove them.
243 for (np = m_defs, pp = NULL; np; np = np->n_next) {
244 if (isprefix("atr-", np->n_name) &&
245 (j = strlen(np->n_name) - plen) > alen &&
246 *(np->n_name + j) == '-' &&
247 strcmp(cp, np->n_name + j + 1) == 0) {
249 admonish(NULL, "bug: context_del(key=\"%s\")",
252 pp->n_next = np->n_next;