aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/helper_functions.c11
-rw-r--r--src/lsh_builtins.c11
-rw-r--r--src/lsh_main_func.c12
-rw-r--r--src/lsh_split_line.c6
-rwxr-xr-xsrc/main.c23
5 files changed, 45 insertions, 18 deletions
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 <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;
}