opendir.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "dirstream.h"
  15. libc_hidden_proto(opendir)
  16. libc_hidden_proto(open)
  17. libc_hidden_proto(fcntl)
  18. /* libc_hidden_proto(close) */
  19. libc_hidden_proto(stat)
  20. libc_hidden_proto(fstat)
  21. /* opendir just makes an open() call - it return NULL if it fails
  22. * (open sets errno), otherwise it returns a DIR * pointer.
  23. */
  24. DIR *opendir(const char *name)
  25. {
  26. int fd;
  27. struct stat statbuf;
  28. DIR *ptr;
  29. #ifndef O_DIRECTORY
  30. /* O_DIRECTORY is linux specific and has been around since like 2.1.x */
  31. if (stat(name, &statbuf))
  32. return NULL;
  33. if (!S_ISDIR(statbuf.st_mode)) {
  34. __set_errno(ENOTDIR);
  35. return NULL;
  36. }
  37. # define O_DIRECTORY 0
  38. #endif
  39. if ((fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY)) < 0)
  40. return NULL;
  41. /* Note: we should check to make sure that between the stat() and open()
  42. * call, 'name' didnt change on us, but that's only if O_DIRECTORY isnt
  43. * defined and since Linux has supported it for like ever, i'm not going
  44. * to worry about it right now (if ever). */
  45. if (fstat(fd, &statbuf) < 0)
  46. goto close_and_ret;
  47. /* According to POSIX, directory streams should be closed when
  48. * exec. From "Anna Pluzhnikov" <besp@midway.uchicago.edu>.
  49. */
  50. if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
  51. int saved_errno;
  52. close_and_ret:
  53. saved_errno = errno;
  54. close(fd);
  55. __set_errno(saved_errno);
  56. return NULL;
  57. }
  58. if (!(ptr = malloc(sizeof(*ptr))))
  59. goto nomem_close_and_ret;
  60. ptr->dd_fd = fd;
  61. ptr->dd_nextloc = ptr->dd_size = ptr->dd_nextoff = 0;
  62. ptr->dd_max = statbuf.st_blksize;
  63. if (ptr->dd_max < 512)
  64. ptr->dd_max = 512;
  65. if (!(ptr->dd_buf = calloc(1, ptr->dd_max))) {
  66. free(ptr);
  67. nomem_close_and_ret:
  68. close(fd);
  69. __set_errno(ENOMEM);
  70. return NULL;
  71. }
  72. __pthread_mutex_init(&(ptr->dd_lock), NULL);
  73. return ptr;
  74. }
  75. libc_hidden_def(opendir)