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.
This commit is contained in:
Khaïs COLIN 2025-02-18 16:16:02 +01:00
parent af90bec318
commit ab6a4d6368
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 35 additions and 20 deletions

View file

@ -6,7 +6,7 @@
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <assert.h>
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);
}