/*
 * pipe.c
 * 
 * Copyright 2025 osboxes <osboxes@osboxes>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>

int main(int argc, char **argv) {
	int p1[2];pipe(p1);
	pid_t pid = fork();
	if ( pid == 0 ) {
		char c;char tmp[100];
		close(p1[1]);
		int n=1,i=0;
		while(n) {
			n = read(p1[0],&c,1);
			if ( n == 1 ) {
				if ( c != '\n' )	tmp[i++]=c; 
				else {  					
					tmp[i]='\0'; 
					printf("%s\n",tmp);
					i=0;
				}
			}
		}
		close(p1[0]);
	} else {
		close(p1[0]);
		char *s = "Junta experiencia en la vida\nHasta pa dar y prestar\nQuien la tiene que pasar\nEntre sufrimiento y llanto\nPorque nada enseña tanto\ncomo el sufrir y el llorar";
		write(p1[1],s,strlen(s)+1);
		close(p1[1]);
		wait(0);
	}
	return 0;
}

