__getpwent.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * __getpwent.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. *
  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 <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <fcntl.h>
  24. #include <pwd.h>
  25. #define PWD_BUFFER_SIZE 256
  26. /* This isn't as flash as my previous version -- it doesn't dynamically
  27. scale down the gecos on too-long lines, but it also makes fewer syscalls,
  28. so it's probably nicer. Write me if you want the old version. Maybe I
  29. should include it as a build-time option... ?
  30. -Nat <ndf@linux.mit.edu> */
  31. struct passwd *
  32. __getpwent(int pwd_fd)
  33. {
  34. static char line_buff[PWD_BUFFER_SIZE];
  35. static struct passwd passwd;
  36. char * field_begin;
  37. char * endptr;
  38. char * gid_ptr;
  39. char * uid_ptr;
  40. int line_len;
  41. int i;
  42. /* We use the restart label to handle malformatted lines */
  43. restart:
  44. /* Read the passwd line into the static buffer using a minimal of
  45. syscalls. */
  46. if ((line_len=read(pwd_fd, line_buff, PWD_BUFFER_SIZE))<=0)
  47. return NULL;
  48. field_begin=strchr(line_buff, '\n');
  49. if (field_begin!=NULL)
  50. lseek(pwd_fd, (long) (1+field_begin-(line_buff+line_len)), SEEK_CUR);
  51. else /* The line is too long - skip it. :-\ */
  52. {
  53. do { if ((line_len=read(pwd_fd, line_buff, PWD_BUFFER_SIZE))<=0)
  54. return NULL;
  55. } while (!(field_begin=strchr(line_buff, '\n')));
  56. lseek(pwd_fd, (long) (field_begin-line_buff)-line_len+1, SEEK_CUR);
  57. goto restart;
  58. }
  59. if (*line_buff=='#' || *line_buff==' ' || *line_buff=='\n' ||
  60. *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<7;i++)
  66. {
  67. switch(i)
  68. {
  69. case 0: passwd.pw_name=field_begin; break;
  70. case 1: passwd.pw_passwd=field_begin; break;
  71. case 2: uid_ptr=field_begin; break;
  72. case 3: gid_ptr=field_begin; break;
  73. case 4: passwd.pw_gecos=field_begin; break;
  74. case 5: passwd.pw_dir=field_begin; break;
  75. case 6: passwd.pw_shell=field_begin; break;
  76. }
  77. if (i<6)
  78. {
  79. field_begin=strchr(field_begin, ':');
  80. if (field_begin==NULL) goto restart;
  81. *field_begin++='\0';
  82. }
  83. }
  84. passwd.pw_gid=(gid_t) strtoul(gid_ptr, &endptr, 10);
  85. if (*endptr!='\0') goto restart;
  86. passwd.pw_uid=(uid_t) strtoul(uid_ptr, &endptr, 10);
  87. if (*endptr!='\0') goto restart;
  88. return &passwd;
  89. }