|
|
1.1 root 1: /*
2: * Copyright (c) 1983 The Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that the above copyright notice and this paragraph are
7: * duplicated in all such forms and that any documentation,
8: * advertising materials, and other materials related to such
9: * distribution and use acknowledge that the software was developed
10: * by the University of California, Berkeley. The name of the
11: * University may not be used to endorse or promote products derived
12: * from this software without specific prior written permission.
13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16: */
17:
18: #ifndef lint
19: char copyright[] =
20: "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
21: All rights reserved.\n";
22: #endif /* not lint */
23:
24: #ifndef lint
25: static char sccsid[] = "@(#)gettable.c 5.4 (Berkeley) 10/11/88";
26: #endif /* not lint */
27:
28: #include <sys/types.h>
29:
30: #include <stdio.h>
31: #include <ipc.h>
32:
33:
34: #define OUTFILE "hosts.txt" /* default output file */
35: #define VERFILE "hosts.ver" /* default version file */
36: #define QUERY "ALL\r\n" /* query to hostname server */
37: #define VERSION "VERSION\r\n" /* get version number */
38:
39: #define equaln(s1, s2, n) (!strncmp(s1, s2, n))
40:
41: char buf[BUFSIZ];
42: char *outfile = OUTFILE;
43:
44: main(argc, argv)
45: int argc;
46: char *argv[];
47: {
48: int s;
49: register len;
50: register FILE *sfi, *sfo, *hf;
51: char *host;
52: register struct hostent *hp;
53: struct servent *sp;
54: int version = 0;
55: int beginseen = 0;
56: int endseen = 0;
57:
58: argv++, argc--;
59: if (**argv == '-') {
60: if (argv[0][1] != 'v')
61: fprintf(stderr, "unknown option %s ignored\n", *argv);
62: else
63: version++, outfile = VERFILE;
64: argv++, argc--;
65: }
66: if (argc < 1 || argc > 2) {
67: fprintf(stderr, "usage: gettable [-v] host [ file ]\n");
68: exit(1);
69: }
70: host = *argv;
71: argv++, argc--;
72: if ((s = ipcopen(ipcpath(host, "tcp", "hostnames")))< 0) {
73: fprintf(stderr, "gettable: connect: %s\n", errstr);
74: exit(6);
75: }
76: if (argc > 0)
77: outfile = *argv;
78: sfi = fdopen(s, "r");
79: sfo = fdopen(s, "w");
80: if (sfi == NULL || sfo == NULL) {
81: perror("gettable: fdopen");
82: close(s);
83: exit(1);
84: }
85: hf = fopen(outfile, "w");
86: if (hf == NULL) {
87: fprintf(stderr, "gettable: "); perror(outfile);
88: close(s);
89: exit(1);
90: }
91: fprintf(sfo, version ? VERSION : QUERY);
92: fflush(sfo);
93: while (fgets(buf, sizeof(buf), sfi) != NULL) {
94: len = strlen(buf);
95: buf[len-2] = '\0';
96: if (!version && equaln(buf, "BEGIN", 5)) {
97: if (beginseen || endseen) {
98: fprintf(stderr,
99: "gettable: BEGIN sequence error\n");
100: exit(90);
101: }
102: beginseen++;
103: continue;
104: }
105: if (!version && equaln(buf, "END", 3)) {
106: if (!beginseen || endseen) {
107: fprintf(stderr,
108: "gettable: END sequence error\n");
109: exit(91);
110: }
111: endseen++;
112: continue;
113: }
114: if (equaln(buf, "ERR", 3)) {
115: fprintf(stderr,
116: "gettable: hostname service error: %s", buf);
117: exit(92);
118: }
119: fprintf(hf, "%s\n", buf);
120: }
121: fclose(hf);
122: if (!version) {
123: if (!beginseen) {
124: fprintf(stderr, "gettable: no BEGIN seen\n");
125: exit(93);
126: }
127: if (!endseen) {
128: fprintf(stderr, "gettable: no END seen\n");
129: exit(94);
130: }
131: }
132: close(s);
133: exit(0);
134: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.