mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
env_set_entry: use ft_errno to indicate error conditions
This commit is contained in:
parent
df8aec148f
commit
e985fcc562
5 changed files with 32 additions and 17 deletions
|
|
@ -6,12 +6,13 @@
|
|||
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "libft.h"
|
||||
#include "ft_errno.h"
|
||||
|
||||
/*
|
||||
** 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
|
||||
** 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
|
||||
** returned.
|
||||
** If key is an empty string, key and value is freed, ft_errno is set to
|
||||
** 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.
|
||||
**
|
||||
** Implementation notes: free2 always returns NULL
|
||||
*/
|
||||
t_env *env_set_entry(t_env **env, char *key, char *value)
|
||||
{
|
||||
t_env *node;
|
||||
|
||||
if (key == NULL || value == NULL)
|
||||
return (free(key), free(value), NULL);
|
||||
return (ft_errno(FT_EINVAL), free2(key, value));
|
||||
if (*key == '\0')
|
||||
return (free(key), free(value), *env);
|
||||
return (ft_errno(FT_EBADID), free2(key, value));
|
||||
node = env_find_node_bykey(*env, key);
|
||||
if (node == NULL)
|
||||
{
|
||||
node = ft_calloc(1, sizeof(t_env));
|
||||
if (!node)
|
||||
return (free(key), free(value), NULL);
|
||||
return (ft_errno(FT_EERRNO), free2(key, value));
|
||||
node->key = key;
|
||||
node->value = value;
|
||||
env_add_back(env, node);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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_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);
|
||||
/*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
|
||||
compose it.*/
|
||||
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);
|
||||
t_env *env_from_envp(char **envp);
|
||||
/*env_manip_utils*/
|
||||
size_t env_get_size(t_env *env);
|
||||
void env_destroy_node(t_env *node);
|
||||
void env_destroy(t_env *env);
|
||||
void envp_destroy(char **envp);
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@
|
|||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/19 12:53/26 by khais #+# #+# */
|
||||
/* Updated: 2025/02/19 12:53:26 by khais ### ########.fr */
|
||||
/* Created: 2025/02/19 13:13/28 by khais #+# #+# */
|
||||
/* Updated: 2025/02/19 13:13:28 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -48,6 +48,8 @@ char *ft_strerror(t_errno errno)
|
|||
{
|
||||
static char *strs[] = {
|
||||
[FT_ESUCCESS] = "Success",
|
||||
[FT_EINVAL] = "Invalid argument",
|
||||
[FT_EBADID] = "Bad identifier",
|
||||
};
|
||||
|
||||
if (errno >= 0 && errno < FT_EMAXERRNO)
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@
|
|||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/19 12:53/30 by khais #+# #+# */
|
||||
/* Updated: 2025/02/19 12:53:30 by khais ### ########.fr */
|
||||
/* Created: 2025/02/19 13:13/11 by khais #+# #+# */
|
||||
/* Updated: 2025/02/19 13:13:11 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -18,6 +18,8 @@ typedef enum e_errno
|
|||
FT_EERRNO = -2,
|
||||
FT_EGET = -1,
|
||||
FT_ESUCCESS = 0,
|
||||
FT_EINVAL,
|
||||
FT_EBADID,
|
||||
FT_EMAXERRNO,
|
||||
} t_errno;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,11 +6,12 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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/ft_errno.h"
|
||||
#include "libft.h"
|
||||
#include "testutil.h"
|
||||
#include "unistd.h"
|
||||
|
|
@ -79,9 +80,13 @@ static void test_env_set_entry_nullargs(void)
|
|||
|
||||
env = 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(ft_errno_get() == FT_EINVAL);
|
||||
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(1 == env_get_size(env));
|
||||
env_destroy(env);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue