]> andersk Git - moira.git/blame - gen/hosts.pc
cant_fix takes an argument
[moira.git] / gen / hosts.pc
CommitLineData
2289e07e 1/* $Header$
2 *
3 * This generates the hstath.txt hosttable.
4 *
5 * (c) Copyright 1993 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 <stdio.h>
12#include <moira.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <sys/time.h>
16EXEC SQL INCLUDE sqlca;
17
18extern int errno;
19char *whoami = "hosts.gen";
9c04b191 20char *db = "moira/moira";
2289e07e 21
22main(argc, argv)
23int argc;
24char **argv;
25{
26 FILE *out = stdout;
27 char *outf = NULL, outft[64], *p, buf[256];
28 struct stat sb;
29 struct timeval now;
30 int flag1, flag2, i;
31 struct hash *aliases;
32 EXEC SQL BEGIN DECLARE SECTION;
33 int id;
34 char name[65], vendor[33], model[33], os[33], addr[17];
35 EXEC SQL END DECLARE SECTION;
36
9c04b191 37 EXEC SQL CONNECT :db;
2289e07e 38
39 if (argc == 2) {
40 if (stat(argv[1], &sb) == 0) {
41 if (ModDiff(&flag1, "machine", sb.st_mtime) ||
42 ModDiff(&flag2, "subnet", sb.st_mtime))
43 exit(MR_DATE);
44 if (flag1 < 0 && flag2 < 0) {
45 fprintf(stderr, "File %s does not need to be rebuilt.\n",
46 argv[1]);
47 exit(MR_NO_CHANGE);
48 }
49 }
50 outf = argv[1];
51 sprintf(outft, "%s~", outf);
52 if ((out = fopen(outft, "w")) == NULL) {
53 fprintf(stderr, "unable to open %s for output\n", outf);
54 exit(MR_OCONFIG);
55 }
56 } else if (argc != 1) {
57 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
58 exit(MR_ARGS);
59 } else {
60 outf = NULL;
61 }
62
63 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
64
65 gettimeofday(&now, NULL);
4e82e7ca 66
67 fprintf(out, "; MIT Network Host Table\n;\n");
d64f665d 68 fprintf(out, "; \t%cAuthor: $\n", '$');
69 fprintf(out, "; \t%cDate: $\n", '$');
70 fprintf(out, "; \t%cRevision: $\n;\n", '$');
3bc4b180 71 fprintf(out, "; Host table generated by Moira at %s;\n",
4e82e7ca 72 ctime(&now.tv_sec));
2289e07e 73
74 EXEC SQL DECLARE y CURSOR FOR SELECT
75 mach_id, name FROM hostalias;
76 EXEC SQL OPEN y;
77 aliases = create_hash(1001);
78 while (1) {
79 EXEC SQL FETCH y INTO :id, :name;
80 if (sqlca.sqlcode != 0) break;
40c80ef8 81 if (id == 0) continue;
2289e07e 82 if (*strtrim(name) == 0) continue;
83 if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) {
84 fprintf(stderr, "Name %s not in MIT domain\n", name);
85 continue;
86 } else {
87 name[i-8] = 0;
88 }
89 if (p = hash_lookup(aliases, id)) {
90 sprintf(buf, "%s,%s", p, name);
2289e07e 91 hash_update(aliases, id, strsave(buf));
40c80ef8 92 free(p);
2289e07e 93 } else
94 hash_store(aliases, id, strsave(name));
95 }
96
97 EXEC SQL DECLARE x CURSOR FOR SELECT
98 name, mach_id, vendor, model, os, address
99 FROM machine WHERE status=1 ORDER BY address;
100 EXEC SQL OPEN x;
101 while (1) {
102 EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr;
103 if (sqlca.sqlcode != 0) break;
40c80ef8 104 if (id == 0) continue;
2289e07e 105 if (*strtrim(name) == 0) continue;
106 if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) {
2289e07e 107 continue;
108 } else {
109 name[i-8] = 0;
110 }
111 strtrim(vendor);
112 strtrim(model);
113 strtrim(os);
114 strtrim(addr);
b61b1212 115 if (*addr == 0 || inet_addr(addr) == -1)
2289e07e 116 continue;
117 if (p = hash_lookup(aliases, id))
118 sprintf(buf, "%s,%s", name, p);
119 else
120 strcpy(buf, name);
121 fprintf(out, "HOST : %s : %s : ", addr, buf);
b61b1212 122 if ((*vendor || *model) && *os) {
123 if (*vendor && *model)
124 fprintf(out, "%s/%s : %s :\n", vendor, model, os);
125 else
126 fprintf(out, "%s%s : %s :\n", vendor, model, os);
127 } else
128 fputs("\n", out);
2289e07e 129 }
130
131 EXEC SQL CLOSE x;
9c04b191 132
133 EXEC SQL COMMIT;
2289e07e 134
135 fprintf(out, "; End of automatically generated host table\n");
136 if (fclose(out)) {
137 perror("close failed");
138 exit(MR_CCONFIG);
139 }
140 if (outf)
141 fix_file(outf);
142 exit(MR_SUCCESS);
143
144 sqlerr:
9c04b191 145 db_error(sqlca.sqlcode);
146 exit(MR_DBMS_ERR);
2289e07e 147}
This page took 0.090647 seconds and 5 git commands to generate.