Bruno Haible <br...@clisp.org> writes: > Simon Josefsson wrote: >> The current gethostname module will return an empty string on mingw: >> >> strcpy (name, ""); /* Hardcode your system name if you want. */ >> >> This is sub-optimal since Windows has a gethostname function in >> -lws2_32. >> >> The following patch should make gethostname return proper values. >> Tested on x86 debian lenny and mingw cross-compile. Any objections to >> installing it? > > Well, I don't much like the dependency on an external library for the > modules 'gethostname' and 'uname' (later, when we get an 'uname' module). > > I did some experiments with the GetComputerNameEx function [1], which is > present in kernel32.dll. The parameters ComputerNameDnsHostname and > ComputerNamePhysicalDnsHostname returned the same value as gethostname() > for me, except that gethostname() returns a lowercased result (which can > be simulated in 3 lines of code). > > OTOH, the gethostname() in -lws2_32 is a "Unixy" API, and it seems idiotic > want to use a "Windozy" API instead. > > I'm undecided.
Avoiding -lws2_32 is good, but if the application links to -lws2_32 anyway, there will be no saving. Maybe the gethostname module could detect this? I.e., if the gnulib module sys_sockets_h is also used, so the application is likely to link to ws2_32 anyway, it can use -lws2_32 otherwise it will fall back on GetComputerNameEx. I'm not sure how to implement a M4 test like that though? Anyway, that solution isn't very reliable: you may want to use gethostname in an application that doesn't use sockets otherwise. Maybe two different gnulib modules then? gethostname uses -lws2_32 and gethostname-lean uses GetComputerNameEx? Some additional questions: 1) How do I use it? This doesn't seem to work: j...@mocca:~$ 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); } j...@mocca:~$ 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 j...@mocca:~$ 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? It seems the gethostname result will be different in some configurations? I'm not sure how important that is, but for gss/shishi the use of gethostname is to derive a Kerberos V5 principal name so there could be security implications if the value is not right. /Simon