man-pages/man3/stdin.3

156 lines
3.9 KiB
Groff
Raw Normal View History

2004-11-03 13:51:07 +00:00
.\" From dholland@burgundy.eecs.harvard.edu Tue Mar 24 18:08:15 1998
.\"
.\" This man page was written in 1998 by David A. Holland
.\" and placed in the Public Domain. Polished a bit by aeb.
.\" 2005-06-16 mtk, mentioned freopen()
2004-11-03 13:51:07 +00:00
.\"
.\" 2007-12-08, mtk, Converted from mdoc to man macros
2007-12-03 19:53:53 +00:00
.\"
.TH STDIN 3 2008-07-14 "Linux" "Linux Programmer's Manual"
2007-12-03 19:53:53 +00:00
.SH NAME
stdin, stdout, stderr \- standard I/O streams
.SH SYNOPSIS
.nf
.B #include <stdio.h>
2007-12-23 08:20:39 +00:00
.BI "extern FILE *" stdin ;
.BI "extern FILE *" stdout ;
.BI "extern FILE *" stderr ;
2007-12-03 19:53:53 +00:00
.fi
.SH DESCRIPTION
intro.1, time.1, accept.2, bind.2, connect.2, execve.2, flock.2, getdents.2, getpriority.2, getuid.2, intro.2, ioctl.2, mincore.2, mknod.2, personality.2, ptrace.2, read.2, recv.2, select_tut.2, send.2, sendfile.2, shmctl.2, sigaction.2, signal.2, stat.2, times.2, truncate.2, umask.2, wait.2, MB_CUR_MAX.3, MB_LEN_MAX.3, argz_add.3, btowc.3, clearenv.3, clock.3, cmsg.3, end.3, endian.3, errno.3, exit.3, fgetwc.3, fgetws.3, fopen.3, fputwc.3, fputws.3, fseek.3, fwide.3, getfsent.3, getgrnam.3, gethostid.3, getipnodebyname.3, getmntent.3, getpwnam.3, getwchar.3, grantpt.3, iconv.3, iconv_close.3, iconv_open.3, insque.3, intro.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, malloc.3, mblen.3, mbrlen.3, mbrtowc.3, mbsinit.3, mbsnrtowcs.3, mbsrtowcs.3, mbstowcs.3, mbtowc.3, mkstemp.3, mktemp.3, nl_langinfo.3, openpty.3, posix_openpt.3, printf.3, ptsname.3, putwchar.3, qecvt.3, rcmd.3, readdir.3, rexec.3, rpc.3, setnetgrent.3, shm_open.3, sigpause.3, stdin.3, stpcpy.3, strftime.3, strptime.3, syslog.3, towctrans.3, towlower.3, towupper.3, ttyslot.3, ungetwc.3, unlocked_stdio.3, wcpcpy.3, wcpncpy.3, wcrtomb.3, wcscasecmp.3, wcscat.3, wcschr.3, wcscmp.3, wcscpy.3, wcscspn.3, wcsdup.3, wcslen.3, wcsncasecmp.3, wcsncat.3, wcsncmp.3, wcsncpy.3, wcsnlen.3, wcsnrtombs.3, wcspbrk.3, wcsrchr.3, wcsrtombs.3, wcsspn.3, wcsstr.3, wcstok.3, wcstombs.3, wcswidth.3, wctob.3, wctomb.3, wctrans.3, wctype.3, wcwidth.3, wmemchr.3, wmemcmp.3, wmemcpy.3, wmemmove.3, wmemset.3, wprintf.3, console_ioctl.4, pts.4, elf.5, filesystems.5, hosts.5, proc.5, ttytype.5, boot.7, capabilities.7, credentials.7, epoll.7, glob.7, koi8-r.7, path_resolution.7, pty.7, signal.7, suffixes.7, time.7, unicode.7, unix.7, uri.7, utf-8.7: global fix: s/Unix/UNIX/ The man pages were rather inconsistent in the use of "Unix" versus "UNIX". Let's go with the trademark usage. Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-10-12 04:45:38 +00:00
Under normal circumstances every UNIX program has three streams opened
2004-11-03 13:51:07 +00:00
for it when it starts up, one for input, one for output, and one for
printing diagnostic or error messages.
These are typically attached to
2004-11-03 13:51:07 +00:00
the user's terminal (see
2007-12-03 19:53:53 +00:00
.BR tty (4)
2004-11-03 13:51:07 +00:00
but might instead refer to files or other devices, depending on what
2008-06-28 04:57:20 +00:00
the parent process chose to set up.
(See also the "Redirection" section of
2007-12-03 19:53:53 +00:00
.BR sh (1).)
.PP
2007-12-03 19:53:53 +00:00
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
2004-11-03 13:51:07 +00:00
used to refer to these files, namely
2007-12-03 19:53:53 +00:00
.IR stdin ,
.IR stdout ,
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.IR stderr .
2004-11-03 13:51:07 +00:00
Each of these symbols is a
2007-12-03 19:53:53 +00:00
.BR stdio (3)
2007-12-16 13:39:24 +00:00
macro of type pointer to
.IR FILE ,
and can be used with functions like
2007-12-03 19:53:53 +00:00
.BR fprintf (3)
2004-11-03 13:51:07 +00:00
or
2007-12-03 19:53:53 +00:00
.BR fread (3).
.PP
2007-12-16 13:39:24 +00:00
Since
.IR FILE s
intro.1, time.1, accept.2, bind.2, connect.2, execve.2, flock.2, getdents.2, getpriority.2, getuid.2, intro.2, ioctl.2, mincore.2, mknod.2, personality.2, ptrace.2, read.2, recv.2, select_tut.2, send.2, sendfile.2, shmctl.2, sigaction.2, signal.2, stat.2, times.2, truncate.2, umask.2, wait.2, MB_CUR_MAX.3, MB_LEN_MAX.3, argz_add.3, btowc.3, clearenv.3, clock.3, cmsg.3, end.3, endian.3, errno.3, exit.3, fgetwc.3, fgetws.3, fopen.3, fputwc.3, fputws.3, fseek.3, fwide.3, getfsent.3, getgrnam.3, gethostid.3, getipnodebyname.3, getmntent.3, getpwnam.3, getwchar.3, grantpt.3, iconv.3, iconv_close.3, iconv_open.3, insque.3, intro.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, malloc.3, mblen.3, mbrlen.3, mbrtowc.3, mbsinit.3, mbsnrtowcs.3, mbsrtowcs.3, mbstowcs.3, mbtowc.3, mkstemp.3, mktemp.3, nl_langinfo.3, openpty.3, posix_openpt.3, printf.3, ptsname.3, putwchar.3, qecvt.3, rcmd.3, readdir.3, rexec.3, rpc.3, setnetgrent.3, shm_open.3, sigpause.3, stdin.3, stpcpy.3, strftime.3, strptime.3, syslog.3, towctrans.3, towlower.3, towupper.3, ttyslot.3, ungetwc.3, unlocked_stdio.3, wcpcpy.3, wcpncpy.3, wcrtomb.3, wcscasecmp.3, wcscat.3, wcschr.3, wcscmp.3, wcscpy.3, wcscspn.3, wcsdup.3, wcslen.3, wcsncasecmp.3, wcsncat.3, wcsncmp.3, wcsncpy.3, wcsnlen.3, wcsnrtombs.3, wcspbrk.3, wcsrchr.3, wcsrtombs.3, wcsspn.3, wcsstr.3, wcstok.3, wcstombs.3, wcswidth.3, wctob.3, wctomb.3, wctrans.3, wctype.3, wcwidth.3, wmemchr.3, wmemcmp.3, wmemcpy.3, wmemmove.3, wmemset.3, wprintf.3, console_ioctl.4, pts.4, elf.5, filesystems.5, hosts.5, proc.5, ttytype.5, boot.7, capabilities.7, credentials.7, epoll.7, glob.7, koi8-r.7, path_resolution.7, pty.7, signal.7, suffixes.7, time.7, unicode.7, unix.7, uri.7, utf-8.7: global fix: s/Unix/UNIX/ The man pages were rather inconsistent in the use of "Unix" versus "UNIX". Let's go with the trademark usage. Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-10-12 04:45:38 +00:00
are a buffering wrapper around UNIX file descriptors, the
same underlying files may also be accessed using the raw UNIX file
2004-11-03 13:51:07 +00:00
interface, that is, the functions like
2007-12-03 19:53:53 +00:00
.BR read (2)
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.BR lseek (2).
.PP
On program startup, the integer file descriptors
associated with the streams
.IR stdin ,
2007-12-03 19:53:53 +00:00
.IR stdout ,
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.I stderr
are 0, 1, and 2, respectively.
2007-12-03 19:53:53 +00:00
The preprocessor symbols
.BR STDIN_FILENO ,
.BR STDOUT_FILENO ,
and
.B STDERR_FILENO
are defined with these values in
2007-06-20 21:53:34 +00:00
\fI<unistd.h>\fP.
(Applying
2007-12-03 19:53:53 +00:00
.BR freopen (3)
to one of these streams can change the file descriptor number
associated with the stream.)
.PP
2007-12-16 13:39:24 +00:00
Note that mixing use of
.IR FILE s
and raw file descriptors can produce
2004-11-03 13:51:07 +00:00
unexpected results and should generally be avoided.
(For the masochistic among you: POSIX.1, section 8.2.3, describes
in detail how this interaction is supposed to work.)
A general rule is that file descriptors are handled in the kernel,
while stdio is just a library.
This means for example, that after an
2007-12-03 19:53:53 +00:00
.BR exec (3),
2005-10-20 15:11:10 +00:00
the child inherits all open file descriptors, but all old streams
have become inaccessible.
.PP
2004-11-03 13:51:07 +00:00
Since the symbols
.IR stdin ,
2007-12-03 19:53:53 +00:00
.IR stdout ,
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.I stderr
are specified to be macros, assigning to them is nonportable.
2004-11-03 13:51:07 +00:00
The standard streams can be made to refer to different files
with help of the library function
2007-12-03 19:53:53 +00:00
.BR freopen (3),
2004-11-03 13:51:07 +00:00
specially introduced to make it possible to reassign
2007-12-03 19:53:53 +00:00
.IR stdin ,
.IR stdout ,
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.IR stderr .
2004-11-03 13:51:07 +00:00
The standard streams are closed by a call to
2007-12-03 19:53:53 +00:00
.BR exit (3)
2004-11-03 13:51:07 +00:00
and by normal program termination.
.SH "CONFORMING TO"
The
.IR stdin ,
.IR stdout ,
and
.I stderr
macros conform to C89
and this standard also stipulates that these three
streams shall be open at program startup.
.SH NOTES
2004-11-03 13:51:07 +00:00
The stream
.I stderr
is unbuffered.
The stream
.I stdout
is line-buffered when it points to a terminal.
Partial lines will not
2004-11-03 13:51:07 +00:00
appear until
2007-12-03 19:53:53 +00:00
.BR fflush (3)
2004-11-03 13:51:07 +00:00
or
2007-12-03 19:53:53 +00:00
.BR exit (3)
is called, or a newline is printed.
This can produce unexpected
2004-11-03 13:51:07 +00:00
results, especially with debugging output.
The buffering mode of the standard streams (or any other stream)
can be changed using the
2007-12-03 19:53:53 +00:00
.BR setbuf (3)
2004-11-03 13:51:07 +00:00
or
2007-12-03 19:53:53 +00:00
.BR setvbuf (3)
2004-11-03 13:51:07 +00:00
call.
Note that in case
.I stdin
is associated with a terminal, there may also be input buffering
2004-11-03 13:51:07 +00:00
in the terminal driver, entirely unrelated to stdio buffering.
(Indeed, normally terminal input is line buffered in the kernel.)
This kernel input handling can be modified using calls like
2007-12-03 19:53:53 +00:00
.BR tcsetattr (3);
2004-11-03 13:51:07 +00:00
see also
2007-12-03 19:53:53 +00:00
.BR stty (1),
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.BR termios (3).
.SH SEE ALSO
.BR csh (1),
.BR sh (1),
2007-12-03 19:53:53 +00:00
.BR open (2),
.BR fopen (3),
.BR stdio (3)