]> andersk Git - moira.git/blame - gen/mitdir.dc
New database and column names for Moira2.
[moira.git] / gen / mitdir.dc
CommitLineData
f852c398 1/* $Header$
2 *
3 * This generates the input file for the mkdirdb program for the MIT.EDU
4 * mailhub.
5 *
6 * (c) Copyright 1989, 1990 by the Massachusetts Institute of Technology.
7 * For copying and distribution information, please see the file
8 * <mit-copyright.h>.
9 */
10
11
12#include <mit-copyright.h>
13#include <stdio.h>
14#include <string.h>
15#include <moira.h>
16#include <moira_site.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <sys/time.h>
20EXEC SQL INCLUDE sqlca;
21
22
23extern int errno;
24char *whoami = "mitdir.gen";
25
26
27main(argc, argv)
28int argc;
29char **argv;
30{
31 long tm = time(NULL);
32 FILE *out= stdout;
33 char filename[64], *targetfile;
34 struct stat sb;
35 EXEC SQL BEGIN DECLARE SECTION;
36 int flag1;
37 EXEC SQL END DECLARE SECTION;
38 int ingerr();
39
40 initialize_sms_error_table();
41
42#ifsql INGRES
9fd1c2b8 43 EXEC SQL CONNECT moira;
f852c398 44#endsql
45#ifsql INFORMIX
9fd1c2b8 46 EXEC SQL DATABASE moira;
f852c398 47#endsql
48
49 if (argc == 2) {
50 if (stat(argv[1], &sb) == 0) {
51 if (ModDiff (&flag1, "users", sb.st_mtime))
52 exit(MR_DATE);
53 if (flag1 < 0) {
54 fprintf(stderr, "File %s does not need to be rebuilt.\n",
55 argv[1]);
56 exit(MR_NO_CHANGE);
57 }
58 }
59 targetfile = argv[1];
60 sprintf(filename, "%s~", targetfile);
61 if ((out = fopen(filename, "w")) == NULL) {
62 fprintf(stderr, "unable to open %s for output\n", filename);
63 exit(MR_OCONFIG);
64 }
65 } else if (argc != 1) {
66 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
67 exit(MR_ARGS);
68 }
69
70 get_info(out);
71
72#ifsql INGRES
73 EXEC SQL DISCONNECT;
74#endsql
75#ifsql INFORMIX
76 EXEC SQL CLOSE DATABASE;
77#endsql
78
79 if (fclose(out)) {
80 perror("close failed");
81 exit(MR_CCONFIG);
82 }
83
84 if (argc == 2)
85 fix_file(targetfile);
86 exit(MR_SUCCESS);
87}
88
89
90get_info(out)
91FILE *out;
92{
93 int mitroom, mitphone;
94 char stuemp, *mhost, *uname, *pob, *tmp;
95 struct hash *strings;
96 EXEC SQL BEGIN DECLARE SECTION;
97 int id, pid, bid, mid;
98 char name[129], type[9], buf[257], fname[17], mname[17], lname[17];
99 char year[9], dept[13], oaddr[17], ophone[13], haddr[81], hphone[17];
100 char affil[5];
101 EXEC SQL END DECLARE SECTION;
102
9fd1c2b8 103 /* The following is declarative, not executed,
104 * and so is dependent on where it is in the file,
105 * not in the order of execution of statements.
106 */
f852c398 107 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
108
109 /* get locks */
110 EXEC SQL SELECT modtime INTO :buf FROM users WHERE users_id = 0;
111
112 strings = create_hash(2000);
113 EXEC SQL DECLARE s_lock CURSOR FOR
114 SELECT string_id, string
115 FROM strings;
116 EXEC SQL OPEN s_lock;
117 while (1) {
118 EXEC SQL FETCH s_lock INTO :id, :name;
119 if (sqlca.sqlcode != 0) break;
120 hash_store(strings, id, strsave(strtrim(name)));
121 }
122 EXEC SQL CLOSE s_lock;
123
124 EXEC SQL DECLARE u_cursor1 CURSOR FOR
125 SELECT users_id, login, first, middle, last,
9fd1c2b8 126 affiliation, department, office_addr, office_phone,
f852c398 127 home_addr, home_phone, potype, box_id
128 FROM users
129 WHERE status != 3;
130 EXEC SQL OPEN u_cursor1;
131 while (1) {
132 EXEC SQL FETCH u_cursor1 INTO :id, :name, :fname, :mname, :lname,
133 :year, :dept, :oaddr, :ophone, :haddr, :hphone, :type, :bid;
134 if (sqlca.sqlcode != 0) break;
135
136 if (id == 0)
137 continue;
138
139 strtrim(year);
140 if (atoi(year) || !strcmp(year, "G"))
141 stuemp = 'S';
142 else if (!strcmp(year, "FACULTY") || !strcmp(year, "MITS") ||
143 !strcmp(year, "STAFF"))
144 stuemp = 'E';
145 else if (!strcmp(year, "PROJECT") || !strcmp(year, "SYSTEM"))
146 continue;
147 else
148 stuemp = '?';
149
150 if (type[0] == 'S' & (pob = hash_lookup(strings, bid)) != NULL) {
151 uname = pob;
152 mhost = index(pob, '@');
153 if (mhost)
154 *mhost++ = 0;
155 else
156 mhost = &uname[strlen(uname) + 1];
157 } else if (type[0] == 'N') {
158 mhost = "";
159 uname = "";
160 } else {
161 mhost = "ATHENA.MIT.EDU";
162 uname = strtrim(name);
163 }
164
165 strtrim(oaddr);
166 if (oaddr[0] != ' ') {
167 tmp = &oaddr[strlen(oaddr) - 1];
168 if (*tmp == 'm' || *tmp == 'M') {
169 *tmp = 0;
170 }
171 }
172
173 if (tmp = index(ophone, '-')) {
174 bcopy(tmp+1, tmp, strlen(tmp)-1);
175 }
176 if (tmp = index(hphone, '-')) {
177 bcopy(tmp+1, tmp, strlen(tmp)-1);
178 }
179
180 fprintf(out, "%c:%s, %s %s:",
181 stuemp, strtrim(lname), strtrim(fname), strtrim(mname));
182 fprintf(out, "%s:%s:%s:%s::", lname, fname, mname, oaddr);
183 if (ophone[0] != ' ')
184 fprintf(out, "%010d:::%s:%s:%s:",
185 atoi(ophone), strtrim(dept), uname, mhost);
186 else
187 fprintf(out, ":::%s:%s:%s:",
188 strtrim(dept), uname, mhost);
189 if (hphone[0] != ' ')
190 fprintf(out, "%s:::::%010d::::%s::0\n",
191 strtrim(haddr), atoi(hphone), year);
192 else
193 fprintf(out, "%s:::::::::%s::0\n",
194 strtrim(haddr), year);
195 }
196 EXEC SQL CLOSE u_cursor1;
197 return;
198 sqlerr:
199 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
200 critical_alert("DCM", "Mitdir build encountered INGRES ERROR %d",
201 sqlca.sqlcode);
202 exit(MR_INGRES_ERR);
203}
This page took 0.090198 seconds and 5 git commands to generate.