]> diplodocus.org Git - nmh/blob - uip/rcvpack.c
f63a89d066fb034146074d477ea2e35bbb8b1f9f
[nmh] / uip / rcvpack.c
1 /* rcvpack.c -- append message to a file
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/error.h"
10 #include "h/dropsbr.h"
11 #include "h/rcvmail.h"
12 #include "h/tws.h"
13 #include "h/mts.h"
14 #include "h/done.h"
15 #include "h/utils.h"
16
17 #define RCVPACK_SWITCHES \
18 X("mbox", 0, MBOXSW) \
19 X("mmdf", 0, MMDFSW) \
20 X("version", 0, VERSIONSW) \
21 X("help", 0, HELPSW) \
22
23 #define X(sw, minchars, id) id,
24 DEFINE_SWITCH_ENUM(RCVPACK);
25 #undef X
26
27 #define X(sw, minchars, id) { sw, minchars, id },
28 DEFINE_SWITCH_ARRAY(RCVPACK, switches);
29 #undef X
30
31 /*
32 * default format in which to save messages
33 */
34 static int mbx_style = MBOX_FORMAT;
35
36
37 int
38 main (int argc, char **argv)
39 {
40 int md;
41 char *cp, *file = NULL, buf[BUFSIZ];
42 char **argp, **arguments;
43
44 if (nmh_init(argv[0], true, false)) { return 1; }
45
46 mts_init ();
47 arguments = getarguments (invo_name, argc, argv, 1);
48 argp = arguments;
49
50 /* parse arguments */
51 while ((cp = *argp++)) {
52 if (*cp == '-') {
53 switch (smatch (++cp, switches)) {
54 case AMBIGSW:
55 ambigsw (cp, switches);
56 done (1);
57 case UNKWNSW:
58 die("-%s unknown", cp);
59
60 case HELPSW:
61 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
62 print_help (buf, switches, 1);
63 done (0);
64 case VERSIONSW:
65 print_version(invo_name);
66 done (0);
67
68 case MBOXSW:
69 mbx_style = MBOX_FORMAT;
70 continue;
71 case MMDFSW:
72 mbx_style = MMDF_FORMAT;
73 continue;
74 }
75 }
76 if (file)
77 die("only one file at a time!");
78 file = cp;
79 }
80
81 if (!file)
82 die("%s [switches] file", invo_name);
83
84 rewind (stdin);
85
86 /* open and lock the file */
87 if ((md = mbx_open (file, mbx_style, getuid(), getgid(), m_gmprot())) == NOTOK)
88 done (RCV_MBX);
89
90 /* append the message */
91 if (mbx_copy (file, mbx_style, md, fileno(stdin), NULL) == NOTOK) {
92 mbx_close (file, md);
93 done (RCV_MBX);
94 }
95
96 /* close and unlock the file */
97 if (mbx_close (file, md) == NOTOK)
98 done (RCV_MBX);
99
100 done (RCV_MOK);
101 return 1;
102 }