]> andersk Git - moira.git/blob - gen/network.pc
Warn people if they're making a list owned by 'NONE'.
[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 timeval now;
35   struct in_addr addr;
36   EXEC SQL BEGIN DECLARE SECTION;
37   int id, saddr;
38   char name[SUBNET_NAME_SIZE], description[SUBNET_DESCRIPTION_SIZE];
39   EXEC SQL END DECLARE SECTION;
40
41   EXEC SQL CONNECT :db;
42
43   if (argc == 2)
44     {
45       outf = argv[1];
46       sprintf(outft, "%s~", outf);
47       if (!(out = fopen(outft, "w")))
48         {
49           fprintf(stderr, "unable to open %s for output\n", outf);
50           exit(MR_OCONFIG);
51         }
52     }
53   else if (argc != 1)
54     {
55       fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
56       exit(MR_ARGS);
57     }
58   else
59     outf = NULL;
60
61   EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
62
63   gettimeofday(&now, NULL);
64
65   fprintf(out, "; MIT Network Table\n;\n");
66   fprintf(out, "; \t%cAuthor: $\n", '$');
67   fprintf(out, "; \t%cDate: $\n", '$');
68   fprintf(out, "; \t%cRevision: $\n;\n", '$');
69   fprintf(out, "; Network table generated by Moira at %s;\n",
70           ctime(&now.tv_sec));
71
72   EXEC SQL DECLARE x CURSOR FOR SELECT
73     name, snet_id, saddr, description
74     FROM subnet ORDER BY saddr;
75   EXEC SQL OPEN x;
76   while (1)
77     {
78       EXEC SQL FETCH x INTO :name, :id, :saddr, :description;
79       if (sqlca.sqlcode)
80         break;
81       if (id == 0)
82         continue;
83       if (!*strtrim(name))
84         continue;
85       addr.s_addr = htonl(saddr);
86       fprintf(out, "NETWORK : %-16.16s : %-12.12s : %s\n", name,
87               inet_ntoa(addr), strtrim(description));
88     }
89
90   EXEC SQL CLOSE x;
91
92   EXEC SQL COMMIT;
93
94   fprintf(out, "; End of automatically generated network table\n");
95   if (fclose(out))
96     {
97       perror("close failed");
98       exit(MR_CCONFIG);
99     }
100   if (outf)
101     fix_file(outf);
102   exit(MR_SUCCESS);
103
104 sqlerr:
105   db_error(sqlca.sqlcode);
106   exit(MR_DBMS_ERR);
107 }
This page took 0.070511 seconds and 5 git commands to generate.