]>
Commit | Line | Data |
---|---|---|
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> | |
16 | ||
17 | EXEC SQL INCLUDE sqlca; | |
18 | ||
19 | extern int errno; | |
20 | char *whoami = "network.gen"; | |
21 | ||
22 | ||
23 | main(argc, argv) | |
24 | int argc; | |
25 | char **argv; | |
26 | { | |
27 | FILE *out = stdout; | |
28 | char *outf = NULL, outft[64], *p, buf[256]; | |
29 | struct stat sb; | |
30 | struct timeval now; | |
31 | int flag1, i; | |
32 | EXEC SQL BEGIN DECLARE SECTION; | |
33 | int id, addr; | |
34 | char name[65], description[65]; | |
35 | EXEC SQL END DECLARE SECTION; | |
36 | ||
37 | #ifsql INGRES | |
38 | EXEC SQL CONNECT moira; | |
39 | EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED; | |
40 | #endsql | |
41 | #ifsql INFORMIX | |
42 | EXEC SQL DATABASE moira; | |
43 | #endsql | |
44 | ||
45 | if (argc == 2) { | |
46 | if (stat(argv[1], &sb) == 0) { | |
47 | if (ModDiff(&flag1, "subnet", sb.st_mtime)) | |
48 | exit(MR_DATE); | |
49 | if (flag1 < 0) { | |
50 | fprintf(stderr, "File %s does not need to be rebuilt.\n", | |
51 | argv[1]); | |
52 | exit(MR_NO_CHANGE); | |
53 | } | |
54 | } | |
55 | outf = argv[1]; | |
56 | sprintf(outft, "%s~", outf); | |
57 | if ((out = fopen(outft, "w")) == NULL) { | |
58 | fprintf(stderr, "unable to open %s for output\n", outf); | |
59 | exit(MR_OCONFIG); | |
60 | } | |
61 | } else if (argc != 1) { | |
62 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
63 | exit(MR_ARGS); | |
64 | } else { | |
65 | outf = NULL; | |
66 | } | |
67 | ||
68 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
69 | ||
70 | gettimeofday(&now, NULL); | |
71 | ||
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)); | |
78 | ||
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 | EXEC SQL FETCH x INTO :name, :id, :addr, :description; | |
85 | if (sqlca.sqlcode != 0) break; | |
86 | if (id == 0) continue; | |
87 | if (*strtrim(name) == 0) continue; | |
88 | fprintf(out, "NETWORK : %-16.16s : %-13.13s : %s \n", name, inet_ntoa(htonl(addr)), | |
89 | description); | |
90 | } | |
91 | ||
92 | EXEC SQL CLOSE x; | |
93 | #ifsql INGRES | |
94 | EXEC SQL DISCONNECT; | |
95 | #endsql | |
96 | #ifsql INFORMIX | |
97 | EXEC SQL CLOSE DATABASE; | |
98 | #endsql | |
99 | ||
100 | fprintf(out, "; End of automatically generated network table\n"); | |
101 | if (fclose(out)) { | |
102 | perror("close failed"); | |
103 | exit(MR_CCONFIG); | |
104 | } | |
105 | if (outf) | |
106 | fix_file(outf); | |
107 | exit(MR_SUCCESS); | |
108 | ||
109 | sqlerr: | |
110 | com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode); | |
111 | #ifsql INGRES | |
112 | if (sqlca.sqlcode == 17700 || sqlca.sqlcode == -37000) | |
113 | exit(MR_DEADLOCK); | |
114 | #endsql | |
115 | critical_alert("DCM", "Networks build encountered INGRES ERROR %d", | |
116 | sqlca.sqlcode); | |
117 | exit(MR_INGRES_ERR); | |
118 | } |