If rcvpack missed file argument, it prints to stdout; Removed unused code.
[mmh] / uip / packf.c
1 /*
2 ** packf.c -- pack a nmh folder into a file
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 #include <fcntl.h>
11 #include <h/dropsbr.h>
12 #include <h/utils.h>
13 #include <errno.h>
14
15 static struct swit switches[] = {
16 #define FILESW  0
17         { "file name", 0 },
18 #define VERSIONSW  1
19         { "version", 0 },
20 #define HELPSW  2
21         { "help", 0 },
22         { NULL, 0 }
23 };
24
25 static int md = NOTOK;
26
27 static void mbxclose_done(int) NORETURN;
28
29 char *file = NULL;
30
31
32 int
33 main(int argc, char **argv)
34 {
35         int fd, msgnum;
36         char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
37         char **argp, **arguments;
38         struct msgs_array msgs = { 0, 0, NULL };
39         struct msgs *mp;
40         struct stat st;
41
42         done=mbxclose_done;
43
44 #ifdef LOCALE
45         setlocale(LC_ALL, "");
46 #endif
47         invo_name = mhbasename(argv[0]);
48
49         /* read user profile/context */
50         context_read();
51
52         arguments = getarguments(invo_name, argc, argv, 1);
53         argp = arguments;
54
55         /*
56         ** Parse arguments
57         */
58         while ((cp = *argp++)) {
59                 if (*cp == '-') {
60                         switch (smatch(++cp, switches)) {
61                         case AMBIGSW:
62                                 ambigsw(cp, switches);
63                                 done(1);
64                         case UNKWNSW:
65                                 adios(NULL, "-%s unknown", cp);
66
67                         case HELPSW:
68                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
69                                 print_help(buf, switches, 1);
70                                 done(1);
71                         case VERSIONSW:
72                                 print_version(invo_name);
73                                 done(1);
74
75                         case FILESW:
76                                 if (file)
77                                         adios(NULL, "only one file at a time!");
78                                 if (!(file = *argp++) || *file == '-')
79                                         adios(NULL, "missing argument to %s",
80                                                         argp[-2]);
81                                 continue;
82                         }
83                 }
84                 if (*cp == '+' || *cp == '@') {
85                         if (folder)
86                                 adios(NULL, "only one folder at a time!");
87                         folder = getcpy(expandfol(cp));
88                 } else
89                         app_msgarg(&msgs, cp);
90         }
91
92         if (!file)
93                 file = "./msgbox";
94         file = getcpy(expanddir(file));
95
96         /*
97         ** Check if file to be created (or appended to)
98         ** exists.  If not, ask for confirmation.
99         */
100         if (stat (file, &st) == NOTOK) {
101                 if (errno != ENOENT)
102                         adios(file, "error on file");
103                 cp = concat("Create file \"", file, "\"? ", NULL);
104                 if (!getanswer(cp))
105                         done(1);
106                 free(cp);
107         }
108
109         /* default is to pack whole folder */
110         if (!msgs.size)
111                 app_msgarg(&msgs, seq_all);
112
113         if (!folder)
114                 folder = getcurfol();
115         maildir = toabsdir(folder);
116
117         if (chdir(maildir) == NOTOK)
118                 adios(maildir, "unable to change directory to ");
119
120         /* read folder and create message structure */
121         if (!(mp = folder_read(folder)))
122                 adios(NULL, "unable to read folder %s", folder);
123
124         /* check for empty folder */
125         if (mp->nummsg == 0)
126                 adios(NULL, "no messages in %s", folder);
127
128         /* parse all the message ranges/sequences and set SELECTED */
129         for (msgnum = 0; msgnum < msgs.size; msgnum++)
130                 if (!m_convert(mp, msgs.msgs[msgnum]))
131                         done(1);
132         seq_setprev(mp);  /* set the previous-sequence */
133
134         /* open and lock new maildrop file */
135         if ((md = mbx_open(file, getuid(), getgid(), m_gmprot()))
136                         == NOTOK)
137                 adios(file, "unable to open");
138
139         /* copy all the SELECTED messages to the file */
140         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
141                 if (is_selected(mp, msgnum)) {
142                         if ((fd = open(msgnam = m_name(msgnum), O_RDONLY))
143                                         == NOTOK) {
144                                 admonish(msgnam, "unable to read message");
145                                 break;
146                         }
147
148                         if (mbx_copy(md, fd) == NOTOK)
149                                 adios(file, "error writing to file");
150
151                         close(fd);
152                 }
153
154         /* close and unlock maildrop file */
155         mbx_close(file, md);
156
157         context_replace(curfolder, folder);  /* update current folder */
158         if (mp->hghsel != mp->curmsg)
159                 seq_setcur(mp, mp->lowsel);
160         seq_save(mp);
161         context_save();  /* save the context file */
162         folder_free(mp);  /* free folder/message structure */
163         done(0);
164         return 1;
165 }
166
167 static void
168 mbxclose_done(int status)
169 {
170         mbx_close(file, md);
171         exit(status);
172 }