├── makefile ├── README.md └── my_ls.c /makefile: -------------------------------------------------------------------------------- 1 | CC := gcc 2 | CC := ${CC} 3 | OUTPUT := my_ls 4 | 5 | all: my_ls 6 | 7 | my_ls: 8 | @echo Compiling 9 | @${CC} my_ls.c -Wall -Wextra -Werror -o ${OUTPUT}; 10 | @echo "Compiled to ${OUTPUT}" 11 | 12 | clean: 13 | # rm *.o 14 | 15 | fclean: clean 16 | rm ${OUTPUT} 17 | 18 | re: fclean all -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Welcome to My Ls 2 | *** 3 | 4 | ## Task 5 | it was very difficult. What I like the most is that you have to be very logical. 6 | you must write "make" on the check 7 | 8 | ## Description 9 | I thought a lot and searched on the Internet. 10 | then i saw something. 11 | I tried to write based on the code I found, but it didn't work. 12 | I tried again and changed a lot of places and got the expected result 13 | 14 | ## Installation 15 | not Installed 16 | 17 | terminal ====> make 18 | 19 | ## Usage 20 | TODO - How does it work? 21 | ``` 22 | ./my_project argument1 argument2 23 | ``` 24 | 25 | ### The Core Team 26 | 27 | 28 | Made at Qwasar Silicon Valley 29 | Qwasar Silicon Valley Logo 30 | -------------------------------------------------------------------------------- /my_ls.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #ifndef STRUCT_LISTNODE 9 | #define STRUCT_LISTNODE 10 | 11 | typedef struct slistnodes{ 12 | struct slistnodes *nextt; 13 | int types; 14 | long times; 15 | char *names; 16 | } 17 | listnodes; 18 | #endif 19 | 20 | int getColorsType(int types){ 21 | switch (types) { 22 | case 4: 23 | return 35; 24 | case 8: 25 | return 36; 26 | case 9: 27 | return 32; 28 | default: 29 | return 37; 30 | } 31 | } 32 | 33 | void printLists(listnodes *curren){ 34 | 35 | while (curren != NULL){ 36 | printf("\033[0;%dm%s\033[0m\n", getColorsType(curren->types), curren->names); 37 | curren = curren->nextt; 38 | } 39 | printf("\n"); 40 | } 41 | 42 | listnodes *addElementLast(listnodes *heads, listnodes *elements){ 43 | 44 | if (heads == NULL){ 45 | 46 | return elements; 47 | } 48 | listnodes *lists = heads; 49 | while (lists->nextt != NULL){ 50 | 51 | lists = lists->nextt; 52 | } 53 | lists->nextt = elements; 54 | return heads; 55 | } 56 | 57 | listnodes *addElementSorted(listnodes *heads, listnodes *elements){ 58 | 59 | if (heads == NULL){ 60 | 61 | return elements; 62 | } 63 | if (heads->times < elements->times){ 64 | 65 | elements->nextt = heads; 66 | return elements; 67 | } 68 | if (heads->nextt == NULL){ 69 | 70 | heads->nextt = elements; 71 | return heads; 72 | } 73 | listnodes *curren = heads->nextt; 74 | listnodes *prev = heads; 75 | while (curren != NULL){ 76 | 77 | if (curren->times < elements->times){ 78 | prev->nextt = elements; 79 | elements->nextt = curren; 80 | return heads; 81 | } 82 | else if (curren->times == elements->times){ 83 | 84 | char *elemNames = (elements->names[0] == '.') ? elements->names + 1 : elements->names; 85 | char *currNames = (curren->names[0] == '.') ? curren->names + 1 : curren->names; 86 | int compares = strcmp(elemNames, currNames); 87 | if (compares < 0){ 88 | 89 | elements->nextt = curren; 90 | prev->nextt = elements; 91 | return heads; 92 | } 93 | } 94 | prev = curren; 95 | curren = curren->nextt; 96 | } 97 | 98 | prev->nextt = elements; 99 | return heads; 100 | } 101 | 102 | int main(int argc, char const *argv[]){ 103 | 104 | struct dirent *dp; 105 | struct stat attr; 106 | bool showHiddens = false; 107 | bool isSorteds = false; 108 | int i = 1; 109 | 110 | while (argv[i] && argv[i][0] == '-'){ 111 | 112 | if (strcmp(argv[i], "-at") == 0 || strcmp(argv[i], "-ta") == 0){ 113 | 114 | isSorteds = true; 115 | showHiddens = true; 116 | } 117 | else if (strcmp(argv[i], "-t") == 0){ 118 | 119 | isSorteds = true; 120 | } 121 | else if (strcmp(argv[i], "-a") == 0){ 122 | 123 | showHiddens = true; 124 | } 125 | i++; 126 | } 127 | if (!argv[i]){ 128 | 129 | argv[i] = "."; 130 | argc++; 131 | } 132 | while (i < argc){ 133 | 134 | DIR *directory = opendir(argv[i]); 135 | 136 | if (directory == NULL){ 137 | 138 | printf("Directory might not exist or can not be accessed\n"); 139 | return 0; 140 | } 141 | listnodes *heads = (listnodes *)malloc(sizeof(listnodes)); 142 | heads = NULL; 143 | 144 | while ((dp = readdir(directory)) != NULL){ 145 | 146 | if (showHiddens || !(dp->d_name[0] == '.')){ 147 | 148 | char dirPath[50]; 149 | strcpy(dirPath, argv[argc - 1]); 150 | lstat(strcat(dirPath, dp->d_name), &attr); 151 | listnodes *elements = (listnodes *)malloc(sizeof(listnodes)); 152 | elements->times = attr.st_mtim.tv_sec; 153 | elements->names = dp->d_name; 154 | elements->types = (dp->d_name[0] == '.') ? 9 : dp->d_type; 155 | elements->nextt = NULL; 156 | if (isSorteds){ 157 | heads = addElementSorted(heads, elements); 158 | } 159 | else{ 160 | heads = addElementLast(heads, elements); 161 | } 162 | } 163 | } 164 | 165 | closedir(directory); 166 | printLists(heads); 167 | printf("\n\n"); 168 | i++; 169 | } 170 | return 0; 171 | } --------------------------------------------------------------------------------