X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=bootcensus;a=blobdiff_plain;f=src%2Flibc%2Fdirent.c;fp=src%2Flibc%2Fdirent.c;h=1af59fe36697aaedf5bfe9beef8d88e5a19f25b9;hp=0000000000000000000000000000000000000000;hb=78e3e75fc7b5838d0261c876d00e1b8c3e0bcfe0;hpb=7b6f6de2124e28ae7da5599a7cdaf2c171c4f15e diff --git a/src/libc/dirent.c b/src/libc/dirent.c new file mode 100644 index 0000000..1af59fe --- /dev/null +++ b/src/libc/dirent.c @@ -0,0 +1,95 @@ +/* +pcboot - bootable PC demo/game kernel +Copyright (C) 2018-2019 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY, without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +#include +#include +#include +#include "dirent.h" +#include "fs.h" + +struct DIR { + struct fs_node *fsn; + struct dirent dent; +}; + +DIR *opendir(const char *path) +{ + DIR *dir; + struct fs_node *node; + + if(!path) { + errno = EINVAL; + return 0; + } + + if(!(node = fs_open(path, 0))) { + errno = ENOENT; + return 0; + } + if(node->type != FSNODE_DIR) { + errno = ENOTDIR; + fs_close(node); + return 0; + } + + if(!(dir = malloc(sizeof *dir))) { + errno = ENOMEM; + fs_close(node); + return 0; + } + dir->fsn = node; + + return dir; +} + +int closedir(DIR *dir) +{ + if(!dir) { + errno = EINVAL; + return -1; + } + fs_close(dir->fsn); + free(dir); + return 0; +} + +void rewinddir(DIR *dir) +{ + if(!dir) { + errno = EINVAL; + return; + } + fs_rewinddir(dir->fsn); +} + +struct dirent *readdir(DIR *dir) +{ + struct fs_dirent *fsdent; + + if(!dir) { + errno = EINVAL; + return 0; + } + if(!(fsdent = fs_readdir(dir->fsn))) { + return 0; + } + + strcpy(dir->dent.d_name, fsdent->name); + dir->dent.d_type = fsdent->type == FSNODE_DIR ? DT_DIR : DT_REG; + dir->dent.d_fsize = fsdent->fsize; + return &dir->dent; +}