Rearranged whitespace (and comments) in all the code!
[mmh] / uip / rcvpack.c
1 /*
2  * rcvpack.c -- append message to a file
3  *
4  * This code is Copyright (c) 2002, by the authors of nmh.  See the
5  * COPYRIGHT file in the root directory of the nmh distribution for
6  * complete copyright information.
7  */
8
9 #include <h/mh.h>
10 #include <h/dropsbr.h>
11 #include <h/rcvmail.h>
12 #include <h/tws.h>
13 #include <h/mts.h>
14
15 static struct swit switches[] = {
16 #define MBOXSW  0
17         { "mbox", 0 },
18 #define MMDFSW  1
19         { "mmdf", 0 },
20 #define VERSIONSW  2
21         { "version", 0 },
22 #define HELPSW  3
23         { "help", 0 },
24         { NULL, 0 }
25 };
26
27 /*
28  * default format in which to save messages
29  */
30 static int mbx_style = MBOX_FORMAT;
31
32
33 int
34 main (int argc, char **argv)
35 {
36         int md;
37         char *cp, *file = NULL, buf[BUFSIZ];
38         char **argp, **arguments;
39
40 #ifdef LOCALE
41         setlocale(LC_ALL, "");
42 #endif
43         invo_name = r1bindex (argv[0], '/');
44
45         /* read user profile/context */
46         context_read();
47
48         mts_init (invo_name);
49         arguments = getarguments (invo_name, argc, argv, 1);
50         argp = arguments;
51
52         /* parse arguments */
53         while ((cp = *argp++)) {
54                 if (*cp == '-') {
55                         switch (smatch (++cp, switches)) {
56                                 case AMBIGSW:
57                                         ambigsw (cp, switches);
58                                         done (1);
59                                 case UNKWNSW:
60                                         adios (NULL, "-%s unknown", cp);
61
62                                 case HELPSW:
63                                         snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
64                                         print_help (buf, switches, 1);
65                                         done (1);
66                                 case VERSIONSW:
67                                         print_version(invo_name);
68                                         done (1);
69
70                                 case MBOXSW:
71                                         mbx_style = MBOX_FORMAT;
72                                         continue;
73                                 case MMDFSW:
74                                         mbx_style = MMDF_FORMAT;
75                                         continue;
76                         }
77                 }
78                 if (file)
79                         adios (NULL, "only one file at a time!");
80                 else
81                         file = cp;
82         }
83
84         if (!file)
85                 adios (NULL, "%s [switches] file", invo_name);
86
87         rewind (stdin);
88
89         /* open and lock the file */
90         if ((md = mbx_open (file, mbx_style, getuid(), getgid(), m_gmprot())) == NOTOK)
91                 done (RCV_MBX);
92
93         /* append the message */
94         if (mbx_copy (file, mbx_style, md, fileno(stdin), 1, NULL, 0) == NOTOK) {
95                 mbx_close (file, md);
96                 done (RCV_MBX);
97         }
98
99         /* close and unlock the file */
100         if (mbx_close (file, md) == NOTOK)
101                 done (RCV_MBX);
102
103         done (RCV_MOK);
104         return 1;
105 }