-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword.c
121 lines (102 loc) · 2.18 KB
/
word.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* find a random word in a dictionary file as part of letters.
*
* copyright 1991 Larry Moss ([email protected])
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "config.h"
#include "kinput.h"
#include "turboc.h"
#ifdef __TURBOC__
# include <conio.h>
# include <stdlib.h>
# define random rand
#endif
#ifdef SYSV
# define random lrand48
#endif
extern char *dictionary, *choice;
extern int choicelen;
char *getword()
{
static char buf[512];
static FILE *fp = NULL;
static struct stat s_buf;
/*
* if there's a 'choice' string set, choose a selection of letters
* from it instead of reading the dictionary.
*/
if (choice) {
char *start, *p = buf;
int wlen;
start = choice + (random() % choicelen);
wlen = (MINSTRING + (random() % (MAXSTRING - MINSTRING)))
% sizeof(buf);
while(wlen--) {
if (*start == '\0')
start = choice;
*p++ = *start++;
}
*p = '\0';
return buf;
}
/*
* This is stuff that only needs to get done once.
*/
if(fp == NULL) {
/*
* open the dictionary file
*/
if((fp = fopen(dictionary, "r")) == NULL) {
fprintf(stderr, "can't open file: %s.\n", dictionary);
setterm(ORIG);
textattr_clr;
exit(1);
}
/*
* Get length of dictionary in bytes so we can pick a
* random entry in it.
*/
if(stat(dictionary, &s_buf) == -1) {
perror("stat");
setterm(ORIG);
textattr_clr;
exit(1);
}
}
/*
* pick a random place in the dictionary
*/
#ifdef __TURBOC__
fseek(fp, ((((long)rand() << 16) | rand())) % s_buf.st_size, 0);
#else
fseek(fp, random() % s_buf.st_size, 0);
#endif
/*
* read until the end of a line, then read the next word.
*/
fscanf(fp, "%*s%s", buf);
/*
* Since we're reading two words at a time it's possible to go past
* the end of the file. If that happens, use the first word in the
* dictionary.
*/
if(buf == NULL) {
fseek(fp, 0L, 0);
fscanf(fp, "%s", buf);
}
return buf;
}
char *bonusword()
{
static char buf[BONUSLENGTH + 1];
int i;
for(i = 0; i < BONUSLENGTH; i++)
buf[i] = choice ? *(choice + (random() % choicelen))
: (char)(random() % 94) + 33;
buf[BONUSLENGTH] = 0;
return buf;
}