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