tabenv.c 748 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "sh.h"
  2. void add_env(t_e *e, char *nenv)
  3. {
  4. int i;
  5. char **env;
  6. env = (char **)malloc(sizeof(char *) * (ft_tabsize(e->env) + 2));
  7. env[0] = nenv;
  8. i = 0;
  9. if (e->env != NULL)
  10. while (e->env[++i - 1] != NULL)
  11. env[i] = e->env[i - 1];
  12. env[i] = NULL;
  13. e->env = env;
  14. }
  15. /*
  16. ** Copy every line till encounter the one which must be deleted
  17. ** Free memory of deleted line
  18. ** Continue copying ever line till the end
  19. */
  20. void del_env(t_e *e, char *old_env)
  21. {
  22. int i;
  23. char **env;
  24. env = (char **)malloc(sizeof(char *) * ft_tabsize(e->env));
  25. i = -1;
  26. while (e->env[++i] != NULL && ft_strcmp(e->env[i], old_env) != '=')
  27. env[i] = e->env[i];
  28. while (e->env[i] != NULL)
  29. {
  30. env[i] = e->env[i + 1];
  31. i++;
  32. }
  33. env[i] = NULL;
  34. e->env = env;
  35. }