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