Removed void casts for ignored return values of function calls.
[mmh] / uip / rmf.c
1 /*
2 ** rmf.c -- remove a folder
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10
11 static struct swit switches[] = {
12 #define INTRSW  0
13         { "interactive", 0 },
14 #define NINTRSW  1
15         { "nointeractive", 0 },
16 #define VERSIONSW  2
17         { "version", 0 },
18 #define HELPSW  3
19         { "help", 0 },
20         { NULL, 0 }
21 };
22
23 /*
24 ** static prototypes
25 */
26 static int rmf(char *);
27 static void rma(char *);
28
29
30 int
31 main(int argc, char **argv)
32 {
33         int defolder = 0, interactive = -1;
34         char *cp, *folder = NULL, newfolder[BUFSIZ];
35         char buf[BUFSIZ], **argp, **arguments;
36
37 #ifdef LOCALE
38         setlocale(LC_ALL, "");
39 #endif
40         invo_name = mhbasename(argv[0]);
41
42         /* read user profile/context */
43         context_read();
44
45         arguments = getarguments(invo_name, argc, argv, 1);
46         argp = arguments;
47
48         while ((cp = *argp++)) {
49                 if (*cp == '-') {
50                         switch (smatch(++cp, switches)) {
51                                 case AMBIGSW:
52                                         ambigsw(cp, switches);
53                                         done(1);
54                                 case UNKWNSW:
55                                         adios(NULL, "-%s unknown", cp);
56
57                                 case HELPSW:
58                                         snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
59                                         print_help(buf, switches, 1);
60                                         done(1);
61                                 case VERSIONSW:
62                                         print_version(invo_name);
63                                         done(1);
64
65                                 case INTRSW:
66                                         interactive = 1;
67                                         continue;
68                                 case NINTRSW:
69                                         interactive = 0;
70                                         continue;
71                         }
72                 }
73                 if (*cp == '+' || *cp == '@') {
74                         if (folder)
75                                 adios(NULL, "only one folder at a time!");
76                         else
77                                 folder = getcpy(expandfol(cp));
78                 } else {
79                         adios(NULL, "usage: %s [+folder] [switches]",
80                                         invo_name);
81                 }
82         }
83
84         if (!folder) {
85                 folder = getcurfol();
86                 defolder++;
87         }
88         if (strcmp(toabsdir(folder), pwd()) == 0)
89                 adios(NULL, "You can't remove the current working directory");
90
91         if (interactive == -1)
92                 interactive = defolder;
93
94         if (strchr(folder, '/') && (*folder != '/') && (*folder != '.')) {
95                 strcpy(newfolder, folder);
96                 cp = newfolder + strlen(newfolder);
97                 while (cp > newfolder && *cp != '/')
98                         cp--;
99                 if (cp > newfolder)
100                         *cp = '\0';
101                 else
102                         strncpy(newfolder, getdeffol(), sizeof(newfolder));
103         } else {
104                 strncpy(newfolder, getdeffol(), sizeof(newfolder));
105         }
106
107         if (interactive) {
108                 cp = concat("Remove folder \"", folder, "\"? ", NULL);
109                 if (!getanswer(cp))
110                         done(0);
111                 free(cp);
112         }
113
114         if (rmf(folder) == OK) {
115                 char *cfolder = context_find(curfolder);
116                 if (cfolder && strcmp(cfolder, newfolder)) {
117                         printf("[+%s now current]\n", newfolder);
118                         /* update current folder */
119                         context_replace(curfolder, newfolder);
120                 }
121         }
122         context_save();  /* save the context file */
123         done(0);
124         return 1;
125 }
126
127 static int
128 rmf(char *folder)
129 {
130         int i, j, others;
131         register char *maildir;
132         char cur[BUFSIZ];
133         register struct dirent *dp;
134         register DIR *dd;
135
136         switch (i = chdir(maildir = toabsdir(folder))) {
137                 case OK:
138                         if (access(".", W_OK) != NOTOK &&
139                                         access("..", W_OK) != NOTOK)
140                                 break;  /* fall otherwise */
141
142                 case NOTOK:
143                         snprintf(cur, sizeof(cur), "atr-%s-%s",
144                                                 current, toabsdir(folder));
145                         if (!context_del(cur)) {
146                                 printf("[+%s de-referenced]\n", folder);
147                                 return OK;
148                         }
149                         advise(NULL, "you have no profile entry for the %s folder +%s", i == NOTOK ? "unreadable" : "read-only", folder);
150                         return NOTOK;
151         }
152
153         if ((dd = opendir(".")) == NULL)
154                 adios(NULL, "unable to read folder +%s", folder);
155         others = 0;
156
157         /*
158         ** Run the external delete hook program.
159         */
160
161         ext_hook("del-hook", maildir, (char *)0);
162
163         j = strlen(backup_prefix);
164         while ((dp = readdir(dd))) {
165                 switch (dp->d_name[0]) {
166                         case '.':
167                                 if (strcmp(dp->d_name, ".") == 0 ||
168                                                 strcmp(dp->d_name, "..") == 0)
169                                         continue;  /* else fall */
170
171                         case ',':
172 #ifdef MHE
173                         case '+':
174 #endif /* MHE */
175 #ifdef UCI
176                         case '_':
177                         case '#':
178 #endif /* UCI */
179                                 break;
180
181                         default:
182                                 if (m_atoi(dp->d_name))
183                                         break;
184                                 if (strcmp(dp->d_name, altmsglink) == 0 ||
185                                                 strncmp(dp->d_name,
186                                                 backup_prefix, j) == 0)
187                                         break;
188
189                                 admonish(NULL, "file \"%s/%s\" not deleted",
190                                                 folder, dp->d_name);
191                                 others++;
192                                 continue;
193                 }
194                 if (unlink(dp->d_name) == NOTOK) {
195                         admonish(dp->d_name, "unable to unlink %s:", folder);
196                         others++;
197                 }
198         }
199
200         closedir(dd);
201
202         /*
203         ** Remove any relevant private sequences
204         ** or attributes from context file.
205         */
206         rma(folder);
207
208         chdir("..");
209         if (others == 0) {
210                 context_save();  /* Is this needed? meillo 2011-10 */
211                 fflush(stdout);  /* Is this needed? meillo 2011-10 */
212                 if (rmdir(maildir) != -1) {
213                         return OK;
214                 }
215                 admonish(maildir, "unable to remove directory");
216         }
217
218         advise(NULL, "folder +%s not removed", folder);
219         return NOTOK;
220 }
221
222
223 /*
224 ** Remove all the (private) sequence information for
225 ** this folder from the profile/context list.
226 */
227
228 static void
229 rma(char *folder)
230 {
231         register int alen, j, plen;
232         register char *cp;
233         register struct node *np, *pp;
234
235         alen = strlen("atr-");
236         plen = strlen(cp = getcpy(toabsdir(folder))) + 1;
237
238         /*
239         ** Search context list for keys that look like
240         ** "atr-something-folderpath", and remove them.
241         */
242         for (np = m_defs, pp = NULL; np; np = np->n_next) {
243                 if (isprefix("atr-", np->n_name) &&
244                                 (j = strlen(np->n_name) - plen) > alen &&
245                                 *(np->n_name + j) == '-' &&
246                                 strcmp(cp, np->n_name + j + 1) == 0) {
247                         if (!np->n_context)
248                                 admonish(NULL, "bug: context_del(key=\"%s\")",
249                                                 np->n_name);
250                         if (pp) {
251                                 pp->n_next = np->n_next;
252                                 np = pp;
253                         } else {
254                                 m_defs = np->n_next;
255                         }
256                         ctxflags |= CTXMOD;
257                 } else {
258                         pp = np;
259                 }
260         }
261         free(cp);
262 }