]> andersk Git - moira.git/blob - gen/mitdir.dc
f393d1827a233acd8e877fd04d843a953c4d38ce
[moira.git] / gen / mitdir.dc
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>
20 EXEC SQL INCLUDE sqlca;
21
22
23 extern int errno;
24 char *whoami = "mitdir.gen";
25
26
27 main(argc, argv)
28 int argc;
29 char **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
43     EXEC SQL CONNECT moira;
44     EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
45 #endsql
46 #ifsql INFORMIX
47     EXEC SQL DATABASE moira;
48 #endsql
49
50     if (argc == 2) {
51         if (stat(argv[1], &sb) == 0) {
52             if (ModDiff (&flag1, "users", sb.st_mtime))
53               exit(MR_DATE);
54             if (flag1 < 0) {
55                 fprintf(stderr, "File %s does not need to be rebuilt.\n",
56                         argv[1]);
57                 exit(MR_NO_CHANGE);
58             }
59         }
60         targetfile = argv[1];
61         sprintf(filename, "%s~", targetfile);
62         if ((out = fopen(filename, "w")) == NULL) {
63             fprintf(stderr, "unable to open %s for output\n", filename);
64             exit(MR_OCONFIG);
65         }
66     } else if (argc != 1) {
67         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
68         exit(MR_ARGS);
69     }
70
71     get_info(out);
72
73 #ifsql INGRES
74     EXEC SQL DISCONNECT;
75 #endsql
76 #ifsql INFORMIX
77     EXEC SQL CLOSE DATABASE;
78 #endsql
79
80     if (fclose(out)) {
81         perror("close failed");
82         exit(MR_CCONFIG);
83     }
84
85     if (argc == 2)
86       fix_file(targetfile);
87     exit(MR_SUCCESS);
88 }
89
90
91 get_info(out)
92 FILE *out;
93 {
94     int mitroom, mitphone;
95     char stuemp, *mhost, *uname, *pob, *tmp;
96     struct hash *strings;
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];
101     char affil[5];
102     EXEC SQL END DECLARE SECTION;
103
104     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
105
106     /* get locks */
107     EXEC SQL SELECT modtime INTO :buf FROM users WHERE users_id = 0;
108
109     strings = create_hash(2000);
110     EXEC SQL DECLARE s_lock CURSOR FOR
111       SELECT string_id, string
112       FROM strings;
113     EXEC SQL OPEN s_lock;
114     while (1) {
115         EXEC SQL FETCH s_lock INTO :id, :name;
116         if (sqlca.sqlcode != 0) break;
117         hash_store(strings, id, strsave(strtrim(name)));
118     }
119     EXEC SQL CLOSE s_lock;
120
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
125       FROM users
126       WHERE status != 3;
127     EXEC SQL OPEN u_cursor1;
128     while (1) {
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;
132
133         if (id == 0)
134           continue;
135
136         strtrim(year);
137         if (atoi(year) || !strcmp(year, "G"))
138           stuemp = 'S';
139         else if (!strcmp(year, "FACULTY") || !strcmp(year, "MITS") ||
140                  !strcmp(year, "STAFF"))
141           stuemp = 'E';
142         else if (!strcmp(year, "PROJECT") || !strcmp(year, "SYSTEM"))
143           continue;
144         else
145           stuemp = '?';
146
147         if (type[0] == 'S' & (pob = hash_lookup(strings, bid)) != NULL) {
148             uname = pob;
149             mhost = index(pob, '@');
150             if (mhost)
151               *mhost++ = 0;
152             else
153               mhost = &uname[strlen(uname) + 1];
154         } else if (type[0] == 'N') {
155             mhost = "";
156             uname = "";
157         } else {
158             mhost = "ATHENA.MIT.EDU";
159             uname = strtrim(name);
160         }
161
162         strtrim(oaddr);
163         if (oaddr[0] != ' ') {
164             tmp = &oaddr[strlen(oaddr) - 1];
165             if (*tmp == 'm' || *tmp == 'M') {
166                 *tmp = 0;
167             }
168           }
169
170         if (tmp = index(ophone, '-')) {
171             bcopy(tmp+1, tmp, strlen(tmp)-1);
172         }
173         if (tmp = index(hphone, '-')) {
174             bcopy(tmp+1, tmp, strlen(tmp)-1);
175         }
176
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);
183         else
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);
189         else
190           fprintf(out, "%s:::::::::%s::0\n",
191                   strtrim(haddr), year);
192       }
193     EXEC SQL CLOSE u_cursor1;
194     return;
195  sqlerr:
196     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
197     critical_alert("DCM", "Mitdir build encountered INGRES ERROR %d",
198                    sqlca.sqlcode);
199     exit(MR_INGRES_ERR);
200 }
This page took 0.347044 seconds and 3 git commands to generate.