]> andersk Git - moira.git/blob - gen/networks.dc
655e1797056ad4b772fa7c70f5c23f151df0cc24
[moira.git] / gen / networks.dc
1 /* $Header$
2  *
3  * This generates the network table.
4  *
5  *  (c) Copyright 1994 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 #include <netinet/in.h>
17
18 EXEC SQL INCLUDE sqlca;
19
20 extern int errno;
21 char *whoami = "network.gen";
22 char *db = "moira/moira";
23
24 main(argc, argv)
25 int argc;
26 char **argv;
27 {
28     FILE *out = stdout;
29     char *outf = NULL, outft[64], *p, buf[256];
30     struct stat sb;
31     struct timeval now;
32     struct in_addr addr;
33     int flag1, i;
34     EXEC SQL BEGIN DECLARE SECTION;
35     int id, saddr;
36     char name[65], description[65];
37     EXEC SQL END DECLARE SECTION;
38
39     EXEC SQL CONNECT :db;
40
41     if (argc == 2) { 
42         if (stat(argv[1], &sb) == 0) { 
43             if (ModDiff(&flag1, "subnet", sb.st_mtime))
44               exit(MR_DATE);
45             if (flag1 < 0) {
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")) == NULL) {
54             fprintf(stderr, "unable to open %s for output\n", outf);
55             exit(MR_OCONFIG);
56         }
57     } else if (argc != 1) {
58         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
59         exit(MR_ARGS);
60     } else {
61         outf = NULL;
62     }
63
64     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
65
66     gettimeofday(&now, NULL);
67     
68     fprintf(out, "; MIT Network Table\n;\n");
69     fprintf(out, "; \t%cAuthor: $\n", '$');
70     fprintf(out, "; \t%cDate: $\n", '$');
71     fprintf(out, "; \t%cRevision: $\n;\n", '$');
72     fprintf(out, "; Network table generated by Moira at %s;\n",
73             ctime(&now.tv_sec));
74
75     EXEC SQL DECLARE x CURSOR FOR SELECT 
76       name, snet_id, saddr, description
77       FROM subnet ORDER BY saddr;
78     EXEC SQL OPEN x;
79     while (1) {
80         EXEC SQL FETCH x INTO :name, :id, :saddr, :description;
81         if (sqlca.sqlcode != 0) break;
82         if (id == 0) continue;
83         if (*strtrim(name) == 0) continue;
84         addr.s_addr=htonl(saddr);
85         fprintf(out, "NETWORK : %-16.16s : %-12.12s : %s\n", name,
86                 inet_ntoa(addr), 
87            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         perror("close failed");
97         exit(MR_CCONFIG);
98     }
99     if (outf)
100       fix_file(outf);
101     exit(MR_SUCCESS);
102
103  sqlerr:
104     db_error(sqlca.sqlcode);
105     exit(MR_DBMS_ERR);
106 }
This page took 0.097822 seconds and 3 git commands to generate.