]>
diplodocus.org Git - nmh/blob - uip/rmf.c
1 /* rmf.c -- remove a folder
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
15 #include "sbr/m_maildir.h"
16 #include "sbr/m_mktemp.h"
18 #define RMF_SWITCHES \
19 X("interactive", 0, INTRSW) \
20 X("nointeractive", 0, NINTRSW) \
21 X("version", 0, VERSIONSW) \
22 X("help", 0, HELPSW) \
24 #define X(sw, minchars, id) id,
25 DEFINE_SWITCH_ENUM(RMF
);
28 #define X(sw, minchars, id) { sw, minchars, id },
29 DEFINE_SWITCH_ARRAY(RMF
, switches
);
35 static int rmf(char *);
36 static void rma (char *);
40 main (int argc
, char **argv
)
42 bool defolder
= false;
44 char *cp
, *folder
= NULL
, newfolder
[BUFSIZ
];
45 char buf
[BUFSIZ
], **argp
, **arguments
;
48 if (nmh_init(argv
[0], true, true)) { return 1; }
50 arguments
= getarguments (invo_name
, argc
, argv
, 1);
53 while ((cp
= *argp
++)) {
55 switch (smatch (++cp
, switches
)) {
57 ambigsw (cp
, switches
);
60 die("-%s unknown", cp
);
63 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
65 print_help (buf
, switches
, 1);
68 print_version(invo_name
);
79 if (*cp
== '+' || *cp
== '@') {
81 die("only one folder at a time!");
82 folder
= pluspath (cp
);
84 die("usage: %s [+folder] [switches]", invo_name
);
88 if (!context_find ("path"))
89 free (path ("./", TFOLDER
));
91 folder
= getfolder (1);
94 fp
= m_mailpath(folder
);
95 if (!strcmp(fp
, pwd()))
96 die("sorry, you can't remove the current working directory");
99 if (interactive
== -1)
100 interactive
= defolder
;
102 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
103 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
108 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
110 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
114 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
115 if (!read_yes_or_no_if_tty (cp
))
120 if (rmf (folder
) == OK
) {
121 char *cfolder
= context_find(pfolder
);
122 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
123 printf ("[+%s now current]\n", newfolder
);
124 context_replace (pfolder
, newfolder
); /* update current folder */
127 context_save (); /* save the context file */
143 switch (i
= chdir (maildir
= m_maildir (folder
))) {
145 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
150 fp
= m_mailpath(folder
);
151 snprintf (cur
, sizeof(cur
), "atr-%s-%s", current
, fp
);
153 if (!context_del (cur
)) {
154 printf ("[+%s de-referenced]\n", folder
);
157 inform("you have no profile entry for the %s folder +%s",
158 i
== NOTOK
? "unreadable" : "read-only", folder
);
162 if ((dd
= opendir (".")) == NULL
)
163 die("unable to read folder +%s", folder
);
167 * Run the external delete hook program.
170 (void)ext_hook("del-hook", maildir
, NULL
);
172 while ((dp
= readdir (dd
))) {
173 switch (dp
->d_name
[0]) {
175 if (strcmp (dp
->d_name
, ".") == 0
176 || strcmp (dp
->d_name
, "..") == 0)
184 if (m_atoi (dp
->d_name
))
186 if (strcmp (dp
->d_name
, LINK
) == 0
187 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
190 inform("file \"%s/%s\" not deleted, continuing...",
195 if (m_unlink (dp
->d_name
) == NOTOK
) {
196 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
204 * Remove any relevant private sequences
205 * or attributes from context file.
209 if (chdir ("..") < 0) {
210 advise ("..", "chdir");
212 if (!others
&& remdir (maildir
))
215 inform("folder +%s not removed", folder
);
221 * Remove all the (private) sequence information for
222 * this folder from the profile/context list.
230 struct node
*np
, *pp
;
233 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
236 * Search context list for keys that look like
237 * "atr-something-folderpath", and remove them.
239 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
240 if (ssequal ("atr-", np
->n_name
)
241 && (j
= strlen (np
->n_name
) - plen
) > alen
242 && *(np
->n_name
+ j
) == '-'
243 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
245 inform("bug: context_del(key=\"%s\"), continuing...",
248 pp
->n_next
= np
->n_next
;