tgrekov-pipex
HIVE pipex May 2024
Loading...
Searching...
No Matches
here_doc.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* here_doc.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/04/26 12:01:21 by tgrekov #+# #+# */
9/* Updated: 2024/05/20 12:26:53 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include <unistd.h>
21#include "../../mandatory/utils/utils.h"
22#include <ft_printf.h>
23#include <get_next_line_bonus.h>
24#include <libft.h>
25
26static int _cmp(const char *s1, const char *s2)
27{
28 int len;
29 int len2;
30
31 len = ft_strlen(s1);
32 len2 = ft_strlen(s2) - 1;
33 if (len2 > len)
34 len = len2;
35 return (ft_strncmp(s1, s2, len));
36}
37
46int here_doc(char *limiter, int *in_out)
47{
48 int fds[2];
49 char *line;
50 size_t len;
51
52 if (pipe(fds) == -1)
53 return ((int) err("pipe()", 0));
54 in_out[0] = fds[0];
55 while (1)
56 {
57 ft_printf("pipex heredoc> ");
58 line = get_next_line(0);
59 if (!line)
60 return ((int) err("get_next_line()", 0));
61 len = ft_strlen(line);
62 if (!_cmp(limiter, line))
63 break ;
64 if (write(fds[1], line, len) < 0)
65 return ((int) err("write()", 0));
66 free(line);
67 }
68 free(line);
69 if (close(fds[1]))
70 return ((int) err("close()", 0));
71 return (1);
72}
int ft_printf(const char *format,...)
Prints and formats a variable set of arguments.
Definition ft_printf.c:252
void * err(const char *str, void *retval)
Wrapper around perror() that always returns retval.
Definition err.c:29
size_t ft_strlen(const char *str)
Get length of str.
Definition ft_strlen.c:28
int ft_strncmp(const char *s1, const char *s2, size_t n)
Compare up to n characters of string s1 with string s2.
Definition ft_strncmp.c:33
char * get_next_line(int fd)
Return the next segment of text ending in a newline or EOF from file descriptor fd.
int here_doc(char *limiter, int *in_out)
Capture multiline input from stdin and write to a pipe readable from in_out[0] until a line containin...
Definition here_doc.c:46