/*-------------------------------------------------------------------*/ /* testdup.c */ /* The dup system call. */ /*-------------------------------------------------------------------*/ /* Error handling code omitted. */ /*-------------------------------------------------------------------*/ #include #include #include int main(int argc, char *argv[]) { int iFd; int iOldFd1; int iOldFd0; char pcBuffer[6]; /* Write "hello" to a file named tempfile. */ iFd = creat("tempfile", 0644); iOldFd1 = dup(1); close(1); dup(iFd); close(iFd); printf("hello"); close(1); dup(iOldFd1); close(iOldFd1); /* Read "hello" from a file named tempfile. */ iFd = open("tempfile", O_RDONLY); iOldFd0 = dup(0); close(0); dup(iFd); close(iFd); scanf("%s", pcBuffer); close(0); dup(iOldFd0); close(iOldFd0); /* Print the data to verify that the previous worked. */ printf("%s\n", pcBuffer); return 0; } /* Sample execution: --> gcc -Wall -ansi -pedantic -o testdup testdup.c --> testdup hello --> */