Skip to content

Commit

Permalink
implement display env variable, handle by env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Ycaro02 authored and Ycaro02 committed Feb 22, 2024
1 parent 11d0b5d commit 212f431
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: nfour <<marvin@42.fr>> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/11/12 16:33:38 by nfour #+# #+# #
# Updated: 2024/02/21 10:10:38 by nfour ### ########.fr #
# Updated: 2024/02/22 15:52:49 by nfour ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -56,6 +56,8 @@ MAIN = tester/src/main_test.c

CALL_TESTER="make -s -C tester"

MALLOC_ENV=$(shell env | grep MALLOC | cut -d '=' -f 1 | tr '\n' ' ')

all: ${NAME}

%.o : %.c
Expand Down Expand Up @@ -89,6 +91,9 @@ testv : ${NAME}
@valgrind ${REPLACE_MALLOC_LIB} ${HELGRIND} ./${TEST}
# @valgrind ${HELGRIND} ./${TEST}

env:
@echo "\033[6;32m${MALLOC_ENV}\033[0m"

clean:
@echo "\033[7;31m\n ----- Cleaning all objects... ----- \033[0m\n"
@${RM} ${OBJS}
Expand Down
3 changes: 2 additions & 1 deletion include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
# define MALLOC_COLOR_ENV "COLOR_MALLOC" /* Environement variable for ENABLE COLOR */
# define MALLOC_LEAKS_ENV "CHECK_LEAKS_MALLOC" /* Environement variable for DETECT_LEAK */
# define MALLOC_GARBAGE_ENV "GARBAGE_FREE_MALLOC" /* Environement variable for GARBAGE_COLLECTOR_FREE */

# define MALLOC_DISPLAY_ENV "ENV_DISPLAY_MALLOC" /* Environement variable for ENV_DISPLAY */

/********************************************************************
* ENUM *
Expand All @@ -93,6 +93,7 @@ enum type_block_e {
ENABLE_COLOR=32, /* Enable color for allocation trace option */
DETECT_LEAK=64, /* Detect leak output issue on stderr, or the given fd */
GARBAGE_COLLECTOR_FREE=128, /* Free all page at the end of program */
ENV_DISPLAY=256,
};

typedef enum type_block_e e_type;
Expand Down
19 changes: 12 additions & 7 deletions src/handle_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@ void display_env_var(char *env, char *to_check)
ft_printf_fd(1, "%s%s %s: %s\n"RESET, color, to_check, str, env);
}

static int16_t bool_check_env(char *to_check, int16_t flag)
static int16_t bool_check_env(char *to_check, int16_t flag, int16_t display)
{
char *env = getenv(to_check);

// display_env_var(env, to_check);
if (display != 0) {
display_env_var(env, to_check);
}
/*if env == NULL --> 0 * FLAG, otherwise --> 1 *FLAG*/
return ((env != NULL) * flag);
}

int handle_env_variable(int16_t *special_flag)
{
int fd = -1;
char *trace_file = check_env_variable(MALLOC_TRACE_ENV);

char *trace_file = NULL;
int16_t display = bool_check_env(MALLOC_DISPLAY_ENV, ENV_DISPLAY\
, bool_check_env(MALLOC_DISPLAY_ENV, ENV_DISPLAY, 0));

trace_file = check_env_variable(MALLOC_TRACE_ENV);
if (trace_file) {
fd = open(trace_file, O_CREAT | O_APPEND | O_WRONLY, 00666);
if (fd > 0) {
Expand All @@ -52,8 +57,8 @@ int handle_env_variable(int16_t *special_flag)
fd = -1;
}
}
*special_flag += bool_check_env(MALLOC_COLOR_ENV, ENABLE_COLOR);
*special_flag += bool_check_env(MALLOC_LEAKS_ENV, DETECT_LEAK);
*special_flag += bool_check_env(MALLOC_GARBAGE_ENV, GARBAGE_COLLECTOR_FREE);
*special_flag += bool_check_env(MALLOC_COLOR_ENV, ENABLE_COLOR, display);
*special_flag += bool_check_env(MALLOC_LEAKS_ENV, DETECT_LEAK, display);
*special_flag += bool_check_env(MALLOC_GARBAGE_ENV, GARBAGE_COLLECTOR_FREE, display);
return (fd);
}

0 comments on commit 212f431

Please sign in to comment.