《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
795 B

10 years ago
  1. #include <defs.h>
  2. #include <string.h>
  3. #include <syscall.h>
  4. #include <stat.h>
  5. #include <dirent.h>
  6. #include <file.h>
  7. #include <dir.h>
  8. #include <error.h>
  9. #include <unistd.h>
  10. DIR dir, *dirp=&dir;
  11. DIR *
  12. opendir(const char *path) {
  13. if ((dirp->fd = open(path, O_RDONLY)) < 0) {
  14. goto failed;
  15. }
  16. struct stat __stat, *stat = &__stat;
  17. if (fstat(dirp->fd, stat) != 0 || !S_ISDIR(stat->st_mode)) {
  18. goto failed;
  19. }
  20. dirp->dirent.offset = 0;
  21. return dirp;
  22. failed:
  23. return NULL;
  24. }
  25. struct dirent *
  26. readdir(DIR *dirp) {
  27. if (sys_getdirentry(dirp->fd, &(dirp->dirent)) == 0) {
  28. return &(dirp->dirent);
  29. }
  30. return NULL;
  31. }
  32. void
  33. closedir(DIR *dirp) {
  34. close(dirp->fd);
  35. }
  36. int
  37. getcwd(char *buffer, size_t len) {
  38. return sys_getcwd(buffer, len);
  39. }