getdents.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright (C) 1993, 1995-2002 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #include <alloca.h>
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <dirent.h>
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/param.h>
  24. #include <sys/types.h>
  25. #include <sysdep.h>
  26. #include <sys/syscall.h>
  27. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  28. struct kernel_dirent
  29. {
  30. long d_ino;
  31. __kernel_off_t d_off;
  32. unsigned short d_reclen;
  33. char d_name[256];
  34. };
  35. #define __NR___syscall_getdents __NR_getdents
  36. static inline _syscall3(int, __syscall_getdents, int, fd, unsigned char *, kdirp, size_t, count);
  37. ssize_t __getdents (int fd, char *buf, size_t nbytes)
  38. {
  39. struct dirent *dp;
  40. off_t last_offset = -1;
  41. ssize_t retval;
  42. size_t red_nbytes;
  43. struct kernel_dirent *skdp, *kdp;
  44. const size_t size_diff = (offsetof (struct dirent, d_name)
  45. - offsetof (struct kernel_dirent, d_name));
  46. red_nbytes = MIN (nbytes - ((nbytes /
  47. (offsetof (struct dirent, d_name) + 14)) * size_diff),
  48. nbytes - size_diff);
  49. dp = (struct dirent *) buf;
  50. skdp = kdp = alloca (red_nbytes);
  51. retval = __syscall_getdents(fd, (char *)kdp, red_nbytes);
  52. if (retval == -1)
  53. return -1;
  54. while ((char *) kdp < (char *) skdp + retval) {
  55. const size_t alignment = __alignof__ (struct dirent);
  56. /* Since kdp->d_reclen is already aligned for the kernel structure
  57. this may compute a value that is bigger than necessary. */
  58. size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
  59. & ~(alignment - 1));
  60. if ((char *) dp + new_reclen > buf + nbytes) {
  61. /* Our heuristic failed. We read too many entries. Reset
  62. the stream. */
  63. assert (last_offset != -1);
  64. lseek(fd, last_offset, SEEK_SET);
  65. if ((char *) dp == buf) {
  66. /* The buffer the user passed in is too small to hold even
  67. one entry. */
  68. __set_errno (EINVAL);
  69. return -1;
  70. }
  71. break;
  72. }
  73. last_offset = kdp->d_off;
  74. dp->d_ino = kdp->d_ino;
  75. dp->d_off = kdp->d_off;
  76. dp->d_reclen = new_reclen;
  77. dp->d_type = DT_UNKNOWN;
  78. memcpy (dp->d_name, kdp->d_name,
  79. kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
  80. dp = (struct dirent *) ((char *) dp + new_reclen);
  81. kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
  82. }
  83. return (char *) dp - buf;
  84. }