]> diplodocus.org Git - nmh/blob - uip/rcvpack.c
Fix stupid accidental dependence on a bash quirk in previous
[nmh] / uip / rcvpack.c
1
2 /*
3 * rcvpack.c -- append message to a file
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/dropsbr.h>
14 #include <h/rcvmail.h>
15 #include <h/tws.h>
16 #include <h/mts.h>
17
18 static struct swit switches[] = {
19 #define MBOXSW 0
20 { "mbox", 0 },
21 #define MMDFSW 1
22 { "mmdf", 0 },
23 #define VERSIONSW 2
24 { "version", 0 },
25 #define HELPSW 3
26 { "help", 0 },
27 { NULL, 0 }
28 };
29
30 /*
31 * default format in which to save messages
32 */
33 static int mbx_style = MBOX_FORMAT;
34
35
36 int
37 main (int argc, char **argv)
38 {
39 int md;
40 char *cp, *file = NULL, buf[BUFSIZ];
41 char **argp, **arguments;
42
43 #ifdef LOCALE
44 setlocale(LC_ALL, "");
45 #endif
46 invo_name = r1bindex (argv[0], '/');
47
48 /* read user profile/context */
49 context_read();
50
51 mts_init (invo_name);
52 arguments = getarguments (invo_name, argc, argv, 1);
53 argp = arguments;
54
55 /* parse arguments */
56 while ((cp = *argp++)) {
57 if (*cp == '-') {
58 switch (smatch (++cp, switches)) {
59 case AMBIGSW:
60 ambigsw (cp, switches);
61 done (1);
62 case UNKWNSW:
63 adios (NULL, "-%s unknown", cp);
64
65 case HELPSW:
66 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
67 print_help (buf, switches, 1);
68 done (1);
69 case VERSIONSW:
70 print_version(invo_name);
71 done (1);
72
73 case MBOXSW:
74 mbx_style = MBOX_FORMAT;
75 continue;
76 case MMDFSW:
77 mbx_style = MMDF_FORMAT;
78 continue;
79 }
80 }
81 if (file)
82 adios (NULL, "only one file at a time!");
83 else
84 file = cp;
85 }
86
87 if (!file)
88 adios (NULL, "%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), 1, NULL, 0) == 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 return done (RCV_MOK);
107 }