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