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:
Thanks
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);
}
}