]> andersk Git - moira.git/blob - gen/hosts.pc
second code style cleanup: void/void * usage, proper #includes. try to
[moira.git] / gen / hosts.pc
1 /* $Id$
2  *
3  * This generates the hstath.txt hosttable.
4  *
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>.
8  */
9
10 #include <mit-copyright.h>
11 #include <moira.h>
12
13 #include <sys/stat.h>
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
25 EXEC SQL INCLUDE sqlca;
26
27 RCSID("$Header$");
28
29 char *whoami = "hosts.gen";
30 char *db = "moira/moira";
31
32 int main(int argc, char **argv)
33 {
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;
44
45   EXEC SQL CONNECT :db;
46
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);
59             }
60         }
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);
67         }
68     }
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;
78
79   gettimeofday(&now, NULL);
80
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));
87
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;
104         }
105       else
106         name[i - 8] = 0;
107       if ((p = hash_lookup(aliases, id)))
108         {
109           sprintf(buf, "%s,%s", p, name);
110           hash_update(aliases, id, strdup(buf));
111           free(p);
112         }
113       else
114         hash_store(aliases, id, strdup(name));
115     }
116
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);
151         }
152       else
153         fputs("\n", out);
154     }
155
156   EXEC SQL CLOSE x;
157
158   EXEC SQL COMMIT;
159
160   fprintf(out, "; End of automatically generated host table\n");
161   if (fclose(out))
162     {
163       perror("close failed");
164       exit(MR_CCONFIG);
165     }
166   if (outf)
167     fix_file(outf);
168   exit(MR_SUCCESS);
169
170 sqlerr:
171   db_error(sqlca.sqlcode);
172   exit(MR_DBMS_ERR);
173 }
This page took 0.083489 seconds and 5 git commands to generate.