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>
- #define MAX_FILENAME 65
- typedef struct SharedContent {
- char *data;
- int refcount;
- } SharedContent;
- typedef struct Inode {
- char name[MAX_FILENAME];
- bool is_directory;
- bool is_symlink;
- SharedContent *shared;
- struct Inode *parent;
- struct Inode *children;
- struct Inode *next;
- } Inode;
- Inode *root;
- Inode *cwd;
- void init_filesystem(void);
- void free_inode(Inode *inode);
- SharedContent* create_shared_content(const char *initial);
- Inode* find_inode_no_symlink(const char *path);
- Inode* find_inode_follow(const char *path);
- void cmd_ls(char *path);
- void cmd_cd(char *path);
- void cmd_cat(char *filePath);
- void cmd_echo(char *text, char *filename, bool append);
- void cmd_touch_single(char *filename);
- void cmd_touch(char *args);
- void cmd_mkdir(char *args);
- void cmd_rm(char *params);
- void cmd_find(Inode *dir, const char *path);
- void cmd_ln(char *path1, char *path2, bool hard);
- void cmd_ln_symlink(char *targetPath, char *linkPath);
- void cmd_cp(char *s1, char *s2, bool recursive);
- void cmd_mv(char *path1, char *path2);
- void process_command(char *command);
- void shell_loop(void);
- void init_filesystem() {
- root = (Inode*)calloc(1, sizeof(Inode));
- // Root’s name can be just "/"
- strncpy(root->name, "/", MAX_FILENAME - 1);
- root->name[MAX_FILENAME - 1] = '\0';
- root->is_directory = true;
- root->is_symlink = false;
- root->shared = NULL;
- root->parent = root;
- cwd = root;
- }
- void free_inode(Inode *inode) {
- if (inode->shared) {
- inode->shared->refcount--;
- if (inode->shared->refcount <= 0) {
- free(inode->shared->data);
- free(inode->shared);
- }
- }
- free(inode);
- }
- SharedContent* create_shared_content(const char *initial) {
- SharedContent *sc = (SharedContent*)malloc(sizeof(SharedContent));
- sc->refcount = 1;
- if (initial) {
- sc->data = (char*)malloc(strlen(initial) + 1);
- strcpy(sc->data, initial);
- } else {
- sc->data = NULL;
- }
- return sc;
- }
- static void remove_from_parent(Inode *inode);
- static void rm_recursive(Inode *inode);
- static void remove_from_parent(Inode *inode) {
- if (!inode || !inode->parent) return;
- Inode *p = inode->parent;
- // If it’s the first child
- if (p->children == inode) {
- p->children = inode->next;
- } else {
- // Otherwise find it
- Inode *cur = p->children;
- while (cur && cur->next != inode) {
- cur = cur->next;
- }
- if (cur) {
- cur->next = inode->next;
- }
- }
- inode->parent = NULL;
- inode->next = NULL;
- }
- static void rm_recursive(Inode *inode) {
- if (!inode) return;
- // If directory with children, remove them first
- if (inode->is_directory && inode->children) {
- Inode *child = inode->children;
- while (child) {
- Inode *nxt = child->next;
- rm_recursive(child);
- child = nxt;
- }
- }
- if(inode == cwd)
- {
- cwd = root;
- }
- remove_from_parent(inode);
- free_inode(inode);
- }
- static Inode* linear_find_child(Inode *parent, const char *name) {
- if (!parent->is_directory) return NULL;
- Inode *child = parent->children;
- while (child) {
- if (strcmp(child->name, name) == 0) {
- return child;
- }
- child = child->next;
- }
- return NULL;
- }
- Inode* find_inode_no_symlink(const char *path) {
- if (!path) return NULL;
- if (!*path) return cwd;
- // Make a copy for strtok
- char *path_copy = (char*)malloc(strlen(path) + 1);
- strcpy(path_copy, path);
- // If path starts with '/', we begin at root, else at cwd
- Inode *current = (path_copy[0] == '/') ? root : cwd;
- // Tokenize
- char *component = strtok(path_copy, "/");
- if (!component && path_copy[0] == '/') {
- free(path_copy);
- return root;
- }
- while (component) {
- if (strcmp(component, ".") == 0) {
- // stay
- } else if (strcmp(component, "..") == 0) {
- if (current != root)
- current = current->parent;
- } else {
- // find a child
- Inode *child = linear_find_child(current, component);
- if (!child) {
- free(path_copy);
- return NULL;
- }
- current = child;
- }
- component = strtok(NULL, "/");
- }
- free(path_copy);
- return current;
- }
- // Like find_inode_no_symlink but if the final Inode is a symlink, follow it repeatedly (up to 100).
- Inode* find_inode_follow(const char *path) {
- if (!path) return NULL;
- Inode *node = find_inode_no_symlink(path);
- if (!node) return NULL;
- int symlink_depth = 0;
- while (node->is_symlink) {
- if (!node->shared || !node->shared->data) {
- // broken symlink
- return NULL;
- }
- if (++symlink_depth > 100) {
- // protect from infinite loops
- return NULL;
- }
- char *linkTarget = node->shared->data;
- bool isAbs = (linkTarget[0] == '/');
- Inode *startDir = isAbs ? root : node->parent;
- // temporarily treat startDir as "cwd"
- Inode *oldCwd = cwd;
- cwd = startDir;
- Inode *resolved = find_inode_no_symlink(linkTarget);
- cwd = oldCwd;
- if (!resolved) return NULL;
- node = resolved;
- }
- return node;
- }
- static int cstring_cmp(const void *p1, const void *p2) {
- const char *s1 = *(const char **)p1;
- const char *s2 = *(const char **)p2;
- return strcmp(s1, s2);
- }
- static int inode_name_cmp(const void *p1, const void *p2) {
- const Inode *i1 = *(const Inode **)p1;
- const Inode *i2 = *(const Inode **)p2;
- return strcmp(i1->name, i2->name);
- }
- // ----------------------------------------------------------------
- // Commands
- // ----------------------------------------------------------------
- void cmd_ls(char *path) {
- Inode *dir = cwd;
- if (path && *path) {
- Inode *maybe = find_inode_follow(path);
- if (!maybe) return; // invalid path => no output
- if (!maybe->is_directory) {
- printf("%s\n", maybe->name);
- return;
- }
- dir = maybe;
- }
- // Count children
- int count = 0;
- Inode *child = dir->children;
- while (child) {
- count++;
- child = child->next;
- }
- if (count == 0) return;
- // Gather child->name in array
- char **names = (char**)malloc(count * sizeof(char*));
- child = dir->children;
- for (int i=0; i<count; i++) {
- names[i] = child->name;
- child = child->next;
- }
- qsort(names, count, sizeof(char*), cstring_cmp);
- for (int i=0; i<count; i++) {
- printf("%s\n", names[i]);
- }
- free(names);
- }
- void cmd_cd(char *path) {
- if (!path || !*path) {
- // cd with no args => go to root
- cwd = root;
- return;
- }
- Inode *dest = find_inode_follow(path);
- if (dest && dest->is_directory) {
- cwd = dest;
- }
- }
- void cmd_cat(char *filePath) {
- if (!filePath) return;
- Inode *f = find_inode_follow(filePath);
- if (!f) return; // doesn’t exist
- if (f->is_directory) return; // cat on a directory => do nothing
- if (f->shared && f->shared->data) {
- printf("%s\n", f->shared->data);
- }
- }
- // Helper for echo overwrite
- static void echo_overwrite(char *text, char *filename) {
- if (!text || !filename) return;
- Inode *f = find_inode_follow(filename);
- if (!f) {
- Inode *maybe = find_inode_no_symlink(filename);
- if (!maybe) {
- cmd_touch_single(filename);
- f = find_inode_follow(filename);
- if (!f) return;
- } else {
- return;
- }
- }
- if (f->is_directory) return;
- if (!f->shared) {
- f->shared = create_shared_content(NULL);
- } else if (f->shared->data) {
- free(f->shared->data);
- f->shared->data = NULL;
- }
- f->shared->data = (char*)malloc(strlen(text)+1);
- strcpy(f->shared->data, text);
- }
- static void echo_add(char *text, char *filename) {
- if (!text || !filename) return;
- Inode *f = find_inode_follow(filename);
- if (!f) {
- Inode *maybe = find_inode_no_symlink(filename);
- if (!maybe) {
- cmd_touch_single(filename);
- f = find_inode_follow(filename);
- if (!f) return;
- } else {
- return;
- }
- }
- if (f->is_directory) return;
- if (!f->shared) {
- f->shared = create_shared_content(NULL);
- }
- if (!f->shared->data) {
- f->shared->data = (char*)malloc(strlen(text)+1);
- strcpy(f->shared->data, text);
- } else {
- size_t oldlen = strlen(f->shared->data);
- size_t newlen = oldlen + strlen(text) + 1;
- char *new_data = (char*)realloc(f->shared->data, newlen);
- if (!new_data) {
- free(f->shared->data);
- f->shared->data = NULL;
- return;
- }
- f->shared->data = new_data;
- strcat(f->shared->data, text);
- }
- }
- void cmd_echo(char *text, char *filename, bool append) {
- if (!text || !filename) return;
- if (!append) {
- echo_overwrite(text, filename);
- } else {
- echo_add(text, filename);
- }
- }
- void cmd_touch_single(char *filename) {
- if (!filename || !*filename) return;
- Inode *exists = find_inode_no_symlink(filename);
- if (exists) return;
- int idx = -1;
- size_t len = strlen(filename);
- for (size_t i = 0; i < len; i++) {
- if (filename[i] == '/') idx = i;
- }
- char *dir_path = NULL;
- char *base_name = NULL;
- if (idx != -1) {
- if (idx == 0) {
- dir_path = strdup("/");
- } else {
- dir_path = (char*)malloc(idx + 1);
- if (dir_path) {
- strncpy(dir_path, filename, idx);
- dir_path[idx] = '\0';
- }
- }
- base_name = strdup(filename + idx + 1);
- } else {
- dir_path = strdup("");
- base_name = strdup(filename);
- }
- Inode *parent_dir = find_inode_no_symlink(dir_path);
- if (!parent_dir) {
- parent_dir = cwd;
- }
- if (!parent_dir->is_directory) {
- free(dir_path);
- free(base_name);
- return;
- }
- Inode *newf = (Inode*)malloc(sizeof(Inode));
- memset(newf, 0, sizeof(Inode));
- strncpy(newf->name, base_name, MAX_FILENAME - 1);
- newf->name[MAX_FILENAME - 1] = '\0';
- newf->is_directory = false;
- newf->is_symlink = false;
- newf->shared = create_shared_content(NULL);
- newf->parent = parent_dir;
- newf->next = parent_dir->children;
- parent_dir->children = newf;
- free(dir_path);
- free(base_name);
- }
- void cmd_touch(char *args) {
- if (!args || !*args) return;
- char *args_copy = strdup(args);
- char *save;
- char *token = strtok_r(args_copy, " ", &save);
- while (token) {
- cmd_touch_single(token);
- token = strtok_r(NULL, " ", &save);
- }
- free(args_copy);
- }
- void _mkdir(char *dirname) {
- char *copy = malloc((strlen(dirname) + 1) * sizeof(char));
- strcpy(copy, dirname);
- Inode *current = (copy[0] == '/') ? root : cwd;
- char *save;
- char *component = strtok_r(copy, "/", &save);
- while (component) {
- if (strcmp(component, ".") == 0) {
- } else if (strcmp(component, "..") == 0) {
- if (current != root) current = current->parent;
- } else {
- Inode *scan = current->children;
- Inode *found = NULL;
- while (scan) {
- if (scan->is_directory && strcmp(scan->name, component) == 0) {
- found = scan;
- break;
- }
- scan = scan->next;
- }
- if (found) {
- current = found;
- } else {
- Inode *newd = (Inode*)calloc(1, sizeof(Inode));
- strncpy(newd->name, component, MAX_FILENAME - 1);
- newd->name[MAX_FILENAME - 1] = '\0';
- newd->is_directory = true;
- newd->is_symlink = false;
- newd->parent = current;
- newd->next = current->children;
- current->children = newd;
- current = newd;
- }
- }
- component = strtok_r(NULL, "/", &save);
- }
- free(copy);
- }
- void cmd_mkdir(char *args) {
- char *copy = malloc((strlen(args) + 1) * sizeof(char));
- strcpy(copy, args);
- char *save;
- char *token = strtok_r(copy, " ", &save);
- while (token) {
- if (strcmp(token, "-p") == 0) {
- } else {
- _mkdir(token);
- }
- token = strtok_r(NULL, " ", &save);
- }
- free(copy);
- }
- void cmd_rm(char *params) {
- if (!params || !*params) return;
- char *copy = strdup(params);
- char *p = copy;
- while (*p == ' ') p++;
- bool recursive_flag = false;
- if (strncmp(p, "-r", 2) == 0 && (p[2] == ' ' || p[2] == '\0')) {
- recursive_flag = true;
- p += 2;
- while (*p == ' ') p++;
- }
- while (*p) {
- char *start = p;
- while (*p && *p != ' ') p++;
- bool ended = false;
- if (*p == ' ') {
- *p = '\0';
- p++;
- ended = true;
- }
- if (*start) {
- Inode *target = find_inode_no_symlink(start);
- if (target) {
- if (target->is_directory && !recursive_flag) {
- } else {
- rm_recursive(target);
- }
- }
- }
- while (*p == ' ') p++;
- if (!ended && !*p) break;
- }
- free(copy);
- }
- void cmd_find(Inode *dir, const char *path) {
- if (!dir || !path) return;
- if (strcmp(path, ".") == 0) {
- printf(".\n");
- }
- int count = 0;
- Inode *child = dir->children;
- while (child) {
- count++;
- child = child->next;
- }
- if (count == 0) return;
- Inode **arr = (Inode**)malloc(count * sizeof(Inode*));
- child = dir->children;
- for (int i = 0; i < count; i++) {
- arr[i] = child;
- child = child->next;
- }
- qsort(arr, count, sizeof(Inode*), inode_name_cmp);
- for (int i = 0; i < count; i++) {
- Inode *c = arr[i];
- size_t new_path_len = strlen(path) + 1 /* for "/" */ + strlen(c->name) + 1;
- char *new_path = (char*)malloc(new_path_len);
- if (!new_path) continue;
- snprintf(new_path, new_path_len, "%s/%s", path, c->name);
- printf("%s\n", new_path);
- if (c->is_directory) {
- cmd_find(c, new_path);
- }
- free(new_path);
- }
- free(arr);
- }
- void cmd_ln(char *path1, char *path2, bool hard) {
- // (void)hard; // unused, but you can keep this line if needed
- if (!path1 || !path2) return;
- Inode *source = find_inode_no_symlink(path1);
- if (!source) return;
- if (source->is_directory) {
- return; // The assignment disallows creating hard links to directories
- }
- // Find the slash position
- int idx = -1;
- int len = strlen(path2);
- for (int i = 0; i < len; i++) {
- if (path2[i] == '/') idx = (int)i;
- }
- // Allocate dir_path / base_name dynamically
- char *dir_path = NULL;
- char *base_name = NULL;
- if (idx != -1) {
- // path2 has at least one slash
- if (idx == 0) {
- // Leading slash => parent directory is "/"
- dir_path = strdup("/");
- } else {
- // Copy everything up to idx
- dir_path = (char*)malloc(idx + 1);
- strncpy(dir_path, path2, idx);
- dir_path[idx] = '\0';
- }
- base_name = strdup(path2 + idx + 1);
- } else {
- // No slash => parent directory is "."
- dir_path = strdup(".");
- base_name = strdup(path2);
- }
- Inode *parent_dir = find_inode_no_symlink(dir_path);
- // If parent_dir does not exist, fallback to cwd
- if (!parent_dir) {
- parent_dir = cwd;
- }
- if (!parent_dir->is_directory) {
- free(dir_path);
- free(base_name);
- return;
- }
- // Create the new hard link inode
- Inode *new_link = (Inode*)calloc(1, sizeof(Inode));
- strncpy(new_link->name, base_name, MAX_FILENAME - 1);
- new_link->name[MAX_FILENAME - 1] = '\0';
- new_link->is_directory = false;
- new_link->is_symlink = false;
- new_link->shared = source->shared; // share the file data
- if (new_link->shared) {
- new_link->shared->refcount++;
- }
- new_link->parent = parent_dir;
- new_link->next = parent_dir->children;
- parent_dir->children = new_link;
- free(dir_path);
- free(base_name);
- }
- // Symlink creation
- void cmd_ln_symlink(char *targetPath, char *linkPath) {
- if (!targetPath || !linkPath) return;
- int idx = -1;
- size_t len = strlen(linkPath);
- for (size_t i = 0; i < len; i++) {
- if (linkPath[i] == '/') idx = i;
- }
- char *dir_path = NULL;
- char *file_name = NULL;
- if (idx != -1) {
- if (idx == 0) {
- dir_path = strdup("/");
- } else {
- dir_path = (char*)malloc(idx + 1);
- if (dir_path) {
- strncpy(dir_path, linkPath, idx);
- dir_path[idx] = '\0';
- }
- }
- file_name = strdup(linkPath + idx + 1);
- } else {
- dir_path = strdup(".");
- file_name = strdup(linkPath);
- }
- Inode *parent_dir = find_inode_no_symlink(dir_path);
- if (!parent_dir) parent_dir = cwd;
- if (!parent_dir->is_directory) {
- free(dir_path);
- free(file_name);
- return;
- }
- Inode *new_link = (Inode*)calloc(1, sizeof(Inode));
- strncpy(new_link->name, file_name, MAX_FILENAME - 1);
- new_link->name[MAX_FILENAME - 1] = '\0';
- new_link->is_directory = false;
- new_link->is_symlink = true;
- new_link->shared = create_shared_content(targetPath);
- new_link->parent = parent_dir;
- new_link->next = parent_dir->children;
- parent_dir->children = new_link;
- free(dir_path);
- free(file_name);
- }
- static void copy_file(Inode *src, char *dest_path) {
- if (!src || !dest_path) return;
- Inode *dest = find_inode_no_symlink(dest_path);
- if (dest) rm_recursive(dest);
- cmd_touch_single(dest_path);
- dest = find_inode_no_symlink(dest_path);
- if (!dest) return;
- if (src->shared && src->shared->data) {
- if (!dest->shared) {
- dest->shared = create_shared_content(NULL);
- } else {
- free(dest->shared->data);
- dest->shared->data = NULL;
- }
- dest->shared->data = (char*)malloc(strlen(src->shared->data)+1);
- strcpy(dest->shared->data, src->shared->data);
- }
- }
- static void copy_directory(Inode *src, Inode *dest_dir) {
- if (!src || !dest_dir) return;
- // For each child in src, replicate
- for (Inode *child = src->children; child; child = child->next) {
- if (child->is_directory) {
- Inode *nd = (Inode*)calloc(1, sizeof(Inode));
- strncpy(nd->name, child->name, MAX_FILENAME - 1);
- nd->name[MAX_FILENAME - 1] = '\0';
- nd->is_directory = true;
- nd->is_symlink = false;
- nd->parent = dest_dir;
- nd->next = dest_dir->children;
- dest_dir->children = nd;
- copy_directory(child, nd);
- } else {
- Inode *nf = (Inode*)calloc(1, sizeof(Inode));
- strncpy(nf->name, child->name, MAX_FILENAME - 1);
- nf->name[MAX_FILENAME - 1] = '\0';
- nf->is_directory = false;
- nf->is_symlink = child->is_symlink;
- nf->parent = dest_dir;
- nf->next = dest_dir->children;
- dest_dir->children = nf;
- if (child->shared && child->shared->data) {
- nf->shared = create_shared_content(child->shared->data);
- } else {
- nf->shared = create_shared_content(NULL);
- }
- }
- }
- }
- void cmd_cp(char *s1, char *s2, bool recursive) {
- if (!s1 || !s2) return;
- Inode *src = find_inode_follow(s1);
- if (!src) return;
- if (src->is_directory) {
- if (!recursive) return;
- Inode *dst = find_inode_no_symlink(s2);
- if (!dst) {
- // create it
- _mkdir(s2);
- dst = find_inode_no_symlink(s2);
- if (!dst) return;
- }
- copy_directory(src, dst);
- } else {
- // copy file
- copy_file(src, s2);
- }
- }
- void cmd_mv(char *path1, char *path2) {
- if (!path1 || !path2) return;
- Inode *file1 = find_inode_no_symlink(path1);
- if (!file1) return;
- int last_slash = -1;
- size_t len = strlen(path2);
- for (size_t i = 0; i < len; i++) {
- if (path2[i] == '/') last_slash = i;
- }
- char *dir_path = NULL;
- char *base_name = NULL;
- if (last_slash == -1) {
- dir_path = strdup(".");
- base_name = strdup(path2);
- } else {
- if (last_slash == 0) {
- dir_path = strdup("/");
- } else {
- dir_path = (char*)malloc(last_slash + 1);
- if (dir_path) {
- strncpy(dir_path, path2, last_slash);
- dir_path[last_slash] = '\0';
- }
- }
- base_name = strdup(path2 + last_slash + 1);
- }
- Inode *new_parent = find_inode_no_symlink(dir_path);
- if (!new_parent) {
- free(dir_path);
- free(base_name);
- return;
- }
- if (!new_parent->is_directory) {
- free(dir_path);
- free(base_name);
- return;
- }
- Inode *c = new_parent->children;
- Inode *existing = NULL;
- while (c) {
- if (strcmp(c->name, base_name) == 0) {
- existing = c;
- break;
- }
- c = c->next;
- }
- if (existing) rm_recursive(existing);
- remove_from_parent(file1);
- memset(file1->name, 0, MAX_FILENAME);
- strncpy(file1->name, base_name, MAX_FILENAME - 1);
- file1->name[MAX_FILENAME - 1] = '\0';
- file1->parent = new_parent;
- file1->next = new_parent->children;
- new_parent->children = file1;
- free(dir_path);
- free(base_name);
- }
- void process_command(char *command) {
- char *localbuf = malloc((strlen(command) + 1) * sizeof(char));
- strcpy(localbuf, command);
- // printf("LOCALBUF: %s\n\n", localbuf);
- char *cmd = strtok(localbuf, " ");
- if (!cmd) {
- return;
- }
- char *rest = strtok(NULL, "");
- if (rest) {
- while (*rest == ' ') rest++;
- }
- if (strcmp(cmd, "ls") == 0) {
- if (rest && *rest) cmd_ls(rest);
- else cmd_ls(NULL);
- }
- else if (strcmp(cmd, "cd") == 0) {
- if (rest && *rest) cmd_cd(rest);
- else cmd_cd(NULL);
- }
- else if (strcmp(cmd, "cat") == 0) {
- if (rest && *rest) cmd_cat(rest);
- }
- else if (strcmp(cmd, "touch") == 0) {
- if (rest && *rest) cmd_touch(rest);
- }
- else if (strcmp(cmd, "echo") == 0) {
- if (!rest || !*rest) {
- return;
- }
- char *quoteBegin = strchr(rest, '"');
- if (!quoteBegin) {
- return;
- }
- quoteBegin++;
- char *quoteEnd = strchr(quoteBegin, '"');
- if (!quoteEnd) {
- return;
- }
- *quoteEnd = '\0';
- char *afterQuote = quoteEnd + 1;
- while (*afterQuote == ' ') afterQuote++;
- if (!*afterQuote) {
- return;
- }
- char *redir = strtok(afterQuote, " ");
- if (!redir) {
- return;
- }
- char *filename = strtok(NULL, " ");
- if (!filename) {
- return;
- }
- bool append = (strcmp(redir, ">>") == 0);
- cmd_echo(quoteBegin, filename, append);
- }
- else if (strcmp(cmd, "mkdir") == 0) {
- // if (rest && *rest) cmd_mkdir(rest);
- }
- else if (strcmp(cmd, "rm") == 0) {
- // if (rest && *rest) cmd_rm(rest);
- }
- else if (strcmp(cmd, "find") == 0) {
- // cmd_find(cwd, ".");
- }
- else if (strcmp(cmd, "cp") == 0) {
- /*if (!rest || !*rest) {
- return;
- }
- char *local = strdup(rest);
- char *tok = strtok(local, " ");
- if (!tok) {
- free(local);
- return;
- }
- bool recursive = false;
- if (strcmp(tok, "-r") == 0) {
- recursive = true;
- tok = strtok(NULL, " ");
- if (!tok) {
- free(local);
- return;
- }
- }
- char *arg1 = tok;
- char *arg2 = strtok(NULL, "");
- if (!arg2 || !*arg2) {
- free(local);
- return;
- }
- while (*arg2 == ' ') arg2++;
- if (!*arg2) {
- free(local);
- return;
- }
- cmd_cp(arg1, arg2, recursive);
- free(local);*/
- }
- else if (strcmp(cmd, "mv") == 0) {
- /*if (!rest || !*rest) {
- return;
- }
- char *local = strdup(rest);
- char *arg1 = strtok(local, " ");
- if (!arg1) {
- free(local);
- return;
- }
- char *arg2 = strtok(NULL, "");
- if (!arg2) {
- free(local);
- return;
- }
- while (*arg2 == ' ') arg2++;
- if (!*arg2) {
- free(local);
- return;
- }
- cmd_mv(arg1, arg2);
- free(local);*/
- }
- else if (strcmp(cmd, "ln") == 0) {
- /*if (!rest || !*rest) {
- return;
- }
- char *local = strdup(rest);
- char *maybeFlag = strtok(local, " ");
- if (!maybeFlag) {
- free(local);
- return;
- }
- if (strcmp(maybeFlag, "-s") == 0) {
- char *path1 = strtok(NULL, " ");
- if (!path1) {
- free(local);
- return;
- }
- char *path2 = strtok(NULL, "");
- if (!path2) {
- free(local);
- return;
- }
- while (*path2 == ' ') path2++;
- if (!*path2) {
- free(local);
- return;
- }
- cmd_ln_symlink(path1, path2);
- } else {
- // Hard link
- char *path2 = strtok(NULL, "");
- if (!path2) {
- free(local);
- return;
- }
- while (*path2 == ' ') path2++;
- if (!*path2) {
- free(local);
- return;
- }
- cmd_ln(maybeFlag, path2, true);
- }
- free(local);*/
- }
- else if (strcmp(cmd, "exit") == 0) {
- Inode *child = root->children;
- while (child) {
- Inode *nx = child->next;
- rm_recursive(child);
- child = nx;
- }
- free(localbuf);
- free_inode(root);
- exit(0);
- }
- free(localbuf);
- }
- void shell_loop() {
- char *command = NULL;
- size_t len = 0;
- while (getline(&command, &len, stdin) != -1) {
- size_t command_len = strlen(command);
- if (command_len > 0 && command[command_len - 1] == '\n') {
- command[command_len - 1] = '\0';
- }
- process_command(command);
- }
- free(command);
- }
- int main() {
- init_filesystem();
- shell_loop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement