It is currently Fri, 28 Nov 2014 18:12:00 GMT



 
Author Message
 UNIX file descriptor for stdin, stdout and stderr
Hello
  Just want to make sure that for every process, the file descriptor for
stdin is ALWAYS 0, for stdout 1 and for stderr 2.

Thanks

TCS



 Sun, 28 Dec 2003 23:26:18 GMT   
 UNIX file descriptor for stdin, stdout and stderr
On Wed, 11 Jul 2001 08:26:18 -0700, TC Shen <techengs...@yahoo.com>
wrote:

Yes. FD 0 is always stdin
     FD 1 is always stdout
     FD 2 is always stderr

Lew Pitcher, Information Technology Consultant, Toronto Dominion Bank Financial Group
(Lew_Pitc...@td.com)

(Opinions expressed are my own, not my employer's.)



 Sun, 28 Dec 2003 23:31:11 GMT   
 UNIX file descriptor for stdin, stdout and stderr
        I believe that you are assuming that someone hasn't closed
those fds and then opened new files with the same fds.

Speaking only for myself,

Joe Durusau



 Mon, 29 Dec 2003 02:54:53 GMT   
 UNIX file descriptor for stdin, stdout and stderr
On Wed, 11 Jul 2001 11:54:53 -0700, Joe Durusau

OK, let me rephrase

  stdin  is always FD 0 (no matter what file FD 0 represents)
  stdout is always FD 1 (no matter what file FD 1 represents)
  stderr is always FD 2 (no matter what file FD 2 represents)

Lew Pitcher, Information Technology Consultant, Toronto Dominion Bank Financial Group
(Lew_Pitc...@td.com)

(Opinions expressed are my own, not my employer's.)



 Mon, 29 Dec 2003 01:04:49 GMT   
 UNIX file descriptor for stdin, stdout and stderr

freopen() will always guarantee this?  e.g.

        fclose(stdin);
        freopen(somepath, mode, stdout);

will end up with stdout still using fd 1 and not fd 0?

Joe Seigh



 Mon, 29 Dec 2003 17:47:50 GMT   
 UNIX file descriptor for stdin, stdout and stderr

Yes.

--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576



 Wed, 31 Dec 2003 10:10:03 GMT   
 UNIX file descriptor for stdin, stdout and stderr

To put it _another_ way...

stdin is just another name for FD 0
stdout is just another name for FD 1
stderr is just another name for FD 2

By definition, anything going to FD 0 is going to stdin
By definition, anything coming from FD 1 is coming from stdout
By definition, anything coming from FD2 is coming from stderr

It doesn't matter whether you close/open any of these, redirect them, or
rename them, stdin/stdout/stderr are just _names_ FOR FD0, FD1, and FD2
respectively.

--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576



 Wed, 31 Dec 2003 10:13:17 GMT   
 UNIX file descriptor for stdin, stdout and stderr

  file descriptor is a concept of OS level so that I am trying not to
use any C-library function calls.  fclose() and freopen() are C-library
function calls, aren't they?

Thanks

TCS



 Sat, 03 Jan 2004 00:33:26 GMT   
 UNIX file descriptor for stdin, stdout and stderr
On Mon, 16 Jul 2001 09:33:26 -0700, TC Shen

Well, all program call interfaces to a Unix system are through C
library functions. ;-)

However, it is true to say that the f*() calls (fopen(), freopen(),
etc) conform in parameters and behaviour to the C standard libary
definition, while the open()/close()/etc. calls are not defined by the
C standard.

Having said that, in Unix implementations, the f*() family of calls
use the Unix native I/O calls (open(), etc) and Unix file descriptors
under the covers.

Now, the Unix implementation of the C f*() family of functions
conforms to both the ISO/ANSI C specification, *and* the appropriate
Unix specification (POSIX, SUS, BSD, SVID, etc.). Naturally, since all
these specifications were originally derived from Unix behaviour,
there is a predisposition towards the Unix way of doing things.

The C standard defines that there will be three 'files' preopened for
the program: stdin, stdout, and stderr. The Unix standard says that
STDIN is FD 0, STDOUT is FD 1, and STDERR is FD 2. The implementation
of the f*() functions mates the ISO C standard to the Unix standard,
and thus stdin _is always_ FD 0 (on a Unix platform), stdout _is
always_ FD 1 (on a Unix platform) and stderr _is always_ FD 2 (on a
Unix platform).

freopen() is _defined and guaranteed_ to use the same FD as the
original file used, so freopen("/dev/null","w",stdout); will result in
a new file being associated with stdout _and_ FD 1 (not difficult,
because, in Unix, FD 1 _is_ stdout). If it doesn't behave that way,
then you have a broken implementation of freopen().

Lew Pitcher, Information Technology Consultant, Toronto Dominion Bank Financial Group
(Lew_Pitc...@td.com)

(Opinions expressed are my own, not my employer's.)



 Sat, 03 Jan 2004 01:00:15 GMT   
 UNIX file descriptor for stdin, stdout and stderr

Up until now I have given you the benefit of the doubt and assumed
that when, earlier in this thread, you have said "stdout" you were
using it as short hand for "standard output", and you weren't
actually referring to the C identifier declared as "FILE *stdout"
in <stdio.h>.

Now with this reference to the f*() functions it seems that you
really did mean the C identifier, in which case I'm afraid you
are wrong.

No.  freopen() is required by the standards to assign the lowest
available file descriptor to the newly opened stream, same as fopen().

No.  If FD 0 is available, then freopen("/dev/null","w",stdout)
will result in fileno(stdout) being 0.

Try the following code on any POSIX/UNIX conforming system, and
it will output "fileno(stdout) = 0".

#include <stdio.h>

int main()
{
        fclose(stdin);
        freopen("/dev/null", "w", stdout);
        fprintf(stderr, "fileno(stdout) = %d\n", fileno(stdout));

        return 0;

--
Geoff Clare                         g...@unisoft.com
UniSoft Limited, London, England.   g...@root.co.uk



 Sat, 03 Jan 2004 21:00:46 GMT   
 UNIX file descriptor for stdin, stdout and stderr
In article <ggmc5a....@unisoft.com>, Geoff Clare  <g...@unisoft.com> wrote:
...

Correct.  The effect of the freopen() function is (to set):

        fileno(stream) = fdUponWhichTheSpecifiedFileHasJustBeenOpened



 Thu, 15 Jan 2004 02:23:52 GMT   
 
   [ 11 post ] 

Similar Threads

1. Establishing new streams for file descriptors stdin, stdout, stderr

2. redirecting stdout,stderr and stdin to /dev/null

3. dup2() and closing stdin, stdout and stderr

4. IP Address for stdin, stdout, stderr

5. stdin stdout stderr portable???

6. redirecting stdin, stdout, stderr (like inetd)

7. logging stdin/stdout/stderr

8. Utility for logging stdin, stdout, and stderr

9. redirect stdin, stdout, stderr separately in tcsh

10. Color - stdin, stdout, stderr ????


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software