]>
Commit | Line | Data |
---|---|---|
469f80df | 1 | /* $Header$ |
2 | */ | |
3 | ||
4 | #include <stdio.h> | |
5 | #include <strings.h> | |
6 | #include <ctype.h> | |
7 | #include <sys/time.h> | |
932028de | 8 | #include <moira.h> |
9 | #include <moira_site.h> | |
02cd9ede | 10 | EXEC SQL INCLUDE sqlca; |
469f80df | 11 | |
12 | ||
02cd9ede | 13 | #define WHO 11859 /* root */ |
14 | #define PROG "emp-tape" | |
469f80df | 15 | |
397e9044 | 16 | #define MAX_ID_VALUE 31999 |
469f80df | 17 | #define MIN_ID_VALUE 101 |
18 | ||
19 | /* File format is: | |
20 | ||
21 | 0-8 id number | |
22 | 9-38 name | |
23 | 39-62 office address | |
24 | 63-74 phone1 | |
25 | 75-86 phone2 | |
26 | 87-106 dept | |
27 | 107-156 title | |
28 | 157-186 username | |
29 | 187-241 host | |
30 | ||
31 | */ | |
32 | ||
33 | #define LOC_ID 0 | |
34 | #define LOC_NAME 9 | |
35 | #define LOC_OFFICE 39 | |
36 | #define LOC_PHONE 63 | |
37 | #define LOC_PHONE2 75 | |
38 | #define LOC_DEPT 87 | |
39 | #define LOC_TITLE 107 | |
40 | #define LOC_USERNAME 157 | |
41 | #define LOC_HOST 187 | |
42 | ||
43 | #define LEN_ID 9 | |
a6e9fead | 44 | #define LEN_NAME 30 |
45 | #define LEN_OFFICE 24 | |
469f80df | 46 | #define LEN_PHONE 12 |
47 | #define LEN_PHONE2 12 | |
a6e9fead | 48 | #define LEN_DEPT 20 |
49 | #define LEN_TITLE 50 | |
50 | #define LEN_USERNAME 30 | |
469f80df | 51 | #define LEN_HOST 55 |
52 | ||
53 | ||
54 | struct entry { | |
a6e9fead | 55 | char *name; |
469f80df | 56 | char *last; |
57 | char *first; | |
58 | char *middle; | |
59 | char *title; | |
60 | char *class; | |
61 | char *id; | |
62 | char *eid; | |
63 | char *dept; | |
64 | char *address; | |
65 | char *phone; | |
a6e9fead | 66 | char *phone2; |
469f80df | 67 | char *email; |
68 | }; | |
69 | ||
70 | ||
71 | char *whoami; | |
72 | int newfinger = 0; | |
73 | ||
3e77c6a3 | 74 | #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 100) |
75 | #define SQL_DUPLICATE -40100 | |
6f1556b3 | 76 | #define SQL_DEADLOCK -49900 |
3e77c6a3 | 77 | |
469f80df | 78 | |
79 | main(argc, argv) | |
80 | int argc; | |
81 | char **argv; | |
02cd9ede | 82 | { |
469f80df | 83 | FILE *in; |
84 | struct entry *e, *get_next_entry(); | |
85 | int i, wait = 0; | |
86 | char buf[BUFSIZ], *file = NULL; | |
87 | ||
88 | whoami = rindex(argv[0], '/'); | |
89 | if (whoami) | |
90 | whoami++; | |
91 | else | |
92 | whoami = argv[0]; | |
93 | ||
94 | for (i = 1; i < argc; i++) { | |
95 | if (!strcmp(argv[i], "-w")) | |
96 | wait++; | |
97 | else if (!strcmp(argv[i], "-D")) | |
98 | setenv("ING_SET", "set printqry"); | |
99 | else if (!strcmp(argv[i], "-n")) | |
100 | newfinger++; | |
101 | else if (file != NULL) | |
325821e4 | 102 | fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami); |
469f80df | 103 | else |
104 | file = argv[i]; | |
105 | } | |
106 | ||
107 | in = fopen(file, "r"); | |
108 | if (in == NULL) { | |
109 | fprintf(stderr, "Unable to open %s for input\n", file); | |
110 | exit(1); | |
111 | } | |
112 | ||
325821e4 | 113 | setlinebuf(stdout); |
114 | setlinebuf(stderr); | |
115 | ||
02cd9ede | 116 | EXEC SQL CONNECT moira; |
3e77c6a3 | 117 | if (sqlca.sqlcode != 0) { |
118 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
119 | exit(1); | |
120 | } | |
469f80df | 121 | |
122 | while (e = get_next_entry(in)) { | |
9b2c129e | 123 | again: |
469f80df | 124 | process_entry(e); |
3e77c6a3 | 125 | EXEC SQL COMMIT WORK; |
126 | if (sqlca.sqlcode != 0) { | |
6f1556b3 | 127 | if (sqlca.sqlcode == SQL_DEADLOCK) { |
128 | com_err(whoami, MR_DEADLOCK, "commiting work"); | |
129 | goto again; | |
130 | } else { | |
131 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
132 | exit(1); | |
133 | } | |
3e77c6a3 | 134 | } |
469f80df | 135 | if (wait) { |
136 | printf("Next"); | |
137 | fflush(stdout); | |
138 | gets(buf); | |
139 | } | |
140 | } | |
141 | ||
469f80df | 142 | exit(0); |
02cd9ede | 143 | } |
469f80df | 144 | |
145 | ||
146 | char *substr(buf, key) | |
147 | char *buf; | |
148 | char *key; | |
149 | { | |
150 | int l; | |
151 | ||
152 | for (l = strlen(key); *buf; buf++) | |
153 | if (!strncmp(buf, key, l)) | |
154 | return(buf); | |
155 | return(NULL); | |
156 | } | |
157 | ||
158 | ||
159 | struct entry *get_next_entry(in) | |
160 | FILE *in; | |
161 | { | |
162 | static struct entry e; | |
a6e9fead | 163 | static char buf[BUFSIZ], mid[16], eid[16], email[256]; |
164 | static char name[LEN_NAME+1], sname[LEN_NAME+1], id[LEN_ID+1]; | |
165 | static char office[LEN_OFFICE+1], phone[LEN_PHONE+1], phone2[LEN_PHONE2+1]; | |
166 | static char dept[LEN_DEPT+1], title[LEN_TITLE+1], username[LEN_USERNAME+1]; | |
167 | static char host[LEN_HOST+1]; | |
039f9637 | 168 | int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v; |
469f80df | 169 | char *p; |
170 | ||
4763be9c | 171 | if (fgets(buf, sizeof(buf), in) == NULL) |
172 | return((struct entry *)NULL); | |
a6e9fead | 173 | |
174 | strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0; | |
175 | strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0; | |
176 | strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE); office[LEN_OFFICE] = 0; | |
177 | strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); phone[LEN_PHONE] = 0; | |
178 | strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2); phone2[LEN_PHONE2] = 0; | |
179 | strncpy(dept, &buf[LOC_DEPT], LEN_DEPT); dept[LEN_DEPT] = 0; | |
180 | strncpy(title, &buf[LOC_TITLE], LEN_TITLE); title[LEN_TITLE] = 0; | |
181 | strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME); username[LEN_USERNAME] = 0; | |
182 | strncpy(host, &buf[LOC_HOST], LEN_HOST); host[LEN_HOST] = 0; | |
183 | ||
184 | strcpy(sname, name); | |
185 | e.name = strtrim(sname); | |
186 | p = index(name, ','); | |
469f80df | 187 | if (p) |
188 | *p = 0; | |
a6e9fead | 189 | e.last = strtrim(name); |
469f80df | 190 | if (p) { |
a6e9fead | 191 | p++; |
192 | while (isspace(*p)) | |
193 | p++; | |
194 | e.first = p; | |
469f80df | 195 | if (p = index(e.first, ' ')) { |
196 | *p = 0; | |
a6e9fead | 197 | e.first = strtrim(e.first); |
469f80df | 198 | e.middle = strtrim(p + 1); |
a6e9fead | 199 | } else { |
200 | e.first = strtrim(e.first); | |
201 | e.middle = ""; | |
202 | } | |
203 | } else { | |
204 | e.first = ""; | |
205 | e.middle = ""; | |
206 | } | |
039f9637 | 207 | ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0; |
469f80df | 208 | LookForSt(e.last); |
209 | LookForO(e.last); | |
039f9637 | 210 | LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv, |
211 | &ends_ii, &ends_v); | |
212 | LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv, | |
213 | &ends_ii, &ends_v); | |
469f80df | 214 | FixCase(e.last); |
215 | FixCase(e.first); | |
216 | FixCase(e.middle); | |
217 | ||
a6e9fead | 218 | e.id = id; |
469f80df | 219 | e.eid = eid; |
220 | EncryptID(e.eid, e.id, e.first, e.last); | |
221 | ||
a6e9fead | 222 | e.address = strtrim(office); |
223 | e.phone = strtrim(phone); | |
224 | e.phone2 = strtrim(phone2); | |
225 | e.dept = strtrim(dept); | |
226 | e.title = strtrim(title); | |
469f80df | 227 | |
228 | e.class = "MITS"; | |
229 | if (!strcmp(e.dept, "PROJECT ATHENA")) | |
230 | e.class = "STAFF"; | |
e1695a1c | 231 | else if (substr(e.title, "PROF") || substr(e.title, "LECTURE")) |
469f80df | 232 | e.class = "FACULTY"; |
233 | else if (!strcmp(e.title, "VISITING SCIENTIST")) | |
234 | e.class = "VSCIENTI"; | |
235 | ||
a6e9fead | 236 | strcpy(email, strtrim(username)); |
237 | if (host[0] == '@') | |
238 | strncat(email, strtrim(host)); | |
239 | e.email = email; | |
469f80df | 240 | |
241 | return(&e); | |
242 | } | |
243 | ||
244 | ||
245 | process_entry(e) | |
246 | struct entry *e; | |
02cd9ede | 247 | { |
3e77c6a3 | 248 | int changed, nochange, encrypted; |
469f80df | 249 | char buf[BUFSIZ], *from, *to; |
02cd9ede | 250 | EXEC SQL BEGIN DECLARE SECTION; |
3e77c6a3 | 251 | char *first, *last, *middle, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle; |
252 | char *raddr, *rhphone, *rophone, *prog; | |
253 | char class[9], oaddr[25], ophone[17], dept[128], dfirst[17], dlast[17], dmiddle[17]; | |
254 | int id, status, who; | |
02cd9ede | 255 | EXEC SQL END DECLARE SECTION; |
469f80df | 256 | |
3e77c6a3 | 257 | who = WHO; |
258 | prog = PROG; | |
469f80df | 259 | first = e->first; |
9e4455c2 | 260 | if (strlen(first) > 16) |
261 | first[16] = 0; | |
469f80df | 262 | last = e->last; |
9e4455c2 | 263 | if (strlen(last) > 16) |
264 | last[16] = 0; | |
3e77c6a3 | 265 | middle = e->middle; |
469f80df | 266 | eid = e->eid; |
325821e4 | 267 | sid = e->id; |
469f80df | 268 | id = 0; |
3e77c6a3 | 269 | encrypted = 0; |
325821e4 | 270 | |
271 | /* Get user info */ | |
3e77c6a3 | 272 | EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status |
273 | INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status | |
02cd9ede | 274 | FROM users |
3e77c6a3 | 275 | WHERE clearid = :sid; |
276 | if (sqlfail()) { | |
277 | if (sqlca.sqlcode == SQL_DUPLICATE) { | |
278 | com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last); | |
279 | return; | |
6f1556b3 | 280 | } else if (sqlca.sqlcode == SQL_DEADLOCK) { |
281 | com_err(whoami, MR_DEADLOCK, "looking up user %s", sid); | |
282 | EXEC SQL ROLLBACK; | |
283 | return process_entry(e); | |
3e77c6a3 | 284 | } else |
285 | sqlexit(); | |
286 | } | |
469f80df | 287 | if (id == 0) { |
3e77c6a3 | 288 | EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status |
289 | INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status | |
02cd9ede | 290 | FROM users |
291 | WHERE last = :last and first = :first and clearid = :eid; | |
6f1556b3 | 292 | if (sqlfail()) { |
293 | if (sqlca.sqlcode == SQL_DEADLOCK) { | |
294 | com_err(whoami, MR_DEADLOCK, "looking up user %s", sid); | |
295 | EXEC SQL ROLLBACK; | |
296 | return process_entry(e); | |
297 | } else if (sqlca.sqlcode != SQL_DUPLICATE) | |
298 | sqlexit(); | |
299 | } | |
3e77c6a3 | 300 | encrypted++; |
325821e4 | 301 | if (id == 0) { |
302 | newuser(e); | |
303 | return; | |
304 | } | |
469f80df | 305 | } |
325821e4 | 306 | |
3e77c6a3 | 307 | /* Update class/state if necessary */ |
e1695a1c | 308 | if (strcmp(e->class, strtrim(class)) && |
309 | strcmp(class, "STAFF") && strcmp(class, "SIPB")) { | |
469f80df | 310 | com_err(whoami, 0, "updating class for %s %s from %s to %s", |
311 | first, last, class, e->class); | |
a6e9fead | 312 | if (status == US_NOT_ALLOWED && !strcmp(e->class, "FACULTY")) |
313 | status = US_NO_LOGIN_YET; | |
314 | if (status == US_ENROLL_NOT_ALLOWED && !strcmp(e->class, "FACULTY")) | |
315 | status = US_ENROLLED; | |
469f80df | 316 | strcpy(class, e->class); |
02cd9ede | 317 | EXEC SQL UPDATE users |
318 | SET type = :class, status = :status, modtime = 'now', | |
3e77c6a3 | 319 | modby = :who, modwith = :prog |
02cd9ede | 320 | WHERE users_id = :id; |
3e77c6a3 | 321 | if (sqlca.sqlcode != 0) { |
322 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
323 | exit(1); | |
324 | } | |
325 | } | |
326 | ||
327 | /* Update name if necessary */ | |
328 | if (strcmp(first, strtrim(dfirst)) || | |
329 | strcmp(last, strtrim(dlast)) || | |
330 | strcmp(middle, strtrim(dmiddle))) { | |
331 | com_err(whoami, 0, "updating real name for %s %s", first, last); | |
332 | EXEC SQL UPDATE users | |
333 | SET first = :first, last = :last, middle = :middle, | |
334 | modby = :who, modwith = :prog, modtime = 'now' | |
335 | WHERE users_id = :id; | |
336 | if (sqlca.sqlcode != 0) { | |
337 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
338 | exit(1); | |
339 | } | |
469f80df | 340 | } |
325821e4 | 341 | |
469f80df | 342 | changed = nochange = 0; |
3e77c6a3 | 343 | if (encrypted) changed++; |
469f80df | 344 | strcpy(buf, e->address); |
345 | while (to = index(buf, ',')) | |
346 | *to = ';'; | |
347 | while (to = index(buf, ':')) | |
348 | *to = ';'; | |
349 | if (newfinger) { | |
a6e9fead | 350 | if (oaddr[0] == ' ' && buf[0]) { |
469f80df | 351 | strncpy(oaddr, buf, 16); |
352 | oaddr[16] = 0; | |
353 | changed++; | |
6eb764c5 | 354 | } else if (strncmp(strtrim(oaddr), buf, 15)) |
469f80df | 355 | nochange++; |
356 | } else { | |
357 | if (strncmp(strtrim(oaddr), buf, 15)) | |
358 | changed++; | |
359 | strncpy(oaddr, buf, 16); | |
360 | oaddr[16] = 0; | |
361 | } | |
362 | from = e->phone; | |
363 | to = buf; | |
364 | while (*from) { | |
365 | if (isdigit(*from)) | |
366 | *to++ = *from; | |
367 | from++; | |
368 | } | |
369 | *to = 0; | |
370 | if (newfinger) { | |
371 | if (ophone[0] == ' ') { | |
372 | strncpy(ophone, buf, 16); | |
373 | ophone[16] = 0; | |
6eb764c5 | 374 | } else if (strncmp(strtrim(ophone), buf, 11)) |
469f80df | 375 | nochange++; |
376 | } else { | |
377 | if (strncmp(strtrim(ophone), buf, 11)) | |
378 | changed++; | |
379 | strncpy(ophone, buf, 16); | |
380 | ophone[16] = 0; | |
381 | } | |
382 | FixCase(e->dept); | |
a6e9fead | 383 | FixCase(e->title); |
469f80df | 384 | if (newfinger) { |
385 | if (dept[0] == ' ') { | |
386 | strncpy(dept, e->dept, 12); | |
387 | dept[12] = 0; | |
6eb764c5 | 388 | } else if (strncmp(strtrim(dept), e->dept, 11)) |
469f80df | 389 | nochange++; |
390 | } else { | |
391 | if (strncmp(strtrim(dept), e->dept, 11)) | |
392 | changed++; | |
393 | strncpy(dept, e->dept, 12); | |
394 | dept[12] = 0; | |
395 | } | |
325821e4 | 396 | sid = e->id; |
397 | name = e->name; | |
398 | rdept = e->dept; | |
399 | rtitle = e->title; | |
400 | raddr = e->address; | |
401 | rhphone = e->phone; | |
402 | rophone = e->phone2; | |
469f80df | 403 | if (changed) { |
404 | com_err(whoami, 0, "updating finger for %s %s", first, last); | |
02cd9ede | 405 | EXEC SQL REPEATED UPDATE users |
3e77c6a3 | 406 | SET office_addr = :oaddr, office_phone = :ophone, department = :dept, |
407 | fmodtime = 'now', fmodby = :who, fmodwith = :prog, | |
02cd9ede | 408 | xname = :name, xdept = :rdept, xtitle = :rtitle, |
409 | xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone, | |
3e77c6a3 | 410 | xmodtime = 'now', clearid = :sid |
02cd9ede | 411 | WHERE users_id = :id; |
3e77c6a3 | 412 | if (sqlca.sqlcode != 0) { |
6f1556b3 | 413 | if (sqlca.sqlcode == SQL_DEADLOCK) { |
414 | com_err(whoami, MR_DEADLOCK, "updating user %s", sid); | |
415 | EXEC SQL ROLLBACK; | |
416 | return process_entry(e); | |
417 | } else { | |
418 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
419 | exit(1); | |
420 | } | |
3e77c6a3 | 421 | } |
325821e4 | 422 | } else { |
02cd9ede | 423 | EXEC SQL REPEATED UPDATE users |
424 | SET xname = :name, xdept = :rdept, xtitle = :rtitle, | |
425 | xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone, | |
3e77c6a3 | 426 | xmodtime = 'now', clearid = :sid |
02cd9ede | 427 | WHERE users_id = :id; |
3e77c6a3 | 428 | if (sqlca.sqlcode != 0) { |
6f1556b3 | 429 | if (sqlca.sqlcode == SQL_DEADLOCK) { |
430 | com_err(whoami, MR_DEADLOCK, "updating user %s", sid); | |
431 | EXEC SQL ROLLBACK; | |
432 | return process_entry(e); | |
433 | } else { | |
434 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
435 | exit(1); | |
436 | } | |
3e77c6a3 | 437 | } |
a6e9fead | 438 | } |
02cd9ede | 439 | } |
469f80df | 440 | |
441 | ||
442 | newuser(e) | |
443 | struct entry *e; | |
02cd9ede | 444 | { |
469f80df | 445 | char *from, *to; |
02cd9ede | 446 | EXEC SQL BEGIN DECLARE SECTION; |
447 | int id, uid, st, who; | |
448 | char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog; | |
449 | char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17]; | |
450 | char *rdept, *rhphone, *rophone; | |
451 | EXEC SQL END DECLARE SECTION; | |
452 | ||
453 | who = WHO; | |
454 | prog = PROG; | |
469f80df | 455 | strncpy(oaddr, e->address, 16); |
a6e9fead | 456 | oaddr[16] = 0; |
469f80df | 457 | while (to = index(oaddr, ',')) |
458 | *to = ';'; | |
459 | while (to = index(oaddr, ':')) | |
460 | *to = ';'; | |
461 | from = e->phone; | |
462 | to = ophone; | |
463 | while (*from) { | |
464 | if (isdigit(*from)) | |
465 | *to++ = *from; | |
466 | from++; | |
467 | } | |
468 | *to = 0; | |
a6e9fead | 469 | FixCase(e->dept); |
469f80df | 470 | strncpy(dept, e->dept, 12); |
a6e9fead | 471 | dept[12] = 0; |
469f80df | 472 | |
3e77c6a3 | 473 | id = set_next_users_id(0); |
474 | uid = set_next_uid(1); | |
469f80df | 475 | sprintf(login, "#%d", uid); |
476 | last = e->last; | |
477 | first = e->first; | |
478 | middle = e->middle; | |
469f80df | 479 | class = e->class; |
480 | if (*middle) | |
481 | sprintf(fullname, "%s %s %s", first, middle, last); | |
482 | else | |
483 | sprintf(fullname, "%s %s", first, last); | |
484 | st = US_NOT_ALLOWED; | |
e1695a1c | 485 | if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF")) |
469f80df | 486 | st = US_NO_LOGIN_YET; |
325821e4 | 487 | |
488 | sid = e->id; | |
489 | name = e->name; | |
490 | rdept = e->dept; | |
491 | title = e->title; | |
492 | rhphone = e->phone; | |
493 | rophone = e->phone2; | |
494 | ||
3e77c6a3 | 495 | EXEC SQL REPEATED INSERT INTO users |
02cd9ede | 496 | (login, users_id, uid, shell, last, first, middle, status, |
497 | clearid, type, modtime, modby, modwith, fullname, office_addr, | |
498 | office_phone, department, fmodtime, fmodby, fmodwith, | |
499 | potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime) | |
500 | VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, :st, | |
501 | :sid, :class, 'now', :who, :prog, :fullname, :oaddr, :ophone, | |
502 | :dept, 'now', :who, :prog, 'NONE', :name, :rdept, | |
503 | :title, :oaddr, :rhphone, :rophone, 'now'); | |
3e77c6a3 | 504 | if (sqlca.sqlcode != 0) { |
505 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
506 | exit(1); | |
507 | } else | |
508 | com_err(whoami, 0, "adding user %s %s", e->first, e->last); | |
02cd9ede | 509 | } |
469f80df | 510 | |
511 | ||
3e77c6a3 | 512 | set_next_users_id(limit) |
27736587 | 513 | int limit; |
02cd9ede | 514 | { |
515 | EXEC SQL BEGIN DECLARE SECTION; | |
3e77c6a3 | 516 | int rowcount, flag, value, retval; |
517 | EXEC SQL END DECLARE SECTION; | |
518 | ||
519 | EXEC SQL REPEATED SELECT value INTO :value FROM numvalues | |
520 | WHERE name = 'users_id'; | |
521 | if (sqlfail()) sqlexit(); | |
522 | if (sqlca.sqlerrd[2] != 1) { | |
523 | EXEC SQL ROLLBACK; | |
524 | com_err(whoami, MR_INTERNAL, "values table inconsistancy"); | |
525 | exit(1); | |
526 | } | |
527 | ||
528 | flag = 0; | |
529 | EXEC SQL REPEATED SELECT users_id INTO :flag FROM users | |
530 | WHERE users_id = :value; | |
531 | if (sqlfail()) sqlexit(); | |
532 | if (sqlca.sqlerrd[2] == 0) | |
533 | flag = 0; | |
534 | while (flag) { | |
535 | value++; | |
536 | if (limit && value > MAX_ID_VALUE) | |
537 | value = MIN_ID_VALUE; | |
538 | flag = 0; | |
539 | EXEC SQL REPEATED SELECT users_id INTO :flag FROM users | |
540 | WHERE users_id = :value; | |
541 | if (sqlfail()) sqlexit(); | |
542 | if (sqlca.sqlerrd[2] == 0) | |
543 | flag = 0; | |
544 | } | |
545 | ||
546 | retval = value++; | |
547 | if (limit && value > MAX_ID_VALUE) | |
548 | value = MIN_ID_VALUE; | |
549 | EXEC SQL REPEATED UPDATE numvalues SET value = :value | |
550 | WHERE name = 'users_id'; | |
551 | if (sqlca.sqlcode != 0) { | |
9b2c129e | 552 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); |
553 | exit(1); | |
3e77c6a3 | 554 | } |
555 | return(retval); | |
556 | } | |
557 | ||
558 | set_next_uid(limit) | |
559 | int limit; | |
560 | { | |
561 | EXEC SQL BEGIN DECLARE SECTION; | |
562 | int rowcount, flag, value, retval; | |
02cd9ede | 563 | EXEC SQL END DECLARE SECTION; |
469f80df | 564 | |
3e77c6a3 | 565 | EXEC SQL REPEATED SELECT value INTO :value FROM numvalues |
566 | WHERE name = 'uid'; | |
567 | if (sqlfail()) sqlexit(); | |
02cd9ede | 568 | if (sqlca.sqlerrd[2] != 1) { |
569 | EXEC SQL ROLLBACK; | |
570 | com_err(whoami, MR_INTERNAL, "values table inconsistancy"); | |
571 | exit(1); | |
469f80df | 572 | } |
573 | ||
02cd9ede | 574 | flag = 0; |
3e77c6a3 | 575 | EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value; |
576 | if (sqlfail()) sqlexit(); | |
02cd9ede | 577 | if (sqlca.sqlerrd[2] == 0) |
578 | flag = 0; | |
579 | while (flag) { | |
469f80df | 580 | value++; |
27736587 | 581 | if (limit && value > MAX_ID_VALUE) |
469f80df | 582 | value = MIN_ID_VALUE; |
02cd9ede | 583 | flag = 0; |
3e77c6a3 | 584 | EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value; |
585 | if (sqlfail()) sqlexit(); | |
02cd9ede | 586 | if (sqlca.sqlerrd[2] == 0) |
587 | flag = 0; | |
469f80df | 588 | } |
589 | ||
3e77c6a3 | 590 | retval = value++; |
02cd9ede | 591 | if (limit && value > MAX_ID_VALUE) |
592 | value = MIN_ID_VALUE; | |
3e77c6a3 | 593 | EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid'; |
594 | if (sqlca.sqlcode != 0) { | |
595 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
596 | exit(1); | |
597 | } | |
598 | return(retval); | |
599 | } | |
600 | ||
601 | ||
602 | sqlexit() | |
603 | { | |
6f1556b3 | 604 | if (sqlca.sqlcode == SQL_DEADLOCK) |
605 | com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d", | |
606 | sqlca.sqlcode); | |
607 | else | |
608 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
3e77c6a3 | 609 | EXEC SQL ROLLBACK WORK; |
610 | exit(1); | |
02cd9ede | 611 | } |