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.
14 static struct swit switches[] = {
18 { "nointeractive", 2 },
29 static int rmf(char *);
30 static void rma(char *);
34 main(int argc, char **argv)
36 int defolder = 0, interactive = -1;
37 char *cp, *folder = NULL, newfolder[BUFSIZ];
38 char buf[BUFSIZ], **argp, **arguments;
40 setlocale(LC_ALL, "");
41 invo_name = mhbasename(argv[0]);
43 /* read user profile/context */
46 arguments = getarguments(invo_name, argc, argv, 1);
49 while ((cp = *argp++)) {
51 switch (smatch(++cp, switches)) {
53 ambigsw(cp, switches);
56 adios(NULL, "-%s unknown", cp);
59 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
60 print_help(buf, switches, 1);
63 print_version(invo_name);
74 if (*cp == '+' || *cp == '@') {
76 adios(NULL, "only one folder at a time!");
78 folder = getcpy(expandfol(cp));
80 adios(NULL, "usage: %s [+folder] [switches]",
89 if (strcmp(toabsdir(folder), pwd()) == 0)
90 adios(NULL, "You can't remove the current working directory");
92 if (interactive == -1)
93 interactive = defolder;
95 if (strchr(folder, '/') && (*folder != '/') && (*folder != '.')) {
96 strcpy(newfolder, folder);
97 cp = newfolder + strlen(newfolder);
98 while (cp > newfolder && *cp != '/')
103 strncpy(newfolder, getdeffol(), sizeof(newfolder));
105 strncpy(newfolder, getdeffol(), sizeof(newfolder));
109 cp = concat("Remove folder \"", folder, "\"? ", NULL);
115 if (rmf(folder) == OK) {
116 char *cfolder = context_find(curfolder);
117 if (cfolder && strcmp(cfolder, newfolder)!=0) {
118 printf("[+%s now current]\n", newfolder);
119 /* update current folder */
120 context_replace(curfolder, newfolder);
123 context_save(); /* save the context file */
131 register char *maildir;
133 register struct dirent *dp;
136 switch (i = chdir(maildir = toabsdir(folder))) {
138 if (access(".", W_OK) != NOTOK && access("..", W_OK) != NOTOK)
139 break; /* fall otherwise */
142 snprintf(cur, sizeof(cur), "atr-%s-%s", seq_cur,
144 if (!context_del(cur)) {
145 printf("[+%s de-referenced]\n", folder);
148 advise(NULL, "you have no profile entry for the %s folder +%s",
149 i == NOTOK ? "unreadable" : "read-only",
154 if ((dd = opendir(".")) == NULL)
155 adios(NULL, "unable to read folder +%s", folder);
159 ** Run the external delete hook program.
162 ext_hook("del-hook", maildir, NULL);
164 while ((dp = readdir(dd))) {
165 switch (dp->d_name[0]) {
167 if (strcmp(dp->d_name, ".") == 0 ||
168 strcmp(dp->d_name, "..") == 0)
169 continue; /* else fall */
175 if (m_atoi(dp->d_name))
178 admonish(NULL, "file \"%s/%s\" not deleted",
183 if (unlink(dp->d_name) == NOTOK) {
184 admonish(dp->d_name, "unable to unlink %s:", folder);
192 ** Remove any relevant private sequences
193 ** or attributes from context file.
199 context_save(); /* Is this needed? meillo 2011-10 */
200 fflush(stdout); /* Is this needed? meillo 2011-10 */
201 if (rmdir(maildir) != -1) {
204 admonish(maildir, "unable to remove directory");
207 advise(NULL, "folder +%s not removed", folder);
213 ** Remove all the (private) sequence information for
214 ** this folder from the profile/context list.
220 register int alen, j, plen;
222 register struct node *np, *pp;
224 alen = strlen("atr-");
225 plen = strlen(cp = getcpy(toabsdir(folder))) + 1;
228 ** Search context list for keys that look like
229 ** "atr-something-folderpath", and remove them.
231 for (np = m_defs, pp = NULL; np; np = np->n_next) {
232 if (strncmp(np->n_name, "atr-", alen)==0 &&
233 (j = strlen(np->n_name) - plen) > alen &&
234 *(np->n_name + j) == '-' &&
235 strcmp(cp, np->n_name + j + 1) == 0) {
237 admonish(NULL, "bug: context_del(key=\"%s\")",
240 pp->n_next = np->n_next;