home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / rwords < prev    next >
Text File  |  1989-05-14  |  4KB  |  145 lines

  1. Newsgroups: comp.sources.misc
  2. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  3. Subject: v06i101: random word-list generator src
  4. Keywords: random word-list generator /usr/dict/words
  5. Organization: Johns Hopkins Hospital
  6. Reply-To: glenn@osiris.UUCP (Glenn M. Mason)
  7.  
  8. Posting-number: Volume 6, Issue 101
  9. Submitted-by: glenn@osiris.UUCP (Glenn M. Mason)
  10. Archive-name: rwords
  11.  
  12. [I fixed "dummyshar" this time; the file size is reported correctly.  ++bsa]
  13.  
  14. Here is the source for a simple, but very useful program for generating
  15. random word lists. Rwords is useful for generating input for testing
  16. software, such as debugging btree-lib code, etc. I compiled and tested
  17. this program on a Sun-3/60 and a Sun-4(SunOS-4.0).
  18.  
  19. I hope that someone may find it useful.
  20. Enjoy!
  21.  
  22.  
  23. #! /bin/sh
  24. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  25. # Contents:  rwords.c
  26. echo extracting 'rwords.c'
  27. if test -f 'rwords.c' -a -z "$1"; then echo Not overwriting 'rwords.c'; else
  28. sed 's/^X//' << \EOF > 'rwords.c'
  29. X/*
  30. X * NAME
  31. X *  rwords - a random word list generator
  32. X *
  33. X * SYNOPSIS
  34. X *  rwords [-fdictfile] n
  35. X *
  36. X * DESCRIPTION
  37. X *  Rwords generates a random list of words from /usr/dict/words
  38. X *  or an alternate user-specified word list file with the same
  39. X *  format as /usr/dict/words. A number, n, must be specified on
  40. X *  the command line which is the number of words to generate; n
  41. X *  must be greater that 0.  Words will be multiply generated in
  42. X *  the case where n is greater than the number of words in the word
  43. X *  file (note also that there is a very small probability that all
  44. X *  words in the word file will be generated in any case)
  45. X *
  46. X * AUTHOR
  47. X *  Glenn M. Mason,  May 1989
  48. X *  (C)Laboratory for Applied Research in Academic Information
  49. X *  The William H. Welch Medical Library,
  50. X *  The Johns Hopkins University
  51. X *
  52. X */
  53. X#include <stdio.h>
  54. X#include <ctype.h>
  55. X#include <fcntl.h>
  56. X#include <sys/types.h>
  57. X#include <sys/stat.h>
  58. X#include <sys/file.h>
  59. X#include <sys/timeb.h>
  60. X
  61. X#define DICT "/usr/dict/words"
  62. X
  63. X
  64. Xmain(argc,argv)
  65. Xint argc;
  66. Xchar *argv[];
  67. X{
  68. X    char *words,usage[80];
  69. X    struct stat sbuf;
  70. X    char wbuf[BUFSIZ];
  71. X    int fd,nw,nread;
  72. X    register char *p,*q;
  73. X    sprintf(usage,"Usage: %s [-fwordlist] n\n",argv[0]);
  74. X    if (argc < 2)
  75. X        fail(usage,0);
  76. X    words = DICT;
  77. X    if (argc > 2) {
  78. X        if (strncmp(argv[1],"-f",2))
  79. X            fail(usage,0);
  80. X        if (argv[1][2] == NULL)
  81. X            fail(usage,0);
  82. X        words = &argv[1][2];
  83. X        nw = atoi(argv[2]);
  84. X    }
  85. X    else
  86. X        nw = atoi(argv[1]);
  87. X    if (nw == 0) {
  88. X        sprintf(usage,"%s: specify number of words > 0\n",argv[0]);
  89. X        fail(usage,0);
  90. X    }
  91. X    if ((fd = open(words,O_RDONLY)) == -1)
  92. X        fail(words,1);
  93. X    if (fstat(fd,&sbuf) == -1)
  94. X        fail("fstat",1);
  95. X    if (!(sbuf.st_mode & S_IFREG)) {
  96. X        sprintf(usage,"%s: not a regular file\n",words);
  97. X        fail(usage,0);
  98. X    }
  99. X    srand48(time((time_t*)0));  /*non-repeating sequences*/
  100. X    while (nw > 0) {
  101. X        long r = lrand48()%sbuf.st_size;
  102. X        if (r < 2L)  /* prob. at 1st byte = first word */
  103. X            r = 0L;
  104. X        if (lseek(fd,(off_t)r,L_SET) == (off_t)-1)
  105. X            fail("lseek",1);
  106. X        if ((nread = read(fd,wbuf,sizeof(wbuf))) == -1)
  107. X            fail("read",1);
  108. X        if (nread < sizeof(wbuf))
  109. X            wbuf[nread] = NULL;
  110. X        p = wbuf;
  111. X        if (r != 0L) {
  112. X            for (;*p != NULL && *p != '\n';p++)
  113. X                ;
  114. X            p++;
  115. X        }
  116. X        if (*p == NULL)  /*EOF*/
  117. X            continue;
  118. X        for (q = p;*q != NULL && *q != '\n';q++)
  119. X            ;
  120. X        if (*q == NULL)  /*try again*/
  121. X            continue;
  122. X        else
  123. X            *q = NULL;  /*... a word!*/
  124. X        nw--;
  125. X        puts(p);
  126. X    }
  127. X    exit(0);
  128. X}
  129. X
  130. Xfail (s,flg)
  131. Xchar *s;
  132. Xint flg;
  133. X{
  134. X    if (flg)
  135. X        perror(s);
  136. X    else
  137. X        fputs(s,stderr);
  138. X    exit(1);
  139. X}
  140. EOF
  141. chars=`wc -c < 'rwords.c'`
  142. if test $chars !=     2483; then echo 'rwords.c' is $chars characters, should be     2483 characters!; fi
  143. fi
  144. exit 0
  145.