gcc2 lint
[unix-history] / usr / src / old / gettable / gettable.c
CommitLineData
629e75d6 1/*
0d6797a3
KB
2 * Copyright (c) 1983 The Regents of the University of California.
3 * All rights reserved.
4 *
32ce521f 5 * %sccs.include.redist.c%
629e75d6
DF
6 */
7
b9feeb00 8#ifndef lint
0d6797a3
KB
9char copyright[] =
10"@(#) Copyright (c) 1983 The Regents of the University of California.\n\
11 All rights reserved.\n";
12#endif /* not lint */
13
14#ifndef lint
7f3b056b 15static char sccsid[] = "@(#)gettable.c 5.6 (Berkeley) %G%";
0d6797a3 16#endif /* not lint */
b9feeb00
SL
17
18#include <sys/types.h>
19#include <sys/socket.h>
20
21#include <netinet/in.h>
22
23#include <stdio.h>
24#include <netdb.h>
25
26#define OUTFILE "hosts.txt" /* default output file */
329362bd 27#define VERFILE "hosts.ver" /* default version file */
b9feeb00 28#define QUERY "ALL\r\n" /* query to hostname server */
329362bd 29#define VERSION "VERSION\r\n" /* get version number */
b9feeb00
SL
30
31#define equaln(s1, s2, n) (!strncmp(s1, s2, n))
32
33struct sockaddr_in sin;
34char buf[BUFSIZ];
35char *outfile = OUTFILE;
36
37main(argc, argv)
38 int argc;
39 char *argv[];
40{
41 int s;
42 register len;
43 register FILE *sfi, *sfo, *hf;
b9feeb00
SL
44 char *host;
45 register struct hostent *hp;
46 struct servent *sp;
329362bd 47 int version = 0;
b949b1b9
SL
48 int beginseen = 0;
49 int endseen = 0;
b9feeb00
SL
50
51 argv++, argc--;
329362bd
RC
52 if (**argv == '-') {
53 if (argv[0][1] != 'v')
54 fprintf(stderr, "unknown option %s ignored\n", *argv);
55 else
56 version++, outfile = VERFILE;
57 argv++, argc--;
58 }
b9feeb00 59 if (argc < 1 || argc > 2) {
329362bd 60 fprintf(stderr, "usage: gettable [-v] host [ file ]\n");
b9feeb00
SL
61 exit(1);
62 }
e0754ba7 63 sp = getservbyname("hostnames", "tcp");
b9feeb00 64 if (sp == NULL) {
e0754ba7 65 fprintf(stderr, "gettable: hostnames/tcp: unknown service\n");
b9feeb00
SL
66 exit(3);
67 }
68 host = *argv;
69 argv++, argc--;
70 hp = gethostbyname(host);
71 if (hp == NULL) {
b956af97
KB
72 fprintf(stderr, "gettable: %s: ", host);
73 herror((char *)NULL);
b9feeb00
SL
74 exit(2);
75 }
76 host = hp->h_name;
77 if (argc > 0)
78 outfile = *argv;
79 sin.sin_family = hp->h_addrtype;
f5bcb151 80 s = socket(hp->h_addrtype, SOCK_STREAM, 0);
b9feeb00
SL
81 if (s < 0) {
82 perror("gettable: socket");
83 exit(4);
84 }
7f3b056b 85 if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
b9feeb00
SL
86 perror("gettable: bind");
87 exit(5);
88 }
7f3b056b 89 bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
b9feeb00 90 sin.sin_port = sp->s_port;
7f3b056b 91 if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
b9feeb00
SL
92 perror("gettable: connect");
93 exit(6);
94 }
95 fprintf(stderr, "Connection to %s opened.\n", host);
96 sfi = fdopen(s, "r");
97 sfo = fdopen(s, "w");
98 if (sfi == NULL || sfo == NULL) {
99 perror("gettable: fdopen");
100 close(s);
101 exit(1);
102 }
103 hf = fopen(outfile, "w");
104 if (hf == NULL) {
105 fprintf(stderr, "gettable: "); perror(outfile);
106 close(s);
107 exit(1);
108 }
329362bd 109 fprintf(sfo, version ? VERSION : QUERY);
b9feeb00
SL
110 fflush(sfo);
111 while (fgets(buf, sizeof(buf), sfi) != NULL) {
112 len = strlen(buf);
113 buf[len-2] = '\0';
b949b1b9
SL
114 if (!version && equaln(buf, "BEGIN", 5)) {
115 if (beginseen || endseen) {
116 fprintf(stderr,
117 "gettable: BEGIN sequence error\n");
118 exit(90);
119 }
120 beginseen++;
b9feeb00
SL
121 continue;
122 }
b949b1b9
SL
123 if (!version && equaln(buf, "END", 3)) {
124 if (!beginseen || endseen) {
125 fprintf(stderr,
126 "gettable: END sequence error\n");
127 exit(91);
128 }
129 endseen++;
b9feeb00
SL
130 continue;
131 }
b949b1b9
SL
132 if (equaln(buf, "ERR", 3)) {
133 fprintf(stderr,
134 "gettable: hostname service error: %s", buf);
135 exit(92);
136 }
b9feeb00
SL
137 fprintf(hf, "%s\n", buf);
138 }
139 fclose(hf);
b949b1b9
SL
140 if (!version) {
141 if (!beginseen) {
142 fprintf(stderr, "gettable: no BEGIN seen\n");
143 exit(93);
144 }
145 if (!endseen) {
146 fprintf(stderr, "gettable: no END seen\n");
147 exit(94);
148 }
329362bd 149 fprintf(stderr, "Host table received.\n");
b949b1b9
SL
150 } else
151 fprintf(stderr, "Version number received.\n");
b9feeb00
SL
152 close(s);
153 fprintf(stderr, "Connection to %s closed\n", host);
329362bd 154 exit(0);
b9feeb00 155}