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

Commit

Permalink
adding comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nicknamemohaji committed Apr 15, 2024
1 parent 58fde55 commit 1233fce
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 55 deletions.
12 changes: 9 additions & 3 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/09 20:29:12 by kyungjle ### ########.fr */
/* Updated: 2024/04/15 15:05:25 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,7 +26,7 @@ int builtin_cd(char *args[], t_ld_map_env *env)
{
char *pwd;

if (!builtin_check_argument_count((const char **)args, 2))
if (!builtin_check_argument_count((const char **)args, 2, "cd"))
return (EXIT_FAILURE);
if (args[1] != NULL)
{
Expand All @@ -38,7 +38,7 @@ int builtin_cd(char *args[], t_ld_map_env *env)
}
else if (ft_strchr(args[1], '/') != NULL
&& ld_chdir("cd", args[1]) != TRUE)
return (no_such_file_or_directory(NULL, pwd));
return (no_such_file_or_directory(args[1], pwd));
else if (ft_strchr(args[1], '/') == NULL && chdir(args[1]) != 0)
return (no_such_file_or_directory(args[1], pwd));
ldpre_env_add(ft_strdup("PWD"), do_getcwd_f(NULL, 0), env);
Expand All @@ -49,6 +49,12 @@ int builtin_cd(char *args[], t_ld_map_env *env)

/*
static int no_such_file_or_directory(char *path, char *pwd)
:path: path failed to move into. it is from argument, so do not free this..
(arguments will be freed later)
:pwd: CWD before CD job was tried. as ld_chdir function breaks directories
and gradually changes directory, CWD even if cd was unsuccessful may have
been changed. this function will restore CWD and free the string.
:return: execution result (always 1)
used to avoid line constraint, aka 'norm function'...
*/
Expand Down
12 changes: 10 additions & 2 deletions codes/builtins/builtin_echo.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/05 12:45:54 by kyungjle #+# #+# */
/* Updated: 2024/04/09 18:01:44 by kyungjle ### ########.fr */
/* Updated: 2024/04/15 15:01:47 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -27,6 +27,7 @@ int builtin_echo(char *args[], t_ld_map_env *env)
t_bool print_nl;

(void) env;
index = 1;
print_nl = parse_args(args, &index);
while (args[index] != NULL)
{
Expand All @@ -40,13 +41,20 @@ int builtin_echo(char *args[], t_ld_map_env *env)
return (EXIT_SUCCESS);
}

/*
static t_bool parse_args(char *args[], int *index)
:args: arguments to be checked
:index: pointer of index variable to be changed
:return: TRUE if `-n` argument was given
checks for `-n` in arguments, and moves the index if it is an argument
*/
static t_bool parse_args(char *args[], int *index)
{
t_bool ret;
int i;

ret = TRUE;
*index = 1;
while (args[*index] != NULL)
{
if (args[*index][0] != '-')
Expand Down
10 changes: 2 additions & 8 deletions codes/builtins/builtin_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 06:04:39 by kyungjle #+# #+# */
/* Updated: 2024/04/09 15:01:34 by kyungjle ### ########.fr */
/* Updated: 2024/04/15 15:02:39 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

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

int builtin_env(char *args[], t_ld_map_env *env);
static int builtin_env_print(t_ld_map_env *env);

/*
int builtin_env(char *args[], t_ld_map_env *env)
Expand All @@ -23,15 +22,10 @@ int builtin_env(char *args[], t_ld_map_env *env)
:return: execution result (always 0)
*/
int builtin_env(char *args[], t_ld_map_env *env)
{
(void)args;
return (builtin_env_print(env));
}

static int builtin_env_print(t_ld_map_env *env)
{
t_ld_map_node *node;

(void)args;
node = (env->contents)->next;
while (node != NULL)
{
Expand Down
15 changes: 9 additions & 6 deletions codes/builtins/builtin_exit.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:41 by nicknamemoh #+# #+# */
/* Updated: 2024/04/10 16:34:46 by kyungjle ### ########.fr */
/* Updated: 2024/04/15 15:13:18 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,7 +15,8 @@

int builtin_exit(char *args[], t_ld_map_env *env);
static t_bool check_digit(const char *c);
t_bool builtin_check_argument_count(const char *args[], int limit);
t_bool builtin_check_argument_count(const char *args[],
int limit, char *cmd);

/*
int builtin_exit(char *args[], t_ld_map_env *env)
Expand All @@ -26,7 +27,7 @@ will return with argument given (0 if no argument given)
*/
int builtin_exit(char *args[], t_ld_map_env *env)
{
if (!builtin_check_argument_count((const char **) args, 2))
if (!builtin_check_argument_count((const char **) args, 2, "exit"))
return (1);
if (args[1] != NULL)
{
Expand Down Expand Up @@ -70,14 +71,15 @@ static t_bool check_digit(const char *c)
}

/*
t_bool builtin_check_argument_count(const char *args[], int limit)
t_bool builtin_check_argument_count(const char *args[], int limit, char *cmd)
:args: argument list to be checked
:limit: limit
:cmd: command name for printing trace
:return: true if arguments are given less than limit
checks for how many arguments given, checking for max argument count
*/
t_bool builtin_check_argument_count(const char *args[], int limit)
t_bool builtin_check_argument_count(const char *args[], int limit, char *cmd)
{
int count;

Expand All @@ -89,7 +91,8 @@ t_bool builtin_check_argument_count(const char *args[], int limit)
}
if (count > limit)
{
write(2, "exit: too many arguments\n", 25);
write(2, cmd, ft_strlen(cmd));
write(2, ": too many arguments\n", 21);
return (FALSE);
}
else
Expand Down
13 changes: 12 additions & 1 deletion codes/builtins/builtin_export.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:43 by nicknamemoh #+# #+# */
/* Updated: 2024/04/09 15:04:29 by kyungjle ### ########.fr */
/* Updated: 2024/04/15 15:10:47 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -53,6 +53,11 @@ int builtin_export(char *args[], t_ld_map_env *env)
return (EXIT_SUCCESS);
}

/*
static int builtin_export_print(t_ld_map_env *env)
:env: env to print
:return: execution result (always 0)
*/
static int builtin_export_print(t_ld_map_env *env)
{
char **envp;
Expand All @@ -70,6 +75,12 @@ static int builtin_export_print(t_ld_map_env *env)
return (EXIT_SUCCESS);
}

/*
static t_bool cmp(const void *c1, const void *c2)
compare function for ft_qsort. compare up to `=` delimeter.
this will sort env keys ascending.
*/
static t_bool cmp(const void *c1, const void *c2)
{
unsigned char *c1_ptr;
Expand Down
6 changes: 2 additions & 4 deletions codes/builtins/builtin_pwd.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:45 by nicknamemoh #+# #+# */
/* Updated: 2024/04/09 15:12:32 by kyungjle ### ########.fr */
/* Updated: 2024/04/15 15:11:11 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -30,10 +30,8 @@ int builtin_pwd(char *args[], t_ld_map_env *env)
if (pwd == NULL)
{
pwd = do_getcwd_f(NULL, 0);
printf("%s\n", pwd);
ldpre_env_add(ft_strdup("PWD"), pwd, env);
}
else
printf("%s\n", pwd);
printf("%s\n", pwd);
return (EXIT_SUCCESS);
}
4 changes: 2 additions & 2 deletions codes/includes/builtin.h
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:05:01 by nicknamemoh #+# #+# */
/* Updated: 2024/04/05 12:59:12 by kyungjle ### ########.fr */
/* Updated: 2024/04/15 15:05:08 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,7 +34,7 @@ int builtin_echo(char *args[], t_ld_map_env *env);
// builtin_exit.c

int builtin_exit(char *args[], t_ld_map_env *env);
t_bool builtin_check_argument_count(const char *args[], int limit);
t_bool builtin_check_argument_count(const char *args[], int limit, char *cmd);

// builtin_export.c

Expand Down
12 changes: 2 additions & 10 deletions codes/includes/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dogwak <dogwak@student.42.fr> +#+ +:+ +#+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 22:18:03 by kyungjle #+# #+# */
/* Updated: 2024/04/09 15:31:58 by dogwak ### ########.fr */
/* Updated: 2024/04/15 15:16:39 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -33,14 +33,6 @@ typedef enum e_bool
FALSE = 0
} t_bool;

// TODO ast travel status code
typedef enum e_ast_status
{
AST_SUCCESS = 0,
AST_FAIL_REDIR = -2,
AST_SUCCESS_PIPE = -10
} t_ast_status;

extern volatile sig_atomic_t g_sigint;
# define INPUT_READLINE 2

Expand Down
3 changes: 2 additions & 1 deletion codes/includes/utils.h
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:54:44 by nicknamemoh #+# #+# */
/* Updated: 2024/04/09 20:12:51 by kyungjle ### ########.fr */
/* Updated: 2024/04/15 15:16:27 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -59,6 +59,7 @@ t_ld_map_node **ldpre_env_searchkey_f(char *key, t_ld_map_env *map);
t_bool ldpre_env_validate_key(char *key);

// ft_qsort.c

void ft_qsort(void **arr, int left, int right,
t_bool (*cmp)(const void*, const void*));

Expand Down
2 changes: 1 addition & 1 deletion codes/input/input_sighandler.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/12 22:19:05 by kyungjle #+# #+# */
/* Updated: 2024/04/10 14:23:49 by kyungjle ### ########.fr */
/* Updated: 2024/04/15 15:24:53 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
6 changes: 3 additions & 3 deletions 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/09 15:50:43 by kyungjle ### ########.fr */
/* Updated: 2024/04/15 15:14:44 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -37,6 +37,6 @@ int ldpre_ast(t_ast_node *ast, t_ld_map_env *env,
else if (ast->node_type == EXP_PRE_RAPPEND
|| ast->node_type == EXP_PRE_RWRITE)
return (ldpre_ast_wpopen(ast, env, exec, heredoc));
dprintf(2, "unknown node type..!!! %d %s", ast->node_type, ast->pcmd[0]);
return (-2);
printf("unknown node type..!!! %d %s", ast->node_type, ast->pcmd[0]);
return (-42);
}
4 changes: 2 additions & 2 deletions codes/loader/ldpre_ast_exec.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/08 20:04:57 by kyungjle #+# #+# */
/* Updated: 2024/04/09 15:50:43 by kyungjle ### ########.fr */
/* Updated: 2024/04/15 15:17:32 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -46,7 +46,7 @@ int ldpre_ast_exec(t_ast_node *ast, t_ld_map_env *env,
while (exec->next != NULL)
exec = exec->next;
exec->next = node;
return (AST_SUCCESS_PIPE);
return (0);
}
return (exitcode);
}
Expand Down
4 changes: 2 additions & 2 deletions codes/loader/ldpre_ast_pipe.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/08 18:08:58 by kyungjle #+# #+# */
/* Updated: 2024/04/10 00:28:07 by kyungjle ### ########.fr */
/* Updated: 2024/04/15 15:17:02 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -69,5 +69,5 @@ static int run_pipe(t_ast_node *ast, t_ld_map_env *env,
redirect_fd(&preserve_fd[0], STDIN_FD, pipe_fd[0]);
ldpre_ast(ast->right, env, exec, heredoc);
restore_fd(preserve_fd[0], STDIN_FD);
return (AST_SUCCESS_PIPE);
return (0);
}
Loading

0 comments on commit 1233fce

Please sign in to comment.