Skip to content

Commit

Permalink
Implement cd and cd -
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesulayomy committed Dec 16, 2022
1 parent 2e26a8d commit c3ab2c2
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 6 deletions.
40 changes: 40 additions & 0 deletions _getenv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "shell.h"

/**
* _getenv - gets the value of an environment variable
* @shell: shell data
* @name: name of variable
*
* Return: pointer to the value
*/

char *_getenv(sh_data *shell, char *name)
{
int i, j, k, l, check;
char *value;

for (i = 0; shell->_environ[i]; i++)
{
j = 0, check = 0;
while (shell->_environ[i][j] != '=' && name[j] != '\0')
{
if (shell->_environ[i][j] != name[j])
check = 1;
j++;
}
if (check == 0)
{
for (k = j + 1, l = 0; shell->_environ[i][k]; k++, l++)
;

value = malloc(sizeof(char) * (l + 1));

for (k = j + 1, l = 0; shell->_environ[i][k]; l++, k++)
value[l] = shell->_environ[i][k];
value[l] = '\0';
return (value);
}
}

return (NULL);
}
59 changes: 59 additions & 0 deletions builtins.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
#include "shell.h"

/**
* my_cd - exits the shell
* @shell: shell data
*
* Return: an exit value
*/
int my_cd(sh_data *shell)
{
int m, n = 0;
char *new_pwd;
char *old_pwd = _getenv(shell, "PWD");

if (shell->arr[1] == NULL)
{
free_arr2(shell->arr);
shell->arr = malloc(sizeof(char *) * 3);
shell->arr[0] = my_strdup("cd");
shell->arr[1] = _getenv(shell, "HOME");
shell->arr[2] = NULL;
}
else if (my_strcmp(shell->arr[1], "-") == 0)
{
free(shell->arr[1]);
shell->arr[1] = _getenv(shell, "OLDPWD");
n++;
}

m = chdir(shell->arr[1]);
if (m == -1)
printf("%s: %s: No such file or directory\n", shell->arr[0], shell->arr[1]);
if (n == 1)
{
write(STDOUT_FILENO, shell->arr[1], my_strlen(shell->arr[1]));
write(STDOUT_FILENO, "\n", 1);
}

new_pwd = getcwd(NULL, 1024);

free_arr2(shell->arr);
shell->arr = malloc(sizeof(char *) * 4);
shell->arr[0] = my_strdup("setenv");
shell->arr[1] = my_strdup("PWD");
shell->arr[2] = my_strdup(new_pwd);
shell->arr[3] = NULL;
my_set(shell);

free_arr2(shell->arr);
shell->arr = malloc(sizeof(char *) * 4);
shell->arr[0] = my_strdup("setenv");
shell->arr[1] = my_strdup("OLDPWD");
shell->arr[2] = my_strdup(old_pwd);
shell->arr[3] = NULL;
my_set(shell);

free(new_pwd);
free(old_pwd);
return (0);
}

/**
* my_exit - exits the shell
* @shell: shell data
Expand Down
1 change: 0 additions & 1 deletion get_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ char **get_commands(char *buffer, size_t n)
{
char **arr = NULL;
char *token;
/* char delim[6] = " \t\r\a\n"; */
size_t k = 0;
ssize_t i, r;

Expand Down
12 changes: 9 additions & 3 deletions shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ int (*get_func(char **arr))(sh_data *)
{"env", my_env},
{"setenv", my_set},
{"unsetenv", my_unset},
/**
* {"cd", my_cd}
*/
{"cd", my_cd},
{NULL, NULL}
};

Expand Down Expand Up @@ -62,7 +60,15 @@ void loop_shell(sh_data *shell)
{
shell->status = execve(path, shell->arr, shell->_environ);
if (shell->status == -1)
{
printf("%s: Permission denied\n", shell->arr[0]);
free_list(shell->path);
free_arr2(shell->_environ);
free_arr2(shell->arr);
free(shell->line);
free(path);
exit(98);
}
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ typedef struct built_in
} built_in;

char **get_commands(char *, size_t);
char *_getenv(const char *);
char *_getenv(sh_data *, char *);
char *mod_env(sh_data *);
char *check_shell(sh_data *);
char *my_strcat(char *, char *);
Expand All @@ -75,7 +75,8 @@ char *my_strdup(char *);
char *my_strtok(char *, const char *);
char *search_path(path_l *, char *);
int (*get_func(char **))(sh_data *);
int my_atoi(char *s);
int my_atoi(char *);
int my_cd(sh_data *);
int my_env(sh_data *);
int my_exit(sh_data *);
int my_set(sh_data *);
Expand Down

0 comments on commit c3ab2c2

Please sign in to comment.