Xref: utzoo comp.lang.c:19274 comp.unix.wizards:16842 Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!decwrl!megatest!djones From: djones@megatest.UUCP (Dave Jones) Newsgroups: comp.lang.c,comp.unix.wizards Subject: Re: redirect stdin when using execl Message-ID: <5587@goofy.megatest.UUCP> Date: 9 Jun 89 22:31:11 GMT References: <851@pcsbst.UUCP> Organization: Megatest Corporation, San Jose, Ca Lines: 54 > In article <414@sc50.UUCP> ron@sc50.UUCP ( Ron Winnacott ) writes: >>Hello net. >> >>Can anyone tell me how to redirect stdin when I use execl to >>start a new program. The problem I am haveing is this, I am writing >>a C program that forks then execl's a new program, But I need to >>use a redirect "<" in the execl call. >> Under BSD Unix, you use dup2. (All you folks who want only to see complete, production quality, QA-tested code, wine-soaked and sugar-cured, shut your eyes quick!) It goes something like this: if((pid = fork()) == 0) { /* This the the spawned process. */ int file_descriptor = open("foo/bar", "r"); if(file_descriptor == -1) { perror("foo/bar"); _exit(-1); } else { dup2(file_descriptor,0); /* make it the standard input */ close(file_descriptor); /* Here, close any of the parent process's file-descriptors that * will not be used by this process.. */ /* and now... */ execl("prog", "arg0", /* etc */ (char*)0); /* It's an error if we get to here.... */ /* etc.. */ _exit(-1); } } else { if(pid != -1) { /* provide for "reaping" the process. */ /* See man page for "wait" */ } else { /* Could not execl the program... */ } }