-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchange_dir.c
43 lines (41 loc) · 1.36 KB
/
change_dir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include "header.h"
#include <dirent.h>
#include "libshell/libshell.h"
/*
* ch_dir() - Takes a path as parameter and uses the Linux builtin function
* chdir to change the process to that directory. If the path is NULL, the
* path is set to the user's home directory.
* @path: The path to change directories to.
* @env: The user's environment.
*
* Return: On success, zero is returned. On error, -1 is returned, and
* errno is set appropriately.
*/
int ch_dir(char *path, char **env) {
char *tmp;
/*
* Add code to track the "previous working directory" with set_env()
* every time this function is called. Then, if `cd -` is used,
* the path will be set to this environmental variable. See placeholder
* code below.
*/
/* setenv("OLDPWD") = get_env_var("PWD", env) */
if (path == NULL) {
path = get_env_var("HOME", env);
tmp = path;
free(path);
return chdir(tmp);
} else if (*path == '-') {
path = get_env_var("OLDPWD", env);
tmp = path;
free(path);
return chdir(tmp);
}
/* setenv("PWD") = get_env_var(path, env) */
return chdir(path);
}