mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 14:17:36 +00:00
Implement basic chmod() syscall and /bin/chmod helper.
Only raw octal modes are supported right now. This patch also changes mode_t from 32-bit to 16-bit to match the on-disk type used by Ext2FS. I also ran into EPERM being errno=0 which was confusing, so I inserted an ESUCCESS in its place.
This commit is contained in:
parent
ad53f6afd3
commit
c30e2c8d44
22 changed files with 156 additions and 4 deletions
|
@ -66,6 +66,47 @@ ALWAYS_INLINE int printNumber(PutChFunc putch, char*& bufptr, dword number, bool
|
|||
return fieldWidth;
|
||||
}
|
||||
|
||||
template<typename PutChFunc>
|
||||
ALWAYS_INLINE int print_octal_number(PutChFunc putch, char*& bufptr, dword number, bool leftPad, bool zeroPad, dword fieldWidth)
|
||||
{
|
||||
dword divisor = 134217728;
|
||||
char ch;
|
||||
char padding = 1;
|
||||
char buf[32];
|
||||
char* p = buf;
|
||||
|
||||
for (;;) {
|
||||
ch = '0' + (number / divisor);
|
||||
number %= divisor;
|
||||
if (ch != '0')
|
||||
padding = 0;
|
||||
if (!padding || divisor == 1)
|
||||
*(p++) = ch;
|
||||
if (divisor == 1)
|
||||
break;
|
||||
divisor /= 8;
|
||||
}
|
||||
|
||||
size_t numlen = p - buf;
|
||||
if (!fieldWidth || fieldWidth < numlen)
|
||||
fieldWidth = numlen;
|
||||
if (!leftPad) {
|
||||
for (unsigned i = 0; i < fieldWidth - numlen; ++i) {
|
||||
putch(bufptr, zeroPad ? '0' : ' ');
|
||||
}
|
||||
}
|
||||
for (unsigned i = 0; i < numlen; ++i) {
|
||||
putch(bufptr, buf[i]);
|
||||
}
|
||||
if (leftPad) {
|
||||
for (unsigned i = 0; i < fieldWidth - numlen; ++i) {
|
||||
putch(bufptr, ' ');
|
||||
}
|
||||
}
|
||||
|
||||
return fieldWidth;
|
||||
}
|
||||
|
||||
template<typename PutChFunc>
|
||||
ALWAYS_INLINE int printString(PutChFunc putch, char*& bufptr, const char* str, bool leftPad, dword fieldWidth)
|
||||
{
|
||||
|
@ -145,6 +186,10 @@ one_more:
|
|||
ret += printNumber(putch, bufptr, va_arg(ap, dword), leftPad, zeroPad, fieldWidth);
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
ret += print_octal_number(putch, bufptr, va_arg(ap, dword), leftPad, zeroPad, fieldWidth);
|
||||
break;
|
||||
|
||||
case 'x':
|
||||
ret += printHex(putch, bufptr, va_arg(ap, dword), 8);
|
||||
break;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue