update: functions done: echo + echo -n, pwd, exit with and without arguments / parser appears to work, test with commands and local files (only tested with ls -l)
This commit is contained in:
28
src/builtins/cd.c
Normal file
28
src/builtins/cd.c
Normal file
@@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/08/01 11:09:13 by sede-san #+# #+# */
|
||||
/* Updated: 2025/08/03 02:11:18 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int cd_builtin(
|
||||
int argc,
|
||||
char const *argv[])
|
||||
{
|
||||
char *path;
|
||||
|
||||
if (argc == 1)
|
||||
path = getenv("HOME");
|
||||
else
|
||||
path = (char *)argv[1];
|
||||
if (chdir(path) != 0)
|
||||
return (fprintf(stderr, "cd: %s\n", path), 1);
|
||||
return (0);
|
||||
}
|
||||
106
src/builtins/echo.c
Normal file
106
src/builtins/echo.c
Normal file
@@ -0,0 +1,106 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* echo.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/08/01 13:22:11 by sede-san #+# #+# */
|
||||
/* Updated: 2025/08/03 02:15:38 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static t_echo_flags set_flags(char const *argv[], size_t *i);
|
||||
static void set_default_flags(t_echo_flags *flags);
|
||||
|
||||
/**
|
||||
* @brief Executes the echo builtin command.
|
||||
*
|
||||
* This function processes the arguments passed to the echo command,
|
||||
* handles any flags (such as -n for no trailing newline), and prints
|
||||
* the arguments to standard output separated by spaces.
|
||||
*
|
||||
* @param argc The number of arguments in argv.
|
||||
* @param argv The array of argument strings, with argv[0] being "echo".
|
||||
*
|
||||
* @return EXIT_SUCCESS on successful execution.
|
||||
*/
|
||||
int echo_builtin(
|
||||
int argc,
|
||||
char const *argv[])
|
||||
{
|
||||
t_echo_flags flags;
|
||||
size_t i;
|
||||
|
||||
(void)argc;
|
||||
flags = set_flags(argv, &i);
|
||||
while (argv[i])
|
||||
{
|
||||
ft_putstr((char *)argv[i]);
|
||||
if (argv[++i])
|
||||
ft_putchar(' ');
|
||||
}
|
||||
if (flags.newline)
|
||||
ft_putchar('\n');
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parses command-line arguments to set flags for the echo builtin.
|
||||
*
|
||||
* This function examines the arguments passed to the echo command, starting
|
||||
* from argv[1], and sets the appropriate flags based on the presence of
|
||||
* options (e.g., "-n"). It updates the index `i` to point to the first
|
||||
* non-option argument.
|
||||
*
|
||||
* @param argv Array of argument strings (NULL-terminated).
|
||||
* @param i Pointer to a size_t variable that will be updated to the index
|
||||
* of the first non-option argument.
|
||||
*
|
||||
* @return t_echo_flags Structure containing the parsed flags (e.g., whether
|
||||
* to print a trailing newline).
|
||||
*/
|
||||
static t_echo_flags set_flags(
|
||||
char const *argv[],
|
||||
size_t *i
|
||||
)
|
||||
{
|
||||
t_echo_flags flags;
|
||||
size_t j;
|
||||
|
||||
set_default_flags(&flags);
|
||||
*i = 1;
|
||||
while (*argv[*i] == '-')
|
||||
{
|
||||
j = 1;
|
||||
if (!argv[*i][j])
|
||||
return (flags);
|
||||
while (argv[*i][j])
|
||||
{
|
||||
if (argv[*i][j] == 'n')
|
||||
flags.newline = 0;
|
||||
else
|
||||
return (set_default_flags(&flags), flags);
|
||||
j++;
|
||||
}
|
||||
(*i)++;
|
||||
}
|
||||
return (flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initializes the t_echo_flags structure with default values.
|
||||
*
|
||||
* This function zeroes out all fields of the given t_echo_flags structure
|
||||
* and sets the 'newline' flag to 1, indicating that a newline should be
|
||||
* printed by default.
|
||||
*
|
||||
* @param flags Pointer to the t_echo_flags structure to initialize.
|
||||
*/
|
||||
static void set_default_flags(t_echo_flags *flags)
|
||||
{
|
||||
ft_bzero(flags, sizeof(t_echo_flags));
|
||||
flags->newline = 1;
|
||||
}
|
||||
36
src/builtins/env.c
Normal file
36
src/builtins/env.c
Normal file
@@ -0,0 +1,36 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* env.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/08/01 14:15:09 by sede-san #+# #+# */
|
||||
/* Updated: 2025/08/03 02:33:30 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
// static void printenv(char **envp);
|
||||
|
||||
int env_builtin(
|
||||
int argc,
|
||||
char const *argv[])
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
// if (!argv || !*argv)
|
||||
// printenv(envp);
|
||||
return (0);
|
||||
}
|
||||
|
||||
// static void printenv(
|
||||
// char **envp)
|
||||
// {
|
||||
// size_t i;
|
||||
|
||||
// i = 0;
|
||||
// while (envp[i])
|
||||
// ft_putendl(envp[i++]);
|
||||
// }
|
||||
62
src/builtins/exit.c
Normal file
62
src/builtins/exit.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* exit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/08/01 14:38:18 by sede-san #+# #+# */
|
||||
/* Updated: 2025/08/03 02:11:33 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
/**
|
||||
* @brief Handles the shell's exit builtin command.
|
||||
*
|
||||
* Prints "exit" to standard output and determines the exit code based on the
|
||||
* arguments provided.
|
||||
*
|
||||
* @param argc The number of arguments passed to the exit command.
|
||||
* @param argv The array of argument strings.
|
||||
* @return The exit code to be used by the shell.
|
||||
*
|
||||
* Behavior:
|
||||
* - If no additional arguments are provided (argc == 1), exits with code 0.
|
||||
* - If more than one argument is provided:
|
||||
* - If the first argument is not a valid numeric string, prints an error
|
||||
* message to stderr and exits with error code 2.
|
||||
* - If the first argument is valid but there are too many arguments,
|
||||
* prints an error message to stderr and returns -1. In this case the
|
||||
* shell must not exit.
|
||||
* - Otherwise, parses the first argument as a long integer and casts it to an
|
||||
* unsigned char, then to an unsigned int to ensure the exit code is within
|
||||
* the range 0-255.
|
||||
*/
|
||||
int exit_builtin(
|
||||
int argc,
|
||||
char const *argv[])
|
||||
{
|
||||
int exit_code;
|
||||
|
||||
ft_putendl("exit");
|
||||
if (argc == 1)
|
||||
exit_code = 0;
|
||||
else if (argc > 1)
|
||||
{
|
||||
if (!ft_isdigit(*argv[1]) || !ft_strisnum(argv[1]))
|
||||
{
|
||||
fprintf(stderr, "exit: %s: numeric argument required", argv[1]);
|
||||
exit_code = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "exit: too many arguments");
|
||||
exit_code = -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
exit_code = (unsigned int)((unsigned char)(ft_atol(argv[1])));
|
||||
return (exit_code);
|
||||
}
|
||||
0
src/builtins/export.c
Normal file
0
src/builtins/export.c
Normal file
27
src/builtins/pwd.c
Normal file
27
src/builtins/pwd.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pwd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/08/01 11:52:33 by sede-san #+# #+# */
|
||||
/* Updated: 2025/08/03 02:11:02 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int pwd_builtin(
|
||||
int argc,
|
||||
char const *argv[])
|
||||
{
|
||||
char cwd[PATH_MAX];
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
if (!getcwd(cwd, sizeof(cwd)))
|
||||
ft_eputendl("getcwd()");
|
||||
ft_putendl(cwd);
|
||||
return (0);
|
||||
}
|
||||
0
src/builtins/unset.c
Normal file
0
src/builtins/unset.c
Normal file
Reference in New Issue
Block a user