[m-users.] Compiler warning in my readdir.m

Julien Fischer jfischer at opturion.com
Mon Oct 14 15:49:00 AEDT 2019


Hi all,

On Sun, 13 Oct 2019, Julian Fondren wrote:

> Probably you arrived at this design because per the pred
> declaration you're obliged to return a string even when there's no
> useful string to return. What you can do though, is just not set
> Entry at all (untested):
>
> :- type low_readdir_result
>     --->    ok
>    ;        eod
>    ;        error.
> :- pragma foreign_export_enum("C",
>     low_readdir_result/0, [prefix("LRR_"), uppercase]).
>
> readdir(Dir, Result, !IO) :-
>     readdir0(Dir, Entry, Res, !IO),
>     (
>         Res = ok,
>         Result = ok(yes(Entry))
>    ;
>         Res = eod,
>        %  !!
>        %  what is Entry in this case?
>        %  ... let's just promise not to look at it.
>        %  !!
>         Result = ok(no)
>    ;
>         Res = error,
>        %  !!
>         errno(Err, !IO),
>         Result = error(Err),
>    ) .
>
> :- pred readdir0(dir::in, string::out, low_readdir_result::out, io::di, 
> io::uo) is det.
> :- pragma foreign_proc("C",
>     readdir0(Dir::in, Entry::out, Result::out, _IO0::di, _IO::uo),
>     [will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
> "
>     struct dirent *ent = NULL;
>     errno = 0;
>     ent = readdir(Dir);
>     if (ent != NULL) {
>         MR_make_aligned_string_copy(Entry, ent->d_name);
>         Result = LRR_OK;
>     } else if (errno == 0) {
>        Result = LRR_EOD;
>     else {
>        Result = LRR_ERROR;
>    }
> ").
>
> I've wondered if predicates like this should just be impure, so
> that callers at least have to explicitly opt in, but it's only
> come up in small libraries, in unexported code.

An alterantive approach would be to use pragma foreign_exported'd
functions to construct the result values and call those from the
C code of readdir0.

Julien.


More information about the users mailing list