aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
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;
}