I'm writing the following code on HP-UX:
The "get" command basically prints the contents of a text file to the screen. The get is piped through a "more". When the text fills more than a screens worth, it waits for the user to press a button, or quit. If they choose to quit (with the Q button), "more" terminates but the parent (child?) process reports a broken pipe error. If ignore the SIGPIPE signal, I get an fgets error because the reading process no longer exists that the writing process is writing to. (Or is it the other way round? )
How can I trap and handle the broken pipe so the process terminates cleanly?
Code:
int pipefd[2],
ret_code,
status;
pid_t pid;
pipe (pipefd);
if (l_debug)
{
printf("In ViewFile\n");
fflush(stdout);
}
if ((pid = fork()) == (pid_t)0)
{
close(1);
dup(pipefd[1]);
close(pipefd[0]);
execlp("get","get","-sp",SccsFileName,(char *)0);
}
else if (pid < (pid_t)0)
{
printf("[%d]: Unable to create fork process.\n",__LINE__-9);
return 1;
}
else if (pid > (pid_t)0)
{
close(0);
dup(pipefd[0]);
close(pipefd[1]);
execlp("more","more",(char *)0);
}
return 0;
The "get" command basically prints the contents of a text file to the screen. The get is piped through a "more". When the text fills more than a screens worth, it waits for the user to press a button, or quit. If they choose to quit (with the Q button), "more" terminates but the parent (child?) process reports a broken pipe error. If ignore the SIGPIPE signal, I get an fgets error because the reading process no longer exists that the writing process is writing to. (Or is it the other way round? )
How can I trap and handle the broken pipe so the process terminates cleanly?