Rev | Author | # | Line |
---|---|---|---|
1 | perry | 1 | __NAME__ |
2 | |||
3 | stdin, stdout, stderr - standard I/O streams | ||
2 | CraigBox | 4 | |
1 | perry | 5 | __SYNOPSIS__ |
3 | CraigBox | 6 | <verbatim> |
2 | CraigBox | 7 | #include <stdio.h> |
1 | perry | 8 | extern FILE stdin; |
9 | extern FILE stdout; | ||
10 | extern FILE stderr; | ||
3 | CraigBox | 11 | </verbatim> |
2 | CraigBox | 12 | |
1 | perry | 13 | __DESCRIPTION__ |
14 | |||
15 | Under normal circumstances every Unix program has three | ||
16 | streams opened for it when it starts up, one for input, one | ||
17 | for output, and one for printing diagnostic or error mes- | ||
18 | sages. These are typically attached to the user's terminal | ||
19 | (see tty(4)) but might instead refer to files or | ||
20 | other devices, depending on what the parent process chose to | ||
21 | set up. (See also the ``Redirection'' section of | ||
22 | sh(1) .) | ||
23 | |||
24 | |||
2 | CraigBox | 25 | The input stream is referred to as ``standard input''; the output stream is referred to as ``standard output''; and the error stream is referred to as ``standard error''. These terms are abbreviated to form the symbols used to refer to these files, namely __stdin__, __stdout__, and __stderr__. |
1 | perry | 26 | |
27 | |||
28 | Each of these symbols is a stdio(3) macro of type | ||
29 | pointer to FILE, and can be used with functions like | ||
30 | fprintf(3) or fread(3). | ||
31 | |||
32 | |||
33 | Since FILEs are a buffering wrapper around Unix file | ||
34 | descriptors, the same underlying files may also be accessed | ||
35 | using the raw Unix file interface, that is, the functions | ||
36 | like read(2) and lseek(2). The integer | ||
37 | file descriptors associated with the streams stdin, stdout, | ||
38 | and stderr are 0, 1, and 2, respectively. The preprocessor | ||
39 | symbols STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are | ||
40 | defined with these values in | ||
41 | |||
42 | |||
43 | Note that mixing use of FILEs and raw file descriptors can | ||
44 | produce unexpected results and should generally be avoided. | ||
45 | (For the masochistic among you: POSIX.1, section 8.2.3, | ||
46 | describes in detail how this interaction is supposed to | ||
47 | work.) A general rule is that file descriptors are handled | ||
48 | in the kernel, while stdio is just a library. This means for | ||
49 | example, that after an exec, the child inherits all open | ||
50 | file descriptors, but all old streams have become inaccessi- | ||
51 | ble. | ||
52 | |||
53 | |||
54 | Since the symbols stdin, stdout, and stderr are specified to | ||
55 | be macros, assigning to them is non-portable. The standard | ||
56 | streams can be made to refer to different files with help of | ||
57 | the library function freopen(3), specially | ||
58 | introduced to make it possible to reassign stdin, stdout, | ||
59 | and stderr. The standard streams are closed by a call to | ||
60 | exit(3) and by nor- mal program | ||
61 | termination. | ||
2 | CraigBox | 62 | |
1 | perry | 63 | __SEE ALSO__ |
64 | |||
2 | CraigBox | 65 | sh(1), csh(1), open(2), fopen(3), stdio(3) |
1 | perry | 66 | |
67 | __CONSIDERATIONS__ | ||
68 | |||
69 | The stream stderr is unbuffered. The stream stdout is | ||
70 | line-buffered when it points to a terminal. Partial lines | ||
71 | will not appear until fflush(3) or exit(3) | ||
72 | is called, or a newline is printed. This can produce | ||
73 | unexpected results, especially with debugging output. The | ||
74 | buffering mode of the standard streams (or any other stream) | ||
75 | can be changed using the setbuf(3) or | ||
76 | setvbuf(3) call. Note that in case stdin is | ||
77 | associated with a terminal, there may also be input | ||
78 | buffering in the terminal driver, entirely unrelated to | ||
79 | stdio buffering. (Indeed, normally terminal input is line | ||
80 | buffered in the kernel.) This kernel input handling can be | ||
81 | modified using calls like tcsetattr(3); see also | ||
82 | stty(1), and termios(3). | ||
2 | CraigBox | 83 | |
1 | perry | 84 | __CONFORMING TO__ |
85 | |||
86 | The stdin, stdout, and stderr macros conform to | ||
2 | CraigBox | 87 | ANSI X3.159-1989 (`` ANSI C''), and this standard also stipulates that these three streams shall be open at program startup. |
lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 6 times)