From 8b41ecf750e4a20e65943a14ce80d4a01f663dce Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 17 Nov 2004 07:18:27 +0000 Subject: [PATCH] Import from coreutils. --- ChangeLog | 4 +++ lib/ChangeLog | 4 +++ lib/canon-host.c | 97 ++++++++++++++++++++++++++++++++---------------------- lib/human.c | 6 ++++ lib/human.h | 10 ++++-- lib/xgetcwd.c | 20 +++++++++-- m4/ChangeLog | 8 +++++ m4/canon-host.m4 | 6 ++-- m4/jm-macros.m4 | 3 +- modules/canon-host | 2 +- 10 files changed, 111 insertions(+), 49 deletions(-) diff --git a/ChangeLog b/ChangeLog index 400a2104f..70ac83cd8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2004-11-16 Paul Eggert + + * modules/canon-host (Depends-on): Add strdup. + 2004-11-16 Jim Meyering * modules/closeout (Depends-on): Add fpending. diff --git a/lib/ChangeLog b/lib/ChangeLog index fd50ab022..8670508a1 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,5 +1,9 @@ 2004-11-16 Paul Eggert + * canon-host.c: Include "strdup.h". + (canon_host): Use getaddrinfo if available, so that IPv6 works. + Use strdup instead of malloc/strcpy to duplicate strings. + Further getopt changes to make it more likely that glibc will buy the changes back. * getopt.c (POSIXLY_CORRECT): New constant. diff --git a/lib/canon-host.c b/lib/canon-host.c index b0c2f44bb..fb6621cb5 100644 --- a/lib/canon-host.c +++ b/lib/canon-host.c @@ -1,6 +1,7 @@ /* Host name canonicalization - Copyright (C) 1995, 1999, 2000, 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 1995, 1999, 2000, 2002, 2003, 2004 Free Software + Foundation, Inc. Written by Miles Bader @@ -42,55 +43,73 @@ # include #endif +#include "strdup.h" + /* Returns the canonical hostname associated with HOST (allocated in a static - buffer), or 0 if it can't be determined. */ + buffer), or NULL if it can't be determined. */ char * -canon_host (const char *host) +canon_host (char const *host) { -#ifdef HAVE_GETHOSTBYNAME - struct hostent *he = gethostbyname (host); + char *h_addr_copy = NULL; - if (he) - { +#if HAVE_GETADDRINFO + { + struct addrinfo hint = { 0 }; + struct addrinfo *res = NULL; + hint.ai_flags = AI_CANONNAME; + if (getaddrinfo (host, NULL, &hint, &res) == 0) + { + h_addr_copy = strdup (res->ai_canonname); + freeaddrinfo (res); + } + } +#elif HAVE_GETHOSTBYNAME + { + struct hostent *he = gethostbyname (host); + + if (he) + { # ifdef HAVE_GETHOSTBYADDR - char *addr = 0; + char *addr = NULL; - /* Try and get an ascii version of the numeric host address. */ - switch (he->h_addrtype) - { + /* Try and get an ascii version of the numeric host address. */ + switch (he->h_addrtype) + { # ifdef HAVE_INET_NTOA - case AF_INET: - addr = inet_ntoa (*(struct in_addr *) he->h_addr); - break; + case AF_INET: + addr = inet_ntoa (*(struct in_addr *) he->h_addr); + break; # endif /* HAVE_INET_NTOA */ - } - - if (addr && strcmp (he->h_name, addr) == 0) - { - /* gethostbyname has returned a string representation of the IP - address, for example, "127.0.0.1". So now, look up the host - name via the address. Although it may seem reasonable to look - up the host name via the address, we must not pass `he->h_addr' - directly to gethostbyaddr because on some systems he->h_addr - is located in a static library buffer that is reused in the - gethostbyaddr call. Make a copy and use that instead. */ - char *h_addr_copy = (char *) malloc (he->h_length); - if (h_addr_copy == NULL) - he = NULL; - else - { - memcpy (h_addr_copy, he->h_addr, he->h_length); - he = gethostbyaddr (h_addr_copy, he->h_length, he->h_addrtype); - free (h_addr_copy); - } - } + } + + if (addr && strcmp (he->h_name, addr) == 0) + { + /* gethostbyname has returned a string representation of the IP + address, for example, "127.0.0.1". So now, look up the host + name via the address. Although it may seem reasonable to look + up the host name via the address, we must not pass `he->h_addr' + directly to gethostbyaddr because on some systems he->h_addr + is located in a static library buffer that is reused in the + gethostbyaddr call. Make a copy and use that instead. */ + h_addr_copy = (char *) malloc (he->h_length); + if (h_addr_copy == NULL) + he = NULL; + else + { + memcpy (h_addr_copy, he->h_addr, he->h_length); + he = gethostbyaddr (h_addr_copy, he->h_length, he->h_addrtype); + free (h_addr_copy); + } + } # endif /* HAVE_GETHOSTBYADDR */ - if (he) - return (char *) (he->h_name); - } + if (he) + h_addr_copy = strdup (he->h_name); + } + } #endif /* HAVE_GETHOSTBYNAME */ - return 0; + + return h_addr_copy; } #ifdef TEST_CANON_HOST diff --git a/lib/human.c b/lib/human.c index f024c73ea..4960876dd 100644 --- a/lib/human.c +++ b/lib/human.c @@ -158,6 +158,9 @@ group_number (char *number, size_t numberlen, so on. Numbers smaller than the power aren't modified. human_autoscale is normally used together with human_SI. + If (OPTS & human_space_before_unit), use a space to separate the + number from any suffix that is appended as described below. + If (OPTS & human_SI), append an SI prefix indicating which power is being used. If in addition (OPTS & human_B), append "B" (if base 1000) or "iB" (if base 1024) to the SI prefix. When ((OPTS & @@ -384,6 +387,9 @@ human_readable (uintmax_t n, char *buf, int opts, break; } + if ((exponent | (opts & human_B)) && (opts & human_space_before_unit)) + *psuffix++ = ' '; + if (exponent) *psuffix++ = (! (opts & human_base_1024) && exponent == 1 ? 'k' diff --git a/lib/human.h b/lib/human.h index b67ba4e9a..553d7ad1b 100644 --- a/lib/human.h +++ b/lib/human.h @@ -42,10 +42,11 @@ 302 / 1000 is ceil (log10 (2.0)). Add 1 for integer division truncation. Also, the output can have a thousands separator between every digit, so multiply by MB_LEN_MAX + 1 and then subtract MB_LEN_MAX. + Append 1 for a space before the suffix. Finally, append 3, the maximum length of a suffix. */ # define LONGEST_HUMAN_READABLE \ ((2 * sizeof (uintmax_t) * CHAR_BIT * 302 / 1000 + 1) * (MB_LEN_MAX + 1) \ - - MB_LEN_MAX + 3) + - MB_LEN_MAX + 1 + 3) /* Options for human_readable. */ enum @@ -74,11 +75,14 @@ enum /* Prefer base 1024 to base 1000. */ human_base_1024 = 32, + /* Prepend " " before unit symbol. */ + human_space_before_unit = 64, + /* Append SI prefix, e.g. "k" or "M". */ - human_SI = 64, + human_SI = 128, /* Append "B" (if base 1000) or "iB" (if base 1024) to SI prefix. */ - human_B = 128 + human_B = 256 }; char *human_readable (uintmax_t, char *, int, uintmax_t, uintmax_t); diff --git a/lib/xgetcwd.c b/lib/xgetcwd.c index 785002e0e..f08580357 100644 --- a/lib/xgetcwd.c +++ b/lib/xgetcwd.c @@ -23,6 +23,7 @@ # include #endif +#include #include #include #include @@ -60,6 +61,8 @@ xgetcwd (void) return cwd; #else + int saved_errno; + /* The initial buffer size for the working directory. A power of 2 detects arithmetic overflow earlier, but is not required. */ # ifndef INITIAL_BUFFER_SIZE @@ -72,16 +75,29 @@ xgetcwd (void) { char *buf = xmalloc (buf_size); char *cwd = getcwd (buf, buf_size); - int saved_errno; if (cwd) return cwd; saved_errno = errno; free (buf); if (saved_errno != ERANGE) - return NULL; + break; + +#ifdef PATH_MAX + if (PATH_MAX / 2 < buf_size) + { + if (PATH_MAX <= buf_size) + break; + buf_size = PATH_MAX; + continue; + } +#endif + buf_size *= 2; if (buf_size == 0) xalloc_die (); } + + errno = saved_errno; + return NULL; #endif } diff --git a/m4/ChangeLog b/m4/ChangeLog index 776176d6f..4e986c05e 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,11 @@ +2004-11-16 Paul Eggert + + * canon-host.m4 (gl_CANON_HOST): Check for getaddrinfo. + +2004-11-13 Jim Meyering + + * jm-macros.m4: Do require gl_FUNC_FPENDING. + 2004-11-11 Paul Eggert * getopt.m4 (gl_GETOPT_SUBSTITUTE): Define __GETOPT_PREFIX instead diff --git a/m4/canon-host.m4 b/m4/canon-host.m4 index 3f1f5eb65..bd7569a26 100644 --- a/m4/canon-host.m4 +++ b/m4/canon-host.m4 @@ -1,5 +1,5 @@ -# canon-host.m4 serial 3 -dnl Copyright (C) 2002, 2003 Free Software Foundation, Inc. +# canon-host.m4 serial 4 +dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. dnl This file is free software, distributed under the terms of the GNU dnl General Public License. As a special exception to the GNU General dnl Public License, this file may be distributed as part of a program @@ -18,5 +18,5 @@ AC_DEFUN([gl_CANON_HOST], AC_SEARCH_LIBS(gethostbyname, [inet nsl]) dnl These come from -lnsl on Solaris 2.5.1. - AC_CHECK_FUNCS(gethostbyname gethostbyaddr inet_ntoa) + AC_CHECK_FUNCS(getaddrinfo gethostbyname gethostbyaddr inet_ntoa) ]) diff --git a/m4/jm-macros.m4 b/m4/jm-macros.m4 index 63cbff3e4..3529e7643 100644 --- a/m4/jm-macros.m4 +++ b/m4/jm-macros.m4 @@ -1,4 +1,4 @@ -#serial 77 -*- autoconf -*- +#serial 78 -*- autoconf -*- dnl Misc type-related macros for coreutils. @@ -59,6 +59,7 @@ AC_DEFUN([gl_MACROS], AC_REQUIRE([gl_FUNC_GROUP_MEMBER]) AC_REQUIRE([gl_AFS]) AC_REQUIRE([gl_AC_FUNC_LINK_FOLLOWS_SYMLINK]) + AC_REQUIRE([gl_FUNC_FPENDING]) # This is for od and stat, and any other program that # uses the PRI.MAX macros from inttypes.h. diff --git a/modules/canon-host b/modules/canon-host index 823a30fdb..c7479b331 100644 --- a/modules/canon-host +++ b/modules/canon-host @@ -7,6 +7,7 @@ lib/canon-host.c m4/canon-host.m4 Depends-on: +strdup configure.ac: gl_CANON_HOST @@ -21,4 +22,3 @@ GPL Maintainer: Jim Meyering - -- 2.11.0