diff --git a/exec_cd.c b/exec_cd.c new file mode 100644 index 0000000..764025f --- /dev/null +++ b/exec_cd.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + +/** +* exec_cd - the function that is used to change the directory +* arg: the argument. that is the +* Return: Always 0 +*/ + +char lastdir[MAX_PATH]; // initialized to zero + +int exec_cd(char *arg) { + char curdir[MAX_PATH]; + char path[MAX_PATH]; + + if (getcwd(curdir, sizeof curdir)) { + /* current directory might be unreachable: not an error */ + *curdir = '\0'; + } + if (arg == NULL) { + arg = getenv("HOME"); + } + if (!strcmp(arg, "-")) { + if (*lastdir == '\0') { + fprintf(stderr, "no previous directory\n"); + return 1; + } + arg = lastdir; + } else { + /* this should be done on all words during the parse phase */ + if (*arg == '~') { + if (arg[1] == '/' || arg[1] == '\0') { + snprintf(path, sizeof path, "%s%s", getenv("HOME"), arg + 1); + arg = path; + } else { + /* ~name should expand to the home directory of user with login `name` + this can be implemented with getpwent() */ + fprintf(stderr, "syntax not supported: %s\n", arg); + return 1; + } + } + } + if (chdir(arg)) { + fprintf(stderr, "chdir: %s: %s\n", strerror(errno), path); + return 1; + } + strcpy(lastdir, curdir); + return 0; +}