[m-rev.] posix patch (stat, pipe, kill)

Michael Day mikeday at bigpond.net.au
Wed Jul 25 15:15:28 AEST 2001


> I think that should be changed.
> (With `:- type mode == mode_t. % XXX This is deprecated; please use mode_t.'
> for backwards compatibility.)

In that case... here is a diff :)

Estimated hours taken: 2
Branches: main

Adds support for the stat, pipe and kill system calls. Renames mode and
pid to mode_t and pid_t.

Michael


Index: posix.fork.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/posix/posix.fork.m,v
retrieving revision 1.1
diff -u -r1.1 posix.fork.m
--- posix.fork.m	2001/07/16 03:08:18	1.1
+++ posix.fork.m	2001/07/25 05:11:27
@@ -14,7 +14,7 @@

 :- type whoami
 	--->	child
-	;	parent(pid)
+	;	parent(pid_t)
 	.

 :- pred fork(posix__result(whoami), io__state, io__state).

Index: posix.kill.m
===================================================================
RCS file: posix.kill.m
diff -N posix.kill.m
--- /dev/null	Wed Nov 15 09:24:47 2000
+++ posix.kill.m	Wed Jul 25 15:11:27 2001
@@ -0,0 +1,50 @@
+%------------------------------------------------------------------------------%
+% 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__kill.m
+% main author: Michael Day <miked at lendtech.com.au>
+%
+%------------------------------------------------------------------------------%
+:- module posix__kill.
+
+:- interface.
+
+:- import_module int.
+
+:- pred kill(pid_t, int, posix__result, io__state, io__state).
+:- mode kill(in, in, out, di, uo) is det.
+
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- pragma c_header_code("
+	#include <sys/types.h>
+	#include <signal.h>
+").
+
+%------------------------------------------------------------------------------%
+
+kill(Pid, Sig, Result) -->
+	kill0(Pid, Sig, Res),
+	( if { Res \= 0 } then
+		errno(Err),
+		{ Result = error(Err) }
+	else
+		{ Result = ok }
+	).
+
+:- pred kill0(pid_t, int, int, io__state, io__state).
+:- mode kill0(in, in, out, di, uo) is det.
+
+:- pragma c_code(kill0(Pid::in, Sig::in, Res::out, IO0::di, IO::uo),
+		[will_not_call_mercury], "{
+	Res = kill(Pid, Sig);
+	IO = IO0;
+}").
+
+%------------------------------------------------------------------------------%
+

Index: posix.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/posix/posix.m,v
retrieving revision 1.3
diff -u -r1.3 posix.m
--- posix.m	2001/07/16 03:08:18	1.3
+++ posix.m	2001/07/25 05:11:27
@@ -37,15 +37,18 @@
 :- include_module posix__dup.
 :- include_module posix__exec.
 :- include_module posix__fork.
+:- include_module posix__kill.
 :- include_module posix__lseek.
 :- include_module posix__mkdir.
 :- include_module posix__open.
 :- include_module posix__opendir.
+:- include_module posix__pipe.
 :- include_module posix__read.
 :- include_module posix__readdir.
 :- include_module posix__rmdir.
 :- include_module posix__select.
 :- include_module posix__socket.
+:- include_module posix__stat.
 :- include_module posix__wait.
 :- include_module posix__write.

@@ -55,8 +58,23 @@
 	% Directory streams.
 :- type dir ---> dir(c_pointer).

+	% Devices.
+:- type dev_t ---> dev(int).
+
+	% File modes.
+:- type mode_t ---> mode(int).
+
+	% Inodes.
+:- type ino_t ---> ino(int).
+
 	% Process identifiers.
-:- type pid ---> pid(int).
+:- type pid_t ---> pid(int).
+
+	% User identifiers.
+:- type uid_t ---> uid(int).
+
+	% Group identifiers.
+:- type gid_t ---> gid(int).

 :- type error
 	--->	e2BIG			/* Arg list too long */
@@ -115,7 +133,7 @@
 	;	error(posix__error)
 	.

-:- type (mode)	--->	mode(int).
+:- type (mode) == mode_t. % XXX This is deprecated; please use mode_t.

 :- type timeval
 	--->	timeval(int, int). % time(Sec, uSec)

Index: posix.mkdir.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/posix/posix.mkdir.m,v
retrieving revision 1.1
diff -u -r1.1 posix.mkdir.m
--- posix.mkdir.m	2001/07/16 03:08:18	1.1
+++ posix.mkdir.m	2001/07/25 05:11:27
@@ -14,7 +14,7 @@

 :- import_module string.

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

 %------------------------------------------------------------------------------%
@@ -39,7 +39,7 @@
 	    { Result = error(Err) }
 	).

-:- pred mkdir0(string, mode, int, io__state, io__state).
+:- pred mkdir0(string, mode_t, 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),

Index: posix.open.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/posix/posix.open.m,v
retrieving revision 1.2
diff -u -r1.2 posix.open.m
--- posix.open.m	1999/10/18 00:50:26	1.2
+++ posix.open.m	2001/07/25 05:11:27
@@ -33,11 +33,11 @@
 :- pred open(string, list(oflag), posix__result(fd), io__state, io__state).
 :- mode open(in, in, out, di, uo) is det.

-:- pred open(string, list(oflag), (mode), posix__result(fd),
+:- pred open(string, list(oflag), mode_t, posix__result(fd),
 		io__state, io__state).
 :- mode open(in, in, in, out, di, uo) is det.

-:- pred creat(string, (mode), posix__result(fd), io__state, io__state).
+:- pred creat(string, mode_t, posix__result(fd), io__state, io__state).
 :- mode creat(in, in, out, di, uo) is det.

 :- pred close(fd, posix__result, io__state, io__state).
@@ -85,7 +85,7 @@
 		{ Result = ok(fd(FdNo)) }
 	).

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

 :- pragma c_code(open0(PathName::in, Flags::in, Mode::in, FileDes::out,
@@ -105,7 +105,7 @@
 		{ Result = ok(fd(FdNo)) }
 	).

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

 :- pragma c_code(creat0(PathName::in, Mode::in, FileDes::out,

Index: posix.pipe.m
===================================================================
RCS file: posix.pipe.m
diff -N posix.pipe.m
--- /dev/null	Wed Nov 15 09:24:47 2000
+++ posix.pipe.m	Wed Jul 25 15:11:27 2001
@@ -0,0 +1,53 @@
+%------------------------------------------------------------------------------%
+% 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__pipe.m
+% main author: Michael Day <miked at lendtech.com.au>
+%
+%------------------------------------------------------------------------------%
+:- module posix__pipe.
+
+:- interface.
+
+:- pred pipe(posix__result({fd, fd}), io__state, io__state).
+:- mode pipe(out, di, uo) is det.
+
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module int.
+
+:- pragma c_header_code("
+	#include <sys/types.h>
+	#include <unistd.h>
+").
+
+%------------------------------------------------------------------------------%
+
+pipe(Result) -->
+	pipe0(Reading, Writing, Res),
+	( if { Res \= 0 } then
+		errno(Err),
+		{ Result = error(Err) }
+	else
+		{ Result = ok({Reading, Writing}) }
+	).
+
+:- pred pipe0(fd, fd, int, io__state, io__state).
+:- mode pipe0(out, out, out, di, uo) is det.
+
+:- pragma c_code(pipe0(R::out, W::out, Res::out, IO0::di, IO::uo),
+		[will_not_call_mercury], "{
+	int filedes[2];
+	Res = pipe(filedes);
+	R = filedes[0];
+	W = filedes[1];
+	IO = IO0;
+}").
+
+%------------------------------------------------------------------------------%
+

Index: posix.stat.m
===================================================================
RCS file: posix.stat.m
diff -N posix.stat.m
--- /dev/null	Wed Nov 15 09:24:47 2000
+++ posix.stat.m	Wed Jul 25 15:11:27 2001
@@ -0,0 +1,178 @@
+%------------------------------------------------------------------------------%
+% 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: Michael Day <miked at lendtech.com.au>
+%
+%------------------------------------------------------------------------------%
+:- module posix__stat.
+
+:- interface.
+
+:- import_module int, string, time.
+
+:- type file_type
+    --->    file
+    ;	    directory
+    ;	    symbolic_link
+    ;	    character_device
+    ;	    block_device
+    ;	    fifo
+    ;	    unknown
+    .
+
+:- type stat.
+
+:- func dev(stat) = dev_t.
+:- func ino(stat) = ino_t.
+:- func mode(stat) = mode_t.
+:- func file_type(stat) = file_type.
+:- func nlink(stat) = int.
+:- func uid(stat) = uid_t.
+:- func gid(stat) = gid_t.
+:- func rdev(stat) = dev_t.
+:- func size(stat) = int.
+:- func blksize(stat) = int.
+:- func blocks(stat) = int.
+:- func atime(stat) = time_t.
+:- func mtime(stat) = time_t.
+:- func ctime(stat) = time_t.
+
+:- pred stat(string, posix__result(stat), io__state, io__state).
+:- mode stat(in, out, di, uo) is det.
+
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- pragma c_header_code("
+	#include <sys/types.h>
+	#include <sys/stat.h>
+	#include <unistd.h>
+").
+
+%------------------------------------------------------------------------------%
+
+:- type stat ---> stat(c_pointer).
+
+stat(Path, Result) -->
+	stat0(Path, Res, Stat),
+	( if { Res = 0 } then
+		{ Result = ok(Stat) }
+	else
+		errno(Err),
+		{ Result = error(Err) }
+	).
+
+:- pred stat0(string, int, stat, io__state, io__state).
+:- mode stat0(in, out, out, di, uo) is det.
+
+:- pragma c_code(stat0(Path::in, Res::out, Stat::out, IO0::di, IO::uo),
+		[will_not_call_mercury, thread_safe], "{
+	Stat = (MR_Word) MR_NEW(struct stat);
+	Res = stat(Path, (struct stat *)Stat);
+	IO = IO0;
+}").
+
+file_type(Stat) =
+	( 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 ) :- Mode = Stat ^ (mode).
+
+:- pred is_slnk(mode_t).
+:- 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(mode_t).
+:- 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(mode_t).
+:- 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(mode_t).
+:- 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(mode_t).
+:- 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(mode_t).
+:- mode is_fifo(in) is semidet.
+
+:- pragma c_code(is_fifo(Mode::in), [will_not_call_mercury, thread_safe], "
+	SUCCESS_INDICATOR = S_ISFIFO(Mode); ").
+
+:- pragma c_code(dev(S::in) = (Dev::out),
+	[will_not_call_mercury, thread_safe],
+	"Dev = ((struct stat *)S)->st_dev; ").
+
+:- pragma c_code(ino(S::in) = (Ino::out),
+	[will_not_call_mercury, thread_safe],
+	"Ino = ((struct stat *)S)->st_ino; ").
+
+:- pragma c_code(mode(S::in) = (Mode::out),
+	[will_not_call_mercury, thread_safe],
+	"Mode = ((struct stat *)S)->st_mode; ").
+
+:- pragma c_code(nlink(S::in) = (Nlink::out),
+	[will_not_call_mercury, thread_safe],
+	"Nlink = ((struct stat *)S)->st_nlink; ").
+
+:- pragma c_code(uid(S::in) = (Uid::out),
+	[will_not_call_mercury, thread_safe],
+	"Uid = ((struct stat *)S)->st_uid; ").
+
+:- pragma c_code(gid(S::in) = (Gid::out),
+	[will_not_call_mercury, thread_safe],
+	"Gid = ((struct stat *)S)->st_gid; ").
+
+:- pragma c_code(rdev(S::in) = (Rdev::out),
+	[will_not_call_mercury, thread_safe],
+	"Rdev = ((struct stat *)S)->st_rdev; ").
+
+:- pragma c_code(size(S::in) = (Size::out),
+	[will_not_call_mercury, thread_safe],
+	"Size = ((struct stat *)S)->st_size; ").
+
+:- pragma c_code(blksize(S::in) = (Blksize::out),
+	[will_not_call_mercury, thread_safe],
+	"Blksize = ((struct stat *)S)->st_blksize; ").
+
+:- pragma c_code(blocks(S::in) = (Blocks::out),
+	[will_not_call_mercury, thread_safe],
+	"Blocks = ((struct stat *)S)->st_blocks; ").
+
+:- pragma c_code(atime(S::in) = (Atime::out),
+	[will_not_call_mercury, thread_safe],
+	"Atime = ((struct stat *)S)->st_atime; ").
+
+:- pragma c_code(mtime(S::in) = (Mtime::out),
+	[will_not_call_mercury, thread_safe],
+	"Mtime = ((struct stat*)S)->st_mtime; ").
+
+:- pragma c_code(ctime(S::in) = (Ctime::out),
+	[will_not_call_mercury, thread_safe],
+	"Ctime = ((struct stat *)S)->st_ctime; ").
+
+%------------------------------------------------------------------------------%
+

Index: posix.wait.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/posix/posix.wait.m,v
retrieving revision 1.1
diff -u -r1.1 posix.wait.m
--- posix.wait.m	2001/07/16 03:08:19	1.1
+++ posix.wait.m	2001/07/25 05:11:27
@@ -17,7 +17,7 @@
 	;	signal(int)
 	.

-:- pred wait(posix__result({pid, status}), io__state, io__state).
+:- pred wait(posix__result({pid_t, status}), io__state, io__state).
 :- mode wait(out, di, uo) is det.

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

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list