home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume6
/
rwords
< prev
next >
Wrap
Text File
|
1989-05-14
|
4KB
|
145 lines
Newsgroups: comp.sources.misc
From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
Subject: v06i101: random word-list generator src
Keywords: random word-list generator /usr/dict/words
Organization: Johns Hopkins Hospital
Reply-To: glenn@osiris.UUCP (Glenn M. Mason)
Posting-number: Volume 6, Issue 101
Submitted-by: glenn@osiris.UUCP (Glenn M. Mason)
Archive-name: rwords
[I fixed "dummyshar" this time; the file size is reported correctly. ++bsa]
Here is the source for a simple, but very useful program for generating
random word lists. Rwords is useful for generating input for testing
software, such as debugging btree-lib code, etc. I compiled and tested
this program on a Sun-3/60 and a Sun-4(SunOS-4.0).
I hope that someone may find it useful.
Enjoy!
#! /bin/sh
# This file was wrapped with "dummyshar". "sh" this file to extract.
# Contents: rwords.c
echo extracting 'rwords.c'
if test -f 'rwords.c' -a -z "$1"; then echo Not overwriting 'rwords.c'; else
sed 's/^X//' << \EOF > 'rwords.c'
X/*
X * NAME
X * rwords - a random word list generator
X *
X * SYNOPSIS
X * rwords [-fdictfile] n
X *
X * DESCRIPTION
X * Rwords generates a random list of words from /usr/dict/words
X * or an alternate user-specified word list file with the same
X * format as /usr/dict/words. A number, n, must be specified on
X * the command line which is the number of words to generate; n
X * must be greater that 0. Words will be multiply generated in
X * the case where n is greater than the number of words in the word
X * file (note also that there is a very small probability that all
X * words in the word file will be generated in any case)
X *
X * AUTHOR
X * Glenn M. Mason, May 1989
X * (C)Laboratory for Applied Research in Academic Information
X * The William H. Welch Medical Library,
X * The Johns Hopkins University
X *
X */
X#include <stdio.h>
X#include <ctype.h>
X#include <fcntl.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <sys/file.h>
X#include <sys/timeb.h>
X
X#define DICT "/usr/dict/words"
X
X
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X char *words,usage[80];
X struct stat sbuf;
X char wbuf[BUFSIZ];
X int fd,nw,nread;
X register char *p,*q;
X sprintf(usage,"Usage: %s [-fwordlist] n\n",argv[0]);
X if (argc < 2)
X fail(usage,0);
X words = DICT;
X if (argc > 2) {
X if (strncmp(argv[1],"-f",2))
X fail(usage,0);
X if (argv[1][2] == NULL)
X fail(usage,0);
X words = &argv[1][2];
X nw = atoi(argv[2]);
X }
X else
X nw = atoi(argv[1]);
X if (nw == 0) {
X sprintf(usage,"%s: specify number of words > 0\n",argv[0]);
X fail(usage,0);
X }
X if ((fd = open(words,O_RDONLY)) == -1)
X fail(words,1);
X if (fstat(fd,&sbuf) == -1)
X fail("fstat",1);
X if (!(sbuf.st_mode & S_IFREG)) {
X sprintf(usage,"%s: not a regular file\n",words);
X fail(usage,0);
X }
X srand48(time((time_t*)0)); /*non-repeating sequences*/
X while (nw > 0) {
X long r = lrand48()%sbuf.st_size;
X if (r < 2L) /* prob. at 1st byte = first word */
X r = 0L;
X if (lseek(fd,(off_t)r,L_SET) == (off_t)-1)
X fail("lseek",1);
X if ((nread = read(fd,wbuf,sizeof(wbuf))) == -1)
X fail("read",1);
X if (nread < sizeof(wbuf))
X wbuf[nread] = NULL;
X p = wbuf;
X if (r != 0L) {
X for (;*p != NULL && *p != '\n';p++)
X ;
X p++;
X }
X if (*p == NULL) /*EOF*/
X continue;
X for (q = p;*q != NULL && *q != '\n';q++)
X ;
X if (*q == NULL) /*try again*/
X continue;
X else
X *q = NULL; /*... a word!*/
X nw--;
X puts(p);
X }
X exit(0);
X}
X
Xfail (s,flg)
Xchar *s;
Xint flg;
X{
X if (flg)
X perror(s);
X else
X fputs(s,stderr);
X exit(1);
X}
EOF
chars=`wc -c < 'rwords.c'`
if test $chars != 2483; then echo 'rwords.c' is $chars characters, should be 2483 characters!; fi
fi
exit 0