]> andersk Git - moira.git/blame - gen/network.pc
Enable qsort() on all platforms, not jusr WIN32.
[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;
dfaf9b68 33 char *outf = NULL, outft[MAXPATHLEN];
5eaef520 34 struct timeval now;
35 struct in_addr addr;
5eaef520 36 EXEC SQL BEGIN DECLARE SECTION;
37 int id, saddr;
dfaf9b68 38 char name[SUBNET_NAME_SIZE], description[SUBNET_DESCRIPTION_SIZE];
5eaef520 39 EXEC SQL END DECLARE SECTION;
ff4ac895 40
5eaef520 41 EXEC SQL CONNECT :db;
ff4ac895 42
5eaef520 43 if (argc == 2)
44 {
5eaef520 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);
ff4ac895 51 }
ff4ac895 52 }
5eaef520 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;
ff4ac895 62
5eaef520 63 gettimeofday(&now, NULL);
ff4ac895 64
5eaef520 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));
ff4ac895 71
5eaef520 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));
ff4ac895 88 }
89
5eaef520 90 EXEC SQL CLOSE x;
9c04b191 91
5eaef520 92 EXEC SQL COMMIT;
ff4ac895 93
5eaef520 94 fprintf(out, "; End of automatically generated network table\n");
95 if (fclose(out))
96 {
97 perror("close failed");
98 exit(MR_CCONFIG);
ff4ac895 99 }
5eaef520 100 if (outf)
101 fix_file(outf);
102 exit(MR_SUCCESS);
ff4ac895 103
5eaef520 104sqlerr:
105 db_error(sqlca.sqlcode);
106 exit(MR_DBMS_ERR);
ff4ac895 107}
This page took 0.139336 seconds and 5 git commands to generate.