1 /* Read a line from an nss_files database file.
2 Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <nss_files.h>
26 __nss_readline (FILE *fp
, char *buf
, size_t len
, off64_t
*poffset
)
28 /* We need space for at least one character, the line terminator,
39 /* Keep original offset for retries. */
40 *poffset
= __ftello64 (fp
);
42 buf
[len
- 1] = '\xff'; /* Marker to recognize truncation. */
43 if (__fgets_unlocked (buf
, len
, fp
) == NULL
)
45 if (__feof_unlocked (fp
))
52 /* Any other error. Do not return ERANGE in this case
53 because the caller would retry. */
59 else if (buf
[len
- 1] != '\xff')
60 /* The buffer is too small. Arrange for re-reading the same
61 line on the next call. */
62 return __nss_readline_seek (fp
, *poffset
);
64 /* __fgets_unlocked succeeded. */
66 /* Remove leading whitespace. */
70 if (*p
== '\0' || *p
== '#')
71 /* Skip empty lines and comments. */
74 memmove (buf
, p
, strlen (p
));
76 /* Return line to the caller. */
80 libc_hidden_def (__nss_readline
)
83 __nss_readline_seek (FILE *fp
, off64_t offset
)
85 if (offset
< 0 /* __ftello64 failed. */
86 || __fseeko64 (fp
, offset
, SEEK_SET
) < 0)
88 /* Without seeking support, it is not possible to
89 re-read the same line, so this is a hard failure. */
90 fseterr_unlocked (fp
);