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