diff options
Diffstat (limited to 'gl/gethostname.c')
| -rw-r--r-- | gl/gethostname.c | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/gl/gethostname.c b/gl/gethostname.c index acff351d..b0da748f 100644 --- a/gl/gethostname.c +++ b/gl/gethostname.c | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | /* gethostname emulation for SysV and POSIX.1. | 1 | /* gethostname emulation for SysV and POSIX.1. |
| 2 | 2 | ||
| 3 | Copyright (C) 1992, 2003, 2006, 2008 Free Software Foundation, Inc. | 3 | Copyright (C) 1992, 2003, 2006, 2008, 2009, 2010 Free Software Foundation, |
| 4 | Inc. | ||
| 4 | 5 | ||
| 5 | This program is free software: you can redistribute it and/or modify | 6 | This program is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by | 7 | it under the terms of the GNU General Public License as published by |
| @@ -15,10 +16,14 @@ | |||
| 15 | You should have received a copy of the GNU General Public License | 16 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 17 | 18 | ||
| 18 | /* David MacKenzie <djm@gnu.ai.mit.edu> */ | 19 | /* David MacKenzie <djm@gnu.ai.mit.edu> |
| 20 | Windows port by Simon Josefsson <simon@josefsson.org> */ | ||
| 19 | 21 | ||
| 20 | #include <config.h> | 22 | #include <config.h> |
| 21 | 23 | ||
| 24 | #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) | ||
| 25 | /* Unix API. */ | ||
| 26 | |||
| 22 | /* Specification. */ | 27 | /* Specification. */ |
| 23 | #include <unistd.h> | 28 | #include <unistd.h> |
| 24 | 29 | ||
| @@ -50,7 +55,51 @@ gethostname (char *name, size_t len) | |||
| 50 | } | 55 | } |
| 51 | strncpy (name, uts.nodename, len); | 56 | strncpy (name, uts.nodename, len); |
| 52 | #else | 57 | #else |
| 53 | strcpy (name, ""); /* Hardcode your system name if you want. */ | 58 | strcpy (name, ""); /* Hardcode your system name if you want. */ |
| 54 | #endif | 59 | #endif |
| 55 | return 0; | 60 | return 0; |
| 56 | } | 61 | } |
| 62 | |||
| 63 | #else | ||
| 64 | /* Native Windows API. Which primitive to choose? | ||
| 65 | - gethostname() requires linking with -lws2_32. | ||
| 66 | - GetComputerName() does not return the right kind of hostname. | ||
| 67 | - GetComputerNameEx(ComputerNameDnsHostname,...) returns the right hostname, | ||
| 68 | but it is hard to use portably: | ||
| 69 | - It requires defining _WIN32_WINNT to at least 0x0500. | ||
| 70 | - With mingw, it also requires | ||
| 71 | "#define GetComputerNameEx GetComputerNameExA". | ||
| 72 | - With older versions of mingw, none of the declarations are present at | ||
| 73 | all, not even of the enum value ComputerNameDnsHostname. | ||
| 74 | So we use gethostname(). Linking with -lws2_32 is the least evil. */ | ||
| 75 | |||
| 76 | #define WIN32_LEAN_AND_MEAN | ||
| 77 | /* Get winsock2.h. */ | ||
| 78 | #include <unistd.h> | ||
| 79 | |||
| 80 | /* Get INT_MAX. */ | ||
| 81 | #include <limits.h> | ||
| 82 | |||
| 83 | /* Get set_winsock_errno. */ | ||
| 84 | #include "w32sock.h" | ||
| 85 | |||
| 86 | #include "sockets.h" | ||
| 87 | |||
| 88 | #undef gethostname | ||
| 89 | |||
| 90 | int | ||
| 91 | rpl_gethostname (char *name, size_t len) | ||
| 92 | { | ||
| 93 | int r; | ||
| 94 | |||
| 95 | if (len > INT_MAX) | ||
| 96 | len = INT_MAX; | ||
| 97 | gl_sockets_startup (SOCKETS_1_1); | ||
| 98 | r = gethostname (name, (int) len); | ||
| 99 | if (r < 0) | ||
| 100 | set_winsock_errno (); | ||
| 101 | |||
| 102 | return r; | ||
| 103 | } | ||
| 104 | |||
| 105 | #endif | ||
