Skip to content

Commit

Permalink
Merge pull request #22 from maqamylee0/peter_shell
Browse files Browse the repository at this point in the history
incomplete implementation of cd
  • Loading branch information
maqamylee0 authored Apr 29, 2023
2 parents d432c58 + 49cf401 commit 9ede876
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
31 changes: 31 additions & 0 deletions _getenv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "main.h"

/**
* _getenv - gets an environment variable
* @name: name of the variable
* @env: environment variables
*
* Return: pointer to variable or NULL (not found)
*/
char *_getenv(const char *name, char **env)
{
char *value = NULL;
char *temp;
int i, j;

i = 0;
while (env[i] != NULL)
{
j = 0;
temp = env[i];
while (name[j] != '\0' && name[j] == temp[j])
j++;
if (name[j] == '\0' && temp[j] == '=')
{
value = temp + j + 1;
break;
}
i++;
}
return (value);
}
17 changes: 16 additions & 1 deletion execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@ void execute(char **argv, char **env)
{
pid_t pid;
int status;
char *cmd_path = NULL;
char *cmd_path = NULL, *cmd = NULL;

if (_strcmp(argv[0], "cd") == 0)
{
cmd = get_cd_path(argv, env);
if (chdir(cmd) != -1)
{
;
}
if (_strcmp(argv[1], "-") == 0)
{
_puts(cmd);
_putchar('\n');
}
return;
}

if (_strchr(argv[0], '/') != NULL)
{
Expand Down
27 changes: 27 additions & 0 deletions get_cd_path.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "main.h"

/**
* get_cd_path - gets path for cd
* @argv: array of arguments
* @env: environment variable
*
* Return: path to change directory to
*/
char *get_cd_path(char **argv, char **env)
{
char *path = NULL;

if (argv[1] == NULL)
{
path = _getenv("HOME", env);
}
else if (_strcmp(argv[1], "-") == 0)
{
path = _getenv("OLDPWD", env);
}
else
{
path = argv[1];
}
return (path);
}
2 changes: 2 additions & 0 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ int has_space(char *input);
int _isletter(int c);
char *_realloc(char *ptr, size_t size);
char *_strtok(char *str, const char *delim);
char *_getenv(const char *name, char **env);
char *get_cd_path(char **argv, char **env);

#endif /* _MAIN__H_ */
Empty file added peter/emmily
Empty file.

0 comments on commit 9ede876

Please sign in to comment.