__sgetspent_r.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * __sgetspent_r.c - Based on __getpwent_r.c
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the Free
  16. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include "config.h"
  23. int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, size_t buflen)
  24. {
  25. char *field_begin;
  26. char *endptr;
  27. char *lstchg_ptr=NULL;
  28. char *min_ptr=NULL;
  29. char *max_ptr=NULL;
  30. char *warn_ptr=NULL;
  31. char *inact_ptr=NULL;
  32. char *expire_ptr=NULL;
  33. char *flag_ptr=NULL;
  34. int i;
  35. if (buflen<PWD_BUFFER_SIZE)
  36. return ERANGE;
  37. if (string != line_buff) {
  38. if (strlen(string) >= buflen)
  39. return ERANGE;
  40. strcpy(line_buff, string);
  41. }
  42. if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' ||
  43. *line_buff == '\t')
  44. return EINVAL;
  45. field_begin = strchr(line_buff, '\n');
  46. if (field_begin != NULL)
  47. *field_begin = '\0';
  48. /* We've read the line; now parse it. */
  49. field_begin = line_buff;
  50. for (i = 0; i < 9; i++) {
  51. switch (i) {
  52. case 0:
  53. spwd->sp_namp = field_begin;
  54. break;
  55. case 1:
  56. spwd->sp_pwdp = field_begin;
  57. break;
  58. case 2:
  59. lstchg_ptr = field_begin;
  60. break;
  61. case 3:
  62. min_ptr = field_begin;
  63. break;
  64. case 4:
  65. max_ptr = field_begin;
  66. break;
  67. case 5:
  68. warn_ptr = field_begin;
  69. break;
  70. case 6:
  71. inact_ptr = field_begin;
  72. break;
  73. case 7:
  74. expire_ptr = field_begin;
  75. break;
  76. case 8:
  77. flag_ptr = field_begin;
  78. break;
  79. }
  80. if (i < 8) {
  81. field_begin = strchr(field_begin, ':');
  82. if (field_begin == NULL) {
  83. if (i==4 || i==7)
  84. break;
  85. return EINVAL;
  86. }
  87. *field_begin++ = '\0';
  88. }
  89. }
  90. if (*lstchg_ptr == '\0') {
  91. spwd->sp_lstchg = -1;
  92. } else {
  93. spwd->sp_lstchg = (gid_t) strtoul(lstchg_ptr, &endptr, 10);
  94. if (*endptr != '\0')
  95. return EINVAL;
  96. }
  97. if (*min_ptr == '\0') {
  98. spwd->sp_min = -1;
  99. } else {
  100. spwd->sp_min = (gid_t) strtoul(min_ptr, &endptr, 10);
  101. if (*endptr != '\0')
  102. return EINVAL;
  103. }
  104. if (*max_ptr == '\0') {
  105. spwd->sp_max = -1;
  106. } else {
  107. spwd->sp_max = (gid_t) strtoul(max_ptr, &endptr, 10);
  108. if (*endptr != '\0')
  109. return EINVAL;
  110. }
  111. if (warn_ptr == NULL) {
  112. /* Support for old format */
  113. spwd->sp_warn = -1;
  114. spwd->sp_inact = -1;
  115. spwd->sp_expire = -1;
  116. spwd->sp_flag = 0;
  117. }
  118. else {
  119. if (*warn_ptr == '\0') {
  120. spwd->sp_warn = -1;
  121. } else {
  122. spwd->sp_warn = (gid_t) strtoul(warn_ptr, &endptr, 10);
  123. if (*endptr != '\0')
  124. return EINVAL;
  125. }
  126. if (*inact_ptr == '\0') {
  127. spwd->sp_inact = -1;
  128. } else {
  129. spwd->sp_inact = (gid_t) strtoul(inact_ptr, &endptr, 10);
  130. if (*endptr != '\0')
  131. return EINVAL;
  132. }
  133. if (*expire_ptr == '\0') {
  134. spwd->sp_expire = -1;
  135. } else {
  136. spwd->sp_expire = (gid_t) strtoul(expire_ptr, &endptr, 10);
  137. if (*endptr != '\0')
  138. return EINVAL;
  139. }
  140. if (flag_ptr==NULL || *flag_ptr=='\0') {
  141. spwd->sp_flag = ~0ul;
  142. } else {
  143. spwd->sp_flag = (gid_t) strtoul(flag_ptr, &endptr, 10);
  144. if (*endptr != '\0')
  145. return EINVAL;
  146. }
  147. }
  148. return 0;
  149. }