3 * This generates the hstath.txt hosttable.
5 * (c) Copyright 1993-1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
25 EXEC SQL INCLUDE sqlca;
29 char *whoami = "hosts.gen";
30 char *db = "moira/moira";
32 int main(int argc, char **argv)
35 char *outf = NULL, outft[64], *p, buf[256];
40 EXEC SQL BEGIN DECLARE SECTION;
42 char name[65], vendor[33], model[33], os[33], addr[17];
43 EXEC SQL END DECLARE SECTION;
49 if (stat(argv[1], &sb) == 0)
51 if (ModDiff(&flag1, "machine", sb.st_mtime) ||
52 ModDiff(&flag2, "subnet", sb.st_mtime))
54 if (flag1 < 0 && flag2 < 0)
56 fprintf(stderr, "File %s does not need to be rebuilt.\n",
62 sprintf(outft, "%s~", outf);
63 if (!(out = fopen(outft, "w")))
65 fprintf(stderr, "unable to open %s for output\n", outf);
71 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
77 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
79 gettimeofday(&now, NULL);
81 fprintf(out, "; MIT Network Host Table\n;\n");
82 fprintf(out, "; \t%cAuthor: $\n", '$');
83 fprintf(out, "; \t%cDate: $\n", '$');
84 fprintf(out, "; \t%cRevision: $\n;\n", '$');
85 fprintf(out, "; Host table generated by Moira at %s;\n",
88 EXEC SQL DECLARE y CURSOR FOR SELECT mach_id, name FROM hostalias;
90 aliases = create_hash(1001);
93 EXEC SQL FETCH y INTO :id, :name;
100 if ((i = strlen(name)) < 9 || !strchr(name, '.') ||
101 strcmp(strchr(name, '.'), ".MIT.EDU"))
103 fprintf(stderr, "Ignoring alias %s\n", name);
108 if ((p = hash_lookup(aliases, id)))
110 sprintf(buf, "%s,%s", p, name);
111 hash_update(aliases, id, strdup(buf));
115 hash_store(aliases, id, strdup(name));
118 EXEC SQL DECLARE x CURSOR FOR SELECT
119 name, mach_id, vendor, model, os, address
120 FROM machine WHERE status = 1 ORDER BY address;
124 EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr;
131 if ((i = strlen(name)) < 9 || !strchr(name, '.') ||
132 strcmp(strchr(name, '.'), ".MIT.EDU"))
134 fprintf(stderr, "Ignoring machine %s\n", name);
143 if (*addr == 0 || inet_addr(addr) == -1)
145 if ((p = hash_lookup(aliases, id)))
146 sprintf(buf, "%s,%s", name, p);
149 fprintf(out, "HOST : %s : %s : ", addr, buf);
150 if ((*vendor || *model) && *os)
152 if (*vendor && *model)
153 fprintf(out, "%s/%s : %s :\n", vendor, model, os);
155 fprintf(out, "%s%s : %s :\n", vendor, model, os);
165 fprintf(out, "; End of automatically generated host table\n");
168 perror("close failed");
176 db_error(sqlca.sqlcode);