]>
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.
11 #include "sbr/m_maildir.h"
12 #include "sbr/m_mktemp.h"
14 #define RMF_SWITCHES \
15 X("interactive", 0, INTRSW) \
16 X("nointeractive", 0, NINTRSW) \
17 X("version", 0, VERSIONSW) \
18 X("help", 0, HELPSW) \
20 #define X(sw, minchars, id) id,
21 DEFINE_SWITCH_ENUM(RMF
);
24 #define X(sw, minchars, id) { sw, minchars, id },
25 DEFINE_SWITCH_ARRAY(RMF
, switches
);
31 static int rmf(char *);
32 static void rma (char *);
36 main (int argc
, char **argv
)
38 bool defolder
= false;
40 char *cp
, *folder
= NULL
, newfolder
[BUFSIZ
];
41 char buf
[BUFSIZ
], **argp
, **arguments
;
44 if (nmh_init(argv
[0], true, true)) { return 1; }
46 arguments
= getarguments (invo_name
, argc
, argv
, 1);
49 while ((cp
= *argp
++)) {
51 switch (smatch (++cp
, switches
)) {
53 ambigsw (cp
, switches
);
56 die("-%s unknown", cp
);
59 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
61 print_help (buf
, switches
, 1);
64 print_version(invo_name
);
75 if (*cp
== '+' || *cp
== '@') {
77 die("only one folder at a time!");
78 folder
= pluspath (cp
);
80 die("usage: %s [+folder] [switches]", invo_name
);
84 if (!context_find ("path"))
85 free (path ("./", TFOLDER
));
87 folder
= getfolder (1);
90 fp
= m_mailpath(folder
);
91 if (!strcmp(fp
, pwd()))
92 die("sorry, you can't remove the current working directory");
95 if (interactive
== -1)
96 interactive
= defolder
;
98 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
99 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
104 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
106 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
110 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
111 if (!read_yes_or_no_if_tty (cp
))
116 if (rmf (folder
) == OK
) {
117 char *cfolder
= context_find(pfolder
);
118 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
119 printf ("[+%s now current]\n", newfolder
);
120 context_replace (pfolder
, newfolder
); /* update current folder */
123 context_save (); /* save the context file */
139 switch (i
= chdir (maildir
= m_maildir (folder
))) {
141 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
146 fp
= m_mailpath(folder
);
147 snprintf (cur
, sizeof(cur
), "atr-%s-%s", current
, fp
);
149 if (!context_del (cur
)) {
150 printf ("[+%s de-referenced]\n", folder
);
153 inform("you have no profile entry for the %s folder +%s",
154 i
== NOTOK
? "unreadable" : "read-only", folder
);
158 if ((dd
= opendir (".")) == NULL
)
159 die("unable to read folder +%s", folder
);
163 * Run the external delete hook program.
166 (void)ext_hook("del-hook", maildir
, NULL
);
168 while ((dp
= readdir (dd
))) {
169 switch (dp
->d_name
[0]) {
171 if (strcmp (dp
->d_name
, ".") == 0
172 || strcmp (dp
->d_name
, "..") == 0)
180 if (m_atoi (dp
->d_name
))
182 if (strcmp (dp
->d_name
, LINK
) == 0
183 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
186 inform("file \"%s/%s\" not deleted, continuing...",
191 if (m_unlink (dp
->d_name
) == NOTOK
) {
192 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
200 * Remove any relevant private sequences
201 * or attributes from context file.
205 if (chdir ("..") < 0) {
206 advise ("..", "chdir");
208 if (!others
&& remdir (maildir
))
211 inform("folder +%s not removed", folder
);
217 * Remove all the (private) sequence information for
218 * this folder from the profile/context list.
226 struct node
*np
, *pp
;
229 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
232 * Search context list for keys that look like
233 * "atr-something-folderpath", and remove them.
235 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
236 if (ssequal ("atr-", np
->n_name
)
237 && (j
= strlen (np
->n_name
) - plen
) > alen
238 && *(np
->n_name
+ j
) == '-'
239 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
241 inform("bug: context_del(key=\"%s\"), continuing...",
244 pp
->n_next
= np
->n_next
;