glibc/nss/nss_readline.c
Stefan Liebler 0e1f068108 Fix linknamespace errors and local-plt-usages in nss_files.
After commit f9c8b11ed7
"nss: Access nss_files through direct references",
when building with -Os, multiple conform/.../linknamespace tests
and elf/check-localplt are failing:
Extra PLT reference: libc.so: fgetc_unlocked
Extra PLT reference: libc.so: getline

Or e.g.:
[initial] glob -> [libc.a(glob.o)] __getpwnam_r -> [libc.a(getpwnam_r.o)] __nss_database_custom -> [libc.a(nsswitch.o)] __nss_module_get_function -> [libc.a(nss_module.o)] __nss_files_functions -> [libc.a(nss_files_functions.o)] _nss_files_endaliasent -> [libc.a(files-alias.o)] feof_unlocked
[initial] glob -> [libc.a(glob.o)] __getpwnam_r -> [libc.a(getpwnam_r.o)] __nss_database_custom -> [libc.a(nsswitch.o)] __nss_module_get_function -> [libc.a(nss_module.o)] __nss_files_functions -> [libc.a(nss_files_functions.o)] _nss_files_endaliasent -> [libc.a(files-alias.o)] fgetc_unlocked
[initial] glob -> [libc.a(glob.o)] __getpwnam_r -> [libc.a(getpwnam_r.o)] __nss_database_custom -> [libc.a(nsswitch.o)] __nss_module_get_function -> [libc.a(nss_module.o)] __nss_files_functions -> [libc.a(nss_files_functions.o)] _nss_files_endnetgrent -> [libc.a(files-netgrp.o)] getline

This patch is using the hidden symbols where possible.
Instead of fputc_unlocked, __putc_unlocked is used.
(Compare to commit eeaa19f75e
"mntent: Use __putc_unlocked instead of fputc_unlocked")
2021-07-14 16:59:12 +02:00

100 lines
2.8 KiB
C

/* Read a line from an nss_files database file.
Copyright (C) 2020-2021 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <nss_files.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
int
__nss_readline (FILE *fp, char *buf, size_t len, off64_t *poffset)
{
/* We need space for at least one character, the line terminator,
and the NUL byte. */
if (len < 3)
{
*poffset = -1;
__set_errno (ERANGE);
return ERANGE;
}
while (true)
{
/* Keep original offset for retries. */
*poffset = __ftello64 (fp);
buf[len - 1] = '\xff'; /* Marker to recognize truncation. */
if (__fgets_unlocked (buf, len, fp) == NULL)
{
if (__feof_unlocked (fp))
{
__set_errno (ENOENT);
return ENOENT;
}
else
{
/* Any other error. Do not return ERANGE in this case
because the caller would retry. */
if (errno == ERANGE)
__set_errno (EINVAL);
return errno;
}
}
else if (buf[len - 1] != '\xff')
/* The buffer is too small. Arrange for re-reading the same
line on the next call. */
return __nss_readline_seek (fp, *poffset);
/* __fgets_unlocked succeeded. */
/* Remove leading whitespace. */
char *p = buf;
while (isspace (*p))
++p;
if (*p == '\0' || *p == '#')
/* Skip empty lines and comments. */
continue;
if (p != buf)
memmove (buf, p, strlen (p));
/* Return line to the caller. */
return 0;
}
}
libc_hidden_def (__nss_readline)
int
__nss_readline_seek (FILE *fp, off64_t offset)
{
if (offset < 0 /* __ftello64 failed. */
|| __fseeko64 (fp, offset, SEEK_SET) < 0)
{
/* Without seeking support, it is not possible to
re-read the same line, so this is a hard failure. */
fseterr_unlocked (fp);
__set_errno (ESPIPE);
return ESPIPE;
}
else
{
__set_errno (ERANGE);
return ERANGE;
}
}