opendir.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* $Id$ */
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/dir.h>
  5. /*
  6. * open a directory.
  7. */
  8. DIR *opendir(name)
  9. char *name;
  10. {
  11. register DIR *dirp;
  12. register int fd;
  13. struct stat stbuf;
  14. long siz;
  15. extern char *malloc();
  16. #ifdef __BSD4_2
  17. siz = stbuf.st_blksize;
  18. #else
  19. siz = DIRBLKSIZ;
  20. #endif
  21. if ((fd = open(name, 0)) == -1)
  22. return NULL;
  23. fstat(fd, &stbuf);
  24. if (((stbuf.st_mode & S_IFDIR) == 0) ||
  25. ((dirp = (DIR *)malloc(sizeof (DIR))) == NULL)) {
  26. close (fd);
  27. return NULL;
  28. }
  29. if (stbuf.st_size > siz) siz = stbuf.st_size;
  30. if ((unsigned) siz == siz &&
  31. (dirp->dd_buf = malloc((unsigned) siz))) {
  32. dirp->dd_bsize = siz;
  33. #ifdef __BSD4_2
  34. dirp->dd_size = getdirentries(fd,
  35. (char *) dirp->dd_buf,
  36. (int) siz,
  37. &siz);
  38. if (dirp->dd_size < 0 )
  39. #endif
  40. dirp->dd_size = read(fd, dirp->dd_buf, dirp->dd_bsize);
  41. close(fd);
  42. dirp->dd_fd = -2;
  43. dirp->dd_loc = 0;
  44. return dirp;
  45. }
  46. #ifndef __BSD4_2
  47. else if (dirp->dd_buf = malloc(8*DIRBLKSIZ)) {
  48. dirp->dd_bsize = 8 * DIRBLKSIZ;
  49. }
  50. else if (dirp->dd_buf = malloc(DIRBLKSIZ)) {
  51. dirp->dd_bsize = DIRBLKSIZ;
  52. }
  53. #endif
  54. else {
  55. close(fd);
  56. free((char *) dirp);
  57. return NULL;
  58. }
  59. dirp->dd_fd = fd;
  60. dirp->dd_loc = -1;
  61. return dirp;
  62. }