/* $Id: split_unrmailed_RMAIL.c 6 2006-03-13 02:00:20Z flaterco $ */ /* Turn the results of the Emacs command unrmail into individual messages, undoing RMAIL damage to From lines along the way. */ #include #include #include #include #include #include #define bufsize 10000 FILE *newmsg (char *dest_dir) { static unsigned long serialnum = 0; size_t l; char msgfilename[bufsize]; FILE *fp; struct stat stat_struct; l = strlen(dest_dir); assert (l + 80 < bufsize); strcpy (msgfilename, dest_dir); if (msgfilename[l-1] != '/') msgfilename[l++] = '/'; do sprintf (msgfilename+l, "msg%lu", serialnum++); while (stat (msgfilename, &stat_struct) == 0); if (!(fp = fopen (msgfilename, "w"))) { perror (msgfilename); fprintf (stderr, "Aborting run\n"); exit (-1); } return fp; } int main (int argc, char **argv) { FILE *mbox, *msg=NULL; char inbuf[bufsize]; if (argc != 3) { fprintf (stderr, "Usage: split_unrmailed_RMAIL mailbox-filename destination-directory\n"); exit (-1); } if (!(mbox = fopen (argv[1], "r"))) { perror (argv[1]); exit (-1); } while (fgets (inbuf, bufsize, mbox)) { if (!(strncmp (inbuf, "From ", 5))) { if (msg) fclose (msg); msg = newmsg (argv[2]); /* Hack to undo the RMAIL mangling of From line */ /* (just throw out the coding system line) */ assert ((fgets (inbuf, bufsize, mbox))); assert (!(strncmp (inbuf, "X-Coding-System: ", 17))); assert ((fgets (inbuf, bufsize, mbox))); assert (!(strncmp (inbuf, "Mail-from: ", 11))); strcpy (inbuf, inbuf+11); } fprintf (msg, "%s", inbuf); } fclose (mbox); if (msg) fclose (msg); exit (0); }