bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gethostname on Windows


From: Bruno Haible
Subject: Re: gethostname on Windows
Date: Sun, 2 Aug 2009 12:04:53 +0200
User-agent: KMail/1.9.9

Simon Josefsson wrote:
> > I'm undecided.
> 
> Avoiding -lws2_32 is good, but if the application links to -lws2_32
> anyway, there will be no saving.
> ...
> Some additional questions:
> 
> 1) How do I use it?  This doesn't seem to work:
> 
> address@hidden:~$ cat foo.c
> #define WINVER 0x0500
> #include <windows.h>
> #include <stdio.h>
> int main () {
>   char out[1024];
>   DWORD size = 1024;
>   BOOL t = GetComputerNameEx(ComputerNameDnsHostname, out, &size);
>   printf("hi %d: %s", t, out);
> }
> address@hidden:~$ i586-mingw32msvc-gcc -o foo.exe foo.c 
> /tmp/ccDXn2yi.o:foo.c:(.text+0x37): undefined reference to 
> `_GetComputerNameEx'
> collect2: ld returned 1 exit status
> address@hidden:~$ 
>
> 2) What is the maximum string size that GetComputerNameEx can return?
>    For the gethostname, the max size is documented.
> 
> 3) Is the GetComputerNameEx semantics right?

You are right. In summary:

   - 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.
   - GetComputerName() does not return the right kind of hostname.
   - gethostname() requires linking with -lws2_32.

I'm now convinced that linking with -lws2_32 is the least evil.
I have applied your patch from 2009-03-31.

Also, I'm fixing the handling of len > INT_MAX.


2009-08-02  Bruno Haible  <address@hidden>

        * lib/gethostname.c (gethostname): Fix handling of large len argument.
        Add comments.

--- lib/gethostname.c.orig      2009-08-02 12:02:00.000000000 +0200
+++ lib/gethostname.c   2009-08-02 11:59:12.000000000 +0200
@@ -21,6 +21,7 @@
 #include <config.h>
 
 #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
+/* Unix API.  */
 
 /* Specification.  */
 #include <unistd.h>
@@ -59,6 +60,17 @@
 }
 
 #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 @@
 #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 ();
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]