]> andersk Git - moira.git/blob - gen/hosts.pc
Code style cleanup. (No functional changes)
[moira.git] / gen / hosts.pc
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>
16 EXEC SQL INCLUDE sqlca;
17
18 extern int errno;
19 char *whoami = "hosts.gen";
20 char *db = "moira/moira";
21
22 int main(int argc, char **argv)
23 {
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;
34
35   EXEC SQL CONNECT :db;
36
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);
49             }
50         }
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);
57         }
58     }
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;
68
69   gettimeofday(&now, NULL);
70
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));
77
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;
94         }
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));
105     }
106
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);
141         }
142       else
143         fputs("\n", out);
144     }
145
146   EXEC SQL CLOSE x;
147
148   EXEC SQL COMMIT;
149
150   fprintf(out, "; End of automatically generated host table\n");
151   if (fclose(out))
152     {
153       perror("close failed");
154       exit(MR_CCONFIG);
155     }
156   if (outf)
157     fix_file(outf);
158   exit(MR_SUCCESS);
159
160 sqlerr:
161   db_error(sqlca.sqlcode);
162   exit(MR_DBMS_ERR);
163 }
This page took 0.053639 seconds and 5 git commands to generate.