-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.c
44 lines (35 loc) · 1.18 KB
/
color.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "headers.h"
void print_in_color(char* string,char* print_string){
struct stat file_stat;
int code=0;
// Use the stat function to get file information
if (stat(string, &file_stat) == 0) {
if (S_ISDIR(file_stat.st_mode)) {
code=2;
}
if ((file_stat.st_mode & S_IXUSR || file_stat.st_mode & S_IXGRP || file_stat.st_mode & S_IXOTH) && code!=2) {
code=3;
}
}
if(code==0){printf("%s",print_string);}
else if(code==2){
printf(ANSI_COLOR_BLUE "%s" ANSI_COLOR_RESET,print_string);
}
else if(code==3){
printf(ANSI_COLOR_GREEN "%s" ANSI_COLOR_RESET,print_string);
}
}
void print_permissions(mode_t mode) {
putchar((mode & S_IRUSR) ? 'r' : '-');
putchar((mode & S_IWUSR) ? 'w' : '-');
putchar((mode & S_IXUSR) ? 'x' : '-');
putchar((mode & S_IRGRP) ? 'r' : '-');
putchar((mode & S_IWGRP) ? 'w' : '-');
putchar((mode & S_IXGRP) ? 'x' : '-');
putchar((mode & S_IROTH) ? 'r' : '-');
putchar((mode & S_IWOTH) ? 'w' : '-');
putchar((mode & S_IXOTH) ? 'x' : '-');
}
void perrorf(char* s){
printf(ANSI_COLOR_RED "%s\n" ANSI_COLOR_RESET,s);
}