summaryrefslogtreecommitdiffstats
path: root/src/ft_history.c
blob: 7a5c7959fe9530405b6b14d1f49bb5c2665e69f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* ************************************************************************** */
/*                                                          LE - /            */
/*                                                              /             */
/*   ft_history.c                                     .::    .:/ .      .::   */
/*                                                 +:+:+   +:    +:  +:+:+    */
/*   By: rbousset <marvin@le-101.fr>                +:+   +:    +:    +:+     */
/*                                                 #+#   #+    #+    #+#      */
/*   Created: 2019/10/30 20:36:05 by rbousset     #+#   ##    ##    #+#       */
/*   Updated: 2019/10/30 20:36:07 by rbousset    ###    #+. /#+    ###.fr     */
/*                                                         /                  */
/*                                                        /                   */
/* ************************************************************************** */

#include <libft.h>
#include <minishell.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#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 c;
	int fd;
	size_t lines;
	size_t i;

	i = 0;
	if ((fd = open("joe-sh_history", O_CREAT | O_RDWR, 0644)) == -1)
		return (NULL);
	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
ft_history(char *arg)
{
	char *buff;
	int fd;
	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);
	read(fd, buff, info.st_size);
	ft_putendl_fd(arg, fd);
	free(buff);
	close(fd);
	return (0);
}