__getpwent_r.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * __getpwent_r.c - This file is part of the libc-8086/pwd 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. * March 7, 2001 -- Reworked to be reentrant by Erik Andersen
  22. * Oct 9, 2003 -- Reworked again by Erik Andersen to be fully reentrant
  23. */
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include "config.h"
  30. /* This isn't as flash as my previous version -- it doesn't dynamically
  31. scale down the gecos on too-long lines, but it also makes fewer syscalls,
  32. so it's probably nicer. Write me if you want the old version. Maybe I
  33. should include it as a build-time option... ?
  34. -Nat <ndf@linux.mit.edu> */
  35. int __getpwent_r(struct passwd * passwd, char * line_buff, size_t buflen, int pwd_fd)
  36. {
  37. char *endptr, *field_begin;
  38. char *gid_ptr=NULL;
  39. char *uid_ptr=NULL;
  40. int i, line_len;
  41. if (buflen<PWD_BUFFER_SIZE) {
  42. return ERANGE;
  43. }
  44. if (pwd_fd == -1)
  45. setpwent();
  46. /* We use the restart label to handle malformatted lines */
  47. restart:
  48. /* Read the passwd line into the buffer for processing */
  49. if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0) {
  50. return EIO;
  51. }
  52. field_begin = strchr(line_buff, '\n');
  53. if (field_begin != NULL)
  54. lseek(pwd_fd, (long) (1 + field_begin - (line_buff + line_len)), SEEK_CUR);
  55. else {
  56. /* The line is too long - skip it. :-\ */
  57. do {
  58. if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0) {
  59. return EIO;
  60. }
  61. } while (!(field_begin = strchr(line_buff, '\n')));
  62. lseek(pwd_fd, (long) (field_begin - line_buff) - line_len + 1, SEEK_CUR);
  63. goto restart;
  64. }
  65. if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' || *line_buff == '\t')
  66. goto restart;
  67. *field_begin = '\0';
  68. /* We've read the line; now parse it. */
  69. field_begin = line_buff;
  70. for (i = 0; i < 7; i++) {
  71. switch (i) {
  72. case 0:
  73. passwd->pw_name = field_begin;
  74. break;
  75. case 1:
  76. passwd->pw_passwd = field_begin;
  77. break;
  78. case 2:
  79. uid_ptr = field_begin;
  80. break;
  81. case 3:
  82. gid_ptr = field_begin;
  83. break;
  84. case 4:
  85. passwd->pw_gecos = field_begin;
  86. break;
  87. case 5:
  88. passwd->pw_dir = field_begin;
  89. break;
  90. case 6:
  91. passwd->pw_shell = field_begin;
  92. break;
  93. }
  94. if (i < 6) {
  95. field_begin = strchr(field_begin, ':');
  96. if (field_begin == NULL)
  97. goto restart;
  98. *field_begin++ = '\0';
  99. }
  100. }
  101. passwd->pw_gid = (gid_t) strtoul(gid_ptr, &endptr, 10);
  102. if (*endptr != '\0')
  103. goto restart;
  104. passwd->pw_uid = (uid_t) strtoul(uid_ptr, &endptr, 10);
  105. if (*endptr != '\0')
  106. goto restart;
  107. return 0;
  108. }