Task
Implement the function run_cd
- Check if the
cdcommand is called correctly (with exactly 1 argument: the path), then callchdir()to change directories - if
argc != 2, printusage: cd <path>tostderrand return-1 - if
chdir()fails, callperror("cd")and return-1 - If successful, return
0
// Handle the cd command. Should have argc == 2 ("cd" and path).
// Call chdir() on the path. Return 0 on success, -1 on error.
int run_cd(int argc, char* path);TIP
use
chdir(path)to change directories. It returns0on success,-1on failure
Example
$ gcc run_cd.c -o run_cd
$ ./run_cd
cd ..
success
cd
usage: cd <path>
cd /nonexistent
cd: No such file or directory
cd .
successCode
#include <stdio.h>
#include <unistd.h>
int run_cd(int argc, char *path)
{
if(argc != 2){
fprintf(stderr, "usage: cd %s\n", path);
return -1;
}
if(chdir(path) < 0){
perror("cd");
return -1;
}
return 0;
}