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 */
129 register char *maildir;
131 register struct dirent *dp;
134 switch (i = chdir(maildir = toabsdir(folder))) {
136 if (access(".", W_OK) != NOTOK && access("..", W_OK) != NOTOK)
137 break; /* fall otherwise */
140 snprintf(cur, sizeof(cur), "atr-%s-%s", seq_cur,
142 if (!context_del(cur)) {
143 printf("[+%s de-referenced]\n", folder);
146 advise(NULL, "you have no profile entry for the %s folder +%s",
147 i == NOTOK ? "unreadable" : "read-only",
152 if ((dd = opendir(".")) == NULL)
153 adios(NULL, "unable to read folder +%s", folder);
157 ** Run the external delete hook program.
160 ext_hook("del-hook", maildir, NULL);
162 while ((dp = readdir(dd))) {
163 switch (dp->d_name[0]) {
165 if (strcmp(dp->d_name, ".") == 0 ||
166 strcmp(dp->d_name, "..") == 0)
167 continue; /* else fall */
173 if (m_atoi(dp->d_name))
176 admonish(NULL, "file \"%s/%s\" not deleted",
181 if (unlink(dp->d_name) == NOTOK) {
182 admonish(dp->d_name, "unable to unlink %s:", folder);
190 ** Remove any relevant private sequences
191 ** or attributes from context file.
197 context_save(); /* Is this needed? meillo 2011-10 */
198 fflush(stdout); /* Is this needed? meillo 2011-10 */
199 if (rmdir(maildir) != -1) {
202 admonish(maildir, "unable to remove directory");
205 advise(NULL, "folder +%s not removed", folder);
211 ** Remove all the (private) sequence information for
212 ** this folder from the profile/context list.
218 register int alen, j, plen;
220 register struct node *np, *pp;
222 alen = strlen("atr-");
223 plen = strlen(cp = getcpy(toabsdir(folder))) + 1;
226 ** Search context list for keys that look like
227 ** "atr-something-folderpath", and remove them.
229 for (np = m_defs, pp = NULL; np; np = np->n_next) {
230 if (strncmp(np->n_name, "atr-", alen)==0 &&
231 (j = strlen(np->n_name) - plen) > alen &&
232 *(np->n_name + j) == '-' &&
233 strcmp(cp, np->n_name + j + 1) == 0) {
235 admonish(NULL, "bug: context_del(key=\"%s\")",
238 pp->n_next = np->n_next;