]>
diplodocus.org Git - nmh/blob - uip/rmf.c
95328881421fd3aba988a9361d1a0602db358c33
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.
9 #include "sbr/context_save.h"
10 #include "sbr/context_replace.h"
11 #include "sbr/context_del.h"
12 #include "sbr/context_find.h"
13 #include "sbr/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
20 #include "sbr/m_maildir.h"
21 #include "sbr/m_mktemp.h"
23 #define RMF_SWITCHES \
24 X("interactive", 0, INTRSW) \
25 X("nointeractive", 0, NINTRSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(RMF
);
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(RMF
, switches
);
40 static int rmf(char *);
41 static void rma (char *);
45 main (int argc
, char **argv
)
47 bool defolder
= false;
49 char *cp
, *folder
= NULL
, newfolder
[BUFSIZ
];
50 char buf
[BUFSIZ
], **argp
, **arguments
;
53 if (nmh_init(argv
[0], true, true)) { return 1; }
55 arguments
= getarguments (invo_name
, argc
, argv
, 1);
58 while ((cp
= *argp
++)) {
60 switch (smatch (++cp
, switches
)) {
62 ambigsw (cp
, switches
);
65 die("-%s unknown", cp
);
68 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
70 print_help (buf
, switches
, 1);
73 print_version(invo_name
);
84 if (*cp
== '+' || *cp
== '@') {
86 die("only one folder at a time!");
87 folder
= pluspath (cp
);
89 die("usage: %s [+folder] [switches]", invo_name
);
93 if (!context_find ("path"))
94 free (path ("./", TFOLDER
));
96 folder
= getfolder (1);
99 fp
= m_mailpath(folder
);
100 if (!strcmp(fp
, pwd()))
101 die("sorry, you can't remove the current working directory");
104 if (interactive
== -1)
105 interactive
= defolder
;
107 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
108 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
113 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
115 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
119 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
120 if (!read_yes_or_no_if_tty (cp
))
125 if (rmf (folder
) == OK
) {
126 char *cfolder
= context_find(pfolder
);
127 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
128 printf ("[+%s now current]\n", newfolder
);
129 context_replace (pfolder
, newfolder
); /* update current folder */
132 context_save (); /* save the context file */
148 switch (i
= chdir (maildir
= m_maildir (folder
))) {
150 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
155 fp
= m_mailpath(folder
);
156 snprintf (cur
, sizeof(cur
), "atr-%s-%s", current
, fp
);
158 if (!context_del (cur
)) {
159 printf ("[+%s de-referenced]\n", folder
);
162 inform("you have no profile entry for the %s folder +%s",
163 i
== NOTOK
? "unreadable" : "read-only", folder
);
167 if ((dd
= opendir (".")) == NULL
)
168 die("unable to read folder +%s", folder
);
172 * Run the external delete hook program.
175 (void)ext_hook("del-hook", maildir
, NULL
);
177 while ((dp
= readdir (dd
))) {
178 switch (dp
->d_name
[0]) {
180 if (strcmp (dp
->d_name
, ".") == 0
181 || strcmp (dp
->d_name
, "..") == 0)
189 if (m_atoi (dp
->d_name
))
191 if (strcmp (dp
->d_name
, LINK
) == 0
192 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
195 inform("file \"%s/%s\" not deleted, continuing...",
200 if (m_unlink (dp
->d_name
) == NOTOK
) {
201 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
209 * Remove any relevant private sequences
210 * or attributes from context file.
214 if (chdir ("..") < 0) {
215 advise ("..", "chdir");
217 if (!others
&& remdir (maildir
))
220 inform("folder +%s not removed", folder
);
226 * Remove all the (private) sequence information for
227 * this folder from the profile/context list.
235 struct node
*np
, *pp
;
238 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
241 * Search context list for keys that look like
242 * "atr-something-folderpath", and remove them.
244 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
245 if (ssequal ("atr-", np
->n_name
)
246 && (j
= strlen (np
->n_name
) - plen
) > alen
247 && *(np
->n_name
+ j
) == '-'
248 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
250 inform("bug: context_del(key=\"%s\"), continuing...",
253 pp
->n_next
= np
->n_next
;