-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcdir.nit
More file actions
31 lines (27 loc) · 750 Bytes
/
cdir.nit
File metadata and controls
31 lines (27 loc) · 750 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
`{
#include <sys/types.h>
#include <dirent.h>
`}
extern class CDir `{ DIR* `}
# Open a directory
new(path: NativeString) `{ return opendir(path); `}
# Close a directory
fun closedir `{ closedir(self); `}
# Read the next directory entry
fun readdir: NativeString `{
struct dirent *de;
de = readdir(self);
if (!de) return NULL;
return de->d_name;
`}
end
# Simple client
# Disclaimer: Usually it is often a better idea to add another API level to avoid
# the direct manipulation of foreign values.
var dir = new CDir(".".to_cstring)
loop
var ent = dir.readdir
if ent.address_is_null then break
print ent.to_s
end
dir.closedir