3 * rmf.c -- remove a folder
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 static struct swit switches[] = {
18 { "nointeractive", 0 },
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;
41 setlocale(LC_ALL, "");
43 invo_name = r1bindex (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 (NULL, "-%s unknown", cp);
61 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
63 print_help (buf, switches, 1);
66 print_version(invo_name);
77 if (*cp == '+' || *cp == '@') {
79 adios (NULL, "only one folder at a time!");
81 folder = pluspath (cp);
83 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
87 if (!context_find ("path"))
88 free (path ("./", TFOLDER));
90 folder = getfolder (1);
93 if (strcmp (m_mailpath (folder), pwd ()) == 0)
94 adios (NULL, "sorry, you can't remove the current working directory");
96 if (interactive == -1)
97 interactive = defolder;
99 if (strchr (folder, '/') && (*folder != '/') && (*folder != '.')) {
100 for (cp = copy (folder, newfolder); cp > newfolder && *cp != '/'; cp--)
105 strncpy (newfolder, getfolder(0), sizeof(newfolder));
107 strncpy (newfolder, getfolder(0), sizeof(newfolder));
111 cp = concat ("Remove folder \"", folder, "\"? ", NULL);
117 if (rmf (folder) == OK) {
118 char *cfolder = context_find(pfolder);
119 if (cfolder && strcmp (cfolder, newfolder)) {
120 printf ("[+%s now current]\n", newfolder);
121 context_replace (pfolder, newfolder); /* update current folder */
124 context_save (); /* save the context file */
132 register char *maildir;
134 register struct dirent *dp;
137 switch (i = chdir (maildir = m_maildir (folder))) {
139 if (access (".", W_OK) != NOTOK && access ("..", W_OK) != NOTOK)
140 break; /* fall otherwise */
143 snprintf (cur, sizeof(cur), "atr-%s-%s",
144 current, m_mailpath (folder));
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", folder);
154 if ((dd = opendir (".")) == NULL)
155 adios (NULL, "unable to read folder +%s", folder);
159 * Run the external delete hook program.
162 (void)ext_hook("del-hook", maildir, (char *)0);
164 j = strlen(BACKUP_PREFIX);
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 */
183 if (m_atoi (dp->d_name))
185 if (strcmp (dp->d_name, LINK) == 0
186 || strncmp (dp->d_name, BACKUP_PREFIX, j) == 0)
189 admonish (NULL, "file \"%s/%s\" not deleted",
194 if (unlink (dp->d_name) == NOTOK) {
195 admonish (dp->d_name, "unable to unlink %s:", folder);
203 * Remove any relevant private sequences
204 * or attributes from context file.
209 if (others == 0 && remdir (maildir))
212 advise (NULL, "folder +%s not removed", folder);
218 * Remove all the (private) sequence information for
219 * this folder from the profile/context list.
225 register int alen, j, plen;
227 register struct node *np, *pp;
229 alen = strlen ("atr-");
230 plen = strlen (cp = m_mailpath (folder)) + 1;
233 * Search context list for keys that look like
234 * "atr-something-folderpath", and remove them.
236 for (np = m_defs, pp = NULL; np; np = np->n_next) {
237 if (ssequal ("atr-", np->n_name)
238 && (j = strlen (np->n_name) - plen) > alen
239 && *(np->n_name + j) == '-'
240 && strcmp (cp, np->n_name + j + 1) == 0) {
242 admonish (NULL, "bug: context_del(key=\"%s\")", np->n_name);
244 pp->n_next = np->n_next;