__getgrent.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * __getgrent.c - This file is part of the libc-8086/grp package for ELKS,
  3. * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, write to the Free
  17. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. */
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "config.h"
  24. /*
  25. * This is the core group-file read function. It behaves exactly like
  26. * getgrent() except that it is passed a file descriptor. getgrent()
  27. * is just a wrapper for this function.
  28. */
  29. struct group *__getgrent(int grp_fd)
  30. {
  31. #ifdef GR_SCALE_DYNAMIC
  32. static char *line_buff = NULL;
  33. static char **members = NULL;
  34. short line_index;
  35. short buff_size;
  36. #else
  37. static char line_buff[GR_MAX_LINE_LEN];
  38. static char *members[GR_MAX_MEMBERS];
  39. #endif
  40. static struct group group;
  41. register char *ptr;
  42. char *field_begin;
  43. short member_num;
  44. char *endptr;
  45. int line_len;
  46. /* We use the restart label to handle malformatted lines */
  47. restart:
  48. #ifdef GR_SCALE_DYNAMIC
  49. line_index = 0;
  50. buff_size = 256;
  51. #endif
  52. #ifdef GR_SCALE_DYNAMIC
  53. line_buff = realloc(line_buff, buff_size);
  54. while (1) {
  55. if ((line_len = read(grp_fd, line_buff + line_index,
  56. buff_size - line_index)) <= 0)
  57. return NULL;
  58. field_begin = strchr(line_buff, '\n');
  59. if (field_begin != NULL) {
  60. lseek(grp_fd,
  61. (long) (1 + field_begin -
  62. (line_len + line_index + line_buff)), SEEK_CUR);
  63. *field_begin = '\0';
  64. if (*line_buff == '#' || *line_buff == ' '
  65. || *line_buff == '\n' || *line_buff == '\t')
  66. goto restart;
  67. break;
  68. } else { /* Allocate some more space */
  69. line_index = buff_size;
  70. buff_size += 256;
  71. line_buff = realloc(line_buff, buff_size);
  72. }
  73. }
  74. #else
  75. /* Read the line into the static buffer */
  76. if ((line_len = read(grp_fd, line_buff, GR_MAX_LINE_LEN)) <= 0)
  77. return NULL;
  78. field_begin = strchr(line_buff, '\n');
  79. if (field_begin != NULL)
  80. lseek(grp_fd, (long) (1 + field_begin - (line_buff + line_len)),
  81. SEEK_CUR);
  82. else { /* The line is too long - skip it :-\ */
  83. do {
  84. if ((line_len = read(grp_fd, line_buff, GR_MAX_LINE_LEN)) <= 0)
  85. return NULL;
  86. } while (!(field_begin = strchr(line_buff, '\n')));
  87. lseek(grp_fd, (long) ((field_begin - line_buff) - line_len + 1),
  88. SEEK_CUR);
  89. goto restart;
  90. }
  91. if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' ||
  92. *line_buff == '\t')
  93. goto restart;
  94. *field_begin = '\0';
  95. #endif /* GR_SCALE_DYNAMIC */
  96. /* Now parse the line */
  97. group.gr_name = line_buff;
  98. ptr = strchr(line_buff, ':');
  99. if (ptr == NULL)
  100. goto restart;
  101. *ptr++ = '\0';
  102. group.gr_passwd = ptr;
  103. ptr = strchr(ptr, ':');
  104. if (ptr == NULL)
  105. goto restart;
  106. *ptr++ = '\0';
  107. field_begin = ptr;
  108. ptr = strchr(ptr, ':');
  109. if (ptr == NULL)
  110. goto restart;
  111. *ptr++ = '\0';
  112. group.gr_gid = (gid_t) strtoul(field_begin, &endptr, 10);
  113. if (*endptr != '\0')
  114. goto restart;
  115. member_num = 0;
  116. field_begin = ptr;
  117. #ifdef GR_SCALE_DYNAMIC
  118. if (members != NULL)
  119. free(members);
  120. members = (char **) malloc((member_num + 1) * sizeof(char *));
  121. for ( ; field_begin && *field_begin != '\0'; field_begin = ptr) {
  122. if ((ptr = strchr(field_begin, ',')) != NULL)
  123. *ptr++ = '\0';
  124. members[member_num++] = field_begin;
  125. members = (char **) realloc(members,
  126. (member_num + 1) * sizeof(char *));
  127. }
  128. members[member_num] = NULL;
  129. #else
  130. while ((ptr = strchr(ptr, ',')) != NULL) {
  131. *ptr = '\0';
  132. ptr++;
  133. members[member_num] = field_begin;
  134. field_begin = ptr;
  135. member_num++;
  136. }
  137. if (*field_begin == '\0')
  138. members[member_num] = NULL;
  139. else {
  140. members[member_num] = field_begin;
  141. members[member_num + 1] = NULL;
  142. }
  143. #endif
  144. group.gr_mem = members;
  145. return &group;
  146. }