]>
diplodocus.org Git - nmh/blob - uip/rmf.c
3043d316548c076802536749fccbfc3724a17b0a
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_del.h"
10 #include "sbr/context_find.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
18 #include "sbr/m_maildir.h"
19 #include "sbr/m_mktemp.h"
21 #define RMF_SWITCHES \
22 X("interactive", 0, INTRSW) \
23 X("nointeractive", 0, NINTRSW) \
24 X("version", 0, VERSIONSW) \
25 X("help", 0, HELPSW) \
27 #define X(sw, minchars, id) id,
28 DEFINE_SWITCH_ENUM(RMF
);
31 #define X(sw, minchars, id) { sw, minchars, id },
32 DEFINE_SWITCH_ARRAY(RMF
, switches
);
38 static int rmf(char *);
39 static void rma (char *);
43 main (int argc
, char **argv
)
45 bool defolder
= false;
47 char *cp
, *folder
= NULL
, newfolder
[BUFSIZ
];
48 char buf
[BUFSIZ
], **argp
, **arguments
;
51 if (nmh_init(argv
[0], true, true)) { return 1; }
53 arguments
= getarguments (invo_name
, argc
, argv
, 1);
56 while ((cp
= *argp
++)) {
58 switch (smatch (++cp
, switches
)) {
60 ambigsw (cp
, switches
);
63 die("-%s unknown", cp
);
66 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
68 print_help (buf
, switches
, 1);
71 print_version(invo_name
);
82 if (*cp
== '+' || *cp
== '@') {
84 die("only one folder at a time!");
85 folder
= pluspath (cp
);
87 die("usage: %s [+folder] [switches]", invo_name
);
91 if (!context_find ("path"))
92 free (path ("./", TFOLDER
));
94 folder
= getfolder (1);
97 fp
= m_mailpath(folder
);
98 if (!strcmp(fp
, pwd()))
99 die("sorry, you can't remove the current working directory");
102 if (interactive
== -1)
103 interactive
= defolder
;
105 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
106 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
111 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
113 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
117 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
118 if (!read_yes_or_no_if_tty (cp
))
123 if (rmf (folder
) == OK
) {
124 char *cfolder
= context_find(pfolder
);
125 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
126 printf ("[+%s now current]\n", newfolder
);
127 context_replace (pfolder
, newfolder
); /* update current folder */
130 context_save (); /* save the context file */
146 switch (i
= chdir (maildir
= m_maildir (folder
))) {
148 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
153 fp
= m_mailpath(folder
);
154 snprintf (cur
, sizeof(cur
), "atr-%s-%s", current
, fp
);
156 if (!context_del (cur
)) {
157 printf ("[+%s de-referenced]\n", folder
);
160 inform("you have no profile entry for the %s folder +%s",
161 i
== NOTOK
? "unreadable" : "read-only", folder
);
165 if ((dd
= opendir (".")) == NULL
)
166 die("unable to read folder +%s", folder
);
170 * Run the external delete hook program.
173 (void)ext_hook("del-hook", maildir
, NULL
);
175 while ((dp
= readdir (dd
))) {
176 switch (dp
->d_name
[0]) {
178 if (strcmp (dp
->d_name
, ".") == 0
179 || strcmp (dp
->d_name
, "..") == 0)
187 if (m_atoi (dp
->d_name
))
189 if (strcmp (dp
->d_name
, LINK
) == 0
190 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
193 inform("file \"%s/%s\" not deleted, continuing...",
198 if (m_unlink (dp
->d_name
) == NOTOK
) {
199 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
207 * Remove any relevant private sequences
208 * or attributes from context file.
212 if (chdir ("..") < 0) {
213 advise ("..", "chdir");
215 if (!others
&& remdir (maildir
))
218 inform("folder +%s not removed", folder
);
224 * Remove all the (private) sequence information for
225 * this folder from the profile/context list.
233 struct node
*np
, *pp
;
236 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
239 * Search context list for keys that look like
240 * "atr-something-folderpath", and remove them.
242 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
243 if (ssequal ("atr-", np
->n_name
)
244 && (j
= strlen (np
->n_name
) - plen
) > alen
245 && *(np
->n_name
+ j
) == '-'
246 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
248 inform("bug: context_del(key=\"%s\"), continuing...",
251 pp
->n_next
= np
->n_next
;