]>
Commit | Line | Data |
---|---|---|
1 | /* $Header$ | |
2 | */ | |
3 | ||
4 | #include <stdio.h> | |
5 | #include <strings.h> | |
6 | #include <ctype.h> | |
7 | #include <sys/time.h> | |
8 | #include <moira.h> | |
9 | #include <moira_site.h> | |
10 | EXEC SQL INCLUDE sqlca; | |
11 | ||
12 | ||
13 | #define WHO 11859 /* root */ | |
14 | #define PROG "stu-tape" | |
15 | ||
16 | #define MAX_ID_VALUE 31999 | |
17 | #define MIN_ID_VALUE 101 | |
18 | ||
19 | /* File format is: | |
20 | ||
21 | 0-29 name | |
22 | 30-38 id number | |
23 | 50-54 school code | |
24 | 55-79 year | |
25 | 80-109 address | |
26 | 110-124 room | |
27 | 125-144 city | |
28 | 145-158 state | |
29 | 159-168 dorm phone | |
30 | 169-212 home address | |
31 | 213-232 home city | |
32 | 243-251 mit phone (?) | |
33 | */ | |
34 | ||
35 | #define LOC_NAME 0 | |
36 | #define LOC_ID 30 | |
37 | #define LOC_COURSE 50 | |
38 | #define LOC_YEAR 55 | |
39 | #define LOC_ADDRESS 80 | |
40 | #define LOC_DORM_ROOM 110 | |
41 | #define LOC_CITY 125 | |
42 | #define LOC_STATE 145 | |
43 | #define LOC_DPHONE 155 | |
44 | #define LOC_MPHONE 243 | |
45 | ||
46 | #define LEN_NAME 30 | |
47 | #define LEN_ID 9 | |
48 | #define LEN_COURSE 5 | |
49 | #define LEN_YEAR 25 | |
50 | #define LEN_ADDRESS 30 | |
51 | #define LEN_DORM_ROOM 15 | |
52 | #define LEN_CITY 20 | |
53 | #define LEN_STATE 10 | |
54 | #define LEN_DPHONE 12 | |
55 | #define LEN_MPHONE 12 | |
56 | ||
57 | struct entry { | |
58 | char *name; | |
59 | char *last; | |
60 | char *first; | |
61 | char *middle; | |
62 | char *title; | |
63 | char *id; | |
64 | char *eid; | |
65 | char *course; | |
66 | char *year; | |
67 | char *address; | |
68 | char *dorm; | |
69 | char *city; | |
70 | char *state; | |
71 | char *dphone; | |
72 | char *mphone; | |
73 | char *class; | |
74 | }; | |
75 | ||
76 | ||
77 | char *whoami; | |
78 | int newfinger = 0; | |
79 | ||
80 | #define SQL_DUPLICATE -40100 | |
81 | #define SQL_DEADLOCK -49900 | |
82 | #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 100) | |
83 | ||
84 | main(argc, argv) | |
85 | int argc; | |
86 | char **argv; | |
87 | { | |
88 | FILE *in; | |
89 | struct entry *e, *get_next_entry(); | |
90 | int i, wait = 0; | |
91 | char buf[BUFSIZ], *file = NULL; | |
92 | ||
93 | whoami = rindex(argv[0], '/'); | |
94 | if (whoami) | |
95 | whoami++; | |
96 | else | |
97 | whoami = argv[0]; | |
98 | ||
99 | for (i = 1; i < argc; i++) { | |
100 | if (!strcmp(argv[i], "-w")) | |
101 | wait++; | |
102 | else if (!strcmp(argv[i], "-D")) | |
103 | setenv("ING_SET", "set printqry"); | |
104 | else if (!strcmp(argv[i], "-n")) | |
105 | newfinger++; | |
106 | else if (file != NULL) | |
107 | fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami); | |
108 | else | |
109 | file = argv[i]; | |
110 | } | |
111 | ||
112 | in = fopen(file, "r"); | |
113 | if (in == NULL) { | |
114 | fprintf(stderr, "Unable to open %s for input\n", file); | |
115 | exit(1); | |
116 | } | |
117 | ||
118 | setlinebuf(stdout); | |
119 | setlinebuf(stderr); | |
120 | initialize_sms_error_table(); | |
121 | ||
122 | EXEC SQL CONNECT moira; | |
123 | if (sqlca.sqlcode != 0) { | |
124 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
125 | exit(1); | |
126 | } | |
127 | ||
128 | while (e = get_next_entry(in)) { | |
129 | again: | |
130 | process_entry(e); | |
131 | EXEC SQL COMMIT WORK; | |
132 | if (sqlca.sqlcode != 0) { | |
133 | if (sqlca.sqlcode == SQL_DEADLOCK) { | |
134 | com_err(whoami, MR_DEADLOCK, "commiting work"); | |
135 | goto again; | |
136 | } else { | |
137 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
138 | exit(1); | |
139 | } | |
140 | } | |
141 | if (wait) { | |
142 | printf("Next"); | |
143 | fflush(stdout); | |
144 | gets(buf); | |
145 | } | |
146 | } | |
147 | ||
148 | exit(0); | |
149 | } | |
150 | ||
151 | ||
152 | struct entry *get_next_entry(in) | |
153 | FILE *in; | |
154 | { | |
155 | static struct entry e; | |
156 | static char buf[BUFSIZ], eid[16], classbuf[10], titlebuf[12]; | |
157 | static char name[LEN_NAME+1], id[LEN_ID+1], course[LEN_COURSE+1]; | |
158 | static char year[LEN_YEAR+1], address[LEN_ADDRESS+1]; | |
159 | static char dorm_room[LEN_DORM_ROOM+1], city[LEN_CITY+1]; | |
160 | static char state[LEN_STATE+1], dphone[LEN_DPHONE+1], mphone[LEN_MPHONE+1]; | |
161 | static char sname[LEN_NAME+1], title[128]; | |
162 | static int nyear = 0; | |
163 | int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v; | |
164 | char *p; | |
165 | ||
166 | if (nyear == 0) { | |
167 | struct tm *tm; | |
168 | struct timeval tv; | |
169 | ||
170 | gettimeofday(&tv, NULL); | |
171 | tm = localtime(&tv.tv_sec); | |
172 | nyear = tm->tm_year; | |
173 | if (tm->tm_mon > 5) | |
174 | nyear++; | |
175 | } | |
176 | ||
177 | if (fgets(buf, sizeof(buf), in) == NULL) | |
178 | return((struct entry *)NULL); | |
179 | ||
180 | strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0; | |
181 | strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0; | |
182 | strncpy(course, &buf[LOC_COURSE], LEN_COURSE); course[LEN_COURSE] = 0; | |
183 | strncpy(year, &buf[LOC_YEAR], LEN_YEAR); year[LEN_YEAR] = 0; | |
184 | strncpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS); address[LEN_ADDRESS] = 0; | |
185 | strncpy(dorm_room, &buf[LOC_DORM_ROOM], LEN_DORM_ROOM); dorm_room[LEN_DORM_ROOM] = 0; | |
186 | strncpy(city, &buf[LOC_CITY], LEN_CITY); city[LEN_CITY] = 0; | |
187 | strncpy(state, &buf[LOC_STATE], LEN_STATE); state[LEN_STATE] = 0; | |
188 | strncpy(dphone, &buf[LOC_DPHONE], LEN_DPHONE); dphone[LEN_DPHONE] = 0; | |
189 | strncpy(mphone, &buf[LOC_MPHONE], LEN_MPHONE); mphone[LEN_MPHONE] = 0; | |
190 | ||
191 | strcpy(sname, name); | |
192 | e.name = strtrim(sname); | |
193 | p = index(name, ','); | |
194 | if (p) | |
195 | *p = 0; | |
196 | e.last = strtrim(name); | |
197 | if (p) { | |
198 | p++; | |
199 | while (isspace(*p)) | |
200 | p++; | |
201 | e.first = p; | |
202 | if (p = index(e.first, ' ')) { | |
203 | *p = 0; | |
204 | e.first = strtrim(e.first); | |
205 | e.middle = strtrim(p + 1); | |
206 | } else { | |
207 | e.first = strtrim(e.first); | |
208 | e.middle = ""; | |
209 | } | |
210 | } else { | |
211 | e.first = ""; | |
212 | e.middle = ""; | |
213 | } | |
214 | ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0; | |
215 | LookForSt(e.last); | |
216 | LookForO(e.last); | |
217 | LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv, | |
218 | &ends_ii, &ends_v); | |
219 | LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv, | |
220 | &ends_ii, &ends_v); | |
221 | FixCase(e.last); | |
222 | FixCase(e.first); | |
223 | FixCase(e.middle); | |
224 | ||
225 | e.id = id; | |
226 | e.id[LEN_ID] = 0; | |
227 | e.eid = eid; | |
228 | EncryptID(e.eid, e.id, e.first, e.last); | |
229 | ||
230 | e.year = strtrim(year); | |
231 | e.title = title; | |
232 | if (e.year[0] == 'G') { | |
233 | e.class = "G"; | |
234 | sprintf(title, "Grad Student"); | |
235 | } else { | |
236 | e.class = classbuf; | |
237 | sprintf(classbuf, "%d", nyear + 4 - atoi(e.year) + 1900); | |
238 | sprintf(title, "Undergrad (class of %s)", classbuf); | |
239 | } | |
240 | ||
241 | e.course = strtrim(course); | |
242 | e.address = strtrim(address); | |
243 | e.dorm = strtrim(dorm_room); | |
244 | e.city = strtrim(city); | |
245 | e.state = strtrim(state); | |
246 | e.dphone = strtrim(dphone); | |
247 | e.mphone = strtrim(mphone); | |
248 | return(&e); | |
249 | } | |
250 | ||
251 | ||
252 | process_entry(e) | |
253 | struct entry *e; | |
254 | { | |
255 | int changed, nochange, encrypted; | |
256 | char buf[BUFSIZ], *from, *to; | |
257 | EXEC SQL BEGIN DECLARE SECTION; | |
258 | char *first, *last, *middle, *eid, *title, *sid, *name, *rname, *rdept, *rtitle; | |
259 | char *rophone, *rhphone, *prog; | |
260 | char class[9], haddr[128], hphone[33], ophone[33], dept[33], raddr[128]; | |
261 | char dfirst[17], dlast[17], dmiddle[17]; | |
262 | int id, status, who; | |
263 | EXEC SQL END DECLARE SECTION; | |
264 | ||
265 | who = WHO; | |
266 | prog = PROG; | |
267 | first = e->first; | |
268 | if (strlen(first) > 16) | |
269 | first[16] = 0; | |
270 | last = e->last; | |
271 | if (strlen(last) > 16) | |
272 | last[16] = 0; | |
273 | middle = e->middle; | |
274 | eid = e->eid; | |
275 | sid = e->id; | |
276 | id = 0; | |
277 | encrypted = 0; | |
278 | ||
279 | /* Get user info */ | |
280 | EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department | |
281 | INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept | |
282 | FROM users | |
283 | WHERE clearid = :sid; | |
284 | if (sqlfail()) { | |
285 | if (sqlca.sqlcode == SQL_DUPLICATE) { | |
286 | com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last); | |
287 | return; | |
288 | } else if (sqlca.sqlcode == SQL_DEADLOCK) { | |
289 | com_err(whoami, MR_DEADLOCK, "looking up user %s", sid); | |
290 | EXEC SQL ROLLBACK; | |
291 | return process_entry(e); | |
292 | } else | |
293 | sqlexit(); | |
294 | } | |
295 | if (id == 0) { | |
296 | EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department | |
297 | INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept | |
298 | FROM users | |
299 | WHERE last = :last and first = :first and clearid = :eid; | |
300 | if (sqlfail()) { | |
301 | if (sqlca.sqlcode == SQL_DEADLOCK) { | |
302 | com_err(whoami, MR_DEADLOCK, "looking up user %s", sid); | |
303 | EXEC SQL ROLLBACK; | |
304 | return process_entry(e); | |
305 | } else if (sqlca.sqlcode != SQL_DUPLICATE) | |
306 | sqlexit(); | |
307 | } | |
308 | encrypted++; | |
309 | if (id == 0) { | |
310 | newuser(e); | |
311 | return; | |
312 | } | |
313 | } | |
314 | ||
315 | /* See if class changed: if it's different, and the value in the database | |
316 | * is not STAFF or SIPB, then update the database. Since they were on the | |
317 | * students tape, make the account usable. | |
318 | */ | |
319 | if (strcmp(e->class, strtrim(class)) && | |
320 | strcmp(class, "STAFF") && strcmp(class, "SIPB")) { | |
321 | com_err(whoami, 0, "updating class for user %s %s from %s to %s", | |
322 | first, last, class, e->class); | |
323 | if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET; | |
324 | if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED; | |
325 | strcpy(class, e->class); | |
326 | EXEC SQL REPEATED UPDATE users | |
327 | SET type = :class, status = :status, modtime = 'now', | |
328 | modby = :who, modwith = :prog | |
329 | WHERE users_id = :id; | |
330 | if (sqlca.sqlcode != 0) { | |
331 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
332 | exit(1); | |
333 | } | |
334 | } | |
335 | ||
336 | /* Update name if necessary */ | |
337 | if (strcmp(first, strtrim(dfirst)) || | |
338 | strcmp(last, strtrim(dlast)) || | |
339 | strcmp(middle, strtrim(dmiddle))) { | |
340 | com_err(whoami, 0, "updating real name for %s %s", first, last); | |
341 | EXEC SQL UPDATE users | |
342 | SET first = :first, last = :last, middle = :middle, | |
343 | modby = :who, modwith = :prog, modtime = 'now' | |
344 | WHERE users_id = :id; | |
345 | if (sqlca.sqlcode != 0) { | |
346 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
347 | exit(1); | |
348 | } | |
349 | } | |
350 | ||
351 | /* Deal with updating the finger info if necessary */ | |
352 | changed = nochange = 0; | |
353 | if (encrypted) changed++; | |
354 | strcpy(buf, e->address); | |
355 | if (*e->dorm) { | |
356 | strcat(buf, " "); | |
357 | strcat(buf, e->dorm); | |
358 | } | |
359 | if (*e->city) { | |
360 | strcat(buf, " "); | |
361 | strcat(buf, e->city); | |
362 | } | |
363 | FixCase(buf); | |
364 | if (*e->state) { | |
365 | strcat(buf, " "); | |
366 | strcat(buf, e->state); | |
367 | } | |
368 | while (to = index(buf, ',')) | |
369 | *to = ';'; | |
370 | while (to = index(buf, ':')) | |
371 | *to = ';'; | |
372 | if (newfinger) { | |
373 | if (haddr[0] == ' ') { | |
374 | strncpy(haddr, buf, 80); | |
375 | haddr[80] = 0; | |
376 | changed++; | |
377 | } else if (strncmp(strtrim(haddr), buf, 80)) | |
378 | nochange++; | |
379 | } else { | |
380 | if (strncmp(strtrim(haddr), buf, 80)) | |
381 | changed++; | |
382 | strncpy(haddr, buf, 80); | |
383 | haddr[80] = 0; | |
384 | } | |
385 | from = e->dphone; | |
386 | to = buf; | |
387 | while (*from) { | |
388 | if (isdigit(*from)) | |
389 | *to++ = *from; | |
390 | from++; | |
391 | } | |
392 | *to = 0; | |
393 | if (newfinger) { | |
394 | if (hphone[0] == ' ') { | |
395 | strncpy(hphone, buf, 16); | |
396 | hphone[16] = 0; | |
397 | } else if (strncmp(strtrim(hphone), buf, 16)) | |
398 | nochange++; | |
399 | } else { | |
400 | if (strncmp(strtrim(hphone), buf, 16)) | |
401 | changed++; | |
402 | strncpy(hphone, buf, 16); | |
403 | hphone[16] = 0; | |
404 | } | |
405 | from = e->mphone; | |
406 | to = buf; | |
407 | while (*from) { | |
408 | if (isdigit(*from)) | |
409 | *to++ = *from; | |
410 | from++; | |
411 | } | |
412 | *to = 0; | |
413 | if (newfinger) { | |
414 | if (ophone[0] == ' ') { | |
415 | strncpy(ophone, buf, 12); | |
416 | ophone[12] = 0; | |
417 | } else if (strncmp(strtrim(ophone), buf, 12)) | |
418 | nochange++; | |
419 | } else { | |
420 | if (strncmp(strtrim(ophone), buf, 12)) | |
421 | changed++; | |
422 | strncpy(ophone, buf, 12); | |
423 | ophone[12] = 0; | |
424 | } | |
425 | e->course = e->course; | |
426 | if (newfinger) { | |
427 | if (dept[0] == ' ') { | |
428 | strncpy(dept, e->course, 12); | |
429 | dept[12] = 0; | |
430 | } else if (strncmp(strtrim(dept), e->course, 11)) | |
431 | nochange++; | |
432 | } else { | |
433 | if (strncmp(strtrim(dept), e->course, 11)) | |
434 | changed++; | |
435 | strncpy(dept, e->course, 12); | |
436 | dept[12] = 0; | |
437 | } | |
438 | sid = e->id; | |
439 | name = e->name; | |
440 | rdept = e->course; | |
441 | rtitle = e->title; | |
442 | rophone = e->mphone; | |
443 | rhphone = e->dphone; | |
444 | strcpy(raddr, e->address); | |
445 | if (*e->dorm) { | |
446 | strcat(raddr, " "); | |
447 | strcat(raddr, e->dorm); | |
448 | } | |
449 | strcat(raddr, "|"); | |
450 | if (*e->city) { | |
451 | strcat(raddr, e->city); | |
452 | FixCase(raddr); | |
453 | if (*e->state) { | |
454 | strcat(raddr, " "); | |
455 | if (isupper(e->state[0]) && isupper(e->state[1]) && | |
456 | isdigit(e->state[2]) && isdigit(e->state[3]) && | |
457 | isdigit(e->state[4]) && isdigit(e->state[5]) && | |
458 | isdigit(e->state[6])) { | |
459 | sprintf(buf, "%c%c %s", e->state[0],e->state[1], | |
460 | &(e->state[2])); | |
461 | strcat(raddr, buf); | |
462 | } else | |
463 | strcat(raddr, e->state); | |
464 | } | |
465 | } else { | |
466 | FixCase(raddr); | |
467 | strcat(raddr, "MIT INTERDEPARTMENTAL MAIL"); | |
468 | } | |
469 | if (changed) { | |
470 | com_err(whoami, 0, "updating finger for %s %s", first, last); | |
471 | EXEC SQL REPEATED UPDATE users | |
472 | SET home_addr = :haddr, home_phone = :hphone, | |
473 | office_phone = :ophone, department = :dept, | |
474 | fmodtime = 'now', fmodby = :who, fmodwith = :prog, | |
475 | xname = :name, xdept = :rdept, xtitle = :rtitle, | |
476 | xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone, | |
477 | xmodtime = date('now'), clearid = :sid | |
478 | WHERE users_id = :id; | |
479 | if (sqlca.sqlcode != 0) { | |
480 | if (sqlca.sqlcode == SQL_DEADLOCK) { | |
481 | com_err(whoami, MR_DEADLOCK, "updating user %s", sid); | |
482 | EXEC SQL ROLLBACK; | |
483 | return process_entry(e); | |
484 | } else { | |
485 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
486 | exit(1); | |
487 | } | |
488 | } | |
489 | } else { | |
490 | EXEC SQL REPEATED UPDATE users | |
491 | SET xname = :name, xdept = :rdept, xtitle = :rtitle, | |
492 | xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone, | |
493 | xmodtime = date('now'), clearid = :sid | |
494 | WHERE users_id = :id; | |
495 | if (sqlca.sqlcode != 0) { | |
496 | if (sqlca.sqlcode == SQL_DEADLOCK) { | |
497 | com_err(whoami, MR_DEADLOCK, "updating user %s", sid); | |
498 | EXEC SQL ROLLBACK; | |
499 | return process_entry(e); | |
500 | } else { | |
501 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
502 | exit(1); | |
503 | } | |
504 | } | |
505 | } | |
506 | } | |
507 | ||
508 | ||
509 | newuser(e) | |
510 | struct entry *e; | |
511 | { | |
512 | char buf[512], *from, *to; | |
513 | EXEC SQL BEGIN DECLARE SECTION; | |
514 | int id, uid, who; | |
515 | char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog; | |
516 | char haddr[81], hphone[17], ophone[13], dept[24], *title, raddr[81], *name; | |
517 | EXEC SQL END DECLARE SECTION; | |
518 | ||
519 | who = WHO; | |
520 | prog = PROG; | |
521 | strcpy(buf, e->address); | |
522 | if (*e->dorm) { | |
523 | strcat(buf, " "); | |
524 | strcat(buf, e->dorm); | |
525 | } | |
526 | if (*e->city) { | |
527 | strcat(buf, " "); | |
528 | strcat(buf, e->city); | |
529 | } | |
530 | if (*e->state) { | |
531 | strcat(buf, " "); | |
532 | strcat(buf, e->state); | |
533 | } | |
534 | strncpy(haddr, buf, 80); | |
535 | from = e->dphone; | |
536 | to = buf; | |
537 | while (*from) { | |
538 | if (isdigit(*from)) | |
539 | *to++ = *from; | |
540 | from++; | |
541 | } | |
542 | *to = 0; | |
543 | strncpy(hphone, buf, 16); | |
544 | from = e->mphone; | |
545 | to = buf; | |
546 | while (*from) { | |
547 | if (isdigit(*from)) | |
548 | *to++ = *from; | |
549 | from++; | |
550 | } | |
551 | *to = 0; | |
552 | strncpy(ophone, buf, 12); | |
553 | strncpy(dept, e->course, 12); | |
554 | ||
555 | id = set_next_users_id(0); | |
556 | uid = set_next_uid(1); | |
557 | sprintf(login, "#%d", uid); | |
558 | last = e->last; | |
559 | first = e->first; | |
560 | middle = e->middle; | |
561 | sid = e->id; | |
562 | class = e->class; | |
563 | title = e->title; | |
564 | if (*middle) | |
565 | sprintf(fullname, "%s %s %s", first, middle, last); | |
566 | else | |
567 | sprintf(fullname, "%s %s", first, last); | |
568 | name = e->name; | |
569 | strcpy(raddr, e->address); | |
570 | if (*e->dorm) { | |
571 | strcat(raddr, " "); | |
572 | strcat(raddr, e->dorm); | |
573 | } | |
574 | strcat(raddr, "|"); | |
575 | if (*e->city) { | |
576 | strcat(raddr, e->city); | |
577 | FixCase(raddr); | |
578 | if (*e->state) { | |
579 | strcat(raddr, " "); | |
580 | if (isupper(e->state[0]) && isupper(e->state[1]) && | |
581 | isdigit(e->state[2]) && isdigit(e->state[3]) && | |
582 | isdigit(e->state[4]) && isdigit(e->state[5]) && | |
583 | isdigit(e->state[6])) { | |
584 | sprintf(buf, "%c%c %s", e->state[0],e->state[1], | |
585 | &(e->state[2])); | |
586 | strcat(raddr, buf); | |
587 | } else | |
588 | strcat(raddr, e->state); | |
589 | } | |
590 | } else { | |
591 | FixCase(raddr); | |
592 | strcat(raddr, "MIT INTERDEPARTMENTAL MAIL"); | |
593 | } | |
594 | EXEC SQL REPEATED INSERT INTO users | |
595 | (login, users_id, uid, shell, last, first, middle, status, | |
596 | clearid, type, modtime, modby, modwith, fullname, home_addr, | |
597 | home_phone, office_phone, department, fmodtime, fmodby, fmodwith, | |
598 | potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime) | |
599 | VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, 0, | |
600 | :sid, :class, 'now', :who, :prog, :fullname, :haddr, :hphone, | |
601 | :ophone, :dept, 'now', :who, :prog, 'NONE', :name, :dept, | |
602 | :title, :raddr, :hphone, :ophone, date('now')); | |
603 | if (sqlca.sqlcode != 0) { | |
604 | if (sqlca.sqlcode == SQL_DEADLOCK) { | |
605 | com_err(whoami, MR_DEADLOCK, "adding user %s", sid); | |
606 | EXEC SQL ROLLBACK; | |
607 | return newuser(e); | |
608 | } else { | |
609 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
610 | exit(1); | |
611 | } | |
612 | } else | |
613 | com_err(whoami, 0, "adding user %s %s", e->first, e->last); | |
614 | } | |
615 | ||
616 | ||
617 | ||
618 | set_next_users_id(limit) | |
619 | int limit; | |
620 | { | |
621 | EXEC SQL BEGIN DECLARE SECTION; | |
622 | int rowcount, flag, value, retval; | |
623 | EXEC SQL END DECLARE SECTION; | |
624 | ||
625 | EXEC SQL REPEATED SELECT value INTO :value FROM numvalues | |
626 | WHERE name = 'users_id'; | |
627 | if (sqlfail()) sqlexit(); | |
628 | if (sqlca.sqlerrd[2] != 1) { | |
629 | EXEC SQL ROLLBACK; | |
630 | com_err(whoami, MR_INTERNAL, "values table inconsistancy"); | |
631 | exit(1); | |
632 | } | |
633 | ||
634 | flag = 0; | |
635 | EXEC SQL REPEATED SELECT users_id INTO :flag FROM users | |
636 | WHERE users_id = :value; | |
637 | if (sqlfail()) sqlexit(); | |
638 | if (sqlca.sqlerrd[2] == 0) | |
639 | flag = 0; | |
640 | while (flag) { | |
641 | value++; | |
642 | if (limit && value > MAX_ID_VALUE) | |
643 | value = MIN_ID_VALUE; | |
644 | flag = 0; | |
645 | EXEC SQL REPEATED SELECT users_id INTO :flag FROM users | |
646 | WHERE users_id = :value; | |
647 | if (sqlfail()) sqlexit(); | |
648 | if (sqlca.sqlerrd[2] == 0) | |
649 | flag = 0; | |
650 | } | |
651 | ||
652 | retval = value++; | |
653 | if (limit && value > MAX_ID_VALUE) | |
654 | value = MIN_ID_VALUE; | |
655 | EXEC SQL REPEATED UPDATE numvalues SET value = :value | |
656 | WHERE name = 'users_id'; | |
657 | if (sqlca.sqlcode != 0) { | |
658 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
659 | exit(1); | |
660 | } | |
661 | return(retval); | |
662 | } | |
663 | ||
664 | set_next_uid(limit) | |
665 | int limit; | |
666 | { | |
667 | EXEC SQL BEGIN DECLARE SECTION; | |
668 | int rowcount, flag, value, retval; | |
669 | EXEC SQL END DECLARE SECTION; | |
670 | ||
671 | EXEC SQL REPEATED SELECT value INTO :value FROM numvalues | |
672 | WHERE name = 'uid'; | |
673 | if (sqlfail()) sqlexit(); | |
674 | if (sqlca.sqlerrd[2] != 1) { | |
675 | EXEC SQL ROLLBACK; | |
676 | com_err(whoami, MR_INTERNAL, "values table inconsistancy"); | |
677 | exit(1); | |
678 | } | |
679 | ||
680 | flag = 0; | |
681 | EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value; | |
682 | if (sqlfail()) sqlexit(); | |
683 | if (sqlca.sqlerrd[2] == 0) | |
684 | flag = 0; | |
685 | while (flag) { | |
686 | value++; | |
687 | if (limit && value > MAX_ID_VALUE) | |
688 | value = MIN_ID_VALUE; | |
689 | flag = 0; | |
690 | EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value; | |
691 | if (sqlfail()) sqlexit(); | |
692 | if (sqlca.sqlerrd[2] == 0) | |
693 | flag = 0; | |
694 | } | |
695 | ||
696 | retval = value++; | |
697 | if (limit && value > MAX_ID_VALUE) | |
698 | value = MIN_ID_VALUE; | |
699 | EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid'; | |
700 | if (sqlca.sqlcode != 0) { | |
701 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
702 | exit(1); | |
703 | } | |
704 | return(retval); | |
705 | } | |
706 | ||
707 | ||
708 | sqlexit() | |
709 | { | |
710 | if (sqlca.sqlcode == SQL_DEADLOCK) | |
711 | com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d", | |
712 | sqlca.sqlcode); | |
713 | else | |
714 | com_err(whoami, 0, "ingres error %d", sqlca.sqlcode); | |
715 | EXEC SQL ROLLBACK WORK; | |
716 | exit(1); | |
717 | } |