Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
5 #define index findex
6 char choice[2048];
7 char *index = "#9/lib/fortunes.index";
8 char *fortunes = "#9/lib/fortunes";
10 #define lrand rand
12 void
13 main(int argc, char *argv[])
14 {
15 int i;
16 long offs;
17 uchar off[4];
18 int ix, nix;
19 int newindex, oldindex;
20 char *p;
21 Dir *fbuf, *ixbuf;
22 Biobuf *f, g;
24 index = unsharp(index);
25 fortunes = unsharp(fortunes);
27 newindex = 0;
28 oldindex = 0;
29 ix = offs = 0;
30 if((f=Bopen(argc>1?argv[1]:fortunes, OREAD)) == 0){
31 print("Misfortune!\n");
32 exits("misfortune");
33 }
34 ixbuf = nil;
35 if(argc == 1){
36 ix = open(index, OREAD);
37 if(ix>=0){
38 oldindex = 1;
39 ixbuf = dirfstat(ix);
40 fbuf = dirfstat(Bfildes(f));
41 if(ixbuf == nil || fbuf == nil){
42 print("Misfortune?\n");
43 exits("misfortune");
44 }
45 if(fbuf->mtime > ixbuf->mtime){
46 nix = create(index, OWRITE, 0666);
47 if(nix >= 0){
48 close(ix);
49 ix = nix;
50 newindex = 1;
51 oldindex = 0;
52 }
53 }
54 }else{
55 ix = create(index, OWRITE, 0666);
56 if(ix >= 0)
57 newindex = 1;
58 }
59 }
60 if(oldindex){
61 srand(getpid());
62 seek(ix, lrand()%(ixbuf->length/sizeof(offs))*sizeof(offs), 0);
63 read(ix, off, sizeof(off));
64 Bseek(f, off[0]|(off[1]<<8)|(off[2]<<16)|(off[3]<<24), 0);
65 p = Brdline(f, '\n');
66 if(p){
67 p[Blinelen(f)-1] = 0;
68 strcpy(choice, p);
69 }else
70 strcpy(choice, "Misfortune!");
71 }else{
72 Binit(&g, ix, 1);
73 srand(getpid());
74 for(i=1;;i++){
75 if(newindex)
76 offs = Boffset(f);
77 p = Brdline(f, '\n');
78 if(p == 0)
79 break;
80 p[Blinelen(f)-1] = 0;
81 if(newindex){
82 off[0] = offs;
83 off[1] = offs>>8;
84 off[2] = offs>>16;
85 off[3] = offs>>24;
86 Bwrite(&g, off, sizeof(off));
87 }
88 if(lrand()%i==0)
89 strcpy(choice, p);
90 }
91 }
92 print("%s\n", choice);
93 exits(0);
94 }