opendir.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #define STAT stat64
  18. #define FSTAT fstat64
  19. static DIR *fd_to_DIR(int fd, __blksize_t size)
  20. {
  21. DIR *ptr;
  22. ptr = malloc(sizeof(*ptr));
  23. if (!ptr)
  24. return NULL;
  25. ptr->dd_fd = fd;
  26. ptr->dd_nextloc = ptr->dd_size = ptr->dd_nextoff = 0;
  27. ptr->dd_max = size;
  28. if (ptr->dd_max < 512)
  29. ptr->dd_max = 512;
  30. ptr->dd_buf = calloc(1, ptr->dd_max);
  31. if (!ptr->dd_buf) {
  32. free(ptr);
  33. return NULL;
  34. }
  35. __UCLIBC_MUTEX_INIT_VAR(ptr->dd_lock);
  36. return ptr;
  37. }
  38. DIR *fdopendir(int fd)
  39. {
  40. int flags;
  41. struct STAT st;
  42. if (FSTAT(fd, &st))
  43. return NULL;
  44. if (!S_ISDIR(st.st_mode)) {
  45. __set_errno(ENOTDIR);
  46. return NULL;
  47. }
  48. flags = fcntl(fd, F_GETFL);
  49. if (flags == -1)
  50. return NULL;
  51. if ((flags & O_ACCMODE) == O_WRONLY) {
  52. __set_errno(EINVAL);
  53. return NULL;
  54. }
  55. return fd_to_DIR(fd, st.st_blksize);
  56. }
  57. /* opendir just makes an open() call - it return NULL if it fails
  58. * (open sets errno), otherwise it returns a DIR * pointer.
  59. */
  60. DIR *opendir(const char *name)
  61. {
  62. int fd;
  63. struct STAT statbuf;
  64. DIR *ptr;
  65. #ifndef O_DIRECTORY
  66. /* O_DIRECTORY is linux specific and has been around since like 2.1.x */
  67. if (STAT(name, &statbuf))
  68. return NULL;
  69. if (!S_ISDIR(statbuf.st_mode)) {
  70. __set_errno(ENOTDIR);
  71. return NULL;
  72. }
  73. # define O_DIRECTORY 0
  74. #endif
  75. fd = open_not_cancel_2(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_not_cancel_no_status(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. #ifndef __ASSUME_O_CLOEXEC
  94. fcntl_not_cancel(fd, F_SETFD, FD_CLOEXEC);
  95. #endif
  96. ptr = fd_to_DIR(fd, statbuf.st_blksize);
  97. if (!ptr) {
  98. close_not_cancel_no_status(fd);
  99. /* __set_errno(ENOMEM); */
  100. }
  101. return ptr;
  102. }
  103. libc_hidden_def(opendir)