Task
Implement the function run_ls
- Takes a
char* pathto a directory and runslson that path using one of the exec family of functions - Should completely replace the current process (must not return)
- If
execfails, you should print an error usingperror
Function Signature
// Given a path, execute `ls` on that path, replacing the current process.
void run_ls(char* path);Note
- Use
fgetsor similar to read the path string fromstdin - Strip
'\n'if present - May use
execlp,execl, or any otherexec*function - If the
execcall fails, you should print a meaningful error andexit(1) - Make sure function does not return if the exec call is successful
Example
$ ./run_ls
.
run_ls
run_ls.c
$ ./run_ls
/nonexistent
ls: cannot access '/nonexistent': No such file or directoryCode
int run_ls(const char str[]){
execlp("ls", "ls", str, (char*)NULL);
perror("exec");
exit(1);
}