comparison uip/rmf.c @ 0:bce86c4163a3

Initial revision
author kono
date Mon, 18 Apr 2005 23:46:02 +0900
parents
children 441a2190cfae
comparison
equal deleted inserted replaced
-1:000000000000 0:bce86c4163a3
1 /* rmf.c - remove a folder */
2 #ifndef lint
3 static char ident[] = "@(#)$Id$";
4 #endif /* lint */
5
6 #include "../h/mh.h"
7 #include "../h/local.h"
8 #include <stdio.h>
9 #ifdef LOCALE
10 #include <locale.h>
11 #endif
12
13 /* */
14
15 static struct swit switches[] = {
16 #define INTRSW 0
17 "interactive", 0,
18 #define NINTRSW 1
19 "nointeractive", 0,
20
21 #define HELPSW 2
22 "help", 4,
23
24 NULL, 0
25 };
26
27 static int rmf();
28 static rma();
29 /* */
30
31 /* ARGSUSED */
32
33 main (argc, argv)
34 int argc;
35 char *argv[];
36 {
37 int defolder = 0,
38 interactive = -1;
39 char *cp,
40 *folder = NULL,
41 newfolder[BUFSIZ],
42 buf[100],
43 **ap,
44 **argp,
45 *arguments[MAXARGS];
46
47 #ifdef LOCALE
48 setlocale(LC_ALL, "");
49 #endif
50 #ifdef JAPAN
51 ml_init();
52 #endif /* JAPAN */
53 invo_name = r1bindex (argv[0], '/');
54 if ((cp = m_find (invo_name)) != NULL) {
55 ap = brkstring (cp = getcpy (cp), " ", "\n");
56 ap = copyip (ap, arguments);
57 }
58 else
59 ap = arguments;
60 (void) copyip (argv + 1, ap);
61 argp = arguments;
62
63 /* */
64
65 while (cp = *argp++) {
66 if (*cp == '-')
67 switch (smatch (++cp, switches)) {
68 case AMBIGSW:
69 ambigsw (cp, switches);
70 done (1);
71 case UNKWNSW:
72 adios (NULLCP, "-%s unknown", cp);
73 case HELPSW:
74 (void) sprintf (buf, "%s [+folder] [switches]", invo_name);
75 help (buf, switches);
76 done (1);
77
78 case INTRSW:
79 interactive = 1;
80 continue;
81 case NINTRSW:
82 interactive = 0;
83 continue;
84 }
85 if (*cp == '+' || *cp == '@') {
86 if (folder)
87 adios (NULLCP, "only one folder at a time!");
88 else
89 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
90 }
91 else
92 adios (NULLCP, "usage: %s [+folder] [switches]", invo_name);
93 }
94
95 /* */
96
97 if (!m_find ("path"))
98 free (path ("./", TFOLDER));
99 if (!folder) {
100 folder = m_getfolder ();
101 defolder++;
102 }
103 if (strcmp (m_mailpath (folder), pwd ()) == 0)
104 adios (NULLCP, "sorry, you can't remove the current working directory");
105
106 if (interactive == -1)
107 interactive = defolder;
108
109 if (index (folder, '/') && (*folder != '/') && (*folder != '.')) {
110 for (cp = copy (folder, newfolder); cp > newfolder && *cp != '/'; cp--)
111 continue;
112 if (cp > newfolder)
113 *cp = '\0';
114 else
115 (void) strcpy (newfolder, (cp = m_find (inbox)) ? cp : defalt);
116 }
117 else
118 (void) strcpy (newfolder, (cp = m_find (inbox)) ? cp : defalt);
119
120 if (interactive) {
121 cp = concat ("Remove folder \"", folder, "\"? ", NULLCP);
122 if (!getanswer (cp))
123 done (0);
124 free (cp);
125 }
126
127 if (rmf (folder) == OK && strcmp (m_find (pfolder), newfolder)) {
128 printf ("[+%s now current]\n", newfolder);
129 m_replace (pfolder, newfolder);
130 }
131 m_update ();
132
133 done (0);
134 }
135
136 /* */
137
138 static int rmf (folder)
139 register char *folder;
140 {
141 int i,
142 j,
143 others;
144 register char *maildir;
145 char cur[BUFSIZ];
146 #ifdef SYS5DIR
147 register struct dirent *dp;
148 #else /* SYS5DIR */
149 register struct direct *dp;
150 #endif /* SYS5DIR */
151 register DIR * dd;
152
153 #ifdef COMPAT
154 (void) m_delete (concat (current, "-", m_mailpath (folder), NULLCP));
155 #endif /* COMPAT */
156 switch (i = chdir (maildir = m_maildir (folder))) {
157 case OK:
158 if (access (".", 2) != NOTOK && access ("..", 2) != NOTOK)
159 break; /* fall otherwise */
160
161 case NOTOK:
162 (void) sprintf (cur, "atr-%s-%s", current, m_mailpath (folder));
163 if (!m_delete (cur)) {
164 printf ("[+%s de-referenced]\n", folder);
165 return OK;
166 }
167 advise (NULLCP, "you have no profile entry for the %s folder +%s",
168 i == NOTOK ? "unreadable" : "read-only", folder);
169 return NOTOK;
170 }
171
172 if ((dd = opendir (".")) == NULL)
173 adios (NULLCP, "unable to read folder +%s", folder);
174 others = 0;
175
176 j = strlen (SBACKUP);
177 while (dp = readdir (dd)) {
178 switch (dp -> d_name[0]) {
179 case '.':
180 if (strcmp (dp -> d_name, ".") == 0
181 || strcmp (dp -> d_name, "..") == 0)
182 continue; /* else fall */
183
184 case ',':
185 #ifdef MHE
186 case '+':
187 #endif /* MHE */
188 #ifdef UCI
189 case '_':
190 case '#':
191 #endif /* UCI */
192 break;
193
194 default:
195 if (m_atoi (dp -> d_name))
196 break;
197 #ifdef COMPAT
198 if (strcmp (dp -> d_name, current) == 0)
199 break;
200 #endif /* COMPAT */
201 if (strcmp (dp -> d_name, LINK) == 0
202 || strncmp (dp -> d_name, SBACKUP, j) == 0)
203 break;
204
205 admonish (NULLCP, "file \"%s/%s\" not deleted",
206 folder, dp -> d_name);
207 others++;
208 continue;
209 }
210 if (unlink (dp -> d_name) == NOTOK) {
211 admonish (dp -> d_name, "unable to unlink %s:", folder);
212 others++;
213 }
214 }
215
216 closedir (dd);
217
218 rma (folder);
219
220 (void) chdir ("..");
221 if (others == 0 && remdir (maildir))
222 return OK;
223
224 advise (NULLCP, "folder +%s not removed", folder);
225 return NOTOK;
226 }
227
228 /* */
229
230 static rma (folder)
231 register char *folder;
232 {
233 register int alen,
234 j,
235 plen;
236 register char *cp;
237 register struct node *np,
238 *pp;
239
240 alen = strlen ("atr-");
241 plen = strlen (cp = m_mailpath (folder)) + 1;
242
243 m_getdefs ();
244 for (np = m_defs, pp = NULL; np; np = np -> n_next)
245 if (ssequal ("atr-", np -> n_name)
246 && (j = strlen (np -> n_name) - plen) > alen
247 && *(np -> n_name + j) == '-'
248 && strcmp (cp, np -> n_name + j + 1) == 0) {
249 if (!np -> n_context)
250 admonish (NULLCP, "bug: m_delete(key=\"%s\")", np -> n_name);
251 if (pp) {
252 pp -> n_next = np -> n_next;
253 np = pp;
254 }
255 else
256 m_defs = np -> n_next;
257 ctxflags |= CTXMOD;
258 }
259 else
260 pp = np;
261 }