Skip to content

Commit

Permalink
FEAT: unifying return and error handling from the original `access-os…
Browse files Browse the repository at this point in the history
…` function

Instead of `0` it returns `true` as a result of successful `set` operation
Instead of returning none, it throws `not supported on your system` error when return value is of type OS_ENA
It throws error when any other error happen.

So for example on Windows it looks like:

```
>> access-os/set 'pid 33488 ;<-- terminates process
== true

>> access-os/set 'pid 33488
** Access error: process not found: 33488
** Where: access-os
** Near: access-os/set 'pid 33488

>> access-os 'euid
** Internal error: euid not supported on your system
** Where: access-os
** Near: access-os 'euid

```
  • Loading branch information
Oldes committed Jun 17, 2018
1 parent 64b724c commit c011a25
Showing 1 changed file with 40 additions and 135 deletions.
175 changes: 40 additions & 135 deletions src/core/n-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1062,152 +1062,57 @@ REBNATIVE(access_os)
REBVAL *field = D_ARG(1);
REBOOL set = D_REF(2);
REBVAL *val = D_ARG(3);
REBINT ret = 0;
REBVAL *pid = 0;

switch (VAL_WORD_CANON(field)) {
case SYM_UID:
if (set) {
if (IS_INTEGER(val)) {
REBINT ret = OS_SET_UID(VAL_INT32(val));
if (ret < 0) {
switch (ret) {
case OS_ENA:
return R_NONE;
case OS_EPERM:
Trap0(RE_PERMISSION_DENIED);
break;
case OS_EINVAL:
Trap_Arg(val);
break;
default:
Trap_Arg(val);
break;
}
} else {
SET_INTEGER(D_RET, ret);
return R_RET;
}
ret = OS_SET_UID(VAL_INT32(val));
} else {
Trap_Arg(val);
}
} else {
REBINT ret = OS_GET_UID();
if (ret < 0) {
return R_NONE;
} else {
SET_INTEGER(D_RET, ret);
return R_RET;
}
ret = OS_GET_UID();
}
break;
case SYM_GID:
if (set) {
if (IS_INTEGER(val)) {
REBINT ret = OS_SET_GID(VAL_INT32(val));
if (ret < 0) {
switch (ret) {
case OS_ENA:
return R_NONE;
case OS_EPERM:
Trap0(RE_PERMISSION_DENIED);
break;
case OS_EINVAL:
Trap_Arg(val);
break;
default:
Trap_Arg(val);
break;
}
} else {
SET_INTEGER(D_RET, ret);
return R_RET;
}
ret = OS_SET_GID(VAL_INT32(val));
} else {
Trap_Arg(val);
}
} else {
REBINT ret = OS_GET_GID();
if (ret < 0) {
return R_NONE;
} else {
SET_INTEGER(D_RET, ret);
return R_RET;
}
ret = OS_GET_GID();
}
break;
case SYM_EUID:
if (set) {
if (IS_INTEGER(val)) {
REBINT ret = OS_SET_EUID(VAL_INT32(val));
if (ret < 0) {
switch (ret) {
case OS_ENA:
return R_NONE;
case OS_EPERM:
Trap0(RE_PERMISSION_DENIED);
break;
case OS_EINVAL:
Trap_Arg(val);
break;
default:
Trap_Arg(val);
break;
}
} else {
SET_INTEGER(D_RET, ret);
return R_RET;
}
ret = OS_SET_EUID(VAL_INT32(val));
} else {
Trap_Arg(val);
}
} else {
REBINT ret = OS_GET_EUID();
if (ret < 0) {
return R_NONE;
} else {
SET_INTEGER(D_RET, ret);
return R_RET;
}
ret = OS_GET_EUID();
}
break;
case SYM_EGID:
if (set) {
if (IS_INTEGER(val)) {
REBINT ret = OS_SET_EGID(VAL_INT32(val));
if (ret < 0) {
switch (ret) {
case OS_ENA:
return R_NONE;
case OS_EPERM:
Trap0(RE_PERMISSION_DENIED);
break;
case OS_EINVAL:
Trap_Arg(val);
break;
default:
Trap_Arg(val);
break;
}
} else {
SET_INTEGER(D_RET, ret);
return R_RET;
}
ret = OS_SET_EGID(VAL_INT32(val));
} else {
Trap_Arg(val);
}
} else {
REBINT ret = OS_GET_EGID();
if (ret < 0) {
return R_NONE;
} else {
SET_INTEGER(D_RET, ret);
return R_RET;
}
ret = OS_GET_EGID();
}
break;
case SYM_PID:
if (set) {
REBINT ret = 0;
REBVAL *pid = val;
pid = val;
REBVAL *arg = val;
if (IS_INTEGER(val)) {
ret = OS_KILL(VAL_INT32(pid));
Expand All @@ -1230,39 +1135,39 @@ REBNATIVE(access_os)
} else {
Trap_Arg(val);
}

if (ret < 0) {
switch (ret) {
case OS_ENA:
return R_NONE;
case OS_EPERM:
Trap0(RE_PERMISSION_DENIED);
break;
case OS_EINVAL:
Trap_Arg(arg);
break;
case OS_ESRCH:
Trap1(RE_PROCESS_NOT_FOUND, pid);
break;
default:
Trap_Arg(val);
break;
}
} else {
SET_INTEGER(D_RET, ret);
return R_RET;
}
} else {
REBINT ret = OS_GET_PID();
if (ret < 0) {
return R_NONE;
} else {
SET_INTEGER(D_RET, ret);
return R_RET;
}
ret = OS_GET_PID();
}
break;
default:
Trap_Arg(field);
}

if(ret > 0) {
SET_INTEGER(D_RET, ret);
return R_RET;
}

if (ret == 0) {
SET_TRUE(D_RET);
return R_RET;
}

switch (ret) {
case OS_ENA:
Trap1(RE_NOT_HERE, field);
break;
case OS_EPERM:
Trap0(RE_PERMISSION_DENIED);
break;
case OS_EINVAL:
Trap_Arg(val);
break;
case OS_ESRCH:
Trap1(RE_PROCESS_NOT_FOUND, pid);
break;
default:
Trap_Arg(val);
break;
}
}

0 comments on commit c011a25

Please sign in to comment.