]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/upas/bayes/addhash.c
New libregexp and APE ported to native
[plan9front.git] / sys / src / cmd / upas / bayes / addhash.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include "regexp.h"
5 #include "hash.h"
6
7 Hash hash;
8
9 void
10 usage(void)
11 {
12         fprint(2, "addhash [-o out] file scale [file scale]...\n");
13         exits("usage");
14 }
15
16 void
17 main(int argc, char **argv)
18 {
19         int i, fd, n;
20         char err[ERRMAX], *out;
21         Biobuf *b, bout;
22
23         out = nil;
24         ARGBEGIN{
25         case 'o':
26                 out = EARGF(usage());
27                 break;
28         default:
29                 usage();
30         }ARGEND;
31
32         if(argc==0 || argc%2)
33                 usage();
34
35         while(argc > 0){
36                 if((b = Bopenlock(argv[0], OREAD)) == nil)
37                         sysfatal("open %s: %r", argv[0]);
38                 n = atoi(argv[1]);
39                 if(n == 0)
40                         sysfatal("0 scale given");
41                 Breadhash(b, &hash, n);
42                 Bterm(b);
43                 argv += 2;
44                 argc -= 2;
45         }
46
47         fd = 1;
48         if(out){
49                 for(i=0; i<120; i++){
50                         if((fd = create(out, OWRITE, 0666|DMEXCL)) >= 0)
51                                 break;
52                         rerrstr(err, sizeof err);
53                         if(strstr(err, "file is locked")==nil && strstr(err, "exclusive lock")==nil)
54                                 break;
55                         sleep(1000);
56                 }
57                 if(fd < 0)
58                         sysfatal("could not open %s: %r", out);
59         }
60                 
61         Binit(&bout, fd, OWRITE);
62         Bwritehash(&bout, &hash);
63         Bterm(&bout);
64         exits(0);
65 }
66