__getgrent_r.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * __getgrent.c - This file is part of the libc-8086/grp package for ELKS,
  4. * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>.
  5. * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the Free
  19. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <features.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include "config.h"
  28. /*
  29. * This is the core group-file read function. It behaves exactly like
  30. * getgrent() except that it is passed a file descriptor. getgrent()
  31. * is just a wrapper for this function.
  32. */
  33. int __getgrent_r (struct group *__restrict group,
  34. char *__restrict line_buff, size_t buflen, int grp_fd)
  35. {
  36. char *endptr, *field_begin, **members;
  37. int i, line_len, member_num = 0;
  38. if (buflen<GRP_BUFFER_SIZE) {
  39. return ERANGE;
  40. }
  41. /* We use the restart label to handle malformatted lines */
  42. restart:
  43. /* Read the group line into the buffer for processing */
  44. if ((line_len = read(grp_fd, line_buff, buflen)) <= 0) {
  45. return EIO;
  46. }
  47. field_begin = strchr(line_buff, '\n');
  48. if (field_begin != NULL)
  49. lseek(grp_fd, (long) (1 + field_begin - (line_buff + line_len)), SEEK_CUR);
  50. else {
  51. /* The line is too long - skip it. :-\ */
  52. do {
  53. if ((line_len = read(grp_fd, line_buff, buflen)) <= 0) {
  54. return EIO;
  55. }
  56. } while (!(field_begin = strchr(line_buff, '\n')));
  57. lseek(grp_fd, (long) (field_begin - line_buff) - line_len + 1, SEEK_CUR);
  58. goto restart;
  59. }
  60. if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' || *line_buff == '\t')
  61. goto restart;
  62. *field_begin = '\0';
  63. /* We've read the line; now parse it. */
  64. field_begin = line_buff;
  65. for (i = 0; i < 3; i++) {
  66. switch (i) {
  67. case 0:
  68. group->gr_name = field_begin;
  69. break;
  70. case 1:
  71. group->gr_passwd = field_begin;
  72. break;
  73. case 2:
  74. group->gr_gid = (__gid_t) strtoul(field_begin, &endptr, 10);
  75. if (*endptr != ':')
  76. goto restart;
  77. break;
  78. }
  79. if (i < 3) {
  80. field_begin = strchr(field_begin, ':');
  81. if (field_begin == NULL)
  82. break;
  83. *field_begin++ = '\0';
  84. }
  85. }
  86. members = (char **) malloc(sizeof(char *));
  87. if (members==NULL) {
  88. return ENOMEM;
  89. }
  90. while(field_begin && strlen(field_begin)) {
  91. members[member_num++] = field_begin;
  92. members = (char **) realloc(members, (member_num + 1) * sizeof(char *));
  93. if (members==NULL) {
  94. return ENOMEM;
  95. }
  96. endptr = strchr(field_begin, ',');
  97. if (endptr == NULL) {
  98. /* Final entry */
  99. break;
  100. }
  101. *field_begin++ = '\0';
  102. }
  103. members[member_num] = NULL;
  104. group->gr_mem = members;
  105. return 0;
  106. }