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));
}