__sgetspent_r.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <shadow.h>
  22. int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, size_t buflen)
  23. {
  24. char *field_begin;
  25. char *endptr;
  26. char *lstchg_ptr=NULL;
  27. char *min_ptr=NULL;
  28. char *max_ptr=NULL;
  29. char *warn_ptr=NULL;
  30. char *inact_ptr=NULL;
  31. char *expire_ptr=NULL;
  32. char *flag_ptr=NULL;
  33. int i;
  34. if (string != line_buff) {
  35. if (strlen(string) >= buflen)
  36. return -1;
  37. strcpy(line_buff, string);
  38. }
  39. if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' ||
  40. *line_buff == '\t')
  41. return -1;
  42. field_begin = strchr(line_buff, '\n');
  43. if (field_begin != NULL)
  44. *field_begin = '\0';
  45. /* We've read the line; now parse it. */
  46. field_begin = line_buff;
  47. for (i = 0; i < 9; i++) {
  48. switch (i) {
  49. case 0:
  50. spwd->sp_namp = field_begin;
  51. break;
  52. case 1:
  53. spwd->sp_pwdp = field_begin;
  54. break;
  55. case 2:
  56. lstchg_ptr = field_begin;
  57. break;
  58. case 3:
  59. min_ptr = field_begin;
  60. break;
  61. case 4:
  62. max_ptr = field_begin;
  63. break;
  64. case 5:
  65. warn_ptr = field_begin;
  66. break;
  67. case 6:
  68. inact_ptr = field_begin;
  69. break;
  70. case 7:
  71. expire_ptr = field_begin;
  72. break;
  73. case 8:
  74. flag_ptr = field_begin;
  75. break;
  76. }
  77. if (i < 8) {
  78. field_begin = strchr(field_begin, ':');
  79. if (field_begin == NULL) {
  80. if (i==4 || i==7)
  81. break;
  82. return -1;
  83. }
  84. *field_begin++ = '\0';
  85. }
  86. }
  87. if (*lstchg_ptr == '\0') {
  88. spwd->sp_lstchg = -1;
  89. } else {
  90. spwd->sp_lstchg = (gid_t) strtoul(lstchg_ptr, &endptr, 10);
  91. if (*endptr != '\0')
  92. return -1;
  93. }
  94. if (*min_ptr == '\0') {
  95. spwd->sp_min = -1;
  96. } else {
  97. spwd->sp_min = (gid_t) strtoul(min_ptr, &endptr, 10);
  98. if (*endptr != '\0')
  99. return -1;
  100. }
  101. if (*max_ptr == '\0') {
  102. spwd->sp_max = -1;
  103. } else {
  104. spwd->sp_max = (gid_t) strtoul(max_ptr, &endptr, 10);
  105. if (*endptr != '\0')
  106. return -1;
  107. }
  108. if (warn_ptr == NULL) {
  109. /* Support for old format */
  110. spwd->sp_warn = -1;
  111. spwd->sp_inact = -1;
  112. spwd->sp_expire = -1;
  113. spwd->sp_flag = 0;
  114. }
  115. else {
  116. if (*warn_ptr == '\0') {
  117. spwd->sp_warn = -1;
  118. } else {
  119. spwd->sp_warn = (gid_t) strtoul(warn_ptr, &endptr, 10);
  120. if (*endptr != '\0')
  121. return -1;
  122. }
  123. if (*inact_ptr == '\0') {
  124. spwd->sp_inact = -1;
  125. } else {
  126. spwd->sp_inact = (gid_t) strtoul(inact_ptr, &endptr, 10);
  127. if (*endptr != '\0')
  128. return -1;
  129. }
  130. if (*expire_ptr == '\0') {
  131. spwd->sp_expire = -1;
  132. } else {
  133. spwd->sp_expire = (gid_t) strtoul(expire_ptr, &endptr, 10);
  134. if (*endptr != '\0')
  135. return -1;
  136. }
  137. if (flag_ptr==NULL || *flag_ptr=='\0') {
  138. spwd->sp_flag = ~0ul;
  139. } else {
  140. spwd->sp_flag = (gid_t) strtoul(flag_ptr, &endptr, 10);
  141. if (*endptr != '\0')
  142. return -1;
  143. }
  144. }
  145. return 0;
  146. }