]> andersk Git - moira.git/blame - gen/network.pc
.dc -> .pc
[moira.git] / gen / network.pc
CommitLineData
ff4ac895 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>
9c04b191 16#include <netinet/in.h>
ff4ac895 17
18EXEC SQL INCLUDE sqlca;
19
20extern int errno;
21char *whoami = "network.gen";
9c04b191 22char *db = "moira/moira";
ff4ac895 23
24main(argc, argv)
25int argc;
26char **argv;
27{
28 FILE *out = stdout;
29 char *outf = NULL, outft[64], *p, buf[256];
30 struct stat sb;
31 struct timeval now;
9c04b191 32 struct in_addr addr;
ff4ac895 33 int flag1, i;
34 EXEC SQL BEGIN DECLARE SECTION;
9c04b191 35 int id, saddr;
ff4ac895 36 char name[65], description[65];
37 EXEC SQL END DECLARE SECTION;
38
9c04b191 39 EXEC SQL CONNECT :db;
ff4ac895 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) {
9c04b191 80 EXEC SQL FETCH x INTO :name, :id, :saddr, :description;
ff4ac895 81 if (sqlca.sqlcode != 0) break;
82 if (id == 0) continue;
83 if (*strtrim(name) == 0) continue;
9c04b191 84 addr.s_addr=htonl(saddr);
85 fprintf(out, "NETWORK : %-16.16s : %-12.12s : %s\n", name,
86 inet_ntoa(addr),
87 strtrim(description));
ff4ac895 88 }
89
90 EXEC SQL CLOSE x;
9c04b191 91
92 EXEC SQL COMMIT;
ff4ac895 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:
9c04b191 104 db_error(sqlca.sqlcode);
105 exit(MR_DBMS_ERR);
ff4ac895 106}
This page took 0.060412 seconds and 5 git commands to generate.