Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / packf.c
1 /* packf.c - pack a folder (used to be called "pack") */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: packf.c,v 1.4 1992/12/15 00:20:22 jromine Exp $";
4 #endif  /*  lint */
5
6 #include "../h/mh.h"
7 #include "../h/dropsbr.h"
8 #include <errno.h>
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #ifdef LOCALE
13 #include        <locale.h>
14 #endif
15
16 /* \f */
17
18 static struct swit switches[] = {
19 #define FILESW  0
20     "file name", 0,
21
22 #define HELPSW  1
23     "help", 4,
24
25     NULL, 0
26 };
27
28 /* \f */
29
30 extern int errno;
31
32
33 static int  md = NOTOK;
34
35 char   *file = NULL;
36
37 /* \f */
38
39 /* ARGSUSED */
40
41 main (argc, argv)
42 int     argc;
43 char  **argv;
44 {
45     int     msgp = 0,
46             fd,
47             msgnum;
48     char   *cp,
49            *maildir,
50            *msgnam,
51            *folder = NULL,
52             buf[100],
53           **ap,
54           **argp,
55            *arguments[MAXARGS],
56            *msgs[MAXARGS];
57     struct msgs *mp;
58     struct stat st;
59
60 #ifdef LOCALE
61         setlocale(LC_ALL, "");
62 #endif
63     invo_name = r1bindex (argv[0], '/');
64     if ((cp = m_find (invo_name)) != NULL) {
65         ap = brkstring (cp = getcpy (cp), " ", "\n");
66         ap = copyip (ap, arguments);
67     }
68     else
69         ap = arguments;
70     (void) copyip (argv + 1, ap);
71     argp = arguments;
72
73 /* \f */
74
75     while (cp = *argp++) {
76         if (*cp == '-')
77             switch (smatch (++cp, switches)) {
78                 case AMBIGSW: 
79                     ambigsw (cp, switches);
80                     done (1);
81                 case UNKWNSW: 
82                     adios (NULLCP, "-%s unknown", cp);
83                 case HELPSW: 
84                     (void) sprintf (buf, "%s [+folder] [msgs] [switches]",
85                             invo_name);
86                     help (buf, switches);
87                     done (1);
88
89                 case FILESW: 
90                     if (file)
91                         adios (NULLCP, "only one file at a time!");
92                     if (!(file = *argp++) || *file == '-')
93                         adios (NULLCP, "missing argument to %s", argp[-2]);
94                     continue;
95             }
96         if (*cp == '+' || *cp == '@') {
97             if (folder)
98                 adios (NULLCP, "only one folder at a time!");
99             folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
100         }
101         else
102             msgs[msgp++] = cp;
103     }
104
105 /* \f */
106
107     if (!file)
108         file = "./msgbox";
109     file = path (file, TFILE);
110     if (stat (file, &st) == NOTOK) {
111         if (errno != ENOENT)
112             adios (file, "error on file");
113         cp = concat ("Create file \"", file, "\"? ", NULLCP);
114         if (!getanswer (cp))
115             done (1);
116         free (cp);
117     }
118
119     if (!m_find ("path"))
120         free (path ("./", TFOLDER));
121     if (!msgp)
122         msgs[msgp++] = "all";
123     if (!folder)
124         folder = m_getfolder ();
125     maildir = m_maildir (folder);
126
127     if (chdir (maildir) == NOTOK)
128         adios (maildir, "unable to change directory to ");
129     if (!(mp = m_gmsg (folder)))
130         adios (NULLCP, "unable to read folder %s", folder);
131     if (mp -> hghmsg == 0)
132         adios (NULLCP, "no messages in %s", folder);
133
134     for (msgnum = 0; msgnum < msgp; msgnum++)
135         if (!m_convert (mp, msgs[msgnum]))
136             done (1);
137     m_setseq (mp);
138
139     if ((md = mbx_open (file, getuid (), getgid (), m_gmprot ())) == NOTOK)
140         adios (file, "unable to open");
141
142     for (msgnum = mp -> lowsel; msgnum <= mp -> hghsel; msgnum++)
143         if (mp -> msgstats[msgnum] & SELECTED) {
144             if ((fd = open (msgnam = m_name (msgnum), 0)) == NOTOK) {
145                 admonish (msgnam, "unable to read message");
146                 break;
147             }
148
149             if (mbx_copy (file, md, fd, 1, NULLCP, 1) == NOTOK)
150                 adios (file, "error writing to file");
151
152             (void) close (fd);
153         }
154     (void) mbx_close (file, md);
155
156     m_replace (pfolder, folder);
157     if (mp -> hghsel != mp -> curmsg)
158         m_setcur (mp, mp -> lowsel);
159     m_sync (mp);
160     m_update ();
161
162     done (0);
163 }
164
165 /* \f */
166
167 void done (status)
168 int status;
169 {
170     (void) mbx_close (file, md);
171
172     exit (status);
173 }