MS-DOS 6.22 Command Piping and the SORT Filter

In MS-DOS 6.22, command line piping through the SORT filter utility is accomplished through intermediate disk storage rather than true multitasking inter-process communication. Because MS-DOS is a single-tasking operating system, the command processor (COMMAND.COM) cannot execute two programs simultaneously. Instead, it intercepts the pipe operator (|), sequentially executes the left-hand command while redirecting its standard output to a hidden temporary file, and subsequently executes SORT.EXE with standard input redirected from that temporary file. Once sorting is complete and the final output is delivered to the console or the next destination, the shell deletes the temporary file.

Command Line Parsing and Pipeline Interception

When a user submits a command such as DIR | SORT, the command interpreter COMMAND.COM scans the command string sequentially. Upon encountering the pipe symbol (|), the parser splits the input string into two distinct operations: the producing command (the left side) and the consuming filter (the right side).

Because MS-DOS lacks native streaming pipelines, it executes the pipeline in distinct serial phases:

  1. Temporary File Generation: COMMAND.COM creates a unique temporary file on disk using DOS Interrupt 21h, Function 5Ah (Create Temporary File). The file is typically stored in the directory defined by the TEMP or TMP environment variable. If neither variable is set, the file is created in the current working directory.
  2. Standard Output Redirection: The file handle for standard output (STDOUT, Handle 1) of the first command is duplicated and redirected to the newly created temporary file.
  3. Execution of the Producer: The command processor executes the left-hand command. All generated character output is written directly to the temporary file rather than the console screen (CON).

Invocation of the SORT Utility

Once the producing program finishes execution and exits, control returns to COMMAND.COM. The shell closes the temporary file to flush any unwritten disk buffers.

Next, the shell sets up the execution environment for the right-hand program:

  1. Standard Input Redirection: COMMAND.COM redirects standard input (STDIN, Handle 0) to point to the temporary file containing the output from the first command.
  2. Standard Output Assignment: Standard output (STDOUT) is pointed either to the video display device (CON) or, if chained into another pipe or file redirection, to another temporary file or specified target.
  3. Execution of SORT.EXE: The shell loads SORT.EXE into conventional memory using DOS Interrupt 21h, Function 4Bh (EXEC).

Data Handling Inside SORT.EXE

SORT.EXE operates as an external filter utility designed to read plain text data from standard input and write the ordered results to standard output:

  • Memory Allocation: In MS-DOS 6.22, SORT.EXE relies on standard 16-bit segmented real-mode memory. It allocates a buffer up to the maximum available contiguous memory within a single 64KB segment. Because of this architecture, standard MS-DOS SORT cannot sort files or piped data streams larger than 64 Kilobytes without returning an "Out of memory" error.
  • Line Reading and Comparison: The utility reads the input stream character by character, identifying lines terminated by carriage return and line feed (CRLF) delimiters. It constructs an array of line pointers in memory. Comparisons are performed based on the ASCII collation sequence by default, though it respects active localized character sets if country settings are configured via COUNTRY.SYS.
  • Switches: If parameters were passed during invocation (such as /R for reverse sort or /+n to begin sorting at column n), SORT.EXE adjusts its pointer offsets and comparison logic accordingly.
  • Output Stream: Once all lines are held in the buffer and sorted, SORT.EXE iterates through the sorted pointer list and writes each line to standard output (Handle 1).

Pipeline Cleanup

After SORT.EXE finishes writing the sorted records, it terminates and returns an exit code to COMMAND.COM. The command interpreter then unlinks (deletes) the intermediate temporary file using DOS Interrupt 21h, Function 41h (Delete File), restores the original console handles for STDIN and STDOUT, and presents the standard command prompt to the user.