]> andersk Git - moira.git/blob - gen/mitdir.dc
666264e0b80f67e6bcec17fcaec5397544d790a2
[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 sms;
44 #endsql
45 #ifsql INFORMIX
46     EXEC SQL DATABASE sms;
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
90 get_info(out)
91 FILE *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
103     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
104
105     /* get locks */
106     EXEC SQL SELECT modtime INTO :buf FROM users WHERE users_id = 0;
107
108     strings = create_hash(2000);
109     EXEC SQL DECLARE s_lock CURSOR FOR
110       SELECT string_id, string
111       FROM strings;
112     EXEC SQL OPEN s_lock;
113     while (1) {
114         EXEC SQL FETCH s_lock INTO :id, :name;
115         if (sqlca.sqlcode != 0) break;
116         hash_store(strings, id, strsave(strtrim(name)));
117     }
118     EXEC SQL CLOSE s_lock;
119
120     EXEC SQL DECLARE u_cursor1 CURSOR FOR
121       SELECT users_id, login, first, middle, last,
122              mit_year, mit_dept, office_addr, office_phone,
123              home_addr, home_phone, potype, box_id
124       FROM users
125       WHERE status != 3;
126     EXEC SQL OPEN u_cursor1;
127     while (1) {
128         EXEC SQL FETCH u_cursor1 INTO :id, :name, :fname, :mname, :lname, 
129                   :year, :dept, :oaddr, :ophone, :haddr, :hphone, :type, :bid;
130         if (sqlca.sqlcode != 0) break;
131
132         if (id == 0)
133           continue;
134
135         strtrim(year);
136         if (atoi(year) || !strcmp(year, "G"))
137           stuemp = 'S';
138         else if (!strcmp(year, "FACULTY") || !strcmp(year, "MITS") ||
139                  !strcmp(year, "STAFF"))
140           stuemp = 'E';
141         else if (!strcmp(year, "PROJECT") || !strcmp(year, "SYSTEM"))
142           continue;
143         else
144           stuemp = '?';
145
146         if (type[0] == 'S' & (pob = hash_lookup(strings, bid)) != NULL) {
147             uname = pob;
148             mhost = index(pob, '@');
149             if (mhost)
150               *mhost++ = 0;
151             else
152               mhost = &uname[strlen(uname) + 1];
153         } else if (type[0] == 'N') {
154             mhost = "";
155             uname = "";
156         } else {
157             mhost = "ATHENA.MIT.EDU";
158             uname = strtrim(name);
159         }
160
161         strtrim(oaddr);
162         if (oaddr[0] != ' ') {
163             tmp = &oaddr[strlen(oaddr) - 1];
164             if (*tmp == 'm' || *tmp == 'M') {
165                 *tmp = 0;
166             }
167           }
168
169         if (tmp = index(ophone, '-')) {
170             bcopy(tmp+1, tmp, strlen(tmp)-1);
171         }
172         if (tmp = index(hphone, '-')) {
173             bcopy(tmp+1, tmp, strlen(tmp)-1);
174         }
175
176         fprintf(out, "%c:%s, %s %s:",
177                 stuemp, strtrim(lname), strtrim(fname), strtrim(mname));
178         fprintf(out, "%s:%s:%s:%s::", lname, fname, mname, oaddr);
179         if (ophone[0] != ' ')
180           fprintf(out, "%010d:::%s:%s:%s:",
181                   atoi(ophone), strtrim(dept), uname, mhost);
182         else
183           fprintf(out, ":::%s:%s:%s:",
184                   strtrim(dept), uname, mhost);
185         if (hphone[0] != ' ')
186           fprintf(out, "%s:::::%010d::::%s::0\n",
187                   strtrim(haddr), atoi(hphone), year);
188         else
189           fprintf(out, "%s:::::::::%s::0\n",
190                   strtrim(haddr), year);
191       }
192     EXEC SQL CLOSE u_cursor1;
193     return;
194  sqlerr:
195     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
196     critical_alert("DCM", "Mitdir build encountered INGRES ERROR %d",
197                    sqlca.sqlcode);
198     exit(MR_INGRES_ERR);
199 }
This page took 0.042872 seconds and 3 git commands to generate.