From 42abbc8f06fb56761502824b8085516299535534 Mon Sep 17 00:00:00 2001 From: iceyrazor Date: Mon, 9 Feb 2026 23:48:22 -0600 Subject: [PATCH] - readme. todo - update pwd - update SHLVL - basename pwd in prompt - updating pwd --- README.md | 18 +++++++++++++ src/helper_functions.c | 36 ++++++++++++++++++++++++++ src/lsh_builtins.c | 13 ++++++++-- src/lsh_main_func.c | 2 ++ src/main.c | 58 ++++++++++++++++++++++++++++++++---------- 5 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..fd0dbdc --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# todo + +- [x] environment variables +- [x] custom prompts +- [x] minimal current directory +- [ ] quotes +- [-] handle ~ +- [ ] dynamically allocate cwd in cd builtin command +- [-] all of posixs + - [ ] aliases + - [x] increase the SHLVL var + - [x] update pwd + - [ ] pipes | + - [ ] redirections > < + - [ ] append >> + - [ ] heredoc +- [ ] rcfile + - [ ] rcfile in homedir diff --git a/src/helper_functions.c b/src/helper_functions.c index 9427b87..5c81f15 100644 --- a/src/helper_functions.c +++ b/src/helper_functions.c @@ -1,6 +1,8 @@ #ifndef shell_helper_functions #define shell_helper_functions +#include +#include #include #include @@ -51,4 +53,38 @@ char *str_replace(char *orig, char *rep, char *with) { return result; } +char *basename(char *path_in) +{ + char *token = NULL; + char *lastToken = NULL; + char *basePath; + + basePath = (char *)malloc(sizeof(char) * (strlen(path_in) + 1)); + if (!basePath) { + fprintf(stderr, "lsh: allocatin error\n"); + exit(EXIT_FAILURE); + } + strcpy(basePath, path_in); + + token = strtok(basePath, "/"); + while (token != NULL) { + lastToken = token; + token = strtok(NULL, "/"); + } + + if (lastToken == NULL){ + free(basePath); + return NULL; + } + + char *ret_str = (char*)malloc(sizeof(char)*strlen(lastToken)+1); + if (!ret_str) { + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } + strcpy(ret_str,lastToken); + free(basePath); + return ret_str; +} + #endif diff --git a/src/lsh_builtins.c b/src/lsh_builtins.c index 33b1b88..81fc2b5 100644 --- a/src/lsh_builtins.c +++ b/src/lsh_builtins.c @@ -44,9 +44,18 @@ int lsh_cd(char **args) if (args[1] == NULL) { fprintf(stderr, "lsh: expected argument to \"cd\"\n"); } else { - if (chdir(args[1]) != 0) { + int err = chdir(args[1]); + if (err != 0) { perror("lsh"); } + char *cwd=(char*)malloc(sizeof(char)*4096); + if (!cwd) { + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } + getcwd(cwd,sizeof(char*)*4096); + setenv("PWD",cwd,1); + free(cwd); } return 1; } @@ -54,7 +63,7 @@ int lsh_cd(char **args) int lsh_help(char **args) { int i; - printf("Stephen Brennan's LSH\n"); + printf("FOX SHELL\n"); printf("Type program names and arguments, and hit enter.\n"); printf("The following are built in:\n"); diff --git a/src/lsh_main_func.c b/src/lsh_main_func.c index f2a19a2..c144238 100644 --- a/src/lsh_main_func.c +++ b/src/lsh_main_func.c @@ -46,10 +46,12 @@ char *lsh_read_line(void) } char *handle_vars(char *token){ + token=&token[0]; if (token[0] == '$'){ token=&token[1]; token=getenv(token); } + return token; } diff --git a/src/main.c b/src/main.c index 59f474b..15830ac 100755 --- a/src/main.c +++ b/src/main.c @@ -2,43 +2,75 @@ #include #include +#include "helper_functions.c" #include "lsh_main_func.c" #include "lsh_builtins.c" typedef struct{ char *prompt; + char *pre_prompt; } SHELL_OB; void lsh_loop(SHELL_OB *shell_obj) { - char *line; - char **args; - int status; + char *line; + char **args; + int status; - do { - printf("%s",shell_obj->prompt); - line = lsh_read_line(); - args = lsh_split_line(line); - status = lsh_execute(args); + do { + //get pwd + char *cwd= getenv("PWD"); + char *pwd=basename(cwd); + snprintf(shell_obj->prompt,sizeof(char) * 1024,"%s %s> ",shell_obj->pre_prompt,pwd); + free(pwd); + printf("%s",shell_obj->prompt); + line = lsh_read_line(); + args = lsh_split_line(line); + status = lsh_execute(args); - free(line); - free(args); - } while (status); + free(line); + free(args); + } while (status); } int main(int argc, char **argv){ + //set sh level + char *shlvl = getenv("SHLVL"); + if(shlvl != NULL){ + int shlvli = atoi(shlvl); + shlvli++; + sprintf(shlvl,"%d",shlvli); + setenv("SHLVL",shlvl,1); + } + + //Set The prompt SHELL_OB shell_obj; shell_obj.prompt = (char *)malloc(sizeof(char) * 1024); + if (!shell_obj.prompt){ + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } + shell_obj.pre_prompt = (char *)malloc(sizeof(char) * 1024); + if (!shell_obj.pre_prompt){ + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } //get the hostname char *hostname = (char *)malloc(sizeof(char) * 1024); + if (!hostname) { + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } gethostname(hostname,sizeof(char) * 1024); - snprintf(shell_obj.prompt,sizeof(char) * 1024,"%s@%s> ",getenv("USER"),hostname); + //set the pre_prompt + snprintf(shell_obj.pre_prompt,sizeof(char) * 1024,"%s@%s",getenv("USER"),hostname); + free(hostname); lsh_loop(&shell_obj); - free(hostname); free(shell_obj.prompt); + free(shell_obj.pre_prompt); return EXIT_SUCCESS; }