-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from maqamylee0/emmilly
cd cmd
- Loading branch information
Showing
4 changed files
with
49 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters