gethostname.c 2.7 KB

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