Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 100
- //structura pentru trie
- typedef struct Trie
- {
- struct Trie *children[123];
- int wordEnd;
- }TrieNode;
- //aceasta functie converteste o cale din relativa in absoluta
- char *convertToAbsolute(char currentFile[], char currentDirectory[])
- {
- int i;
- char a[MAX];
- strcpy(a, currentFile);
- while (1)
- {
- int check = 0;
- while (currentFile[0] == '.' && currentFile[1] != '.')
- {
- currentFile = currentFile + 2;
- check = 1;
- }
- while (currentFile[0] == '.' && currentFile[1] == '.')
- {
- if (strlen(currentDirectory) == 1)
- {
- fprintf(stderr, "%s: No such file or directory\n", a);
- return "/";
- }
- char *aux = strrchr(currentDirectory, '/');
- char auxDir[100];
- for (i = 0; i < (int)(strlen(currentDirectory) - strlen(aux)); i++)
- {
- auxDir[i] = currentDirectory[i];
- }
- auxDir[i] = '\0';
- currentFile = currentFile + 3;
- currentDirectory = auxDir;
- check = 1;
- }
- if (check == 0)
- {
- break;
- }
- }
- char *auxFile = malloc(MAX);
- if(auxFile == NULL)
- {
- exit(-1);
- }
- if (strlen(currentDirectory) != 1)
- {
- strcpy(auxFile, currentDirectory);
- strcat(auxFile, "/");
- }
- else
- {
- strcpy(auxFile, "/");
- }
- strcat(auxFile, currentFile);
- currentFile = auxFile;
- return currentFile;
- }
- //aceasta functie creaza un nod pentru trie si ii initializeaza copiii cu NULL
- TrieNode* createNode()
- {
- TrieNode *pNode = NULL;
- pNode = malloc(sizeof(TrieNode));
- if (pNode != NULL)
- {
- int i;
- pNode->wordEnd = 0;
- for (i = 0; i < 123; i++)
- {
- pNode->children[i] = NULL;
- }
- }
- else
- {
- exit(-1);
- }
- return pNode;
- }
- //aceasta functie insereaza un cuvant in trie
- void insert(TrieNode *root, const char file[])
- {
- int level;
- int length = strlen(file);
- int index;
- TrieNode *pCrawl = root;
- for (level = 0; level < length; level++)
- {
- index = (int)(file[level]);
- if (pCrawl->children[index] == NULL)
- {
- pCrawl->children[index] = createNode();
- }
- pCrawl = pCrawl->children[index];
- }
- pCrawl->wordEnd = 1;
- }
- /*aceasta functie cauta un cuvant in trie si daca il gaseste returneaza 1,
- altfel 0*/
- int search(TrieNode *root, const char file[])
- {
- int level;
- int length = strlen(file);
- int index;
- TrieNode *pCrawl = root;
- for (level = 0; level < length; level++)
- {
- index = (int)(file[level]);
- if (pCrawl->children[index] == NULL)
- {
- return 0;
- }
- pCrawl = pCrawl->children[index];
- }
- if (pCrawl != NULL && pCrawl->wordEnd == 1)
- {
- return 1;
- }
- return 0;
- }
- /*aceasta functie cauta un cuvant in trie, iar daca il gaseste returneaza
- un pointer la nod*/
- TrieNode *find(TrieNode *root, const char file[])
- {
- int level;
- int length = strlen(file);
- int index;
- TrieNode *pCrawl = root;
- for (level = 0; level < length; level++)
- {
- index = (int)(file[level]);
- if (pCrawl->children[index] == NULL)
- {
- return NULL;
- }
- pCrawl = pCrawl->children[index];
- }
- if (pCrawl != NULL && pCrawl->wordEnd == 1)
- {
- return pCrawl;
- }
- return NULL;
- }
- //printeaza toate cuvintele copii ale acestui nod care contin un singur /
- void ls(TrieNode* root, char str[], int level, int type)
- {
- if (root->wordEnd == 1)
- {
- str[level] = '\0';
- if (str[0] != '\0')
- {
- char *p;
- char aux[MAX];
- strcpy(aux, str);
- aux[strlen(aux) - 1] = '\0';
- p = strchr(aux, '/');
- if (p == NULL)
- {
- if (type == 0)
- {
- printf("%s ", aux);
- }
- else
- {
- printf("%s ", str);
- }
- }
- }
- }
- int i;
- for (i = 0; i < 123; i++)
- {
- if (root->children[i])
- {
- str[level] = i;
- ls(root->children[i], str, level + 1, type);
- }
- }
- }
- //verifica daca un nod are copiii nuli
- int isEmpty(TrieNode* root)
- {
- for (int i = 0; i < 123; i++)
- {
- if (root->children[i] != NULL)
- {
- return 0;
- }
- }
- return 1;
- }
- //sterge toti copiii unui nod
- TrieNode* rm(TrieNode* root, char* file, int depth)
- {
- if (root == NULL)
- {
- return NULL;
- }
- if (depth == (int)strlen(file))
- {
- if (root->wordEnd == 1)
- {
- root->wordEnd = 0;
- }
- if (isEmpty(root))
- {
- free(root);
- root = NULL;
- }
- return root;
- }
- int index = (int)(file[depth]);
- root->children[index] = rm(root->children[index], file, depth + 1);
- if (isEmpty(root) && root->wordEnd == 0)
- {
- free(root);
- root = NULL;
- }
- return root;
- }
- //aceasta este functia de baza care citeste comenzile si le executa
- void execute()
- {
- TrieNode *root = createNode();
- char *currentDirectory = malloc(MAX);
- strcpy(currentDirectory, "/");
- insert(root, "/");
- char a[MAX], b[MAX], *p, *p2;
- int z, n;
- insert(root, "/");
- //citim numarul de comenzi
- if(fgets(a, sizeof(a), stdin) == NULL)
- {
- exit(-1);
- }
- n = atoi(a);
- TrieNode *auxTrie = malloc(sizeof(TrieNode));
- for(z = 0; z < n; z ++)
- {
- if(fgets(a, sizeof(a), stdin) == NULL)
- {
- return;
- }
- strcpy(b, a);
- p = strtok(a, " \n");
- //testam
- if (strcmp(p, "pwd") == 0)
- {
- printf("%s\n", currentDirectory);
- p = strtok(NULL, " \n");
- }
- else if (strcmp(p, "mkdir") == 0)
- {
- int i = 0;
- while (p != NULL)
- {
- i++;
- p = strtok(NULL, " \n");
- if (p != NULL)
- {
- char path[MAX];
- strcpy(path, p);
- if (p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else if (p[0] == '/')
- {
- p = convertToAbsolute(p, "\0");
- p = p + 1;
- }
- else
- {
- strcpy(b, "./");
- strcat(b, p);
- p = b;
- p = convertToAbsolute(p, currentDirectory);
- }
- char *p4 = strrchr(p, '/');
- p = strcat(p, "/");
- char y[MAX];
- strcpy(y, p);
- char *p5 = p;
- p5[strlen(p) - strlen(p4) + 1] = '\0';
- if(search(root, p5) == 0)
- {
- fprintf(stderr, "%s: No such file or directory\n",
- path);
- }
- else
- {
- if(search(root, y) == 0)
- {
- insert(root, y);
- }
- else
- {
- fprintf(stderr, "%s: already exists\n", path);
- }
- }
- }
- }
- if (i < 2)
- {
- fprintf(stderr, "mkdir: missing operand\n");
- }
- }
- else if (strcmp(p, "touch") == 0)
- {
- int i = 0;
- while (p != NULL)
- {
- i++;
- p = strtok(NULL, " \n");
- if (p != NULL)
- {
- char path[MAX];
- strcpy(path, p);
- if (p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else if (p[0] == '/')
- {
- p = convertToAbsolute(p, "\0");
- p = p + 1;
- }
- else
- {
- strcpy(b, "./");
- strcat(b, p);
- p = b;
- p = convertToAbsolute(p, currentDirectory);
- }
- char *p4 = strrchr(p, '/');
- p = strcat(p, "*");
- char y[MAX];
- strcpy(y, p);
- char *p5 = p;
- p5[strlen(p) - strlen(p4) + 1] = '\0';
- if(search(root, p5) == 0)
- {
- fprintf(stderr, "%s: No such file or directory\n",
- path);
- }
- else
- {
- if(search(root, y) == 0)
- {
- insert(root, y);
- }
- else
- {
- fprintf(stderr, "%s: already exists\n", path);
- }
- }
- }
- }
- if (i < 2)
- {
- fprintf(stderr, "touch: missing operand\n");
- }
- }
- else if (strcmp(p, "ls") == 0)
- {
- char y[MAX];
- strcpy(y, b);
- y[strlen(y) - 1] = '\0';
- if (strlen(currentDirectory) != 1)
- {
- strcat(currentDirectory, "/");
- }
- p = strtok(NULL, " \n");
- if (p == NULL)
- {
- auxTrie = find(root, currentDirectory);
- ls(auxTrie, b, 0, 0);
- printf("\n");
- if(strlen(currentDirectory) != 1)
- {
- currentDirectory[strlen(currentDirectory) - 1] = '\0';
- }
- continue;
- }
- else if (strcmp(p, "-F") == 0)
- {
- p = strtok(NULL, " \n");
- if (p == NULL)
- {
- auxTrie = find(root, currentDirectory);
- ls(auxTrie, b, 0, 1);
- printf("\n");
- }
- else
- {
- char path[MAX];
- strcpy(path, p);
- if(strlen(p) <= 2 && p[0] == '.')
- {
- currentDirectory[strlen(currentDirectory) - 1]
- = '\0';
- p = convertToAbsolute(p, currentDirectory);
- if(strlen(p) > 1)
- {
- while(p[strlen(p) - 1] == '/')
- {
- p[strlen(p) - 1] = '\0';
- }
- }
- strcat(currentDirectory, "/");
- }
- else
- {
- strcpy(b, "./");
- strcat(b, p);
- p = b;
- p = convertToAbsolute(p, currentDirectory);
- }
- p = strcat(p, "/");
- char aux[MAX];
- strcpy(aux, p);
- auxTrie = find(root, p);
- p2 = strtok(NULL, " \n");
- if (p2 != NULL)
- {
- fprintf(stderr, "%s: too many arguments\n", y);
- if(strlen(currentDirectory) != 1)
- {
- currentDirectory[strlen(currentDirectory) - 1]
- = '\0';
- }
- continue;
- }
- if (auxTrie == NULL)
- {
- fprintf(stderr, "%s: No such file or directory\n",
- path);
- if(strlen(currentDirectory) != 1)
- {
- currentDirectory[strlen(currentDirectory) - 1]
- = '\0';
- }
- continue;
- }
- else
- {
- p2 = strtok(NULL, " \n");
- if (p2 != NULL)
- {
- fprintf(stderr, "%s: too many arguments\n", y);
- if(strlen(currentDirectory) != 1)
- {
- currentDirectory[strlen(currentDirectory)
- - 1] = '\0';
- }
- continue;
- }
- ls(auxTrie, b, 0, 1);
- printf("\n");
- }
- }
- }
- else
- {
- char path[MAX];
- strcpy(path, p);
- if(strlen(p) < 3 && p[0] == '.')
- {
- currentDirectory[strlen(currentDirectory) - 1] = '\0';
- p = convertToAbsolute(p, currentDirectory);
- if(strlen(p) > 1)
- {
- p[strlen(p) - 1] = '\0';
- }
- strcat(currentDirectory, "/");
- }
- else if (p[0] != '/')
- {
- strcpy(b, "./");
- strcat(b, p);
- p = b;
- p = convertToAbsolute(p, currentDirectory);
- }
- if (strlen(p) != 1)
- {
- p = strcat(p, "/");
- }
- auxTrie = find(root, p);
- if (auxTrie == NULL)
- {
- fprintf(stderr, "%s: No such file or directory\n",
- path);
- if(strlen(currentDirectory) != 1)
- {
- currentDirectory[strlen(currentDirectory) - 1]
- = '\0';
- }
- continue;
- }
- else
- {
- p2 = strtok(NULL, " \n");
- if (p2 != NULL)
- {
- fprintf(stderr, "%s: too many arguments\n", y);
- if(strlen(currentDirectory) != 1)
- {
- currentDirectory[strlen(currentDirectory) - 1]
- = '\0';
- }
- break;
- }
- ls(auxTrie, b, 0, 0);
- printf("\n");
- }
- }
- if(strlen(currentDirectory) != 1)
- {
- currentDirectory[strlen(currentDirectory) - 1] = '\0';
- }
- }
- else if (strcmp(p, "cp") == 0)
- {
- p = strtok(NULL, " \n");
- p2 = strrchr(b, ' ');
- if (p2 == NULL)
- {
- fprintf(stderr, "cp: missing operand\n");
- continue;
- }
- char path[MAX];
- p2 = p2 + 1;
- p2[strlen(p2) - 1] = '\0';
- strcpy(path, p2);
- if (strcmp(p, p2) == 0)
- {
- fprintf(stderr, "cp: missing operand\n");
- continue;
- }
- char c[MAX];
- strcpy(c, p2);
- if (p2[0] == '.')
- {
- p2 = convertToAbsolute(p2, currentDirectory);
- }
- else if (p2[0] != '/' && p2[0] != '.')
- {
- strcpy(b, "./");
- strcat(b, p2);
- p2 = b;
- p2 = convertToAbsolute(p2, currentDirectory);
- }
- p2 = strcat(p2, "/");
- if (search(root, p2) == 0)
- {
- fprintf(stderr, "%s: No such file or directory\n", path);
- continue;
- }
- int i = 0;
- while (strcmp(p, c) != 0)
- {
- char path[MAX];
- strcpy(path, p);
- i++;
- if (p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else if (p[0] != '/' && p[0] != '.')
- {
- strcpy(b, "./");
- strcat(b, p);
- p = b;
- p = convertToAbsolute(p, currentDirectory);
- }
- if (strlen(p) != 1)
- {
- p = strcat(p, "*");
- }
- if (search(root, p) == 0)
- {
- fprintf(stderr, "%s: No such file or directory\n",
- path);
- }
- else
- {
- p = p + 1;
- strcat(p2, p);
- insert(root, p2);
- }
- p = strtok(NULL, " \n");
- }
- if (i < 1)
- {
- fprintf(stderr, "cp: missing operand\n");
- }
- }
- else if (strcmp(p, "cd") == 0)
- {
- p = strtok(NULL, " \n");
- if (p == NULL)
- {
- strcpy(currentDirectory,"/");
- continue;
- }
- else
- {
- char path[MAX];
- strcpy(path, p);
- if(p[0] == '.' && strlen(p) == 2)
- {
- p = convertToAbsolute(p, currentDirectory);
- if(strlen(p) > 1)
- {
- p[strlen(p) - 1] = '\0';
- }
- }
- else if (p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else if (p[0] != '/' && p[0] != '.')
- {
- strcpy(b, "./");
- strcat(b, p);
- p = b;
- p = convertToAbsolute(p, currentDirectory);
- }
- if (strlen(p) != 1)
- {
- int j = strlen(p) + 1;
- p[strlen(p)] = '/';
- p[j] = '\0';
- }
- char y[MAX];
- strcpy(y, p);
- y[strlen(y) - 1] = '*';
- if (search(root, y) == 1)
- {
- fprintf(stderr, "%s: Not a directory\n", path);
- }
- else if (search(root, p) == 0)
- {
- fprintf(stderr, "%s: No such file or directory\n",
- path);
- }
- else
- {
- if (strlen(p) != 1)
- {
- p[strlen(p) - 1] = '\0';
- }
- if(strcmp(currentDirectory, p) != 0)
- {
- strcpy(currentDirectory, p);
- }
- }
- }
- }
- else if (strcmp(p, "rmdir") == 0)
- {
- p = strtok(NULL, " \n");
- if (p == NULL)
- {
- fprintf(stderr, "rmdir: missing operand\n");
- continue;
- }
- while (p != NULL)
- {
- char path[MAX];
- strcpy(path, p);
- if (p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else if (p[0] != '/' && p[0] != '.')
- {
- strcpy(b, "./");
- strcat(b, p);
- p = b;
- p = convertToAbsolute(p, currentDirectory);
- }
- if (strlen(p) != 1)
- {
- p = strcat(p, "/");
- }
- if (search(root, p) == 0)
- {
- fprintf(stderr, "%s: No such file or directory\n",
- path);
- }
- else
- {
- if (isEmpty(find(root, p)) == 1)
- {
- root = rm(root, p, 0);
- }
- else
- {
- fprintf(stderr, "%s: Directory not empty\n",
- path);
- }
- }
- p = strtok(NULL, " \n");
- }
- }
- else if (strcmp(p, "rm") == 0)
- {
- p = strtok(NULL, " \n");
- if (p == NULL)
- {
- fprintf(stderr, "rm: missing operand\n");
- continue;
- }
- while (p != NULL)
- {
- char path[MAX];
- strcpy(path, p);
- if (p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else if (p[0] != '/' && p[0] != '.')
- {
- strcpy(b, "./");
- strcat(b, p);
- p = b;
- p = convertToAbsolute(p, currentDirectory);
- }
- if (strlen(p) != 1)
- {
- p = strcat(p, "/");
- }
- char y[MAX];
- strcpy(y, p);
- y[strlen(y) - 1] = '*';
- if (search(root, p) == 1)
- {
- root = rm(root, p, 0);
- }
- else if (search(root, y) == 1)
- {
- root = rm(root, y, 0);
- }
- else
- {
- fprintf(stderr, "%s: No such file or directory\n",
- path);
- }
- p = strtok(NULL, " \n");
- }
- }
- else if (strcmp(p, "mv") == 0)
- {
- p = strtok(NULL, " \n");
- p2 = strrchr(b, ' ');
- if (p2 == NULL)
- {
- fprintf(stderr, "%s: missing operand\n", b);
- continue;
- }
- p2 = p2 + 1;
- p2[strlen(p2) - 1] = '\0';
- char path[MAX], path2[MAX];
- strcpy(path, p);
- strcpy(path2, p2);
- if (strcmp(p, p2) == 0)
- {
- fprintf(stderr, "%s: missing operand\n", b);
- continue;
- }
- char c[MAX];
- strcpy(c, p2);
- if (p2[0] == '.')
- {
- p2 = convertToAbsolute(p2, currentDirectory);
- }
- else if (p2[0] != '/' && p2[0] != '.')
- {
- strcpy(b, "./");
- strcat(b, p2);
- p2 = b;
- p2 = convertToAbsolute(p2, currentDirectory);
- }
- p2 = strcat(p2, "/");
- char y[MAX];
- strcpy(y, p2);
- y[strlen(y) - 1] = '*';
- if (search(root, y) == 1)
- {
- fprintf(stderr, "%s: Not a directory\n", path2);
- continue;
- }
- int i = 0;
- while (strcmp(p, c) != 0)
- {
- char path[MAX];
- strcpy(path, p);
- i++;
- if (p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else if (p[0] != '/' && p[0] != '.')
- {
- strcpy(b, "./");
- strcat(b, p);
- p = b;
- p = convertToAbsolute(p, currentDirectory);
- }
- if (strlen(p) != 1)
- {
- p = strcat(p, "*");
- }
- if (search(root, p) == 0)
- {
- fprintf(stderr, "%s: No such file or directory\n",
- path);
- break;
- }
- else if (search(root, p2) == 0)
- {
- fprintf(stderr, "%s: No such file or directory\n",
- path2);
- break;
- }
- else
- {
- root = rm(root, p, 0);
- p = p + 1;
- strcat(p2, p);
- insert(root, p2);
- }
- p = strtok(NULL, " \n");
- }
- if (i < 1)
- {
- fprintf(stderr, "mv: missing operand\n");
- }
- }
- else
- {
- fprintf(stderr, "%s: command not found\n", p);
- }
- }
- rm(root, "/", 0);
- free(currentDirectory);
- free(root);
- }
- int main()
- {
- execute();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement