Merge branch 'master' of git://git.ucc.asn.au/tpg/acess2
[tpg/acess2.git] / Usermode / Libraries / libposix.so_src / dirent.c
1 /*
2  * Acess2 C Library
3  * - By John Hodge (thePowersGang)
4  *
5  * dirent.c
6  * - Directory Functions
7  */
8 #include <dirent.h>
9 #include <stddef.h>     // NULL
10 #include <acess/sys.h>
11 #include <errno.h>
12 #include <stdlib.h>
13
14 struct DIR_s
15 {
16          int    fd;
17          int    pos;
18         struct dirent   tmpent;
19 };
20
21 // === CODE ===
22 DIR *fdopendir(int fd)
23 {
24         t_sysFInfo      info;
25         if( _SysFInfo(fd, &info, 0) != 0 )
26                 return NULL;
27
28         DIR     *ret = malloc(sizeof(DIR));
29         ret->fd = fd;
30         ret->pos = 0;
31         return ret;
32 }
33
34 DIR *opendir(const char *name)
35 {
36         int fd = _SysOpen(name, OPENFLAG_READ);
37         if( fd == -1 )
38                 return NULL;
39         
40         DIR *ret = fdopendir(fd);
41         if( !ret ) {
42                 _SysClose(fd);
43         }
44         return ret;
45 }
46
47 int closedir(DIR *dp)
48 {
49         if( !dp ) {
50                 errno = EINVAL;
51                 return -1;
52         }
53         _SysClose(dp->fd);
54         free(dp);
55         return 0;
56 }
57
58 struct dirent *readdir(DIR *dp)
59 {
60         if( !dp ) {
61                 errno = EINVAL;
62                 return NULL;
63         }
64
65         errno = EOK;
66         int rv = _SysReadDir(dp->fd, dp->tmpent.d_name);
67         if( rv != 1 ) {
68                 // TODO: Fix kernel-land API
69                 return NULL;
70         }
71         dp->tmpent.d_ino = 0;
72
73         return &dp->tmpent;
74 }
75
76 extern int      readdir_r(DIR *, struct dirent *, struct dirent **);
77 extern void     rewinddir(DIR *);
78 extern void     seekdir(DIR *, long int);
79 extern long int telldir(DIR *);

UCC git Repository :: git.ucc.asn.au