shithub: mc

ref: 6df84aac3adcbcc21c64312472347634d8237dc3
dir: /sys-linux.myr/

View raw version
use "types.use"

pkg std =
	type scno = int
	type fdopt = int
	type mprot = int
	type mopt = int
	type statbuf = struct
		 dev     : uint
		 ino     : uint
		 mode    : uint16
		 nlink   : uint16
		 uid     : uint16
		 gid     : uint16
		 rdev    : uint
		 size    : uint
		 blksize : uint
		 blocks  : uint
		 atime   : uint
		 atimens : uint
		 mtime   : uint
		 mtimens : uint
		 ctime   : uint
		 ctimens : uint
		 _unused1: uint
		 _unused2: uint
	;;

	/* open options */
	const Ordonly  	: fdopt = 0x0
	const Owronly  	: fdopt = 0x1
	const Ordwr    	: fdopt = 0x2
	const Oappend  	: fdopt = 0x80
	const Ocreat   	: fdopt = 0x40
	const Onofollow	: fdopt = 0x20000
	const Ondelay  	: fdopt = 0x800
	const Otrunc   	: fdopt = 0x200

	/* mmap protection */
	const Mprotnone	: mprot = 0x0
	const Mprotrd	: mprot = 0x1
	const Mprotwr	: mprot = 0x2
	const Mprotexec	: mprot = 0x4
	const Mprotrw	: mprot = 0x3 /* convenience */
	
	/* mmap options */
	const Mshared	: mopt = 0x1
	const Mpriv	: mopt = 0x2
	const Mfixed	: mopt = 0x10
	const Mfile	: mopt = 0x0
	const Manon	: mopt = 0x20
	const M32bit	: mopt = 0x40

	/* return value for a failed mapping */
	const Mapbad	: byte* = -1 castto(byte*)

	/* syscalls */
	const Sysexit	: scno = 1
	const Sysfork	: scno = 3
	const Sysread	: scno = 3
	const Syswrite	: scno = 4
	const Sysopen	: scno = 5
	const Sysclose	: scno = 6
	const Syswait4	: scno = 7
	const Syscreat	: scno = 8
	const Syslseek	: scno = 19
	const Sysfstat	: scno = 108
	const Syskill	: scno = 37
	const Sysgetpid	: scno = 20
	const Sysmmap	: scno = 90
	const Sysmmap2	: scno = 192
	const Sysmunmap	: scno = 91

	extern const syscall : (sc:scno, args:... -> int)

	const exit	: (status:int -> int)
	const getpid	: ( -> int)
	const kill	: (pid:int, sig:int -> int)
	const open	: (path:byte[,], opts:fdopt, mode:int -> int)
	const close	: (fd:int -> int)
	const creat	: (path:byte[,], mode:int -> int)
	const read	: (fd:int, buf:byte[,] -> int)
	const write	: (fd:int, buf:byte[,] -> int)
	const lseek	: (fd:int, off:uint, whence:int -> int)
	const fstat	: (fd:int, sb:statbuf* -> int)
	const munmap	: (addr:byte*, len:size -> int)
	const mmap	: (addr:byte*, len:size, prot:mprot, flags:mopt, fd:int, off:off -> byte*)
;;

const exit	= {status;		-> syscall(Sysexit, 1);}
const getpid	= {;			-> syscall(Sysgetpid, 1);}
const kill	= {pid, sig;		-> syscall(Syskill, pid, sig);}
const open	= {path, opts, mode;	-> syscall(Sysopen, path castto(char*), opts);}
const close	= {fd;			-> syscall(Sysclose, fd);}
const creat	= {path, mode;		-> syscall(Syscreat, path castto(char*), mode);}
const read	= {fd, buf;		-> syscall(Sysread, fd, buf castto(char*), buf.len);}
const write	= {fd, buf;		-> syscall(Syswrite, fd, buf castto(char*), buf.len);}
const lseek	= {fd, off, whence;	-> syscall(Syslseek, fd, off, whence);}
const fstat	= {fd, sb;		-> syscall(Sysfstat, fd, sb);}
const munmap	= {addr, len;		-> syscall(Sysmunmap, addr, len);}
const mmap	= {addr, len, prot, flags, fd, off;	-> syscall(Sysmmap2, addr, len, prot, flags, fd, off) castto(byte*);}