Browse Source

fix binaries search

master
narnaud 3 years ago
parent
commit
ea17acaa1d
  1. 2
      Makefile
  2. 17
      caller.c
  3. 14
      errors.c
  4. 12
      lexer_2.c
  5. 2
      libft/Makefile
  6. 3
      libft/libft.h
  7. 27
      libft/str/ft_join_with.c
  8. 19
      minishell.h
  9. 0
      utils.c

2
Makefile

@ -1,7 +1,7 @@
NAME = minishell
LIBFT = libft.a
SRCS = minishell.c lexer.c lexer_2.c lexer_3.c parser.c caller.c
SRCS += built-in.c env.c utils_1.c utils_2.c
SRCS += built-in.c env.c utils.c errors.c
OBJS = ${SRCS:.c=.o}

17
caller.c

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */
/* Updated: 2022/05/16 11:57:13 by narnaud ### ########.fr */
/* Updated: 2022/05/17 09:58:36 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,26 +39,23 @@ static void call_binary(t_datas *datas, t_command *cmd)
{
char **path_dirs;
char *path;
size_t path_length;
if (ft_strchr(cmd->argv[0], '/'))
execve(cmd->argv[0], cmd->argv, datas->envp);
else
{
path_dirs = ft_split(getenv("PATH"), ':');
while (path_dirs)
path = get_var_value(datas, "PATH", 4);
path_dirs = ft_split(path, ':');
free(path);
while (path_dirs && *path_dirs)
{
path_length = ft_strlen(*path_dirs) + ft_strlen(cmd->argv[0]) + 2;
path = ft_calloc(path_length, sizeof(char));
ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs));
ft_memcpy(path + ft_strlen(*path_dirs), "/", 1);
ft_memcpy(path + ft_strlen(*path_dirs) + 1, \
cmd->argv[0], ft_strlen(cmd->argv[0]));
path = ft_strjoin_with(*path_dirs, cmd->argv[0], "/");
execve(path, cmd->argv, datas->envp);
free(path);
path_dirs++;
}
}
cmd_error(cmd->argv[0], "command not found");
}
static pid_t call(t_datas *datas, t_command *cmd)

14
utils_2.c → errors.c

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_2.c :+: :+: :+: */
/* errors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */
/* Updated: 2022/05/16 12:52:38 by narnaud ### ########.fr */
/* Updated: 2022/05/17 09:59:56 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,6 +24,16 @@ int file_error(char *cmd, char *file, char *msg)
return (0);
}
int cmd_error(char *cmd, char *msg)
{
ft_putstr_fd("Minishell: ", 2);
ft_putstr_fd(cmd, 2);
ft_putstr_fd(": ", 2);
ft_putstr_fd(msg, 2);
ft_putchar_fd('\n', 2);
return (0);
}
int is_valid_identifier(char *ident)
{
int i;

12
lexer_2.c

@ -6,14 +6,12 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 07:53:17 by narnaud #+# #+# */
/* Updated: 2022/05/16 10:22:46 by mea ### ########.fr */
/* Updated: 2022/05/17 08:36:11 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *get_var_value(t_datas *datas, char **line, int name_len);
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i)
{
int name_len;
@ -28,10 +26,10 @@ int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i)
else
{
name_len = 1;
while (ft_isalpha((*line)[name_len]) \
while (ft_isalpha((*line)[name_len])
|| ft_isdigit((*line)[name_len]) || (*line)[name_len] == '_')
name_len++;
value = get_var_value(datas, line, name_len);
value = get_var_value(datas, *line + 1, name_len - 1);
}
i = 0;
while (value[i])
@ -61,14 +59,14 @@ char *expend_str(t_datas *datas, char *line)
return (tmp);
}
static char *get_var_value(t_datas *datas, char **line, int name_len)
char *get_var_value(t_datas *datas, char *line, int name_len)
{
int i;
char **env;
char *name;
char *value;
name = ft_substr(*line, 1, name_len - 1);
name = ft_substr(line, 0, name_len);
i = 0;
value = NULL;
while (datas->envp[i] && !value)

2
libft/Makefile

@ -6,7 +6,7 @@ SRCS = is/ft_isalpha.c is/ft_isdigit.c is/ft_isascii.c is/ft_isprint.c \
SRCS += str/ft_len.c str/ft_lcpy.c str/ft_lcat.c str/ft_chr.c \
str/ft_rchr.c str/ft_ncmp.c str/ft_nstr.c str/ft_dup.c \
str/ft_sub.c str/ft_join.c str/ft_trim.c str/ft_split.c \
str/ft_rev.c str/ft_mapi.c str/ft_iteri.c
str/ft_rev.c str/ft_mapi.c str/ft_iteri.c str/ft_join_with.c
SRCS += conv/ft_toupper.c conv/ft_tolower.c conv/ft_atoi.c conv/ft_itoa.c \
conv/ft_itox.c conv/ft_utoa.c

3
libft/libft.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */
/* Updated: 2022/05/09 08:59:36 by narnaud ### ########.fr */
/* Updated: 2022/05/17 09:55:08 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -74,6 +74,7 @@ int ft_tolower(int c);
int ft_toupper(int c);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(const char *s1, const char *s2);
char *ft_strjoin_with(char *s1, char *s2, char *inter);
char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *str, char ch);
char *ft_itoa(int n);

27
libft/str/ft_join_with.c

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_join_with.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/17 09:44:59 by narnaud #+# #+# */
/* Updated: 2022/05/17 09:56:37 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *ft_strjoin_with(char *s1, char *s2, char *inter)
{
int path_length;
char *ret;
path_length = ft_strlen(s1) + ft_strlen(s2) + ft_strlen(inter) + 1;
ret = ft_calloc(path_length, sizeof(char));
ft_memcpy(ret, s1, ft_strlen(s1));
ft_memcpy(ret + ft_strlen(s1), inter, ft_strlen(inter));
ft_memcpy(ret + ft_strlen(s1) + ft_strlen(inter), \
s2, ft_strlen(s2));
return (ret);
}

19
minishell.h

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */
/* Updated: 2022/05/16 10:42:18 by mea ### ########.fr */
/* Updated: 2022/05/17 09:11:13 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -50,22 +50,17 @@ typedef struct s_datas
int silent;
} t_datas;
// ----------------------------------Utils_1.c
// ---------------------------------- Utils.c
int is_empty(char *line);
int handle_status(t_datas *datas, int status);
void nothing(int sig_num);
void sigs_handler(int sig_num);
void halt(t_datas *datas, int ret_code, int silent);
typedef struct s_lex t_lexer;
// ----------------------------------Utils_2.c
// ---------------------------------- Errors.c
int file_error(char *cmd, char *file, char *msg);
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i);
char *expend_str(t_datas *datas, char *line);
int set_redir(t_lexer *lex, char **line, char ch);
// ----------------------------------Utils_3.c
int cmd_error(char *cmd, char *msg);
int is_valid_identifier(char *ident);
int create_wc(t_lexer *lex, char *tmp);
// ----------------------------------Builtins.c
int is_builtin(char *cmd);
@ -129,8 +124,12 @@ typedef struct s_lex
t_token *lexer(t_datas *datas, char *line);
int create_token(t_lexer *lex, char str[]);
char *get_var_value(t_datas *datas, char *line, int name_len);
int check_state(t_lexer *lex, char **line);
char *expend_str(t_datas *datas, char *line);
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i);
int set_redir(t_lexer *lex, char **line, char ch);
int create_wc(t_lexer *lex, char *wc);
#endif
#endif

0
utils_1.c → utils.c

Loading…
Cancel
Save