[m-dev.] posix modules again

Michael Day mikeday at corplink.com.au
Sun Jul 1 23:22:24 AEST 2001


Hi,

Although I theoretically still have cvs access, cvs complains that my home
directory doesn't exist so I'm not entirely sure how to make it work.

cvs [server aborted]: can't chdir(/home/stude/m/mcda): No such file or
directory

If anyone can give me a home directory or check in the code that would be
very nice :)

The posix.stat module needs some review; it compiles and works for me but
I can't vouch for its elegance or posix correctishness.

Thanks,

Michael

-------------- next part --------------
%------------------------------------------------------------------------------%
% Copyright (C) 2001 The University of Melbourne.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file COPYING.LIB in the Mercury distribution.
%------------------------------------------------------------------------------%
%
% module: posix__mkdir.m
% main author: miked at lendtech.com.au
%
%------------------------------------------------------------------------------%
:- module posix__mkdir.

:- interface.

:- import_module string.

:- pred mkdir(string, mode, posix__result, io__state, io__state).
:- mode mkdir(in, in, out, di, uo) is det.

%------------------------------------------------------------------------------%

:- implementation.

:- import_module int.

:- pragma c_header_code("
	#include <sys/types.h>
	#include <sys/stat.h>
").

%------------------------------------------------------------------------------%

mkdir(Path, Mode, Result) -->
	mkdir0(Path, Mode, Res),
	( if { Res = 0 } then
	    { Result = ok }
	else
	    errno(Err),
	    { Result = error(Err) }
	).				    

:- pred mkdir0(string, mode, int, io__state, io__state).
:- mode mkdir0(in, in, out, di, uo) is det.

:- pragma c_code(mkdir0(Path::in, Mode::in, Res::out, IO0::di, IO::uo),
	    [will_not_call_mercury, thread_safe], "
	Res = mkdir(Path, Mode);
	IO = IO0;
").
		
%------------------------------------------------------------------------------%

-------------- next part --------------
%------------------------------------------------------------------------------%
% Copyright (C) 2001 The University of Melbourne.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file COPYING.LIB in the Mercury distribution.
%------------------------------------------------------------------------------%
%
% module: posix__rmdir.m
% main author: miked at lendtech.com.au
%
%------------------------------------------------------------------------------%
:- module posix__rmdir.

:- interface.

:- import_module string.

:- pred rmdir(string, posix__result, io__state, io__state).
:- mode rmdir(in, out, di, uo) is det.

%------------------------------------------------------------------------------%

:- implementation.

:- import_module int.

:- pragma c_header_code("
	#include <unistd.h>
").

%------------------------------------------------------------------------------%

rmdir(Path, Result) -->
	rmdir0(Path, Res),
	( if { Res = 0 } then
	    { Result = ok }
	else
	    errno(Err),
	    { Result = error(Err) }
	).				    

:- pred rmdir0(string, int, io__state, io__state).
:- mode rmdir0(in, out, di, uo) is det.

:- pragma c_code(rmdir0(Path::in, Res::out, IO0::di, IO::uo),
	    [will_not_call_mercury, thread_safe], "
	Res = rmdir(Path);
	IO = IO0;
").
		
%------------------------------------------------------------------------------%

-------------- next part --------------
%------------------------------------------------------------------------------%
% Copyright (C) 2001 The University of Melbourne.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file COPYING.LIB in the Mercury distribution.
%------------------------------------------------------------------------------%
%
% module: posix__stat.m
% main author: miked at lendtech.com.au
%
%------------------------------------------------------------------------------%
:- module posix__stat.

:- interface.

:- import_module string, time.

:- type file_type
    --->    file
    ;	    directory
    ;	    symbolic_link
    ;	    character_device
    ;	    block_device
    ;	    fifo
    ;	    unknown
    .

:- type stat
    --->    stat(
		dev :: int,
		ino :: int,
		(mode) :: (mode),
		(type) :: file_type,
		nlink :: int,
		uid :: int,
		gid :: int,
		rdev :: int,
		size :: int,
		blksize :: int,
		blocks :: int,
		atime :: int,
		mtime :: int,
		ctime :: int
	    ).

:- pred stat(string, posix__result(stat), io__state, io__state).
:- mode stat(in, out, di, uo) is det.

%------------------------------------------------------------------------------%

:- implementation.

:- import_module int.

:- pragma c_header_code("
	#include <sys/types.h>
	#include <sys/stat.h>
	#include <unistd.h>
").

%------------------------------------------------------------------------------%

stat(Path, Result) -->
	stat0(Path, Res, Dev, Ino, Mode, Nlink, Uid, Gid, Rdev,
		Size, Blksize, Blocks, Atime, Mtime, Ctime),
	( if { Res = 0 } then
	    { Type = get_type(Mode) },
	    { Result = ok(stat(Dev, Ino, mode(Mode), Type, Nlink, Uid, Gid,
		Rdev, Size, Blksize, Blocks, Atime, Mtime, Ctime)) }
	else
	    errno(Err),
	    { Result = error(Err) }
	).				    

:- pred stat0(string, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, io__state, io__state).
:- mode stat0(in, out, out, out, out, out, out, out, out, out, out, out,
out, out, out, di, uo) is det.

:- pragma c_code(stat0(Path::in, Res::out, Dev::out, Ino::out, Mode::out,
Nlink::out, Uid::out, Gid::out, Rdev::out, Size::out, Blksize::out,
Blocks::out, Atime::out, Mtime::out, Ctime::out, IO0::di, IO::uo),
	    [will_not_call_mercury, thread_safe], "{
	struct stat buf;
	Res = stat(Path, &buf);
	if (Res == 0) {
	    Dev = buf.st_dev;
	    Ino = buf.st_ino;
	    Mode = buf.st_mode;
	    Nlink = buf.st_nlink;
	    Uid = buf.st_uid;
	    Gid = buf.st_gid;
	    Rdev = buf.st_rdev;
	    Size = buf.st_size;
	    Blksize = buf.st_blksize;
	    Blocks = buf.st_blocks;
	    Atime = buf.st_atime;
	    Mtime = buf.st_mtime;
	    Ctime = buf.st_ctime;
	} else {
	    Dev = Ino = Mode = Nlink = Uid = Gid = Rdev = Size =
		Blksize = Blocks = Atime = Mtime = Ctime = 0;
	}
	IO = IO0;
}").

:- func get_type(int) = file_type.

get_type(Mode) =
    ( if is_slnk(Mode) then symbolic_link
    else if is_reg(Mode) then file
    else if is_dir(Mode) then directory
    else if is_chr(Mode) then character_device
    else if is_blk(Mode) then block_device
    else if is_fifo(Mode) then fifo
    else unknown ).

:- pred is_slnk(int).
:- mode is_slnk(in) is semidet.

:- pragma c_code(is_slnk(Mode::in), [will_not_call_mercury, thread_safe], "
    SUCCESS_INDICATOR = S_ISLNK(Mode);
").

:- pred is_reg(int).
:- mode is_reg(in) is semidet.

:- pragma c_code(is_reg(Mode::in), [will_not_call_mercury, thread_safe], "
    SUCCESS_INDICATOR = S_ISREG(Mode);
").

:- pred is_dir(int).
:- mode is_dir(in) is semidet.

:- pragma c_code(is_dir(Mode::in), [will_not_call_mercury, thread_safe], "
    SUCCESS_INDICATOR = S_ISDIR(Mode);
").

:- pred is_chr(int).
:- mode is_chr(in) is semidet.

:- pragma c_code(is_chr(Mode::in), [will_not_call_mercury, thread_safe], "
    SUCCESS_INDICATOR = S_ISCHR(Mode);
").

:- pred is_blk(int).
:- mode is_blk(in) is semidet.

:- pragma c_code(is_blk(Mode::in), [will_not_call_mercury, thread_safe], "
    SUCCESS_INDICATOR = S_ISBLK(Mode);
").

:- pred is_fifo(int).
:- mode is_fifo(in) is semidet.

:- pragma c_code(is_fifo(Mode::in), [will_not_call_mercury, thread_safe], "
    SUCCESS_INDICATOR = S_ISFIFO(Mode);
").

%------------------------------------------------------------------------------%



More information about the developers mailing list