fgetpwent_r.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library 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 GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <ctype.h>
  15. #include <errno.h>
  16. #include <stdio.h>
  17. #include <pwd.h>
  18. /* Define a line parsing function using the common code
  19. used in the nss_files module. */
  20. #define STRUCTURE passwd
  21. #define ENTNAME pwent
  22. struct pwent_data {};
  23. #include <nss/nss_files/files-parse.c>
  24. LINE_PARSER
  25. (,
  26. STRING_FIELD (result->pw_name, ISCOLON, 0);
  27. if (line[0] == '\0'
  28. && (result->pw_name[0] == '+' || result->pw_name[0] == '-'))
  29. {
  30. /* This a special case. We allow lines containing only a `+' sign
  31. since this is used for nss_compat. All other services will
  32. reject this entry later. Initialize all other fields now. */
  33. result->pw_passwd = NULL;
  34. result->pw_uid = 0;
  35. result->pw_gid = 0;
  36. result->pw_gecos = NULL;
  37. result->pw_dir = NULL;
  38. result->pw_shell = NULL;
  39. }
  40. else
  41. {
  42. STRING_FIELD (result->pw_passwd, ISCOLON, 0);
  43. if (result->pw_name[0] == '+' || result->pw_name[0] == '-')
  44. {
  45. INT_FIELD_MAYBE_NULL (result->pw_uid, ISCOLON, 0, 10, , 0)
  46. INT_FIELD_MAYBE_NULL (result->pw_gid, ISCOLON, 0, 10, , 0)
  47. }
  48. else
  49. {
  50. INT_FIELD (result->pw_uid, ISCOLON, 0, 10,)
  51. INT_FIELD (result->pw_gid, ISCOLON, 0, 10,)
  52. }
  53. STRING_FIELD (result->pw_gecos, ISCOLON, 0);
  54. STRING_FIELD (result->pw_dir, ISCOLON, 0);
  55. result->pw_shell = line;
  56. }
  57. )
  58. /* Read one entry from the given stream. */
  59. int
  60. __fgetpwent_r (FILE *stream, struct passwd *resbuf, char *buffer,
  61. size_t buflen, struct passwd **result)
  62. {
  63. int ret = __nss_fgetent_r (stream, resbuf, buffer, buflen, parse_line);
  64. if (ret == 0)
  65. *result = resbuf;
  66. else
  67. *result = NULL;
  68. return ret;
  69. }
  70. weak_alias (__fgetpwent_r, fgetpwent_r)