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.
- */
- /*
- * kfhash: hashing program for kfortune.tp
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
-
- void usage(void)
- {
- fprintf(stderr, "Usage: kfrandom <.tp file>\n");
- exit(1);
- }
-
- int main(int argc, char **argv)
- {
- FILE *tp, *txtp;
- unsigned int strs, num1, num2, i, j, n1l, n1h, n2l, n2h;
- unsigned long pos1, pos2;
-
- if (argc != 2) usage();
-
- if ((tp = fopen(argv[1], "rb+")) == NULL) {
- fprintf(stderr, ".tp file open error: %s\n", argv[1]);
- exit(2);
- }
-
- rewind(tp);
- if ((strs = getw(tp)) == EOF) {
- fprintf(stderr, "read error: number of strings\n");
- exit(4);
- }
-
- fprintf(stdout, "total strings: %d\n", strs);
-
- srand(time(NULL));
- for (i = 0; i < (time(NULL) % 100); i++)
- (void)rand();
-
- for (i = 0; i < (strs / 2); i++) {
- num1 = (unsigned int)rand() % strs;
- pos1 = (unsigned long)sizeof(int) +
- ((unsigned long)sizeof(long) * (unsigned long)num1);
- num2 = (unsigned int)rand() % strs;
- pos2 = (unsigned long)sizeof(int) +
- ((unsigned long)sizeof(long) * (unsigned long)num2);
-
- if ((i % 100) == 0) {
- fprintf(stdout, "exchanging #%d: %d and %d\n", i + 1, num1, num2);
- }
-
-
- if (fseek(tp, pos1, SEEK_SET) != 0) {
- fprintf(stderr, "fseek() for .tp file failed:pos1 #1\n");
- exit(5);
- }
- n1l = getw(tp);
- n1h = getw(tp);
- if (fseek(tp, pos2, SEEK_SET) != 0) {
- fprintf(stderr, "fseek() for .tp file failed:pos2 #1\n");
- exit(5);
- }
- n2l = getw(tp);
- n2h = getw(tp);
- if (fseek(tp, pos2, SEEK_SET) != 0) {
- fprintf(stderr, "fseek() for .tp file failed:pos2 #2\n");
- exit(5);
- }
- if (putw(n1l, tp) == EOF) {
- fprintf(stderr, "write error n1l\n");
- exit(4);
- }
- if (putw(n1h, tp) == EOF) {
- fprintf(stderr, "write error n1h\n");
- exit(4);
- }
- fflush(tp);
- if (fseek(tp, pos1, SEEK_SET) != 0) {
- fprintf(stderr, "fseek() for .tp file failed:pos1 #2\n");
- exit(5);
- }
- if (putw(n2l, tp) == EOF) {
- fprintf(stderr, "write error n2l\n");
- exit(4);
- }
- if (putw(n2h, tp) == EOF) {
- fprintf(stderr, "write error n2h\n");
- exit(4);
- }
- fflush(tp);
- }
- fprintf(stdout, "hashing finished\n");
- fcloseall();
- exit(0);
- }
-