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