#include #include #include #include #include #include #include #include /* * mmm (In the style of Homer Simpson) Kees Cook 1997 * * Picks psudo-random words from the dictionary */ main(int argc, char * argv[0]) { int fd; struct stat st; struct timeval tv; char *base, *ptr; int offset,loops; gettimeofday(&tv,NULL); srand(tv.tv_sec * 1000 + tv.tv_usec); if ((fd=open("/usr/dict/words", O_RDONLY,0)) < 0) { perror("open"); exit(1); } fstat(fd,&st); /* Get the size */ if ((int)(base=(char *)mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0)) == -1) { perror("mmap"); exit(1); } close(fd); /* Now we can get to it via pointers */ /* if the argument is a number use that many words, otherwise 1 */ if (argc < 2 || (loops=atoi(argv[1]))==0) loops=1; for (;loops>0;loops--) { /* get a random offset within the size of the file */ /* (this assumes your max rand is bigger than your file size) */ while((offset=rand()) >= st.st_size) ; /* look backwards for beginning of a words (this random process tends to choose longer words) */ while ((offset >= 0) && (base[offset--] != '\n')); ptr = &base[offset+2]; write(STDOUT_FILENO,ptr,strchr(ptr,'\n') - (int)ptr); if (loops != 1) write(STDOUT_FILENO," ",1); } write(STDOUT_FILENO,".\n",2); }