]>
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 int defolder
= 0, interactive
= -1;
39 char *cp
, *folder
= NULL
, newfolder
[BUFSIZ
];
40 char buf
[BUFSIZ
], **argp
, **arguments
;
43 if (nmh_init(argv
[0], 1)) { return 1; }
45 arguments
= getarguments (invo_name
, argc
, argv
, 1);
48 while ((cp
= *argp
++)) {
50 switch (smatch (++cp
, switches
)) {
52 ambigsw (cp
, switches
);
55 adios (NULL
, "-%s unknown", cp
);
58 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
60 print_help (buf
, switches
, 1);
63 print_version(invo_name
);
74 if (*cp
== '+' || *cp
== '@') {
76 adios (NULL
, "only one folder at a time!");
77 folder
= pluspath (cp
);
79 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
83 if (!context_find ("path"))
84 free (path ("./", TFOLDER
));
86 folder
= getfolder (1);
89 fp
= m_mailpath(folder
);
90 if (!strcmp(fp
, pwd()))
91 adios (NULL
, "sorry, you can't remove the current working directory");
94 if (interactive
== -1)
95 interactive
= defolder
;
97 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
98 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
103 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
105 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
109 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
110 if (!read_yes_or_no_if_tty (cp
))
115 if (rmf (folder
) == OK
) {
116 char *cfolder
= context_find(pfolder
);
117 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
118 printf ("[+%s now current]\n", newfolder
);
119 context_replace (pfolder
, newfolder
); /* update current folder */
122 context_save (); /* save the context file */
137 switch (i
= chdir (maildir
= m_maildir (folder
))) {
139 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
144 fp
= m_mailpath(folder
);
145 snprintf (cur
, sizeof(cur
), "atr-%s-%s", current
, fp
);
147 if (!context_del (cur
)) {
148 printf ("[+%s de-referenced]\n", folder
);
151 inform("you have no profile entry for the %s folder +%s",
152 i
== NOTOK
? "unreadable" : "read-only", folder
);
156 if ((dd
= opendir (".")) == NULL
)
157 adios (NULL
, "unable to read folder +%s", folder
);
161 * Run the external delete hook program.
164 (void)ext_hook("del-hook", maildir
, NULL
);
166 while ((dp
= readdir (dd
))) {
167 switch (dp
->d_name
[0]) {
169 if (strcmp (dp
->d_name
, ".") == 0
170 || strcmp (dp
->d_name
, "..") == 0)
178 if (m_atoi (dp
->d_name
))
180 if (strcmp (dp
->d_name
, LINK
) == 0
181 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
184 inform("file \"%s/%s\" not deleted, continuing...",
189 if (m_unlink (dp
->d_name
) == NOTOK
) {
190 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
198 * Remove any relevant private sequences
199 * or attributes from context file.
203 if (chdir ("..") < 0) {
204 advise ("..", "chdir");
206 if (others
== 0 && remdir (maildir
))
209 inform("folder +%s not removed", folder
);
215 * Remove all the (private) sequence information for
216 * this folder from the profile/context list.
224 struct node
*np
, *pp
;
227 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
230 * Search context list for keys that look like
231 * "atr-something-folderpath", and remove them.
233 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
234 if (ssequal ("atr-", np
->n_name
)
235 && (j
= strlen (np
->n_name
) - plen
) > alen
236 && *(np
->n_name
+ j
) == '-'
237 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
239 inform("bug: context_del(key=\"%s\"), continuing...",
242 pp
->n_next
= np
->n_next
;