diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ft_history.c | 128 | ||||
-rw-r--r-- | src/main.c | 30 |
2 files changed, 67 insertions, 91 deletions
diff --git a/src/ft_history.c b/src/ft_history.c index 7a5c795..a700c8b 100644 --- a/src/ft_history.c +++ b/src/ft_history.c @@ -21,94 +21,88 @@ #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 *buff; - char c; - int fd; - size_t lines; - size_t i; + char *line; + int ret; + int fd; - 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) + while ((ret = get_next_line(fd, &line)) > 0) { - buff[i] = c; - i++; + ft_putendl(line); + free(line); } - buff[i - 1] = '\0'; close(fd); - return (buff); + return (line); } +/* +** Prints user-given line into +** joe-sh_hisotry +*/ + int ft_history(char *arg) { char *buff; int fd; - struct stat info; + int ret; + // 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); + // 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); + } ft_putendl_fd(arg, fd); free(buff); close(fd); @@ -21,34 +21,16 @@ int main(void) { - char c; - char *arg; - uint8_t i; + char *arg; + int gnlret; - i = 0; - arg = NULL; - while (1) + ft_putstr(FT_PS1); + while ((gnlret= get_next_line(STDIN_FILENO, &arg)) > 0) { - ft_putstr(FT_PS1); - if (arg) - { - free(arg); - arg = NULL; - } - if (!(arg = (char*)ft_calloc(129, sizeof(char)))) - return (1); - i = 0; - while (read(STDIN_FILENO, &c, 1) > 0) - { - if (c == '\n') - break ; - arg[i] = c; - i++; - } - arg[i] = '\0'; ft_process_arg(arg); + free(arg); + ft_putstr(FT_PS1); } free(arg); - arg = NULL; return (0); } |