3 * rmf.c -- remove a folder
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 static struct swit switches[] = {
16 { "nointeractive", 0 },
27 static int rmf(char *);
28 static void rma (char *);
32 main (int argc, char **argv)
34 int defolder = 0, interactive = -1;
35 char *cp, *folder = NULL, newfolder[BUFSIZ];
36 char buf[BUFSIZ], **argp, **arguments;
39 setlocale(LC_ALL, "");
41 invo_name = r1bindex (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]",
61 print_help (buf, switches, 1);
64 print_version(invo_name);
75 if (*cp == '+' || *cp == '@') {
77 adios (NULL, "only one folder at a time!");
79 folder = pluspath (cp);
81 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
85 if (!context_find ("path"))
86 free (path ("./", TFOLDER));
88 folder = getfolder (1);
91 if (strcmp (m_mailpath (folder), pwd ()) == 0)
92 adios (NULL, "sorry, you can't remove the current working directory");
94 if (interactive == -1)
95 interactive = defolder;
97 if (strchr (folder, '/') && (*folder != '/') && (*folder != '.')) {
98 for (cp = copy (folder, newfolder); cp > newfolder && *cp != '/'; cp--)
103 strncpy (newfolder, getfolder(0), sizeof(newfolder));
105 strncpy (newfolder, getfolder(0), sizeof(newfolder));
109 cp = concat ("Remove folder \"", folder, "\"? ", NULL);
115 if (rmf (folder) == OK) {
116 char *cfolder = context_find(pfolder);
117 if (cfolder && strcmp (cfolder, newfolder)) {
118 printf ("[+%s now current]\n", newfolder);
119 context_replace (pfolder, newfolder); /* update current folder */
122 context_save (); /* save the context file */
131 register char *maildir;
133 register struct dirent *dp;
136 switch (i = chdir (maildir = m_maildir (folder))) {
138 if (access (".", W_OK) != NOTOK && access ("..", W_OK) != NOTOK)
139 break; /* fall otherwise */
142 snprintf (cur, sizeof(cur), "atr-%s-%s",
143 current, m_mailpath (folder));
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", folder);
153 if ((dd = opendir (".")) == NULL)
154 adios (NULL, "unable to read folder +%s", folder);
158 * Run the external delete hook program.
161 (void)ext_hook("del-hook", maildir, (char *)0);
163 j = strlen(BACKUP_PREFIX);
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))
177 if (strcmp (dp->d_name, LINK) == 0
178 || strncmp (dp->d_name, BACKUP_PREFIX, j) == 0)
181 admonish (NULL, "file \"%s/%s\" not deleted",
186 if (unlink (dp->d_name) == NOTOK) {
187 admonish (dp->d_name, "unable to unlink %s:", folder);
195 * Remove any relevant private sequences
196 * or attributes from context file.
201 if (others == 0 && remdir (maildir))
204 advise (NULL, "folder +%s not removed", folder);
210 * Remove all the (private) sequence information for
211 * this folder from the profile/context list.
217 register int alen, j, plen;
219 register struct node *np, *pp;
221 alen = strlen ("atr-");
222 plen = strlen (cp = m_mailpath (folder)) + 1;
225 * Search context list for keys that look like
226 * "atr-something-folderpath", and remove them.
228 for (np = m_defs, pp = NULL; np; np = np->n_next) {
229 if (ssequal ("atr-", np->n_name)
230 && (j = strlen (np->n_name) - plen) > alen
231 && *(np->n_name + j) == '-'
232 && strcmp (cp, np->n_name + j + 1) == 0) {
234 admonish (NULL, "bug: context_del(key=\"%s\")", np->n_name);
236 pp->n_next = np->n_next;