]>
diplodocus.org Git - nmh/blob - uip/rmf.c
acf71e744093f6b6689af636320a2c660bd2d4ab
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_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/error.h"
14 #include "sbr/m_maildir.h"
15 #include "sbr/m_mktemp.h"
17 #define RMF_SWITCHES \
18 X("interactive", 0, INTRSW) \
19 X("nointeractive", 0, NINTRSW) \
20 X("version", 0, VERSIONSW) \
21 X("help", 0, HELPSW) \
23 #define X(sw, minchars, id) id,
24 DEFINE_SWITCH_ENUM(RMF
);
27 #define X(sw, minchars, id) { sw, minchars, id },
28 DEFINE_SWITCH_ARRAY(RMF
, switches
);
34 static int rmf(char *);
35 static void rma (char *);
39 main (int argc
, char **argv
)
41 bool defolder
= false;
43 char *cp
, *folder
= NULL
, newfolder
[BUFSIZ
];
44 char buf
[BUFSIZ
], **argp
, **arguments
;
47 if (nmh_init(argv
[0], true, true)) { return 1; }
49 arguments
= getarguments (invo_name
, argc
, argv
, 1);
52 while ((cp
= *argp
++)) {
54 switch (smatch (++cp
, switches
)) {
56 ambigsw (cp
, switches
);
59 die("-%s unknown", cp
);
62 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
64 print_help (buf
, switches
, 1);
67 print_version(invo_name
);
78 if (*cp
== '+' || *cp
== '@') {
80 die("only one folder at a time!");
81 folder
= pluspath (cp
);
83 die("usage: %s [+folder] [switches]", invo_name
);
87 if (!context_find ("path"))
88 free (path ("./", TFOLDER
));
90 folder
= getfolder (1);
93 fp
= m_mailpath(folder
);
94 if (!strcmp(fp
, pwd()))
95 die("sorry, you can't remove the current working directory");
98 if (interactive
== -1)
99 interactive
= defolder
;
101 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
102 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
107 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
109 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
113 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
114 if (!read_yes_or_no_if_tty (cp
))
119 if (rmf (folder
) == OK
) {
120 char *cfolder
= context_find(pfolder
);
121 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
122 printf ("[+%s now current]\n", newfolder
);
123 context_replace (pfolder
, newfolder
); /* update current folder */
126 context_save (); /* save the context file */
142 switch (i
= chdir (maildir
= m_maildir (folder
))) {
144 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
149 fp
= m_mailpath(folder
);
150 snprintf (cur
, sizeof(cur
), "atr-%s-%s", current
, fp
);
152 if (!context_del (cur
)) {
153 printf ("[+%s de-referenced]\n", folder
);
156 inform("you have no profile entry for the %s folder +%s",
157 i
== NOTOK
? "unreadable" : "read-only", folder
);
161 if ((dd
= opendir (".")) == NULL
)
162 die("unable to read folder +%s", folder
);
166 * Run the external delete hook program.
169 (void)ext_hook("del-hook", maildir
, NULL
);
171 while ((dp
= readdir (dd
))) {
172 switch (dp
->d_name
[0]) {
174 if (strcmp (dp
->d_name
, ".") == 0
175 || strcmp (dp
->d_name
, "..") == 0)
183 if (m_atoi (dp
->d_name
))
185 if (strcmp (dp
->d_name
, LINK
) == 0
186 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
189 inform("file \"%s/%s\" not deleted, continuing...",
194 if (m_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.
208 if (chdir ("..") < 0) {
209 advise ("..", "chdir");
211 if (!others
&& remdir (maildir
))
214 inform("folder +%s not removed", folder
);
220 * Remove all the (private) sequence information for
221 * this folder from the profile/context list.
229 struct node
*np
, *pp
;
232 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
235 * Search context list for keys that look like
236 * "atr-something-folderpath", and remove them.
238 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
239 if (ssequal ("atr-", np
->n_name
)
240 && (j
= strlen (np
->n_name
) - plen
) > alen
241 && *(np
->n_name
+ j
) == '-'
242 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
244 inform("bug: context_del(key=\"%s\"), continuing...",
247 pp
->n_next
= np
->n_next
;