1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
/*
Split a tth-produced MIME file into its consituent files.
Copyright 2004 I.H.Hutchinson.
*/
#define LINELEN 256
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(argc,argv)
int argc;
char *argv[];
{
/* int slen; */
char *ch,*ch2;
char bound[LINELEN]={0};
char buff[LINELEN]={0};
FILE *file;
if(argc > 1){
printf( "Usage: tthsplit <infile \n Split a tth MIME file on stdin.\n");
return 1;
}
do {
ch2=fgets(buff,LINELEN,stdin);
if((ch=strstr(buff,"MULTIPART/MIXED; BOUNDARY="))!=NULL){
ch=strstr(ch,"\"")+1;
*(strstr(ch,"\""))=0;
strcpy(bound,"--");
strcat(bound,ch);
}
} while (ch2 != NULL && *buff != 13 && *buff != 10); /* header lines */
if(!*(bound)){
fprintf(stderr,"File does not start with MIME boundary definition\n");
exit(2);
}
file=NULL;
while(fgets(buff,LINELEN,stdin) != NULL){
if(strstr(buff,bound)==buff){
/*printf("Found bound. file=%d\n",(int)file); */
fgets(buff,LINELEN,stdin);
if(file!=NULL) fclose(file);
if((ch=strstr(buff,"name=\"")) != NULL){
ch=ch+6;
*(strstr(ch,"\""))=0;
if((file=fopen(ch,"w"))!=NULL){
fprintf(stderr,"%s\n",ch);
}else{
fprintf(stderr,"Can't open %s\n",ch);
return 2;
}
}else{
fprintf(stderr,"Can't find file name in:\n%s",buff);
return 3;
}
} else fputs(buff,file);
}
fclose(file);
return EXIT_SUCCESS;
}
|