Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
fix builtin errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nicknamemohaji committed Apr 5, 2024
1 parent 3e6c210 commit f464613
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 49 deletions.
3 changes: 2 additions & 1 deletion codes/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ UTIL_FILES = utils/do_exit.c \
utils/ld_chdir.c \
utils/ld_errno_file.c \
utils/ld_map_functions.c \
utils/ld_map_functions2.c
utils/ld_map_functions2.c \
utils/ft_qsort.c
BUILTIN_FILES = builtins/builtin_cd.c \
builtins/builtin_echo.c \
builtins/builtin_exit.c \
Expand Down
4 changes: 2 additions & 2 deletions codes/builtins/builtin_cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 06:04:33 by kyungjle #+# #+# */
/* Updated: 2024/04/05 12:45:42 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 14:51:09 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -32,7 +32,7 @@ int builtin_cd(char *args[], t_ld_map_env *env)
}
}
else if ((ft_strchr(args[1], '/') != NULL && ld_chdir(args[1]) != TRUE)
|| (ft_strchr(args[1], '/') == NULL && !chdir(args[1])))
|| (ft_strchr(args[1], '/') == NULL && chdir(args[1])))
return (EXIT_FAILURE);
key_pwd = ft_strdup("PWD");
if (key_pwd == NULL)
Expand Down
7 changes: 3 additions & 4 deletions codes/builtins/builtin_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 06:04:39 by kyungjle #+# #+# */
/* Updated: 2024/04/05 13:00:50 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 14:39:38 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,11 +26,10 @@ static int builtin_env_print(t_ld_map_env *env)
{
t_ld_map_node *node;

node = env->contents;
node = (env->contents)->next;
while (node != NULL)
{
if (ldpre_env_validate_key(node->key) == TRUE)
printf("%s=%s\n", node->key, node->value);
printf("%s=%s\n", node->key, node->value);
node = node->next;
}
return (EXIT_SUCCESS);
Expand Down
32 changes: 29 additions & 3 deletions codes/builtins/builtin_export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 06:04:43 by nicknamemoh #+# #+# */
/* Updated: 2024/04/05 14:05:37 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 15:19:43 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

#include "builtin.h"
#include "utils.h"

int builtin_export(char *args[], t_ld_map_env *env);
static int builtin_export_print(t_ld_map_env *env);
int builtin_export(char *args[], t_ld_map_env *env);
static int builtin_export_print(t_ld_map_env *env);
static t_bool cmp(const void *c1, const void *c2);


int builtin_export(char *args[], t_ld_map_env *env)
{
Expand Down Expand Up @@ -50,8 +52,32 @@ static int builtin_export_print(t_ld_map_env *env)
char **envp;

envp = ldpre_env_toenvp_f(env);
ft_qsort((void **)envp, 0, env->count - 1, cmp);
for (int i = 0; envp[i] != NULL; i++)
printf("%s\n", envp[i]);
free_ft_split(envp);
return (EXIT_SUCCESS);
}

static t_bool cmp(const void *c1, const void *c2)
{
unsigned char *c1_ptr;
unsigned char *c2_ptr;

if (c1 == c2)
return (FALSE);
c1_ptr = (unsigned char *) c1;
c2_ptr = (unsigned char *) c2;
while (*c1_ptr != '=' && *c2_ptr != '=')
{
if (*c1_ptr != *c2_ptr)
break ;
c1_ptr++;
c2_ptr++;
}
if (*c1_ptr == '=')
c1_ptr = (unsigned char *)"\0";
if (*c2_ptr == '=')
c2_ptr = (unsigned char *)"\0";
return (*c1_ptr < *c2_ptr);
}
32 changes: 13 additions & 19 deletions codes/loader/ldexec_env_exitcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/21 13:44:12 by nicknamemoh #+# #+# */
/* Updated: 2024/03/21 17:39:55 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 14:23:31 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,8 +21,7 @@ char *ldexec_env_exitcode_fetch_f(t_ld_map_env *env)
:param env: environment variable map
:return: itoa'ed last exitcode
Fetches `$?` from map. `$?` is registered when previous execution finished,
so it might not exist. bash's default is 0, so we follow bash's behavior.
Fetches `$?` from map. `$?` is always at first node of env map.
`$?` is not a valid key, so created special methods. user cannot access,
modify, delete to this environment variable, since fetch/add/remove interface
Expand All @@ -31,13 +30,10 @@ validates the key before doing action.
char *ldexec_env_exitcode_fetch_f(t_ld_map_env *env)
{
char *ret;
t_ld_map_node **nodes;
t_ld_map_node *node;

nodes = ldpre_env_searchkey("?", env);
if (nodes == NULL)
ret = ft_itoa(0);
else
ret = ft_strdup(nodes[0]->value);
node = env->contents;
ret = ft_strdup(node->value);
if (ret == NULL)
do_exit("ldexec_env_exitcode_fetch_f.malloc");
return (ret);
Expand All @@ -52,17 +48,14 @@ Update/Adds last status code(`$?`) to map
*/
void ldexec_env_exitcode_update(int code, t_ld_map_env *env)
{
t_ld_map_node **nodes;
t_ld_map_node *node;
t_ld_map_node *next;

nodes = ldpre_env_searchkey("?", env);
if (nodes != NULL)
{
nodes[1]->next = nodes[0]->next;
free(nodes[0]->key);
free(nodes[0]->value);
free(nodes[0]);
}
node = env->contents;
next = node->next;
free(node->key);
free(node->value);
free(node);
node = malloc(1 * sizeof(t_ld_map_node));
if (node == NULL)
do_exit("ldexec_env_exitcode_update.malloc");
Expand All @@ -73,5 +66,6 @@ void ldexec_env_exitcode_update(int code, t_ld_map_env *env)
if (node->value == NULL)
do_exit("ldexec_env_exitcode_update.malloc");
node->next = NULL;
ld_map_node_attach(env, node);
node->next = next;
env->contents = node;
}
4 changes: 3 additions & 1 deletion codes/loader/ldpre_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 11:20:45 by kyungjle #+# #+# */
/* Updated: 2024/04/05 12:54:20 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 15:18:01 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,6 +23,8 @@ int ldpre_ast(t_ast_node *ast, t_ld_map_env *env,
{
case NODE_COMMAND:
{
// TODO 커맨드 실행 구조 검토
// 파이프일때 손자까지 만들 필요가 있나...
t_ld_exec_nodes *node;
t_bool free_flag;
pid_t pid;
Expand Down
3 changes: 2 additions & 1 deletion codes/loader/ldpre_ast_redir.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 18:41:08 by kyungjle #+# #+# */
/* Updated: 2024/04/05 12:44:40 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 14:56:04 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -73,6 +73,7 @@ static t_bool infile_heredoc(char *filename,
if (access(heredoc.heredoc_name, F_OK | W_OK | R_OK) != 0)
return (ld_errno_file("ldpre_ast_redir.access", heredoc.heredoc_name));
delim_len = ft_strlen(filename);
// TODO heredoc delim에는 param expansion 없이 quote removal만 진행되여야 함
filename = ldpre_param_quote_f(ft_strdup(filename), env, NULL);
expansion = (ft_strlen(filename) == delim_len);
fd = dup(heredoc.stdin_fd);
Expand Down
22 changes: 20 additions & 2 deletions codes/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nicknamemohaji <nicknamemohaji@student. +#+ +:+ +#+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 22:17:43 by kyungjle #+# #+# */
/* Updated: 2024/04/01 13:03:53 by nicknamemoh ### ########.fr */
/* Updated: 2024/04/05 14:26:21 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

volatile sig_atomic_t g_sigint = FALSE;

static void ld_env_set_exitcode(t_ld_map_env *env);

int main(int argc, char *argv[], char *envp[])
{
t_ld_map_env *env;
Expand All @@ -22,6 +24,7 @@ int main(int argc, char *argv[], char *envp[])
(void)argc;
(void)argv;
env = ldpre_env_fromenvp_f(envp);
ld_env_set_exitcode(env);
while (1)
{
input = input_readline_f();
Expand All @@ -41,3 +44,18 @@ int main(int argc, char *argv[], char *envp[])
rl_clear_history();
return (0);
}

static void ld_env_set_exitcode(t_ld_map_env *env)
{
t_ld_map_node *node;

node = malloc(1 * sizeof(t_ld_map_node));
if (node == NULL)
do_exit("main.ld_env_set_exitcode.malloc");
node->key = ft_strdup("?");
node->value = ft_itoa(0);
if (node->key == NULL || node->value == NULL)
do_exit("main.ld_env_set_exitcode.malloc");
node->next = env->contents;
env->contents = node;
}
2 changes: 1 addition & 1 deletion codes/utils/free_ft_split.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 19:53:33 by nicknamemoh #+# #+# */
/* Updated: 2024/04/04 15:44:11 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 14:57:55 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
8 changes: 4 additions & 4 deletions codes/utils/ft_qsort.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_qsort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dogwak <dogwak@student.42.fr> +#+ +:+ +#+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 17:59:28 by dogwak #+# #+# */
/* Updated: 2024/04/04 19:42:41 by dogwak ### ########.fr */
/* Updated: 2024/04/05 15:15:17 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,8 +26,8 @@
t_bool compare(void *pa, void *pb)
if a is greater than b -> *pa > *pb, descending order.
if a is less than b -> *pa < *pb, ascending order.
if a is greater than b -> return *pa > *pb, descending order.
if a is less than b -> return *pa < *pb, ascending order.
*/
void ft_qsort(void **arr, int left, int right,
t_bool (*cmp)(const void*, const void*))
Expand Down
22 changes: 11 additions & 11 deletions codes/utils/ld_map_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/19 00:09:12 by nicknamemoh #+# #+# */
/* Updated: 2024/03/21 17:38:55 by kyungjle ### ########.fr */
/* Updated: 2024/04/05 15:15:08 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -116,20 +116,20 @@ char **ldpre_env_toenvp_f(t_ld_map_env *map)
ret = malloc(sizeof(char *) * (map->count + 1));
if (ret == NULL)
do_exit("ldpre_env_toenvp_f.malloc");
index = -1;
node = map->contents;
while (++index < map->count)
index = 0;
node = (map->contents)->next;
while (++index <= map->count)
{
ret[index] = malloc(sizeof(char) * (1 + 1
ret[index - 1] = malloc(sizeof(char) * (1 + 1
+ ft_strlen(node->key) + ft_strlen(node->value)));
if (ret == NULL)
if (ret[index - 1] == NULL)
do_exit("ldpre_env_toenvp_f.malloc");
ft_strlcpy(ret[index], node->key, ft_strlen(node->key) + 1);
ft_strlcat(ret[index], "=", ft_strlen(ret[index]) + 2);
ft_strlcat(ret[index], node->value,
ft_strlen(ret[index]) + ft_strlen(node->value) + 1);
ft_strlcpy(ret[index - 1], node->key, ft_strlen(node->key) + 1);
ft_strlcat(ret[index - 1], "=", ft_strlen(ret[index - 1]) + 2);
ft_strlcat(ret[index - 1], node->value,
ft_strlen(ret[index - 1]) + ft_strlen(node->value) + 1);
node = node->next;
}
ret[index] = NULL;
ret[index - 1] = NULL;
return (ret);
}
1 change: 1 addition & 0 deletions codes/utils/ld_map_functions2.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ void ldpre_env_add(char *key, char *value, t_ld_map_env *map)
node->value = value_copy;
node->next = NULL;
ld_map_node_attach(map, node);
map->count++;
return ;
}

Expand Down

0 comments on commit f464613

Please sign in to comment.