get-host.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Get a host configuration item kept as the whole contents of a file.
  2. Copyright (C) 1996-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <fcntl.h>
  16. #include <hurd.h>
  17. #include <hurd/lookup.h>
  18. #include "hurdhost.h"
  19. #include <string.h>
  20. ssize_t
  21. _hurd_get_host_config (const char *item, char *buf, size_t buflen)
  22. {
  23. error_t err;
  24. char *data;
  25. mach_msg_type_number_t nread;
  26. vm_size_t more;
  27. file_t config;
  28. err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0,
  29. item, O_RDONLY, 0, &config);
  30. switch (err)
  31. {
  32. case 0: /* Success; read file contents below. */
  33. break;
  34. case ENOENT: /* ? Others? All errors? */
  35. /* The file does not exist, so no value has been set. Rather than
  36. causing gethostname et al to fail with ENOENT, give an empty value
  37. as other systems do before sethostname has been called. */
  38. if (buflen != 0)
  39. *buf = '\0';
  40. return 0;
  41. default:
  42. return __hurd_fail (err);
  43. }
  44. data = buf;
  45. nread = buflen;
  46. err = __io_read (config, &data, &nread, -1, buflen);
  47. if (! err)
  48. /* Check if there is more in the file we didn't read. */
  49. err = __io_readable (config, &more);
  50. __mach_port_deallocate (__mach_task_self (), config);
  51. if (err)
  52. return __hurd_fail (err);
  53. if (data != buf)
  54. {
  55. memcpy (buf, data, nread);
  56. __vm_deallocate (__mach_task_self (), (vm_address_t) data, nread);
  57. }
  58. /* If the file is empty, give an empty value. */
  59. if (nread == 0 && more == 0)
  60. {
  61. if (buflen != 0)
  62. *buf = '\0';
  63. return 0;
  64. }
  65. /* Remove newlines in case someone wrote the file by hand. */
  66. while (nread > 0 && buf[nread - 1] == '\n')
  67. buf[--nread] = '\0';
  68. /* Null-terminate the result if there is enough space. */
  69. if (nread < buflen)
  70. buf[nread] = '\0';
  71. else
  72. if (nread != 0 && buf[nread - 1] != '\0')
  73. more = 1;
  74. if (more)
  75. /* If we didn't read the whole file, tell the caller to use a bigger
  76. buffer next time. */
  77. return __hurd_fail (ENAMETOOLONG);
  78. return nread;
  79. }