From 922424abd400d87698e2b8c37e06974d16ad28a0 Mon Sep 17 00:00:00 2001
From: Dm1tr1y147 <me@dmitriy.icu>
Date: Sat, 11 Jul 2020 17:57:58 +0500
Subject: [PATCH] Added path display in prompt, and yeah, solved some memory
 errors and added some new (I hate valgrind)

---
 README.md       |  3 ++-
 include/shell.h |  3 +++
 src/complete.c  |  3 +++
 src/shell.c     | 18 ++++++++++++++----
 src/utils.c     |  5 +++--
 5 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/README.md b/README.md
index 1a6027f..d5d84cd 100644
--- a/README.md
+++ b/README.md
@@ -19,4 +19,5 @@ Work is still in porgress, buf when you will see a "finished" topic assigned to
 * Replace linux `echo` command with builtin one with support of environmental variables
 * Environmental variables
 * `Ctrl+Z` running programm with `fd`
-* Getting commands path from system `PATH` environment variable 
\ No newline at end of file
+* Getting commands path from system `PATH` environment variable 
+* Create global struture for shell to save data between commands
\ No newline at end of file
diff --git a/include/shell.h b/include/shell.h
index 5a441a0..8bb3459 100644
--- a/include/shell.h
+++ b/include/shell.h
@@ -1,6 +1,9 @@
 #ifndef _SHELL_H
 #define _SHELL_H
 
+#define _GNU_SOURCE
+
+#include <features.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
diff --git a/src/complete.c b/src/complete.c
index 6e6ebda..8df2a37 100644
--- a/src/complete.c
+++ b/src/complete.c
@@ -90,6 +90,8 @@ size_t get_complete_options(char ***opts, char *line, char **to_complete)
             curr_pos = strcat(curr_pos, folders[i]);
         }
         sz = get_dir_list(opts, curr_pos, 0);
+
+        free(curr_pos);
     }
     else
     {
@@ -132,6 +134,7 @@ size_t filter_options(char ***comp_list, size_t *size, char *filter_string)
     *size = list_strings_containing(child_dirs_root, last_option, comp_list);
 
     free_tree(child_dirs_root);
+    free_str_arr(folders);
 
     return *size;
 }
\ No newline at end of file
diff --git a/src/shell.c b/src/shell.c
index 26940af..2ea5232 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -1,6 +1,7 @@
 #include "../include/shell.h"
 #include "../include/input.h"
 #include "../include/output.h"
+#include "../include/utils.h"
 
 /* Global definitions */
 char *builtin[] = {
@@ -33,7 +34,8 @@ void process_command()
         status = execute(args);
 
         free(line);
-        free(args);
+        free(prompt);
+        free_str_arr(args);
     } while (status);
 }
 
@@ -47,11 +49,11 @@ int process_line(char *line, char ***args)
 {
     int buff_size = ARG_SIZE, pos = 0;
     *args = malloc(buff_size * sizeof(char *));
-    char *tok = NULL, *rest = strdup(line);
+    char *tok = NULL, *rest = strdup(line), *free_rest = rest;
 
     while ((tok = strtok_r(rest, " ", &rest)) != NULL)
     {
-        (*args)[pos] = tok;
+        (*args)[pos] = strdup(tok);
         pos++;
 
         if (pos > buff_size)
@@ -66,6 +68,8 @@ int process_line(char *line, char ***args)
         }
     }
 
+    free(free_rest);
+
     (*args)[pos] = NULL;
     return pos;
 }
@@ -156,7 +160,13 @@ int sh_exit(char **args)
 
 char *compose_prompt()
 {
-    char *prompt = strdup("");
+    char *prompt = strdup("\n");
+
+    char *full_path = get_current_dir_name();
+    prompt = realloc(prompt, strlen(prompt) + strlen(full_path) + 2);
+    prompt = strcat(prompt, full_path);
+
+    free(full_path);
 
     prompt = realloc(prompt, strlen(prompt) + 4);
     if (getuid() == 0)
diff --git a/src/utils.c b/src/utils.c
index 2b9e6cd..5fc1fff 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -50,6 +50,7 @@ int sep_string(char *line, char ***toks, char *sep)
 {
     free(*toks);
     char *tmp_line = strdup(line);
+    char *free_tmp_line = tmp_line; 
     int n = 0;
     *toks = malloc(sizeof(char *) * n);
 
@@ -61,7 +62,7 @@ int sep_string(char *line, char ***toks, char *sep)
         (*toks)[n - 1] = strdup(tmp);
     }
 
-    free(tmp_line);
+    free(free_tmp_line);
 
     return n;
 }
@@ -83,7 +84,7 @@ char *trim_string(char **str)
 
 void free_str_arr(char **arr)
 {
-    if (arr)
+    if (arr[0] != NULL)
         for (int i = 0; i < sizeof(arr) / sizeof(char *); i++)
             free(arr[i]);
     free(arr);