3 * This generates the input file for the mkdirdb program for the MIT.EDU
6 * (c) Copyright 1989, 1990 by the Massachusetts Institute of Technology.
7 * For copying and distribution information, please see the file
12 #include <mit-copyright.h>
16 #include <moira_site.h>
17 #include <sys/types.h>
20 EXEC SQL INCLUDE sqlca;
24 char *whoami = "mitdir.gen";
33 char filename[64], *targetfile;
35 EXEC SQL BEGIN DECLARE SECTION;
37 EXEC SQL END DECLARE SECTION;
40 initialize_sms_error_table();
43 EXEC SQL CONNECT moira;
44 EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
47 EXEC SQL DATABASE moira;
51 if (stat(argv[1], &sb) == 0) {
52 if (ModDiff (&flag1, "users", sb.st_mtime))
55 fprintf(stderr, "File %s does not need to be rebuilt.\n",
61 sprintf(filename, "%s~", targetfile);
62 if ((out = fopen(filename, "w")) == NULL) {
63 fprintf(stderr, "unable to open %s for output\n", filename);
66 } else if (argc != 1) {
67 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
77 EXEC SQL CLOSE DATABASE;
81 perror("close failed");
94 int mitroom, mitphone;
95 char stuemp, *mhost, *uname, *pob, *tmp;
97 EXEC SQL BEGIN DECLARE SECTION;
98 int id, pid, bid, mid;
99 char name[129], type[9], buf[257], fname[17], mname[17], lname[17];
100 char year[9], dept[13], oaddr[17], ophone[13], haddr[81], hphone[17];
102 EXEC SQL END DECLARE SECTION;
104 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
107 EXEC SQL SELECT modtime INTO :buf FROM users WHERE users_id = 0;
109 strings = create_hash(2000);
110 EXEC SQL DECLARE s_lock CURSOR FOR
111 SELECT string_id, string
113 EXEC SQL OPEN s_lock;
115 EXEC SQL FETCH s_lock INTO :id, :name;
116 if (sqlca.sqlcode != 0) break;
117 hash_store(strings, id, strsave(strtrim(name)));
119 EXEC SQL CLOSE s_lock;
121 EXEC SQL DECLARE u_cursor1 CURSOR FOR
122 SELECT users_id, login, first, middle, last,
123 mit_year, mit_dept, office_addr, office_phone,
124 home_addr, home_phone, potype, box_id
127 EXEC SQL OPEN u_cursor1;
129 EXEC SQL FETCH u_cursor1 INTO :id, :name, :fname, :mname, :lname,
130 :year, :dept, :oaddr, :ophone, :haddr, :hphone, :type, :bid;
131 if (sqlca.sqlcode != 0) break;
137 if (atoi(year) || !strcmp(year, "G"))
139 else if (!strcmp(year, "FACULTY") || !strcmp(year, "MITS") ||
140 !strcmp(year, "STAFF"))
142 else if (!strcmp(year, "PROJECT") || !strcmp(year, "SYSTEM"))
147 if (type[0] == 'S' & (pob = hash_lookup(strings, bid)) != NULL) {
149 mhost = index(pob, '@');
153 mhost = &uname[strlen(uname) + 1];
154 } else if (type[0] == 'N') {
158 mhost = "ATHENA.MIT.EDU";
159 uname = strtrim(name);
163 if (oaddr[0] != ' ') {
164 tmp = &oaddr[strlen(oaddr) - 1];
165 if (*tmp == 'm' || *tmp == 'M') {
170 if (tmp = index(ophone, '-')) {
171 bcopy(tmp+1, tmp, strlen(tmp)-1);
173 if (tmp = index(hphone, '-')) {
174 bcopy(tmp+1, tmp, strlen(tmp)-1);
177 fprintf(out, "%c:%s, %s %s:",
178 stuemp, strtrim(lname), strtrim(fname), strtrim(mname));
179 fprintf(out, "%s:%s:%s:%s::", lname, fname, mname, oaddr);
180 if (ophone[0] != ' ')
181 fprintf(out, "%010d:::%s:%s:%s:",
182 atoi(ophone), strtrim(dept), uname, mhost);
184 fprintf(out, ":::%s:%s:%s:",
185 strtrim(dept), uname, mhost);
186 if (hphone[0] != ' ')
187 fprintf(out, "%s:::::%010d::::%s::0\n",
188 strtrim(haddr), atoi(hphone), year);
190 fprintf(out, "%s:::::::::%s::0\n",
191 strtrim(haddr), year);
193 EXEC SQL CLOSE u_cursor1;
196 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
197 critical_alert("DCM", "Mitdir build encountered INGRES ERROR %d",