linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Marius Nita <marius@cs.pdx.edu>
To: Chuck Winters <cwinters@atl.lmco.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: opening standard streams - who does that?
Date: Mon, 8 Jul 2002 13:27:28 -0700	[thread overview]
Message-ID: <20020708132728.A27031@cs.pdx.edu> (raw)
In-Reply-To: <20020708135329.A3340@atl.lmco.com>; from cwinters@atl.lmco.com on Mon, Jul 08, 2002 at 01:53:29PM -0400

On Mon, Jul 08, 2002 at 01:53:29PM -0400, Chuck Winters wrote:
> On the same note, I have a program which uses another library.  Is it possible to
> redirect stderr to a file, and then later set it back to stderr.  I know I can close
> stderr, then reopen stderr to the file I want.  The problem is that when I run the 
> following code, I get a, "No Such File or Directory" error.
> 
> #include <stdlib.h>
> #include <stdio.h>
> 
> int main(void)
> {
> 	FILE *f = NULL;
> 
> 	fclose(stdout);
> 
> 	if( !(f = fopen("/dev/stdout", "w+")) ) {

This gives you a file not found because /dev/stdout is a symlink. Track down
the pts that it points to and open that. On my system it's /dev/pts/12

> 		perror("Opening stdout");
> 		exit(1);
> 	}
> 
> 	fprintf(stdout, "Hey\n");

This won't work because the file pointer "stdout" points to a closed stream.
(You closed it with "fclose(stdout);" above. Remember that "stdout" is just a
FILE * pointer defined in stdio.h.

Above, you opened /dev/pts/XX and pointed f to the new stream. If you say:

        fprintf(f, "Hey\n");

it will work. (Provided that you open the right device file)

> 	exit(0);
> }
> 
> Chuck Winters
> 
> On Sun, Jul 07, 2002 at 08:26:53AM +0100, Glynn Clements wrote:
> > 
> > Suriya Narayanan M S wrote:
> > 
> > > 	I want to know who opens the streams
> > > stdin, stdout, stderr in file-descriptors 1, 2, 3
> > 
> > 0, 1, 2
> > 
> > > and calls the process. I know (right or wrong?)
> > > that on a fork the filedescriptors are duplicated.
> > 
> > Yes.
> > 
> > > So bash or the shell can just forks a new process
> > > without assinging the fds. Is it the kernel
> > > which assigns 0 to the keyboard. How is this
> > > done?
> > 
> > For terminal logins, it's done by getty[1], which is run from the
> > inittab.
> > 
> > [1] "getty" is the generic term; the program may actually be called
> > something else, e.g. agetty, mgetty, uugetty, mingetty.
> > 
> > For telnet/ssh/rsh etc logins, it's done by the daemon (in.telnetd,
> > sshd, in.rshd etc).
> > 
> > Under X, terminal emulators (xterm etc) set up the descriptors. Note
> > that programs which aren't run from a terminal, but from the X session
> > scripts (~/.xsession, ~/.xinitrc) or from the window manager, may not
> > actually have stdin/stdout/stderr assigned.
> > 
> > -- 
> > Glynn Clements <glynn.clements@virgin.net>
> > -
> > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at  http://vger.kernel.org/majordomo-info.html
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

      reply	other threads:[~2002-07-08 20:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-06 15:30 opening standard streams - who does that? Suriya Narayanan M S
2002-07-07  7:26 ` Glynn Clements
2002-07-08 17:53   ` Chuck Winters
2002-07-08 20:27     ` Marius Nita [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020708132728.A27031@cs.pdx.edu \
    --to=marius@cs.pdx.edu \
    --cc=cwinters@atl.lmco.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).