[m-users.] Another bit for the posix library

Julien Fischer jfischer at opturion.com
Sat Oct 26 00:45:14 AEDT 2019


Hi Volker,

On Thu, 24 Oct 2019, Volker Wysk wrote:

> Here is another bit for the posix library: The realpath(3) library
> call.

Thanks for that!  I've modified it a bit (diff attached to this mail),
in order to avoid the issues with PATH_MAX.  (This requires that the
your system support POSIX.1-2008, but that should be ok.)

The update should make its way into the next ROTD.

Cheers,
Julien.
-------------- next part --------------
diff --git a/extras/posix/posix.m b/extras/posix/posix.m
index 809430627..a4a23c957 100644
--- a/extras/posix/posix.m
+++ b/extras/posix/posix.m
@@ -50,6 +50,7 @@
 :- include_module posix.pipe.
 :- include_module posix.read.
 :- include_module posix.readdir.
+:- include_module posix.realpath.
 :- include_module posix.rmdir.
 :- include_module posix.select.
 :- include_module posix.socket.
diff --git a/extras/posix/posix.realpath.m b/extras/posix/posix.realpath.m
index e69de29bb..47c9cbf99 100644
--- a/extras/posix/posix.realpath.m
+++ b/extras/posix/posix.realpath.m
@@ -0,0 +1,65 @@
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%------------------------------------------------------------------------------%
+% Copyright (C) 2019 The Mercury team.
+% This file is distributed under the terms specified in COPYING.LIB.
+%------------------------------------------------------------------------------%
+%
+% Module: posix.readdir.m.
+% Main author: Volker Wysk
+%
+%------------------------------------------------------------------------------%
+
+:- module posix.realpath.
+:- interface.
+
+:- import_module io.
+:- import_module string.
+
+    % Return the canonicalized absolute pathname. See realpath(3).
+    %
+:- pred realpath(string::in, posix.result(string)::out, io::di, io::uo)
+    is det.
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module bool.
+
+realpath(Path, Result, !IO) :-
+    realpath0(Path, IsOk, RealPath, !IO),
+    (
+        IsOk = yes,
+        Result = ok(RealPath)
+    ;
+        IsOk = no,
+        errno(Errno, !IO),
+        Result = error(Errno)
+    ).
+
+:- pragma foreign_decl("C", "
+    #include <stdlib.h>
+").
+
+:- pred realpath0(string::in, bool::out, string::out, io::di, io::uo) is det.
+:- pragma foreign_proc("C",
+    realpath0(Path::in, IsOk::out, Result::out, _IO0::di, _IO::uo),
+    [promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
+"
+    char *actualpath;
+    actualpath = realpath(Path, NULL);
+    if (actualpath != NULL) {
+        IsOk = MR_YES;
+        MR_make_aligned_string_copy(Result, actualpath);
+        free(actualpath);
+    } else {
+        IsOk = MR_NO;
+        Result = MR_make_string_const("""");
+    }
+").
+
+%----------------------------------------------------------------------------%
+:- end_module posix.realpath.
+%----------------------------------------------------------------------------%


More information about the users mailing list