home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / kfortune.zoo / kfhash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-20  |  2.4 KB  |  105 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.  * kfhash: hashing program for kfortune.tp
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17.  
  18. void usage(void)
  19. {
  20.     fprintf(stderr, "Usage: kfrandom <.tp file>\n");
  21.     exit(1);
  22. }
  23.  
  24. int main(int argc, char **argv)
  25. {
  26.     FILE *tp, *txtp;
  27.     unsigned int strs, num1, num2, i, j, n1l, n1h, n2l, n2h;
  28.     unsigned long pos1, pos2;
  29.  
  30.     if (argc != 2) usage();
  31.     
  32.     if ((tp = fopen(argv[1], "rb+")) == NULL) {
  33.         fprintf(stderr, ".tp file open error: %s\n", argv[1]);
  34.         exit(2);
  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.     fprintf(stdout, "total strings: %d\n", strs);
  44.     
  45.     srand(time(NULL));
  46.     for (i = 0; i < (time(NULL) % 100); i++)
  47.         (void)rand();
  48.         
  49.     for (i = 0; i < (strs / 2); i++) {
  50.         num1 = (unsigned int)rand() % strs;
  51.         pos1 = (unsigned long)sizeof(int) +
  52.                ((unsigned long)sizeof(long) * (unsigned long)num1);
  53.         num2 = (unsigned int)rand() % strs;
  54.         pos2 = (unsigned long)sizeof(int) +
  55.                ((unsigned long)sizeof(long) * (unsigned long)num2);
  56.  
  57.     if ((i % 100) == 0) {
  58.         fprintf(stdout, "exchanging #%d: %d and %d\n", i + 1, num1, num2);
  59.         }
  60.     
  61.  
  62.         if (fseek(tp, pos1, SEEK_SET) != 0) {
  63.             fprintf(stderr, "fseek() for .tp file failed:pos1 #1\n");
  64.             exit(5);
  65.             }
  66.         n1l = getw(tp);
  67.         n1h = getw(tp);
  68.         if (fseek(tp, pos2, SEEK_SET) != 0) {
  69.             fprintf(stderr, "fseek() for .tp file failed:pos2 #1\n");
  70.             exit(5);
  71.             }
  72.         n2l = getw(tp);
  73.         n2h = getw(tp);
  74.         if (fseek(tp, pos2, SEEK_SET) != 0) {
  75.             fprintf(stderr, "fseek() for .tp file failed:pos2 #2\n");
  76.             exit(5);
  77.             }
  78.         if (putw(n1l, tp) == EOF) {
  79.             fprintf(stderr, "write error n1l\n");
  80.             exit(4);
  81.             }
  82.         if (putw(n1h, tp) == EOF) {
  83.             fprintf(stderr, "write error n1h\n");
  84.             exit(4);
  85.             }
  86.         fflush(tp);
  87.         if (fseek(tp, pos1, SEEK_SET) != 0) {
  88.             fprintf(stderr, "fseek() for .tp file failed:pos1 #2\n");
  89.             exit(5);
  90.             }
  91.         if (putw(n2l, tp) == EOF) {
  92.             fprintf(stderr, "write error n2l\n");
  93.             exit(4);
  94.             }
  95.         if (putw(n2h, tp) == EOF) {
  96.             fprintf(stderr, "write error n2h\n");
  97.             exit(4);
  98.             }
  99.         fflush(tp);
  100.         }
  101.     fprintf(stdout, "hashing finished\n");
  102.     fcloseall();
  103.     exit(0);
  104. }
  105.