Luna/libos/src/Mode.cpp

44 lines
1.3 KiB
C++

/**
* @file Mode.cpp
* @author apio (cloudapio.eu)
* @brief Visual file permissions parsing and formatting.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
#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_ISBLK(mode)) return 'b';
if (S_ISLNK(mode)) return 'l';
if (S_ISFIFO(mode)) return 'p';
if (S_ISSOCK(mode)) return 's';
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' : '-';
out[9] = (mode & S_ISVTX) ? ((mode & S_IXOTH) ? 't' : 'T') : ((mode & S_IXOTH) ? 'x' : '-');
out[10] = '\0';
}
}