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

Commit

Permalink
fix getcwd; builtin: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nicknamemohaji committed Apr 9, 2024
1 parent c26ab1c commit 8c5a18b
Show file tree
Hide file tree
Showing 19 changed files with 184 additions and 74 deletions.
1 change: 1 addition & 0 deletions codes/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ LOADER_FILES = loader/ldpre_param_expansion.c \
loader/ldpre_ast_exec_execall.c \
loader/loader_wrapper.c
UTIL_FILES = utils/do_exit.c \
utils/do_getcwd.c \
utils/free_ft_split.c \
utils/ld_chdir.c \
utils/ld_errno_file.c \
Expand Down
39 changes: 25 additions & 14 deletions codes/builtins/builtin_cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,49 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 06:04:33 by kyungjle #+# #+# */
/* Updated: 2024/04/05 14:51:09 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 15:19:52 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

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

int builtin_cd(char *args[], t_ld_map_env *env);
int builtin_cd(char *args[], t_ld_map_env *env);
static int no_such_file_or_directory(void);

/*
int builtin_cd(char *args[], t_ld_map_env *env)
:args: arguments
:env: environmen variables, used for changing PWD
:return: execution result (0 if cd was successful)
*/
int builtin_cd(char *args[], t_ld_map_env *env)
{
char *key_pwd;

if (!builtin_check_argument_count((const char **)args, 2))
return (EXIT_FAILURE);
if (args[1] != NULL)
{
if (args[1][0] == '/')
{
if (access(args[1], F_OK | X_OK) != 0 || chdir(args[1]) != 0)
{
printf("no such file");
return (EXIT_FAILURE);
}
return (no_such_file_or_directory());
}
else if ((ft_strchr(args[1], '/') != NULL && ld_chdir(args[1]) != TRUE)
else if ((ft_strchr(args[1], '/') != NULL
&& ld_chdir("cd", args[1]) != TRUE)
|| (ft_strchr(args[1], '/') == NULL && chdir(args[1])))
return (EXIT_FAILURE);
key_pwd = ft_strdup("PWD");
if (key_pwd == NULL)
do_exit("builtin_cd.malloc");
ldpre_env_add(key_pwd, getcwd(NULL, 0), env);
return (no_such_file_or_directory());
ldpre_env_add(ft_strdup("PWD"), do_getcwd_f(NULL, 0), env);
}
return (EXIT_SUCCESS);
}

/*
static int no_such_file_or_directory(void)
used to avoid line constraint, aka 'norm function'...
*/
static int no_such_file_or_directory(void)
{
printf("no such file or directory\n");
return (EXIT_FAILURE);
}
8 changes: 7 additions & 1 deletion codes/builtins/builtin_echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/05 12:45:54 by kyungjle #+# #+# */
/* Updated: 2024/04/05 12:46:00 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 14:55:55 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

#include "builtin.h"

int builtin_echo(char *args[], t_ld_map_env *env);

/*
int builtin_echo(char *args[], t_ld_map_env *env)
:args: arguments
:env: not used
:return: execution result(always 0)
*/
int builtin_echo(char *args[], t_ld_map_env *env)
{
int i;
Expand Down
11 changes: 9 additions & 2 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 14:39:38 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 15:01:34 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,6 +16,12 @@
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)
:args: not used
:env: environment variables, used to print
:return: execution result (always 0)
*/
int builtin_env(char *args[], t_ld_map_env *env)
{
(void)args;
Expand All @@ -29,7 +35,8 @@ static int builtin_env_print(t_ld_map_env *env)
node = (env->contents)->next;
while (node != NULL)
{
printf("%s=%s\n", node->key, node->value);
if (*(node->value) != '\0')
printf("%s=%s\n", node->key, node->value);
node = node->next;
}
return (EXIT_SUCCESS);
Expand Down
24 changes: 23 additions & 1 deletion 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/09 12:34:47 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 14:59:46 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,6 +17,13 @@ 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);

/*
int builtin_exit(char *args[], t_ld_map_env *env)
:args: arguments
:env: environment variables, used to free when exiting
:return: 1 if execution fails. if execution was successful, the process
will return with argument given (0 if no argument given)
*/
int builtin_exit(char *args[], t_ld_map_env *env)
{
printf("exit\n");
Expand All @@ -33,6 +40,13 @@ int builtin_exit(char *args[], t_ld_map_env *env)
exit(EXIT_SUCCESS);
}

/*
static t_bool check_digit(const char *c)
:c: string to be checked
:return: true if all characters in string is digit
check for string is only digit
*/
static t_bool check_digit(const char *c)
{
const char *ptr;
Expand All @@ -50,6 +64,14 @@ static t_bool check_digit(const char *c)
return (TRUE);
}

/*
t_bool builtin_check_argument_count(const char *args[], int limit)
:args: argument list to be checked
:limit: limit
: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)
{
int count;
Expand Down
9 changes: 8 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/05 16:13:32 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 15:04:29 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,6 +17,13 @@ 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)
:args: arguments. if no argument is given, print all the environment variables,
and if argument is given, register environment variables
:env: environment variables, used to set or print
:return: execution result (always 0)
*/
int builtin_export(char *args[], t_ld_map_env *env)
{
int i;
Expand Down
10 changes: 8 additions & 2 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 13:47:00 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 15:12:32 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,6 +15,12 @@

int builtin_pwd(char *args[], t_ld_map_env *env);

/*
int builtin_pwd(char *args[], t_ld_map_env *env)
:args: not used
:env: environment variables, used to get PWD
:return: execution result (always 0)
*/
int builtin_pwd(char *args[], t_ld_map_env *env)
{
char *pwd;
Expand All @@ -23,7 +29,7 @@ int builtin_pwd(char *args[], t_ld_map_env *env)
pwd = ldpre_env_fetch("PWD", env);
if (pwd == NULL)
{
pwd = getcwd(NULL, 0);
pwd = do_getcwd_f(NULL, 0);
printf("%s\n", pwd);
ldpre_env_add(ft_strdup("PWD"), pwd, env);
}
Expand Down
10 changes: 8 additions & 2 deletions codes/builtins/builtin_unset.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* builtin_unset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nicknamemohaji <nicknamemohaji@student. +#+ +:+ +#+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 06:04:47 by nicknamemoh #+# #+# */
/* Updated: 2024/03/27 06:04:48 by nicknamemoh ### ########.fr */
/* Updated: 2024/04/09 15:05:04 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,6 +15,12 @@

int builtin_unset(char *args[], t_ld_map_env *env);

/*
int builtin_unset(char *args[], t_ld_map_env *env)
:args: unsets keys given
:env: environment variables, used to unset
:return: execution result (always 0)
*/
int builtin_unset(char *args[], t_ld_map_env *env)
{
int i;
Expand Down
19 changes: 15 additions & 4 deletions codes/builtins/builtin_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@
/* ::: :::::::: */
/* builtin_wrapper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nicknamemohaji <nicknamemohaji@student. +#+ +:+ +#+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 06:04:49 by nicknamemoh #+# #+# */
/* Updated: 2024/04/01 13:18:40 by nicknamemoh ### ########.fr */
/* Updated: 2024/04/09 15:07:35 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

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

pid_t builtin_wrapper(t_ld_exec exec, t_ld_map_env *env);
int builtin_wrapper(t_ld_exec exec, t_ld_map_env *env);
t_bool builtin_isbuiltin(char *name);

pid_t builtin_wrapper(t_ld_exec exec, t_ld_map_env *env)
/*
int builtin_wrapper(t_ld_exec exec, t_ld_map_env *env)
:exec: execution infos. only use argv member variable
:env: environment variables
:return: execution result
*/
int builtin_wrapper(t_ld_exec exec, t_ld_map_env *env)
{
if (ft_strncmp(exec.argv[0], "cd", 2) == 0)
return (builtin_cd(exec.argv, env));
Expand All @@ -36,6 +42,11 @@ pid_t builtin_wrapper(t_ld_exec exec, t_ld_map_env *env)
return (EXIT_FAILURE);
}

/*
t_bool builtin_isbuiltin(char *name)
:name: command name to check
:return: true if command is builtin function
*/
t_bool builtin_isbuiltin(char *name)
{
if (ft_strncmp(name, "cd", 2) == 0)
Expand Down
4 changes: 2 additions & 2 deletions codes/includes/input.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/12 22:17:56 by kyungjle #+# #+# */
/* Updated: 2024/04/05 16:19:20 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 14:48:42 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,7 +25,7 @@

// input_readline.c

char *input_readline_f(void);
char *input_readline_f(t_ld_map_env *env);

// input_terminal.c

Expand Down
6 changes: 3 additions & 3 deletions codes/includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nicknamemohaji <nicknamemohaji@student. +#+ +:+ +#+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 22:18:00 by kyungjle #+# #+# */
/* Updated: 2024/04/01 13:08:25 by nicknamemoh ### ########.fr */
/* Updated: 2024/04/09 15:05:56 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,7 +23,7 @@

// builtin/builtin_wrapper.c

pid_t builtin_wrapper(t_ld_exec exec, t_ld_map_env *env);
int builtin_wrapper(t_ld_exec exec, t_ld_map_env *env);
t_bool builtin_isbuiltin(char *name);

// loader/loader_wrapper.c
Expand Down
12 changes: 8 additions & 4 deletions 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/05 15:43:58 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 15:18:07 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -29,15 +29,19 @@ void free_ft_split(char **ptr);

// ld_errno_file.c

t_bool ld_errno_file(char *trace, char *path);
t_bool ld_errno_file(const char *trace, char *path);

// ld_chdir.c

t_bool ld_chdir(char *path);
t_bool ld_chdir(const char *trace, char *path);

// do_exit.c

void do_exit(char *errorstr);
void do_exit(const char *errorstr);

// do_getcwd.c

char *do_getcwd_f(char *buf, size_t size);

// ld_map_functions.c

Expand Down
Loading

0 comments on commit 8c5a18b

Please sign in to comment.