]> andersk Git - moira.git/blame - gen/hosts.pc
use CODE=ANSI_C option to proc
[moira.git] / gen / hosts.pc
CommitLineData
2289e07e 1/* $Header$
2 *
3 * This generates the hstath.txt hosttable.
4 *
5 * (c) Copyright 1993 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
8 */
9
10#include <mit-copyright.h>
11#include <stdio.h>
12#include <moira.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <sys/time.h>
16EXEC SQL INCLUDE sqlca;
17
18extern int errno;
19char *whoami = "hosts.gen";
9c04b191 20char *db = "moira/moira";
2289e07e 21
5eaef520 22int main(int argc, char **argv)
2289e07e 23{
5eaef520 24 FILE *out = stdout;
25 char *outf = NULL, outft[64], *p, buf[256];
26 struct stat sb;
27 struct timeval now;
28 int flag1, flag2, i;
29 struct hash *aliases;
30 EXEC SQL BEGIN DECLARE SECTION;
31 int id;
32 char name[65], vendor[33], model[33], os[33], addr[17];
33 EXEC SQL END DECLARE SECTION;
2289e07e 34
5eaef520 35 EXEC SQL CONNECT :db;
2289e07e 36
5eaef520 37 if (argc == 2)
38 {
39 if (stat(argv[1], &sb) == 0)
40 {
41 if (ModDiff(&flag1, "machine", sb.st_mtime) ||
42 ModDiff(&flag2, "subnet", sb.st_mtime))
43 exit(MR_DATE);
44 if (flag1 < 0 && flag2 < 0)
45 {
46 fprintf(stderr, "File %s does not need to be rebuilt.\n",
47 argv[1]);
48 exit(MR_NO_CHANGE);
2289e07e 49 }
50 }
5eaef520 51 outf = argv[1];
52 sprintf(outft, "%s~", outf);
53 if (!(out = fopen(outft, "w")))
54 {
55 fprintf(stderr, "unable to open %s for output\n", outf);
56 exit(MR_OCONFIG);
2289e07e 57 }
2289e07e 58 }
5eaef520 59 else if (argc != 1)
60 {
61 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
62 exit(MR_ARGS);
63 }
64 else
65 outf = NULL;
66
67 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
2289e07e 68
5eaef520 69 gettimeofday(&now, NULL);
2289e07e 70
5eaef520 71 fprintf(out, "; MIT Network Host Table\n;\n");
72 fprintf(out, "; \t%cAuthor: $\n", '$');
73 fprintf(out, "; \t%cDate: $\n", '$');
74 fprintf(out, "; \t%cRevision: $\n;\n", '$');
75 fprintf(out, "; Host table generated by Moira at %s;\n",
76 ctime(&now.tv_sec));
2289e07e 77
5eaef520 78 EXEC SQL DECLARE y CURSOR FOR SELECT mach_id, name FROM hostalias;
79 EXEC SQL OPEN y;
80 aliases = create_hash(1001);
81 while (1)
82 {
83 EXEC SQL FETCH y INTO :id, :name;
84 if (sqlca.sqlcode)
85 break;
86 if (id == 0)
87 continue;
88 if (!*strtrim(name))
89 continue;
90 if ((i = strlen(name)) < 9 || strcmp(&name[i - 8], ".MIT.EDU"))
91 {
92 fprintf(stderr, "Name %s not in MIT domain\n", name);
93 continue;
2289e07e 94 }
5eaef520 95 else
96 name[i - 8] = 0;
97 if (p = hash_lookup(aliases, id))
98 {
99 sprintf(buf, "%s,%s", p, name);
100 hash_update(aliases, id, strsave(buf));
101 free(p);
102 }
103 else
104 hash_store(aliases, id, strsave(name));
2289e07e 105 }
106
5eaef520 107 EXEC SQL DECLARE x CURSOR FOR SELECT
108 name, mach_id, vendor, model, os, address
109 FROM machine WHERE status = 1 ORDER BY address;
110 EXEC SQL OPEN x;
111 while (1)
112 {
113 EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr;
114 if (sqlca.sqlcode)
115 break;
116 if (id == 0)
117 continue;
118 if (!*strtrim(name))
119 continue;
120 if ((i = strlen(name)) < 9 || strcmp(&name[i - 8], ".MIT.EDU"))
121 continue;
122 else
123 name[i - 8] = 0;
124 strtrim(vendor);
125 strtrim(model);
126 strtrim(os);
127 strtrim(addr);
128 if (*addr == 0 || inet_addr(addr) == -1)
129 continue;
130 if ((p = hash_lookup(aliases, id)))
131 sprintf(buf, "%s,%s", name, p);
132 else
133 strcpy(buf, name);
134 fprintf(out, "HOST : %s : %s : ", addr, buf);
135 if ((*vendor || *model) && *os)
136 {
137 if (*vendor && *model)
138 fprintf(out, "%s/%s : %s :\n", vendor, model, os);
139 else
140 fprintf(out, "%s%s : %s :\n", vendor, model, os);
2289e07e 141 }
5eaef520 142 else
143 fputs("\n", out);
2289e07e 144 }
145
5eaef520 146 EXEC SQL CLOSE x;
9c04b191 147
5eaef520 148 EXEC SQL COMMIT;
2289e07e 149
5eaef520 150 fprintf(out, "; End of automatically generated host table\n");
151 if (fclose(out))
152 {
153 perror("close failed");
154 exit(MR_CCONFIG);
2289e07e 155 }
5eaef520 156 if (outf)
157 fix_file(outf);
158 exit(MR_SUCCESS);
2289e07e 159
5eaef520 160sqlerr:
161 db_error(sqlca.sqlcode);
162 exit(MR_DBMS_ERR);
2289e07e 163}
This page took 0.105383 seconds and 5 git commands to generate.