]> andersk Git - moira.git/blame - gen/ndb.pc
Command line printer manipulation client, and build goo.
[moira.git] / gen / ndb.pc
CommitLineData
7ac48069 1/* $Id$
f2dd7e91 2 *
3 * This generates the msql database for the network tables.
4 *
7ac48069 5 * Copyright 1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
f2dd7e91 8 */
9
10#include <mit-copyright.h>
f2dd7e91 11#include <moira.h>
7ac48069 12
f2dd7e91 13#include <sys/stat.h>
7ac48069 14
15#include <stdio.h>
16#include <time.h>
17
18#include "util.h"
19
f2dd7e91 20EXEC SQL INCLUDE sqlca;
21
7ac48069 22RCSID("$Header$");
23
f2dd7e91 24char *whoami = "ndb.gen";
25char *db = "moira/moira";
26
27void users(FILE *out);
28void hosts(FILE *out);
29
30int main(int argc, char **argv)
31{
32 FILE *out = stdout;
dfaf9b68 33 char *outf = NULL, outft[MAXPATHLEN];
f2dd7e91 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
dfaf9b68 64 if (fclose(out) < 0)
f2dd7e91 65 {
66 perror("close failed");
67 exit(MR_CCONFIG);
68 }
69
70 if (outf)
71 fix_file(outf);
72 exit(MR_SUCCESS);
73}
74
75void users(FILE *out)
76{
77 char *c;
78 EXEC SQL BEGIN DECLARE SECTION;
dfaf9b68 79 char login[USERS_LOGIN_SIZE], id[USERS_CLEARID_SIZE];
80 char first[USERS_FIRST_SIZE], middle[USERS_MIDDLE_SIZE];
81 char last[USERS_LAST_SIZE], type[USERS_TYPE_SIZE];
82 int status, users_id;
f2dd7e91 83 EXEC SQL END DECLARE SECTION;
84
85 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
86
dfaf9b68 87 EXEC SQL DECLARE users1 CURSOR FOR
88 SELECT login, clearid, users_id, type, status, first, middle, last
5a7e1e92 89 FROM users WHERE clearid != '0'
dfaf9b68 90 AND login NOT LIKE '#%';
f2dd7e91 91 EXEC SQL OPEN users1;
92 while (1)
93 {
dfaf9b68 94 EXEC SQL FETCH users1 INTO :login, :id, :users_id, :type, :status,
95 :first, :middle, :last;
f2dd7e91 96
97 if (sqlca.sqlcode)
98 break;
99 strtrim(login);
100 strtrim(id);
dfaf9b68 101 strtrim(type);
102 strtrim(first);
103 strtrim(middle);
104 strtrim(last);
105 if (!*id)
106 continue;
107 fprintf(out, "user,%d,%s,%s,%s,%d,%s,%s,%s\n", users_id, id, login,
108 type, status, first, middle, last);
f2dd7e91 109 }
110
111 EXEC SQL CLOSE users1;
112
113 EXEC SQL COMMIT;
114
115 return;
116
117sqlerr:
118 db_error(sqlca.sqlcode);
119 exit(MR_DBMS_ERR);
120}
121
122void hosts(FILE *out)
123{
124 struct hash *users;
125 char *p;
126 int i;
127 EXEC SQL BEGIN DECLARE SECTION;
dfaf9b68 128 char name[MACHINE_NAME_SIZE], owner_type[MACHINE_OWNER_TYPE_SIZE];
129 char addr[MACHINE_ADDRESS_SIZE];
f2dd7e91 130 int id, use, status, owner;
131 EXEC SQL END DECLARE SECTION;
132
133 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
134
135 fprintf(stderr, "aliases...\n");
136
137 EXEC SQL DECLARE hosts1 CURSOR FOR SELECT
138 mach_id, name FROM hostalias;
139 EXEC SQL OPEN hosts1;
140
141 while (1)
142 {
143 EXEC SQL FETCH hosts1 INTO :id, :name;
144 if (sqlca.sqlcode)
145 break;
146 if (id == 0)
147 continue;
148 if (!*strtrim(name))
149 continue;
dfaf9b68 150 if ((i = strlen(name)) < 9 || strcmp(&name[i - 8], ".MIT.EDU"))
f2dd7e91 151 {
152 fprintf(stderr, "Name %s not in MIT domain\n", name);
153 continue;
154 }
155 fprintf(out, "host_alias,%d,%s\n", id, name);
156 }
157
158 EXEC SQL CLOSE hosts1;
159
160 EXEC SQL COMMIT;
161
f2dd7e91 162 fprintf(stderr, "hosts (for real)...\n");
163
164 EXEC SQL DECLARE hosts3 CURSOR FOR SELECT
dfaf9b68 165 name, mach_id, address, use, status, owner_type, owner_id
f2dd7e91 166 FROM machine;
167 EXEC SQL OPEN hosts3;
168 while (1)
169 {
dfaf9b68 170 EXEC SQL FETCH hosts3 INTO :name, :id, :addr, :use, :status,
f2dd7e91 171 :owner_type, :owner;
172 if (sqlca.sqlcode)
173 break;
174 if (id == 0)
175 continue;
176 if (!*strtrim(name))
177 continue;
dfaf9b68 178 if ((i = strlen(name)) < 9 || strcmp(&name[i - 8], ".MIT.EDU"))
f2dd7e91 179 continue;
180 strtrim(addr);
181 strtrim(owner_type);
dfaf9b68 182 fprintf(out, "host,%d,%s,%s,%d,0,%d", id, name, addr, use, status);
f2dd7e91 183 if (!strcmp(owner_type, "USER"))
dfaf9b68 184 fprintf(out, ",USER,%d\n", owner);
f2dd7e91 185 else
186 fprintf(out, ",NONE,0\n");
187 }
188
189 EXEC SQL CLOSE hosts3;
190
191 EXEC SQL COMMIT;
192
193 return;
194sqlerr:
195 db_error(sqlca.sqlcode);
196 exit(MR_DBMS_ERR);
197}
198
199
This page took 2.288042 seconds and 5 git commands to generate.