Merge branch 'master' of ssh://Aleaxander@repo.or.cz/srv/git/fstk
[fstk.git] / dir.c
blob6feba96b9dcdbfee368554d1835078ca3e5e4130
1 /*
2 * All the functions related with dir, like open/read/closedir, are
3 * implemented here.
5 * Copyright (C) 2009 Liu Aleaxander -- All rights reserved. This file
6 * can be redistributed under the terms of the GNU Public License.
7 */
9 #include <stdio.h>
10 #include "fstk.h"
11 #include "file.h"
12 #include "dirent.h"
13 #include "fstk_malloc.h"
16 DIR * this_dir = NULL;
18 DIR * fstk_opendir(struct fstk *fs, char *name)
20 DIR *dir = fstk_malloc(sizeof(*dir));
22 if (!dir) {
23 malloc_error("DIR structure");
24 return NULL;
26 dir->dd_dir = fstk_open(fs, name);
27 if (!dir->dd_dir) {
28 fstk_free(dir);
29 return NULL;
32 return dir;
36 struct dirent *fstk_readdir(DIR *dir)
38 struct fstk *fs = dir->dd_dir->fs;
40 return fs->ops->readdir(dir->dd_dir);
44 void fstk_closedir(DIR *dir)
46 if (!dir)
47 return;
48 fstk_close(dir->dd_dir);
49 fstk_free(dir);
53 void set_current_dentry(struct fstk *fs)
55 if (!(this_dir = fstk_opendir(fs, "/")))
56 printf("Warning: setting this_dir error\n");
59 void free_current_dentry(void)
61 if (this_dir)
62 fstk_closedir(this_dir);