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 },
28 char *version=VERSION;
33 static int rmf(char *);
34 static void rma(char *);
38 main(int argc, char **argv)
40 int defolder = 0, interactive = -1;
41 char *cp, *folder = NULL, newfolder[BUFSIZ];
42 char buf[BUFSIZ], **argp, **arguments;
44 setlocale(LC_ALL, "");
45 invo_name = mhbasename(argv[0]);
47 /* read user profile/context */
50 arguments = getarguments(invo_name, argc, argv, 1);
53 while ((cp = *argp++)) {
55 switch (smatch(++cp, switches)) {
57 ambigsw(cp, switches);
60 adios(EX_USAGE, NULL, "-%s unknown", cp);
63 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
64 print_help(buf, switches, 1);
65 exit(argc == 2 ? EX_OK : EX_USAGE);
67 print_version(invo_name);
68 exit(argc == 2 ? EX_OK : EX_USAGE);
78 if (*cp == '+' || *cp == '@') {
80 adios(EX_USAGE, NULL, "only one folder at a time!");
82 folder = mh_xstrdup(expandfol(cp));
84 adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]",
93 if (strcmp(toabsdir(folder), pwd()) == 0)
94 adios(EX_USAGE, NULL, "You can't remove the current working directory");
96 if (interactive == -1)
97 interactive = defolder;
99 if (strchr(folder, '/') && (*folder != '/') && (*folder != '.')) {
100 strcpy(newfolder, folder);
101 cp = newfolder + strlen(newfolder);
102 while (cp > newfolder && *cp != '/')
107 strncpy(newfolder, getdeffol(), sizeof(newfolder));
109 strncpy(newfolder, getdeffol(), sizeof(newfolder));
113 cp = concat("Remove folder \"", folder, "\"? ", NULL);
119 if (rmf(folder) == OK) {
120 char *cfolder = context_find(curfolder);
121 if (cfolder && strcmp(cfolder, newfolder)!=0) {
122 printf("[+%s now current]\n", newfolder);
123 /* update current folder */
124 context_replace(curfolder, newfolder);
127 context_save(); /* save the context file */
140 switch (i = chdir(maildir = toabsdir(folder))) {
142 if (access(".", W_OK) != NOTOK && access("..", W_OK) != NOTOK)
143 break; /* fall otherwise */
146 snprintf(cur, sizeof(cur), "atr-%s-%s", seq_cur,
148 if (!context_del(cur)) {
149 printf("[+%s de-referenced]\n", folder);
152 advise(NULL, "you have no profile entry for the %s folder +%s",
153 i == NOTOK ? "unreadable" : "read-only",
158 if ((dd = opendir(".")) == NULL)
159 adios(EX_IOERR, NULL, "unable to read folder +%s", folder);
163 ** Run the external delete hook program.
166 ext_hook("del-hook", maildir, NULL);
168 while ((dp = readdir(dd))) {
169 switch (dp->d_name[0]) {
171 if (strcmp(dp->d_name, ".") == 0 ||
172 strcmp(dp->d_name, "..") == 0)
173 continue; /* else fall */
179 if (m_atoi(dp->d_name))
182 admonish(NULL, "file \"%s/%s\" not deleted",
187 if (unlink(dp->d_name) == NOTOK) {
188 admonish(dp->d_name, "unable to unlink %s:", folder);
196 ** Remove any relevant private sequences
197 ** or attributes from context file.
203 context_save(); /* Is this needed? meillo 2011-10 */
204 fflush(stdout); /* Is this needed? meillo 2011-10 */
205 if (rmdir(maildir) != -1) {
208 admonish(maildir, "unable to remove directory");
211 advise(NULL, "folder +%s not removed", folder);
217 ** Remove all the (private) sequence information for
218 ** this folder from the profile/context list.
226 struct node *np, *pp;
228 alen = strlen("atr-");
229 plen = strlen(cp = mh_xstrdup(toabsdir(folder))) + 1;
232 ** Search context list for keys that look like
233 ** "atr-something-folderpath", and remove them.
235 for (np = m_defs, pp = NULL; np; np = np->n_next) {
236 if (strncmp(np->n_name, "atr-", alen)==0 &&
237 (j = strlen(np->n_name) - plen) > alen &&
238 *(np->n_name + j) == '-' &&
239 strcmp(cp, np->n_name + j + 1) == 0) {
241 admonish(NULL, "bug: context_del(key=\"%s\")",
244 pp->n_next = np->n_next;