You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.1 KiB
77 lines
2.1 KiB
3 years ago
|
/* ************************************************************************** */
|
||
|
/* */
|
||
|
/* ::: :::::::: */
|
||
3 years ago
|
/* utils_2.c :+: :+: :+: */
|
||
3 years ago
|
/* +:+ +:+ +:+ */
|
||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
|
||
|
/* +#+#+#+#+#+ +#+ */
|
||
|
/* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */
|
||
|
/* Updated: 2022/05/06 13:25:32 by narnaud ### ########.fr */
|
||
|
/* */
|
||
|
/* ************************************************************************** */
|
||
|
|
||
|
#include "minishell.h"
|
||
|
|
||
|
int file_error(char *cmd, char *file, char *msg)
|
||
|
{
|
||
|
ft_putstr_fd("Minishell: ", 2);
|
||
|
ft_putstr_fd(cmd, 2);
|
||
|
ft_putstr_fd(": ", 2);
|
||
|
ft_putstr_fd(file, 2);
|
||
|
ft_putstr_fd(": ", 2);
|
||
|
ft_putstr_fd(msg, 2);
|
||
|
ft_putchar_fd('\n', 2);
|
||
|
return (0);
|
||
|
}
|
||
3 years ago
|
|
||
|
char *get_var_value(t_datas *datas, char **line, int *name_len)
|
||
|
{
|
||
|
int i;
|
||
|
char **env;
|
||
|
char *name;
|
||
|
char *value;
|
||
|
|
||
|
i = 1;
|
||
|
while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_')
|
||
|
i++;
|
||
|
name = ft_substr(*line, 1, i - 1);
|
||
|
i = 0;
|
||
|
*name_len = ft_strlen(name);
|
||
|
value = NULL;
|
||
|
while (datas->envp[i] && !value)
|
||
|
{
|
||
|
env = ft_split(datas->envp[i], '=');
|
||
|
if (!ft_strncmp(name, env[0], *name_len + 1))
|
||
|
value = (free(env[0]), env[1]);
|
||
|
else if (++i)
|
||
|
ft_free_split(env);
|
||
|
}
|
||
|
if (!datas->envp[i])
|
||
|
value = strdup("");
|
||
|
else
|
||
|
free(env);
|
||
|
return (value);
|
||
|
}
|
||
|
|
||
|
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i)
|
||
|
{
|
||
|
int name_len;
|
||
|
int i;
|
||
|
char *value;
|
||
|
|
||
|
if ((*line)[1] == '?')
|
||
|
{
|
||
|
value = ft_itoa(datas->exit_code);
|
||
|
name_len = 2;
|
||
|
}
|
||
|
else
|
||
|
value = get_var_value(datas, line, &name_len);
|
||
|
i = 0;
|
||
|
while (value[i])
|
||
|
tmp[tmp_i++] = value[i++];
|
||
|
tmp[tmp_i] = 0;
|
||
|
*line += name_len + 1;
|
||
|
free(value);
|
||
|
return (tmp_i);
|
||
|
}
|