gethostname.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* gethostname emulation for SysV and POSIX.1.
  2. Copyright (C) 1992, 2003, 2006, 2008, 2009, 2010 Free Software Foundation,
  3. Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* David MacKenzie <djm@gnu.ai.mit.edu>
  15. Windows port by Simon Josefsson <simon@josefsson.org> */
  16. #include <config.h>
  17. #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
  18. /* Unix API. */
  19. /* Specification. */
  20. #include <unistd.h>
  21. #ifdef HAVE_UNAME
  22. # include <sys/utsname.h>
  23. #endif
  24. #include <string.h>
  25. /* Put up to LEN chars of the host name into NAME.
  26. Null terminate it if the name is shorter than LEN.
  27. Return 0 if ok, -1 if error. */
  28. #include <stddef.h>
  29. int
  30. gethostname (char *name, size_t len)
  31. {
  32. #ifdef HAVE_UNAME
  33. struct utsname uts;
  34. if (uname (&uts) == -1)
  35. return -1;
  36. if (len > sizeof (uts.nodename))
  37. {
  38. /* More space than we need is available. */
  39. name[sizeof (uts.nodename)] = '\0';
  40. len = sizeof (uts.nodename);
  41. }
  42. strncpy (name, uts.nodename, len);
  43. #else
  44. strcpy (name, ""); /* Hardcode your system name if you want. */
  45. #endif
  46. return 0;
  47. }
  48. #else
  49. /* Native Windows API. Which primitive to choose?
  50. - gethostname() requires linking with -lws2_32.
  51. - GetComputerName() does not return the right kind of hostname.
  52. - GetComputerNameEx(ComputerNameDnsHostname,...) returns the right hostname,
  53. but it is hard to use portably:
  54. - It requires defining _WIN32_WINNT to at least 0x0500.
  55. - With mingw, it also requires
  56. "#define GetComputerNameEx GetComputerNameExA".
  57. - With older versions of mingw, none of the declarations are present at
  58. all, not even of the enum value ComputerNameDnsHostname.
  59. So we use gethostname(). Linking with -lws2_32 is the least evil. */
  60. #define WIN32_LEAN_AND_MEAN
  61. /* Get winsock2.h. */
  62. #include <unistd.h>
  63. /* Get INT_MAX. */
  64. #include <limits.h>
  65. /* Get set_winsock_errno. */
  66. #include "w32sock.h"
  67. #include "sockets.h"
  68. #undef gethostname
  69. int
  70. rpl_gethostname (char *name, size_t len)
  71. {
  72. int r;
  73. if (len > INT_MAX)
  74. len = INT_MAX;
  75. gl_sockets_startup (SOCKETS_1_1);
  76. r = gethostname (name, (int) len);
  77. if (r < 0)
  78. set_winsock_errno ();
  79. return r;
  80. }
  81. #endif