aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-14 18:33:45 +0100
committerGitHub2025-01-14 18:33:45 +0100
commit430521cc47e9f47fbd1005fade9251c31d9f3f18 (patch)
tree14d1f541418576d565f42f0d70faf1369fc17d1b /src
parentda80405b345b58f9e1bd83039b1df3771b5ca193 (diff)
parentd4f3525a01f2566afed0da9209cb1c64bf188776 (diff)
downloadminishell-430521cc47e9f47fbd1005fade9251c31d9f3f18.tar.gz
minishell-430521cc47e9f47fbd1005fade9251c31d9f3f18.zip
Merge pull request #28 from dpu-kaiser/uwu
Some changes
Diffstat (limited to 'src')
-rw-r--r--src/builtins_part_one.c23
-rw-r--r--src/env_to_strlst.c7
-rw-r--r--src/format_string.c12
-rw-r--r--src/main.c2
-rw-r--r--src/repl.c2
5 files changed, 38 insertions, 8 deletions
diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c
index d8bdaa9..9f50157 100644
--- a/src/builtins_part_one.c
+++ b/src/builtins_part_one.c
@@ -11,6 +11,7 @@
/* ************************************************************************** */
#include "env.h"
+#include <stdio.h>
int echo(char **av)
{
@@ -22,7 +23,7 @@ int echo(char **av)
if (av[1] == NULL || av[1][0] == '\0')
{
write(1, "\n", 1);
- return (1);
+ return (0);
}
if (ft_strncmp(av[1], "-n", 3) == 0)
{
@@ -68,6 +69,8 @@ int unset(char **av, t_env **env)
i = 0;
while (av[++i])
{
+ if (ft_strchr(av[i], '?'))
+ return (1);
current = *env;
prev = NULL;
while (current)
@@ -113,6 +116,8 @@ int export(char **av, t_env **env)
{
tmp = ft_strchr(av[i], '=');
*tmp = '\0';
+ if (ft_strchr(av[i], '?'))
+ return (1);
current = check_existing(*env, av[i]);
if (current)
free(current->value);
@@ -127,3 +132,19 @@ int export(char **av, t_env **env)
}
return (0);
}
+
+void set_return_code(int return_code, t_env **env)
+{
+ t_env *cur;
+
+ cur = check_existing(*env, "?");
+ if (cur)
+ free(cur->value);
+ else
+ {
+ cur = env_new(ft_strdup("?"));
+ cur->next = *env;
+ *env = cur;
+ }
+ cur->value = ft_itoa(return_code);
+}
diff --git a/src/env_to_strlst.c b/src/env_to_strlst.c
index 2031c24..40aaf9d 100644
--- a/src/env_to_strlst.c
+++ b/src/env_to_strlst.c
@@ -26,6 +26,11 @@ char **env_to_strlst(t_env *env)
cur = env;
while (cur != NULL)
{
+ if (ft_strchr(cur->name, '?'))
+ {
+ cur = cur->next;
+ continue;
+ }
size++;
cur = cur->next;
}
@@ -36,6 +41,8 @@ char **env_to_strlst(t_env *env)
cur = env;
while (i < size)
{
+ if (ft_strchr(cur->name, '?'))
+ cur = cur->next;
result[i] = get_var_assign(cur);
cur = cur->next;
i++;
diff --git a/src/format_string.c b/src/format_string.c
index 7168ea0..5f31130 100644
--- a/src/format_string.c
+++ b/src/format_string.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:30:11 by chuhlig #+# #+# */
-/* Updated: 2025/01/14 14:21:36 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 18:06:17 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,11 +22,11 @@ char *format_string(char *str, t_env *env)
char *result;
int pos;
int start;
- int mode;
+ int is_literal;
pos = 0;
start = 0;
- mode = 0;
+ is_literal = 0;
result = NULL;
if (str == NULL)
return (NULL);
@@ -36,14 +36,14 @@ char *format_string(char *str, t_env *env)
{
append_slice(&result, str, start, pos);
start = pos + 1;
- mode ^= 1;
+ is_literal = !is_literal;
}
- if (str[pos] == '"' && !(mode & 1))
+ if (str[pos] == '"' && !is_literal)
{
append_slice(&result, str, start, pos);
start = pos + 1;
}
- if (str[pos] == '$' && !(mode & 1))
+ if (str[pos] == '$' && !is_literal)
{
append_slice(&result, str, start, pos);
append_var(&result, str, &pos, env);
diff --git a/src/main.c b/src/main.c
index 8abc4c9..8cebdd1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -24,6 +24,8 @@ int main(int argc, char *argv[], char *envp[])
if (init())
return (1);
getenvlst(&env, envp);
+
+ set_return_code(0, &env);
repl("Minishell $ ", &env, &promptflag);
free_envlst(&env);
return (0);
diff --git a/src/repl.c b/src/repl.c
index 7eb6c0d..5079ee8 100644
--- a/src/repl.c
+++ b/src/repl.c
@@ -37,7 +37,7 @@ void repl(const char *prompt, t_env **env, int *promptflag)
tokenizer(input, &token_list, '\0');
lines = parse(token_list, env);
if (lines)
- eval(lines->content, env);
+ set_return_code(eval(lines->content, env), env);
free(input);
}
}