home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / letters / word.c < prev   
Encoding:
C/C++ Source or Header  |  1992-01-02  |  1.4 KB  |  85 lines

  1. /*
  2.  * find a random word in a dictionary file as part of letters.
  3.  *
  4.  * copyright 1991 Larry Moss (lm03_cif@uhura.cc.rochester.edu)
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include "config.h"
  11. #include "kinput.h"
  12.  
  13. #ifdef SYSV
  14. # define random lrand48
  15. #endif
  16.  
  17. char *getword()
  18. {
  19.     static char    buf[512];
  20.     static FILE    *fp = NULL;
  21.     static struct stat    s_buf;
  22.  
  23.     /*
  24.      * This is stuff that only needs to get done once.
  25.      */
  26.     if(fp == NULL) {
  27.         char    *dictionary = DICTIONARY;
  28.  
  29.         /*
  30.          * open the dictionary file
  31.          */
  32.         if((fp = fopen(dictionary, "r")) == NULL) {
  33.             fprintf(stderr, "can't open file: %s.\n", dictionary);
  34.             setterm(ORIG);
  35.             exit(1);
  36.         }
  37.         
  38.         /*
  39.          * Get length of dictionary in bytes so we can pick a
  40.          * random entry in it.
  41.          */
  42.         if(stat(dictionary, &s_buf) == -1) {
  43.             perror("stat");
  44.             setterm(ORIG);
  45.             exit(1);
  46.         }
  47.     }
  48.     
  49.     /*
  50.      * pick a random place in the dictionary
  51.      */
  52.     fseek(fp, random() % s_buf.st_size, 0);
  53.  
  54.     /*
  55.      * read until the end of a line, then read the next word.
  56.      */
  57.     fscanf(fp, "%*s%s", buf);
  58.  
  59.     /*
  60.      * Since we're reading two words at a time it's possible to go past
  61.      * the end of the file.  If that happens, use the first word in the
  62.      * dictionary.
  63.      */
  64.     if(buf == NULL) {
  65.         fseek(fp, 0L, 0);
  66.         fscanf(fp, "%s", buf);
  67.     }
  68.  
  69.     return buf;
  70. }
  71.  
  72.  
  73. char *bonusword()
  74. {
  75.     static char    buf[BONUSLENGTH + 1];
  76.     int        i;
  77.  
  78.     for(i = 0; i < BONUSLENGTH; i++)
  79.         buf[i] = (char)(random() % 94) + 33;
  80.  
  81.     buf[BONUSLENGTH] = 0;
  82.  
  83.     return buf;
  84. }
  85.