There are several problems.
1. Your code looks like dog-food. The indentation is awful, and probably hiding your mistakes.
Code:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <ctype.h>
//need two pipes and string input
int main(int argc, char *argv[])
{
int pipe1[2];
int pipe2[2];
char input[100];
if (pipe(pipe1) == -1) { // if any issues creating the pipe
printf("pipe1 failed\n");
exit(1);
}
if (pipe(pipe2) == -1) { // if any issues creating the pipe
printf("pipe2 failed\n");
exit(1);
}
scanf("%s", input);
if (fork() < 0) {
printf("fork failed\n");
} else if (fork() == 0) { //that is child process to convert string to uppercase
char new[100]; //to save the uppercase string
//child only read so close writing end
close(pipe1[1]);
read(pipe1[0], new, 100);
int l = strlen(new);
for (int i = 0; i < l; i++) {
new[i] = toupper(new[i]);
}
close(pipe1[0]);
close(pipe2[0]);
write(pipe2[1], new, l + 1); //length of the string
close(pipe2[1]);
exit(0);
} else
(fork() > 0); //!! WTF is this!?
{
char new[100];
close(pipe1[0]);
write(pipe1[1], input, strlen(input) + 1);
close(pipe1[1]);
//once that is done child process starts so
wait(NULL);
close(pipe2[1]);
read(pipe2[0], new, 100);
printf("new string %s\n", new);
close(pipe2[0]);
}
}
2. You're creating more processes than you bargain for.
Code:
if (fork() < 0) {
printf("fork failed\n");
} else if (fork() == 0) { //that is child process to convert string to uppercase
NO!!!
This does not create one child and tests whether it's parent or child, it creates two children
Code:
pid_t p = fork();
if ( p < 0 } {
} else if ( p == 0 ) {
} else {
}
3. Line 46 -> (fork() > 0); //!! WTF is this!?
else clauses don't have conditions.
4. ISSUE : It only read one word and terminates as soon as reaches a first space/null
Well use fgets to read a whole line, instead of scanf %s, which stops at the first white space.