3 * This generates the hstath.txt hosttable.
5 * (c) Copyright 1993 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
13 #include <sys/types.h>
16 EXEC SQL INCLUDE sqlca;
19 char *whoami = "hosts.gen";
27 char *outf = NULL, outft[64], *p, buf[256];
32 EXEC SQL BEGIN DECLARE SECTION;
34 char name[65], vendor[33], model[33], os[33], addr[17];
35 EXEC SQL END DECLARE SECTION;
38 EXEC SQL CONNECT moira;
39 EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
42 EXEC SQL DATABASE moira;
46 if (stat(argv[1], &sb) == 0) {
47 if (ModDiff(&flag1, "machine", sb.st_mtime) ||
48 ModDiff(&flag2, "subnet", sb.st_mtime))
50 if (flag1 < 0 && flag2 < 0) {
51 fprintf(stderr, "File %s does not need to be rebuilt.\n",
57 sprintf(outft, "%s~", outf);
58 if ((out = fopen(outft, "w")) == NULL) {
59 fprintf(stderr, "unable to open %s for output\n", outf);
62 } else if (argc != 1) {
63 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
69 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
71 gettimeofday(&now, NULL);
73 fprintf(out, "; MIT Network Host Table\n;\n");
74 fprintf(out, "; \t%cAuthor: $\n", '$');
75 fprintf(out, "; \t%cDate: $\n", '$');
76 fprintf(out, "; \t%cRevision: $\n;\n", '$');
77 fprintf(out, "; Host table generated by Moira at %s;;\n",
80 EXEC SQL DECLARE y CURSOR FOR SELECT
81 mach_id, name FROM hostalias;
83 aliases = create_hash(1001);
85 EXEC SQL FETCH y INTO :id, :name;
86 if (sqlca.sqlcode != 0) break;
87 if (id == 0) continue;
88 if (*strtrim(name) == 0) continue;
89 if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) {
90 fprintf(stderr, "Name %s not in MIT domain\n", name);
95 if (p = hash_lookup(aliases, id)) {
96 sprintf(buf, "%s,%s", p, name);
97 hash_update(aliases, id, strsave(buf));
100 hash_store(aliases, id, strsave(name));
103 EXEC SQL DECLARE x CURSOR FOR SELECT
104 name, mach_id, vendor, model, os, address
105 FROM machine WHERE status=1 ORDER BY address;
108 EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr;
109 if (sqlca.sqlcode != 0) break;
110 if (id == 0) continue;
111 if (*strtrim(name) == 0) continue;
112 if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) {
121 if (*addr == 0 || inet_addr(addr) == -1)
123 if (p = hash_lookup(aliases, id))
124 sprintf(buf, "%s,%s", name, p);
127 fprintf(out, "HOST : %s : %s : ", addr, buf);
128 if ((*vendor || *model) && *os) {
129 if (*vendor && *model)
130 fprintf(out, "%s/%s : %s :\n", vendor, model, os);
132 fprintf(out, "%s%s : %s :\n", vendor, model, os);
142 EXEC SQL CLOSE DATABASE;
145 fprintf(out, "; End of automatically generated host table\n");
147 perror("close failed");
155 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
157 if (sqlca.sqlcode == 17700 || sqlca.sqlcode == -37000)
160 critical_alert("DCM", "Hosts build encountered INGRES ERROR %d",