home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include "shop.h"
-
- static FILE *nounfile, *pluralfile, *adjfile;
- static int files_opened = FALSE;
- static long int number_of_nouns = -1L;
- static long int number_of_plurals = -1L;
- static long int number_of_adjs = -1L;
-
- extern char *cmdname;
-
- char *getitem(buff)
- char *buff;
- {
- long int my_rand();
-
- long int which_noun1 = 0L;
- long int which_noun2 = 0L;
- long int which_adj1 = 0L;
- long int which_adj2 = 0L;
-
- char adj1[MAX_WORD_LEN], adj2[MAX_WORD_LEN],
- noun1[MAX_WORD_LEN], noun2[MAX_WORD_LEN];
- char *get_nth_string();
- long int get_count();
-
- if (!files_opened) {
- nounfile = fopen(NOUN_FILE,"r");
- if (nounfile == NULL) {
- (void) fprintf(stderr,
- "%s: couldn't open noun file.\n",
- cmdname);
- exit(1);
- }
-
- pluralfile = fopen(PLURAL_FILE,"r");
- if (pluralfile == NULL) {
- (void) fprintf(stderr,
- "%s: couldn't open plural file.\n",
- cmdname);
- exit(1);
- }
-
- adjfile = fopen(ADJ_FILE,"r");
- if (adjfile == NULL) {
- (void) fprintf(stderr,
- "%s: couldn't open adjective file.\n",
- cmdname);
- exit(1);
- }
- files_opened = TRUE;
- }
-
- if (number_of_nouns < 0) {
- number_of_adjs = get_count(adjfile);
- number_of_nouns = get_count(nounfile);
- number_of_plurals = get_count(pluralfile);
- if (number_of_plurals != number_of_nouns) {
- (void) fprintf(stderr,
- "%s: plural file has %d elements; noun file, %d\n",
- cmdname,number_of_plurals, number_of_nouns);
- exit(1);
- }
- }
-
- noun1[0] = '\0';
- noun2[0] = '\0';
- adj1[0] = '\0';
- adj2[0] = '\0';
-
- switch(my_rand() % 5) {
- case 4: /*FALLTHROUGH*/
- case 3:
- which_noun1 = (long) (my_rand() % number_of_nouns);
- get_nth_string(nounfile,
- which_noun1,noun1,MAX_WORD_LEN);
- (void) strcat(noun1," ");
- /*FALLTHROUGH*/
- case 2: /*FALLTHROUGH*/
- case 1: /*FALLTHROUGH*/
- case 0:
- while ((which_noun2 =
- (long) (my_rand() % number_of_nouns))
- == which_noun1)
- /* do nothing */;
- get_nth_string(pluralfile,
- which_noun2,noun2,MAX_WORD_LEN);
- }
-
- switch(my_rand() % 4) {
- case 3:
- which_adj1 = (long) (my_rand() % number_of_adjs);
- get_nth_string(adjfile,which_adj1,adj1,MAX_WORD_LEN);
- (void) strcat(adj1," ");
- /*FALLTHROUGH*/
- case 2: /*FALLTHROUGH*/
- case 1: /*FALLTHROUGH*/
- case 0:
- while ((which_adj2 =
- (long) (my_rand() % number_of_adjs)) == which_adj1)
- /* do nothing */;
- get_nth_string(adjfile,which_adj2,adj2,MAX_WORD_LEN);
- (void) strcat(adj2," ");
- }
-
- (void) sprintf(buff,"%s%s%s%s",adj1,adj2,noun1,noun2);
-
- return buff;
- }
-
-