port of redo-c to BetterC
jfondren
julian.fondren at gmail.com
Wed Jun 9 23:27:06 UTC 2021
On Wednesday, 9 June 2021 at 21:45:33 UTC, aquaratixc wrote:
> 1. In the original implementation, there are places where the
> openat function is used (dir_fd, target, O_RDONLY |
> O_DIRECTORY); and I couldn't find a replacement. Are there any
> options where you can get it?
> 2. Similarly, the dprintf function was not found, which, as I
> understand it, is a non-standard extension. Is there a way to
> add this?
In general you can just use the C functions directly, with
reference to system
manpages, prior interfaces in D repositories (dmd+phobos+druntime
are good
repos to have around), https://dlang.org/spec/interfaceToC.html ,
and (until
ImportC lands) your system's headers for the right magic numbers.
```d
extern(C) nothrow @nogc int dprintf(int fd, scope const char*
format, ...);
extern(C) nothrow @nogc int openat(int dirfd, scope const char
*pathname, int flags);
extern(C) nothrow @nogc int openat(int dirfd, scope const char
*pathname, int flags, int mode);
extern(C) nothrow @nogc int close(int fd);
import core.sys.posix.fcntl : AT_FDCWD, O_RDONLY;
enum O_DIRECTORY = 0x10000;
import std.stdio : File, write;
void main() {
int dir, file;
if (-1 != (dir = openat(AT_FDCWD, "test", O_DIRECTORY |
O_RDONLY))) {
scope(exit) close(dir);
if (-1 != (file = openat(dir, "x", O_RDONLY))) {
File f;
f.fdopen(file, "r");
write("test/x contained: ", f.readln);
}
}
}
```
More information about the Digitalmars-d
mailing list