]>
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
;
42 if (nmh_init(argv
[0], 1)) { return 1; }
44 arguments
= getarguments (invo_name
, argc
, argv
, 1);
47 while ((cp
= *argp
++)) {
49 switch (smatch (++cp
, switches
)) {
51 ambigsw (cp
, switches
);
54 adios (NULL
, "-%s unknown", cp
);
57 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
59 print_help (buf
, switches
, 1);
62 print_version(invo_name
);
73 if (*cp
== '+' || *cp
== '@') {
75 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 fp
= m_mailpath(folder
);
89 if (!strcmp(fp
, pwd()))
90 adios (NULL
, "sorry, you can't remove the current working directory");
93 if (interactive
== -1)
94 interactive
= defolder
;
96 if (strchr (folder
, '/') && (*folder
!= '/') && (*folder
!= '.')) {
97 for (cp
= stpcpy(newfolder
, folder
); cp
> newfolder
&& *cp
!= '/'; cp
--)
102 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
104 strncpy (newfolder
, getfolder(0), sizeof(newfolder
));
108 cp
= concat ("Remove folder \"", folder
, "\"? ", NULL
);
109 if (!read_yes_or_no_if_tty (cp
))
114 if (rmf (folder
) == OK
) {
115 char *cfolder
= context_find(pfolder
);
116 if (cfolder
&& strcmp (cfolder
, newfolder
)) {
117 printf ("[+%s now current]\n", newfolder
);
118 context_replace (pfolder
, newfolder
); /* update current folder */
121 context_save (); /* save the context file */
136 switch (i
= chdir (maildir
= m_maildir (folder
))) {
138 if (access (".", W_OK
) != NOTOK
&& access ("..", W_OK
) != NOTOK
)
143 fp
= m_mailpath(folder
);
144 snprintf (cur
, sizeof(cur
), "atr-%s-%s", current
, fp
);
146 if (!context_del (cur
)) {
147 printf ("[+%s de-referenced]\n", folder
);
150 inform("you have no profile entry for the %s folder +%s",
151 i
== NOTOK
? "unreadable" : "read-only", folder
);
155 if ((dd
= opendir (".")) == NULL
)
156 adios (NULL
, "unable to read folder +%s", folder
);
160 * Run the external delete hook program.
163 (void)ext_hook("del-hook", maildir
, NULL
);
165 while ((dp
= readdir (dd
))) {
166 switch (dp
->d_name
[0]) {
168 if (strcmp (dp
->d_name
, ".") == 0
169 || strcmp (dp
->d_name
, "..") == 0)
177 if (m_atoi (dp
->d_name
))
179 if (strcmp (dp
->d_name
, LINK
) == 0
180 || has_prefix(dp
->d_name
, BACKUP_PREFIX
))
183 inform("file \"%s/%s\" not deleted, continuing...",
188 if (m_unlink (dp
->d_name
) == NOTOK
) {
189 admonish (dp
->d_name
, "unable to unlink %s:", folder
);
197 * Remove any relevant private sequences
198 * or attributes from context file.
202 if (chdir ("..") < 0) {
203 advise ("..", "chdir");
205 if (others
== 0 && remdir (maildir
))
208 inform("folder +%s not removed", folder
);
214 * Remove all the (private) sequence information for
215 * this folder from the profile/context list.
223 struct node
*np
, *pp
;
226 plen
= strlen (cp
= m_mailpath (folder
)) + 1;
229 * Search context list for keys that look like
230 * "atr-something-folderpath", and remove them.
232 for (np
= m_defs
, pp
= NULL
; np
; np
= np
->n_next
) {
233 if (ssequal ("atr-", np
->n_name
)
234 && (j
= strlen (np
->n_name
) - plen
) > alen
235 && *(np
->n_name
+ j
) == '-'
236 && strcmp (cp
, np
->n_name
+ j
+ 1) == 0) {
238 inform("bug: context_del(key=\"%s\"), continuing...",
241 pp
->n_next
= np
->n_next
;