File: read_db.c

package info (click to toggle)
suck 4.3.2-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,156 kB
  • ctags: 1,133
  • sloc: ansic: 12,143; perl: 539; makefile: 400; sh: 363; java: 144
file content (77 lines) | stat: -rw-r--r-- 1,577 bytes parent folder | download | duplicates (8)
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
71
72
73
74
75
76
77
#include <config.h>

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "suck.h"

/* this file reads in the suck.db file and writes it out in a more readable format */

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

	int fdin;
	const char *fin = NULL, *fout = NULL;
	FILE *fpout;
	List item;
	Groups grp;
	long count;
	int retval = 0;
	
	if(argc == 1) {
		fin = "suck.db";
		fout = "suck.db.out";
	}
	else if(argc != 3) {
		fprintf(stderr, "Usage: %s in_file out_file <RETURN>\n", argv[0]);
		retval = -1;
	}
	else {
		fin = argv[1];
		fout = argv[2];
	}
	
	if(retval == 0) {
		if((fdin = open(fin, O_RDONLY)) == -1) {
			perror(fin);
			retval = -1;
		}
		else if((fpout = fopen(fout, "w")) == NULL) {
			perror(fout);
			retval = -1;
		}
		else {
			/* read items */
			read(fdin, &count, sizeof(long)); /* get item count */
			fprintf(fpout, "%ld\n", count);

			while(count > 0 && read(fdin, &item, sizeof(item)) == sizeof(item)) {
				fprintf(fpout, "%s-%d-%ld-%ld-%c-%d-%d-%d\n", item.msgnr, item.groupnr,
					item.nr,item.dbnr,item.mandatory,item.downloaded,
					item.delete,item.sentcmd);
				count--;
			}
			if(count >  0) {
				perror(fin);
				retval = -1;
			}
			else {
				read(fdin, &count, sizeof(long)); /* get group count */
				fprintf(fpout, "%ld\n", count);
				
				while(count >= 0 && read(fdin, &grp, sizeof(grp)) == sizeof(grp)) {
					fprintf(fpout, "%s-%d\n", grp.group, grp.nr);
					count--;
				}
				if(count >= 0) {
					perror(fin);
					retval = -1;
				}
			}
		}
		
	}
	return retval;
}