diff options
author | Bruno Haible <bruno@clisp.org> | 2009-08-02 12:05:11 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2009-08-02 12:05:11 +0200 |
commit | 45ba61a18c1427acf1603ad97e8f49b579fa1ab6 (patch) | |
tree | c8e04f763b922f1b3ab11d5a19d03649678e288c /lib/gethostname.c | |
parent | 33bcc047c11586cff59d22566226e847e0c23954 (diff) | |
download | gnulib-45ba61a18c1427acf1603ad97e8f49b579fa1ab6.tar.gz |
Fix handling of large len argument.
Diffstat (limited to 'lib/gethostname.c')
-rw-r--r-- | lib/gethostname.c | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/gethostname.c b/lib/gethostname.c index 782c4028f0..ef58a40313 100644 --- a/lib/gethostname.c +++ b/lib/gethostname.c @@ -21,6 +21,7 @@ #include <config.h> #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) +/* Unix API. */ /* Specification. */ #include <unistd.h> @@ -59,6 +60,17 @@ gethostname (char *name, size_t len) } #else +/* Native Windows API. Which primitive to choose? + - gethostname() requires linking with -lws2_32. + - GetComputerName() does not return the right kind of hostname. + - GetComputerNameEx(ComputerNameDnsHostname,...) returns the right hostname, + but it hard to use portably: + - It requires defining _WIN32_WINNT to at least 0x0500. + - With mingw, it also requires + "#define GetComputerNameEx GetComputerNameExA". + - With older versions of mingw, none of the declarations are present at + all, not even of the enum value ComputerNameDnsHostname. + So we use gethostname(). Linking with -lws2_32 is the least evil. */ #define WIN32_LEAN_AND_MEAN /* Get winsock2.h. */ @@ -70,9 +82,13 @@ gethostname (char *name, size_t len) #undef gethostname int -rpl_gethostname (char *name, size_t namelen) +rpl_gethostname (char *name, size_t len) { - int r = gethostname (name, (int) namelen); + int r; + + if (len > INT_MAX) + len = INT_MAX; + r = gethostname (name, (int) len); if (r < 0) set_winsock_errno (); |