opendir.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <errno.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <sys/dir.h>
  7. #include <sys/stat.h>
  8. #include "dirstream.h"
  9. libc_hidden_proto(opendir)
  10. libc_hidden_proto(open)
  11. libc_hidden_proto(fcntl)
  12. libc_hidden_proto(close)
  13. libc_hidden_proto(stat)
  14. /* opendir just makes an open() call - it return NULL if it fails
  15. * (open sets errno), otherwise it returns a DIR * pointer.
  16. */
  17. DIR *opendir(const char *name)
  18. {
  19. int fd;
  20. struct stat statbuf;
  21. char *buf;
  22. DIR *ptr;
  23. if (stat(name, &statbuf))
  24. return NULL;
  25. if (!S_ISDIR(statbuf.st_mode)) {
  26. __set_errno(ENOTDIR);
  27. return NULL;
  28. }
  29. if ((fd = open(name, O_RDONLY)) < 0)
  30. return NULL;
  31. /* According to POSIX, directory streams should be closed when
  32. * exec. From "Anna Pluzhnikov" <besp@midway.uchicago.edu>.
  33. */
  34. if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
  35. return NULL;
  36. if (!(ptr = malloc(sizeof(*ptr)))) {
  37. close(fd);
  38. __set_errno(ENOMEM);
  39. return NULL;
  40. }
  41. ptr->dd_fd = fd;
  42. ptr->dd_nextloc = ptr->dd_size = ptr->dd_nextoff = 0;
  43. ptr->dd_max = statbuf.st_blksize;
  44. if (ptr->dd_max < 512)
  45. ptr->dd_max = 512;
  46. if (!(buf = calloc(1, ptr->dd_max))) {
  47. close(fd);
  48. free(ptr);
  49. __set_errno(ENOMEM);
  50. return NULL;
  51. }
  52. ptr->dd_buf = buf;
  53. __pthread_mutex_init(&(ptr->dd_lock), NULL);
  54. return ptr;
  55. }
  56. libc_hidden_def(opendir)