Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define ALPHABET_SIZE (27)
- char *convertToAbsolute(char currentFile[], char currentDirectory[])
- {
- int i;
- while (1)
- {
- int check = 0;
- while (currentFile[0] == '.' && currentFile[1] != '.')
- {
- currentFile = currentFile + 2;
- check = 1;
- }
- while (currentFile[0] == '.' && currentFile[1] == '.')
- {
- char *aux = strrchr(currentDirectory, '/');
- char *auxDir = malloc(sizeof(char) * strlen(currentDirectory) + 1 - sizeof(char) * strlen(aux));
- for (i = 0; i < 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(sizeof(char) * strlen(currentFile) + sizeof(char) * strlen(currentDirectory) + 1);
- if (strlen(currentDirectory) != 1)
- {
- strcpy(auxFile, currentDirectory);
- strcat(auxFile, "/");
- }
- else
- {
- strcpy(auxFile, "/");
- }
- strcat(auxFile, currentFile);
- currentFile = auxFile;
- return currentFile;
- }
- int convertToIndex(int currentLetter)
- {
- if (currentLetter - (int)'/' == 0)
- {
- return 0;
- }
- return currentLetter - (int)'a' + 1;
- }
- typedef struct Trie
- {
- struct Trie *children[123];
- int wordEnd;
- }TrieNode;
- 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;
- }
- 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;
- }
- 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;
- }
- 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;
- }
- 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[100];
- strcpy(aux, str);
- aux[strlen(aux) - 1] = '\0';
- p = strchr(aux, '/');
- if (p == NULL)
- {
- if (type == 0)
- {
- printf("%s\n", aux);
- }
- else
- {
- printf("%s\n", str);
- }
- }
- }
- }
- int i;
- for (i = 0; i < 123; i++)
- {
- if (root->children[i])
- {
- str[level] = i;
- ls(root->children[i], str, level + 1, type);
- }
- }
- }
- int isEmpty(TrieNode* root)
- {
- for (int i = 0; i < 123; i++)
- {
- if (root->children[i] != NULL)
- {
- return 0;
- }
- }
- return 1;
- }
- TrieNode* rm(TrieNode* root, char* file, int depth)
- {
- if (root == NULL)
- {
- return NULL;
- }
- if (depth == 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;
- }
- int main()
- {
- int i;
- TrieNode *root = createNode();
- char *currentDirectory = malloc(1000);
- strcpy(currentDirectory, "/");
- insert(root, "/");
- char currentFile[100];
- char a[100], b[100], c[100], *p, *p2;
- insert(root, "/");
- while (fgets(a, sizeof(a), stdin) != NULL)
- {
- strcpy(b, a);
- p = strtok(a, " \n");
- 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)
- {
- if (p[0] != '/' && p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else
- {
- strcpy(b, "./");
- strcat(b, p);
- p = b;
- p = convertToAbsolute(p, currentDirectory);
- }
- p = strcat(p, "/");
- insert(root, p);
- }
- }
- 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)
- {
- p = convertToAbsolute(p, currentDirectory);
- strcat(p, "*");
- insert(root, p);
- }
- }
- if (i < 2)
- {
- fprintf(stderr, "mkdir: missing operand\n");
- }
- }
- else if (strcmp(p, "ls") == 0)
- {
- if (strlen(currentDirectory) != 1)
- {
- currentDirectory = strcat(currentDirectory, "/");
- }
- p = strtok(NULL, " \n");
- if (p == NULL)
- {
- p = find(root, currentDirectory);
- ls(p, b, 0, 0);
- currentDirectory[strlen(currentDirectory) - 1] = '\0';
- continue;
- }
- else if (strcmp(p, "-F") == 0)
- {
- p = strtok(NULL, " \n");
- if (p == NULL)
- {
- p = find(root, currentDirectory);
- ls(p, b, 0, 1);
- }
- else
- {
- if (p[0] != '/' && p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else
- {
- strcpy(b, "./");
- strcat(b, p);
- p = b;
- p = convertToAbsolute(p, currentDirectory);
- }
- p = strcat(p, "/");
- p = find(root, p);
- if (p == NULL)
- {
- fprintf(stderr, "PATH: No such file or directory\n");
- currentDirectory[strlen(currentDirectory) - 1] = '\0';
- break;
- }
- else
- {
- p2 = strtok(NULL, " \n");
- if (p2 != NULL)
- {
- fprintf(stderr, "ls: too many arguments\n");
- currentDirectory[strlen(currentDirectory) - 1] = '\0';
- break;
- }
- ls(p, b, 0, 1);
- }
- }
- }
- else
- {
- if (p[0] != '/' && p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else
- {
- strcpy(b, "./");
- strcat(b, p);
- p = b;
- p = convertToAbsolute(p, currentDirectory);
- }
- if (strlen(p) != 1)
- {
- p = strcat(p, "/");
- }
- p = find(root, p);
- if (p == NULL)
- {
- fprintf(stderr, "PATH: No such file or directory\n");
- currentDirectory[strlen(currentDirectory) - 1] = '\0';
- break;
- }
- else
- {
- p2 = strtok(NULL, " \n");
- if (p2 != NULL)
- {
- fprintf(stderr, "ls: too many arguments\n");
- currentDirectory[strlen(currentDirectory) - 1] = '\0';
- break;
- }
- ls(p, b, 0, 0);
- }
- }
- 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");
- break;
- }
- p2 = p2 + 1;
- p2[strlen(p2) - 1] = '\0';
- if (strcmp(p, p2) == 0)
- {
- fprintf(stderr, "cp: missing operand\n");
- break;
- }
- char c[100];
- strcpy(c, p2);
- if (p2[0] != '/' && p2[0] == '.')
- {
- p2 = convertToAbsolute(p2, currentDirectory);
- }
- else
- {
- strcpy(b, "./");
- strcat(b, p2);
- p2 = b;
- p2 = convertToAbsolute(p2, currentDirectory);
- }
- p2 = strcat(p2, "/");
- if (search(root, p2) == 0)
- {
- fprintf(stderr, "PATH: No such file or directory\n");
- break;
- }
- int i = 0;
- while (strcmp(p, c) != 0)
- {
- i++;
- if (p[0] != '/' && p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else
- {
- 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, "PATH: No such file or directory\n");
- }
- else
- {
- char *p3 = strrchr(p, '/');
- 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)
- {
- currentDirectory = "/";
- break;
- }
- else
- {
- if (p[0] != '/' && p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else
- {
- 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';
- }
- p2 = strtok(NULL, " \n");
- if (p2 != NULL)
- {
- fprintf(stderr, "cd: too many arguments\n");
- break;
- }
- else
- {
- if (search(root, p) == 0)
- {
- fprintf(stderr, "PATH: No such file or directory\n");
- }
- else
- {
- p[strlen(p) - 1] = '\0';
- strcpy(currentDirectory, p);
- }
- }
- }
- }
- else if (strcmp(p, "rmdir") == 0)
- {
- p = strtok(NULL, " \n");
- if (p == NULL)
- {
- fprintf(stderr, "rmdir: missing operand\n");
- break;
- }
- while (p != NULL)
- {
- if (p[0] != '/' && p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else
- {
- 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, "PATH: No such file or directory\n");
- }
- else
- {
- if (isEmpty(find(root, p)) == 1)
- {
- root = rm(root, p, 0);
- }
- else
- {
- fprintf(stderr, "PATH: Directory not empty\n");
- }
- }
- p = strtok(NULL, " \n");
- }
- }
- else if (strcmp(p, "rm") == 0)
- {
- p = strtok(NULL, " \n");
- if (p == NULL)
- {
- fprintf(stderr, "rm: missing operand\n");
- break;
- }
- while (p != NULL)
- {
- if (p[0] != '/' && p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else
- {
- 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, "PATH: No such file or directory\n");
- }
- else
- {
- root = rm(root, p, 0);
- }
- p = strtok(NULL, " \n");
- }
- }
- else if (strcmp(p, "mv") == 0)
- {
- p = strtok(NULL, " \n");
- p2 = strrchr(b, ' ');
- if (p2 == NULL)
- {
- fprintf(stderr, "mv: missing operand\n");
- break;
- }
- p2 = p2 + 1;
- p2[strlen(p2) - 1] = '\0';
- if (strcmp(p, p2) == 0)
- {
- fprintf(stderr, "mv: missing operand\n");
- break;
- }
- char c[100];
- strcpy(c, p2);
- if (p2[0] != '/' && p2[0] == '.')
- {
- p2 = convertToAbsolute(p2, currentDirectory);
- }
- else
- {
- strcpy(b, "./");
- strcat(b, p2);
- p2 = b;
- p2 = convertToAbsolute(p2, currentDirectory);
- }
- p2 = strcat(p2, "/");
- if (search(root, p2) == 0)
- {
- fprintf(stderr, "PATH: No such file or directory\n");
- break;
- }
- int i = 0;
- while (strcmp(p, c) != 0)
- {
- i++;
- if (p[0] != '/' && p[0] == '.')
- {
- p = convertToAbsolute(p, currentDirectory);
- }
- else
- {
- 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, "PATH: No such file or directory\n");
- }
- else
- {
- root = rm(root, p, 0);
- char *p3 = strrchr(p, '/');
- 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", p);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement