mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
builtin: implement pwd
This commit is contained in:
parent
1c4733b1cc
commit
5ce4a2b85f
7 changed files with 167 additions and 2 deletions
2
Makefile
2
Makefile
|
|
@ -30,6 +30,8 @@ srcs = \
|
||||||
src/executing/here_doc/here_doc_expand_line.c \
|
src/executing/here_doc/here_doc_expand_line.c \
|
||||||
src/executing/here_doc/random_filename.c \
|
src/executing/here_doc/random_filename.c \
|
||||||
src/executing/here_doc/strip_newline.c \
|
src/executing/here_doc/strip_newline.c \
|
||||||
|
src/executing/simple_cmd/builtin_invalid.c \
|
||||||
|
src/executing/simple_cmd/builtin_pwd.c \
|
||||||
src/executing/simple_cmd/simple_cmd_execute.c \
|
src/executing/simple_cmd/simple_cmd_execute.c \
|
||||||
src/ft_errno.c \
|
src/ft_errno.c \
|
||||||
src/get_command.c \
|
src/get_command.c \
|
||||||
|
|
|
||||||
20
src/executing/simple_cmd/builtin_invalid.c
Normal file
20
src/executing/simple_cmd/builtin_invalid.c
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* builtin_invalid.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/03/31 14:17:14 by khais #+# #+# */
|
||||||
|
/* Updated: 2025/03/31 14:17:42 by khais ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "builtins.h"
|
||||||
|
|
||||||
|
t_builtin_type builtin_invalid(t_simple_cmd *cmd, t_minishell *app)
|
||||||
|
{
|
||||||
|
(void)cmd;
|
||||||
|
(void)app;
|
||||||
|
return (BUILTIN_INVALID);
|
||||||
|
}
|
||||||
58
src/executing/simple_cmd/builtin_pwd.c
Normal file
58
src/executing/simple_cmd/builtin_pwd.c
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* builtin_pwd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/03/31 14:21:52 by khais #+# #+# */
|
||||||
|
/* Updated: 2025/03/31 14:48:23 by khais ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "builtins.h"
|
||||||
|
#include "libft.h"
|
||||||
|
#include "../../ft_errno.h"
|
||||||
|
#include "errno.h"
|
||||||
|
|
||||||
|
static char *get_current_dir(void)
|
||||||
|
{
|
||||||
|
char *path;
|
||||||
|
size_t size;
|
||||||
|
|
||||||
|
size = 64;
|
||||||
|
path = ft_calloc(size, sizeof(char));
|
||||||
|
if (path == NULL)
|
||||||
|
return (ft_errno(FT_EERRNO), NULL);
|
||||||
|
while (getcwd(path, size) == NULL)
|
||||||
|
{
|
||||||
|
if (errno != ERANGE)
|
||||||
|
return (ft_errno(FT_EERRNO), NULL);
|
||||||
|
free(path);
|
||||||
|
size *= 2;
|
||||||
|
path = ft_calloc(size, sizeof(char));
|
||||||
|
if (path == NULL)
|
||||||
|
return (ft_errno(FT_EERRNO), NULL);
|
||||||
|
}
|
||||||
|
return (path);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_builtin_type builtin_pwd(t_simple_cmd *cmd, t_minishell *app)
|
||||||
|
{
|
||||||
|
char *path;
|
||||||
|
|
||||||
|
(void)cmd;
|
||||||
|
path = get_current_dir();
|
||||||
|
if (path == NULL)
|
||||||
|
{
|
||||||
|
app->last_return_value = 1;
|
||||||
|
ft_perror("pwd: failed to get current directory");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_printf("%s\n", path);
|
||||||
|
free(path);
|
||||||
|
app->last_return_value = 0;
|
||||||
|
}
|
||||||
|
return (BUILTIN_PWD);
|
||||||
|
}
|
||||||
21
src/executing/simple_cmd/builtins.h
Normal file
21
src/executing/simple_cmd/builtins.h
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* builtins.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/03/31 14:16:13 by khais #+# #+# */
|
||||||
|
/* Updated: 2025/03/31 14:21:46 by khais ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef BUILTINS_H
|
||||||
|
# define BUILTINS_H
|
||||||
|
|
||||||
|
# include "simple_cmd_execute.h"
|
||||||
|
|
||||||
|
t_builtin_type builtin_invalid(t_simple_cmd *cmd, t_minishell *app);
|
||||||
|
t_builtin_type builtin_pwd(t_simple_cmd *cmd, t_minishell *app);
|
||||||
|
|
||||||
|
#endif // BUILTINS_H
|
||||||
|
|
@ -6,11 +6,12 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
|
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
|
||||||
/* Updated: 2025/03/28 16:56:47 by khais ### ########.fr */
|
/* Updated: 2025/03/31 14:21:40 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "simple_cmd_execute.h"
|
#include "simple_cmd_execute.h"
|
||||||
|
#include "builtins.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
#include "../../subst/subst.h"
|
#include "../../subst/subst.h"
|
||||||
#include "../../env/env_convert.h"
|
#include "../../env/env_convert.h"
|
||||||
|
|
@ -38,6 +39,28 @@ static void command_not_found(t_simple_cmd *cmd)
|
||||||
cmd->words->word->word);
|
cmd->words->word->word);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static t_builtin_type get_builtin(t_simple_cmd *cmd)
|
||||||
|
{
|
||||||
|
char *word;
|
||||||
|
|
||||||
|
word = cmd->words->word->word;
|
||||||
|
if (ft_strcmp("pwd", word) == 0)
|
||||||
|
return (BUILTIN_PWD);
|
||||||
|
return (BUILTIN_INVALID);
|
||||||
|
}
|
||||||
|
|
||||||
|
static t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app)
|
||||||
|
{
|
||||||
|
t_builtin_type type;
|
||||||
|
static t_builtin_type (*builtins[])(t_simple_cmd *, t_minishell *) = {
|
||||||
|
[BUILTIN_INVALID] = builtin_invalid,
|
||||||
|
[BUILTIN_PWD] = builtin_pwd,
|
||||||
|
};
|
||||||
|
|
||||||
|
type = get_builtin(cmd);
|
||||||
|
return (builtins[type](cmd, app));
|
||||||
|
}
|
||||||
|
|
||||||
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
||||||
{
|
{
|
||||||
char *exe;
|
char *exe;
|
||||||
|
|
@ -45,6 +68,8 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
||||||
|
|
||||||
if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL)
|
if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL)
|
||||||
return ;
|
return ;
|
||||||
|
if (execute_builtin(cmd, app) != BUILTIN_INVALID)
|
||||||
|
return ;
|
||||||
exe = get_cmdpath(cmd->words->word->word, app);
|
exe = get_cmdpath(cmd->words->word->word, app);
|
||||||
if (exe == NULL)
|
if (exe == NULL)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/27 16:20:31 by khais #+# #+# */
|
/* Created: 2025/03/27 16:20:31 by khais #+# #+# */
|
||||||
/* Updated: 2025/03/27 16:40:32 by khais ### ########.fr */
|
/* Updated: 2025/03/31 13:59:26 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -18,4 +18,10 @@
|
||||||
|
|
||||||
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app);
|
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app);
|
||||||
|
|
||||||
|
typedef enum e_builtin_type
|
||||||
|
{
|
||||||
|
BUILTIN_INVALID,
|
||||||
|
BUILTIN_PWD,
|
||||||
|
} t_builtin_type;
|
||||||
|
|
||||||
#endif // SIMPLE_CMD_EXECUTE_H
|
#endif // SIMPLE_CMD_EXECUTE_H
|
||||||
|
|
|
||||||
33
test.sh
33
test.sh
|
|
@ -143,6 +143,39 @@ minishell: qwertyuiop: command not found
|
||||||
minishell: poiuytrewq: command not found
|
minishell: poiuytrewq: command not found
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
when_run <<EOF "pwd works"
|
||||||
|
pwd
|
||||||
|
pwd hello
|
||||||
|
pwd hello there
|
||||||
|
pwd -O
|
||||||
|
pwd -O hello
|
||||||
|
pwd there -O hello
|
||||||
|
EOF
|
||||||
|
expecting <<EOF
|
||||||
|
/tmp/dir.minishell
|
||||||
|
/tmp/dir.minishell
|
||||||
|
/tmp/dir.minishell
|
||||||
|
/tmp/dir.minishell
|
||||||
|
/tmp/dir.minishell
|
||||||
|
/tmp/dir.minishell
|
||||||
|
EOF
|
||||||
|
|
||||||
|
when_run <<EOF "pwd works in any directory"
|
||||||
|
mkdir test
|
||||||
|
cd test
|
||||||
|
pwd
|
||||||
|
mkdir hi
|
||||||
|
cd hi
|
||||||
|
pwd
|
||||||
|
cd ..
|
||||||
|
pwd
|
||||||
|
EOF
|
||||||
|
todo <<EOF
|
||||||
|
/tmp/dir.minishell/test
|
||||||
|
/tmp/dir.minishell/test/hi
|
||||||
|
/tmp/dir.minishell/test
|
||||||
|
EOF
|
||||||
|
|
||||||
when_run <<EOF "quoted parentheses are not operators"
|
when_run <<EOF "quoted parentheses are not operators"
|
||||||
echo unclosed '('
|
echo unclosed '('
|
||||||
EOF
|
EOF
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue