From f3d03e71ede8e94e7137777ab5ad5570c3d11b31 Mon Sep 17 00:00:00 2001 From: iceyrazor Date: Fri, 6 Mar 2026 22:34:50 -0600 Subject: some cleanup and shell var - SHELL is now set - cwd now has realloc - realloc with error checking is now a fucntion like malloce --- README.md | 2 +- src/helper_functions.c | 11 ++++++++++- src/lsh_builtins.c | 11 +++++++++-- src/lsh_main_func.c | 12 ++---------- src/lsh_split_line.c | 6 +----- src/main.c | 23 +++++++++++++++++++++++ 6 files changed, 46 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index eb9a83e..bba5b3f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Going to try and make it mostly POSIX compliant. - [x] custom prompts - [x] minimal current directory - [x] handle ~ -- [ ] dynamically allocate cwd in cd builtin command +- [x] dynamically allocate cwd in cd builtin command - [ ] history - [ ] cursor movement - [ ] tab to autocomplete files and dirs diff --git a/src/helper_functions.c b/src/helper_functions.c index 162cf45..cd35848 100644 --- a/src/helper_functions.c +++ b/src/helper_functions.c @@ -16,6 +16,15 @@ void *malloce(size_t size){ return mal; } +void *realloce(void* mal,size_t size){ + mal=realloc(mal,size); + if(!mal){ + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } + return mal; +} + char *pop(char *token, int ch_i){ if(token[ch_i]=='\0'){ return token; @@ -83,7 +92,7 @@ char *basename(char *path_in) { char *token = NULL; char *lastToken = NULL; - char *basePath; + char *basePath = NULL; basePath = (char *)malloce(sizeof(char) * (strlen(path_in) + 1)); strcpy(basePath, path_in); diff --git a/src/lsh_builtins.c b/src/lsh_builtins.c index dda49c4..51a44cd 100644 --- a/src/lsh_builtins.c +++ b/src/lsh_builtins.c @@ -40,6 +40,7 @@ int lsh_num_builtins() { /* Builtin function implementations. */ +#define LSH_CWD_BUF_SIZE 1024; int lsh_cd(char **args) { if (args[1] == NULL) { @@ -49,8 +50,14 @@ int lsh_cd(char **args) if (err != 0) { perror("lsh"); } - char *cwd=(char*)malloce(sizeof(char)*4096); - getcwd(cwd,sizeof(char*)*4096); + + int bufsize=LSH_CWD_BUF_SIZE; + char *cwd=(char*)malloce(sizeof(char)*bufsize+1); + while(getcwd(cwd,sizeof(char)*bufsize)==NULL){ + bufsize += LSH_CWD_BUF_SIZE; + cwd=(char*)realloce(cwd,sizeof(char)*bufsize+1); + } + setenv("PWD",cwd,1); free(cwd); } diff --git a/src/lsh_main_func.c b/src/lsh_main_func.c index 52c016b..ea2729d 100644 --- a/src/lsh_main_func.c +++ b/src/lsh_main_func.c @@ -36,11 +36,7 @@ char *lsh_read_line(void) // If we have exceeded the buffer, reallocate. if (position >= bufsize) { bufsize += LSH_RL_BUFSIZE; - buffer = realloc(buffer, bufsize); - if (!buffer) { - fprintf(stderr, "lsh: allocation error\n"); - exit(EXIT_FAILURE); - } + buffer = realloce(buffer, bufsize); } } } @@ -160,11 +156,7 @@ CMD **lsh_split_command(char *line) if (position * bufsize >= bufsize) { bufsize += sizeof(CMD*); - tokens = realloc(tokens, bufsize); - if (!tokens) { - fprintf(stderr, "lsh: allocation error\n"); - exit(EXIT_FAILURE); - } + tokens = realloce(tokens, bufsize); } // do not make Redirect or Append a actual command to run diff --git a/src/lsh_split_line.c b/src/lsh_split_line.c index ff4f644..17e16a7 100644 --- a/src/lsh_split_line.c +++ b/src/lsh_split_line.c @@ -164,11 +164,7 @@ char **lsh_split_line(char *line) if (position >= bufsize) { bufsize += LSH_TOK_BUFSIZE; - tokens = realloc(tokens, bufsize * sizeof(char*)); - if (!tokens) { - fprintf(stderr, "lsh: allocation error\n"); - exit(EXIT_FAILURE); - } + tokens = realloce(tokens, bufsize * sizeof(char*)); } token = strtok_quotes(NULL); diff --git a/src/main.c b/src/main.c index 427d316..ef88291 100755 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "main.h" #include "helper_functions.c" @@ -18,6 +20,11 @@ void lsh_loop(SHELL_OB *shell_obj){ //get pwd char *cwd= getenv("PWD"); char *pwd=basename(cwd); + if(pwd==NULL){ + fprintf(stderr,"lsh:pwd is null\n\n"); + exit(EXIT_FAILURE); + } + snprintf(shell_obj->prompt,sizeof(char) * 1024,"%s %s> ",shell_obj->pre_prompt,pwd); free(pwd); printf("%s",shell_obj->prompt); @@ -46,6 +53,18 @@ void lsh_loop(SHELL_OB *shell_obj){ } while (status); } +static char* call_realpath (char * argv0) { + char* resolved_path=(char*)malloce(sizeof(char)*PATH_MAX); + + if (realpath (argv0, resolved_path) == 0) { + fprintf (stderr, "lsh: realpath failed: %s\n", strerror (errno)); + exit(errno); + } + else { + return resolved_path; + } +} + int main(int argc, char **argv){ //set sh level char *shlvl = getenv("SHLVL"); @@ -56,6 +75,9 @@ int main(int argc, char **argv){ setenv("SHLVL",shlvl,1); } + char* prog_path = call_realpath(argv[0]); + setenv("SHELL",prog_path,1); + //Set The prompt SHELL_OB shell_obj; shell_obj.prompt = (char *)malloce(sizeof(char) * 1024); @@ -73,5 +95,6 @@ int main(int argc, char **argv){ free(shell_obj.prompt); free(shell_obj.pre_prompt); + free(prog_path); return EXIT_SUCCESS; } -- cgit v1.3