home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / kfortune.zoo / kfortune.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-20  |  1.7 KB  |  80 lines

  1. /*
  2.  * kfortune: a very simple "fortune" clone
  3.  * 
  4.  * Copyright (c) 1989 by Kenji Rikitake.
  5.  * All Rights Reserved.
  6.  * 1st version: May 20, 1989
  7.  *
  8.  * The author allows this program to be used for any purposes.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14.  
  15. #define TPFILE "a:\\usr\\games\\lib\\kfortune.tp"
  16. #define TXTFILE "a:\\usr\\games\\lib\\kfortune.txt"
  17. #define DELIM 0x0c
  18.  
  19. int main(int argc, char **argv)
  20. {
  21.     FILE *tp, *txtp;
  22.     int c;
  23.     unsigned int strs, num, i;
  24.     unsigned long pos;
  25.     
  26.     if ((tp = fopen(TPFILE, "rb")) == NULL) {
  27.         fprintf(stderr, ".tp file open error: %s\n", TPFILE);
  28.         exit(2);
  29.         }
  30.  
  31.     if ((txtp = fopen(TXTFILE, "rb")) == NULL) {
  32.         fprintf(stderr, ".txt file open error: %s\n", TXTFILE);
  33.         exit(3);
  34.         }
  35.  
  36.     
  37.     rewind(tp);
  38.     if ((strs = getw(tp)) == EOF) {
  39.         fprintf(stderr, "read error: number of strings\n");
  40.         exit(4);
  41.         }
  42.         
  43. #ifdef DEBUG
  44.     fprintf(stderr, "total strings: %d\n", strs);
  45. #endif
  46.     
  47.     srand(time(NULL));
  48.     for(i = 0; i < (time(NULL) % 100); i++)
  49.         (void)rand();
  50.     num = (unsigned int)rand() % strs;
  51.     pos = (unsigned long)sizeof(int) +
  52.           ((unsigned long)sizeof(long) * (unsigned long)num);
  53.  
  54. #ifdef DEBUG
  55.     fprintf(stderr, "strs = %d, num = %d, pos = %8.8lX\n", strs, num, pos);
  56. #endif
  57.     
  58.     if (fseek(tp, pos, SEEK_SET) != 0) {
  59.         fprintf(stderr, "fseek() for .tp file failed\n");
  60.         exit(5);
  61.         }
  62.     i = getw(tp);
  63.     pos = ((unsigned long)getw(tp) << 16) + (unsigned long)i;
  64. #ifdef DEBUG
  65.     fprintf(stderr, ".txt file position = %8.8lX\n", pos);
  66. #endif
  67.     if (fseek(txtp, pos, SEEK_SET) != 0) {
  68.         fprintf(stderr, "fseek() for .txt file failed\n");
  69.         exit(6);
  70.         }
  71.  
  72.     while((c = getc(txtp)) != EOF) {
  73.         if (c == DELIM) break;
  74.         putchar(c);
  75.         }
  76.  
  77.     fcloseall();
  78.     exit(0);
  79. }
  80.