This commit is contained in:
Loic Deridder
2025-01-15 13:41:40 +01:00
parent eedfe4a0ee
commit 0ec78ee34c
11 changed files with 182 additions and 48 deletions

View File

@@ -24,14 +24,52 @@ int is_silent(char *str)
return (0);
}
void builtin_echo(char *arg)
static int extractenv(char *str, char **envp)
{
int i;
char *var;
char *tmp;
i = 0;
(void)envp;
while (str[i] && str[i] != ' ')
i++;
if (i >= 1)
tmp = ft_substr(str, 1, i - 1);
var = getenv(tmp);
free(tmp);
if (var)
ft_printf("%s", var);
return (i);
}
static void echo_print(char *str, char **envp)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == '$')
{
if (!str[i + 1] || str[i + 1] == ' ')
ft_put_c(str[i++]);
else
i += extractenv(&str[i], envp);
}
else
ft_put_c(str[i++]);
}
}
void builtin_echo(char *arg, char **envp)
{
int i;
i = 4;
while (arg[i] && is_silentchar(arg[i]))
i++;
printf("%s", &arg[i]);
echo_print(&arg[i], envp);
if (!is_silent(arg))
printf("\n");
}