The standard devices are the keyboard, screen, auxiliary device, and printer. The system provides open file handles to these devices when it starts a program, as shown in the following table:
Name | Handle | Default device |
Standard input (STDIN) | 0 | CON |
Standard output (STDOUT) | 1 | CON |
Standard error (STDERR) | 2 | CON |
Standard auxiliary (STDAUX) | 3 | AUX |
Standard printer (STDPRN) | 4 | PRN |
A program can use the specified handles in such system functions as Read File or Device (Interrupt 21h Function 3Fh) and Write File or Device (Interrupt 21h Function 40h), to read from and write to the standard devices.
By default, a standard device corresponds to the device specified in the preceding table. However, users can redirect the standard devices, associating one or more of the handles with other character devices or with files. For example, a user can redirect the standard input to a file, so that the program reads input from the file instead of from the keyboard. The program does nothing special to read from the redirected device; it simply uses the standard input handle (now associated with a file) in Read File or Device to read characters from the file.
A program is not notified that a standard device has been redirected. This can lead to problems if the redirection is to a file and the disk has limited space. If the standard output is redirected to a file, Write File or Device fails when the disk becomes full. A program can use Get Device Data (Interrupt 21h Function 4400h) to determine whether a standard-device handle refers to a character device or a file. character device>file>
A program can set the input/output (I/O) mode of a standard device. This setting has the same effect for a standard device as it does for a device opened explicitly by the program. Note that, since standard devices are shared by all programs, setting the I/O mode affects standard devices for all programs. Before changing the mode of a standard device, a program should use Get Device Data to save the current mode. Before terminating, the program should restore the previous mode by using Set Device Data (Interrupt 21h Function 4401h).
Programs that change the I/O mode of a standard device should also incorporate custom critical-error and CTRL+C interrupt handlers that either restore the I/O mode or prevent unexpected termination. For more information about interrupt handlers, see Chapter 7, “Interrupts.”