os_kernel_lab/labcodes_answer/lab8_result/user/libs/dir.c
2014-08-20 15:42:20 +08:00

47 lines
795 B
C

#include <defs.h>
#include <string.h>
#include <syscall.h>
#include <stat.h>
#include <dirent.h>
#include <file.h>
#include <dir.h>
#include <error.h>
#include <unistd.h>
DIR dir, *dirp=&dir;
DIR *
opendir(const char *path) {
if ((dirp->fd = open(path, O_RDONLY)) < 0) {
goto failed;
}
struct stat __stat, *stat = &__stat;
if (fstat(dirp->fd, stat) != 0 || !S_ISDIR(stat->st_mode)) {
goto failed;
}
dirp->dirent.offset = 0;
return dirp;
failed:
return NULL;
}
struct dirent *
readdir(DIR *dirp) {
if (sys_getdirentry(dirp->fd, &(dirp->dirent)) == 0) {
return &(dirp->dirent);
}
return NULL;
}
void
closedir(DIR *dirp) {
close(dirp->fd);
}
int
getcwd(char *buffer, size_t len) {
return sys_getcwd(buffer, len);
}