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 --- src/main.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/main.c') 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