micro-utils/libmu/mode_to_str.c

54 lines
765 B
C
Raw Normal View History

2024-07-01 10:23:00 +00:00
#include <stdio.h>
2024-07-09 19:33:45 +00:00
#include "mode_to_str.h"
2024-07-01 10:23:00 +00:00
2024-07-09 19:54:12 +00:00
char mode[11];
2024-07-09 19:33:45 +00:00
char *mu_mode_2_str(mode_t file_mode) {
2024-07-01 10:23:00 +00:00
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;
}