]> andersk Git - moira.git/blob - gen/network.pc
Use moira_schema.h
[moira.git] / gen / network.pc
1 /* $Id$
2  *
3  * This generates the network table.
4  *
5  * Copyright (C) 1994-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 <time.h>
20
21 #include "util.h"
22
23 EXEC SQL INCLUDE sqlca;
24
25 RCSID("$Header$");
26
27 char *whoami = "network.gen";
28 char *db = "moira/moira";
29
30 int main(int argc, char **argv)
31 {
32   FILE *out = stdout;
33   char *outf = NULL, outft[MAXPATHLEN];
34   struct stat sb;
35   struct timeval now;
36   struct in_addr addr;
37   int flag1;
38   EXEC SQL BEGIN DECLARE SECTION;
39   int id, saddr;
40   char name[SUBNET_NAME_SIZE], description[SUBNET_DESCRIPTION_SIZE];
41   EXEC SQL END DECLARE SECTION;
42
43   EXEC SQL CONNECT :db;
44
45   if (argc == 2)
46     {
47       if (stat(argv[1], &sb) == 0)
48         {
49           if (ModDiff(&flag1, "subnet", sb.st_mtime))
50             exit(MR_DATE);
51           if (flag1 < 0)
52             {
53               fprintf(stderr, "File %s does not need to be rebuilt.\n",
54                       argv[1]);
55               exit(MR_NO_CHANGE);
56             }
57         }
58       outf = argv[1];
59       sprintf(outft, "%s~", outf);
60       if (!(out = fopen(outft, "w")))
61         {
62           fprintf(stderr, "unable to open %s for output\n", outf);
63           exit(MR_OCONFIG);
64         }
65     }
66   else if (argc != 1)
67     {
68       fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
69       exit(MR_ARGS);
70     }
71   else
72     outf = NULL;
73
74   EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
75
76   gettimeofday(&now, NULL);
77
78   fprintf(out, "; MIT Network Table\n;\n");
79   fprintf(out, "; \t%cAuthor: $\n", '$');
80   fprintf(out, "; \t%cDate: $\n", '$');
81   fprintf(out, "; \t%cRevision: $\n;\n", '$');
82   fprintf(out, "; Network table generated by Moira at %s;\n",
83           ctime(&now.tv_sec));
84
85   EXEC SQL DECLARE x CURSOR FOR SELECT
86     name, snet_id, saddr, description
87     FROM subnet ORDER BY saddr;
88   EXEC SQL OPEN x;
89   while (1)
90     {
91       EXEC SQL FETCH x INTO :name, :id, :saddr, :description;
92       if (sqlca.sqlcode)
93         break;
94       if (id == 0)
95         continue;
96       if (!*strtrim(name))
97         continue;
98       addr.s_addr = htonl(saddr);
99       fprintf(out, "NETWORK : %-16.16s : %-12.12s : %s\n", name,
100               inet_ntoa(addr), strtrim(description));
101     }
102
103   EXEC SQL CLOSE x;
104
105   EXEC SQL COMMIT;
106
107   fprintf(out, "; End of automatically generated network table\n");
108   if (fclose(out))
109     {
110       perror("close failed");
111       exit(MR_CCONFIG);
112     }
113   if (outf)
114     fix_file(outf);
115   exit(MR_SUCCESS);
116
117 sqlerr:
118   db_error(sqlca.sqlcode);
119   exit(MR_DBMS_ERR);
120 }
This page took 0.115804 seconds and 5 git commands to generate.