readdir.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* $Id$ */
  2. #include <sys/types.h>
  3. #include <sys/dir.h>
  4. #ifndef __BSD4_2
  5. /*
  6. * read an old stlye directory entry and present it as a new one
  7. */
  8. #define ODIRSIZ 14
  9. struct olddirect {
  10. ino_t od_ino;
  11. char od_name[ODIRSIZ];
  12. };
  13. #else
  14. #define olddirect direct
  15. #endif
  16. /*
  17. * get next entry in a directory.
  18. */
  19. struct direct *readdir(dirp)
  20. register DIR *dirp;
  21. {
  22. register struct olddirect *dp;
  23. static struct direct dir;
  24. for (;;) {
  25. if (dirp->dd_loc == -1) {
  26. dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
  27. dirp->dd_bsize);
  28. if (dirp->dd_size <= 0) {
  29. dirp->dd_size = 0;
  30. return NULL;
  31. }
  32. dirp->dd_loc = 0;
  33. #ifdef __BSD4_2
  34. if (! ((struct direct *) dirp->dd_buf)->d_ino) {
  35. dirp->dd_loc = ((struct direct *)dirp->dd_buf)->d_reclen;
  36. }
  37. #endif
  38. }
  39. if (dirp->dd_loc >= dirp->dd_size) {
  40. dirp->dd_loc = -1;
  41. continue;
  42. }
  43. dp = (struct olddirect *) (dirp->dd_buf + dirp->dd_loc);
  44. #ifndef __BSD4_2
  45. dirp->dd_loc += sizeof (struct olddirect);
  46. if (dp->od_ino == 0)
  47. continue;
  48. dir.d_ino = dp->od_ino;
  49. strncpy(dir.d_name, dp->od_name, ODIRSIZ);
  50. dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
  51. dir.d_reclen = DIRSIZ(&dir);
  52. dir.d_namlen = strlen(dir.d_name);
  53. #else
  54. dirp->dd_loc += dp->d_reclen;
  55. dir.d_ino = dp->d_ino;
  56. strcpy(dir.d_name, dp->d_name);
  57. dir.d_reclen = dp->d_reclen;
  58. dir.d_namlen = dp->d_namlen;
  59. #endif
  60. return &dir;
  61. }
  62. }