summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-09-19 13:55:08 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-09-19 13:55:08 +0200
commit7eb6a40ab20a4970822a1d34fbb9a8dbc87cd016 (patch)
tree5e1baac90873e7cefc1f45ec0cfdcbbe4f695309
parenth still in progress (diff)
download42-minishell-7eb6a40ab20a4970822a1d34fbb9a8dbc87cd016.tar.gz
42-minishell-7eb6a40ab20a4970822a1d34fbb9a8dbc87cd016.tar.bz2
42-minishell-7eb6a40ab20a4970822a1d34fbb9a8dbc87cd016.tar.xz
42-minishell-7eb6a40ab20a4970822a1d34fbb9a8dbc87cd016.tar.zst
42-minishell-7eb6a40ab20a4970822a1d34fbb9a8dbc87cd016.zip
In progress
-rw-r--r--src/b_h.c53
-rw-r--r--src/m_loop.c1
-rw-r--r--src/s_struct.h1
3 files changed, 47 insertions, 8 deletions
diff --git a/src/b_h.c b/src/b_h.c
index 0e968ff..2c4f7eb 100644
--- a/src/b_h.c
+++ b/src/b_h.c
@@ -20,27 +20,64 @@
#include "s_struct.h"
#include "u_utils.h"
-static void b_print_history(const char arg[], uint64_t max_hist, t_msh *msh)
+static uint64_t b_get_total_hist(t_msh *msh)
{
- char *prev_hist;
+ uint64_t total;
+ char *ptr;
+
+ total = 0;
+ if (msh->prev_hist != NULL)
+ {
+ ptr = msh->prev_hist;
+ while (*ptr != C_NUL)
+ {
+ if (*ptr == C_LF)
+ total += 1;
+ ptr++;
+ }
+ }
+ ptr = msh->curr_hist;
+ while (*ptr != C_NUL)
+ {
+ if (*ptr == C_LF)
+ total += 1;
+ ptr++;
+ }
+ return (total);
+}
+
+static void b_print_history(uint64_t asked, uint64_t total, t_msh *msh)
+{
+ char *hist;
char *tok;
uint32_t i;
- (void)arg;
+ (void)total;
+ (void)asked;
i = 1;
- if (msh->prev_hist != NULL)
+ if (msh->hist != NULL)
{
- if ((prev_hist = ft_strdup(msh->prev_hist)) == NULL)
+ if ((hist = ft_strdup(msh->prev_hist)) == NULL)
return ;
- tok = ft_strtok(prev_hist, "\n");
+ tok = ft_strtok(hist, "\n");
while (tok != NULL)
{
ft_printf("%5u %s\n", i, tok);
i++;
tok = ft_strtok(NULL, "\n");
}
- ft_memdel((void*)&prev_hist);
+ ft_memdel((void*)&hist);
+ }
+ if ((hist = ft_strdup(msh->curr_hist)) == NULL)
+ return ;
+ tok = ft_strtok(hist, "\n");
+ while (tok != NULL)
+ {
+ ft_printf("%5u %s\n", i, tok);
+ i++;
+ tok = ft_strtok(NULL, "\n");
}
+ ft_memdel((void*)&hist);
}
static t_bool b_check_numeric(const char arg[])
@@ -77,7 +114,7 @@ uint8_t b_h(char *args[], t_msh *msh)
}
else
{
- b_print_history(*args, msh);
+ b_print_history(ft_atoi(*args), b_get_total_hist(msh), msh);
}
return (0);
}
diff --git a/src/m_loop.c b/src/m_loop.c
index 0d90b0a..8f3d8d3 100644
--- a/src/m_loop.c
+++ b/src/m_loop.c
@@ -129,6 +129,7 @@ uint8_t
int8_t gnl;
gnl = 1;
+ msh->curr_hist = hist;
while (gnl > 0)
{
if (fd == STDIN_FILENO)
diff --git a/src/s_struct.h b/src/s_struct.h
index f9b6ca3..c9f86e1 100644
--- a/src/s_struct.h
+++ b/src/s_struct.h
@@ -82,6 +82,7 @@ typedef struct s_msh
struct s_lalias *alias;
char **envp;
char *prev_hist;
+ char *curr_hist;
char ps[4][255];
char env_fork_tmp[128][4096];
char sqb_ref[FT_ID_SQB_COUNT][4];