34 lines
1020 B
C++
34 lines
1020 B
C++
|
#include <os/Mode.h>
|
||
|
#include <sys/stat.h>
|
||
|
|
||
|
namespace os
|
||
|
{
|
||
|
static char filetype(mode_t mode)
|
||
|
{
|
||
|
if (S_ISREG(mode)) return '-';
|
||
|
if (S_ISDIR(mode)) return 'd';
|
||
|
|
||
|
if (S_ISCHR(mode)) return 'c';
|
||
|
if (S_ISLNK(mode)) return 'l';
|
||
|
if (S_ISFIFO(mode)) return 'p';
|
||
|
|
||
|
return '?';
|
||
|
}
|
||
|
|
||
|
void format_mode(mode_t mode, char out[11])
|
||
|
{
|
||
|
out[0] = filetype(mode);
|
||
|
out[1] = (mode & S_IRUSR) ? 'r' : '-';
|
||
|
out[2] = (mode & S_IWUSR) ? 'w' : '-';
|
||
|
out[3] = (mode & S_ISUID) ? ((mode & S_IXUSR) ? 's' : 'S') : ((mode & S_IXUSR) ? 'x' : '-');
|
||
|
out[4] = (mode & S_IRGRP) ? 'r' : '-';
|
||
|
out[5] = (mode & S_IWGRP) ? 'w' : '-';
|
||
|
out[6] = (mode & S_ISGID) ? ((mode & S_IXGRP) ? 's' : 'S') : ((mode & S_IXGRP) ? 'x' : '-');
|
||
|
out[7] = (mode & S_IROTH) ? 'r' : '-';
|
||
|
out[8] = (mode & S_IWOTH) ? 'w' : '-';
|
||
|
// FIXME: Support the sticky bit.
|
||
|
out[9] = (mode & S_IXOTH) ? 'x' : '-';
|
||
|
out[10] = '\0';
|
||
|
}
|
||
|
}
|