Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

execlp question

Status
Not open for further replies.

bobetko

Programmer
Jan 14, 2003
155
US
I need to write function (filedesc) that will return a File Descriptor. File Descriptor will then be used to read content of the current folder.
Can somebody confirm that I am on the right path?

Here is my idea:

Code:
int filedesc(){
    int n, fd[2];
    pipe (fd);
    switch (fork()) {
        case 0:
         close(fd[0]);
         dup2(fd[1], STDOUT_FILENO);
         close(fd[1]);
         execlp("ls", "ls", NULL);
        default:
         close(fd[0]);
         dup2(fd[1], STDIN_FILENO);
         close(fd[1]);
         return(STDIN_FILENO);
    }
}
Thanks
 
So far, so good I suppose.

But why are you using this just to read filenames, when opendir / readdir / closedir will do the same without all the messy complication of spawning a new process.

fork() can return an error condition as well, as can pipe()



--
 
I am trying to learn how to create new proces, do plumbing, and other stuff... Thanks a lot....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top