__getgrent.c 4.7 KB

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