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