__getgrent.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, char *line_buff, char **members)
  40. {
  41. short line_index;
  42. short buff_size;
  43. static struct group group;
  44. register char *ptr;
  45. char *field_begin;
  46. short member_num;
  47. char *endptr;
  48. int line_len;
  49. /* We use the restart label to handle malformatted lines */
  50. restart:
  51. line_index = 0;
  52. buff_size = 256;
  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. }
  59. field_begin = strchr(line_buff, '\n');
  60. if (field_begin != NULL) {
  61. lseek(grp_fd,
  62. (long) (1 + field_begin -
  63. (line_len + line_index + line_buff)), SEEK_CUR);
  64. *field_begin = '\0';
  65. if (*line_buff == '#' || *line_buff == ' '
  66. || *line_buff == '\n' || *line_buff == '\t')
  67. goto restart;
  68. break;
  69. } else { /* Allocate some more space */
  70. line_index = buff_size;
  71. buff_size += 256;
  72. line_buff = realloc(line_buff, buff_size);
  73. }
  74. }
  75. /* Now parse the line */
  76. group.gr_name = line_buff;
  77. ptr = strchr(line_buff, ':');
  78. if (ptr == NULL)
  79. goto restart;
  80. *ptr++ = '\0';
  81. group.gr_passwd = ptr;
  82. ptr = strchr(ptr, ':');
  83. if (ptr == NULL)
  84. goto restart;
  85. *ptr++ = '\0';
  86. field_begin = ptr;
  87. ptr = strchr(ptr, ':');
  88. if (ptr == NULL)
  89. goto restart;
  90. *ptr++ = '\0';
  91. group.gr_gid = (gid_t) strtoul(field_begin, &endptr, 10);
  92. if (*endptr != '\0')
  93. goto restart;
  94. member_num = 0;
  95. field_begin = ptr;
  96. if (members != NULL)
  97. free(members);
  98. members = (char **) malloc((member_num + 1) * sizeof(char *));
  99. for ( ; field_begin && *field_begin != '\0'; field_begin = ptr) {
  100. if ((ptr = strchr(field_begin, ',')) != NULL)
  101. *ptr++ = '\0';
  102. members[member_num++] = field_begin;
  103. members = (char **) realloc(members,
  104. (member_num + 1) * sizeof(char *));
  105. }
  106. members[member_num] = NULL;
  107. group.gr_mem = members;
  108. return &group;
  109. }