Repository navigation
Expand file tree
/
Copy pathvoc.c
More file actions
241 lines (199 loc) · 4.62 KB
/
Copy pathvoc.c
File metadata and controls
241 lines (199 loc) · 4.62 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* SPDX-License-Identifier: GPL-2.0+
* Copyright (C) 2017 Abinav Puthan Purayil
* */
#include <stdarg.h>
#include <errno.h>
#include <sys/stat.h>
#include <limits.h>
#include <time.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <getopt.h>
#define MAX_FILES 64
#define MAX_WORDLENGTH 128
#define MAX_WORDS 256
#define BUFSIZE 4096
void usage(){
fprintf(stderr, ""
"Usage : voc -<option> <optarg> <voc_db>\n"
"where <voc_db> is a dir containing all voc files\n"
"\n"
"interactive:\n"
" voc -(c|q) <file> <voc_db>\n"
" where -c for choice, -q for quiz from voc file <file>\n"
" which must be in relative path to voc_db (not absolute!)\n"
"\n"
"non-interactive:\n"
" voc -r <n> <voc_db>\n"
" prints n number of randoms definitions\n"
" from any voc file in voc_db\n"
);
}
void die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
_exit(EXIT_FAILURE);
}
/* return (if path is a dir) ? 1 : 0 */
int is_dir(const char *path)
{
struct stat sbuf;
if ((stat(path, &sbuf)) == -1)
die("stat(\"%s\") : %s\n", path, strerror(errno));
return S_ISDIR(sbuf.st_mode);
}
/* return (if path is a reg_file) ? 1 : 0 */
int is_regfile(const char *path)
{
struct stat sbuf;
if ((stat(path, &sbuf)) == -1)
die("stat(\"%s\") : %s\n", path, strerror(errno));
return S_ISREG(sbuf.st_mode);
}
/*
* Sets words and its corresponding meanings for the file pointed by
* fd and sets n to number of words found.
*/
void fill_dict(int fd, char words[][MAX_WORDLENGTH],
char meanings[][MAX_WORDLENGTH], int *n)
{
int red, iwords, ibuf;
char buf[BUFSIZE] = {0};
for (iwords = 0; (read(fd, buf, BUFSIZE));) {
for (ibuf = 0; buf[ibuf]; iwords++) {
for (int i = 0; buf[ibuf] && buf[ibuf] != ':'; ibuf++)
words[iwords][i++] = buf[ibuf];
ibuf++;
for (int i = 0; buf[ibuf] && buf[ibuf] != '\n'; ibuf++)
meanings[iwords][i++] = buf[ibuf];
ibuf++;
}
}
*n = iwords;
}
/*
* sets file to a random reg file in dir path.
* returns 1 if successful, else -1
*/
int rand_file(char *path, char *file)
{
DIR *dirp;
struct dirent *entry;
char files[MAX_FILES][MAX_WORDLENGTH] = {0};
int i = 0;
if (!(dirp = opendir(path)))
goto err;
for (; (entry = readdir(dirp));) {
if (entry->d_type == DT_REG)
strcpy(files[i++], entry->d_name);
}
if (!i) { /* no reg files in path */
goto err;
}
srand(time(NULL));
strcpy(file, files[rand() % i]);
return 1;
err:
return -1;
}
void start_quiz(char words[][MAX_WORDLENGTH],
char meanings[][MAX_WORDLENGTH], int n)
{
for (int i=0; i<n; i++) {
printf("\n\nDefine %s ? ", words[i]);
getchar();
printf("\n%s ", meanings[i]);
getchar();
printf("\033c");
}
}
void start_interactive_choice(char words[][MAX_WORDLENGTH],
char meanings[][MAX_WORDLENGTH], int n)
{
int choice = 0;
for (int i=0; i<n; i++)
printf("%d %s | ", i, words[i]);
while (1) {
printf("\n ? ");
scanf("%d", &choice); getchar();
if (choice >=n)
continue;
if (choice < 0)
break;
printf("%s -> %s\n", words[choice], meanings[choice]);
}
}
int main(int argc, char *argv[])
{
char voc_db[PATH_MAX], fname[MAX_WORDLENGTH];
char words[MAX_WORDS][MAX_WORDLENGTH] = {0};
char meanings[MAX_WORDS][MAX_WORDLENGTH] = {0};
int fd, n, opt, irand, nrand, run = 0;
if (argc != 4) {
goto invalid_format;
}
strcpy(voc_db, argv[argc - 1]);
if (!(is_dir(voc_db)))
die("Invalid db\n");
/* if arg for voc_db doesn't have a trailing / */
strcat(voc_db, "/");
while ((opt = getopt(argc, argv, "c:q:r:")) != -1) {
switch (opt) {
case 'c' :
strcat(voc_db, optarg);
run = 'c';
break;
case 'q' :
strcat(voc_db, optarg);
run = 'q';
break;
case 'r' :
nrand = atoi(optarg);
run = 'r';
if (rand_file(voc_db, fname) == -1) {
die("invalid db\n");
}
strcat(voc_db, fname);
break;
case '?' :
goto invalid_format;
default :
goto invalid_format;
}
}
/* voc_db should refer to a file now */
if (!(is_regfile(voc_db)))
die("%s is not a regular file\n", voc_db);
if ((fd = open(voc_db, O_RDONLY)) == -1)
die("open(\"%s\") : %s\n", voc_db, strerror(errno));
fill_dict(fd, words, meanings, &n);
switch (run) {
case 'c' :
start_interactive_choice(words, meanings, n);
break;
case 'q' :
start_quiz(words, meanings, n);
break;
case 'r' :
for (int i = 0; i < nrand; i++) {
irand = rand() % n;
while (strlen(words[irand]) <= 1)
irand++;
printf("%s -> %s\n", words[irand], meanings[irand]);
}
break;
}
return EXIT_SUCCESS;
invalid_format:
fprintf(stderr, "Invalid format\n");
usage();
return EXIT_FAILURE;
}