__getgrent.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <grp.h>
  24. #include "config.h"
  25. /*
  26. * This is the core group-file read function. It behaves exactly like
  27. * getgrent() except that it is passed a file descriptor. getgrent()
  28. * is just a wrapper for this function.
  29. */
  30. struct group *__getgrent(int grp_fd)
  31. {
  32. #ifndef GR_SCALE_DYNAMIC
  33. static char line_buff[GR_MAX_LINE_LEN];
  34. static char *members[GR_MAX_MEMBERS];
  35. #else
  36. static char *line_buff = NULL;
  37. static char **members = NULL;
  38. short line_index;
  39. short buff_size;
  40. #endif
  41. static struct group group;
  42. register char *ptr;
  43. char *field_begin;
  44. short member_num;
  45. char *endptr;
  46. int line_len;
  47. /* We use the restart label to handle malformatted lines */
  48. restart:
  49. #ifdef GR_SCALE_DYNAMIC
  50. line_index = 0;
  51. buff_size = 256;
  52. #endif
  53. #ifndef GR_SCALE_DYNAMIC
  54. /* Read the line into the static buffer */
  55. if ((line_len = read(grp_fd, line_buff, GR_MAX_LINE_LEN)) <= 0)
  56. return NULL;
  57. field_begin = strchr(line_buff, '\n');
  58. if (field_begin != NULL)
  59. lseek(grp_fd, (long) (1 + field_begin - (line_buff + line_len)),
  60. SEEK_CUR);
  61. else { /* The line is too long - skip it :-\ */
  62. do {
  63. if ((line_len = read(grp_fd, line_buff, GR_MAX_LINE_LEN)) <= 0)
  64. return NULL;
  65. } while (!(field_begin = strchr(line_buff, '\n')));
  66. lseek(grp_fd, (long) ((field_begin - line_buff) - line_len + 1),
  67. SEEK_CUR);
  68. goto restart;
  69. }
  70. if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' ||
  71. *line_buff == '\t')
  72. goto restart;
  73. *field_begin = '\0';
  74. #else /* !GR_SCALE_DYNAMIC */
  75. line_buff = realloc(line_buff, buff_size);
  76. while (1) {
  77. if ((line_len = read(grp_fd, line_buff + line_index,
  78. buff_size - line_index)) <= 0)
  79. return NULL;
  80. field_begin = strchr(line_buff, '\n');
  81. if (field_begin != NULL) {
  82. lseek(grp_fd,
  83. (long) (1 + field_begin -
  84. (line_len + line_index + line_buff)), SEEK_CUR);
  85. *field_begin = '\0';
  86. if (*line_buff == '#' || *line_buff == ' '
  87. || *line_buff == '\n' || *line_buff == '\t')
  88. goto restart;
  89. break;
  90. } else { /* Allocate some more space */
  91. line_index = buff_size;
  92. buff_size += 256;
  93. line_buff = realloc(line_buff, buff_size);
  94. }
  95. }
  96. #endif /* GR_SCALE_DYNAMIC */
  97. /* Now parse the line */
  98. group.gr_name = line_buff;
  99. ptr = strchr(line_buff, ':');
  100. if (ptr == NULL)
  101. goto restart;
  102. *ptr++ = '\0';
  103. group.gr_passwd = ptr;
  104. ptr = strchr(ptr, ':');
  105. if (ptr == NULL)
  106. goto restart;
  107. *ptr++ = '\0';
  108. field_begin = ptr;
  109. ptr = strchr(ptr, ':');
  110. if (ptr == NULL)
  111. goto restart;
  112. *ptr++ = '\0';
  113. group.gr_gid = (gid_t) strtoul(field_begin, &endptr, 10);
  114. if (*endptr != '\0')
  115. goto restart;
  116. member_num = 0;
  117. field_begin = ptr;
  118. #ifndef GR_SCALE_DYNAMIC
  119. while ((ptr = strchr(ptr, ',')) != NULL) {
  120. *ptr = '\0';
  121. ptr++;
  122. members[member_num] = field_begin;
  123. field_begin = ptr;
  124. member_num++;
  125. }
  126. if (*field_begin == '\0')
  127. members[member_num] = NULL;
  128. else {
  129. members[member_num] = field_begin;
  130. members[member_num + 1] = NULL;
  131. }
  132. #else /* !GR_SCALE_DYNAMIC */
  133. if (members != NULL)
  134. free(members);
  135. members = (char **) malloc((member_num + 1) * sizeof(char *));
  136. for ( ; field_begin && *field_begin != '\0'; field_begin = ptr) {
  137. if ((ptr = strchr(field_begin, ',')) != NULL)
  138. *ptr++ = '\0';
  139. members[member_num++] = field_begin;
  140. members = (char **) realloc(members,
  141. (member_num + 1) * sizeof(char *));
  142. }
  143. members[member_num] = NULL;
  144. #endif /* GR_SCALE_DYNAMIC */
  145. group.gr_mem = members;
  146. return &group;
  147. }