Given the following
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
int main(void)
{
char name[] = "enter name:";
char line[BUFSIZ];
ssize_t n;
int fd;
if((fd = open("/dev/tty", O_RDWR)) < 0) {
fprintf(stderr, "open error\n");
exit(1);
}
if ((write(1, name, sizeof(name)-1)) != sizeof(name)-1) {
fprintf(stderr, "write error \n");
exit(1);
}
while ((n = read(0, line, BUFSIZ)) > 0) {
write(1, line, n);
}
close(fd);
exit(0);
}
[cdalten@localhost oakland]$ gcc -Wall pass.c -o pass
[cdalten@localhost oakland]$ more input
molly
When the program reads from the file input, the program terminates
right after it reads the data,
[cdalten@localhost oakland]$ ./pass < input
enter name:molly
[cdalten@localhost oakland]$
Why does this happen?