-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvote.c
395 lines (350 loc) · 10.3 KB
/
vote.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* Voting SQLite loader, Enoch */
#include <ctype.h>
#include <errno.h>
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#ifdef __MINGW32__
#define QUIT " Enter ctrl-Z to end program.\n"
/*
* public domain strtok_r() by Charlie Gordon
*
* from comp.lang.c 9/14/2007
*
* http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
*
* (Declaration that it's public domain):
* http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
*/
char *strtok_r(char *str, const char *delim, char **nextp)
{
char *ret;
if (str == NULL) {
str = *nextp;
}
str += strspn(str, delim);
if (*str == '\0') {
return NULL;
}
ret = str;
str += strcspn(str, delim);
if (*str) {
*str++ = '\0';
}
*nextp = str;
return ret;
}
#else
#define QUIT " Enter ctrl-D to end program.\n"
#endif
#define SEAS 7 /* max no. of seats */
#define CANS 21 /* max no. of candidates */
#define NCOLS 23 /* max number of db columns */
int Report[NCOLS];
int DBreport(void *name, int ncols, char **values, char **headers)
{
int i;
char *value;
if (name != NULL && ncols > 0)
printf("%s: ", (const char *) name);
for (i = 0; i < ncols; i++) {
value = values[i];
if (value == NULL) {
value = "N/A";
Report[i] = 0;
}
else
sscanf(value, "%i", Report + i);
if (name != NULL)
printf("%s = %s ", headers[i], value);
}
if (name != NULL && ncols > 0)
printf("\n");
return 0;
}
sqlite3 *DB;
int quit;
void Exec(const char *sql, int (*callback)(void *, int, char **, char **),
void *foreword)
{
char *errmsg = NULL;
if (sqlite3_exec(DB, sql, callback, foreword, &errmsg) != SQLITE_OK) {
if (errmsg) {
fprintf(stderr, DBNAME ": %s\n", errmsg);
sqlite3_free(errmsg);
}
sqlite3_close(DB);
exit(EXIT_FAILURE);
}
}
void bye()
{
if (quit)
fprintf(stderr, "EOF\n");
#ifdef __MINGW32__
if (!quit) {
fprintf(stderr, "Press Enter to end...");
fflush(stdout);
while (getchar() != '\n');
}
#endif
}
void cry(int __attribute__ ((unused)) signum)
{
fprintf(stderr, QUIT);
}
int main(int argc, char *argv[])
{
const char *del = "., \n";
char *p, line[1024], *q, text[1024], xX, *sally, *sunit;
int init, opt, seas, cans, secret, anons;
int i, j, rc, cnt, num, ix[CANS];
FILE *fp;
strcpy(line, argv[0]);
strcpy(text, argv[0]);
#ifdef __MINGW32__
p = strrchr(line, '\\');
q = strrchr(text, '\\');
#else
p = strrchr(line, '/');
q = strrchr(text, '/');
#endif
if (p)
p++;
else
p = line;
strcpy(p, "vote.ini");
if (q)
q++;
else
q = text;
strcpy(q, DBNAME ".db");
init = secret = 0;
xX = 'O';
while ((opt = getopt(argc, argv, "?i:")) != -1) {
switch (opt) {
case 'i':
num = sscanf(optarg, "%d:%d:%c", &seas, &cans, &xX);
init = (2 <= num && num <= 3 &&
1 <= seas && seas <= SEAS &&
1 <= cans && cans <= CANS &&
(num == 2 || (secret = toupper(xX) == 'X')));
if (init)
break;
__attribute__((fallthrough));
case '?':
fprintf(stderr,
"https://github.com/wexi/ballot -- " __DATE__ ".\n"
"Init:\tvote -i <#Seats>:<#Candidates>[:X]\n"
"Sqlite:\t" DBNAME ".db\n");
exit(EXIT_FAILURE);
}
}
atexit(bye);
signal(SIGINT, cry);
if (sqlite3_open_v2(text, &DB, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
fprintf(stderr, DBNAME ": %s\n", sqlite3_errmsg(DB));
exit(EXIT_FAILURE);
}
if (init) {
Exec("CREATE TABLE IF NOT EXISTS evotes(apt INTEGER PRIMARY KEY, FOREIGN KEY(apt) REFERENCES shares(apt))", NULL, NULL);
Exec("CREATE TABLE IF NOT EXISTS arrears(apt INTEGER PRIMARY KEY, FOREIGN KEY(apt) REFERENCES shares(apt))", NULL, NULL);
Exec("CREATE TEMP TABLE bans AS SELECT apt FROM evotes UNION SELECT apt FROM arrears", NULL, NULL);
if (secret) {
if (xX == 'X') {
for (cnt = 0; cnt < 10; cnt++) {
rc = sqlite3_exec(DB, "UPDATE shares SET code = random()",
NULL, NULL, NULL);
if (rc == SQLITE_OK || rc != SQLITE_CONSTRAINT)
break;
}
if (rc != SQLITE_OK) {
fprintf(stderr, DBNAME ": %s\n", sqlite3_errmsg(DB));
sqlite3_close(DB);
exit(EXIT_FAILURE);
}
}
Exec("SELECT count(apt) FROM shares WHERE code IS NULL", DBreport, NULL);
if (Report[0]) {
fprintf(stderr, DBNAME ": shares.code is uninitialized, use :X\n");
exit(EXIT_FAILURE);
}
Exec("DROP TABLE IF EXISTS shuffle", NULL, NULL);
Exec("CREATE TABLE shuffle (apt INTEGER PRIMARY KEY AUTOINCREMENT, share INTEGER, code INTEGER UNIQUE)", NULL, NULL);
Exec("INSERT INTO shuffle (share, code) SELECT share, code FROM shares ORDER BY code", NULL, NULL);
Exec("DROP TABLE IF EXISTS anons", NULL, NULL);
Exec("CREATE TABLE anons (apt INTEGER PRIMARY KEY, unit INTEGER UNIQUE, FOREIGN KEY(apt) REFERENCES shares(apt))", NULL, NULL);
Exec("INSERT INTO anons SELECT shares.apt apt, shuffle.apt unit FROM shares JOIN shuffle USING (code) ORDER BY apt", NULL, NULL);
Exec("DELETE FROM shuffle WHERE code IN (SELECT code FROM shares JOIN bans USING (apt))", NULL, NULL);
}
else {
Exec("DROP TABLE IF EXISTS shuffle", NULL, NULL);
Exec("CREATE TABLE shuffle (apt INTEGER PRIMARY KEY, share INTEGER, FOREIGN KEY(apt) REFERENCES shares(apt))", NULL, NULL);
Exec("INSERT INTO shuffle SELECT apt, share FROM shares LEFT JOIN bans USING (apt) WHERE bans.apt IS NULL ORDER BY apt", NULL, NULL);
}
Exec("DROP TABLE IF EXISTS votes", NULL, NULL);
strcpy(text, "CREATE TABLE votes (apt INTEGER PRIMAY KEY, vtime INTEGER");
for (i = 0; i < cans; i++) {
p = text + strlen(text);
sprintf(p, ", can%d INTEGER DEFAULT 0", i + 1);
}
strcat(text, ", FOREIGN KEY(apt) REFERENCES shares(apt))");
Exec(text, NULL, NULL);
if ((fp = fopen(line, "wt")) == NULL ||
fprintf(fp, "%d:%d:%d\n", seas, cans, secret) < 0 ||
fclose(fp) == EOF) {
perror(line);
exit(EXIT_FAILURE);
}
}
else {
if ((fp = fopen(line, "rt")) == NULL) {
perror(line);
exit(EXIT_FAILURE);
}
if (!(fscanf(fp, "%d:%d:%d\n", &seas, &cans, &secret) == 3
&& 1 <= seas && seas <= SEAS && 1 <= cans && cans <= CANS
&& fclose(fp) == 0)) {
fprintf(stderr, "Check vote init!\n");
exit(EXIT_FAILURE);
}
/* verify tables existence */
Exec("SELECT count(*) FROM votes", NULL, NULL);
Exec("SELECT count(*) FROM evotes", NULL, NULL);
Exec("SELECT count(*) FROM arrears", NULL, NULL);
Exec("SELECT count(*) FROM shuffle", NULL, NULL);
if (!secret) {
Exec("CREATE TEMP TABLE bans AS SELECT apt FROM evotes UNION SELECT apt FROM arrears", NULL, NULL);
Exec("SELECT count(*) FROM votes JOIN bans USING (apt)", DBreport, NULL);
if (Report[0] != 0) {
fprintf(stderr, DBNAME " Ineligible apartments had voted, manual intervention is needed!\n");
exit(EXIT_FAILURE);
}
}
}
anons = sqlite3_exec(DB, "SELECT count(*) FROM anons", NULL, NULL, NULL) == SQLITE_OK;
sunit = secret ? "Unit#" : "Apt#";
sally = secret ? "Anon" : "Open";
Exec("SELECT COUNT(apt) AS 'Eligible-Apartments', sum(share) AS 'Eligible-Shares' FROM shuffle", DBreport, DBNAME);
printf("%s Vote: Seats = %d, Candidates = %d\n", sally, seas, cans);
printf("Enter negative %s to delete its vote;" QUIT, sunit);
while (1) {
__label__ cycle;
cycle:
/* report current vote */
strcpy(text, "SELECT ");
for (i = 0; i < cans; i++) {
p = text + strlen(text);
sprintf(p, "sum(can%d), ", i + 1);
}
p = text + strlen(text) - 2;
strcpy(p, " FROM votes");
Exec(text, DBreport, NULL); /* Report[i] sums shares cast for can[i] */
strcpy(text, "SELECT count(apt) 'ballots', sum(shuffle.share) AS 'shares'");
for (i = 0; i <= seas; i++) { /* descending sort candidates */
num = -1;
for (j = 0; j < cans; j++) {
if (Report[j] > num) {
cnt = j;
num = Report[j];
}
}
Report[cnt] = -1;
p = text + strlen(text);
sprintf(p, ", sum(votes.can%d) AS c%d", cnt + 1, cnt + 1);
}
strcat(text, " FROM votes JOIN shuffle USING (apt)");
Exec(text, DBreport, "\nVoted");
/* enter vote */
printf("%s.Can1.Can2... ", sunit);
fflush(stdout);
p = fgets(line, sizeof(line), stdin);
if (p == NULL) {
quit = 1;
sqlite3_close(DB);
exit(EXIT_SUCCESS);
}
p = strtok_r(p, del, &q);
if (p == NULL || sscanf(p, "%d", &cnt) != 1) {
printf("NO INPUT\n");
goto cycle;
}
sprintf(text, "SELECT count(*) FROM shuffle WHERE apt = %d",
abs(cnt));
Exec(text, DBreport, NULL);
if (Report[0] != 1) {
printf("%s UNKNOWN\n", sunit);
if (!secret) {
sprintf(text,
"SELECT count(apt) AS '#evotes' FROM evotes WHERE apt = %d",
abs(cnt));
Exec(text, DBreport, "Search");
sprintf(text,
"SELECT count(apt) AS '#arrears' FROM arrears WHERE apt = %d",
abs(cnt));
Exec(text, DBreport, "Search");
}
goto cycle;
}
if (secret || !anons)
sprintf(text,
"SELECT apt '%s', share 'Share' FROM shuffle WHERE apt = %d",
sunit, cnt);
else
sprintf(text,
"SELECT apt 'Apt#', share 'Share', unit 'Unit#' FROM shuffle JOIN anons USING (apt) WHERE apt = %d",
cnt);
Exec(text, DBreport, "Member");
num = Report[1];
if (cnt < 0) {
sprintf(text, "DELETE FROM votes WHERE apt = %d", abs(cnt));
Exec(text, NULL, NULL);
printf("DELETED %s %d\n", sunit, abs(cnt));
goto cycle;
}
sprintf(text, "SELECT count(*) FROM votes WHERE apt = %d", cnt);
Exec(text, DBreport, NULL);
if (Report[0] != 0) {
printf("%s ALREADY VOTED\n", sunit);
goto cycle;
}
memset(ix, 0, sizeof(int) * cans);
p = strtok_r(NULL, del, &q);
for (i = 0; i < seas; i++) {
if (p == NULL)
break;
if (sscanf(p, "%d", &j) != 1 || j < 1 || j > cans
|| ix[j - 1] > 0) {
printf("ILL CAN\n");
goto cycle;
}
ix[j - 1] = num;
p = strtok_r(NULL, del, &q);
}
if (p != NULL) {
printf("TOO MANY VOTES\n");
goto cycle;
}
strcpy(text, "INSERT INTO votes (apt, vtime, ");
for (i = 0; i < cans; i++) {
if (ix[i] > 0) {
p = text + strlen(text);
sprintf(p, "can%d, ", i + 1);
}
}
p = text + strlen(text) - 2;
sprintf(p, ") VALUES (%d, %ld,", cnt, time(NULL));
for (i = 0; i < cans; i++)
if (ix[i] > 0) {
p = text + strlen(text);
sprintf(p, "%d, ", ix[i]);
}
p = text + strlen(text) - 2;
strcpy(p, ")");
Exec(text, NULL, NULL);
}
}