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