Originally Posted by
MK27
Keep in mind each process has it's own stdin and stdout. If you had a forked process connected to a parent with a pipe, you write into the stdin of the pipe and it comes out of the stdout of the pipe, right? But if the child wrote into it's own stdin (usually you would close that) the same thing will happen.
Code:
fwrite("this",4,1,stdin);
Does the same thing you just observed.
i tried this...
Code:
#include<stdio.h>
int main()
{
fwrite("this",4,1,stdin);
perror("fwrite status");
}
output:
Code:
[c_d@localhost C scratchpad]$ gcc temp.c
[c_d@localhost C scratchpad]$ ./a.out
fwrite status: Bad file descriptor
[c_d@localhost C scratchpad]$
no it did not mimic the system-call.
Code:
#include<unistd.h>
#include<fcntl.h>
int main()
{
close(STDOUT_FILENO);
if((write(STDIN_FILENO,"arrgh!",6))==-1)
{
perror("stdin");
}
if((write(STDOUT_FILENO,"arrgh!",6))==-1)
{
perror("stdout");
}
}
output:
Code:
[c_d@localhost C scratchpad]$ gcc temp.c
[c_d@localhost C scratchpad]$ ./a.out
arrgh!stdout: Bad file descriptor
[c_d@localhost C scratchpad]$ ./a.out <temp.c
stdin: Bad file descriptor
stdout: Bad file descriptor
[c_d@localhost C scratchpad]$
so process stdin stdout fd points /dev/tty under "normal circumstances"
this is what i found about stdin...
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
int main(void)
{
struct stat st;
char *whichtty;
if ( !isatty(STDIN_FILENO) )
{
(void) fprintf(stderr, "%s\n", "stdin is not connected to a tty device.");
}
else
{
if ( (whichtty = ttyname(STDIN_FILENO)) == NULL )
{
perror("ttyname");
return 1;
}
else
{
(void) fprintf(stderr, "stdin is connected to tty device %s.\n", whichtty);
}
}
if ( fstat(STDIN_FILENO, &st) == -1 )
{
perror("file status");
return 1;
}
else
{
if ( st.st_mode & S_IWUSR )
{
(void) fprintf(stderr, "%s\n", "stdin appears to be writeable by the user.");
}
else
{
(void) fprintf(stderr, "%s\n", "stdin does not appear to be writeable by the user.");
}
}
return 0;
}
output:
Code:
[c_d@localhost C scratchpad]$ gcc temp.c
[c_d@localhost C scratchpad]$ ./a.out
stdin is connected to tty device /dev/pts/3.
stdin appears to be writeable by the user.
[c_d@localhost C scratchpad]$
now, when i do this
Code:
[c_d@localhost C scratchpad]$ ./a.out <temp.c
stdin is not connected to a tty device.
stdin appears to be writeable by the user.
[c_d@localhost C scratchpad]$ chmod -w temp.c #remove write perm
[c_d@localhost C scratchpad]$ ./a.out <temp.c
stdin is not connected to a tty device.
stdin does not appear to be writeable by the user.
[c_d@localhost C scratchpad]$ chmod +w temp.c
fwrite() may invoke write() eventually, but there's lots of stuff going on inbetween...
why so much difference FILE *stdin(stream) and STDIN_FILENO(descriptor of the same stdin stream)?