merge master and camille

This commit is contained in:
Camille Chauvet 2023-02-14 07:21:24 +01:00
parent 6ab114eeeb
commit cf4bacd42e
98 changed files with 225 additions and 26 deletions

Binary file not shown.

View File

@ -1,5 +1,5 @@
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_getstr.c UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_getstr.c
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c ft_split_quoted.c SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c ft_split_quoted.c env.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}

194
env.c Normal file
View File

@ -0,0 +1,194 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
/* Updated: 2023/02/03 16:04:16 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int get_index(char *s, char c)
{
int i;
i = -1;
while (s[++i])
if (s[i] == c)
return (i);
return (-1);
}
void print_export(t_list **head, int fd)
{
t_list *current;
char *ctn;
int v;
current = *head;
while (current != NULL)
{
ctn = current->content;
if (*(ft_strchr(ctn, '=') - 1) != '_')
{
v = get_index(ctn, '=');
write(fd, "declare -x ", 11);
write(fd, ctn, v + 1);
write(fd, "\"", 1);
ft_putstr_fd(ctn + v + 1, fd);
write(fd, "\"\n", 2);
}
current = current->next;
}
}
void print_env(t_list **head, int fd)
{
t_list *current;
current = *head;
while (current != NULL)
{
ft_putstr_fd(current->content, fd);
write(fd, "\n", 1);
current = current->next;
}
}
int strcmp_alphabet(char *s1, char *s2)
{
int i;
if (!s1 || !s2)
return (-2);
i = 0;
while (s1[i] && s2[i])
{
if (s1[i] < s2[i])
return (0);
else if (s1[i] > s2[i])
return (1);
i++;
}
return (-1);
}
void ft_double_swap(char **a, char **b)
{
void *c;
c = *a;
*a = *b;
*b = c;
}
void exchange(char **a, char **b, char **c)
{
void *d;
d = *a;
*a = *b;
*b = *c;
*c = d;
}
void add_sort(t_list **head, char *str)
{
t_list *current;
char *last;
current = *head;
while (current->next != NULL && strcmp_alphabet(str, current->content) != 0)
current = current->next;
if (strcmp_alphabet(str, current->content) == 1)
last = str;
else
exchange(&last, (char **)(&current->content), &str);
while (current != NULL)
{
if (current->next == NULL)
current->next = ft_calloc(1, sizeof(t_list));
if (current->next == NULL)
return ;
current = current->next;
if (current->content == NULL)
{
current->content = last;
return ;
}
else
ft_double_swap((char **)(&current->content), &last);
}
}
char *get_value_index(int index, t_list **head)
{
t_list *current;
int i;
current = *head;
i = -1;
while (current != NULL && ++i != index)
current = current->next;
if (i == index)
return (ft_strchr(current->content, '=') + 1);
return (NULL);
}
int is_start(char *big, char *little)
{
int i;
i = 0;
while (big[i])
{
if (little[i] != big[i])
return (0);
i++;
if (!little[i])
return (1);
}
return (0);
}
char *get_value_key(char *key, t_list **head)
{
t_list *current;
current = *head;
while (current != NULL)
{
if (is_start(current->content, key))
return (ft_strchr(current->content, '=') + 1);
current = current->next;
}
return (NULL);
}
t_list **init_env(char **env)
{
t_list **head;
int i;
head = ft_calloc(1, sizeof(t_list *));
*head = ft_calloc(1, sizeof(t_list));
if (*head == NULL)
return (NULL);
i = -1;
while (env[++i])
add_sort(head, env[i]);
return (head);
}
/*int main(int argc, char *argv[], char **env)
{
(void)argc;
(void)argv;
ft_putstr_fd(get_value_index(10, init_env(env)), 1);
//print_env(init_env(env), 1);
return (0);
}*/

View File

@ -7,32 +7,27 @@ int main(int ac, char **av, char **env)
} }
char *ft_get_executable_path(t_data *data, char *executable) /* char *ft_get_executable_path(t_data *data, char *executable) */
{ /* { */
if (ft_strcmp(executable, "env")) /* if (ft_strcmp(executable, "env") == 0) */
return (ft_strjoin("/usr/bin/bo", const char *s2)) /* return (ft_strjoin("", executable)); */
} /* else */
/* return */
/* } */
int ft_excutor(t_cmd *cmd, ) int ft_excutor(t_cmd *cmd)
{ {
char cmd[13] = "/usr/bin/ls";
char *args[3] = {NULL, "cat", NULL};
int pid; int pid;
int fd_in;
int fd_out;
if (ac != 3)
return (1);
fd_out = open(av[2], O_WRONLY | O_CREAT | O_TRUNC);
fd_in = open(av[1], O_RDONLY);
pid = fork(); pid = fork();
if (pid == -1) if (pid == -1)
return (1); return (1);
if (pid == 0) if (pid == 0)
{ {
dup2(fd_out, 1); dup2(cmd->fd_out, 1);
dup2(fd_in, 0); dup2(cmd->fd_in, 0);
execve(cmd, args, env); //TODO ADD ENV VARIABLES
execve(cmd->executable, cmd->args, NULL);
} }
else else
waitpid(pid); waitpid(pid);

BIN
file.o

Binary file not shown.

Binary file not shown.

BIN
heredoc.o

Binary file not shown.

BIN
infile.o

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
main.o

Binary file not shown.

BIN
minishell

Binary file not shown.

View File

@ -1,14 +1,19 @@
#ifndef FT_MINISHELL #ifndef MINISHELL_H
# define FT_MINISHELL # define MINISHELL_H
# include "libftx/libftx.h" # include "libftx/libftx.h"
# include "utils/utils.h" # include "utils/utils.h"
# include <sys/types.h> # include <sys/types.h>
# include <sys/stat.h> # include <sys/stat.h>
# include <fcntl.h> # include <fcntl.h>
# include <sys/wait.h>
# include <stdio.h> # include <stdio.h>
# include <readline/readline.h> # include <readline/readline.h>
# include <readline/history.h> # include <readline/history.h>
t_list **init_env(char **env);
char *get_value_index(int index, t_list **head);
char *get_value_key(char *key, t_list **head);
int ft_syntatic_verif(const char *str); int ft_syntatic_verif(const char *str);
int ft_file_is_readable(const char *path); int ft_file_is_readable(const char *path);
int ft_file_is_writeable(const char *path); int ft_file_is_writeable(const char *path);
@ -27,4 +32,9 @@ typedef struct s_cmd
char **args; char **args;
} t_cmd; } t_cmd;
typedef struct s_data
{
t_list **env;
} t_data;
#endif #endif

BIN
outfile.o

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.