Imported upstream version 1.5
[manpages-zh.git] / raw / man3 / stdin.3
blob42d027175bd8c4ccc8ff20607914aa1ab6455439
1 .\" From dholland@burgundy.eecs.harvard.edu Tue Mar 24 18:08:15 1998
2 .\"
3 .\" This man page was written in 1998 by David A. Holland
4 .\" and placed in the Public Domain. Polished a bit by aeb.
5 .\"
6 .Dd March 24, 1998
7 .Dt STDIN 3
8 .Os "Linux 2.0"
9 .Sh NAME
10 .Nm stdin ,
11 .Nm stdout ,
12 .Nm stderr
13 .Nd standard I/O streams
14 .Sh SYNOPSIS
15 .Fd #include <stdio.h>
16 .Fd extern FILE *stdin;
17 .Fd extern FILE *stdout;
18 .Fd extern FILE *stderr;
19 .Sh DESCRIPTION
20 Under normal circumstances every Unix program has three streams opened
21 for it when it starts up, one for input, one for output, and one for
22 printing diagnostic or error messages. These are typically attached to
23 the user's terminal (see
24 .Xr tty 4 )
25 but might instead refer to files or other devices, depending on what
26 the parent process chose to set up. (See also the ``Redirection'' section of
27 .Xr sh 1 .)
28 .Pp
29 The input stream is referred to as ``standard input''; the output stream is
30 referred to as ``standard output''; and the error stream is referred to
31 as ``standard error''. These terms are abbreviated to form the symbols
32 used to refer to these files, namely
33 .Nm stdin ,
34 .Nm stdout ,
35 and
36 .Nm stderr .
37 .Pp
38 Each of these symbols is a
39 .Xr stdio 3
40 macro of type pointer to FILE, and can be used with functions like
41 .Xr fprintf 3
43 .Xr fread 3 .
44 .Pp
45 Since FILEs are a buffering wrapper around Unix file descriptors, the
46 same underlying files may also be accessed using the raw Unix file
47 interface, that is, the functions like
48 .Xr read 2
49 and
50 .Xr lseek 2 . 
51 The integer file descriptors associated with the streams
52 .Nm stdin ,
53 .Nm stdout ,
54 and
55 .Nm stderr
56 are 0, 1, and 2, respectively. The preprocessor symbols STDIN_FILENO,
57 STDOUT_FILENO, and STDERR_FILENO are defined with these values in 
58 <unistd.h>.
59 .Pp
60 Note that mixing use of FILEs and raw file descriptors can produce
61 unexpected results and should generally be avoided.
62 (For the masochistic among you: POSIX.1, section 8.2.3, describes
63 in detail how this interaction is supposed to work.)
64 A general rule is that file descriptors are handled in the kernel,
65 while stdio is just a library. This means for example, that after an
66 exec, the child inherits all open file descriptors, but all old streams
67 have become inaccessible. 
68 .Pp
69 Since the symbols
70 .Nm stdin ,
71 .Nm stdout ,
72 and
73 .Nm stderr
74 are specified to be macros, assigning to them is non-portable.
75 The standard streams can be made to refer to different files
76 with help of the library function
77 .Xr freopen 3 ,
78 specially introduced to make it possible to reassign
79 .Nm stdin ,
80 .Nm stdout ,
81 and
82 .Nm stderr .
83 The standard streams are closed by a call to
84 .Xr exit 3
85 and by normal program termination.
86 .Sh SEE ALSO
87 .Xr sh 1 ,
88 .Xr csh 1 ,
89 .Xr open 2 ,
90 .Xr fopen 3 ,
91 .Xr stdio 3
92 .Sh CONSIDERATIONS
93 The stream
94 .Nm stderr
95 is unbuffered. The stream
96 .Nm stdout
97 is line-buffered when it points to a terminal. Partial lines will not
98 appear until
99 .Xr fflush 3
101 .Xr exit 3
102 is called, or a newline is printed. This can produce unexpected
103 results, especially with debugging output.
104 The buffering mode of the standard streams (or any other stream)
105 can be changed using the
106 .Xr setbuf 3
108 .Xr setvbuf 3
109 call.
110 Note that in case
111 .Nm stdin
112 is associated with a terminal, there may also be input buffering
113 in the terminal driver, entirely unrelated to stdio buffering.
114 (Indeed, normally terminal input is line buffered in the kernel.)
115 This kernel input handling can be modified using calls like
116 .Xr tcsetattr 3 ;
117 see also
118 .Xr stty 1 ,
120 .Xr termios 3 .
121 .Sh "CONFORMING TO"
123 .Nm stdin ,
124 .Nm stdout ,
126 .Nm stderr
127 macros conform to
128 .St -ansiC ,
129 and this standard also stipulates that these three
130 streams shall be open at program startup.