comp.unix.programmer

ISO C libs with character EOF; Terminal with parent and child process


Hello, I have questions after read Section 15.3 in APUE(Advanced Programming in UNIX Environment).
1st.
/* start of file program1.c */
#include "apue.h"
#include <ctype.h>
int
main(void)
{
int c;
while ((c = getchar()) != EOF) {
if (isupper(c))
c = tolower(c);
if (putchar(c) == EOF)
err_sys("output error");
if (c == '\n')
fflush(stdout);
}
exit(0);
}
/* end of file program1.c */
If I type a string that ends with '\n', it operates as excepted;
If I type a newline(immediately after '\n') that consists and only consists of EOF, it exits as excepted;
But if I type a string that ends with EOF, it operates as if I typed a '\n', and so it echoes, the only difference is that it doesn't change to a new line.
So, why it operates differently when encounters a newline that consists and only consists of EOF and a newline that starts with a normal character and ends with EOF?
It must have something to do with "line buffered", but I cannot understand.
2nd.
/* start of file program2.c */
#include "apue.h"
#include <sys/wait.h>
int
main(void)
{
char line[MAXLINE];
FILE *fpin;
if ((fpin = popen("./program1", "r")) == NULL)
err_sys("popen error");
for ( ; ; ) {
fputs("prompt> ", stdout);
fflush(stdout);
if (fgets(line, MAXLINE, fpin) == NULL) /* read from pipe */
break;
if (fputs(line, stdout) == EOF)
err_sys("fputs error to pipe");
}
if (pclose(fpin) == -1)
err_sys("pclose error");
putchar('\n');
exit(0);
}
/* end of file program2.c */
We compile the program1.c to program1, which is invoked in program2.c in the same directory.
After invoking popen(), is the terminal associated with both stdins, stdouts, and stderrs of the parent process and the child process? Because whataver we type and whatever it displays are on the single terminal.
I hate GFW!!




Written by wy 08/10/2011 23.18.27
Check some pics on this site!
23/05/2012 22.31.07