env_set_entry: use ft_errno to indicate error conditions

This commit is contained in:
Jérôme Guélen 2025-02-17 16:52:17 +01:00 committed by Khaïs COLIN
parent df8aec148f
commit e985fcc562
5 changed files with 32 additions and 17 deletions

View file

@ -6,12 +6,13 @@
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/12 18:29:12 by jguelen #+# #+# */ /* Created: 2025/02/12 18:29:12 by jguelen #+# #+# */
/* Updated: 2025/02/18 18:41:30 by khais ### ########.fr */ /* Updated: 2025/02/19 13:17:55 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "env_manip.h" #include "env_manip.h"
#include "libft.h" #include "libft.h"
#include "ft_errno.h"
/* /*
** Remove the first env node of the given linked list where the key matches the ** Remove the first env node of the given linked list where the key matches the
@ -79,29 +80,33 @@ static void env_add_back(t_env **env, t_env *new)
** freed. In case a node matching the given key is found the provided key value ** freed. In case a node matching the given key is found the provided key value
** is freed. ** is freed.
** **
** If key or value are null, both are freed and NULL is returned. ** If key or value are null, both are freed and NULL is returned. ft_errno is
** set to FT_EINVAL.
** **
** If key is an empty string, key and value is freed and a non-error value is ** If key is an empty string, key and value is freed, ft_errno is set to
** returned. ** FT_EBADID, and NULL is returned.
** **
** If there is a failure allocating a new node, NULL is returned. ** If there is a failure allocating a new node, NULL is returned and ft_errno is
** set to FT_EERRNO (malloc would have set it to ENOMEM).
** **
** Returns a pointer to the first element in the linked list, or NULL on error. ** Returns a pointer to the first element in the linked list, or NULL on error.
**
** Implementation notes: free2 always returns NULL
*/ */
t_env *env_set_entry(t_env **env, char *key, char *value) t_env *env_set_entry(t_env **env, char *key, char *value)
{ {
t_env *node; t_env *node;
if (key == NULL || value == NULL) if (key == NULL || value == NULL)
return (free(key), free(value), NULL); return (ft_errno(FT_EINVAL), free2(key, value));
if (*key == '\0') if (*key == '\0')
return (free(key), free(value), *env); return (ft_errno(FT_EBADID), free2(key, value));
node = env_find_node_bykey(*env, key); node = env_find_node_bykey(*env, key);
if (node == NULL) if (node == NULL)
{ {
node = ft_calloc(1, sizeof(t_env)); node = ft_calloc(1, sizeof(t_env));
if (!node) if (!node)
return (free(key), free(value), NULL); return (ft_errno(FT_EERRNO), free2(key, value));
node->key = key; node->key = key;
node->value = value; node->value = value;
env_add_back(env, node); env_add_back(env, node);

View file

@ -6,7 +6,7 @@
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/14 13:46:39 by jguelen #+# #+# */ /* Created: 2025/02/14 13:46:39 by jguelen #+# #+# */
/* Updated: 2025/02/19 13:07:32 by khais ### ########.fr */ /* Updated: 2025/02/19 13:08:58 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,16 +26,17 @@ typedef struct s_env
char *envp_get_key(char *line); char *envp_get_key(char *line);
char *envp_get_val(char *line); char *envp_get_val(char *line);
char *env_get_val(t_env *env, char *key);
size_t env_get_size(t_env *env);
/**/
void env_rm_entry(t_env **env, char *key); void env_rm_entry(t_env **env, char *key);
/*WARNING: env_set_entry does NOT check for the actual validity of an /*WARNING: env_set_entry does NOT check for the actual validity of an
identifier (i.e. key) in the sense that it does not check what characters identifier (i.e. key) in the sense that it does not check what characters
compose it.*/ compose it.*/
t_env *env_set_entry(t_env **env, char *key, char *value); t_env *env_set_entry(t_env **env, char *key, char *value);
char *env_get_val(t_env *env, char *key);
char **envp_from_env(t_env *env); char **envp_from_env(t_env *env);
t_env *env_from_envp(char **envp); t_env *env_from_envp(char **envp);
/*env_manip_utils*/ /*env_manip_utils*/
size_t env_get_size(t_env *env);
void env_destroy_node(t_env *node); void env_destroy_node(t_env *node);
void env_destroy(t_env *env); void env_destroy(t_env *env);
void envp_destroy(char **envp); void envp_destroy(char **envp);

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 12:53/26 by khais #+# #+# */ /* Created: 2025/02/19 13:13/28 by khais #+# #+# */
/* Updated: 2025/02/19 12:53:26 by khais ### ########.fr */ /* Updated: 2025/02/19 13:13:28 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -48,6 +48,8 @@ char *ft_strerror(t_errno errno)
{ {
static char *strs[] = { static char *strs[] = {
[FT_ESUCCESS] = "Success", [FT_ESUCCESS] = "Success",
[FT_EINVAL] = "Invalid argument",
[FT_EBADID] = "Bad identifier",
}; };
if (errno >= 0 && errno < FT_EMAXERRNO) if (errno >= 0 && errno < FT_EMAXERRNO)

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 12:53/30 by khais #+# #+# */ /* Created: 2025/02/19 13:13/11 by khais #+# #+# */
/* Updated: 2025/02/19 12:53:30 by khais ### ########.fr */ /* Updated: 2025/02/19 13:13:11 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,6 +18,8 @@ typedef enum e_errno
FT_EERRNO = -2, FT_EERRNO = -2,
FT_EGET = -1, FT_EGET = -1,
FT_ESUCCESS = 0, FT_ESUCCESS = 0,
FT_EINVAL,
FT_EBADID,
FT_EMAXERRNO, FT_EMAXERRNO,
} t_errno; } t_errno;

View file

@ -6,11 +6,12 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/18 15:11:14 by khais #+# #+# */ /* Created: 2025/02/18 15:11:14 by khais #+# #+# */
/* Updated: 2025/02/19 12:23:50 by khais ### ########.fr */ /* Updated: 2025/02/19 13:15:43 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../src/env_manip.h" #include "../src/env_manip.h"
#include "../src/ft_errno.h"
#include "libft.h" #include "libft.h"
#include "testutil.h" #include "testutil.h"
#include "unistd.h" #include "unistd.h"
@ -79,9 +80,13 @@ static void test_env_set_entry_nullargs(void)
env = NULL; env = NULL;
assert(env_set_entry(&env, ft_strdup("VAR"), ft_strdup("hello")) != NULL); assert(env_set_entry(&env, ft_strdup("VAR"), ft_strdup("hello")) != NULL);
ft_errno(FT_ESUCCESS);
assert(env_set_entry(&env, NULL, ft_strdup("hello")) == NULL); assert(env_set_entry(&env, NULL, ft_strdup("hello")) == NULL);
assert(ft_errno_get() == FT_EINVAL);
assert(env_set_entry(&env, ft_strdup("VAR"), NULL) == NULL); assert(env_set_entry(&env, ft_strdup("VAR"), NULL) == NULL);
assert(env_set_entry(&env, ft_strdup(""), ft_strdup("value")) == env); ft_errno(FT_ESUCCESS);
assert(env_set_entry(&env, ft_strdup(""), ft_strdup("value")) == NULL);
assert(ft_errno_get() == FT_EBADID);
assert_env_value(env, "VAR", "hello"); assert_env_value(env, "VAR", "hello");
assert(1 == env_get_size(env)); assert(1 == env_get_size(env));
env_destroy(env); env_destroy(env);