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 = getcpy(expandfol(cp));
79 adios(NULL, "usage: %s [+folder] [switches]",
88 if (strcmp(toabsdir(folder), pwd()) == 0)
89 adios(NULL, "You can't remove the current working directory");
91 if (interactive == -1)
92 interactive = defolder;
94 if (strchr(folder, '/') && (*folder != '/') && (*folder != '.')) {
95 strcpy(newfolder, folder);
96 cp = newfolder + strlen(newfolder);
97 while (cp > newfolder && *cp != '/')
102 strncpy(newfolder, getdeffol(), sizeof(newfolder));
104 strncpy(newfolder, getdeffol(), sizeof(newfolder));
108 cp = concat("Remove folder \"", folder, "\"? ", NULL);
114 if (rmf(folder) == OK) {
115 char *cfolder = context_find(curfolder);
116 if (cfolder && strcmp(cfolder, newfolder)) {
117 printf("[+%s now current]\n", newfolder);
118 /* update current folder */
119 context_replace(curfolder, newfolder);
122 context_save(); /* save the context file */
131 register char *maildir;
133 register struct dirent *dp;
136 switch (i = chdir(maildir = toabsdir(folder))) {
138 if (access(".", W_OK) != NOTOK &&
139 access("..", W_OK) != NOTOK)
140 break; /* fall otherwise */
143 snprintf(cur, sizeof(cur), "atr-%s-%s",
144 current, toabsdir(folder));
145 if (!context_del(cur)) {
146 printf("[+%s de-referenced]\n", folder);
149 advise(NULL, "you have no profile entry for the %s folder +%s", i == NOTOK ? "unreadable" : "read-only", folder);
153 if ((dd = opendir(".")) == NULL)
154 adios(NULL, "unable to read folder +%s", folder);
158 ** Run the external delete hook program.
161 ext_hook("del-hook", maildir, NULL);
163 j = strlen(backup_prefix);
164 while ((dp = readdir(dd))) {
165 switch (dp->d_name[0]) {
167 if (strcmp(dp->d_name, ".") == 0 ||
168 strcmp(dp->d_name, "..") == 0)
169 continue; /* else fall */
182 if (m_atoi(dp->d_name))
184 if (strcmp(dp->d_name, altmsglink) == 0 ||
186 backup_prefix, j) == 0)
189 admonish(NULL, "file \"%s/%s\" not deleted",
194 if (unlink(dp->d_name) == NOTOK) {
195 admonish(dp->d_name, "unable to unlink %s:", folder);
203 ** Remove any relevant private sequences
204 ** or attributes from context file.
210 context_save(); /* Is this needed? meillo 2011-10 */
211 fflush(stdout); /* Is this needed? meillo 2011-10 */
212 if (rmdir(maildir) != -1) {
215 admonish(maildir, "unable to remove directory");
218 advise(NULL, "folder +%s not removed", folder);
224 ** Remove all the (private) sequence information for
225 ** this folder from the profile/context list.
231 register int alen, j, plen;
233 register struct node *np, *pp;
235 alen = strlen("atr-");
236 plen = strlen(cp = getcpy(toabsdir(folder))) + 1;
239 ** Search context list for keys that look like
240 ** "atr-something-folderpath", and remove them.
242 for (np = m_defs, pp = NULL; np; np = np->n_next) {
243 if (isprefix("atr-", np->n_name) &&
244 (j = strlen(np->n_name) - plen) > alen &&
245 *(np->n_name + j) == '-' &&
246 strcmp(cp, np->n_name + j + 1) == 0) {
248 admonish(NULL, "bug: context_del(key=\"%s\")",
251 pp->n_next = np->n_next;