Advertisement
Dalacul

SD_1

Mar 11th, 2020
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include "file.h"
  6. #include "headers.h"
  7.  
  8. int same (char *string1, char *string2)
  9. {
  10.     if (!strcmp (string1, string2))
  11.         return 1;
  12.     return 0;
  13. }
  14.  
  15. void create_fs (dir_node **root)
  16. {
  17.     (*root) = malloc (sizeof (dir_node));
  18.     (*root)->d = malloc (sizeof (Directory));
  19.     (*root)->d->name = strdup ("/");
  20.     (*root)->d->file_list = NULL;
  21.     (*root)->d->dir_list = NULL;
  22.     (*root)->d->parentDir = NULL;
  23.     (*root)->next = NULL;
  24.     (*root)->prev = NULL;
  25. }
  26.  
  27. void delete_fs (dir_node **root)
  28. {
  29.     //Caution: use only if root directory is empty
  30.     free ( (*root)->d->name);
  31.     free ( (*root)->d);
  32.     free ( (*root));
  33. }
  34.  
  35. void touch (dir_node *crt, char *file_name, char *file_data)
  36. {
  37.     //add file
  38.     //data initialization
  39.     file_node *new_node;
  40.     new_node = malloc (sizeof (file_node));
  41.     new_node->f = malloc (sizeof (File));
  42.     //
  43.     new_node->f->name = strdup (file_name);
  44.     new_node->f->data = strdup (file_data);
  45.     //
  46.     new_node->f->size = strlen (file_data);
  47.     //placing the file in an ordered list
  48.     if (crt->d->file_list == NULL)
  49.     {
  50.         //empty list
  51.         new_node->next = NULL;
  52.         crt->d->file_list = new_node;
  53.         return;
  54.     }
  55.     if (strcmp (crt->d->file_list->f->name, new_node->f->name) > 0)
  56.     {
  57.         //first element
  58.         //there is at least one file in the list
  59.         new_node->next = crt->d->file_list;
  60.         crt->d->file_list = new_node;
  61.         return;
  62.     }
  63.     file_node *p;
  64.     for (p = crt->d->file_list; p->next != NULL; p = p->next)
  65.         if (strcmp (p->f->name, new_node->f->name) < 0
  66.                 && strcmp (new_node->f->name, p->next->f->name) < 0)
  67.         {
  68.             new_node->next = p->next;
  69.             p->next = new_node;
  70.             return;
  71.         }
  72.     //in the last case, new_node->f is the last file
  73.     new_node->next = NULL;
  74.     p->next = new_node;
  75. }
  76.  
  77. void mkdir (dir_node *crt, char *directory_name)
  78. {
  79.     dir_node *new_node;
  80.     new_node = malloc (sizeof (dir_node));
  81.     new_node-> d = malloc (sizeof (Directory));
  82.     new_node->d->name = strdup (directory_name);
  83.     new_node->d->file_list = NULL;
  84.     new_node->d->dir_list = NULL;
  85.     new_node->d->parentDir = crt->d;
  86.     new_node->prev = crt;
  87.     //placing the directory in an ordered list
  88.     if (crt->d->dir_list == NULL)
  89.     {
  90.         new_node->next = NULL;
  91.         crt->d->dir_list = new_node;
  92.         return;
  93.     }
  94.     if (strcmp (crt->d->dir_list->d->name, new_node->d->name) > 0)
  95.     {
  96.         //there is at least one directory in the list
  97.         new_node->next = crt->d->dir_list;
  98.         crt->d->dir_list = new_node;
  99.         return;
  100.     }
  101.     dir_node *p;
  102.     for (p = crt->d->dir_list; p->next != NULL; p = p->next)
  103.         if (strcmp (p->d->name, new_node->d->name) < 0
  104.                 && strcmp (new_node->d->name, p->next->d->name) < 0)
  105.         {
  106.             new_node->next = p->next;
  107.             p->next = new_node;
  108.             return;
  109.         }
  110.     //in the last case, new_file is the last file
  111.     new_node->next = NULL;
  112.     p->next = new_node;
  113. }
  114.  
  115. void ls (dir_node *crt)
  116. {
  117.     file_node *p = malloc (sizeof (file_node));
  118.     dir_node *q = malloc (sizeof (dir_node));
  119.     //
  120.     for (p = crt->d->file_list; p != NULL; p = p->next)
  121.         printf ("%s ", p->f->name);
  122.     //
  123.     for (q = crt->d->dir_list; q != NULL; q = q->next)
  124.         printf ("%s ", q->d->name);
  125.     printf ("\n");
  126. }
  127.  
  128. dir_node* cd (dir_node *crt, char *directory_name)
  129. {
  130.     if (same (directory_name, ".."))
  131.         return crt->prev;
  132.     if (same (directory_name, "."))
  133.         return crt;
  134.     //search for directory_name in dir_list
  135.     dir_node *p;
  136.     for (p = crt->d->dir_list; p != NULL; p = p->next)
  137.         if (same (directory_name, p->d->name))
  138.             return p;
  139.     printf ("Cannot move to '%s': No such directory!\n", directory_name);
  140.     return crt;
  141. }
  142.  
  143. void print_parents (dir_node *crt, bool root)
  144. {
  145. //root
  146.     if (root)
  147.     {
  148.         printf ("/");
  149.         return;
  150.     }
  151.     //not-root
  152.     if (crt->prev == NULL)
  153.         return;
  154.     print_parents (crt->prev, false);
  155.     printf ("/%s", crt->d->name);
  156. }
  157.  
  158. void pwd (dir_node *crt)
  159. {
  160.     if (same (crt->d->name, "/"))
  161.         print_parents (crt, true);
  162.     else
  163.         print_parents (crt, false);
  164.     printf ("\n");
  165. }
  166.  
  167. void print_tabs (int tabs)
  168. {
  169.     int i;
  170.     for (i = 0; i < tabs; ++i)
  171.         printf ("    ");
  172. }
  173.  
  174. void tree_recursive (dir_node *crt, int tabs)
  175. {
  176.     file_node *p = malloc (sizeof (file_node));
  177.     dir_node *q = malloc (sizeof (dir_node));
  178.     for (p = crt->d->file_list; p != NULL; p = p->next)
  179.     {
  180.         print_tabs (tabs);
  181.         printf ("%s\n", p->f->name);
  182.     }
  183.     q = crt->d->dir_list;
  184.     if (q != NULL)
  185.     {
  186.         print_tabs (tabs);
  187.         printf ("%s\n", q->d->name);
  188.         tree_recursive (q, tabs + 1);
  189.         for (q = q->next; q != NULL; q = q->next)
  190.         {
  191.             print_tabs (tabs);
  192.             printf ("%s\n", q->d->name);
  193.             tree_recursive (q, tabs + 1);
  194.         }
  195.     }
  196. }
  197.  
  198. void tree (dir_node *crt)
  199. {
  200.     printf ("%s\n", crt->d->name);
  201.     tree_recursive (crt, 1);
  202. }
  203.  
  204. int rm (dir_node *crt, char *file_name)
  205. {
  206.     file_node *p;
  207.     p = crt->d->file_list;
  208.     if (p == NULL)
  209.     {
  210.         //empty list
  211.         printf ("Cannot remove '%s': No such file!\n", file_name);
  212.         return 0;
  213.     }
  214.     if (same (p->f->name, file_name))
  215.     {
  216.         //first element is the one
  217.         if (p->next == NULL)
  218.         {
  219.             //there is only one element in the list
  220.             crt->d->file_list = NULL;
  221.             free (p->f->name);
  222.             free (p->f->data);
  223.             free (p->f);
  224.             free (p);
  225.             return 0;
  226.         }
  227.         //there are at least 2 elements
  228.         crt->d->file_list = p->next;
  229.         free (p->f->name);
  230.         free (p->f->data);
  231.         free (p->f);
  232.         free (p);
  233.         return 1;
  234.     }
  235.     //the searched file is not the first element
  236.     for (; p->next != NULL; p = p->next)
  237.     {
  238.         if (same (p->next->f->name, file_name))
  239.         {
  240.             file_node *q = malloc (sizeof (file_node));
  241.             q = p->next;
  242.             p->next = p->next->next;
  243.             free (q->f->name);
  244.             free (q->f->data);
  245.             free (q->f);
  246.             free (q);
  247.             return 1;
  248.         }
  249.     }
  250.     printf ("Cannot remove '%s': No such file!\n", file_name);
  251.     return 0;
  252. }
  253.  
  254. void delete_dir (dir_node *crt)
  255. {
  256.     file_node *p, *p_next;
  257.     dir_node *q, *q_next;
  258.     for (p = crt->d->file_list; p != NULL;)
  259.     {
  260.         p_next = p->next;
  261.         if (rm (crt, p->f->name) == 0)
  262.             break;
  263.         p = p_next;
  264.     }
  265.     for (q = crt->d->dir_list; q != NULL;)
  266.     {
  267.         q_next = q->next;
  268.         rmdir (crt, q->d->name, true);
  269.         q = q_next;
  270.     }
  271.     free (crt->d->name);
  272.     free (crt->d);
  273.     free (crt);
  274. }
  275.  
  276. void rmdir (dir_node *crt, char *dir_name, bool rec)
  277. {
  278.     dir_node *p;
  279.     p = crt->d->dir_list;
  280.     if (p == NULL)
  281.     {
  282.         //no subdirectories
  283.         if (!rec)
  284.             printf ("Cannot remove '%s': No such directory!\n", dir_name);
  285.         else
  286.             delete_dir (crt);
  287.         return;
  288.     }
  289.     if (same (p->d->name, dir_name))
  290.     {
  291.         //first element is the one
  292.         if (p->next == NULL)
  293.         {
  294.             //there is only one element in the list
  295.             crt->d->dir_list = NULL;
  296.             delete_dir (p);
  297.             return;
  298.         }
  299.         //there are at least 2 elements
  300.         crt->d->dir_list = p->next;
  301.         delete_dir (p);
  302.         return;
  303.     }
  304.     //the searched file is not the first element
  305.     for (; p->next != NULL; p = p->next)
  306.     {
  307.         if (same (p->next->d->name, dir_name))
  308.         {
  309.             dir_node *q = malloc (sizeof (dir_node));
  310.             q = p->next;
  311.             p->next = p->next->next;
  312.             delete_dir (q);
  313.             return;
  314.         }
  315.     }
  316.     printf ("Cannot remove '%s': No such directory!\n", dir_name);
  317. }
  318.  
  319. void find (dir_node *crt, int depth, int min_size, int max_size, char *subcontent)
  320. {
  321.     if (depth >= 0)
  322.     {
  323.         file_node *p = malloc (sizeof (file_node));
  324.         dir_node *q = malloc (sizeof (dir_node));
  325.         for (p = crt->d->file_list; p != NULL; p = p->next)
  326.             if (min_size <= p->f->size && p->f->size <= max_size && strstr (p->f->data, subcontent))
  327.                 printf ("%s ", p->f->name);
  328.         for (q = crt->d->dir_list; q != NULL; q = q->next)
  329.             find (q, depth - 1, min_size, max_size, subcontent);
  330.     }
  331. }
  332.  
  333. int check (char *line)
  334. {
  335.     if (same (line, "create fs\n"))
  336.         return 1;
  337.     if (same (line, "delete fs\n"))
  338.         return 2;
  339.     if (same (line, "ls\n"))
  340.         return 3;
  341.     if (same (line, "pwd\n"))
  342.         return 4;
  343.     if (same (line, "tree\n"))
  344.         return 5;
  345.     if (strstr (line, "touch"))
  346.         return 6;
  347.     if (strstr (line, "mkdir"))
  348.         return 7;
  349.     if (strstr (line, "cd"))
  350.         return 8;
  351.     if (strstr (line, "rmdir"))
  352.         return 9;
  353.     if (strstr (line, "rm"))
  354.         return 10;
  355.     if (strstr (line, "find"))
  356.         return 11;
  357.     return 0;
  358. }
  359.  
  360. int main()
  361. {
  362.     dir_node *root = NULL;
  363.     dir_node *crt = NULL;
  364.     char useless[5], file_name[30], file_data[30], directory_name[30];
  365.     int  max_depth, min_size, max_size;
  366.     while (1)
  367.     {
  368.         char line[100];
  369.         //
  370.         //printf("$ ");
  371.         fgets (line, 100, stdin);
  372.         //
  373.         int caz = check (line);
  374.         //
  375.         switch (caz)
  376.         {
  377.         case 1:
  378.             create_fs (&root);
  379.             crt = root;
  380.             break;
  381.         case 2:
  382.             delete_fs (&root);
  383.             return 0;
  384.         case 3:
  385.             ls (crt);
  386.             break;
  387.         case 4:
  388.             pwd (crt);
  389.             break;
  390.         case 5:
  391.             tree (crt);
  392.             break;
  393.         case 6:
  394.             sscanf (line, "%s %s %s", useless, file_name, file_data);
  395.             touch (crt, file_name, file_data);
  396.             break;
  397.         case 7:
  398.             sscanf (line, "%s %s", useless, directory_name);
  399.             mkdir (crt, directory_name);
  400.             break;
  401.         case 8:
  402.             sscanf (line, "%s %s", useless, directory_name);
  403.             crt = cd (crt, directory_name);
  404.             break;
  405.         case 9:
  406.             sscanf (line, "%s %s", useless, directory_name);
  407.             rmdir (crt, directory_name, false);
  408.             break;
  409.         case 10:
  410.             sscanf (line, "%s %s", useless, file_name);
  411.             rm (crt, file_name);
  412.             break;
  413.         case 11:
  414.             sscanf (line, "%s %d %d %d %s", useless, &max_depth, &min_size, &max_size, file_data);
  415.             find (crt, max_depth, min_size, max_size, file_data);
  416.             printf ("\n");
  417.             break;
  418.         default:
  419.             break;
  420.         }
  421.     }
  422.     return 0;
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement