summaryrefslogtreecommitdiffstats
path: root/src/b_h.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/b_h.c')
-rw-r--r--src/b_h.c41
1 files changed, 29 insertions, 12 deletions
diff --git a/src/b_h.c b/src/b_h.c
index 2c4f7eb..cf6ad3f 100644
--- a/src/b_h.c
+++ b/src/b_h.c
@@ -46,34 +46,48 @@ static uint64_t b_get_total_hist(t_msh *msh)
return (total);
}
-static void b_print_history(uint64_t asked, uint64_t total, t_msh *msh)
+static uint32_t b_print_prev_history(uint64_t asked, uint64_t total, t_msh *msh)
{
- char *hist;
- char *tok;
- uint32_t i;
+ char *hist;
+ char *tok;
+ int64_t to_print;
+ int64_t i;
- (void)total;
- (void)asked;
+ to_print = (total - asked < 0) ? (0) : (total - asked);
i = 1;
- if (msh->hist != NULL)
+ if (msh->prev_hist != NULL)
{
if ((hist = ft_strdup(msh->prev_hist)) == NULL)
- return ;
+ return (i);
tok = ft_strtok(hist, "\n");
while (tok != NULL)
{
- ft_printf("%5u %s\n", i, tok);
+ if (i > to_print)
+ ft_printf("%5lu %s\n", i, tok);
i++;
tok = ft_strtok(NULL, "\n");
}
ft_memdel((void*)&hist);
}
+ return (i);
+}
+
+static void b_print_history(uint64_t asked, uint64_t total, t_msh *msh)
+{
+ char *hist;
+ char *tok;
+ int64_t to_print;
+ int64_t i;
+
+ to_print = (total - asked < 0) ? (0) : (total - asked);
+ i = b_print_prev_history(asked, total, msh);
if ((hist = ft_strdup(msh->curr_hist)) == NULL)
return ;
tok = ft_strtok(hist, "\n");
while (tok != NULL)
{
- ft_printf("%5u %s\n", i, tok);
+ if (i > to_print)
+ ft_printf("%5lu %s\n", i, tok);
i++;
tok = ft_strtok(NULL, "\n");
}
@@ -108,13 +122,16 @@ uint8_t b_h(char *args[], t_msh *msh)
if (argc == 1 && b_check_numeric(*args) == FALSE)
{
ft_dprintf(STDERR_FILENO,
- "minishell: history: %s: numeric argument required",
+ "minishell: history: %s: numeric argument required\n",
*args);
return (1);
}
else
{
- b_print_history(ft_atoi(*args), b_get_total_hist(msh), msh);
+ if (argc == 1)
+ b_print_history(ft_atoi(*args), b_get_total_hist(msh), msh);
+ else
+ b_print_history(0, 0, msh);
}
return (0);
}