opendir.c 2.4 KB

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