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