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[MAXPATHLEN], *p;
40 EXEC SQL BEGIN DECLARE SECTION;
42 char name[MACHINE_NAME_SIZE], vendor[MACHINE_VENDOR_SIZE];
43 char model[MACHINE_MODEL_SIZE], os[MACHINE_OS_SIZE];
44 char addr[MACHINE_ADDRESS_SIZE];
45 EXEC SQL END DECLARE SECTION;
51 if (stat(argv[1], &sb) == 0)
53 if (ModDiff(&flag1, "machine", sb.st_mtime) ||
54 ModDiff(&flag2, "subnet", sb.st_mtime))
56 if (flag1 < 0 && flag2 < 0)
58 fprintf(stderr, "File %s does not need to be rebuilt.\n",
64 sprintf(outft, "%s~", outf);
65 if (!(out = fopen(outft, "w")))
67 fprintf(stderr, "unable to open %s for output\n", outf);
73 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
79 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
81 gettimeofday(&now, NULL);
83 fprintf(out, "; MIT Network Host Table\n;\n");
84 fprintf(out, "; \t%cAuthor: $\n", '$');
85 fprintf(out, "; \t%cDate: $\n", '$');
86 fprintf(out, "; \t%cRevision: $\n;\n", '$');
87 fprintf(out, "; Host table generated by Moira at %s;\n",
90 EXEC SQL DECLARE y CURSOR FOR SELECT mach_id, name FROM hostalias;
92 aliases = create_hash(1001);
95 EXEC SQL FETCH y INTO :id, :name;
102 if ((i = strlen(name)) < 9 || !strchr(name, '.') ||
103 strcmp(strchr(name, '.'), ".MIT.EDU"))
105 fprintf(stderr, "Ignoring alias %s\n", name);
110 if ((p = hash_lookup(aliases, id)))
112 p = realloc(p, strlen(p) + strlen(name) + 2);
113 sprintf(strchr(p, '\0'), ",%s", name);
114 hash_update(aliases, id, p);
117 hash_store(aliases, id, strdup(name));
120 EXEC SQL DECLARE x CURSOR FOR SELECT
121 name, mach_id, vendor, model, os, address
122 FROM machine WHERE status = 1 AND mach_id > 0
127 EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr;
132 if ((i = strlen(name)) < 9 || !strchr(name, '.') ||
133 strcmp(strchr(name, '.'), ".MIT.EDU"))
135 fprintf(stderr, "Ignoring machine %s\n", name);
144 if (*addr == 0 || inet_addr(addr) == -1)
146 fprintf(out, "HOST : %s : %s", addr, name);
147 if ((p = hash_lookup(aliases, id)))
148 fprintf(out, ",%s", p);
149 if ((*vendor || *model) && *os)
151 if (*vendor && *model)
152 fprintf(out, " : %s/%s : %s :\n", vendor, model, os);
154 fprintf(out, " : %s%s : %s :\n", vendor, model, os);
164 fprintf(out, "; End of automatically generated host table\n");
167 perror("close failed");
175 db_error(sqlca.sqlcode);