[m-rev.] Another posix patch: waitpid, stat types

Michael Day mikeday at bigpond.net.au
Thu Sep 6 12:28:06 AEST 2001


Estimated hours taken: 1
Branches: main

Added waitpid predicate to posix.wait and added nlink_t, blkcnt_t and
blksize_t types for posix.stat.

Michael


Index: posix.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/posix/posix.m,v
retrieving revision 1.5
diff -u -r1.5 posix.m
--- posix.m	2001/08/24 03:02:37	1.5
+++ posix.m	2001/09/06 02:25:49
@@ -31,10 +31,11 @@

 :- interface.

-:- import_module io.
+:- import_module io, int, integer.

 :- include_module posix__closedir.
 :- include_module posix__dup.
@@ -67,6 +68,18 @@

 	% Inodes.
 :- type ino_t ---> ino(int).
+
+	% Link counts.
+:- type nlink_t ---> nlink(int).
+
+	% File offsets.
+:- type off_t ---> off(integer).
+
+	% Block counts.
+:- type blkcnt_t ---> blkcnt(integer).
+
+	% Block size.
+:- type blksize_t ---> blksize(int).

 	% Process identifiers.
 :- type pid_t ---> pid(int).

Index: posix.stat.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/posix/posix.stat.m,v
retrieving revision 1.1
diff -u -r1.1 posix.stat.m
--- posix.stat.m	2001/07/25 08:37:23	1.1
+++ posix.stat.m	2001/09/06 02:25:49
@@ -12,7 +12,7 @@

 :- interface.

-:- import_module int, string, time.
+:- import_module string, time.

 :- type file_type
     --->    file
@@ -30,13 +30,13 @@
 :- func ino(stat) = ino_t.
 :- func mode(stat) = mode_t.
 :- func file_type(stat) = file_type.
-:- func nlink(stat) = int.
+:- func nlink(stat) = nlink_t.
 :- 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 size(stat) = off_t.
+:- func blksize(stat) = blksize_t.
+:- func blocks(stat) = blkcnt_t.
 :- func atime(stat) = time_t.
 :- func mtime(stat) = time_t.
 :- func ctime(stat) = time_t.
@@ -150,15 +150,23 @@
 	[will_not_call_mercury, thread_safe],
 	"Rdev = ((struct stat *)S)->st_rdev; ").

-:- pragma c_code(size(S::in) = (Size::out),
+size(S) = off(integer(size0(S))).
+
+:- func size0(stat) = int.
+
+:- pragma c_code(size0(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; ").
+
+blocks(S) = blkcnt(integer(blocks0(S))).
+
+:- func blocks0(stat) = int.

-:- pragma c_code(blocks(S::in) = (Blocks::out),
+:- pragma c_code(blocks0(S::in) = (Blocks::out),
 	[will_not_call_mercury, thread_safe],
 	"Blocks = ((struct stat *)S)->st_blocks; ").

Index: posix.wait.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/posix/posix.wait.m,v
retrieving revision 1.2
diff -u -r1.2 posix.wait.m
--- posix.wait.m	2001/07/25 08:37:24	1.2
+++ posix.wait.m	2001/09/06 02:25:49
@@ -12,6 +12,13 @@

 :- interface.

+:- type wait_for
+	--->	any_child
+	;	child(pid_t)
+	;	child_in_group(pid_t)
+	;	child_in_same_group
+	.
+
 :- type status
 	--->	exit(int)
 	;	signal(int)
@@ -20,6 +27,9 @@
 :- pred wait(posix__result({pid_t, status}), io__state, io__state).
 :- mode wait(out, di, uo) is det.

+:- pred waitpid(wait_for, posix__result({pid_t, status}), io__state, io__state).
+:- mode waitpid(in, out, di, uo) is det.
+
 %------------------------------------------------------------------------------%

 :- implementation.
@@ -56,6 +66,43 @@
 	Status = status;
 	IO = IO0;
 }").
+
+%------------------------------------------------------------------------------%
+
+waitpid(WaitFor, Result) -->
+	(
+		{ WaitFor = any_child, Pid0 = pid(-1) }
+	;
+		{ WaitFor = child(Pid0) }
+	;
+		{ WaitFor = child_in_group(pid(Group)), Pid0 = pid(-Group) }
+	;
+		{ WaitFor = child_in_same_group, Pid0 = pid(0) }
+	),
+	waitpid0(Pid0, Pid, Status),
+	( if { Pid < 0 } then
+		errno(Err),
+		{ Result = error(Err) }
+	else
+		{ if if_exited(Status) then
+			Result = ok({pid(Pid), exit(exit_status(Status))})
+		else
+			Result = ok({pid(Pid), signal(term_sig(Status))})
+		}
+	).
+
+:- pred waitpid0(pid_t, int, int, io__state, io__state).
+:- mode waitpid0(in, out, out, di, uo) is det.
+
+:- pragma c_code(waitpid0(Pid0::in, Pid::out, Status::out, IO0::di, IO::uo),
+		[will_not_call_mercury, thread_safe], "{
+	int status;
+	Pid = waitpid(Pid0, &status, 0);
+	Status = status;
+	IO = IO0;
+}").
+
+%------------------------------------------------------------------------------%

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

--------------------------------------------------------------------------
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