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