#include #include #include extern const char *__progname; static const char *pfx; static int printnames = 0; static char *fn; static char *fnp; static int pno; static FILE *pf; static const char sep[] = "\n\nFrom "; static unsigned char adv[sizeof(sep)-1][256]; static int sepx; static const char newpref[] = "From "; static void usage(void) { fprintf(stderr,"Usage: %s [-name] file-prefix\n",__progname); fprintf(stderr,"(mbox file taken from stdin)\n"); fprintf(stderr,"-name print names of parts to stdout\n"); } static void handleargs(int ac, char **av) { while (ac > 1) { if (!strcmp(av[1],"-name")) { printnames = 1; ac --; av ++; continue; } break; } if (ac != 2) { usage(); exit(1); } pfx = av[1]; } static void setup(void) { int pl; int i; int j; int k; pl = strlen(pfx); fn = malloc(pl+64); bcopy(pfx,fn,pl); fnp = fn + pl; pno = 1; pf = 0; sepx = 0; for (i=0;sep[i];i++) { for <"j"> (j=0;j<256;j++) { for (k=0;k; } } adv[i][j] = (j == sep[0]) ? i : (i+1); } } } static void endp(void) { if (pf) fclose(pf); pf = 0; pno ++; sepx = 0; } static void openp(void) { if (pf) return; sprintf(fnp,"%d",pno); pf = fopen(fn,"w"); if (pf == 0) { fprintf(stderr,"%s: can't open part %s: %s\n",__progname,fn,__progname); exit(1); } if (printnames) printf("%s\n",fn); } static void pwrite(const void *buf, int len) { openp(); fwrite(buf,1,len,pf); } static void pput(unsigned char c) { openp(); putc(c,pf); } static void nextp(void) { endp(); pwrite(&newpref[0],strlen(&newpref[0])); } int main(int, char **); int main(int ac, char **av) { int a; handleargs(ac,av); setup(); while (1) { int c; c = getchar(); if (c == EOF) { if (sepx > 0) pwrite(&sep[0],sepx); break; } a = adv[sepx][c]; if (a < 1) { sepx ++; if (! sep[sepx]) nextp(); } else if (a <= sepx) { pwrite(&sep[0],a); sepx -= a - 1; } else { if (sepx > 0) pwrite(&sep[0],sepx); pput(c); sepx = 0; } } endp(); exit(0); }