Skip to content

Commit

Permalink
norme and export shenanigans
Browse files Browse the repository at this point in the history
  • Loading branch information
zakissimo committed Jan 5, 2023
1 parent 68de128 commit a4a120b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
16 changes: 10 additions & 6 deletions cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/03 12:40:50 by zhabri #+# #+# */
/* Updated: 2023/01/05 11:57:24 by zhabri ### ########.fr */
/* Updated: 2023/01/05 12:18:23 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

t_list *get_env_node(char *var)
{
char *var_eq;
t_list *envp_entry;

var_eq = ft_strjoin(var, "=");
envp_entry = *g_glob->envp;
while (envp_entry \
&& ft_strncmp((char *)envp_entry->content, var, ft_strlen(var)))
&& ft_strncmp((char *)envp_entry->content, var_eq, ft_strlen(var_eq)) \
&& ft_strncmp((char *)envp_entry->content, var, ft_strlen(var) + 1))
envp_entry = envp_entry->next;
free(var_eq);
return (envp_entry);
}

Expand All @@ -30,8 +34,8 @@ static void ft_chdir(char *dir, int size)
t_list *pwd;
t_list *old_pwd;

pwd = get_env_node("PWD=");
old_pwd = get_env_node("OLDPWD=");
pwd = get_env_node("PWD");
old_pwd = get_env_node("OLDPWD");
ret = NULL;
free_null(old_pwd->content);
tmp = ft_strdup(pwd->content + 4);
Expand Down Expand Up @@ -74,7 +78,7 @@ void cd(char *cmd, int fd_out)
ft_chdir(cmd_split[1], 16);
}
else
ft_chdir(get_env_node("HOME=")->content + 5, 16);
ft_chdir(get_env_node("HOME")->content + 5, 16);
clean_and_free(cmd_split);
exit(0);
}
Expand Down Expand Up @@ -115,7 +119,7 @@ bool cd_parent(void)
return (true);
}
else
ft_chdir(get_env_node("HOME=")->content + 5, 16);
ft_chdir(get_env_node("HOME")->content + 5, 16);
free_tab(cmd_split);
g_glob->exit_ret = 0;
return (true);
Expand Down
14 changes: 2 additions & 12 deletions export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:56:54 by brenaudo #+# #+# */
/* Updated: 2023/01/05 11:59:22 by zhabri ### ########.fr */
/* Updated: 2023/01/05 12:28:03 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,17 +26,7 @@ static void export_no_arg(int fd_out)
{
envp_entry_split = cut_on_first(envp[i], '=');
if (envp_entry_split && ft_strncmp(envp_entry_split[0], "_", 2))
{
ft_putstr_fd("declare -x ", fd_out);
ft_putstr_fd(envp_entry_split[0], fd_out);
if (envp_entry_split[1][0])
{
ft_putstr_fd("=\"", fd_out);
ft_putstr_fd(envp_entry_split[1], fd_out);
ft_putchar_fd('\"', fd_out);
}
ft_putchar_fd('\n', fd_out);
}
print_export_line(envp_entry_split, fd_out);
free_tab(envp_entry_split);
}
free_tab(envp);
Expand Down
3 changes: 2 additions & 1 deletion minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/27 20:30:45 by zhabri #+# #+# */
/* Updated: 2023/01/05 11:48:27 by zhabri ### ########.fr */
/* Updated: 2023/01/05 12:27:38 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -155,6 +155,7 @@ void print_cmds(void);
void print_cmd_not_found(char *str);
void print_error_dir_cd(char *dir);
void print_exit_num_error(char *cmd);
void print_export_line(char **envp_entry_split, int fd_out);
void print_label(t_label label);
void print_nodes(void *n);
void pwd(char *cmd, int fd_out);
Expand Down
35 changes: 33 additions & 2 deletions utils_export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 16:26:02 by brenaudo #+# #+# */
/* Updated: 2023/01/04 16:26:18 by brenaudo ### ########.fr */
/* Updated: 2023/01/05 12:27:11 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -64,6 +64,37 @@ char **cut_on_first(char *str, char sep)
while (str && str[i] && str[i] != sep)
i++;
ret[0] = ft_substr(str, 0, i);
ret[1] = ft_substr(str, i + 1, ft_strlen(str) - i);
ret[1] = ft_substr(str, i, ft_strlen(str) - i);
return (ret);
}

char *longest_str(char *s1, char *s2)
{
int i;
int j;

i = 0;
j = 0;
while (s1 && s1[i])
i++;
while (s2 && s2[j])
j++;
if (i > j)
return (s1);
else
return (s2);
}

void print_export_line(char **envp_entry_split, int fd_out)
{
ft_putstr_fd("declare -x ", fd_out);
ft_putstr_fd(envp_entry_split[0], fd_out);
if (envp_entry_split[1][0])
{
ft_putchar_fd(envp_entry_split[1][0], fd_out);
ft_putchar_fd('\"', fd_out);
ft_putstr_fd(envp_entry_split[1] + 1, fd_out);
ft_putchar_fd('\"', fd_out);
}
ft_putchar_fd('\n', fd_out);
}

0 comments on commit a4a120b

Please sign in to comment.