1 #include <config.h>
2 
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 
9 #include "suck.h"
10 
11 /* this file reads in the suck.db file and writes it out in a more readable format */
12 
main(int argc,char * argv[])13 int main(int argc, char *argv[]) {
14 
15 	int fdin;
16 	const char *fin = NULL, *fout = NULL;
17 	FILE *fpout;
18 	List item;
19 	Groups grp;
20 	long count;
21 	int retval = 0;
22 
23 	if(argc == 1) {
24 		fin = "suck.db";
25 		fout = "suck.db.out";
26 	}
27 	else if(argc != 3) {
28 		fprintf(stderr, "Usage: %s in_file out_file <RETURN>\n", argv[0]);
29 		retval = -1;
30 	}
31 	else {
32 		fin = argv[1];
33 		fout = argv[2];
34 	}
35 
36 	if(retval == 0) {
37 		if((fdin = open(fin, O_RDONLY)) == -1) {
38 			perror(fin);
39 			retval = -1;
40 		}
41 		else if((fpout = fopen(fout, "w")) == NULL) {
42 			perror(fout);
43 			retval = -1;
44 		}
45 		else {
46 			/* read items */
47 			read(fdin, &count, sizeof(long)); /* get item count */
48 			fprintf(fpout, "%ld\n", count);
49 
50 			while(count > 0 && read(fdin, &item, sizeof(item)) == sizeof(item)) {
51 				fprintf(fpout, "%s-%d-%ld-%ld-%c-%d-%d-%d\n", item.msgnr, item.groupnr,
52 					item.nr,item.dbnr,item.mandatory,item.downloaded,
53 					item.delete,item.sentcmd);
54 				count--;
55 			}
56 			if(count >  0) {
57 				perror(fin);
58 				retval = -1;
59 			}
60 			else {
61 				read(fdin, &count, sizeof(long)); /* get group count */
62 				fprintf(fpout, "%ld\n", count);
63 
64 				while(count >= 0 && read(fdin, &grp, sizeof(grp)) == sizeof(grp)) {
65 					fprintf(fpout, "%s-%d\n", grp.group, grp.nr);
66 					count--;
67 				}
68 				if(count >= 0) {
69 					perror(fin);
70 					retval = -1;
71 				}
72 			}
73 		}
74 
75 	}
76 	return retval;
77 }
78