]> andersk Git - moira.git/blob - gen/ndb.pc
second code style cleanup: void/void * usage, proper #includes. try to
[moira.git] / gen / ndb.pc
1 /* $Id$
2  *
3  * This generates the msql database for the network tables.
4  *
5  * Copyright 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
13 #include <sys/stat.h>
14
15 #include <stdio.h>
16 #include <time.h>
17
18 #include "util.h"
19
20 EXEC SQL INCLUDE sqlca;
21
22 RCSID("$Header$");
23
24 char *whoami = "ndb.gen";
25 char *db = "moira/moira";
26
27 void users(FILE *out);
28 void hosts(FILE *out);
29
30 int main(int argc, char **argv)
31 {
32   FILE *out = stdout;
33   char *outf = NULL, outft[64];
34   struct stat sb;
35   int flag;
36
37   EXEC SQL CONNECT :db;
38
39   if (argc == 2)
40     {
41       outf = argv[1];
42       sprintf(outft, "%s~", outf);
43       if (!(out = fopen(outft, "w")))
44         {
45           fprintf(stderr, "unable to open %s for output\n", outf);
46           exit(MR_OCONFIG);
47         }
48     }
49   else if (argc != 1)
50     {
51       fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
52       exit(MR_ARGS);
53     }
54   else
55     outf = NULL;
56
57   EXEC SQL COMMIT;
58
59   fprintf(stderr, "users...\n");
60   users(out); 
61   fprintf(stderr, "hosts...\n");
62   hosts(out);
63
64   if(fclose(out) < 0)
65     {
66       perror("close failed");
67       exit(MR_CCONFIG);
68     }
69
70   if (outf)
71     fix_file(outf);
72   exit(MR_SUCCESS);
73 }
74
75 void users(FILE *out)
76 {
77   char *c;
78   EXEC SQL BEGIN DECLARE SECTION;
79   char login[9], id[17];
80   EXEC SQL END DECLARE SECTION;
81
82   EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
83
84   EXEC SQL DECLARE users1 CURSOR FOR SELECT login, clearid
85     FROM users WHERE clearid != '0' AND clearid != '999999999' 
86     AND login NOT LIKE '#%';            
87   EXEC SQL OPEN users1;
88   while (1)
89     {
90       EXEC SQL FETCH users1 INTO :login, id;
91
92       if (sqlca.sqlcode)
93         break;
94       strtrim(login);
95       strtrim(id);
96       c = login;
97       while (*c)
98         {
99           if (!isdigit(*c++))
100             break;
101         }
102       fprintf(out, "user,%s,%s\n", id, login);
103     }
104
105   EXEC SQL CLOSE users1;
106
107   EXEC SQL COMMIT;
108
109   return;
110
111 sqlerr:
112   db_error(sqlca.sqlcode);
113   exit(MR_DBMS_ERR);
114 }
115
116 void hosts(FILE *out)
117 {
118   struct hash *users;
119   char *p;
120   int i;
121   EXEC SQL BEGIN DECLARE SECTION;
122   char name[128], mitid[17], owner_type[9], addr[17], inuse[64];
123   int id, use, status, owner;
124   EXEC SQL END DECLARE SECTION;
125
126   EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
127
128   fprintf(stderr, "aliases...\n");
129
130   EXEC SQL DECLARE hosts1 CURSOR FOR SELECT
131     mach_id, name FROM hostalias;
132   EXEC SQL OPEN hosts1;
133
134   while (1)
135     {
136       EXEC SQL FETCH hosts1 INTO :id, :name;
137       if (sqlca.sqlcode)
138         break;
139       if (id == 0)
140         continue;
141       if (!*strtrim(name))
142         continue;
143       if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU"))
144         {
145           fprintf(stderr, "Name %s not in MIT domain\n", name);
146           continue;
147         }
148       fprintf(out, "host_alias,%d,%s\n", id, name);
149     }
150
151   EXEC SQL CLOSE hosts1;
152
153   EXEC SQL COMMIT;
154
155   fprintf(stderr, "users (again)...\n");
156
157   EXEC SQL DECLARE hosts2 CURSOR FOR SELECT
158     users_id, clearid FROM users 
159     WHERE clearid != '0' and clearid !='999999999';
160   EXEC SQL OPEN hosts2;
161   users = create_hash(20001);
162
163   while (1)
164     {
165       EXEC SQL FETCH hosts2 INTO :id, :mitid;
166       if (sqlca.sqlcode)
167         break;
168       if (id == 0)
169         continue;
170       if (!*strtrim(mitid))
171         continue;
172
173       hash_store(users, id, strsave(mitid));
174     }
175
176   EXEC SQL CLOSE hosts2;
177
178   EXEC SQL COMMIT;
179
180   fprintf(stderr, "hosts (for real)...\n");
181
182   EXEC SQL DECLARE hosts3 CURSOR FOR SELECT 
183     name, mach_id, address, use, inuse, status, owner_type, owner_id
184     FROM machine;
185   EXEC SQL OPEN hosts3;
186   while (1)
187     {
188       EXEC SQL FETCH hosts3 INTO :name, :id, :addr, :use, :inuse, :status,
189         :owner_type, :owner;
190       if (sqlca.sqlcode)
191         break;
192       if (id == 0)
193         continue;
194       if (!*strtrim(name))
195         continue;
196       if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU"))
197         continue;
198       strtrim(addr);
199       strtrim(owner_type);
200       strtrim(inuse);
201       fprintf(out, "host,%d,%s,%s,%d,0,%d", id,name,addr,use,status);
202       if (!strcmp(owner_type, "USER")) 
203         {
204           if (p = hash_lookup(users, owner))
205             fprintf(out, ",USER,%s\n", p);
206         }
207       else
208         fprintf(out, ",NONE,0\n");
209     }
210
211   EXEC SQL CLOSE hosts3;
212
213   EXEC SQL COMMIT;
214
215   return;
216 sqlerr:
217   db_error(sqlca.sqlcode);
218   exit(MR_DBMS_ERR);
219 }
220
221
This page took 0.051865 seconds and 5 git commands to generate.