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