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;
39 EXEC SQL BEGIN DECLARE SECTION;
41 char name[MACHINE_NAME_SIZE], vendor[MACHINE_VENDOR_SIZE];
42 char model[MACHINE_MODEL_SIZE], os[MACHINE_OS_SIZE];
43 char addr[MACHINE_ADDRESS_SIZE];
44 EXEC SQL END DECLARE SECTION;
51 sprintf(outft, "%s~", outf);
52 if (!(out = fopen(outft, "w")))
54 fprintf(stderr, "unable to open %s for output\n", outf);
60 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
66 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
68 gettimeofday(&now, NULL);
70 fprintf(out, "; MIT Network Host Table\n;\n");
71 fprintf(out, "; \t%cAuthor: $\n", '$');
72 fprintf(out, "; \t%cDate: $\n", '$');
73 fprintf(out, "; \t%cRevision: $\n;\n", '$');
74 fprintf(out, "; Host table generated by Moira at %s;\n",
77 EXEC SQL DECLARE y CURSOR FOR SELECT mach_id, name FROM hostalias;
79 aliases = create_hash(1001);
82 EXEC SQL FETCH y INTO :id, :name;
89 if ((i = strlen(name)) < 9 || !strchr(name, '.') ||
90 strcmp(strchr(name, '.'), ".MIT.EDU"))
92 fprintf(stderr, "Ignoring alias %s\n", name);
97 if ((p = hash_lookup(aliases, id)))
99 p = realloc(p, strlen(p) + strlen(name) + 2);
100 sprintf(strchr(p, '\0'), ",%s", name);
101 hash_update(aliases, id, p);
104 hash_store(aliases, id, strdup(name));
107 EXEC SQL DECLARE x CURSOR FOR SELECT
108 name, mach_id, vendor, model, os, address
109 FROM machine WHERE status = 1 AND mach_id > 0
114 EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr;
119 if ((i = strlen(name)) < 9 || !strchr(name, '.') ||
120 strcmp(strchr(name, '.'), ".MIT.EDU"))
122 fprintf(stderr, "Ignoring machine %s\n", name);
131 if (*addr == 0 || inet_addr(addr) == -1)
133 fprintf(out, "HOST : %s : %s", addr, name);
134 if ((p = hash_lookup(aliases, id)))
135 fprintf(out, ",%s", p);
136 if ((*vendor || *model) && *os)
138 if (*vendor && *model)
139 fprintf(out, " : %s/%s : %s :\n", vendor, model, os);
141 fprintf(out, " : %s%s : %s :\n", vendor, model, os);
151 fprintf(out, "; End of automatically generated host table\n");
154 perror("close failed");
162 db_error(sqlca.sqlcode);