Advertisement
leo11

Untitled

May 12th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.89 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* log, 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.             entry->d_name[entry_name_len - 1] == 't' &&
  70.             entry->d_name[entry_name_len - 2] == 'x' &&
  71.             entry->d_name[entry_name_len - 3] == 't' &&
  72.             entry->d_name[entry_name_len - 4] == '.')
  73.         {
  74.             fprintf(log, "filename and dir - %s/%s\n", cur_dir->dir, entry->d_name);
  75.  
  76.             char *file = (char*) malloc (strlen(cur_dir->dir)+1+strlen(entry->d_name) + 1);
  77.             strcpy(file, cur_dir->dir);
  78.             strcat(file, "/");
  79.             strcat(file, entry->d_name);
  80.  
  81.             FILE* open_file = fopen(file, "r");
  82.  
  83.             char * str_from_file = (char*) malloc (256);
  84.             fgets(str_from_file, 256, open_file);
  85.             while (!feof(open_file))
  86.             {
  87.                 fprintf(texts, "%s", str_from_file);
  88.                 fgets(str_from_file, 256, open_file);
  89.             }
  90.             fclose(open_file);
  91.         }
  92.         // else if (entry->d_type == DT_DIR &&
  93.         //          strcmp(entry->d_name, "..") &&
  94.         //          strcmp(entry->d_name, "."))
  95.         // {
  96.         //     Node_dir* next = next_dir(cur_dir, entry->d_name);
  97.         //     // fprintf(log, "Next direct %s\n\n", next->dir);
  98.         //     rec_check_dir(next, texts);
  99.         //     prev_dir(next);
  100.         //     free(next);
  101.         // }
  102.     }
  103.     fprintf(log, "Current dir is finished %s\n", cur_dir->dir);
  104.     closedir(cur_open_direct);
  105.     return;
  106. }
  107.  
  108.  
  109. int main()
  110. {
  111.     Node_dir* head_direct = (Node_dir*) malloc (sizeof(Node_dir));
  112.     head_direct->next_dir = NULL;
  113.     head_direct->prev_dir = NULL;
  114.     char* path = "/home/leo11/Projects/C/text_files";
  115.     long len = strlen(path);
  116.     head_direct->dir = (char*) malloc (len +1);
  117.     strcpy(head_direct->dir, path);
  118.  
  119.     FILE* log = fopen("lllllog.txt", "w");
  120.     // FILE* texts = fopen("ssssssssss.txt", "w");
  121.     char** texts = (char**) malloc (sizeof(char*));
  122.     long count_str = 0;
  123.     rec_check_dir(head_direct, log, texts, count);
  124.     fclose(texts);
  125.  
  126.     FILE* _texts = fopen("ssssssssss.txt", "r");
  127.     fseek(_texts, 0, SEEK_END);
  128.     long int pos = ftell(_texts);
  129.     if (pos > 0)
  130.         rewind(_texts);
  131.     else
  132.         exit(EXIT_SUCCESS);
  133.  
  134.     char str_from_file[256];
  135.     char** array_str = (char**) malloc (sizeof(char*));
  136.  
  137.     int counter = 0;
  138.     while (fgets(str_from_file, 256, _texts) != NULL)
  139.     {
  140.         array_str[counter] = (char*) malloc (strlen(str_from_file)+1);
  141.         strcpy(array_str[counter], str_from_file);
  142.         counter++;
  143.         array_str = (char**) realloc(array_str, (counter+1)* sizeof(char*));
  144.     }
  145.     fclose(_texts);
  146.  
  147.     qsort(array_str, counter, sizeof(char*), comp);
  148.  
  149.     FILE* result = fopen("vvvvvvvvvvv.txt", "w");
  150.  
  151.     int i;
  152.     for(i = 0; i < counter; i++)
  153.         fprintf(result, "%s", array_str[i]);
  154.  
  155.     fclose(result);
  156.     for(i = 0; i < counter; i++)
  157.         free(array_str[i]);
  158.     free(array_str);
  159.  
  160.     return 0;
  161. }
  162.  
  163. int comp(const void *a, const void *b)
  164. {
  165.     char* aa = *(char**) a;
  166.     char* bb = *(char**) b;
  167.  
  168.     int count_c1 = 0;
  169.     int count_c2 = 0;
  170.  
  171.     char *caa = aa;
  172.     char* cbb = bb;
  173.  
  174.     while (*caa++)
  175.         count_c1 += (int)(*caa);
  176.  
  177.     while (*cbb++)
  178.         count_c2 += (int)(*cbb);
  179.  
  180.     if (count_c1 > count_c2)
  181.         return 1;
  182.     else if (count_c1 < count_c2)
  183.         return -1;
  184.     else
  185.         return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement