home *** CD-ROM | disk | FTP | other *** search
- /*
- * kfortune: a very simple "fortune" clone
- *
- * Copyright (c) 1989 by Kenji Rikitake.
- * All Rights Reserved.
- * 1st version: May 20, 1989
- *
- * The author allows this program to be used for any purposes.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
-
- #define TPFILE "a:\\usr\\games\\lib\\kfortune.tp"
- #define TXTFILE "a:\\usr\\games\\lib\\kfortune.txt"
- #define DELIM 0x0c
-
- int main(int argc, char **argv)
- {
- FILE *tp, *txtp;
- int c;
- unsigned int strs, num, i;
- unsigned long pos;
-
- if ((tp = fopen(TPFILE, "rb")) == NULL) {
- fprintf(stderr, ".tp file open error: %s\n", TPFILE);
- exit(2);
- }
-
- if ((txtp = fopen(TXTFILE, "rb")) == NULL) {
- fprintf(stderr, ".txt file open error: %s\n", TXTFILE);
- exit(3);
- }
-
-
- rewind(tp);
- if ((strs = getw(tp)) == EOF) {
- fprintf(stderr, "read error: number of strings\n");
- exit(4);
- }
-
- #ifdef DEBUG
- fprintf(stderr, "total strings: %d\n", strs);
- #endif
-
- srand(time(NULL));
- for(i = 0; i < (time(NULL) % 100); i++)
- (void)rand();
- num = (unsigned int)rand() % strs;
- pos = (unsigned long)sizeof(int) +
- ((unsigned long)sizeof(long) * (unsigned long)num);
-
- #ifdef DEBUG
- fprintf(stderr, "strs = %d, num = %d, pos = %8.8lX\n", strs, num, pos);
- #endif
-
- if (fseek(tp, pos, SEEK_SET) != 0) {
- fprintf(stderr, "fseek() for .tp file failed\n");
- exit(5);
- }
- i = getw(tp);
- pos = ((unsigned long)getw(tp) << 16) + (unsigned long)i;
- #ifdef DEBUG
- fprintf(stderr, ".txt file position = %8.8lX\n", pos);
- #endif
- if (fseek(txtp, pos, SEEK_SET) != 0) {
- fprintf(stderr, "fseek() for .txt file failed\n");
- exit(6);
- }
-
- while((c = getc(txtp)) != EOF) {
- if (c == DELIM) break;
- putchar(c);
- }
-
- fcloseall();
- exit(0);
- }
-