]>
diplodocus.org Git - nmh/blob - uip/rmf.c
f00a7bca8ef306154d5194c599d22cb86d23bb0e
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/print_help.h"
10 #include "sbr/error.h"
13 #include "sbr/m_maildir.h"
14 #include "sbr/m_mktemp.h"
16 #define RMF_SWITCHES \
17 X("interactive", 0, INTRSW) \
18 X("nointeractive", 0, NINTRSW) \
19 X("version", 0, VERSIONSW) \
20 X("help", 0, HELPSW) \
22 #define X(sw, minchars, id) id,
23 DEFINE_SWITCH_ENUM(RMF
);
26 #define X(sw, minchars, id) { sw, minchars, id },
27 DEFINE_SWITCH_ARRAY(RMF
, switches
);
33 static int rmf(char *);
34 static void rma (char *);
38 main (int argc
, char **argv
)
40 bool defolder
= false;
42 char *cp
, *folder
= NULL
, newfolder
[BUFSIZ
];
43 char buf
[BUFSIZ
], **argp
, **arguments
;
46 if (nmh_init(argv
[0], true, true)) { return 1; }
48 arguments
= getarguments (invo_name
, argc
, argv
, 1);
51 while ((cp
= *argp
++)) {
53 switch (smatch (++cp
, switches
)) {
55 ambigsw (cp
, switches
);
58 die("-%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 die("only one folder at a time!");
80 folder
= pluspath (cp
);
82 die("usage: %s [+folder] [switches]", invo_name
);
86 if (!context_find ("path"))
87 free (path ("./", TFOLDER
));
89 folder
= getfolder (1);
92 fp
= m_mailpath(folder
);
93 if (!strcmp(fp
, pwd()))
94 die("sorry, you can't remove the current working directory");
97 if (interactive
== -1)
98 interactive
= defolder
;
100 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
101 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
106 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
108 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
112 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
113 if (!read_yes_or_no_if_tty (cp
))
118 if (rmf (folder
) == OK
) {
119 char *cfolder
= context_find(pfolder
);
120 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
121 printf ("[+%s now current]\n", newfolder
);
122 context_replace (pfolder
, newfolder
); /* update current folder */
125 context_save (); /* save the context file */
141 switch (i
= chdir (maildir
= m_maildir (folder
))) {
143 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
148 fp
= m_mailpath(folder
);
149 snprintf (cur
, sizeof(cur
), "atr-%s-%s", current
, fp
);
151 if (!context_del (cur
)) {
152 printf ("[+%s de-referenced]\n", folder
);
155 inform("you have no profile entry for the %s folder +%s",
156 i
== NOTOK
? "unreadable" : "read-only", folder
);
160 if ((dd
= opendir (".")) == NULL
)
161 die("unable to read folder +%s", folder
);
165 * Run the external delete hook program.
168 (void)ext_hook("del-hook", maildir
, NULL
);
170 while ((dp
= readdir (dd
))) {
171 switch (dp
->d_name
[0]) {
173 if (strcmp (dp
->d_name
, ".") == 0
174 || strcmp (dp
->d_name
, "..") == 0)
182 if (m_atoi (dp
->d_name
))
184 if (strcmp (dp
->d_name
, LINK
) == 0
185 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
188 inform("file \"%s/%s\" not deleted, continuing...",
193 if (m_unlink (dp
->d_name
) == NOTOK
) {
194 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
202 * Remove any relevant private sequences
203 * or attributes from context file.
207 if (chdir ("..") < 0) {
208 advise ("..", "chdir");
210 if (!others
&& remdir (maildir
))
213 inform("folder +%s not removed", folder
);
219 * Remove all the (private) sequence information for
220 * this folder from the profile/context list.
228 struct node
*np
, *pp
;
231 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
234 * Search context list for keys that look like
235 * "atr-something-folderpath", and remove them.
237 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
238 if (ssequal ("atr-", np
->n_name
)
239 && (j
= strlen (np
->n_name
) - plen
) > alen
240 && *(np
->n_name
+ j
) == '-'
241 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
243 inform("bug: context_del(key=\"%s\"), continuing...",
246 pp
->n_next
= np
->n_next
;