3 * rmf.c -- remove a folder
10 static struct swit switches[] = {
14 { "nointeractive", 0 },
25 static int rmf(char *);
26 static void rma (char *);
30 main (int argc, char **argv)
32 int defolder = 0, interactive = -1;
33 char *cp, *folder = NULL, newfolder[BUFSIZ];
34 char buf[BUFSIZ], **argp, **arguments;
37 setlocale(LC_ALL, "");
39 invo_name = r1bindex (argv[0], '/');
41 /* read user profile/context */
44 arguments = getarguments (invo_name, argc, argv, 1);
47 while ((cp = *argp++)) {
49 switch (smatch (++cp, switches)) {
51 ambigsw (cp, switches);
54 adios (NULL, "-%s unknown", cp);
57 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
59 print_help (buf, switches, 1);
62 print_version(invo_name);
73 if (*cp == '+' || *cp == '@') {
75 adios (NULL, "only one folder at a time!");
77 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
79 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
83 if (!context_find ("path"))
84 free (path ("./", TFOLDER));
86 folder = getfolder (1);
89 if (strcmp (m_mailpath (folder), pwd ()) == 0)
90 adios (NULL, "sorry, you can't remove the current working directory");
92 if (interactive == -1)
93 interactive = defolder;
95 if (strchr (folder, '/') && (*folder != '/') && (*folder != '.')) {
96 for (cp = copy (folder, newfolder); cp > newfolder && *cp != '/'; cp--)
101 strncpy (newfolder, getfolder(0), sizeof(newfolder));
103 strncpy (newfolder, getfolder(0), sizeof(newfolder));
107 cp = concat ("Remove folder \"", folder, "\"? ", NULL);
113 if (rmf (folder) == OK && strcmp (context_find (pfolder), newfolder)) {
114 printf ("[+%s now current]\n", newfolder);
115 context_replace (pfolder, newfolder); /* update current folder */
117 context_save (); /* save the context file */
125 register char *maildir;
127 register struct dirent *dp;
130 switch (i = chdir (maildir = m_maildir (folder))) {
132 if (access (".", W_OK) != NOTOK && access ("..", W_OK) != NOTOK)
133 break; /* fall otherwise */
136 snprintf (cur, sizeof(cur), "atr-%s-%s",
137 current, m_mailpath (folder));
138 if (!context_del (cur)) {
139 printf ("[+%s de-referenced]\n", folder);
142 advise (NULL, "you have no profile entry for the %s folder +%s",
143 i == NOTOK ? "unreadable" : "read-only", folder);
147 if ((dd = opendir (".")) == NULL)
148 adios (NULL, "unable to read folder +%s", folder);
151 j = strlen(BACKUP_PREFIX);
152 while ((dp = readdir (dd))) {
153 switch (dp->d_name[0]) {
155 if (strcmp (dp->d_name, ".") == 0
156 || strcmp (dp->d_name, "..") == 0)
157 continue; /* else fall */
170 if (m_atoi (dp->d_name))
172 if (strcmp (dp->d_name, LINK) == 0
173 || strncmp (dp->d_name, BACKUP_PREFIX, j) == 0)
176 admonish (NULL, "file \"%s/%s\" not deleted",
181 if (unlink (dp->d_name) == NOTOK) {
182 admonish (dp->d_name, "unable to unlink %s:", folder);
190 * Remove any relevant private sequences
191 * or attributes from context file.
196 if (others == 0 && remdir (maildir))
199 advise (NULL, "folder +%s not removed", folder);
205 * Remove all the (private) sequence information for
206 * this folder from the profile/context list.
212 register int alen, j, plen;
214 register struct node *np, *pp;
216 /* sanity check - check that context has been read */
218 adios (NULL, "oops, context hasn't been read yet");
220 alen = strlen ("atr-");
221 plen = strlen (cp = m_mailpath (folder)) + 1;
224 * Search context list for keys that look like
225 * "atr-something-folderpath", and remove them.
227 for (np = m_defs, pp = NULL; np; np = np->n_next) {
228 if (ssequal ("atr-", np->n_name)
229 && (j = strlen (np->n_name) - plen) > alen
230 && *(np->n_name + j) == '-'
231 && strcmp (cp, np->n_name + j + 1) == 0) {
233 admonish (NULL, "bug: context_del(key=\"%s\")", np->n_name);
235 pp->n_next = np->n_next;