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