[24319] | 1 | /* $Id: vote.pc 3956 2010-01-05 20:56:56Z zacheiss $ |
---|
[23095] | 2 | * |
---|
| 3 | * Copyright (C) 1993-1998 by the Massachusetts Institute of Technology |
---|
| 4 | * For copying and distribution information, please see the file |
---|
| 5 | * <mit-copyright.h>. |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #include <mit-copyright.h> |
---|
| 9 | #include <moira.h> |
---|
| 10 | #include <moira_site.h> |
---|
| 11 | |
---|
| 12 | #include <ctype.h> |
---|
| 13 | #include <stdio.h> |
---|
| 14 | #include <string.h> |
---|
| 15 | |
---|
| 16 | EXEC SQL INCLUDE sqlca; |
---|
| 17 | |
---|
[24319] | 18 | RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/regtape/vote.pc $ $Id: vote.pc 3956 2010-01-05 20:56:56Z zacheiss $"); |
---|
[23095] | 19 | |
---|
| 20 | struct entry { |
---|
| 21 | char *line; |
---|
| 22 | char id[19]; |
---|
| 23 | char login[9]; |
---|
| 24 | }; |
---|
| 25 | |
---|
| 26 | char *whoami; |
---|
| 27 | int debug; |
---|
| 28 | |
---|
| 29 | struct entry *get_next_entry(FILE *in); |
---|
| 30 | int process_entry(struct entry *e); |
---|
| 31 | |
---|
| 32 | int main(int argc, char **argv) |
---|
| 33 | { |
---|
| 34 | FILE *in; |
---|
| 35 | struct entry *e; |
---|
| 36 | int i, wait = 0; |
---|
| 37 | char buf[BUFSIZ], *file = NULL, *p, *p1, *db = "moira"; |
---|
| 38 | |
---|
| 39 | debug = 0; |
---|
| 40 | whoami = strrchr(argv[0], '/'); |
---|
| 41 | if (whoami) |
---|
| 42 | whoami++; |
---|
| 43 | else |
---|
| 44 | whoami = argv[0]; |
---|
| 45 | |
---|
| 46 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ); |
---|
| 47 | setvbuf(stderr, NULL, _IOLBF, BUFSIZ); |
---|
| 48 | |
---|
| 49 | for (i = 1; i < argc; i++) |
---|
| 50 | { |
---|
| 51 | if (!strcmp(argv[i], "-w")) |
---|
| 52 | wait++; |
---|
| 53 | else if (!strcmp(argv[i], "-d")) |
---|
| 54 | debug = 1; |
---|
| 55 | else if (file) |
---|
| 56 | fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami); |
---|
| 57 | else |
---|
| 58 | file = argv[i]; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | in = fopen(file, "r"); |
---|
| 62 | if (!in) |
---|
| 63 | { |
---|
| 64 | fprintf(stderr, "Unable to open %s for input\n", file); |
---|
| 65 | exit(1); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | EXEC SQL CONNECT :db IDENTIFIED BY :db; |
---|
| 69 | if (sqlca.sqlcode) |
---|
| 70 | { |
---|
| 71 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); |
---|
| 72 | exit(1); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | while ((e = get_next_entry(in))) |
---|
| 76 | { |
---|
| 77 | i = process_entry(e); |
---|
| 78 | EXEC SQL COMMIT WORK; |
---|
| 79 | if (i == 0) |
---|
| 80 | { |
---|
| 81 | p = &(e->line[0]); |
---|
| 82 | for (p1 = e->login; *p1; p1++) |
---|
| 83 | *p++ = *p1; |
---|
| 84 | for (; p < &(e->line[10]); p++) |
---|
| 85 | *p = ' '; |
---|
| 86 | fputs(e->line, stdout); |
---|
| 87 | } |
---|
| 88 | if (wait) |
---|
| 89 | { |
---|
| 90 | printf("Next"); |
---|
| 91 | fflush(stdout); |
---|
[24250] | 92 | fgets(buf, sizeof(buf), stdin); |
---|
[23095] | 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | exit(0); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | struct entry *get_next_entry(FILE *in) |
---|
| 100 | { |
---|
| 101 | static struct entry e; |
---|
| 102 | static char buf[BUFSIZ]; |
---|
| 103 | |
---|
| 104 | if (!fgets(buf, sizeof(buf), in)) |
---|
| 105 | return NULL; |
---|
| 106 | |
---|
| 107 | e.line = &buf[0]; |
---|
| 108 | strncpy(e.id, &buf[0], 9); |
---|
| 109 | e.id[9] = '\0'; |
---|
| 110 | e.login[0] = '\0'; |
---|
| 111 | return &e; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | int process_entry(struct entry *e) |
---|
| 115 | { |
---|
| 116 | EXEC SQL BEGIN DECLARE SECTION; |
---|
| 117 | char *id, *login; |
---|
| 118 | EXEC SQL VAR login is STRING(9); |
---|
| 119 | EXEC SQL END DECLARE SECTION; |
---|
| 120 | |
---|
| 121 | id = e->id; |
---|
| 122 | login = e->login; |
---|
| 123 | EXEC SQL SELECT login INTO :login FROM users WHERE clearid = :id; |
---|
| 124 | if (sqlca.sqlcode) |
---|
| 125 | { |
---|
| 126 | fprintf(stderr, "Error %ld on %s\n", sqlca.sqlcode, e->line); |
---|
| 127 | return -1; |
---|
| 128 | } |
---|
| 129 | strncpy(e->login, login, 8); |
---|
| 130 | e->login[8] = 0; |
---|
| 131 | if (debug) |
---|
| 132 | printf("Got username %s\n", login); |
---|
| 133 | return 0; |
---|
| 134 | } |
---|