The operation of a filter program relies on two MS-DOS features that first appeared in version 2.0: standard devices and redirectable I/O.
The standard devices are represented by five handles that are originally established by COMMAND.COM. Each process inherits these handles from its immediate parent. Thus, the standard device handles are already open when a process acquires control of the system, and it can use them with Interrupt 21H Functions 3FH and 40H for read and write operations without further preliminaries. The default assignments of the standard device handles are as follows:
Handle Name Default device
0 stdin (standard input) CON
1 stdout (standard output) CON
2 stderr (standard error) CON
3 stdaux (standard auxiliary) AUX
4 stdprn (standard printer) PRN
The CON device is assigned by default to the system's keyboard and video display. AUX and PRN are respectively associated by default with COM1 (the first physical serial port) and LPT1 (the first parallel printer port). You can use the MODE command to redirect LPT1 to one of the serial ports; the MODE command will also redirect PRN.
When executing a program by entering its name at the COMMAND.COM prompt, you can redirect the standard input, the standard output, or both from their default device (CON) to another file, a character device, or a process. You do this by including one of the special characters <, >, >>, and | in the command line, in the form shown on the following page.
Symbol Effect
< file Takes standard input from the specified file instead of
the keyboard.
< device Takes standard input from the named device instead of
the keyboard.
> file Sends standard output to the specified file instead of
the display.
>> file Appends standard output to the current contents of the
specified file instead of sending it to the display.
> device Sends standard output to the named device instead of
the display.
p1 | p2 Routes standard output of program p1 to become the
standard input of program p2. (Output of p1 is said to
be piped to p2.)
For example, the command
C>SORT <MYFILE.TXT >PRN <Enter>
causes the SORT filter to read its input from the file MYFILE.TXT, sort the lines alphabetically, and write the resulting text to the character device PRN (the logical name for the system's list device).
The redirection requested by the <, >, >>, and | characters takes place at the level of COMMAND.COM and is invisible to the program it affects. Any other process can achieve a similar effect by redirecting the standard input and standard output with Int 21H Function 46H before calling the EXEC function (Int 21H Function 4BH) to run a child process.
Note that if a program circumvents MS-DOS to perform its input and output, either by calling ROM BIOS functions or by manipulating the keyboard or video controller directly, redirection commands placed in the program's command line do not have the expected effect.