[m-rev.] for review: add dir.current_directory

Peter Ross pro at missioncriticalit.com
Wed Sep 19 17:49:03 AEST 2007


On 9/19/07, Peter Wang <novalazy at gmail.com> wrote:
> Estimated hours taken: 1.5
> Branches: main
>
> NEWS:
> library/dir.m:
>         Add a predicate to return the current working directory, currently
>         implemented for C and Erlang backends.
>
> library/io.m:
>         Add supporting functions to create io.res(string) values from foreign
>         code.
>
> tests/hard_coded/dir_test.exp:
> tests/hard_coded/dir_test.exp2:
> tests/hard_coded/dir_test.m:
>         Test the new predicate.
>
>
> Index: NEWS
> ===================================================================
> RCS file: /home/mercury/mercury1/repository/mercury/NEWS,v
> retrieving revision 1.477
> diff -u -r1.477 NEWS
> --- NEWS        7 Sep 2007 15:08:16 -0000       1.477
> +++ NEWS        19 Sep 2007 06:57:30 -0000
> @@ -195,6 +195,8 @@
>  * We have added a predicate io.remove_file_recursively/4
>    which can remove non-empty directories.
>
> +* We have added the predicate `dir.current_directory'.
> +
>  Changes to the Mercury compiler:
>
>  * Support for the reserve tag grades has been removed.
> Index: library/dir.m
> ===================================================================
> RCS file: /home/mercury/mercury1/repository/mercury/library/dir.m,v
> retrieving revision 1.42
> diff -u -r1.42 dir.m
> --- library/dir.m       17 Aug 2007 02:08:38 -0000      1.42
> +++ library/dir.m       19 Sep 2007 06:57:30
> @@ -820,6 +827,50 @@
>
>  %-----------------------------------------------------------------------------%
>
> +:- pragma foreign_proc("C",
> +    dir.current_directory(Res::out, IO0::di, IO::uo),
> +    [may_call_mercury, promise_pure, tabled_for_io, terminates],
> +"
> +    size_t      size = 256;
> +    char        *buf;
> +    MR_String   str;
> +
> +    while (1) {
> +        buf = MR_GC_NEW_ARRAY(char, size);
> +        if (getcwd(buf, size)) {
> +            MR_make_aligned_string(str, buf);
> +            Res = ML_make_io_res_1_ok_string(str);
> +            break;
> +        }
> +        if (errno != ERANGE) {
> +            ML_make_io_res_1_error_string(errno,
> +                MR_make_string_const(""dir.current_directory failed: ""),
> +                &Res);

You should use strerror to get the actual error message.
Note don't forget to test if the result is the empty string, in which
case a more informatiive error message would be useful.

> +            break;
> +        }
> +        /* Buffer too small.  Resize and try again. */
> +        size *= 1.5;
> +    }
> +
> +    IO = IO0;
> +").
> +

Otherwise this diff looks fine.
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list