__getgrent.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /* This function should always be called under lock, so we
  28. * do not lock things in here... */
  29. pthread_mutex_t __getgrent_lock = PTHREAD_MUTEX_INITIALIZER;
  30. #endif
  31. /*
  32. * This is the core group-file read function. It behaves exactly like
  33. * getgrent() except that it is passed a file descriptor. getgrent()
  34. * is just a wrapper for this function.
  35. */
  36. struct group *__getgrent(int grp_fd, char *line_buff, char **members)
  37. {
  38. short line_index;
  39. short buff_size;
  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. line_index = 0;
  49. buff_size = 256;
  50. line_buff = realloc(line_buff, buff_size);
  51. while (1) {
  52. if ((line_len = read(grp_fd, line_buff + line_index,
  53. buff_size - line_index)) <= 0) {
  54. return NULL;
  55. }
  56. field_begin = strchr(line_buff, '\n');
  57. if (field_begin != NULL) {
  58. lseek(grp_fd,
  59. (long) (1 + field_begin -
  60. (line_len + line_index + line_buff)), SEEK_CUR);
  61. *field_begin = '\0';
  62. if (*line_buff == '#' || *line_buff == ' '
  63. || *line_buff == '\n' || *line_buff == '\t')
  64. goto restart;
  65. break;
  66. } else { /* Allocate some more space */
  67. line_index = buff_size;
  68. buff_size += 256;
  69. line_buff = realloc(line_buff, buff_size);
  70. }
  71. }
  72. /* Now parse the line */
  73. group.gr_name = line_buff;
  74. ptr = strchr(line_buff, ':');
  75. if (ptr == NULL)
  76. goto restart;
  77. *ptr++ = '\0';
  78. group.gr_passwd = ptr;
  79. ptr = strchr(ptr, ':');
  80. if (ptr == NULL)
  81. goto restart;
  82. *ptr++ = '\0';
  83. field_begin = ptr;
  84. ptr = strchr(ptr, ':');
  85. if (ptr == NULL)
  86. goto restart;
  87. *ptr++ = '\0';
  88. group.gr_gid = (gid_t) strtoul(field_begin, &endptr, 10);
  89. if (*endptr != '\0')
  90. goto restart;
  91. member_num = 0;
  92. field_begin = ptr;
  93. if (members != NULL)
  94. free(members);
  95. members = (char **) malloc((member_num + 1) * sizeof(char *));
  96. for ( ; field_begin && *field_begin != '\0'; field_begin = ptr) {
  97. if ((ptr = strchr(field_begin, ',')) != NULL)
  98. *ptr++ = '\0';
  99. members[member_num++] = field_begin;
  100. members = (char **) realloc(members,
  101. (member_num + 1) * sizeof(char *));
  102. }
  103. members[member_num] = NULL;
  104. group.gr_mem = members;
  105. return &group;
  106. }