85de619db67ea68e743630227ea737377a4df0d2
[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 <h/mts.h>
14
15 static struct swit switches[] = {
16 #define VERSIONSW  0
17         { "version", 0 },
18 #define HELPSW  1
19         { "help", 0 },
20         { NULL, 0 }
21 };
22
23
24 int
25 main(int argc, char **argv)
26 {
27         int md;
28         char *cp, *file = NULL, buf[BUFSIZ];
29         char **argp, **arguments;
30
31 #ifdef LOCALE
32         setlocale(LC_ALL, "");
33 #endif
34         invo_name = mhbasename(argv[0]);
35
36         /* read user profile/context */
37         context_read();
38
39         mts_init(invo_name);
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                                 done(1);
50                         case UNKWNSW:
51                                 adios(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                                 done(1);
59                         case VERSIONSW:
60                                 print_version(invo_name);
61                                 done(1);
62                         }
63                 }
64                 if (file)
65                         adios(NULL, "only one file at a time!");
66                 else
67                         file = cp;
68         }
69
70         if (!file)
71                 adios(NULL, "%s [switches] file", invo_name);
72
73         rewind(stdin);
74
75         /* open and lock the file */
76         if ((md = mbx_open(file, getuid(), getgid(), m_gmprot()))
77                         == NOTOK)
78                 done(RCV_MBX);
79
80         /* append the message */
81         if (mbx_copy(file, md, fileno(stdin), 0) == NOTOK) {
82                 mbx_close(file, md);
83                 done(RCV_MBX);
84         }
85
86         /* close and unlock the file */
87         if (mbx_close(file, md) == NOTOK)
88                 done(RCV_MBX);
89
90         done(RCV_MOK);
91         return 1;
92 }