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 || strcmp(&name[i - 8], ".MIT.EDU"))
102 fprintf(stderr, "Name %s not in MIT domain\n", name);
107 if ((p = hash_lookup(aliases, id)))
109 sprintf(buf, "%s,%s", p, name);
110 hash_update(aliases, id, strdup(buf));
114 hash_store(aliases, id, strdup(name));
117 EXEC SQL DECLARE x CURSOR FOR SELECT
118 name, mach_id, vendor, model, os, address
119 FROM machine WHERE status = 1 ORDER BY address;
123 EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr;
130 if ((i = strlen(name)) < 9 || strcmp(&name[i - 8], ".MIT.EDU"))
138 if (*addr == 0 || inet_addr(addr) == -1)
140 if ((p = hash_lookup(aliases, id)))
141 sprintf(buf, "%s,%s", name, p);
144 fprintf(out, "HOST : %s : %s : ", addr, buf);
145 if ((*vendor || *model) && *os)
147 if (*vendor && *model)
148 fprintf(out, "%s/%s : %s :\n", vendor, model, os);
150 fprintf(out, "%s%s : %s :\n", vendor, model, os);
160 fprintf(out, "; End of automatically generated host table\n");
163 perror("close failed");
171 db_error(sqlca.sqlcode);