/*-------------------------------------------------------------------*/ /* testdupin.c */ /* The dup system call. */ /*-------------------------------------------------------------------*/ #include #include #include #define BUFFER_LENGTH 100 int main(int argc, char *argv[]) { int iFd; int iRet; char pcBuffer[BUFFER_LENGTH]; iFd = open("tempfile", O_RDONLY); if (iFd == -1) {perror(argv[0]); return 1; } iRet = close(0); if (iRet == -1) {perror(argv[0]); return 1; } iRet = dup(iFd); if (iRet == -1) {perror(argv[0]); return 1; } iRet = close(iFd); if (iRet == -1) {perror(argv[0]); return 1; } scanf("%s", pcBuffer); /* Print the data to verify that the scanf worked. */ printf("%s\n", pcBuffer); return 0; } /* Sample execution: $ gcc -Wall -ansi -pedantic -o testdupin testdupin.c $ testdupin somedata $ */