nss_readline.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Read a line from an nss_files database file.
  2. Copyright (C) 2020-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 <nss_files.h>
  16. #include <ctype.h>
  17. #include <errno.h>
  18. #include <string.h>
  19. int
  20. __nss_readline (FILE *fp, char *buf, size_t len, off64_t *poffset)
  21. {
  22. /* We need space for at least one character, the line terminator,
  23. and the NUL byte. */
  24. if (len < 3)
  25. {
  26. *poffset = -1;
  27. __set_errno (ERANGE);
  28. return ERANGE;
  29. }
  30. while (true)
  31. {
  32. /* Keep original offset for retries. */
  33. *poffset = __ftello64 (fp);
  34. buf[len - 1] = '\xff'; /* Marker to recognize truncation. */
  35. if (__fgets_unlocked (buf, len, fp) == NULL)
  36. {
  37. if (__feof_unlocked (fp))
  38. {
  39. __set_errno (ENOENT);
  40. return ENOENT;
  41. }
  42. else
  43. {
  44. /* Any other error. Do not return ERANGE in this case
  45. because the caller would retry. */
  46. if (errno == ERANGE)
  47. __set_errno (EINVAL);
  48. return errno;
  49. }
  50. }
  51. else if (buf[len - 1] != '\xff')
  52. /* The buffer is too small. Arrange for re-reading the same
  53. line on the next call. */
  54. return __nss_readline_seek (fp, *poffset);
  55. /* __fgets_unlocked succeeded. */
  56. /* Remove leading whitespace. */
  57. char *p = buf;
  58. while (isspace (*p))
  59. ++p;
  60. if (*p == '\0' || *p == '#')
  61. /* Skip empty lines and comments. */
  62. continue;
  63. if (p != buf)
  64. memmove (buf, p, strlen (p));
  65. /* Return line to the caller. */
  66. return 0;
  67. }
  68. }
  69. libc_hidden_def (__nss_readline)
  70. int
  71. __nss_readline_seek (FILE *fp, off64_t offset)
  72. {
  73. if (offset < 0 /* __ftello64 failed. */
  74. || __fseeko64 (fp, offset, SEEK_SET) < 0)
  75. {
  76. /* Without seeking support, it is not possible to
  77. re-read the same line, so this is a hard failure. */
  78. fseterr_unlocked (fp);
  79. __set_errno (ESPIPE);
  80. return ESPIPE;
  81. }
  82. else
  83. {
  84. __set_errno (ERANGE);
  85. return ERANGE;
  86. }
  87. }