aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authoriceyrazor <iceyrazor@mailfence.com>2026-03-06 22:34:50 -0600
committericeyrazor <iceyrazor@mailfence.com>2026-03-06 22:34:50 -0600
commitf3d03e71ede8e94e7137777ab5ad5570c3d11b31 (patch)
treeddd17a5e3e1c40e645772cb9466169b98389985d /src/main.c
parent5964950ca5c018e1163fe308fa4bb17b9220b383 (diff)
some cleanup and shell var
- SHELL is now set - cwd now has realloc - realloc with error checking is now a fucntion like malloce
Diffstat (limited to 'src/main.c')
-rwxr-xr-xsrc/main.c23
1 files changed, 23 insertions, 0 deletions
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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <sys/param.h>
+#include <errno.h>
#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;
}