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.
16 static struct swit switches[] = {
20 { "nointeractive", 2 },
31 static int rmf(char *);
32 static void rma(char *);
36 main(int argc, char **argv)
38 int defolder = 0, interactive = -1;
39 char *cp, *folder = NULL, newfolder[BUFSIZ];
40 char buf[BUFSIZ], **argp, **arguments;
42 setlocale(LC_ALL, "");
43 invo_name = mhbasename(argv[0]);
45 /* read user profile/context */
48 arguments = getarguments(invo_name, argc, argv, 1);
51 while ((cp = *argp++)) {
53 switch (smatch(++cp, switches)) {
55 ambigsw(cp, switches);
58 adios(EX_USAGE, NULL, "-%s unknown", cp);
61 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
62 print_help(buf, switches, 1);
63 exit(argc == 2 ? EX_OK : EX_USAGE);
65 print_version(invo_name);
66 exit(argc == 2 ? EX_OK : EX_USAGE);
76 if (*cp == '+' || *cp == '@') {
78 adios(EX_USAGE, NULL, "only one folder at a time!");
80 folder = mh_xstrdup(expandfol(cp));
82 adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]",
91 if (strcmp(toabsdir(folder), pwd()) == 0)
92 adios(EX_USAGE, NULL, "You can't remove the current working directory");
94 if (interactive == -1)
95 interactive = defolder;
97 if (strchr(folder, '/') && (*folder != '/') && (*folder != '.')) {
98 strcpy(newfolder, folder);
99 cp = newfolder + strlen(newfolder);
100 while (cp > newfolder && *cp != '/')
105 strncpy(newfolder, getdeffol(), sizeof(newfolder));
107 strncpy(newfolder, getdeffol(), sizeof(newfolder));
111 cp = concat("Remove folder \"", folder, "\"? ", NULL);
117 if (rmf(folder) == OK) {
118 char *cfolder = context_find(curfolder);
119 if (cfolder && strcmp(cfolder, newfolder)!=0) {
120 printf("[+%s now current]\n", newfolder);
121 /* update current folder */
122 context_replace(curfolder, newfolder);
125 context_save(); /* save the context file */
138 switch (i = chdir(maildir = toabsdir(folder))) {
140 if (access(".", W_OK) != NOTOK && access("..", W_OK) != NOTOK)
141 break; /* fall otherwise */
144 snprintf(cur, sizeof(cur), "atr-%s-%s", seq_cur,
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",
151 i == NOTOK ? "unreadable" : "read-only",
156 if ((dd = opendir(".")) == NULL)
157 adios(EX_IOERR, NULL, "unable to read folder +%s", folder);
161 ** Run the external delete hook program.
164 ext_hook("del-hook", maildir, NULL);
166 while ((dp = readdir(dd))) {
167 switch (dp->d_name[0]) {
169 if (strcmp(dp->d_name, ".") == 0 ||
170 strcmp(dp->d_name, "..") == 0)
171 continue; /* else fall */
177 if (m_atoi(dp->d_name))
180 admonish(NULL, "file \"%s/%s\" not deleted",
185 if (unlink(dp->d_name) == NOTOK) {
186 admonish(dp->d_name, "unable to unlink %s:", folder);
194 ** Remove any relevant private sequences
195 ** or attributes from context file.
201 context_save(); /* Is this needed? meillo 2011-10 */
202 fflush(stdout); /* Is this needed? meillo 2011-10 */
203 if (rmdir(maildir) != -1) {
206 admonish(maildir, "unable to remove directory");
209 advise(NULL, "folder +%s not removed", folder);
215 ** Remove all the (private) sequence information for
216 ** this folder from the profile/context list.
224 struct node *np, *pp;
226 alen = strlen("atr-");
227 plen = strlen(cp = mh_xstrdup(toabsdir(folder))) + 1;
230 ** Search context list for keys that look like
231 ** "atr-something-folderpath", and remove them.
233 for (np = m_defs, pp = NULL; np; np = np->n_next) {
234 if (strncmp(np->n_name, "atr-", alen)==0 &&
235 (j = strlen(np->n_name) - plen) > alen &&
236 *(np->n_name + j) == '-' &&
237 strcmp(cp, np->n_name + j + 1) == 0) {
239 admonish(NULL, "bug: context_del(key=\"%s\")",
242 pp->n_next = np->n_next;