From 859771b3160dcfa3d3a26aa84fa2edfa09fc09ca Mon Sep 17 00:00:00 2001 From: Michael Santos Date: Sat, 26 Apr 2014 17:23:30 +0000 Subject: [PATCH] Add lseek(2) --- README.md | 7 +++++++ c_src/alcove.h | 5 +++++ c_src/alcove_call.proto | 1 + c_src/alcove_file.c | 45 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/README.md b/README.md index 018c370..59d98ab 100644 --- a/README.md +++ b/README.md @@ -582,6 +582,13 @@ probably confuse the process. kill(2) : terminate a process + lseek(Drv, FD, Offset, Whence) -> ok | {error, posix()} + lseek(Drv, Pids, FD, Offset, Whence) -> ok | {error, posix()} + + Types Offset = Whence = integer() + + lseek(2) : set file offset for read/write + mkdir(Drv, Path, Mode) -> ok | {error, posix()} mkdir(Drv, Pids, Path, Mode) -> ok | {error, posix()} diff --git a/c_src/alcove.h b/c_src/alcove.h index 1e178ea..e348336 100644 --- a/c_src/alcove.h +++ b/c_src/alcove.h @@ -69,6 +69,11 @@ ? ERL_INT_UVALUE(x) \ : ERL_LL_UVALUE(x)) +#define ALCOVE_LL_VALUE(x) \ + ((ERL_IS_INTEGER(x)) \ + ? ERL_INT_VALUE(x) \ + : ERL_LL_VALUE(x)) + #define ALCOVE_SETOPT(x,k,v) \ (x)->opt = (v) ? (x)->opt | (k) : (x)->opt & ~(k) diff --git a/c_src/alcove_call.proto b/c_src/alcove_call.proto index 80b353a..1798d80 100644 --- a/c_src/alcove_call.proto +++ b/c_src/alcove_call.proto @@ -21,6 +21,7 @@ getpid/0 getrlimit/1 getuid/0 kill/2 +lseek/3 mkdir/2 mount/5 mount_define/1 diff --git a/c_src/alcove_file.c b/c_src/alcove_file.c index fa9e104..d83f8f7 100644 --- a/c_src/alcove_file.c +++ b/c_src/alcove_file.c @@ -115,6 +115,51 @@ BADARG: return erl_mk_atom("badarg"); } +/* + * lseek(2) + * + */ + ETERM * +alcove_lseek(alcove_state_t *ap, ETERM *arg) +{ + ETERM *hd = NULL; + int fd = 0; + off_t offset = 0; + int whence = 0; + + /* fd */ + arg = alcove_list_head(&hd, arg); + if (!hd || !ERL_IS_INTEGER(hd)) + goto BADARG; + + fd = ERL_INT_VALUE(hd); + + /* offset */ + arg = alcove_list_head(&hd, arg); + if (!hd || !ALCOVE_IS_LONGLONG(hd)) + goto BADARG; + + offset = ALCOVE_LL_VALUE(hd); + + /* whence */ + arg = alcove_list_head(&hd, arg); + if (!hd || !ERL_IS_INTEGER(hd)) + goto BADARG; + + offset = ERL_INT_VALUE(hd); + + /* stdin, stdout, stderr, ctl are reserved */ + if (fd < 4) + return alcove_errno(EBADF); + + return (lseek(fd, offset, whence) == -1) + ? alcove_errno(errno) + : erl_mk_atom("ok"); + +BADARG: + return erl_mk_atom("badarg"); +} + /* * read(2) *