Merged h/mts.h into h/prototypes.h.
[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
14 static struct swit switches[] = {
15 #define VERSIONSW  0
16         { "version", 0 },
17 #define HELPSW  1
18         { "help", 0 },
19         { NULL, 0 }
20 };
21
22
23 int
24 main(int argc, char **argv)
25 {
26         int md;
27         char *cp, *file = NULL, buf[BUFSIZ];
28         char **argp, **arguments;
29
30 #ifdef LOCALE
31         setlocale(LC_ALL, "");
32 #endif
33         invo_name = mhbasename(argv[0]);
34
35         /* read user profile/context */
36         context_read();
37
38         arguments = getarguments(invo_name, argc, argv, 1);
39         argp = arguments;
40
41         /* parse arguments */
42         while ((cp = *argp++)) {
43                 if (*cp == '-') {
44                         switch (smatch(++cp, switches)) {
45                         case AMBIGSW:
46                                 ambigsw(cp, switches);
47                                 done(1);
48                         case UNKWNSW:
49                                 adios(NULL, "-%s unknown", cp);
50
51                         case HELPSW:
52                                 snprintf(buf, sizeof(buf),
53                                                 "%s [switches] file",
54                                                 invo_name);
55                                 print_help(buf, switches, 1);
56                                 done(1);
57                         case VERSIONSW:
58                                 print_version(invo_name);
59                                 done(1);
60                         }
61                 }
62                 if (file)
63                         adios(NULL, "only one file at a time!");
64                 else
65                         file = cp;
66         }
67
68         if (!file)
69                 adios(NULL, "%s [switches] file", invo_name);
70
71         rewind(stdin);
72
73         /* open and lock the file */
74         if ((md = mbx_open(file, getuid(), getgid(), m_gmprot()))
75                         == NOTOK)
76                 done(RCV_MBX);
77
78         /* append the message */
79         if (mbx_copy(file, md, fileno(stdin), 0) == NOTOK) {
80                 mbx_close(file, md);
81                 done(RCV_MBX);
82         }
83
84         /* close and unlock the file */
85         if (mbx_close(file, md) == NOTOK)
86                 done(RCV_MBX);
87
88         done(RCV_MOK);
89         return 1;
90 }