fortune.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. char choice[2048];
  13. char index[] = "/sys/games/lib/fortunes.index";
  14. char fortunes[] = "/sys/games/lib/fortunes";
  15. void
  16. main(int argc, char *argv[])
  17. {
  18. int i;
  19. int32_t offs;
  20. uint8_t off[4];
  21. int ix, nix;
  22. int newindex, oldindex;
  23. char *p;
  24. Dir *fbuf, *ixbuf;
  25. Biobuf *f, g;
  26. newindex = 0;
  27. oldindex = 0;
  28. ix = offs = 0;
  29. if((f=Bopen(argc>1?argv[1]:fortunes, OREAD)) == 0){
  30. print("Misfortune!\n");
  31. exits("misfortune");
  32. }
  33. ixbuf = nil;
  34. if(argc == 1){
  35. ix = open(index, OREAD);
  36. if(ix>=0){
  37. ixbuf = dirfstat(ix);
  38. fbuf = dirfstat(Bfildes(f));
  39. if(ixbuf == nil || fbuf == nil){
  40. print("Misfortune?\n");
  41. exits("misfortune");
  42. }
  43. if(ixbuf->length == 0){
  44. /* someone else is rewriting the index */
  45. goto NoIndex;
  46. }
  47. oldindex = 1;
  48. if(fbuf->mtime > ixbuf->mtime){
  49. nix = create(index, OWRITE, 0666);
  50. if(nix >= 0){
  51. close(ix);
  52. ix = nix;
  53. newindex = 1;
  54. oldindex = 0;
  55. }
  56. }
  57. }else{
  58. ix = create(index, OWRITE, 0666);
  59. if(ix >= 0)
  60. newindex = 1;
  61. }
  62. }
  63. if(oldindex){
  64. seek(ix, truerand()%(ixbuf->length/sizeof(offs))*sizeof(offs), 0);
  65. read(ix, off, sizeof(off));
  66. Bseek(f, off[0]|(off[1]<<8)|(off[2]<<16)|(off[3]<<24), 0);
  67. p = Brdline(f, '\n');
  68. if(p){
  69. p[Blinelen(f)-1] = 0;
  70. strcpy(choice, p);
  71. }else
  72. strcpy(choice, "Misfortune!");
  73. }else{
  74. NoIndex:
  75. Binit(&g, ix, 1);
  76. srand(truerand());
  77. for(i=1;;i++){
  78. if(newindex)
  79. offs = Boffset(f);
  80. p = Brdline(f, '\n');
  81. if(p == 0)
  82. break;
  83. p[Blinelen(f)-1] = 0;
  84. if(newindex){
  85. off[0] = offs;
  86. off[1] = offs>>8;
  87. off[2] = offs>>16;
  88. off[3] = offs>>24;
  89. Bwrite(&g, off, sizeof(off));
  90. }
  91. if(lrand()%i==0)
  92. strcpy(choice, p);
  93. }
  94. }
  95. print("%s\n", choice);
  96. exits(0);
  97. }