summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRudy Bousset <rbousset@z2r5p6.le-101.fr>2019-11-01 17:45:06 +0100
committerRudy Bousset <rbousset@z2r5p6.le-101.fr>2019-11-01 17:45:06 +0100
commitff258032adf915aa694feb9654e0a5e6019b3aba (patch)
treeeb88e61ef0c3729aa4df2d28448f74505c51949f /src
parentYO PICKED THE WRONG FOOL, HOUSE (diff)
download42-minishell-ff258032adf915aa694feb9654e0a5e6019b3aba.tar.gz
42-minishell-ff258032adf915aa694feb9654e0a5e6019b3aba.tar.bz2
42-minishell-ff258032adf915aa694feb9654e0a5e6019b3aba.tar.xz
42-minishell-ff258032adf915aa694feb9654e0a5e6019b3aba.tar.zst
42-minishell-ff258032adf915aa694feb9654e0a5e6019b3aba.zip
Now deals with history
Diffstat (limited to '')
-rw-r--r--src/ft_history.c70
-rw-r--r--src/ft_process_arg.c14
-rw-r--r--src/main.c6
3 files changed, 79 insertions, 11 deletions
diff --git a/src/ft_history.c b/src/ft_history.c
index 10f683a..7a5c795 100644
--- a/src/ft_history.c
+++ b/src/ft_history.c
@@ -13,6 +13,7 @@
#include <libft.h>
#include <minishell.h>
+#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
@@ -20,17 +21,77 @@
#include <sys/types.h>
#include <sys/stat.h>
+static size_t
+ft_count_lines(int fd)
+{
+ char c;
+ size_t lines;
+
+ lines = 0;
+ while (read(fd, &c, 1) > 0)
+ if (c == '\n')
+ lines++;
+ return (lines);
+}
+
+static size_t
+ft_last_line_len(int fd, size_t lines_max)
+{
+ char c;
+ size_t len;
+ size_t lines;
+
+ len = 0;
+ lines = 0;
+ while (read(fd, &c, 1) > 0)
+ {
+ if (c == '\n')
+ lines++;
+ if (lines == lines_max - 1)
+ break ;
+ }
+ while (read(fd, &c, 1) > 0)
+ len++;
+ return (len);
+}
+
char
*ft_get_last_line(void)
{
- // char *buff;
+ char *buff;
+ char c;
int fd;
- struct stat info;
+ size_t lines;
+ size_t i;
+ i = 0;
if ((fd = open("joe-sh_history", O_CREAT | O_RDWR, 0644)) == -1)
return (NULL);
- fstat(fd, &info);
- return ("qweqwe");
+ lines = ft_count_lines(fd);
+ close(fd);
+ if ((fd = open("joe-sh_history", O_CREAT | O_RDWR, 0644)) == -1)
+ return (NULL);
+ if (!(buff = (char*)malloc(ft_last_line_len(fd, lines) * sizeof(char))))
+ return (NULL);
+ close(fd);
+ if ((fd = open("joe-sh_history", O_CREAT | O_RDWR, 0644)) == -1)
+ return (NULL);
+ while (read(fd, &c, 1) > 0)
+ {
+ if (c == '\n')
+ i++;
+ if (i == lines - 1)
+ break ;
+ }
+ i = 0;
+ while (read(fd, &c, 1) > 0)
+ {
+ buff[i] = c;
+ i++;
+ }
+ buff[i - 1] = '\0';
+ close(fd);
+ return (buff);
}
int
@@ -49,6 +110,7 @@ ft_history(char *arg)
return (0);
read(fd, buff, info.st_size);
ft_putendl_fd(arg, fd);
+ free(buff);
close(fd);
return (0);
}
diff --git a/src/ft_process_arg.c b/src/ft_process_arg.c
index 75999b9..6a2d155 100644
--- a/src/ft_process_arg.c
+++ b/src/ft_process_arg.c
@@ -1,15 +1,19 @@
#include <libft.h>
+#include <stdlib.h>
#include <minishell.h>
#include <inttypes.h>
int
-ft_process_arg(const char *arg)
+ft_process_arg(char *arg)
{
char **com;
+ char *prev;
uint8_t i;
if (arg[0] == '\0')
return (0);
+ if (ft_strncmp(arg, "r", 1))
+ ft_history(arg);
i = 0;
com = ft_split(arg, ' ');
while (com[i])
@@ -20,6 +24,14 @@ ft_process_arg(const char *arg)
return (ft_echo(com, i));
else if (!ft_strncmp(com[0], "pwd", ft_strlen(com[0])))
return (ft_pwd());
+ else if (!ft_strncmp(com[0], "r", 1))
+ {
+ prev = ft_get_last_line();
+ ft_putendl(prev);
+ ft_history("r");
+ ft_process_arg(prev);
+ free(prev);
+ }
else if (!ft_strncmp(com[0], "./", 2))
ft_exec(com);
else
diff --git a/src/main.c b/src/main.c
index cf60fa2..372e04f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -43,15 +43,9 @@ int
if (c == '\n')
break ;
arg[i] = c;
- if (!ft_strncmp("", arg, 4))
- {
- arg = ft_get_last_line();
- break ;
- }
i++;
}
arg[i] = '\0';
- ft_history(arg);
ft_process_arg(arg);
}
free(arg);