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