opendir.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifndef O_CLOEXEC
  73. # define O_CLOEXEC 0
  74. #endif
  75. fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_CLOEXEC);
  76. if (fd < 0)
  77. return NULL;
  78. /* Note: we should check to make sure that between the stat() and open()
  79. * call, 'name' didnt change on us, but that's only if O_DIRECTORY isnt
  80. * defined and since Linux has supported it for like ever, i'm not going
  81. * to worry about it right now (if ever). */
  82. if (fstat(fd, &statbuf) < 0) {
  83. /* this close() never fails
  84. *int saved_errno;
  85. *saved_errno = errno; */
  86. close(fd);
  87. /*__set_errno(saved_errno);*/
  88. return NULL;
  89. }
  90. /* According to POSIX, directory streams should be closed when
  91. * exec. From "Anna Pluzhnikov" <besp@midway.uchicago.edu>.
  92. */
  93. if (O_CLOEXEC == 0)
  94. fcntl(fd, F_SETFD, FD_CLOEXEC);
  95. ptr = fd_to_DIR(fd, statbuf.st_blksize);
  96. if (!ptr) {
  97. close(fd);
  98. __set_errno(ENOMEM);
  99. }
  100. return ptr;
  101. }
  102. libc_hidden_def(opendir)