Skip to content

Commit

Permalink
cd cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
maqamylee0 committed Apr 29, 2023
1 parent 49cf401 commit d819672
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
9 changes: 6 additions & 3 deletions execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ void execute(char **argv, char **env)
{
pid_t pid;
int status;
char cwd[PATH_MAX];
char *cmd_path = NULL, *cmd = NULL;

if (_strcmp(argv[0], "cd") == 0)
{
cmd = get_cd_path(argv, env);
if (chdir(cmd) != -1)
{
;
getcwd(cwd, sizeof(cwd));
setenv("OLDPWD", getenv("PWD"), 1);
setenv("PWD", cwd, 1);
}
if (_strcmp(argv[1], "-") == 0)
/*if (_strcmp(argv[1], "-") == 0)
{
_puts(cmd);
_putchar('\n');
}
}*/
return;
}

Expand Down
42 changes: 42 additions & 0 deletions handle_cd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "main.h"

/**
* handle_cd- handles cd
* @argv: list of arguments
* Return: 0 or 1
*/

int handle_cd(char **argv)
{
char path[1024];
int cd_val = 0;


if (argv[1] == NULL)
cd_val = chdir(getenv("HOME"));
if (_strcmp(argv[1], "-") == 0)
cd_val = chdir(getenv("OLDPWD"));
else
cd_val = chdir(argv[1]);
if (cd_val == -1)
{
perror("Failed to change diretory");
return (-1);
}
if (getcwd(path, sizeof(path)) == NULL)
{
perror("Failed to change directory");
return (-1);
}
if (setenv("OLDPWD", getenv("PWD"), 1) == -1)
{
perror("Failed to change directory");
return (-1);
}
if (setenv("PWD", path, 1) == -1)
{
perror("Failed to change directory");
return (-1);
}
return (0);
}
2 changes: 0 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ void run_non_interactive_mode(int argc, char **argv, char **envp)
argc = num_token(input_cpy, delim);
check_argv(argv, env, input, input_cpy);
if (_strcmp(argv[0], "env") != 0)
{
execute(argv, env);
}
free(input_cpy);
free(input);
cleanup(argv);
Expand Down
1 change: 1 addition & 0 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <linux/limits.h>

char *allocate(int n);
char *_strcpy(char *dest, char *src);
Expand Down

0 comments on commit d819672

Please sign in to comment.