/*-------------------------------------------------------------------*/ /* testdupstdin.c */ /* The dup system call to redirect stdin. */ /*-------------------------------------------------------------------*/ #include #include #include int main(int argc, char *argv[]) { int iFd; char pcBuffer[1024]; scanf("%s", pcBuffer); printf("%s\n", pcBuffer); iFd = open("tempfile", O_RDONLY, 0); close(0); dup(iFd); close(iFd); scanf("%s", pcBuffer); printf("%s\n", pcBuffer); return 0; } /* Sample executions: $ cat tempfile somedata $ testdupstdin somedata <- Typed by the user. somedata somedata $ */