summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--joe-sh_history82
-rw-r--r--src/ft_history.c121
2 files changed, 141 insertions, 62 deletions
diff --git a/joe-sh_history b/joe-sh_history
index ed7c777..fdf941c 100644
--- a/joe-sh_history
+++ b/joe-sh_history
@@ -1,9 +1,75 @@
-wqewqrqrq
-qweqrqrqr
-qwe
-qwe
-qwewq
-qwe
-wqerqrqrqr
-qwe
+r
+weqeqeqwe
+r
+qr
+r
+r
+r
+r
qwe
+exit
+weqeqrqwr
+r
+r
+r
+r
+r
+r
+r
+r
+q
+exit
+clear
+r
+qwerqrqrqr
+ls -l
+r
+ls -
+r
+ls
+r
+ls
+r
+l
+r
+r
+r
+r
+r
+exit
+qwrqrqwr
+r
+qwrqrqwr
+weqqwrqrqr
+r
+weqqwrqrqr
+r
+weqqwrqrqr
+r
+weqqwrqrqr
+r
+weqqwrqrqr
+ls -l
+r
+ls -l
+r
+ls -l
+r
+ls -l
+r
+ls -l
+r
+ls -l
+r
+ls -l
+r
+ls -l
+r
+ls -l
+r
+ls -l
+r
+ls -l
+exit
+/bin/ls -lG
+/bin/ls -l
diff --git a/src/ft_history.c b/src/ft_history.c
index a700c8b..936b9a2 100644
--- a/src/ft_history.c
+++ b/src/ft_history.c
@@ -21,61 +21,83 @@
#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);
-// }
-//
+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 *line;
- int ret;
+ char c;
int fd;
+ size_t lines;
+ size_t i;
if ((fd = open("joe-sh_history", O_CREAT | O_RDWR, 0644)) == -1)
return (NULL);
- while ((ret = get_next_line(fd, &line)) > 0)
+ i = 0;
+ lines = ft_count_lines(fd);
+ close(fd);
+ if ((fd = open("joe-sh_history", O_CREAT | O_RDWR, 0644)) == -1)
+ return (NULL);
+ if (!(line = (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)
{
- ft_putendl(line);
- free(line);
+ if (c == '\n')
+ i++;
+ if (i == lines - 1)
+ break ;
}
+ i = 0;
+ while (read(fd, &c, 1) > 0)
+ {
+ line[i] = c;
+ i++;
+ }
+ line[i - 1] = '\0';
close(fd);
return (line);
}
/*
** Prints user-given line into
-** joe-sh_hisotry
+** joe-sh_hisotry file
*/
int
@@ -84,27 +106,18 @@ ft_history(char *arg)
char *buff;
int fd;
int ret;
- // struct stat info;
+ struct stat info;
if (!*arg)
return (0);
if ((fd = open("joe-sh_history", O_CREAT | O_RDWR, 0644)) == -1)
return (1);
- // fstat(fd, &info);
- // if (!(buff = (char*)malloc(info.st_size * sizeof(char))))
- // return (0);
- // ret = read(fd, buff, info.st_size);
- ret = get_next_line(fd, &buff);
- free(buff);
- while (ret)
- {
- ret = get_next_line(fd, &buff);
- if (ret == 0)
- break ;
- free(buff);
- }
+ fstat(fd, &info);
+ if (!(buff = (char*)malloc(info.st_size * sizeof(char))))
+ return (0);
+ ret = read(fd, buff, info.st_size);
ft_putendl_fd(arg, fd);
free(buff);
close(fd);
- return (0);
+ return (ret);
}