]>
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.
10 #include "../sbr/m_maildir.h"
11 #include "../sbr/m_mktemp.h"
13 #define RMF_SWITCHES \
14 X("interactive", 0, INTRSW) \
15 X("nointeractive", 0, NINTRSW) \
16 X("version", 0, VERSIONSW) \
17 X("help", 0, HELPSW) \
19 #define X(sw, minchars, id) id,
20 DEFINE_SWITCH_ENUM(RMF
);
23 #define X(sw, minchars, id) { sw, minchars, id },
24 DEFINE_SWITCH_ARRAY(RMF
, switches
);
30 static int rmf(char *);
31 static void rma (char *);
35 main (int argc
, char **argv
)
37 int defolder
= 0, interactive
= -1;
38 char *cp
, *folder
= NULL
, newfolder
[BUFSIZ
];
39 char buf
[BUFSIZ
], **argp
, **arguments
;
41 if (nmh_init(argv
[0], 1)) { return 1; }
43 arguments
= getarguments (invo_name
, argc
, argv
, 1);
46 while ((cp
= *argp
++)) {
48 switch (smatch (++cp
, switches
)) {
50 ambigsw (cp
, switches
);
53 adios (NULL
, "-%s unknown", cp
);
56 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
58 print_help (buf
, switches
, 1);
61 print_version(invo_name
);
72 if (*cp
== '+' || *cp
== '@') {
74 adios (NULL
, "only one folder at a time!");
75 folder
= pluspath (cp
);
77 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
81 if (!context_find ("path"))
82 free (path ("./", TFOLDER
));
84 folder
= getfolder (1);
87 if (strcmp (m_mailpath (folder
), pwd ()) == 0)
88 adios (NULL
, "sorry, you can't remove the current working directory");
90 if (interactive
== -1)
91 interactive
= defolder
;
93 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
94 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
99 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
101 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
105 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
106 if (!read_yes_or_no_if_tty (cp
))
111 if (rmf (folder
) == OK
) {
112 char *cfolder
= context_find(pfolder
);
113 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
114 printf ("[+%s now current]\n", newfolder
);
115 context_replace (pfolder
, newfolder
); /* update current folder */
118 context_save (); /* save the context file */
132 switch (i
= chdir (maildir
= m_maildir (folder
))) {
134 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
139 snprintf (cur
, sizeof(cur
), "atr-%s-%s",
140 current
, m_mailpath (folder
));
141 if (!context_del (cur
)) {
142 printf ("[+%s de-referenced]\n", folder
);
145 inform("you have no profile entry for the %s folder +%s",
146 i
== NOTOK
? "unreadable" : "read-only", folder
);
150 if ((dd
= opendir (".")) == NULL
)
151 adios (NULL
, "unable to read folder +%s", folder
);
155 * Run the external delete hook program.
158 (void)ext_hook("del-hook", maildir
, NULL
);
160 while ((dp
= readdir (dd
))) {
161 switch (dp
->d_name
[0]) {
163 if (strcmp (dp
->d_name
, ".") == 0
164 || strcmp (dp
->d_name
, "..") == 0)
172 if (m_atoi (dp
->d_name
))
174 if (strcmp (dp
->d_name
, LINK
) == 0
175 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
178 inform("file \"%s/%s\" not deleted, continuing...",
183 if (m_unlink (dp
->d_name
) == NOTOK
) {
184 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
192 * Remove any relevant private sequences
193 * or attributes from context file.
197 if (chdir ("..") < 0) {
198 advise ("..", "chdir");
200 if (others
== 0 && remdir (maildir
))
203 inform("folder +%s not removed", folder
);
209 * Remove all the (private) sequence information for
210 * this folder from the profile/context list.
218 struct node
*np
, *pp
;
221 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
224 * Search context list for keys that look like
225 * "atr-something-folderpath", and remove them.
227 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
228 if (ssequal ("atr-", np
->n_name
)
229 && (j
= strlen (np
->n_name
) - plen
) > alen
230 && *(np
->n_name
+ j
) == '-'
231 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
233 inform("bug: context_del(key=\"%s\"), continuing...",
236 pp
->n_next
= np
->n_next
;