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.
15 static struct swit switches[] = {
19 { "nointeractive", 2 },
30 static int rmf(char *);
31 static void rma(char *);
35 main(int argc, char **argv)
37 int defolder = 0, interactive = -1;
38 char *cp, *folder = NULL, newfolder[BUFSIZ];
39 char buf[BUFSIZ], **argp, **arguments;
41 setlocale(LC_ALL, "");
42 invo_name = mhbasename(argv[0]);
44 /* read user profile/context */
47 arguments = getarguments(invo_name, argc, argv, 1);
50 while ((cp = *argp++)) {
52 switch (smatch(++cp, switches)) {
54 ambigsw(cp, switches);
57 adios(EX_USAGE, NULL, "-%s unknown", cp);
60 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
61 print_help(buf, switches, 1);
62 exit(argc == 2 ? EX_OK : EX_USAGE);
64 print_version(invo_name);
65 exit(argc == 2 ? EX_OK : EX_USAGE);
75 if (*cp == '+' || *cp == '@') {
77 adios(EX_USAGE, NULL, "only one folder at a time!");
79 folder = getcpy(expandfol(cp));
81 adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]",
90 if (strcmp(toabsdir(folder), pwd()) == 0)
91 adios(EX_USAGE, NULL, "You can't remove the current working directory");
93 if (interactive == -1)
94 interactive = defolder;
96 if (strchr(folder, '/') && (*folder != '/') && (*folder != '.')) {
97 strcpy(newfolder, folder);
98 cp = newfolder + strlen(newfolder);
99 while (cp > newfolder && *cp != '/')
104 strncpy(newfolder, getdeffol(), sizeof(newfolder));
106 strncpy(newfolder, getdeffol(), sizeof(newfolder));
110 cp = concat("Remove folder \"", folder, "\"? ", NULL);
116 if (rmf(folder) == OK) {
117 char *cfolder = context_find(curfolder);
118 if (cfolder && strcmp(cfolder, newfolder)!=0) {
119 printf("[+%s now current]\n", newfolder);
120 /* update current folder */
121 context_replace(curfolder, newfolder);
124 context_save(); /* save the context file */
137 switch (i = chdir(maildir = toabsdir(folder))) {
139 if (access(".", W_OK) != NOTOK && access("..", W_OK) != NOTOK)
140 break; /* fall otherwise */
143 snprintf(cur, sizeof(cur), "atr-%s-%s", seq_cur,
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",
150 i == NOTOK ? "unreadable" : "read-only",
155 if ((dd = opendir(".")) == NULL)
156 adios(EX_IOERR, NULL, "unable to read folder +%s", folder);
160 ** Run the external delete hook program.
163 ext_hook("del-hook", maildir, NULL);
165 while ((dp = readdir(dd))) {
166 switch (dp->d_name[0]) {
168 if (strcmp(dp->d_name, ".") == 0 ||
169 strcmp(dp->d_name, "..") == 0)
170 continue; /* else fall */
176 if (m_atoi(dp->d_name))
179 admonish(NULL, "file \"%s/%s\" not deleted",
184 if (unlink(dp->d_name) == NOTOK) {
185 admonish(dp->d_name, "unable to unlink %s:", folder);
193 ** Remove any relevant private sequences
194 ** or attributes from context file.
200 context_save(); /* Is this needed? meillo 2011-10 */
201 fflush(stdout); /* Is this needed? meillo 2011-10 */
202 if (rmdir(maildir) != -1) {
205 admonish(maildir, "unable to remove directory");
208 advise(NULL, "folder +%s not removed", folder);
214 ** Remove all the (private) sequence information for
215 ** this folder from the profile/context list.
223 struct node *np, *pp;
225 alen = strlen("atr-");
226 plen = strlen(cp = getcpy(toabsdir(folder))) + 1;
229 ** Search context list for keys that look like
230 ** "atr-something-folderpath", and remove them.
232 for (np = m_defs, pp = NULL; np; np = np->n_next) {
233 if (strncmp(np->n_name, "atr-", alen)==0 &&
234 (j = strlen(np->n_name) - plen) > alen &&
235 *(np->n_name + j) == '-' &&
236 strcmp(cp, np->n_name + j + 1) == 0) {
238 admonish(NULL, "bug: context_del(key=\"%s\")",
241 pp->n_next = np->n_next;