4.7 Filters and Redirection

Team-FLY

UNIX provides a large number of utilities that are written as filters. A filter reads from standard input, performs a transformation, and outputs the result to standard output. Filters write their error messages to standard error. All of the parameters of a filter are communicated as command-line arguments. The input data should have no headers or trailers , and a filter should not require any interaction with the user .

Examples of useful UNIX filters include head , tail , more , sort , grep and awk . The cat command takes a list of filenames as command-line arguments, reads each of the files in succession, and echoes the contents of each file to standard output. However, if no input file is specified, cat takes its input from standard input and writes its results to standard output. In this case, cat behaves like a filter.

Recall that a file descriptor is an index into the file descriptor table of that process. Each entry in the file descriptor table points to an entry in the system file table, which is created when the file is opened. A program can modify the file descriptor table entry so that it points to a different entry in the system file table. This action is known as redirection . Most shells interpret the greater than character ( > ) on the command line as redirection of standard output and the less than character ( < ) as redirection of standard input. (Associate > with output by picturing it as an arrow pointing in the direction of the output file.)

Example 4.35

The cat command with no command-line arguments reads from standard input and echoes to standard output. The following command redirects standard output to my.file with > .

 cat > my.file 

The cat command of Example 4.35 gathers what is typed from the keyboard into the file my.file . Figure 4.6 depicts the file descriptor table for Example 4.35. Before redirection, entry [1] of the file descriptor table points to a system file table entry corresponding to the usual standard output device. After the redirection, entry [1] points to a system file table entry for my.file .

Figure 4.6. Status of the file descriptor table before and after redirection for the process that is executing cat > my.file .

graphics/04fig06.gif

The redirection of standard output in cat > my.file occurs because the shell changes the standard output entry of the file descriptor table (a pointer to the system file table) to reference a system file table entry associated with my.file . To accomplish this redirection in a C program, first open my.file to establish an appropriate entry in the system file table. After the open operation, copy the pointer to my.file into the entry for standard output by executing the dup2 function. Then, call close to eliminate the extra file descriptor table entry for my.file .

The dup2 function takes two parameters, fildes and fildes2 . It closes entry fildes2 of the file descriptor table if it was open and then copies the pointer of entry fildes into entry fildes2 .

  SYNOPSIS  #include <unistd.h>   int dup2(int fildes, int fildes2);  POSIX  

On success, dup2 returns the file descriptor value that was duplicated . On failure, dup2 returns “1 and sets errno . The following table lists the mandatory errors for dup2 .

errno

cause

EBADF

fildes is not a valid open file descriptor, or fildes2 is negative or greater than or equal to OPEN_MAX

EINTR

dup2 was interrupted by a signal

Example 4.36

Program 4.18 redirects standard output to the file my.file and then appends a short message to that file.

Figure 4.7 shows the effect of the redirection on the file descriptor table of Program 4.18. The open function causes the operating system to create a new entry in the system file table and to set entry [3] of the file descriptor table to point to this entry. The dup2 function closes the descriptor corresponding to the second parameter (standard output) and then copies the entry corresponding to the first parameter ( fd ) into the entry corresponding to the second parameter ( STDOUT_FILENO ). From that point on in the program, a write to standard output goes to my.file .

Figure 4.7. Status of the file descriptor table during the execution of Program 4.18.

graphics/04fig07.gif

Program 4.18 redirect.c

A program that redirects standard output to the file my.file .

 #include <fcntl.h> #include <stdio.h> #include <sys/stat.h> #include <unistd.h> #include "restart.h" #define CREATE_FLAGS (O_WRONLY  O_CREAT  O_APPEND) #define CREATE_MODE (S_IRUSR  S_IWUSR  S_IRGRP  S_IROTH) int main(void) {    int fd;    fd = open("my.file", CREATE_FLAGS, CREATE_MODE);    if (fd == -1) {        perror("Failed to open my.file");        return 1;    }    if (dup2(fd, STDOUT_FILENO) == -1) {       perror("Failed to redirect standard output");       return 1;    }    if (r_close(fd) == -1) {       perror("Failed to close the file");       return 1;    }    if (write(STDOUT_FILENO, "OK", 2) == -1) {       perror("Failed in writing to file");       return 1;    }    return 0; } 
Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net