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", 2 },
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;
37 setlocale(LC_ALL, "");
38 invo_name = mhbasename(argv[0]);
40 /* read user profile/context */
43 arguments = getarguments(invo_name, argc, argv, 1);
46 while ((cp = *argp++)) {
48 switch (smatch(++cp, switches)) {
50 ambigsw(cp, switches);
53 adios(NULL, "-%s unknown", cp);
56 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
57 print_help(buf, switches, 1);
60 print_version(invo_name);
71 if (*cp == '+' || *cp == '@') {
73 adios(NULL, "only one folder at a time!");
75 folder = getcpy(expandfol(cp));
77 adios(NULL, "usage: %s [+folder] [switches]",
86 if (strcmp(toabsdir(folder), pwd()) == 0)
87 adios(NULL, "You can't remove the current working directory");
89 if (interactive == -1)
90 interactive = defolder;
92 if (strchr(folder, '/') && (*folder != '/') && (*folder != '.')) {
93 strcpy(newfolder, folder);
94 cp = newfolder + strlen(newfolder);
95 while (cp > newfolder && *cp != '/')
100 strncpy(newfolder, getdeffol(), sizeof(newfolder));
102 strncpy(newfolder, getdeffol(), sizeof(newfolder));
106 cp = concat("Remove folder \"", folder, "\"? ", NULL);
112 if (rmf(folder) == OK) {
113 char *cfolder = context_find(curfolder);
114 if (cfolder && strcmp(cfolder, newfolder)!=0) {
115 printf("[+%s now current]\n", newfolder);
116 /* update current folder */
117 context_replace(curfolder, newfolder);
120 context_save(); /* save the context file */
128 register char *maildir;
130 register struct dirent *dp;
133 switch (i = chdir(maildir = toabsdir(folder))) {
135 if (access(".", W_OK) != NOTOK && access("..", W_OK) != NOTOK)
136 break; /* fall otherwise */
139 snprintf(cur, sizeof(cur), "atr-%s-%s", seq_cur,
141 if (!context_del(cur)) {
142 printf("[+%s de-referenced]\n", folder);
145 advise(NULL, "you have no profile entry for the %s folder +%s",
146 i == NOTOK ? "unreadable" : "read-only",
151 if ((dd = opendir(".")) == NULL)
152 adios(NULL, "unable to read folder +%s", folder);
156 ** Run the external delete hook program.
159 ext_hook("del-hook", maildir, NULL);
161 while ((dp = readdir(dd))) {
162 switch (dp->d_name[0]) {
164 if (strcmp(dp->d_name, ".") == 0 ||
165 strcmp(dp->d_name, "..") == 0)
166 continue; /* else fall */
172 if (m_atoi(dp->d_name))
175 admonish(NULL, "file \"%s/%s\" not deleted",
180 if (unlink(dp->d_name) == NOTOK) {
181 admonish(dp->d_name, "unable to unlink %s:", folder);
189 ** Remove any relevant private sequences
190 ** or attributes from context file.
196 context_save(); /* Is this needed? meillo 2011-10 */
197 fflush(stdout); /* Is this needed? meillo 2011-10 */
198 if (rmdir(maildir) != -1) {
201 admonish(maildir, "unable to remove directory");
204 advise(NULL, "folder +%s not removed", folder);
210 ** Remove all the (private) sequence information for
211 ** this folder from the profile/context list.
217 register int alen, j, plen;
219 register struct node *np, *pp;
221 alen = strlen("atr-");
222 plen = strlen(cp = getcpy(toabsdir(folder))) + 1;
225 ** Search context list for keys that look like
226 ** "atr-something-folderpath", and remove them.
228 for (np = m_defs, pp = NULL; np; np = np->n_next) {
229 if (strncmp(np->n_name, "atr-", alen)==0 &&
230 (j = strlen(np->n_name) - plen) > alen &&
231 *(np->n_name + j) == '-' &&
232 strcmp(cp, np->n_name + j + 1) == 0) {
234 admonish(NULL, "bug: context_del(key=\"%s\")",
237 pp->n_next = np->n_next;