__getpwent_r.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * __getpwent_r.c - This file is part of the libc-8086/pwd package for ELKS,
  3. * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>.
  4. * Copyright (C) 2001 Erik Andersen <andersee@debian.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * March 7, 2001 -- Reworked to be reentrant by Erik Andersen
  21. */
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include "config.h"
  28. /* This isn't as flash as my previous version -- it doesn't dynamically
  29. scale down the gecos on too-long lines, but it also makes fewer syscalls,
  30. so it's probably nicer. Write me if you want the old version. Maybe I
  31. should include it as a build-time option... ?
  32. -Nat <ndf@linux.mit.edu> */
  33. int __getpwent_r(struct passwd * passwd, char * line_buff, size_t buflen, int pwd_fd)
  34. {
  35. char *field_begin;
  36. char *endptr;
  37. char *gid_ptr=NULL;
  38. char *uid_ptr=NULL;
  39. int line_len;
  40. int i;
  41. if (buflen<PWD_BUFFER_SIZE)
  42. return ERANGE;
  43. /* We use the restart label to handle malformatted lines */
  44. restart:
  45. /* Read the passwd line into the static buffer using a minimum of
  46. syscalls. */
  47. if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0)
  48. return EIO;
  49. field_begin = strchr(line_buff, '\n');
  50. if (field_begin != NULL)
  51. lseek(pwd_fd, (long) (1 + field_begin - (line_buff + line_len)),
  52. SEEK_CUR);
  53. else { /* The line is too long - skip it. :-\ */
  54. do {
  55. if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0)
  56. return EIO;
  57. } while (!(field_begin = strchr(line_buff, '\n')));
  58. lseek(pwd_fd, (long) (field_begin - line_buff) - line_len + 1,
  59. SEEK_CUR);
  60. goto restart;
  61. }
  62. if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' ||
  63. *line_buff == '\t')
  64. goto restart;
  65. *field_begin = '\0';
  66. /* We've read the line; now parse it. */
  67. field_begin = line_buff;
  68. for (i = 0; i < 7; i++) {
  69. switch (i) {
  70. case 0:
  71. passwd->pw_name = field_begin;
  72. break;
  73. case 1:
  74. passwd->pw_passwd = field_begin;
  75. break;
  76. case 2:
  77. uid_ptr = field_begin;
  78. break;
  79. case 3:
  80. gid_ptr = field_begin;
  81. break;
  82. case 4:
  83. passwd->pw_gecos = field_begin;
  84. break;
  85. case 5:
  86. passwd->pw_dir = field_begin;
  87. break;
  88. case 6:
  89. passwd->pw_shell = field_begin;
  90. break;
  91. }
  92. if (i < 6) {
  93. field_begin = strchr(field_begin, ':');
  94. if (field_begin == NULL)
  95. goto restart;
  96. *field_begin++ = '\0';
  97. }
  98. }
  99. passwd->pw_gid = (gid_t) strtoul(gid_ptr, &endptr, 10);
  100. if (*endptr != '\0')
  101. goto restart;
  102. passwd->pw_uid = (uid_t) strtoul(uid_ptr, &endptr, 10);
  103. if (*endptr != '\0')
  104. goto restart;
  105. return 0;
  106. }