diff --git a/Makefile b/Makefile index c52537a0c..866fe5b46 100644 --- a/Makefile +++ b/Makefile @@ -248,8 +248,9 @@ parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c print_file.c \ process_escape_sequence.c read_package_field.c read_text_file_to_buffer.c \ recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \ syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \ -verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xreadlink.c\ -xregcomp.c interface.c remove_file.c last_char_is.c copyfd.c +verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xreadlink.c \ +xregcomp.c interface.c remove_file.c last_char_is.c copyfd.c \ +vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) LIBBB_CFLAGS = -I$(LIBBB) ifneq ($(strip $(BB_SRC_DIR)),) diff --git a/hostname.c b/hostname.c index f4118ea36..75e4d2e57 100644 --- a/hostname.c +++ b/hostname.c @@ -1,6 +1,6 @@ /* vi: set sw=4 ts=4: */ /* - * $Id: hostname.c,v 1.27 2001/05/16 14:21:09 kraai Exp $ + * $Id: hostname.c,v 1.28 2001/05/16 15:40:48 kraai Exp $ * Mini hostname implementation for busybox * * Copyright (C) 1999 by Randolph Chung @@ -117,11 +117,7 @@ int hostname_main(int argc, char **argv) s = strchr(buf, '.'); puts(s ? s + 1 : ""); } else if (opt_ip) { - h = gethostbyname(buf); - if (!h) { - printf("Host not found\n"); - exit(1); - } + h = xgethostbyname(buf); puts(inet_ntoa(*(struct in_addr *) (h->h_addr))); } else { puts(buf); diff --git a/include/libbb.h b/include/libbb.h index 7ab06388a..29a756b7f 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -29,6 +29,8 @@ #include #include +#include + #ifdef DMALLOC #include "dmalloc.h" #endif @@ -77,6 +79,9 @@ extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)) extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))); extern void perror_msg(const char *s, ...); extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn)); +extern void vherror_msg(const char *s, va_list p); +extern void herror_msg(const char *s, ...); +extern void herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn)); /* These two are used internally -- you shouldn't need to use them */ extern void verror_msg(const char *s, va_list p); @@ -252,6 +257,8 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file); extern void gz_close(int gunzip_pid); extern int gz_open(FILE *compressed_file, int *pid); +extern struct hostent *xgethostbyname(const char *name); + #define CT_AUTO 0 #define CT_UNIX2DOS 1 #define CT_DOS2UNIX 2 diff --git a/libbb/herror_msg.c b/libbb/herror_msg.c new file mode 100644 index 000000000..f4210edad --- /dev/null +++ b/libbb/herror_msg.c @@ -0,0 +1,50 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) tons of folks. Tracking down who wrote what + * isn't something I'm going to worry about... If you wrote something + * here, please feel free to acknowledge your work. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Based in part on code from sash, Copyright (c) 1999 by David I. Bell + * Permission has been granted to redistribute this code under the GPL. + * + */ + +#include +#include + +#include "libbb.h" + +extern void herror_msg(const char *s, ...) +{ + va_list p; + + va_start(p, s); + vherror_msg(s, p); + va_end(p); +} + + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/libbb/herror_msg_and_die.c b/libbb/herror_msg_and_die.c new file mode 100644 index 000000000..0df5ed016 --- /dev/null +++ b/libbb/herror_msg_and_die.c @@ -0,0 +1,51 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) tons of folks. Tracking down who wrote what + * isn't something I'm going to worry about... If you wrote something + * here, please feel free to acknowledge your work. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Based in part on code from sash, Copyright (c) 1999 by David I. Bell + * Permission has been granted to redistribute this code under the GPL. + * + */ + +#include +#include + +#include "libbb.h" + +extern void herror_msg_and_die(const char *s, ...) +{ + va_list p; + + va_start(p, s); + vherror_msg(s, p); + va_end(p); + exit(EXIT_FAILURE); +} + + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ diff --git a/libbb/libbb.h b/libbb/libbb.h index 7ab06388a..29a756b7f 100644 --- a/libbb/libbb.h +++ b/libbb/libbb.h @@ -29,6 +29,8 @@ #include #include +#include + #ifdef DMALLOC #include "dmalloc.h" #endif @@ -77,6 +79,9 @@ extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)) extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))); extern void perror_msg(const char *s, ...); extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn)); +extern void vherror_msg(const char *s, va_list p); +extern void herror_msg(const char *s, ...); +extern void herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn)); /* These two are used internally -- you shouldn't need to use them */ extern void verror_msg(const char *s, va_list p); @@ -252,6 +257,8 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file); extern void gz_close(int gunzip_pid); extern int gz_open(FILE *compressed_file, int *pid); +extern struct hostent *xgethostbyname(const char *name); + #define CT_AUTO 0 #define CT_UNIX2DOS 1 #define CT_DOS2UNIX 2 diff --git a/libbb/vherror_msg.c b/libbb/vherror_msg.c new file mode 100644 index 000000000..9a06f3a38 --- /dev/null +++ b/libbb/vherror_msg.c @@ -0,0 +1,45 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) tons of folks. Tracking down who wrote what + * isn't something I'm going to worry about... If you wrote something + * here, please feel free to acknowledge your work. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Based in part on code from sash, Copyright (c) 1999 by David I. Bell + * Permission has been granted to redistribute this code under the GPL. + * + */ + +#include +#include +extern int h_errno; + +#include + +#include "libbb.h" + +extern void vherror_msg(const char *s, va_list p) +{ + int err = h_errno; + if(s == 0) + s = ""; + verror_msg(s, p); + if (*s) + s = ": "; + fprintf(stderr, "%s%s\n", s, hstrerror(err)); +} diff --git a/libbb/xgethostbyname.c b/libbb/xgethostbyname.c new file mode 100644 index 000000000..c722951cf --- /dev/null +++ b/libbb/xgethostbyname.c @@ -0,0 +1,35 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini xgethostbyname implementation. + * + * + * Copyright (C) 2001 Matt Kraai . + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +extern int h_errno; + +struct hostent *xgethostbyname(const char *name) +{ + struct hostent *retval; + + if ((retval = gethostbyname(name)) == NULL) + herror_msg_and_die("%s", name); + + return retval; +} diff --git a/nc.c b/nc.c index b58bd6a12..5335872e5 100644 --- a/nc.c +++ b/nc.c @@ -91,8 +91,7 @@ int nc_main(int argc, char **argv) close(sfd); sfd = tmpfd; } else { - if ((hostinfo = gethostbyname(argv[optind])) == NULL) - error_msg_and_die("cannot resolve %s\n", argv[optind]); + hostinfo = xgethostbyname(argv[optind]); address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; address.sin_port = htons(atoi(argv[optind+1])); diff --git a/networking/hostname.c b/networking/hostname.c index f4118ea36..75e4d2e57 100644 --- a/networking/hostname.c +++ b/networking/hostname.c @@ -1,6 +1,6 @@ /* vi: set sw=4 ts=4: */ /* - * $Id: hostname.c,v 1.27 2001/05/16 14:21:09 kraai Exp $ + * $Id: hostname.c,v 1.28 2001/05/16 15:40:48 kraai Exp $ * Mini hostname implementation for busybox * * Copyright (C) 1999 by Randolph Chung @@ -117,11 +117,7 @@ int hostname_main(int argc, char **argv) s = strchr(buf, '.'); puts(s ? s + 1 : ""); } else if (opt_ip) { - h = gethostbyname(buf); - if (!h) { - printf("Host not found\n"); - exit(1); - } + h = xgethostbyname(buf); puts(inet_ntoa(*(struct in_addr *) (h->h_addr))); } else { puts(buf); diff --git a/networking/nc.c b/networking/nc.c index b58bd6a12..5335872e5 100644 --- a/networking/nc.c +++ b/networking/nc.c @@ -91,8 +91,7 @@ int nc_main(int argc, char **argv) close(sfd); sfd = tmpfd; } else { - if ((hostinfo = gethostbyname(argv[optind])) == NULL) - error_msg_and_die("cannot resolve %s\n", argv[optind]); + hostinfo = xgethostbyname(argv[optind]); address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; address.sin_port = htons(atoi(argv[optind+1])); diff --git a/networking/ping.c b/networking/ping.c index 8b82dcaac..0967999a9 100644 --- a/networking/ping.c +++ b/networking/ping.c @@ -1,6 +1,6 @@ /* vi: set sw=4 ts=4: */ /* - * $Id: ping.c,v 1.40 2001/04/09 23:52:18 andersen Exp $ + * $Id: ping.c,v 1.41 2001/05/16 15:40:48 kraai Exp $ * Mini ping implementation for busybox * * Copyright (C) 1999 by Randolph Chung @@ -200,10 +200,7 @@ static void ping(const char *host) memset(&pingaddr, 0, sizeof(struct sockaddr_in)); pingaddr.sin_family = AF_INET; - if (!(h = gethostbyname(host))) { - error_msg("unknown host %s", host); - exit(1); - } + h = xgethostbyname(host); memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); hostname = h->h_name; @@ -446,15 +443,9 @@ static void ping(const char *host) memset(&pingaddr, 0, sizeof(struct sockaddr_in)); pingaddr.sin_family = AF_INET; - if (!(h = gethostbyname(host))) { - error_msg("unknown host %s", host); - exit(1); - } - - if (h->h_addrtype != AF_INET) { - error_msg("unknown address type; only AF_INET is currently supported."); - exit(1); - } + h = gethostbyname(host); + if (h->h_addrtype != AF_INET) + error_msg_and_die("unknown address type; only AF_INET is currently supported."); pingaddr.sin_family = AF_INET; /* h->h_addrtype */ memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); diff --git a/networking/telnet.c b/networking/telnet.c index 207732b72..2587193e2 100644 --- a/networking/telnet.c +++ b/networking/telnet.c @@ -644,18 +644,15 @@ static int getport(char * p) static struct in_addr getserver(char * host) { struct in_addr addr; - + struct hostent * he; - if ((he = gethostbyname(host)) == NULL) - { - error_msg_and_die("%s: Unknown host", host); - } + he = xgethostbyname(host); memcpy(&addr, he->h_addr, sizeof addr); TRACE(1, ("addr: %s\n", inet_ntoa(addr))); - + return addr; -} +} static int create_socket() { diff --git a/networking/tftp.c b/networking/tftp.c index 466851c1b..bb75c88ec 100644 --- a/networking/tftp.c +++ b/networking/tftp.c @@ -390,15 +390,10 @@ int tftp_main(int argc, char **argv) s = xstrdup(serverstr); s[cp - serverstr] = '\0'; - if ((host = gethostbyname(s))) { - bad = 0; - } + host = xgethostbyname(s); free(s); } - if (bad) { - error_msg_and_die("bad \"server:file\" combination"); - } if (BB_TFTP_DEBUG) { printf("using server \"%s\", serverfile \"%s\"," diff --git a/networking/wget.c b/networking/wget.c index 5fa918a19..6fd170d4d 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -556,8 +556,7 @@ FILE *open_socket(char *host, int port) memset(&s_in, 0, sizeof(s_in)); s_in.sin_family = AF_INET; - if ((hp = (struct hostent *) gethostbyname(host)) == NULL) - error_msg_and_die("cannot resolve %s", host); + hp = xgethostbyname(host); memcpy(&s_in.sin_addr, hp->h_addr_list[0], hp->h_length); s_in.sin_port = htons(port); @@ -813,7 +812,7 @@ progressmeter(int flag) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: wget.c,v 1.40 2001/05/15 20:11:49 andersen Exp $ + * $Id: wget.c,v 1.41 2001/05/16 15:40:48 kraai Exp $ */ diff --git a/nfsmount.c b/nfsmount.c index a62df3272..90cf9fb1b 100644 --- a/nfsmount.c +++ b/nfsmount.c @@ -335,7 +335,7 @@ int nfsmount(const char *spec, const char *node, int *flags, #endif { if ((hp = gethostbyname(hostname)) == NULL) { - error_msg("can't get address for %s", hostname); + herror_msg("%s", hostname); goto fail; } else { if (hp->h_length > sizeof(struct in_addr)) { @@ -580,7 +580,7 @@ int nfsmount(const char *spec, const char *node, int *flags, mount_server_addr.sin_addr.s_addr = inet_addr(hostname); } else { if ((hp = gethostbyname(mounthost)) == NULL) { - error_msg("can't get address for %s", hostname); + herror_msg("%s", mounthost); goto fail; } else { if (hp->h_length > sizeof(struct in_addr)) { diff --git a/ping.c b/ping.c index 8b82dcaac..0967999a9 100644 --- a/ping.c +++ b/ping.c @@ -1,6 +1,6 @@ /* vi: set sw=4 ts=4: */ /* - * $Id: ping.c,v 1.40 2001/04/09 23:52:18 andersen Exp $ + * $Id: ping.c,v 1.41 2001/05/16 15:40:48 kraai Exp $ * Mini ping implementation for busybox * * Copyright (C) 1999 by Randolph Chung @@ -200,10 +200,7 @@ static void ping(const char *host) memset(&pingaddr, 0, sizeof(struct sockaddr_in)); pingaddr.sin_family = AF_INET; - if (!(h = gethostbyname(host))) { - error_msg("unknown host %s", host); - exit(1); - } + h = xgethostbyname(host); memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); hostname = h->h_name; @@ -446,15 +443,9 @@ static void ping(const char *host) memset(&pingaddr, 0, sizeof(struct sockaddr_in)); pingaddr.sin_family = AF_INET; - if (!(h = gethostbyname(host))) { - error_msg("unknown host %s", host); - exit(1); - } - - if (h->h_addrtype != AF_INET) { - error_msg("unknown address type; only AF_INET is currently supported."); - exit(1); - } + h = gethostbyname(host); + if (h->h_addrtype != AF_INET) + error_msg_and_die("unknown address type; only AF_INET is currently supported."); pingaddr.sin_family = AF_INET; /* h->h_addrtype */ memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); diff --git a/rdate.c b/rdate.c index ead1e7c7d..8deb35d14 100644 --- a/rdate.c +++ b/rdate.c @@ -45,8 +45,7 @@ static time_t askremotedate(const char *host) unsigned long int nett, localt; int fd; - if (!(h = gethostbyname(host))) /* get the IP addr */ - perror_msg_and_die("%s", host); + h = xgethostbyname(host); /* get the IP addr */ if ((tserv = getservbyname("time", "tcp")) == NULL) /* find port # */ perror_msg_and_die("%s", "time"); diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index 9c7375852..6be51d7dc 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c @@ -450,11 +450,7 @@ static void init_RemoteLog (void){ error_msg_and_die("syslogd: cannot create socket"); } - hostinfo = (struct hostent *) gethostbyname(RemoteHost); - - if (!hostinfo) { - error_msg_and_die("syslogd: cannot resolve remote host name [%s]", RemoteHost); - } + hostinfo = xgethostbyname(RemoteHost); remoteaddr.sin_family = AF_INET; remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; diff --git a/syslogd.c b/syslogd.c index 9c7375852..6be51d7dc 100644 --- a/syslogd.c +++ b/syslogd.c @@ -450,11 +450,7 @@ static void init_RemoteLog (void){ error_msg_and_die("syslogd: cannot create socket"); } - hostinfo = (struct hostent *) gethostbyname(RemoteHost); - - if (!hostinfo) { - error_msg_and_die("syslogd: cannot resolve remote host name [%s]", RemoteHost); - } + hostinfo = xgethostbyname(RemoteHost); remoteaddr.sin_family = AF_INET; remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; diff --git a/telnet.c b/telnet.c index 207732b72..2587193e2 100644 --- a/telnet.c +++ b/telnet.c @@ -644,18 +644,15 @@ static int getport(char * p) static struct in_addr getserver(char * host) { struct in_addr addr; - + struct hostent * he; - if ((he = gethostbyname(host)) == NULL) - { - error_msg_and_die("%s: Unknown host", host); - } + he = xgethostbyname(host); memcpy(&addr, he->h_addr, sizeof addr); TRACE(1, ("addr: %s\n", inet_ntoa(addr))); - + return addr; -} +} static int create_socket() { diff --git a/tftp.c b/tftp.c index 466851c1b..bb75c88ec 100644 --- a/tftp.c +++ b/tftp.c @@ -390,15 +390,10 @@ int tftp_main(int argc, char **argv) s = xstrdup(serverstr); s[cp - serverstr] = '\0'; - if ((host = gethostbyname(s))) { - bad = 0; - } + host = xgethostbyname(s); free(s); } - if (bad) { - error_msg_and_die("bad \"server:file\" combination"); - } if (BB_TFTP_DEBUG) { printf("using server \"%s\", serverfile \"%s\"," diff --git a/util-linux/nfsmount.c b/util-linux/nfsmount.c index a62df3272..90cf9fb1b 100644 --- a/util-linux/nfsmount.c +++ b/util-linux/nfsmount.c @@ -335,7 +335,7 @@ int nfsmount(const char *spec, const char *node, int *flags, #endif { if ((hp = gethostbyname(hostname)) == NULL) { - error_msg("can't get address for %s", hostname); + herror_msg("%s", hostname); goto fail; } else { if (hp->h_length > sizeof(struct in_addr)) { @@ -580,7 +580,7 @@ int nfsmount(const char *spec, const char *node, int *flags, mount_server_addr.sin_addr.s_addr = inet_addr(hostname); } else { if ((hp = gethostbyname(mounthost)) == NULL) { - error_msg("can't get address for %s", hostname); + herror_msg("%s", mounthost); goto fail; } else { if (hp->h_length > sizeof(struct in_addr)) { diff --git a/util-linux/rdate.c b/util-linux/rdate.c index ead1e7c7d..8deb35d14 100644 --- a/util-linux/rdate.c +++ b/util-linux/rdate.c @@ -45,8 +45,7 @@ static time_t askremotedate(const char *host) unsigned long int nett, localt; int fd; - if (!(h = gethostbyname(host))) /* get the IP addr */ - perror_msg_and_die("%s", host); + h = xgethostbyname(host); /* get the IP addr */ if ((tserv = getservbyname("time", "tcp")) == NULL) /* find port # */ perror_msg_and_die("%s", "time"); diff --git a/wget.c b/wget.c index 5fa918a19..6fd170d4d 100644 --- a/wget.c +++ b/wget.c @@ -556,8 +556,7 @@ FILE *open_socket(char *host, int port) memset(&s_in, 0, sizeof(s_in)); s_in.sin_family = AF_INET; - if ((hp = (struct hostent *) gethostbyname(host)) == NULL) - error_msg_and_die("cannot resolve %s", host); + hp = xgethostbyname(host); memcpy(&s_in.sin_addr, hp->h_addr_list[0], hp->h_length); s_in.sin_port = htons(port); @@ -813,7 +812,7 @@ progressmeter(int flag) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: wget.c,v 1.40 2001/05/15 20:11:49 andersen Exp $ + * $Id: wget.c,v 1.41 2001/05/16 15:40:48 kraai Exp $ */