324_Book

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ion to read a file: 1 2 if (S_ISREG(stat.st_mode) && (stat.st_mode & S_IRUSR)) printf("This is a regular file that I can read\n"); 12.4.5 The dup2 Function The Unix shell provides an I/O redirection operator that allows users to redirect the standard output to a disk file. For example, unix> ls >foo writes the standard output of the ls program to the file foo. As we shall see in Section 12.7, a Web server performs a similar kind of redirection when it runs a CGI program on behalf of the client. One way to accomplish I/O redirection is to use the dup2 function. #include <unistd.h> int dup2(int oldfd, int newfd); returns: nonnegative descriptor if OK, -1 on error The dup2 function duplicates descriptor oldfd, assigns it to descriptor newfd, and returns newfd. If newfd was already open, then dup2 closes newfd before it duplicates oldfd. For each process, the kernel maintains a descriptor table that is indexed by the process’s open descriptors. The entry for an open descriptor points to a file table entry that consists of, among other thin...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online