summaryrefslogtreecommitdiff
path: root/mb.c
blob: 5d3cb5fbce4b359e5c506a1ab564eb03bd98242f (plain)
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
63
64
65
66
67
68
69
70
/* Mangle a berkley mbox
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <unistd.h>
#include <sys/stat.h>

void	Process(FILE *fp, const char *p);

int main(int argc, char *argv[])
{
    int f;

    if (argc==1)
	Process(stdin,"stdin");
    else
	for(f=1;f<argc;f++)
	    Process(fopen(argv[f],"r"),argv[f]);

    return EXIT_SUCCESS;
}


void Quit(const char *p)
{
    perror(p);
    exit(EXIT_FAILURE);
}


void Process(FILE *fp, const char *p)
{
    FILE *out=NULL;
    char buff[1024];
    char fn[80];
    int n=1;

    printf("Processing : %s\n",p);

    if (!fp)
	Quit("fopen");

    sprintf(fn,"%s.dir",p);

    if (mkdir(fn,0777)==-1)
    	Quit("mkdir");

    while(fgets(buff,sizeof buff,fp))
    	{
	if (strncmp(buff,"From ",5)==0)
	    {
	    if (out)
		fclose(out);

	    sprintf(fn,"%s.dir/%d.txt",p,n++);

	    printf("Creating : %s\n",fn);

	    if (!(out=fopen(fn,"w")))
		Quit("fopen");
	    }

	fprintf(out,"%s",buff);
	}

    fclose(out);
    fclose(fp);
}