sgetspent_r.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright (C) 1996-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 <shadow.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. /* Define a line parsing function using the common code
  20. used in the nss_files module. */
  21. #define STRUCTURE spwd
  22. #define ENTNAME spent
  23. struct spent_data {};
  24. /* Predicate which always returns false, needed below. */
  25. #define FALSEP(arg) 0
  26. #include <nss/nss_files/files-parse.c>
  27. LINE_PARSER
  28. (,
  29. STRING_FIELD (result->sp_namp, ISCOLON, 0);
  30. if (line[0] == '\0'
  31. && (result->sp_namp[0] == '+' || result->sp_namp[0] == '-'))
  32. {
  33. result->sp_pwdp = NULL;
  34. result->sp_lstchg = 0;
  35. result->sp_min = 0;
  36. result->sp_max = 0;
  37. result->sp_warn = -1l;
  38. result->sp_inact = -1l;
  39. result->sp_expire = -1l;
  40. result->sp_flag = ~0ul;
  41. }
  42. else
  43. {
  44. STRING_FIELD (result->sp_pwdp, ISCOLON, 0);
  45. INT_FIELD_MAYBE_NULL (result->sp_lstchg, ISCOLON, 0, 10, (long int) (int),
  46. (long int) -1);
  47. INT_FIELD_MAYBE_NULL (result->sp_min, ISCOLON, 0, 10, (long int) (int),
  48. (long int) -1);
  49. INT_FIELD_MAYBE_NULL (result->sp_max, ISCOLON, 0, 10, (long int) (int),
  50. (long int) -1);
  51. while (isspace (*line))
  52. ++line;
  53. if (*line == '\0')
  54. {
  55. /* The old form. */
  56. result->sp_warn = -1l;
  57. result->sp_inact = -1l;
  58. result->sp_expire = -1l;
  59. result->sp_flag = ~0ul;
  60. }
  61. else
  62. {
  63. INT_FIELD_MAYBE_NULL (result->sp_warn, ISCOLON, 0, 10,
  64. (long int) (int), (long int) -1);
  65. INT_FIELD_MAYBE_NULL (result->sp_inact, ISCOLON, 0, 10,
  66. (long int) (int), (long int) -1);
  67. INT_FIELD_MAYBE_NULL (result->sp_expire, ISCOLON, 0, 10,
  68. (long int) (int), (long int) -1);
  69. if (*line != '\0')
  70. INT_FIELD_MAYBE_NULL (result->sp_flag, FALSEP, 0, 10,
  71. (unsigned long int), ~0ul)
  72. else
  73. result->sp_flag = ~0ul;
  74. }
  75. }
  76. )
  77. /* Read one shadow entry from the given stream. */
  78. int
  79. __sgetspent_r (const char *string, struct spwd *resbuf, char *buffer,
  80. size_t buflen, struct spwd **result)
  81. {
  82. buffer[buflen - 1] = '\0';
  83. char *sp = strncpy (buffer, string, buflen);
  84. if (buffer[buflen - 1] != '\0')
  85. return ERANGE;
  86. int parse_result = parse_line (sp, resbuf, NULL, 0, &errno);
  87. *result = parse_result > 0 ? resbuf : NULL;
  88. return *result == NULL ? errno : 0;
  89. }
  90. weak_alias (__sgetspent_r, sgetspent_r)