micro-utils/libmu/mode_to_str.c
2024-09-01 16:43:41 +03:00

54 lines
765 B
C

#include <stdio.h>
#include "mode_to_str.h"
char mode[11];
char *mu_mode_2_str(mode_t file_mode) {
snprintf(mode, sizeof(mode), "----------");
if (file_mode & S_IRUSR)
mode[1] = 'r';
if (file_mode & S_IRGRP)
mode[4] = 'r';
if (file_mode & S_IROTH)
mode[7] = 'r';
if (file_mode & S_IWUSR)
mode[2] = 'w';
if (file_mode & S_IWGRP)
mode[5] = 'w';
if (file_mode & S_IWOTH)
mode[8] = 'w';
if (file_mode & S_IXUSR)
mode[3] = 'x';
if (file_mode & S_IXGRP)
mode[6] = 'x';
if (file_mode & S_IXOTH)
mode[9] = 'x';
if (file_mode & S_ISUID) {
if (file_mode & S_IXUSR)
mode[3] = 's';
else
mode[3] = 'S';
}
if (file_mode & S_ISGID) {
if (file_mode & S_IRGRP)
mode[6] = 's';
else
mode[6] = 'S';
}
return mode;
}