static char sccsid
[] = "@(#)gettable.c 4.1 (Bekeley) %G%";
#define OUTFILE "hosts.txt" /* default output file */
#define QUERY "ALL\r\n" /* query to hostname server */
#define equaln(s1, s2, n) (!strncmp(s1, s2, n))
register FILE *sfi
, *sfo
, *hf
;
register struct hostent
*hp
;
if (argc
< 1 || argc
> 2) {
fprintf(stderr
, "usage: gettable host [ file ]\n");
sp
= getservbyname("nicname", "tcp");
fprintf(stderr
, "gettable: nicname/tcp: unknown service\n");
hp
= gethostbyname(host
);
fprintf(stderr
, "gettable: %s: host unknown\n", host
);
sin
.sin_family
= hp
->h_addrtype
;
s
= socket(hp
->h_addrtype
, SOCK_STREAM
, 0, 0);
perror("gettable: socket");
if (bind(s
, &sin
, sizeof (sin
), 0) < 0) {
perror("gettable: bind");
bcopy(hp
->h_addr
, &sin
.sin_addr
, hp
->h_length
);
sin
.sin_port
= sp
->s_port
;
if (connect(s
, &sin
, sizeof (sin
), 0) < 0) {
perror("gettable: connect");
fprintf(stderr
, "Connection to %s opened.\n", host
);
if (sfi
== NULL
|| sfo
== NULL
) {
perror("gettable: fdopen");
hf
= fopen(outfile
, "w");
fprintf(stderr
, "gettable: "); perror(outfile
);
while (fgets(buf
, sizeof(buf
), sfi
) != NULL
) {
if (equaln(buf
, "BEGIN", 5) || equaln(buf
, "END", 3)) {
if (equaln(buf
, "ERR", 3)) {
fprintf(stderr
, "gettable: nicname error: %s", buf
);
fprintf(hf
, "%s\n", buf
);
fprintf(stderr
, "Host table received.\n");
fprintf(stderr
, "Connection to %s closed\n", host
);