opendir.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <errno.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <sys/dir.h>
  13. #include <sys/stat.h>
  14. #include <dirent.h>
  15. #include "dirstream.h"
  16. static DIR *fd_to_DIR(int fd, __blksize_t size)
  17. {
  18. DIR *ptr;
  19. ptr = malloc(sizeof(*ptr));
  20. if (!ptr)
  21. return NULL;
  22. ptr->dd_fd = fd;
  23. ptr->dd_nextloc = ptr->dd_size = ptr->dd_nextoff = 0;
  24. ptr->dd_max = size;
  25. if (ptr->dd_max < 512)
  26. ptr->dd_max = 512;
  27. ptr->dd_buf = calloc(1, ptr->dd_max);
  28. if (!ptr->dd_buf) {
  29. free(ptr);
  30. return NULL;
  31. }
  32. __pthread_mutex_init(&ptr->dd_lock, NULL);
  33. return ptr;
  34. }
  35. DIR *fdopendir(int fd)
  36. {
  37. int flags;
  38. struct stat st;
  39. if (fstat(fd, &st))
  40. return NULL;
  41. if (!S_ISDIR(st.st_mode)) {
  42. __set_errno(ENOTDIR);
  43. return NULL;
  44. }
  45. flags = fcntl(fd, F_GETFL);
  46. if (flags == -1)
  47. return NULL;
  48. if ((flags & O_ACCMODE) == O_WRONLY) {
  49. __set_errno(EINVAL);
  50. return NULL;
  51. }
  52. return fd_to_DIR(fd, st.st_blksize);
  53. }
  54. /* opendir just makes an open() call - it return NULL if it fails
  55. * (open sets errno), otherwise it returns a DIR * pointer.
  56. */
  57. DIR *opendir(const char *name)
  58. {
  59. int fd;
  60. struct stat statbuf;
  61. DIR *ptr;
  62. #ifndef O_DIRECTORY
  63. /* O_DIRECTORY is linux specific and has been around since like 2.1.x */
  64. if (stat(name, &statbuf))
  65. return NULL;
  66. if (!S_ISDIR(statbuf.st_mode)) {
  67. __set_errno(ENOTDIR);
  68. return NULL;
  69. }
  70. # define O_DIRECTORY 0
  71. #endif
  72. if ((fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY)) < 0)
  73. return NULL;
  74. /* Note: we should check to make sure that between the stat() and open()
  75. * call, 'name' didnt change on us, but that's only if O_DIRECTORY isnt
  76. * defined and since Linux has supported it for like ever, i'm not going
  77. * to worry about it right now (if ever). */
  78. if (fstat(fd, &statbuf) < 0)
  79. goto close_and_ret;
  80. /* According to POSIX, directory streams should be closed when
  81. * exec. From "Anna Pluzhnikov" <besp@midway.uchicago.edu>.
  82. */
  83. if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
  84. int saved_errno;
  85. close_and_ret:
  86. saved_errno = errno;
  87. close(fd);
  88. __set_errno(saved_errno);
  89. return NULL;
  90. }
  91. ptr = fd_to_DIR(fd, statbuf.st_blksize);
  92. if (!ptr) {
  93. close(fd);
  94. __set_errno(ENOMEM);
  95. }
  96. return ptr;
  97. }
  98. libc_hidden_def(opendir)