do not edit — generated by btf.
git.druid.rocksindexdruid520kaboomsrc/user/chmod.nsc

src/user/chmod.nsc


include "syscalls.nsh";
include "lib.nsh";
include "string.nsh";
include "ctype.nsh";
include "stdio.nsh";
 
/* chmod perm file -- perm is one bitmask digit, 0-7 (r=1 w=2 x=4),
 * not unix's 3-digit owner/group/other octal: kaboom has no user
 * accounts to have separate owner/group/other permissions for, so
 * one digit is the whole permission model (see kfs.nsc). accepts
 * that digit directly ("chmod 3 file"), or the same bitmask spelled
 * out as an rwx letter triplet with '-' for an unset bit ("chmod rw-
 * file") -- exactly the form perms.nsc now prints back, so whatever
 * perms shows you is also valid chmod input, unchanged either way. */
global i32
main(i32 argc, ptr argv)
{
	ptr permstr;
	ptr name;
	u64 name_len;
	u64 perm_len;
	u8 c0;
	u8 c1;
	u8 c2;
	u64 perm;
 
	if(argc < 3)
	{
		fputs(2, "usage: chmod perm file  (perm: 0-7, or rwx letters e.g. rw-)\n");
		return 1;
	}
 
	permstr = argv_get(argv, 1);
	perm_len = strlen(permstr);
 
	if(perm_len == (u64)1 && is_digit(ptr_byte_at(permstr, (u64)0)) != 0 && ptr_byte_at(permstr, (u64)0) <= (u8)55)
	{
		perm = (u64)(ptr_byte_at(permstr, (u64)0) - (u8)48);
	}
	else if(perm_len == (u64)3)
	{
		c0 = ptr_byte_at(permstr, (u64)0);
		c1 = ptr_byte_at(permstr, (u64)1);
		c2 = ptr_byte_at(permstr, (u64)2);
		if((c0 != (u8)114 && c0 != (u8)45) || (c1 != (u8)119 && c1 != (u8)45) || (c2 != (u8)120 && c2 != (u8)45))
		{
			fputs(2, "chmod: perm must be 0-7, or rwx letters e.g. rw-\n");
			return 1;
		}
		perm = (u64)0;
		if(c0 == (u8)114)
		{
			perm = perm | (u64)1;
		}
		if(c1 == (u8)119)
		{
			perm = perm | (u64)2;
		}
		if(c2 == (u8)120)
		{
			perm = perm | (u64)4;
		}
	}
	else
	{
		fputs(2, "chmod: perm must be 0-7, or rwx letters e.g. rw-\n");
		return 1;
	}
 
	name = argv_get(argv, 2);
	name_len = strlen(name);
	if(sys_chmod(name, name_len, perm) == 0)
	{
		print_err(2, "chmod", "failed");
		return 1;
	}
	return 0;
}
powered by btf.