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