#include #include #include #include #include #include // ANSI escape sequences for producing the colors #define PINK "\033[95m" #define BLUE "\033[94m" #define YELLOW "\033[93m" #define GREEN "\033[92m" #define RED "\033[91m" #define ENDC "\033[0m" int main(void) { DIR *dir; struct dirent *entry = NULL; struct stat entry_stat; char *file_name; char *path; // Opens the current directory we're in (man 3 opendir) dir = opendir("."); if (dir == NULL) { return EXIT_FAILURE; } // Reads each directory entry until we run out of entries (man 3 readdir) while ((entry = readdir(dir)) != NULL) { file_name = entry->d_name; // Concatenates "./" to the beginning of the file name, so that it // can be used with lstat() to retrieve information about the file path = (char *) malloc(sizeof(char) * (strlen(file_name) + 3)); strcpy(path, "./"); strcat(path, file_name); // Retrieves information about the file (man 2 lstat) lstat(path, &entry_stat); // Sets the color for the different types of files if (strcmp(file_name, ".") == 0 || strcmp(file_name, "..") == 0) { printf("%s", BLUE); } else if (S_ISDIR(entry_stat.st_mode)) { printf("%s", PINK); } else { printf("%s", RED); } // Prints the file name printf("%s%s\n", file_name, ENDC); } return EXIT_SUCCESS; }