Advertisement
leo11

Untitled

May 6th, 2021
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.44 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<dirent.h>
  4. #include<sys/types.h>
  5. #include<string.h>
  6. #include<ctype.h>
  7.  
  8. int comp(const void *a, const void *b);
  9.  
  10. typedef struct Node_dir
  11. {
  12.     struct Node_dir *next_dir;
  13.     struct Node_dir *prev_dir;
  14.     char *dir;
  15. }
  16.         Node_dir;
  17.  
  18. Node_dir* next_dir(Node_dir* head, char* dir)
  19. {
  20.     while (head->next_dir != NULL)
  21.         head = head->next_dir;
  22.  
  23.     Node_dir* push_dir = (Node_dir*) malloc (sizeof(Node_dir));
  24.  
  25.     push_dir->next_dir = NULL;
  26.     head->next_dir = push_dir;
  27.     push_dir->prev_dir = head;
  28.     push_dir->dir = (char*) malloc(strlen(head->dir) +1+ strlen(dir) + 1);
  29.  
  30.     strcpy(push_dir->dir, head->dir);
  31.  
  32.     if (strcmp(head->dir, "/") == 0){
  33.         strcat(push_dir->dir, dir);
  34.     }
  35.     else{
  36.         strcat(push_dir->dir, "/");
  37.         strcat(push_dir->dir, dir);
  38.     }
  39.  
  40.     return push_dir;
  41. }
  42.  
  43. void prev_dir(Node_dir* head)
  44. {
  45.     while (head->next_dir != NULL)
  46.         head = head->next_dir;
  47.  
  48.     free(head->dir);
  49.     head->prev_dir->next_dir = NULL;
  50.      head->prev_dir = NULL;
  51. }
  52.  
  53. void rec_check_dir(Node_dir* cur_dir, FILE* texts)
  54. {
  55.     // fprintf(log, "Current direct - %s\n", cur_dir->dir);
  56.     DIR* cur_open_direct = opendir(cur_dir->dir);
  57.     if (!cur_open_direct)
  58.     {
  59.         // fprintf(log, "\nCant open direct %s\n", cur_dir->dir);
  60.         return;
  61.     }
  62.  
  63.     struct dirent* entry;
  64.  
  65.     while ((entry = readdir(cur_open_direct)))
  66.     {
  67.         long entry_name_len = strlen(entry->d_name);
  68.         if (entry->d_type == DT_REG)
  69.         {
  70.             // fprintf(log, "filename and dir - %s/%s\n", cur_dir->dir, entry->d_name);
  71.  
  72.             char *file = (char*) malloc (strlen(cur_dir->dir)+1+strlen(entry->d_name) + 1);
  73.             strcpy(file, cur_dir->dir);
  74.             strcat(file, "/");
  75.             strcat(file, entry->d_name);
  76.  
  77.             FILE* open_file = fopen(file, "r");
  78.  
  79.             char * str_from_file = (char*) malloc (256);
  80.             fgets(str_from_file, 256, open_file);
  81.             while (!feof(open_file))
  82.             {
  83.                 fprintf(texts, "%s", str_from_file);
  84.                 fgets(str_from_file, 256, open_file);
  85.             }
  86.             fclose(open_file);
  87.         }
  88.         else if (entry->d_type == DT_DIR &&
  89.                  strcmp(entry->d_name, "..") &&
  90.                  strcmp(entry->d_name, "."))
  91.         {
  92.             Node_dir* next = next_dir(cur_dir, entry->d_name);
  93.             // fprintf(log, "Next direct %s\n\n", next->dir);
  94.             rec_check_dir(next, texts);
  95.             prev_dir(next);
  96.             free(next);
  97.         }
  98.     }
  99.     // fprintf(log, "Current dir is finished %s\n", cur_dir->dir);
  100.     closedir(cur_open_direct);
  101.     return;
  102. }
  103.  
  104.  
  105. int main()
  106. {
  107.     Node_dir* head_direct = (Node_dir*) malloc (sizeof(Node_dir));
  108.     head_direct->next_dir = NULL;
  109.     head_direct->prev_dir = NULL;
  110.     head_direct->dir = (char*) malloc (strlen("/home/box/root") +1);
  111.     strcpy(head_direct->dir, "/home/box/root");
  112.  
  113.     FILE* texts = fopen("_textx", "w");
  114.     rec_check_dir(head_direct, texts);
  115.     fclose(texts);
  116.  
  117.     FILE* _texts = fopen("_textx", "r");
  118.     fseek(_texts, 0, SEEK_END);
  119.     long int pos = ftell(_texts);
  120.     if (pos > 0)
  121.         rewind(_texts);
  122.     else
  123.         exit(EXIT_SUCCESS);
  124.  
  125.     char str_from_file[256];
  126.     char** array_str = (char**) malloc (sizeof(char*));
  127.  
  128.     int counter = 0;
  129.     while (fgets(str_from_file, 256, _texts) != NULL)
  130.     {
  131.         array_str[counter] = (char*) malloc (strlen(str_from_file)+1);
  132.         strcpy(array_str[counter], str_from_file);
  133.         counter++;
  134.         array_str = (char**) realloc(array_str, (counter+1)* sizeof(char*));
  135.     }
  136.     fclose(_texts);
  137.  
  138.     qsort(array_str, counter, sizeof(char*), comp);
  139.  
  140.     FILE* result = fopen("/home/box/a.txt", "w");
  141.  
  142.     int i;
  143.     for(i = 0; i < counter; i++)
  144.         fprintf(result, "%s", array_str[i]);
  145.  
  146.     fclose(result);
  147.     for(i = 0; i < counter; i++)
  148.         free(array_str[i]);
  149.     free(array_str);
  150.  
  151.     return 0;
  152. }
  153.  
  154. int comp(const void *a, const void *b)
  155. {
  156.     char* aa = *(char**) a;
  157.     char* bb = *(char**) b;
  158.  
  159.     int count_c1 = 0;
  160.     int count_c2 = 0;
  161.  
  162.     char *caa = aa;
  163.     char* cbb = bb;
  164.  
  165.     while (*caa++)
  166.         count_c1 += (int)(*caa);
  167.  
  168.     while (*cbb++)
  169.         count_c2 += (int)(*cbb);
  170.  
  171.     if (count_c1 > count_c2)
  172.         return 1;
  173.     else if (count_c1 < count_c2)
  174.         return -1;
  175.     else
  176.         return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement