From 99d0492849bd3351fd51bf758e8808f07cf4b3d2 Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Thu, 16 Nov 2017 20:52:07 +0000 Subject: [PATCH 1/9] Add setnev & unsetnev to builtin --- builtins.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/builtins.c b/builtins.c index 219026c..541e4e7 100644 --- a/builtins.c +++ b/builtins.c @@ -40,6 +40,8 @@ void (*check_built_ins(char *str))(char *str) builtin_t buildin[] = { {"exit", exit_b}, {"env", env_b}, + {"setnev", setnev_b}, + {"unsetnev", unsetnev_b}, {NULL, NULL} }; From eeb636e690693e84855a12ecf35bc117b9db6004 Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Thu, 16 Nov 2017 20:58:34 +0000 Subject: [PATCH 2/9] Add getnev_b, setnev_b, unsetnev_b prototype --- olaf.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/olaf.h b/olaf.h index db27f31..2dbba81 100644 --- a/olaf.h +++ b/olaf.h @@ -48,6 +48,9 @@ void (*check_built_ins(char *str))(char *str); void exit_b(char *str); void env_b(char *str); void cd_b(char *directory); +void getenv_b(char *env_var); +void setnev_b(char *str); +void unsetnev_b(char *str); /*Holberton library functions*/ int _strcmp(char *s1, char *s2); From 9ccc5c3658086e545cdfb14a8658e7b4ffdc08ac Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Fri, 17 Nov 2017 01:24:00 +0000 Subject: [PATCH 3/9] Add _realloc prototype, remove getnev_b --- olaf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/olaf.h b/olaf.h index 2dbba81..4b88609 100644 --- a/olaf.h +++ b/olaf.h @@ -48,12 +48,12 @@ void (*check_built_ins(char *str))(char *str); void exit_b(char *str); void env_b(char *str); void cd_b(char *directory); -void getenv_b(char *env_var); void setnev_b(char *str); void unsetnev_b(char *str); /*Holberton library functions*/ int _strcmp(char *s1, char *s2); char *_strdup(char *src); +void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); #endif From 9ea7064d156aca1f2b1be5ab4a7a6cbbe148a50d Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Fri, 17 Nov 2017 01:26:25 +0000 Subject: [PATCH 4/9] Add _realloc --- hbtlib.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/hbtlib.c b/hbtlib.c index 4fc4c28..ac3e5b2 100644 --- a/hbtlib.c +++ b/hbtlib.c @@ -39,3 +39,40 @@ char *_strdup(char *src) dest[i] = '\0'; return (dest); } + +/** + * _realloc - reallocate a memory block + * @ptr: pointer to old block + * @old_size: old memory size + * @new_size: new memory size + * Return: pointer to new block, NULL when failed + */ +void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) +{ + char *p, *orig = (char *)ptr; + unsigned int i; + + if (old_size == new_size) + return (ptr); + if (new_size == 0 && ptr != NULL) + { + free(ptr); + return (NULL); + } + if (ptr == NULL) + { + p = malloc(new_size); + if (p == NULL) + return (NULL); + } + else + { + p = malloc(new_size); + if (p == NULL) + return (NULL); + for (i = 0; orig[i] != '\0' && i < new_size ; i++) + p[i] = orig[i]; + free(ptr); + } + return (p); +} From eaddcdea582d9ccab7498e19418a654e34a33582 Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Fri, 17 Nov 2017 23:51:19 +0000 Subject: [PATCH 5/9] Add setenv and unsetenv and update header file and hblibs --- hbtlib.c | 4 -- olaf.h | 8 +++- setenv.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++ unsetenv.c | 50 +++++++++++++++++++++++++ 4 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 setenv.c create mode 100644 unsetenv.c diff --git a/hbtlib.c b/hbtlib.c index ac3e5b2..8d0f02e 100644 --- a/hbtlib.c +++ b/hbtlib.c @@ -55,10 +55,7 @@ void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) if (old_size == new_size) return (ptr); if (new_size == 0 && ptr != NULL) - { - free(ptr); return (NULL); - } if (ptr == NULL) { p = malloc(new_size); @@ -72,7 +69,6 @@ void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) return (NULL); for (i = 0; orig[i] != '\0' && i < new_size ; i++) p[i] = orig[i]; - free(ptr); } return (p); } diff --git a/olaf.h b/olaf.h index 4b88609..00a39ff 100644 --- a/olaf.h +++ b/olaf.h @@ -48,8 +48,12 @@ void (*check_built_ins(char *str))(char *str); void exit_b(char *str); void env_b(char *str); void cd_b(char *directory); -void setnev_b(char *str); -void unsetnev_b(char *str); +void unsetenv_b(char *str); +void setenv_b(char *str); +int check_env(char *env); +void modify_env(char *env, char *value); +void add_env(char *env, char *value); +void remove_env(char *env); /*Holberton library functions*/ int _strcmp(char *s1, char *s2); diff --git a/setenv.c b/setenv.c new file mode 100644 index 0000000..e61e30c --- /dev/null +++ b/setenv.c @@ -0,0 +1,106 @@ +#include "olaf.h" + +/** + * setenv_b - Initialize a new environ variable, or modify an existing one + * @line: A string representing the input from the user. + */ +void setenv_b(char *line) +{ + int check, token_count = 0; + char **path_tokens; + const char *delim = "\n\t "; + char *env, *value; + + path_tokens = token_interface(line, delim, token_count); + printf("%s\n", path_tokens[1]); + env = path_tokens[1], value = path_tokens[2]; + + check = check_env(env); + if (check == 1) + modify_env(env, value); + if (check == 0) + add_env(env, value); + + double_free(path_tokens); +} + +/** + * check_env - Check if an environ variable exists + * @env: Variable to be checked + * Return: 1 - found environ variable, 0 - not found + */ +int check_env(char *env) +{ + int i, j; + + i = 0; + while (environ[i]) + { + j = 0; + while (environ[i][j] == env[j]) + { + j++; + if (environ[i][j] == '=' && !env[j]) + return (1); + } + i++; + } + return (0); +} + +/** + * modify_env - Modify environ variable + * @env: environ variable + * @value: environ variable value + */ +void modify_env(char *env, char *value) +{ + int index, i = 0, j = 0; + + index = find_path(env); + + for (i = 0; env[i]; i++) + environ[index][i] = env[i]; + + environ[index][i] = '=', i++; + + for (j = 0; value[j]; i++, j++) + environ[index][i] = value[j]; + environ[index][i] = '\0'; +} + +/** + * add_env - Add new environ variable + * @env: environ variable + * @value: environ variable value + */ +void add_env(char *env, char *value) +{ + int i = 0, j = 0, t = 0, count = 0, size; + char **new; + + for (i = 0; environ[i]; i++) + count++; + + new = _realloc(environ, sizeof(char *) * count, sizeof(char *) * (count + 1)); + if (new == NULL) + return; + for (i = 0; i < count; i++) + new[i] = _strdup(environ[i]); + + size = strlen(env) + strlen(value) + 2; + new[i] = malloc(sizeof(char) * size); + if (new[i] == NULL) + return; + + for (j = 0; env[j]; j++) + new[i][j] = env[j]; + + new[i][j] = '=', j++; + + for (t = 0; value[t]; j++, t++) + new[i][j] = value[t]; + new[i][j] = '\0'; + + environ = new; +} diff --git a/unsetenv.c b/unsetenv.c new file mode 100644 index 0000000..054de21 --- /dev/null +++ b/unsetenv.c @@ -0,0 +1,50 @@ +#include "olaf.h" + +/** + * unsetenv_b - Remove an environ variable + * @line: A string representing the input from the user. + */ +void unsetenv_b(char *line) +{ + int check, token_count = 0; + char **path_tokens; + const char *delim = "\n\t "; + char *env; + + path_tokens = token_interface(line, delim, token_count); + env = path_tokens[1]; + + check = check_env(env); + if (check == 0) + return; + if (check == 1) + remove_env(env); + + double_free(path_tokens); +} + +/** + * remove_env - Remove environ variable + * @env: environ variable + */ +void remove_env(char *env) +{ + int i = 0, j = 0, size = 0, index; + char **str; + + for (i = 0; environ[i]; i++) + size++; + str = malloc(sizeof(char *) * size); + if (str == NULL) + return; + + index = find_path(env); + for (i = 0; environ[i]; i++, j++) + { + if (i == index) + i++; + str[j] = _strdup(environ[i]); + } + str[j] = NULL; + environ = str; +} From cb38b38986603354e6f331ce31b22e918831b30b Mon Sep 17 00:00:00 2001 From: stvngrcia Date: Sat, 18 Nov 2017 00:17:31 +0000 Subject: [PATCH 6/9] fixing spelling and syntax correction --- builtins.c | 4 ++-- hbtlib.c | 1 + setenv.c | 3 +-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/builtins.c b/builtins.c index d77cae2..882cfa0 100644 --- a/builtins.c +++ b/builtins.c @@ -72,8 +72,8 @@ void (*check_built_ins(char *str))(char *str) builtin_t buildin[] = { {"exit", exit_b}, {"env", env_b}, - {"setnev", setnev_b}, - {"unsetnev", unsetnev_b}, + {"setenv", setenv_b}, + {"unsetenv", unsetenv_b}, {"cd", cd_b}, {NULL, NULL} }; diff --git a/hbtlib.c b/hbtlib.c index ecf5f2c..d332cb3 100644 --- a/hbtlib.c +++ b/hbtlib.c @@ -73,6 +73,7 @@ void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) return (p); } +/** * print_str - Prints a string character by character. * @str: String to be printed. If the string is NULL it will print (null) * @new_line: If integer is 0 a new line will be printed. Otherwise a new line diff --git a/setenv.c b/setenv.c index e61e30c..6088006 100644 --- a/setenv.c +++ b/setenv.c @@ -12,7 +12,6 @@ void setenv_b(char *line) char *env, *value; path_tokens = token_interface(line, delim, token_count); - printf("%s\n", path_tokens[1]); env = path_tokens[1], value = path_tokens[2]; check = check_env(env); @@ -21,7 +20,7 @@ void setenv_b(char *line) if (check == 0) add_env(env, value); - double_free(path_tokens); + double_free(path_tokens); } /** From b23c168651a66d1ae650b5309ab98f4310c8ea2f Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Sat, 18 Nov 2017 00:24:48 +0000 Subject: [PATCH 7/9] Add NULL to the end of environ array --- setenv.c | 1 + 1 file changed, 1 insertion(+) diff --git a/setenv.c b/setenv.c index 6088006..93f5cbe 100644 --- a/setenv.c +++ b/setenv.c @@ -86,6 +86,7 @@ void add_env(char *env, char *value) return; for (i = 0; i < count; i++) new[i] = _strdup(environ[i]); + new[i + 1] = NULL; size = strlen(env) + strlen(value) + 2; new[i] = malloc(sizeof(char) * size); From a0cb8e5f388667fde87658805c24dfbe885bb397 Mon Sep 17 00:00:00 2001 From: stvngrcia Date: Sat, 18 Nov 2017 00:52:14 +0000 Subject: [PATCH 8/9] modifying the allocated space for relloc --- setenv.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/setenv.c b/setenv.c index 93f5cbe..5b5fc8f 100644 --- a/setenv.c +++ b/setenv.c @@ -81,13 +81,11 @@ void add_env(char *env, char *value) for (i = 0; environ[i]; i++) count++; - new = _realloc(environ, sizeof(char *) * count, sizeof(char *) * (count + 1)); + new = _realloc(environ, sizeof(char *) * count, sizeof(char *) * (count + 2)); if (new == NULL) return; for (i = 0; i < count; i++) new[i] = _strdup(environ[i]); - new[i + 1] = NULL; - size = strlen(env) + strlen(value) + 2; new[i] = malloc(sizeof(char) * size); if (new[i] == NULL) @@ -101,6 +99,6 @@ void add_env(char *env, char *value) for (t = 0; value[t]; j++, t++) new[i][j] = value[t]; new[i][j] = '\0'; - + new[i + 1] = NULL; environ = new; } From 34e3cf33b9086565aef140a3a259fea4e1ed2a91 Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Sun, 19 Nov 2017 04:45:52 +0000 Subject: [PATCH 9/9] Add error message when unsetenv not found --- unsetenv.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/unsetenv.c b/unsetenv.c index 054de21..1411200 100644 --- a/unsetenv.c +++ b/unsetenv.c @@ -16,7 +16,10 @@ void unsetenv_b(char *line) check = check_env(env); if (check == 0) + { + printf("unsetenv: %s: variable not found\n", env); return; + } if (check == 1) remove_env(env);