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