From ab6a4d63680de4bf0007024ec46b522506982e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 18 Feb 2025 16:16:02 +0100 Subject: [PATCH] envp_get_val: add tests + make it more consistent with envp_get_key In particular, this is what is now changed: - if line is null or empty, return null - if line contains no =, return an empty string This way, if non-null is returned, that means that line was valid. If null is returned, there is either an error with line, or malloc failed. --- src/env_get_set.c | 29 ++++++++++++++++++++--------- tests/env_manip.c | 26 +++++++++++++++----------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/env_get_set.c b/src/env_get_set.c index 734df15..21f11f5 100644 --- a/src/env_get_set.c +++ b/src/env_get_set.c @@ -6,7 +6,7 @@ /* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/14 18:43:38 by jguelen #+# #+# */ -/* Updated: 2025/02/18 15:41:54 by khais ### ########.fr */ +/* Updated: 2025/02/18 16:15:39 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,16 +43,27 @@ char *envp_get_key(char *line) return (key); } -/*Designed to get the value part of an envp entry*/ +/* +** Get the value part of a line of envp +** +** given that line is of the form key=value, return a string containing value, +** which is allocated and must be freed. +** +** if line is null or is empty, return NULL +** +** if line contains no '=', return an empty string +** +** if allocation fail, return NULL +*/ char *envp_get_val(char *line) { - char *value_string; - char *tmp; + char *val_pointer; - tmp = ft_strchr(line, '='); - if (!tmp) + if (line == NULL || line[0] == '\0') return (NULL); - tmp++; - value_string = ft_strdup(tmp); - return (value_string); + val_pointer = ft_strchr(line, '='); + if (!val_pointer) + return (ft_strdup("")); + val_pointer++; + return (ft_strdup(val_pointer)); } diff --git a/tests/env_manip.c b/tests/env_manip.c index cc6fb67..0d01451 100644 --- a/tests/env_manip.c +++ b/tests/env_manip.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/18 15:11:14 by khais #+# #+# */ -/* Updated: 2025/02/18 15:33:36 by khais ### ########.fr */ +/* Updated: 2025/02/18 16:12:23 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,19 +14,23 @@ #include "libft.h" #include -static void test_envp_get_key(char *line, char *expected_key) +static void test_envp_parsing(char *line, char *expected_key, char *expected_value) { - char *value = envp_get_key(line); - ft_dprintf(STDERR_FILENO, "for envp value '%s', expecting key to eq '%s', and got '%s'\n", line, expected_key, value); - assert(expected_key == value || ft_strcmp(expected_key, value) == 0); - free(value); + char *got_key = envp_get_key(line); + char *got_value = envp_get_val(line); + ft_dprintf(STDERR_FILENO, "for envp value '%s', expecting key to eq '%s', and got '%s'\n", line, expected_key, got_key); + assert(expected_key == got_key || ft_strcmp(expected_key, got_key) == 0); + ft_dprintf(STDERR_FILENO, "for envp value '%s', expecting value to eq '%s', and got '%s'\n", line, expected_value, got_value); + assert(expected_value == got_value || ft_strcmp(expected_value, got_value) == 0); + free(got_key); + free(got_value); } int main(void) { - test_envp_get_key("SHELL=/bin/fish", "SHELL"); - test_envp_get_key("=/bin/fish", ""); - test_envp_get_key(NULL, NULL); - test_envp_get_key("", NULL); - test_envp_get_key("VARNAME", "VARNAME"); + test_envp_parsing("SHELL=/bin/fish", "SHELL", "/bin/fish"); + test_envp_parsing("=/bin/fish", "", "/bin/fish"); + test_envp_parsing(NULL, NULL, NULL); + test_envp_parsing("", NULL, NULL); + test_envp_parsing("VARNAME", "VARNAME", ""); return (0); }