3 * - useful general-purpose resolver client program
4 * main program and useful subroutines
8 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
10 * It is part of adns, which is
11 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
12 * Copyright (C) 1999 Tony Finch <dot@dotat.at>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 void sysfail(const char *what
, int errnoval
) {
32 fprintf(stderr
,"adnshost failed: %s: %s\n",what
,strerror(errnoval
));
36 void usageerr(const char *fmt
, ...) {
38 fputs("adnshost usage error: ",stderr
);
40 vfprintf(stderr
,fmt
,al
);
47 sysfail("write to stdout",errno
);
50 static void domain_do_arg(const char *domain
) {
51 if (ov_pipe
) usageerr("-f/--pipe not consistent with domains on command line");
56 void *xmalloc(size_t sz
) {
59 p
= malloc(sz
); if (!p
) sysfail("malloc",sz
);
63 char *xstrsave(const char *str
) {
66 p
= xmalloc(strlen(str
)+1);
71 void of_type(const struct optioninfo
*oi
, const char *arg
) { abort(); }
75 void setnonblock(int fd
, int nonblock
) { }
77 static void read_query(void) { abort(); }
79 int main(int argc
, const char *const *argv
) {
81 const struct optioninfo
*oip
;
82 struct timeval
*tv
, tvbuf
;
87 fd_set readfds
, writefds
, exceptfds
;
89 while ((arg
= *++argv
)) {
92 oip
= opt_findl(arg
+2);
93 if (oip
->type
== ot_funcarg
) {
95 if (!arg
) usageerr("option --%s requires a value argument",oip
->lopt
);
100 } else if (arg
[1] == 0) {
102 if (!arg
) usageerr("option `-' must be followed by a domain");
104 } else { /* arg[1] != '-', != '\0' */
107 oip
= opt_finds(&arg
);
108 if (oip
->type
== ot_funcarg
) {
111 if (!arg
) usageerr("option -%s requires a value argument",oip
->sopt
);
120 } else { /* arg[0] != '-' */
125 if (!ov_pipe
&& !ads
) usageerr("no domains given, and -f/--pipe not used; try --help");
129 qu
= ov_asynch ?
0 : outstanding
.head ? outstanding
.head
->qu
: 0;
130 r
= adns_check(ads
,&qu
,&answer
,&qun_v
);
131 if (r
== EAGAIN
) break;
132 if (r
== ESRCH
) { if (!ov_pipe
) goto x_quit
; else break; }
134 query_done(qun_v
,answer
);
145 adns_beforeselect(ads
, &maxfd
, &readfds
,&writefds
,&exceptfds
, &tv
,&tvbuf
,0);
146 r
= select(maxfd
, &readfds
,&writefds
,&exceptfds
, tv
);
148 if (errno
== EINTR
) continue;
149 sysfail("select",errno
);
151 adns_afterselect(ads
, maxfd
, &readfds
,&writefds
,&exceptfds
, 0);
152 if (ov_pipe
&& FD_ISSET(0,&readfds
)) read_query();
155 if (fclose(stdout
)) outerr();