Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <ctype.h>
void proc_uno();
void proc_dos();
int pipe_uno[2], pipe_dos[2];
int main(int argc, char ** argv)
{
int pid1, pid2, i;
getopt(argc, argv, "f:");
int fd = open(optarg, O_RDONLY);
pipe(pipe_uno);
pipe(pipe_dos);
char buffer[100] = "";
if((pid1 = fork()) == 0)
{
proc_uno();
return 0;
}
close(pipe_uno[0]);
if((pid2 = fork()) == 0)
{
proc_dos();
return 0;
}
close(pipe_dos[0]);
while((i = read(fd, buffer, 100)) != 0)
{
write(pipe_uno[1], buffer, 100);
write(pipe_dos[1], buffer, 100);
}
return 0;
}
void proc_uno()
{
int e, letras, i;
int palabras[21] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
char buffer[100] = "";
letras = -1;
i = 1;
close(pipe_uno[1]);
close(pipe_dos[0]);
close(pipe_dos[1]);
while(( i = read(pipe_uno[0], buffer, 100)) > 0)
{
for(e = 0; e < 100; e++)
{
if(buffer[e] != ' ' && buffer[e] != '.' && buffer[e] != ',' && buffer[e] != '\n')
letras++;
else
{
palabras[letras]++;
letras = -1;
}
}
}
for(e = 0; e < 21; e++)
{
if(palabras[e] != 0)
{
printf("Hay %d palabras de %d letras.\n", palabras[e], e+1);
}
}
}
void proc_dos()
{
close(pipe_dos[1]);
close(pipe_uno[1]);
close(pipe_uno[0]);
int e = 0, i = 0, x = 0;
char buffer[1000] = "";
char palabra[15] = "\0";
char aux[2] = {'a', '\0'};
while((i = read(pipe_dos[0], buffer, 1000)) > 0)
{
for(e = 0; e < i; e++)
{
if(buffer[e] != ' ' && buffer[e] != '.' && buffer[e] != ',' && buffer[e] != '\n')
{
aux[0] = buffer[e];
strcat(palabra, aux);
}
else
{
if(strcmp(palabra, "al") == 0 || strcmp(palabra, "los") == 0 || strcmp(palabra, "rey") == 0)
{
for(x = 0; x < strlen(palabra); x++)
{
buffer[e-1-x] = toupper(buffer[e-1-x]);
}
}
palabra[0] = '\0';
}
}
}
printf("%s", buffer);
}
Counting the words and printing them (proc_uno()) works fine. proc_dos() works only with tiny files, when I tested it with a 12.8 kB files it didn't work, sometines it doesn't print anything at all and sometines it prints less than the half of the file