Fix uip/whom.c for C89 compatibility
[mmh] / uip / rcvpack.c
1 /*
2 ** rcvpack.c -- append message to 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 <h/dropsbr.h>
11 #include <h/rcvmail.h>
12 #include <h/tws.h>
13 #include <unistd.h>
14 #include <locale.h>
15 #include <sysexits.h>
16
17 static struct swit switches[] = {
18 #define VERSIONSW  0
19         { "Version", 0 },
20 #define HELPSW  1
21         { "help", 0 },
22         { NULL, 0 }
23 };
24
25 char *version=VERSION;
26
27 int
28 main(int argc, char **argv)
29 {
30         int md;
31         char *cp, *file = NULL, buf[BUFSIZ];
32         char **argp, **arguments;
33
34         setlocale(LC_ALL, "");
35         invo_name = mhbasename(argv[0]);
36
37         /* read user profile/context */
38         context_read();
39
40         arguments = getarguments(invo_name, argc, argv, 1);
41         argp = arguments;
42
43         /* parse arguments */
44         while ((cp = *argp++)) {
45                 if (*cp == '-') {
46                         switch (smatch(++cp, switches)) {
47                         case AMBIGSW:
48                                 ambigsw(cp, switches);
49                                 exit(EX_USAGE);
50                         case UNKWNSW:
51                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
52
53                         case HELPSW:
54                                 snprintf(buf, sizeof(buf),
55                                                 "%s [switches] file",
56                                                 invo_name);
57                                 print_help(buf, switches, 1);
58                                 exit(argc == 2 ? EX_OK : EX_USAGE);
59                         case VERSIONSW:
60                                 print_version(invo_name);
61                                 exit(argc == 2 ? EX_OK : EX_USAGE);
62                         }
63                 }
64                 if (file)
65                         adios(EX_USAGE, NULL, "only one file at a time!");
66                 else
67                         file = cp;
68         }
69
70         /* copy stdin to stdout, converting rfc822 message to mbox */
71         if (!file) {
72                 if (mbox_copy(fileno(stdout), fileno(stdin)) == NOTOK) {
73                         exit(RCV_MBX);
74                 }
75                 exit(RCV_MOK);
76                 return 1;
77         }
78
79         /* open and lock the file */
80         if ((md = mbox_open(file, getuid(), getgid(), m_gmprot()))
81                         == NOTOK)
82                 exit(RCV_MBX);
83
84         /* append the message */
85         if (mbox_copy(md, fileno(stdin)) == NOTOK) {
86                 mbox_close(file, md);
87                 exit(RCV_MBX);
88         }
89
90         /* close and unlock the file */
91         if (mbox_close(file, md) == NOTOK)
92                 exit(RCV_MBX);
93
94         exit(RCV_MOK);
95         return 1;
96 }