]>
diplodocus.org Git - nmh/blob - uip/rmf.c
3 * rmf.c -- remove a folder
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
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!");
76 folder
= pluspath (cp
);
78 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
82 if (!context_find ("path"))
83 free (path ("./", TFOLDER
));
85 folder
= getfolder (1);
88 if (strcmp (m_mailpath (folder
), pwd ()) == 0)
89 adios (NULL
, "sorry, you can't remove the current working directory");
91 if (interactive
== -1)
92 interactive
= defolder
;
94 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
95 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
100 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
102 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
106 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
107 if (!read_yes_or_no_if_tty (cp
))
112 if (rmf (folder
) == OK
) {
113 char *cfolder
= context_find(pfolder
);
114 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
115 printf ("[+%s now current]\n", newfolder
);
116 context_replace (pfolder
, newfolder
); /* update current folder */
119 context_save (); /* save the context file */
133 switch (i
= chdir (maildir
= m_maildir (folder
))) {
135 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
140 snprintf (cur
, sizeof(cur
), "atr-%s-%s",
141 current
, m_mailpath (folder
));
142 if (!context_del (cur
)) {
143 printf ("[+%s de-referenced]\n", folder
);
146 advise (NULL
, "you have no profile entry for the %s folder +%s",
147 i
== NOTOK
? "unreadable" : "read-only", folder
);
151 if ((dd
= opendir (".")) == NULL
)
152 adios (NULL
, "unable to read folder +%s", folder
);
156 * Run the external delete hook program.
159 (void)ext_hook("del-hook", maildir
, NULL
);
161 while ((dp
= readdir (dd
))) {
162 switch (dp
->d_name
[0]) {
164 if (strcmp (dp
->d_name
, ".") == 0
165 || strcmp (dp
->d_name
, "..") == 0)
173 if (m_atoi (dp
->d_name
))
175 if (strcmp (dp
->d_name
, LINK
) == 0
176 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
179 admonish (NULL
, "file \"%s/%s\" not deleted",
184 if (m_unlink (dp
->d_name
) == NOTOK
) {
185 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
193 * Remove any relevant private sequences
194 * or attributes from context file.
198 if (chdir ("..") < 0) {
199 advise ("..", "chdir");
201 if (others
== 0 && remdir (maildir
))
204 advise (NULL
, "folder +%s not removed", folder
);
210 * Remove all the (private) sequence information for
211 * this folder from the profile/context list.
219 struct node
*np
, *pp
;
222 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
225 * Search context list for keys that look like
226 * "atr-something-folderpath", and remove them.
228 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
229 if (ssequal ("atr-", np
->n_name
)
230 && (j
= strlen (np
->n_name
) - plen
) > alen
231 && *(np
->n_name
+ j
) == '-'
232 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
234 admonish (NULL
, "bug: context_del(key=\"%s\")", np
->n_name
);
236 pp
->n_next
= np
->n_next
;