]> andersk Git - moira.git/blame - gen/network.pc
second code style cleanup: void/void * usage, proper #includes. try to
[moira.git] / gen / network.pc
CommitLineData
7ac48069 1/* $Id$
ff4ac895 2 *
3 * This generates the network table.
4 *
7ac48069 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>.
ff4ac895 8 */
9
10#include <mit-copyright.h>
ff4ac895 11#include <moira.h>
7ac48069 12
ff4ac895 13#include <sys/stat.h>
7ac48069 14
9c04b191 15#include <netinet/in.h>
7ac48069 16#include <arpa/inet.h>
17
18#include <stdio.h>
19#include <time.h>
20
21#include "util.h"
ff4ac895 22
23EXEC SQL INCLUDE sqlca;
24
7ac48069 25RCSID("$Header$");
26
ff4ac895 27char *whoami = "network.gen";
9c04b191 28char *db = "moira/moira";
ff4ac895 29
5eaef520 30int main(int argc, char **argv)
ff4ac895 31{
5eaef520 32 FILE *out = stdout;
7ac48069 33 char *outf = NULL, outft[64];
5eaef520 34 struct stat sb;
35 struct timeval now;
36 struct in_addr addr;
7ac48069 37 int flag1;
5eaef520 38 EXEC SQL BEGIN DECLARE SECTION;
39 int id, saddr;
40 char name[65], description[65];
41 EXEC SQL END DECLARE SECTION;
ff4ac895 42
5eaef520 43 EXEC SQL CONNECT :db;
ff4ac895 44
5eaef520 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 }
ff4ac895 57 }
5eaef520 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);
ff4ac895 64 }
ff4ac895 65 }
5eaef520 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;
ff4ac895 75
5eaef520 76 gettimeofday(&now, NULL);
ff4ac895 77
5eaef520 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));
ff4ac895 84
5eaef520 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));
ff4ac895 101 }
102
5eaef520 103 EXEC SQL CLOSE x;
9c04b191 104
5eaef520 105 EXEC SQL COMMIT;
ff4ac895 106
5eaef520 107 fprintf(out, "; End of automatically generated network table\n");
108 if (fclose(out))
109 {
110 perror("close failed");
111 exit(MR_CCONFIG);
ff4ac895 112 }
5eaef520 113 if (outf)
114 fix_file(outf);
115 exit(MR_SUCCESS);
ff4ac895 116
5eaef520 117sqlerr:
118 db_error(sqlca.sqlcode);
119 exit(MR_DBMS_ERR);
ff4ac895 120}
This page took 0.077583 seconds and 5 git commands to generate.