Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include "file.h"
- #include "headers.h"
- int same (char *string1, char *string2)
- {
- if (!strcmp (string1, string2))
- return 1;
- return 0;
- }
- void create_fs (dir_node **root)
- {
- (*root) = malloc (sizeof (dir_node));
- (*root)->d = malloc (sizeof (Directory));
- (*root)->d->name = strdup ("/");
- (*root)->d->file_list = NULL;
- (*root)->d->dir_list = NULL;
- (*root)->d->parentDir = NULL;
- (*root)->next = NULL;
- (*root)->prev = NULL;
- }
- void delete_fs (dir_node **root)
- {
- //Caution: use only if root directory is empty
- free ( (*root)->d->name);
- free ( (*root)->d);
- free ( (*root));
- }
- void touch (dir_node *crt, char *file_name, char *file_data)
- {
- //add file
- //data initialization
- file_node *new_node;
- new_node = malloc (sizeof (file_node));
- new_node->f = malloc (sizeof (File));
- //
- new_node->f->name = strdup (file_name);
- new_node->f->data = strdup (file_data);
- //
- new_node->f->size = strlen (file_data);
- //placing the file in an ordered list
- if (crt->d->file_list == NULL)
- {
- //empty list
- new_node->next = NULL;
- crt->d->file_list = new_node;
- return;
- }
- if (strcmp (crt->d->file_list->f->name, new_node->f->name) > 0)
- {
- //first element
- //there is at least one file in the list
- new_node->next = crt->d->file_list;
- crt->d->file_list = new_node;
- return;
- }
- file_node *p;
- for (p = crt->d->file_list; p->next != NULL; p = p->next)
- if (strcmp (p->f->name, new_node->f->name) < 0
- && strcmp (new_node->f->name, p->next->f->name) < 0)
- {
- new_node->next = p->next;
- p->next = new_node;
- return;
- }
- //in the last case, new_node->f is the last file
- new_node->next = NULL;
- p->next = new_node;
- }
- void mkdir (dir_node *crt, char *directory_name)
- {
- dir_node *new_node;
- new_node = malloc (sizeof (dir_node));
- new_node-> d = malloc (sizeof (Directory));
- new_node->d->name = strdup (directory_name);
- new_node->d->file_list = NULL;
- new_node->d->dir_list = NULL;
- new_node->d->parentDir = crt->d;
- new_node->prev = crt;
- //placing the directory in an ordered list
- if (crt->d->dir_list == NULL)
- {
- new_node->next = NULL;
- crt->d->dir_list = new_node;
- return;
- }
- if (strcmp (crt->d->dir_list->d->name, new_node->d->name) > 0)
- {
- //there is at least one directory in the list
- new_node->next = crt->d->dir_list;
- crt->d->dir_list = new_node;
- return;
- }
- dir_node *p;
- for (p = crt->d->dir_list; p->next != NULL; p = p->next)
- if (strcmp (p->d->name, new_node->d->name) < 0
- && strcmp (new_node->d->name, p->next->d->name) < 0)
- {
- new_node->next = p->next;
- p->next = new_node;
- return;
- }
- //in the last case, new_file is the last file
- new_node->next = NULL;
- p->next = new_node;
- }
- void ls (dir_node *crt)
- {
- file_node *p = malloc (sizeof (file_node));
- dir_node *q = malloc (sizeof (dir_node));
- //
- for (p = crt->d->file_list; p != NULL; p = p->next)
- printf ("%s ", p->f->name);
- //
- for (q = crt->d->dir_list; q != NULL; q = q->next)
- printf ("%s ", q->d->name);
- printf ("\n");
- }
- dir_node* cd (dir_node *crt, char *directory_name)
- {
- if (same (directory_name, ".."))
- return crt->prev;
- if (same (directory_name, "."))
- return crt;
- //search for directory_name in dir_list
- dir_node *p;
- for (p = crt->d->dir_list; p != NULL; p = p->next)
- if (same (directory_name, p->d->name))
- return p;
- printf ("Cannot move to '%s': No such directory!\n", directory_name);
- return crt;
- }
- void print_parents (dir_node *crt, bool root)
- {
- //root
- if (root)
- {
- printf ("/");
- return;
- }
- //not-root
- if (crt->prev == NULL)
- return;
- print_parents (crt->prev, false);
- printf ("/%s", crt->d->name);
- }
- void pwd (dir_node *crt)
- {
- if (same (crt->d->name, "/"))
- print_parents (crt, true);
- else
- print_parents (crt, false);
- printf ("\n");
- }
- void print_tabs (int tabs)
- {
- int i;
- for (i = 0; i < tabs; ++i)
- printf (" ");
- }
- void tree_recursive (dir_node *crt, int tabs)
- {
- file_node *p = malloc (sizeof (file_node));
- dir_node *q = malloc (sizeof (dir_node));
- for (p = crt->d->file_list; p != NULL; p = p->next)
- {
- print_tabs (tabs);
- printf ("%s\n", p->f->name);
- }
- q = crt->d->dir_list;
- if (q != NULL)
- {
- print_tabs (tabs);
- printf ("%s\n", q->d->name);
- tree_recursive (q, tabs + 1);
- for (q = q->next; q != NULL; q = q->next)
- {
- print_tabs (tabs);
- printf ("%s\n", q->d->name);
- tree_recursive (q, tabs + 1);
- }
- }
- }
- void tree (dir_node *crt)
- {
- printf ("%s\n", crt->d->name);
- tree_recursive (crt, 1);
- }
- int rm (dir_node *crt, char *file_name)
- {
- file_node *p;
- p = crt->d->file_list;
- if (p == NULL)
- {
- //empty list
- printf ("Cannot remove '%s': No such file!\n", file_name);
- return 0;
- }
- if (same (p->f->name, file_name))
- {
- //first element is the one
- if (p->next == NULL)
- {
- //there is only one element in the list
- crt->d->file_list = NULL;
- free (p->f->name);
- free (p->f->data);
- free (p->f);
- free (p);
- return 0;
- }
- //there are at least 2 elements
- crt->d->file_list = p->next;
- free (p->f->name);
- free (p->f->data);
- free (p->f);
- free (p);
- return 1;
- }
- //the searched file is not the first element
- for (; p->next != NULL; p = p->next)
- {
- if (same (p->next->f->name, file_name))
- {
- file_node *q = malloc (sizeof (file_node));
- q = p->next;
- p->next = p->next->next;
- free (q->f->name);
- free (q->f->data);
- free (q->f);
- free (q);
- return 1;
- }
- }
- printf ("Cannot remove '%s': No such file!\n", file_name);
- return 0;
- }
- void delete_dir (dir_node *crt)
- {
- file_node *p, *p_next;
- dir_node *q, *q_next;
- for (p = crt->d->file_list; p != NULL;)
- {
- p_next = p->next;
- if (rm (crt, p->f->name) == 0)
- break;
- p = p_next;
- }
- for (q = crt->d->dir_list; q != NULL;)
- {
- q_next = q->next;
- rmdir (crt, q->d->name, true);
- q = q_next;
- }
- free (crt->d->name);
- free (crt->d);
- free (crt);
- }
- void rmdir (dir_node *crt, char *dir_name, bool rec)
- {
- dir_node *p;
- p = crt->d->dir_list;
- if (p == NULL)
- {
- //no subdirectories
- if (!rec)
- printf ("Cannot remove '%s': No such directory!\n", dir_name);
- else
- delete_dir (crt);
- return;
- }
- if (same (p->d->name, dir_name))
- {
- //first element is the one
- if (p->next == NULL)
- {
- //there is only one element in the list
- crt->d->dir_list = NULL;
- delete_dir (p);
- return;
- }
- //there are at least 2 elements
- crt->d->dir_list = p->next;
- delete_dir (p);
- return;
- }
- //the searched file is not the first element
- for (; p->next != NULL; p = p->next)
- {
- if (same (p->next->d->name, dir_name))
- {
- dir_node *q = malloc (sizeof (dir_node));
- q = p->next;
- p->next = p->next->next;
- delete_dir (q);
- return;
- }
- }
- printf ("Cannot remove '%s': No such directory!\n", dir_name);
- }
- void find (dir_node *crt, int depth, int min_size, int max_size, char *subcontent)
- {
- if (depth >= 0)
- {
- file_node *p = malloc (sizeof (file_node));
- dir_node *q = malloc (sizeof (dir_node));
- for (p = crt->d->file_list; p != NULL; p = p->next)
- if (min_size <= p->f->size && p->f->size <= max_size && strstr (p->f->data, subcontent))
- printf ("%s ", p->f->name);
- for (q = crt->d->dir_list; q != NULL; q = q->next)
- find (q, depth - 1, min_size, max_size, subcontent);
- }
- }
- int check (char *line)
- {
- if (same (line, "create fs\n"))
- return 1;
- if (same (line, "delete fs\n"))
- return 2;
- if (same (line, "ls\n"))
- return 3;
- if (same (line, "pwd\n"))
- return 4;
- if (same (line, "tree\n"))
- return 5;
- if (strstr (line, "touch"))
- return 6;
- if (strstr (line, "mkdir"))
- return 7;
- if (strstr (line, "cd"))
- return 8;
- if (strstr (line, "rmdir"))
- return 9;
- if (strstr (line, "rm"))
- return 10;
- if (strstr (line, "find"))
- return 11;
- return 0;
- }
- int main()
- {
- dir_node *root = NULL;
- dir_node *crt = NULL;
- char useless[5], file_name[30], file_data[30], directory_name[30];
- int max_depth, min_size, max_size;
- while (1)
- {
- char line[100];
- //
- //printf("$ ");
- fgets (line, 100, stdin);
- //
- int caz = check (line);
- //
- switch (caz)
- {
- case 1:
- create_fs (&root);
- crt = root;
- break;
- case 2:
- delete_fs (&root);
- return 0;
- case 3:
- ls (crt);
- break;
- case 4:
- pwd (crt);
- break;
- case 5:
- tree (crt);
- break;
- case 6:
- sscanf (line, "%s %s %s", useless, file_name, file_data);
- touch (crt, file_name, file_data);
- break;
- case 7:
- sscanf (line, "%s %s", useless, directory_name);
- mkdir (crt, directory_name);
- break;
- case 8:
- sscanf (line, "%s %s", useless, directory_name);
- crt = cd (crt, directory_name);
- break;
- case 9:
- sscanf (line, "%s %s", useless, directory_name);
- rmdir (crt, directory_name, false);
- break;
- case 10:
- sscanf (line, "%s %s", useless, file_name);
- rm (crt, file_name);
- break;
- case 11:
- sscanf (line, "%s %d %d %d %s", useless, &max_depth, &min_size, &max_size, file_data);
- find (crt, max_depth, min_size, max_size, file_data);
- printf ("\n");
- break;
- default:
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement