Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include <u.h>
2
#include <libc.h>
3
#include <bio.h>
4
#include "msgdb.h"
5
 
6
void
7
usage(void)
8
{
9
	fprint(2, "usage: msgdb [-c] file\n");
10
	exits("usage");
11
}
12
 
13
void
14
main(int argc, char **argv)
15
{
16
	int create = 0;
17
	Msgdb *db;
18
	char *tok, *p;
19
	long val;
20
	int input;
21
	Biobuf b;
22
 
23
	input = 0;
24
	ARGBEGIN{
25
	case 'c':
26
		create = 1;
27
		break;
28
	case 'i':
29
		input = 1;
30
		break;
31
	default:
32
		usage();
33
	}ARGEND
34
 
35
	if(argc != 1)
36
		usage();
37
 
38
	if((db = mdopen(argv[0], create)) == nil)
39
		sysfatal("open db: %r");
40
 
41
	if(input){
42
		Binit(&b, 0, OREAD);
43
		while((tok = Brdline(&b, '\n')) != nil){
44
			tok[Blinelen(&b)-1] = '\0';
45
			p = strrchr(tok, ' ');
46
			if(p == nil)
47
				val = mdget(db, tok)+1;
48
			else{
49
				*p++ = 0;
50
				val = atoi(p);
51
			}
52
			mdput(db, tok, val);
53
		}
54
	}else{
55
		mdenum(db);
56
		Binit(&b, 1, OWRITE);
57
		while(mdnext(db, &tok, &val) >= 0)
58
			Bprint(&b, "%s %ld\n", tok, val);
59
		Bterm(&b);
60
	}
61
	mdclose(db);
62
	exits(nil);
63
}