| git.druid.rocks | index | druid520 | kaboom | src/ | user/ | perms.nsc |
src/user/perms.nsc
include "syscalls.nsh";
include "lib.nsh";
include "string.nsh";
include "stdio.nsh";
/* perms file -- prints the r/w/x bits chmod actually set (r=1 w=2
* x=4, kaboom's own single-digit bitmask, not real unix's owner/
* group/other octal -- see chmod.nsc's own note), as "<name>: <digit>
* <rwx>" (e.g. "/file: 3 rw-") -- both the digit AND the letters,
* since chmod itself now takes either form (see chmod.nsc), and this
* is the one place that shows what a name is actually set to.
* chmod (kfs_chmod) could only ever WRITE a perm bit before this, and
* kfs_check_perm only ever checked one internally -- nothing exposed
* reading one back out to userspace at all, so there was no way to
* actually see what a previous chmod left a file at short of it
* failing or succeeding. sys_getperm (idt.nsc syscall 24) /
* kfs_getperm (kfs.nsc) are that read-back path now.
*/
global i32
main(i32 argc, ptr argv)
{
ptr name;
u64 name_len;
i64 perm;
i8 out[3];
i8 digit[1];
if(argc < 2)
{
fputs(2, "usage: perms file\n");
return 1;
}
name = argv_get(argv, 1);
name_len = strlen(name);
perm = (i64)sys_getperm(name, name_len);
if(perm < (i64)0)
{
print_err(2, "perms", "not found");
return 1;
}
if((perm & (i64)1) != (i64)0)
{
out[0] = (i8)114; /* 'r' */
}
else
{
out[0] = (i8)45; /* '-' */
}
if((perm & (i64)2) != (i64)0)
{
out[1] = (i8)119; /* 'w' */
}
else
{
out[1] = (i8)45; /* '-' */
}
if((perm & (i64)4) != (i64)0)
{
out[2] = (i8)120; /* 'x' */
}
else
{
out[2] = (i8)45; /* '-' */
}
digit[0] = (i8)(perm + (i64)48); /* '0'-'7' */
/* fputs/sys_write only, deliberately -- putchar buffers and only
* flushes on a newline or a full buffer (stdio.nsc), while fputs
* and sys_write are both immediate; mixing them mid-line reorders
* output (an immediate write can leapfrog a still-buffered
* putchar byte sitting ahead of it). every piece of this line
* goes out the same, immediate way instead. */
sys_write(1, name, name_len); /* argv string, not a literal -- stays sys_write */
fputs(1, ": ");
sys_write(1, &digit[0], (u64)1);
fputs(1, " ");
sys_write(1, &out[0], (u64)3);
fputs(1, "\n");
return 0;
}