]> diplodocus.org Git - nmh/blob - uip/rcvpack.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[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 #include <h/utils.h>
14
15 #define RCVPACK_SWITCHES \
16 X("mbox", 0, MBOXSW) \
17 X("mmdf", 0, MMDFSW) \
18 X("version", 0, VERSIONSW) \
19 X("help", 0, HELPSW) \
20
21 #define X(sw, minchars, id) id,
22 DEFINE_SWITCH_ENUM(RCVPACK);
23 #undef X
24
25 #define X(sw, minchars, id) { sw, minchars, id },
26 DEFINE_SWITCH_ARRAY(RCVPACK, switches);
27 #undef X
28
29 /*
30 * default format in which to save messages
31 */
32 static int mbx_style = MBOX_FORMAT;
33
34
35 int
36 main (int argc, char **argv)
37 {
38 int md;
39 char *cp, *file = NULL, buf[BUFSIZ];
40 char **argp, **arguments;
41
42 if (nmh_init(argv[0], 2)) { return 1; }
43
44 mts_init ();
45 arguments = getarguments (invo_name, argc, argv, 1);
46 argp = arguments;
47
48 /* parse arguments */
49 while ((cp = *argp++)) {
50 if (*cp == '-') {
51 switch (smatch (++cp, switches)) {
52 case AMBIGSW:
53 ambigsw (cp, switches);
54 done (1);
55 case UNKWNSW:
56 adios (NULL, "-%s unknown", cp);
57
58 case HELPSW:
59 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
60 print_help (buf, switches, 1);
61 done (0);
62 case VERSIONSW:
63 print_version(invo_name);
64 done (0);
65
66 case MBOXSW:
67 mbx_style = MBOX_FORMAT;
68 continue;
69 case MMDFSW:
70 mbx_style = MMDF_FORMAT;
71 continue;
72 }
73 }
74 if (file)
75 adios (NULL, "only one file at a time!");
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), NULL) == 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 }