Compare commits
	
		
			182 Commits
		
	
	
		
			camille
			...
			1e00fee53c
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 1e00fee53c | |||
| 4dc8793eec | |||
| f66ab7790e | |||
| 41bbe2bc0b | |||
| 3374d21cad | |||
| 94f55af0be | |||
| 6f3b1ab918 | |||
| 6a81705b98 | |||
| 27b8874f75 | |||
| cdfa81328b | |||
| bb00de527a | |||
| 7e77c4fe48 | |||
| 13e455cbf4 | |||
| 471845b565 | |||
| 1c43d89fa1 | |||
| a941a0138f | |||
| 011c801f1d | |||
| f874ed9f7b | |||
| dd49432d0d | |||
| 1c8892983e | |||
| c1e61780e0 | |||
| 6498031d59 | |||
| 3e656abf5d | |||
| a9df160295 | |||
| 2a0846fbf0 | |||
| 9787d71b85 | |||
| 8a6a0012dc | |||
| 4acefd9208 | |||
| c8c4297f8b | |||
| d05edefa0f | |||
| 392c2d94d0 | |||
| 94fa4de0a2 | |||
| d54cd98197 | |||
| 147af02c6e | |||
| 67e8a60423 | |||
| 96a5856592 | |||
| 57dd5c9962 | |||
| 689a4e4eb6 | |||
| db51c3c85b | |||
| c5ddcdc892 | |||
| 45fc051e9d | |||
| e195fe2c6d | |||
| 215b0dbb7e | |||
| 22dc9776d6 | |||
| b3d060874d | |||
| 72ff3ba134 | |||
| 086fd3bcbe | |||
| 0fa822f6db | |||
| a58f9a0c76 | |||
| d56aac4b25 | |||
| 96696243d0 | |||
| f5940ee99e | |||
| ee3d535393 | |||
| 08261b1921 | |||
| 7ea602b12c | |||
| 7ffe556cd4 | |||
| 6ce54ea6fe | |||
| 74bbbd899a | |||
| b1d827e106 | |||
| bf15a86c2e | |||
| 1b17df6170 | |||
| 51d7e60790 | |||
| 4d3c25547e | |||
| c43331dca3 | |||
| ec752adbfa | |||
| dbb39712ea | |||
| e2c43eb973 | |||
| 3d68490320 | |||
| 0694b36a72 | |||
| 3d62d3dde9 | |||
| 69fc4c573a | |||
| 087a96e54b | |||
| 1a4ed128e7 | |||
| d36d9c92f5 | |||
| 6c8e161a32 | |||
| f20eab31dd | |||
| 86f01779cc | |||
| c6d683a1fc | |||
| f956986789 | |||
| a18c4cae82 | |||
| 5c9be2d56b | |||
| 15c95b45b1 | |||
| 876c75bb92 | |||
| 8093b250ce | |||
| 2930755dd3 | |||
| 3540743135 | |||
| 5bfc613126 | |||
| 686ea5d0db | |||
| e7f8373b4a | |||
| 9cbb1e139a | |||
| 06f6308bc4 | |||
| 8ea2e6c620 | |||
| b2c7013cf9 | |||
| 960daa8658 | |||
| 7f4fd9d421 | |||
| da75bdbf72 | |||
| af7336fbf3 | |||
| 00dc583ff8 | |||
| 4342d9a015 | |||
| 46d30864ed | |||
| e0e329a355 | |||
| 6e0d7288e8 | |||
| 5541a1f3fe | |||
| d5eb265a3f | |||
| b914753c47 | |||
| 5948237aad | |||
| 9a5cca3e2d | |||
| 00e570d92e | |||
| 77620211da | |||
| 07bc37cc71 | |||
| ed546f5a8d | |||
| ca18dd4205 | |||
| 370e5149d5 | |||
| fc82eefd7b | |||
| 51f892350a | |||
| 0941f3a370 | |||
| 9f0b0f5422 | |||
| 3d66fbe9a9 | |||
| 1626424ed7 | |||
| 8e22eb210a | |||
| 57be71a168 | |||
| 0327d0f5d7 | |||
| 2f386686c6 | |||
| 7728ded62d | |||
| 405c1937cd | |||
| b98717ae1a | |||
| 413d75b94c | |||
| 4f66624667 | |||
| f31d805d83 | |||
| 28fedfdd75 | |||
| 0661f2fbd1 | |||
| 29a4a864c3 | |||
| cdde24f0d0 | |||
| a4befa5fa6 | |||
| 94f48602d1 | |||
| 2b66ce5bc3 | |||
| f346c5c2ce | |||
| 9ace235831 | |||
| 4e92c03637 | |||
| 76bc8fbfd8 | |||
| 916f2c3c50 | |||
| 89c80e9bdb | |||
| eb4a8ed663 | |||
| 3fb6eb65a8 | |||
| 1fdc51a668 | |||
| 03a2cc055c | |||
| a136a66522 | |||
| 0ece156b97 | |||
| 4c45a2c603 | |||
| b40762478a | |||
| d7032849d6 | |||
| a20fa2a4c6 | |||
| 255892e12e | |||
| 96de7639cd | |||
| 17ece4bc7b | |||
| 73899f5905 | |||
| 373629b541 | |||
| 5caeed312c | |||
| 5e0ae80404 | |||
| 69d6ac533b | |||
| 73d1f11269 | |||
| e8deb0be19 | |||
| ca09c8ed26 | |||
| 3682d0a348 | |||
| e2a40e07fc | |||
| cfd1dffbcf | |||
| 55e378e2cb | |||
| fb8186ac78 | |||
| 3ef58c0116 | |||
| cc74fda339 | |||
| 7eedf74847 | |||
| c75c6838d6 | |||
| 4b5a50d7fe | |||
| 922db1e08f | |||
| e709eb0dbb | |||
| 3ce5b64420 | |||
| a183971a7a | |||
| 5175073708 | |||
| 72f9dd39cf | |||
| 8bd58675e9 | |||
| afd71e5ed8 | |||
| d59562899b | 
							
								
								
									
										34
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								Makefile
									
									
									
									
									
								
							@ -1,13 +1,39 @@
 | 
			
		||||
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c
 | 
			
		||||
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c execution.c spacer.c env_fill.c
 | 
			
		||||
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c utils/ft_atoi_check.c ./utils/ft_get_executable.c ./utils/fd.c
 | 
			
		||||
 | 
			
		||||
BUILTINS_SRC = builtins/pwd.c \
 | 
			
		||||
			   builtins/export.c \
 | 
			
		||||
			   builtins/env.c \
 | 
			
		||||
			   builtins/cd.c \
 | 
			
		||||
			   builtins/exit.c \
 | 
			
		||||
			   builtins/unset.c \
 | 
			
		||||
			   builtins/echo.c 
 | 
			
		||||
 | 
			
		||||
SRCS = ${BUILTINS_SRC} \
 | 
			
		||||
	   ${UTILS_SRC} \
 | 
			
		||||
	   main.c \
 | 
			
		||||
	   ./cmd/cmd.c \
 | 
			
		||||
	   ./env/env1.c \
 | 
			
		||||
	   ./env/env2.c \
 | 
			
		||||
	   ./env/env3.c \
 | 
			
		||||
	   ./env/env_fill.c \
 | 
			
		||||
	   ./data/data.c \
 | 
			
		||||
	   ./execution/execution.c \
 | 
			
		||||
	   ./syntax/syntax.c \
 | 
			
		||||
	   ./format/format.c \
 | 
			
		||||
	   ./redirection/heredoc.c \
 | 
			
		||||
	   ./redirection/file.c \
 | 
			
		||||
	   ./redirection/redirection.c \
 | 
			
		||||
	   ./redirection/check.c \
 | 
			
		||||
	   ./parse/parse.c \
 | 
			
		||||
	   ./signal/signal.c
 | 
			
		||||
 | 
			
		||||
OBJS = ${SRCS:.c=.o}
 | 
			
		||||
 | 
			
		||||
NAME = minishell
 | 
			
		||||
 | 
			
		||||
CC = gcc
 | 
			
		||||
CC = clang
 | 
			
		||||
 | 
			
		||||
CFLAGS = -Werror -Wextra -g
 | 
			
		||||
CFLAGS = -Werror -Wextra -Wall -g
 | 
			
		||||
 | 
			
		||||
LIBS = libftx/libftx.a
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										40
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,40 @@
 | 
			
		||||
# Minishell
 | 
			
		||||
 | 
			
		||||
Minishell is a 42 School project that involves creating a minimalistic shell in C language. The shell should be able to handle basic commands, manage environment variables and implement several built-in functions.
 | 
			
		||||
 | 
			
		||||
## Features
 | 
			
		||||
 | 
			
		||||
- Prompt management (display, editing, history)
 | 
			
		||||
- Command execution (with or without arguments)
 | 
			
		||||
- Basic shell operations (pipes, redirections)
 | 
			
		||||
- Environment variables management (listing, setting, unsetting)
 | 
			
		||||
- Built-in functions management (echo, cd, env, exit, export, pwd, unset)
 | 
			
		||||
 | 
			
		||||
## Getting started
 | 
			
		||||
 | 
			
		||||
1. Clone this repository: `git clone https://github.com/<your_username>/minishell.git`
 | 
			
		||||
2. `cd` into the cloned directory: `cd minishell`
 | 
			
		||||
3. Run the `make` command to compile the program: `make`
 | 
			
		||||
4. Start the shell by running the executable: `./minishell`
 | 
			
		||||
 | 
			
		||||
## Usage
 | 
			
		||||
 | 
			
		||||
Once the shell is started, you can enter commands as you would with any other shell. The following are some examples of commands that can be executed:
 | 
			
		||||
 | 
			
		||||
- Display the working directory: `pwd`
 | 
			
		||||
- Change directory: `cd <directory_name>`
 | 
			
		||||
- Print the value of an environment variable: `echo $<variable_name>`
 | 
			
		||||
- Set an environment variable: `export <variable_name>=<value>`
 | 
			
		||||
- Unset an environment variable: `unset <variable_name>`
 | 
			
		||||
- Display a list of environment variables: `env`
 | 
			
		||||
- Exit the shell: `exit`
 | 
			
		||||
 | 
			
		||||
## Resources
 | 
			
		||||
 | 
			
		||||
- 
 | 
			
		||||
- The C library documentation for the standard library functions used in the program
 | 
			
		||||
- bash man
 | 
			
		||||
 | 
			
		||||
## Authors
 | 
			
		||||
 | 
			
		||||
This project was created by [Camille CHAUVET](mailto:camille@chauvet.pro) and [Xamora](mailto:ogame.xamora@42l.fr). If you have any questions or suggestions, feel free to contact us.
 | 
			
		||||
@ -1,40 +0,0 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   export.c                                           :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/14 14:27:08 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/14 14:52:50 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "../minishell.h"
 | 
			
		||||
 | 
			
		||||
int	main(char **env)
 | 
			
		||||
{
 | 
			
		||||
	t_list	**env_lst;
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
	char	*key;
 | 
			
		||||
	char	*value;
 | 
			
		||||
 | 
			
		||||
	env_lst = init_env(env);
 | 
			
		||||
	current = *env_lst;
 | 
			
		||||
	while (current != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		value = ft_strchr(current->content, '=') + 1;
 | 
			
		||||
		key = ft_strndup(current->content,
 | 
			
		||||
				ft_strlen(current->content) - ft_strlen(value));
 | 
			
		||||
		if (key == NULL)
 | 
			
		||||
		{
 | 
			
		||||
			ft_lstclear(env_lst, env_del);
 | 
			
		||||
			return (1);
 | 
			
		||||
		}
 | 
			
		||||
		ft_printf("declare -x %s=\"%s\"\n", key, value);
 | 
			
		||||
		free(key);
 | 
			
		||||
		current = current->next;
 | 
			
		||||
	}
 | 
			
		||||
	ft_lstclear(env_lst, env_del);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								builtins/builtins.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								builtins/builtins.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   builtins.h                                         :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:41:30 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/27 13:41:31 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef BUILTINS_H
 | 
			
		||||
# define BUILTINS_H
 | 
			
		||||
# include "../libftx/libft/list.h"
 | 
			
		||||
 | 
			
		||||
int			echo(int fd, char **strs);
 | 
			
		||||
int			pwd(int fd);
 | 
			
		||||
char		*get_pwd(int fd);
 | 
			
		||||
int			print_env(t_list **env, int fd);
 | 
			
		||||
int			ft_export(t_list **env, char **args, int fd);
 | 
			
		||||
int			move_folder(char **args, int fd);
 | 
			
		||||
int			unset(t_list **env, char **args, int fd);
 | 
			
		||||
int			ft_exit(char **args);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										22
									
								
								builtins/builtins_private.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								builtins/builtins_private.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   builtins_private.h                                 :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:41:42 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/27 13:41:44 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef BUILTINS_PRIVATE_H
 | 
			
		||||
# define BUILTINS_PRIVATE_H
 | 
			
		||||
# include <limits.h>
 | 
			
		||||
# include "../libftx/libftx.h"
 | 
			
		||||
# include "../env/env.h"
 | 
			
		||||
# include "../utils/utils.h"
 | 
			
		||||
 | 
			
		||||
char	*get_pwd(int fd);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										50
									
								
								builtins/cd.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								builtins/cd.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,50 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   cd.c                                               :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/20 14:27:36 by erey-bet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/31 15:02:43 by erey-bet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./builtins_private.h"
 | 
			
		||||
 | 
			
		||||
int	make_move(char *path, int fd)
 | 
			
		||||
{
 | 
			
		||||
	char	*join;
 | 
			
		||||
 | 
			
		||||
	join = ft_strjoin("/", path);
 | 
			
		||||
	join = ft_strfjoin(get_pwd(fd), join);
 | 
			
		||||
	if (chdir(join) == 0)
 | 
			
		||||
	{
 | 
			
		||||
		free(join);
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	free(join);
 | 
			
		||||
	write(2, "cd: No such file or directory\n", 30);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	move_folder(char **args, int fd)
 | 
			
		||||
{
 | 
			
		||||
	char	*path;
 | 
			
		||||
 | 
			
		||||
	if (args[0] == NULL || args[1] != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		write(2, "cd: Wrong number's argument\n", 28);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	path = args[0];
 | 
			
		||||
	if (path[0] == '/' || ft_strncmp(path, "..", ft_strlen(path)) == 0)
 | 
			
		||||
	{
 | 
			
		||||
		if (chdir(path) == 0)
 | 
			
		||||
			return (0);
 | 
			
		||||
		write(2, "cd: No such file or directory\n", 30);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
		return (make_move(path, fd));
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										61
									
								
								builtins/echo.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								builtins/echo.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,61 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   echo.c                                             :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/17 13:09:08 by erey-bet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/30 13:55:40 by erey-bet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./builtins_private.h"
 | 
			
		||||
 | 
			
		||||
int	is_space(char c)
 | 
			
		||||
{
 | 
			
		||||
	return (c == ' ' || c == '\f' || c == '\v' || c == '\t'
 | 
			
		||||
		|| c == '\r' || c == '\n');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	check_argument(char *str, int *check_backslash_n)
 | 
			
		||||
{
 | 
			
		||||
	int	i;
 | 
			
		||||
 | 
			
		||||
	i = -1;
 | 
			
		||||
	while (str[++i])
 | 
			
		||||
		if (str[i] != '-' && str[i] != 'n')
 | 
			
		||||
			return (1);
 | 
			
		||||
	if (ft_strnstr(str, "n", ft_strlen(str)) && str[0] == '-')
 | 
			
		||||
		*check_backslash_n = 1;
 | 
			
		||||
	else
 | 
			
		||||
		return (1);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	echo(int fd, char **strs)
 | 
			
		||||
{
 | 
			
		||||
	int		check_backslash_n;
 | 
			
		||||
	int		check_start_write;
 | 
			
		||||
	int		i;
 | 
			
		||||
 | 
			
		||||
	check_backslash_n = 0;
 | 
			
		||||
	check_start_write = 0;
 | 
			
		||||
	i = -1;
 | 
			
		||||
	while (strs[++i])
 | 
			
		||||
	{
 | 
			
		||||
		while (is_space(*strs[i]))
 | 
			
		||||
			strs[i]++;
 | 
			
		||||
		if (check_start_write == 1
 | 
			
		||||
			|| check_argument(strs[i], &check_backslash_n))
 | 
			
		||||
		{
 | 
			
		||||
			check_start_write = 1;
 | 
			
		||||
			ft_putstr_fd(strs[i], fd);
 | 
			
		||||
			if (strs[i + 1] != NULL)
 | 
			
		||||
				write(fd, " ", 1);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if (!check_backslash_n)
 | 
			
		||||
		write(fd, "\n", 1);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
@ -6,20 +6,26 @@
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/14 14:56:02 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/14 14:58:40 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*   Updated: 2023/03/30 15:15:04 by erey-bet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "../minishell.h"
 | 
			
		||||
#include "./builtins_private.h"
 | 
			
		||||
 | 
			
		||||
int	main(char **env)
 | 
			
		||||
int	print_env(t_list **env, int fd)
 | 
			
		||||
{
 | 
			
		||||
	t_list	**head;
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
 | 
			
		||||
	while (current != NULL)
 | 
			
		||||
	current = *env;
 | 
			
		||||
	while (current->next != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_putendl_fd(1, current->content);
 | 
			
		||||
		if (((t_env *)(current->content))->original)
 | 
			
		||||
		{
 | 
			
		||||
			ft_putstr_fd(((t_env *)(current->content))->key, fd);
 | 
			
		||||
			ft_putstr_fd("=", fd);
 | 
			
		||||
			ft_putstr_fd(((t_env *)(current->content))->value, fd);
 | 
			
		||||
			write(fd, "\n", 1);
 | 
			
		||||
		}
 | 
			
		||||
		current = current->next;
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
							
								
								
									
										45
									
								
								builtins/exit.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								builtins/exit.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,45 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   exit.c                                             :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/24 10:17:59 by erey-bet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/30 15:04:47 by erey-bet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./builtins_private.h"
 | 
			
		||||
 | 
			
		||||
static int	error(int err, char *reason, char *problem)
 | 
			
		||||
{
 | 
			
		||||
	ft_putstr_fd("minishell: exit ", 2);
 | 
			
		||||
	if (problem != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_putstr_fd(problem, 2);
 | 
			
		||||
		write(2, ": ", 3);
 | 
			
		||||
	}
 | 
			
		||||
	ft_putstr_fd(reason, 2);
 | 
			
		||||
	return (err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_exit(char **args)
 | 
			
		||||
{
 | 
			
		||||
	int	err;
 | 
			
		||||
 | 
			
		||||
	if (args[0] == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		write(1, "exit\n", 6);
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	err = ft_atoi_check(args[0]);
 | 
			
		||||
	if (err == 1)
 | 
			
		||||
		return (error(err, "exit: numeric argument required", args[0]));
 | 
			
		||||
	if (args[1] != NULL)
 | 
			
		||||
		return (error(1, "exit: too many arguments", NULL));
 | 
			
		||||
	if (err > 0)
 | 
			
		||||
		return (error(err, "exit: numeric argument required", args[0]));
 | 
			
		||||
	write(1, "exit\n", 6);
 | 
			
		||||
	return ((ft_atoi(args[0]) % 256 + 256) % 256);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										107
									
								
								builtins/export.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										107
									
								
								builtins/export.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,107 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   export.c                                           :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/14 14:27:08 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/30 15:05:25 by erey-bet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./builtins_private.h"
 | 
			
		||||
 | 
			
		||||
static int	error(char *str)
 | 
			
		||||
{
 | 
			
		||||
	write(2, "bozoshell: export: `", 20);
 | 
			
		||||
	write(2, str, ft_strlen(str));
 | 
			
		||||
	write(2, "': not a valid identifier\n", 26);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	print_export(t_list **env, int fd)
 | 
			
		||||
{
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
 | 
			
		||||
	current = *env;
 | 
			
		||||
	while (current->next != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		write(fd, "declare -x ", 11);
 | 
			
		||||
		ft_putstr_fd(((t_env *)(current->content))->key, fd);
 | 
			
		||||
		if (((t_env *)(current->content))->value != NULL)
 | 
			
		||||
		{
 | 
			
		||||
			ft_putstr_fd("=", fd);
 | 
			
		||||
			write(fd, "\"", 1);
 | 
			
		||||
			ft_putstr_fd(((t_env *)(current->content))->value, fd);
 | 
			
		||||
			write(fd, "\"\n", 2);
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
			write(fd, "\n", 2);
 | 
			
		||||
		current = current->next;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	set_key_value_export(t_list **env, char *args, char **key, char **value)
 | 
			
		||||
{
 | 
			
		||||
	*key = ft_strndup(args, ft_strnchr(args, '='));
 | 
			
		||||
	if (*key == NULL)
 | 
			
		||||
		return (1);
 | 
			
		||||
	if (ft_strlen(*key) == 0)
 | 
			
		||||
		return (1);
 | 
			
		||||
	if (possible_key(*key) == 2)
 | 
			
		||||
	{
 | 
			
		||||
		(*key)[ft_strlen(*key) - 1] = '\0';
 | 
			
		||||
		if (get_value_by_key(*key, env) == NULL)
 | 
			
		||||
			*value = ft_strdup(ft_strchr(args, '=') + 1);
 | 
			
		||||
		else
 | 
			
		||||
			*value = ft_strjoin(get_value_by_key(*key, env),
 | 
			
		||||
					ft_strchr(args, '=') + 1);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
		*value = ft_strdup(ft_strchr(args, '=') + 1);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	add_export(t_list **env, char *args)
 | 
			
		||||
{
 | 
			
		||||
	char	*key;
 | 
			
		||||
	char	*value;
 | 
			
		||||
 | 
			
		||||
	if (ft_strchr(args, '=') != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		if (set_key_value_export(env, args, &key, &value))
 | 
			
		||||
			return (error(args));
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		key = ft_strdup(args);
 | 
			
		||||
		value = get_value_by_key(key, env);
 | 
			
		||||
		if (value != NULL)
 | 
			
		||||
			value = ft_strdup(value);
 | 
			
		||||
		if (possible_key(key) == 2)
 | 
			
		||||
			return (error(key));
 | 
			
		||||
	}
 | 
			
		||||
	if (!possible_key(key))
 | 
			
		||||
		return (error(args));
 | 
			
		||||
	create_value_by_key(key, value, env);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_export(t_list **env, char **args, int fd)
 | 
			
		||||
{
 | 
			
		||||
	int	err;
 | 
			
		||||
	int	i;
 | 
			
		||||
 | 
			
		||||
	err = 0;
 | 
			
		||||
	if (args[0] == NULL)
 | 
			
		||||
		print_export(env, fd);
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		i = -1;
 | 
			
		||||
		while (args[++i])
 | 
			
		||||
			if (add_export(env, args[i]) == 1)
 | 
			
		||||
				err = 1;
 | 
			
		||||
	}
 | 
			
		||||
	return (err);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										41
									
								
								builtins/pwd.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								builtins/pwd.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,41 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   pwd.c                                              :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/17 16:09:11 by erey-bet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/09 20:00:19 by erey-bet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./builtins_private.h"
 | 
			
		||||
 | 
			
		||||
int	pwd(int fd)
 | 
			
		||||
{
 | 
			
		||||
	char	path[PATH_MAX];
 | 
			
		||||
 | 
			
		||||
	if (getcwd(path, sizeof(path)) != NULL)
 | 
			
		||||
		ft_putendl_fd(path, fd);
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		ft_putendl_fd("Error getcwd", fd);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	*get_pwd(int fd)
 | 
			
		||||
{
 | 
			
		||||
	char	*str;
 | 
			
		||||
 | 
			
		||||
	str = ft_calloc(PATH_MAX, sizeof(char *));
 | 
			
		||||
	if (getcwd(str, PATH_MAX) != NULL)
 | 
			
		||||
		return (str);
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		ft_putendl_fd("Error getcwd", fd);
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										35
									
								
								builtins/unset.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								builtins/unset.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,35 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   unset.c                                            :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/22 13:28:27 by erey-bet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/30 15:07:10 by erey-bet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./builtins_private.h"
 | 
			
		||||
 | 
			
		||||
int	error(char *str, int fd)
 | 
			
		||||
{
 | 
			
		||||
	write(fd, "bozoshell: unset: `", 19);
 | 
			
		||||
	write(fd, str, ft_strlen(str));
 | 
			
		||||
	write(fd, "': not a valid identifier", 25);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	unset(t_list **env, char **args, int fd)
 | 
			
		||||
{
 | 
			
		||||
	int	i;
 | 
			
		||||
	int	err;
 | 
			
		||||
 | 
			
		||||
	i = -1;
 | 
			
		||||
	err = 0;
 | 
			
		||||
	while (args[++i])
 | 
			
		||||
		if (delete_by_key(args[i], env))
 | 
			
		||||
			if (!possible_key(args[i]))
 | 
			
		||||
				err = error(args[i], fd);
 | 
			
		||||
	return (err);
 | 
			
		||||
}
 | 
			
		||||
@ -6,12 +6,11 @@
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/15 14:18:21 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/16 18:25:14 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*   Updated: 2023/02/21 22:27:28 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "libftx/libftx.h"
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
#include "cmd_private.h"
 | 
			
		||||
 | 
			
		||||
void	ft_cmddel(void *ptr)
 | 
			
		||||
{
 | 
			
		||||
@ -20,35 +19,15 @@ void	ft_cmddel(void *ptr)
 | 
			
		||||
	content = (t_cmd *) ptr;
 | 
			
		||||
	if (content->args != NULL)
 | 
			
		||||
		ft_freer_tab_ultimate(1, content->args);
 | 
			
		||||
	if (content->executable != NULL)
 | 
			
		||||
	if (content->own_cmd == false && content->executable != NULL)
 | 
			
		||||
		free(content->executable);
 | 
			
		||||
	if (content->fd_in[0] > 2)
 | 
			
		||||
		close(content->fd_in[0]);
 | 
			
		||||
	if (content->fd_out[0] > 2)
 | 
			
		||||
		close(content->fd_out[0]);
 | 
			
		||||
	if (content->fd_in[1] > 2)
 | 
			
		||||
		close(content->fd_in[1]);
 | 
			
		||||
	if (content->fd_out[1] > 2)
 | 
			
		||||
		close(content->fd_out[1]);
 | 
			
		||||
	free(content);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_cmd_filler(t_list *element, char **args, t_list **env)
 | 
			
		||||
{
 | 
			
		||||
	t_cmd	*content;
 | 
			
		||||
	char	*temp;
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	if (args == NULL)
 | 
			
		||||
		return (1);
 | 
			
		||||
	content = (t_cmd *)element->content;
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (args[i] != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		temp = ft_env_filler(env, args[i]);
 | 
			
		||||
		if (temp == NULL)
 | 
			
		||||
		{
 | 
			
		||||
			ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
			return (1);
 | 
			
		||||
		}
 | 
			
		||||
		free(args[i]);
 | 
			
		||||
		args[i] = temp;
 | 
			
		||||
		ft_quote_remover(temp);
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	content->args = args;
 | 
			
		||||
	content->executable = args[0];
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								cmd/cmd.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								cmd/cmd.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,30 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   cmd.h                                              :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/28 15:47:01 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/28 15:47:02 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef CMD_H
 | 
			
		||||
# define CMD_H
 | 
			
		||||
# include <stdbool.h>
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
 | 
			
		||||
typedef struct s_cmd
 | 
			
		||||
{
 | 
			
		||||
	int		fd_in[2];
 | 
			
		||||
	int		fd_out[2];
 | 
			
		||||
	int		pid;
 | 
			
		||||
	char	*executable;
 | 
			
		||||
	char	**args;
 | 
			
		||||
	bool	own_cmd;
 | 
			
		||||
}	t_cmd;
 | 
			
		||||
 | 
			
		||||
void		ft_cmddel(void *content);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										20
									
								
								cmd/cmd_private.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								cmd/cmd_private.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   cmd_private.h                                      :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/28 15:50:23 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/28 15:50:24 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef CMD_PRIVATE_H
 | 
			
		||||
# define CMD_PRIVATE_H
 | 
			
		||||
# include <sys/types.h>
 | 
			
		||||
# include <sys/wait.h>
 | 
			
		||||
# include "./cmd.h"
 | 
			
		||||
# include "../libftx/libftx.h"
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										114
									
								
								cmds.c
									
									
									
									
									
								
							
							
						
						
									
										114
									
								
								cmds.c
									
									
									
									
									
								
							@ -1,114 +0,0 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   cmds.c                                             :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/15 14:17:26 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/17 15:20:29 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "libftx/libftx.h"
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
 | 
			
		||||
static int	ft_cmds_init(t_list **cmds, size_t len)
 | 
			
		||||
{
 | 
			
		||||
	t_cmd	*content;
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	*cmds = malloc(sizeof(t_list));
 | 
			
		||||
	current = *cmds;
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (i < len)
 | 
			
		||||
	{
 | 
			
		||||
		content = malloc(sizeof(t_cmd));
 | 
			
		||||
		if (content == NULL)
 | 
			
		||||
		{
 | 
			
		||||
			ft_lstclear(cmds, ft_cmddel);
 | 
			
		||||
			return (1);
 | 
			
		||||
		}
 | 
			
		||||
		content->args = NULL;
 | 
			
		||||
		content->executable = NULL;
 | 
			
		||||
		content->fd_in = -1;
 | 
			
		||||
		content->fd_out = -1;
 | 
			
		||||
		current->content = content;
 | 
			
		||||
		if (!((i + 1) < len))
 | 
			
		||||
		{
 | 
			
		||||
			current->next = NULL;
 | 
			
		||||
			return (0);
 | 
			
		||||
		}
 | 
			
		||||
		current->next = malloc(sizeof(t_list));
 | 
			
		||||
		if (current->next == NULL)
 | 
			
		||||
			ft_lstclear(cmds, ft_cmddel);
 | 
			
		||||
		current = current->next;
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
 | 
			
		||||
{
 | 
			
		||||
	size_t	len;
 | 
			
		||||
	t_cmd	*cmd;
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
 | 
			
		||||
	len = ft_seglen_quoted(line, '|');
 | 
			
		||||
	if (len == 0)
 | 
			
		||||
		return (0);
 | 
			
		||||
	if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|')))
 | 
			
		||||
	{
 | 
			
		||||
		free(cmds);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	cmd = (t_cmd *)(*cmds)->content;
 | 
			
		||||
	cmd->fd_in = infile;
 | 
			
		||||
	current = *cmds;
 | 
			
		||||
	while (current->next != NULL)
 | 
			
		||||
		current = current->next;
 | 
			
		||||
	cmd = (t_cmd *) current->content;
 | 
			
		||||
	cmd->fd_out = outfile;
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_cmds_fill(t_list **cmds, t_list **env, const char *line)
 | 
			
		||||
{
 | 
			
		||||
	char	**tab;
 | 
			
		||||
	char	**args;
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	tab = ft_split_quoted(line, '|');
 | 
			
		||||
	if (tab == NULL)
 | 
			
		||||
		return (1);
 | 
			
		||||
	i = 0;
 | 
			
		||||
	current = *cmds;
 | 
			
		||||
	while (tab[i] != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		args = ft_split_quoted(tab[i], ' ');
 | 
			
		||||
		if (ft_cmd_filler(current, args, env) == 1)
 | 
			
		||||
		{
 | 
			
		||||
			ft_lstclear(cmds, ft_cmddel);
 | 
			
		||||
			ft_freer_tab_ultimate(2, args, tab);
 | 
			
		||||
			return (1);
 | 
			
		||||
		}
 | 
			
		||||
		current = current->next;
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
t_list	**ft_parse_cmds(char *line, t_list **env, int infile, int outfile)
 | 
			
		||||
{
 | 
			
		||||
	t_list	**cmds;
 | 
			
		||||
 | 
			
		||||
	cmds = malloc(sizeof(t_list *));
 | 
			
		||||
	if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	if (ft_cmds_fill(cmds, env, line) == 1)
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	return (cmds);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								data/data.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								data/data.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   data.c                                             :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:42:09 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/27 13:42:58 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./data_private.h"
 | 
			
		||||
#include "data.h"
 | 
			
		||||
 | 
			
		||||
t_data	*ft_get_data(void)
 | 
			
		||||
{
 | 
			
		||||
	static t_data	data;
 | 
			
		||||
 | 
			
		||||
	return (&data);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								data/data.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								data/data.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   data.h                                             :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:43:39 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/27 13:43:40 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef DATA_H
 | 
			
		||||
# define DATA_H
 | 
			
		||||
# include "../libftx/libft/list.h"
 | 
			
		||||
 | 
			
		||||
typedef struct s_data
 | 
			
		||||
{
 | 
			
		||||
	t_list	**env;
 | 
			
		||||
	t_list	**cmds;
 | 
			
		||||
	int		exit_code;
 | 
			
		||||
	int		child_pid;
 | 
			
		||||
}	t_data;
 | 
			
		||||
 | 
			
		||||
t_data	*ft_get_data(void);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										18
									
								
								data/data_private.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								data/data_private.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,18 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   data_private.h                                     :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:42:21 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/27 13:42:23 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef DATA_PRIVATE_H
 | 
			
		||||
# define DATA_PRIVATE_H
 | 
			
		||||
# include "../libftx/libftx.h"
 | 
			
		||||
# include "./data.h"
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										38
									
								
								env/env.h
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								env/env.h
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   env.h                                              :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:42:01 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/27 13:42:02 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef ENV_H
 | 
			
		||||
# define ENV_H
 | 
			
		||||
# include <stdbool.h>
 | 
			
		||||
# include "../libftx/libft/list.h"
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
 | 
			
		||||
typedef struct s_env
 | 
			
		||||
{
 | 
			
		||||
	char	*key;
 | 
			
		||||
	char	*value;
 | 
			
		||||
	bool	original;
 | 
			
		||||
}	t_env;
 | 
			
		||||
 | 
			
		||||
char		*ft_env_filler(t_data *data, const char *str);
 | 
			
		||||
void		env_del(void *content);
 | 
			
		||||
t_list		**init_env(char **env);
 | 
			
		||||
char		**env_to_strs(t_list **head);
 | 
			
		||||
int			create_value_by_key(char *key, char *value, t_list **head);
 | 
			
		||||
int			create_value_by_key_dup(char *key, char *value, t_list **head);
 | 
			
		||||
int			set_value_by_key(char *key, char *value, t_list **head);
 | 
			
		||||
char		*get_value_by_key(char *key, t_list **head);
 | 
			
		||||
void		env_del(void *ptr);
 | 
			
		||||
int			delete_by_key(char *key, t_list **head);
 | 
			
		||||
int			possible_key(char *key);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										176
									
								
								env.c → env/env1.c
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										176
									
								
								env.c → env/env1.c
									
									
									
									
										vendored
									
									
								
							@ -1,151 +1,16 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   env.c                                              :+:      :+:    :+:   */
 | 
			
		||||
/*   env1.c                                             :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/02 14:39:56 by erey-bet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/17 13:05:29 by erey-bet         ###   ########.fr       */
 | 
			
		||||
/*   Updated: 2023/03/30 13:07:15 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;
 | 
			
		||||
 | 
			
		||||
	current = *head;
 | 
			
		||||
	while (current->next != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		write(fd, "declare -x ", 11);
 | 
			
		||||
		ft_putstr_fd(((t_env*)(current->content))->key, fd);
 | 
			
		||||
		ft_putstr_fd("=", fd);
 | 
			
		||||
		write(fd, "\"", 1);
 | 
			
		||||
		ft_putstr_fd(((t_env*)(current->content))->value, fd);
 | 
			
		||||
		write(fd, "\"\n", 2);
 | 
			
		||||
		current = current->next;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	print_env(t_list **head, int fd)
 | 
			
		||||
{
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
 | 
			
		||||
	current = *head;
 | 
			
		||||
	while (current->next != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_putstr_fd(((t_env*)(current->content))->key, fd);
 | 
			
		||||
		ft_putstr_fd("=", fd);
 | 
			
		||||
		ft_putstr_fd(((t_env*)(current->content))->value, 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(void *a, void *b)
 | 
			
		||||
{
 | 
			
		||||
	void	*c;
 | 
			
		||||
 | 
			
		||||
	c = a;
 | 
			
		||||
	a = b;
 | 
			
		||||
	b = c;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	exchange(void *a, void *b, void *c)
 | 
			
		||||
{
 | 
			
		||||
	void	*d;
 | 
			
		||||
 | 
			
		||||
	d = a;
 | 
			
		||||
	a = b;
 | 
			
		||||
	b = c;
 | 
			
		||||
	c = d;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	*get_value(char *str)
 | 
			
		||||
{
 | 
			
		||||
	char	*s;
 | 
			
		||||
	int		i;
 | 
			
		||||
	int		start;
 | 
			
		||||
 | 
			
		||||
	s = ft_calloc(ft_strlen(str), sizeof(char));
 | 
			
		||||
	start = get_index(str, '=');
 | 
			
		||||
	i = start;
 | 
			
		||||
	while (str[++i])
 | 
			
		||||
		s[i - start - 1] = str[i];
 | 
			
		||||
	return (s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	env_del(void *ptr)
 | 
			
		||||
{
 | 
			
		||||
	t_env	*content;
 | 
			
		||||
 | 
			
		||||
	content = ptr;
 | 
			
		||||
	free(content->key);
 | 
			
		||||
	free(content->value);
 | 
			
		||||
	free(content);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	*get_key(char *str)
 | 
			
		||||
{
 | 
			
		||||
	char	*s;
 | 
			
		||||
	int		i;
 | 
			
		||||
 | 
			
		||||
	s = ft_calloc(ft_strlen(str), sizeof(char));
 | 
			
		||||
	i = -1;
 | 
			
		||||
	while (str[++i] != '=')
 | 
			
		||||
		s[i] = str[i];
 | 
			
		||||
	return (s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	swap_env_3(void **a, void **b, void **c)
 | 
			
		||||
{
 | 
			
		||||
	void	*d;
 | 
			
		||||
 | 
			
		||||
	d = *a;
 | 
			
		||||
	*a = *b;
 | 
			
		||||
	*b = *c;
 | 
			
		||||
	*c = d;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	swap_env(void **a, void **b)
 | 
			
		||||
{
 | 
			
		||||
	void	*c;
 | 
			
		||||
 | 
			
		||||
	c = *a;
 | 
			
		||||
	*a = *b;
 | 
			
		||||
	*b = c;
 | 
			
		||||
}
 | 
			
		||||
#include "env_private.h"
 | 
			
		||||
 | 
			
		||||
void	add_sort(t_list **head, t_env *var)
 | 
			
		||||
{
 | 
			
		||||
@ -153,7 +18,8 @@ void	add_sort(t_list **head, t_env *var)
 | 
			
		||||
	t_env	*last;
 | 
			
		||||
 | 
			
		||||
	current = *head;
 | 
			
		||||
	while (current->next != NULL && strcmp_alphabet(var->key, ((t_env*)(current->content))->key) != 0)
 | 
			
		||||
	while (current->next != NULL
 | 
			
		||||
		&& ft_strcmp(var->key, ((t_env *)(current->content))->key) > 0)
 | 
			
		||||
		current = current->next;
 | 
			
		||||
	if (current->content == NULL)
 | 
			
		||||
		current->content = var;
 | 
			
		||||
@ -195,7 +61,9 @@ int	set_value_by_key(char *key, char *value, t_list **head)
 | 
			
		||||
		if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
 | 
			
		||||
		{
 | 
			
		||||
			free(((t_env *)current->content)->value);
 | 
			
		||||
			free(((t_env *)current->content)->key);
 | 
			
		||||
			((t_env *)current->content)->value = value;
 | 
			
		||||
			((t_env *)current->content)->key = key;
 | 
			
		||||
			return (0);
 | 
			
		||||
		}
 | 
			
		||||
		current = current->next;
 | 
			
		||||
@ -214,29 +82,11 @@ int	create_value_by_key(char *key, char *value, t_list **head)
 | 
			
		||||
		return (1);
 | 
			
		||||
	content->key = key;
 | 
			
		||||
	content->value = value;
 | 
			
		||||
	content->original = 0;
 | 
			
		||||
	add_sort(head, content);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	**env_to_strs(t_list **head)
 | 
			
		||||
{
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
	t_env	*content;
 | 
			
		||||
	char	**env;
 | 
			
		||||
	int		i;
 | 
			
		||||
 | 
			
		||||
	current = *head;
 | 
			
		||||
	env = ft_calloc(ft_lstsize(*head), sizeof(char *));
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (current->content)
 | 
			
		||||
	{
 | 
			
		||||
		content = current->content;
 | 
			
		||||
		env[i++] = ft_strmerger(3, content->key, "=", content->value);
 | 
			
		||||
		current = current->next;
 | 
			
		||||
	}
 | 
			
		||||
	return (env);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
t_list	**init_env(char **env)
 | 
			
		||||
{
 | 
			
		||||
	t_list	**head;
 | 
			
		||||
@ -257,16 +107,8 @@ t_list	**init_env(char **env)
 | 
			
		||||
			return (NULL);
 | 
			
		||||
		var->key = get_key(env[i]);
 | 
			
		||||
		var->value = get_value(env[i]);
 | 
			
		||||
		var->original = 1;
 | 
			
		||||
		add_sort(head, var);
 | 
			
		||||
	}
 | 
			
		||||
	return (head);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*int	main(int argc, char *argv[], char **env)
 | 
			
		||||
{
 | 
			
		||||
	t_list	**new;
 | 
			
		||||
 | 
			
		||||
	new = init_env(env);
 | 
			
		||||
	ft_printf("%S", env_to_strs(new));
 | 
			
		||||
	return (0);
 | 
			
		||||
}*/
 | 
			
		||||
							
								
								
									
										77
									
								
								env/env2.c
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								env/env2.c
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,77 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   env2.c                                             :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/09 19:59:03 by erey-bet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/09 19:59:10 by erey-bet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "env_private.h"
 | 
			
		||||
 | 
			
		||||
int	get_index(char *s, char c)
 | 
			
		||||
{
 | 
			
		||||
	int	i;
 | 
			
		||||
 | 
			
		||||
	i = -1;
 | 
			
		||||
	while (s[++i])
 | 
			
		||||
		if (s[i] == c)
 | 
			
		||||
			return (i);
 | 
			
		||||
	return (-1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	swap_env_3(void **a, void **b, void **c)
 | 
			
		||||
{
 | 
			
		||||
	void	*d;
 | 
			
		||||
 | 
			
		||||
	d = *a;
 | 
			
		||||
	*a = *b;
 | 
			
		||||
	*b = *c;
 | 
			
		||||
	*c = d;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	swap_env(void **a, void **b)
 | 
			
		||||
{
 | 
			
		||||
	void	*c;
 | 
			
		||||
 | 
			
		||||
	c = *a;
 | 
			
		||||
	*a = *b;
 | 
			
		||||
	*b = c;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	env_del(void *ptr)
 | 
			
		||||
{
 | 
			
		||||
	t_env	*content;
 | 
			
		||||
 | 
			
		||||
	if (ptr == NULL)
 | 
			
		||||
		return ;
 | 
			
		||||
	content = ptr;
 | 
			
		||||
	if (content->key != NULL)
 | 
			
		||||
		free(content->key);
 | 
			
		||||
	if (content->key != NULL)
 | 
			
		||||
		free(content->value);
 | 
			
		||||
	if (content != NULL)
 | 
			
		||||
		free(content);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	**env_to_strs(t_list **head)
 | 
			
		||||
{
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
	t_env	*content;
 | 
			
		||||
	char	**env;
 | 
			
		||||
	int		i;
 | 
			
		||||
 | 
			
		||||
	current = *head;
 | 
			
		||||
	env = ft_calloc(ft_lstsize(*head), sizeof(char *));
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (current->content)
 | 
			
		||||
	{
 | 
			
		||||
		content = current->content;
 | 
			
		||||
		env[i++] = ft_strmerger(3, content->key, "=", content->value);
 | 
			
		||||
		current = current->next;
 | 
			
		||||
	}
 | 
			
		||||
	return (env);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										108
									
								
								env/env3.c
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								env/env3.c
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,108 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   env3.c                                             :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/17 17:25:09 by erey-bet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/30 12:51:54 by erey-bet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "env_private.h"
 | 
			
		||||
 | 
			
		||||
char	*get_value(char *str)
 | 
			
		||||
{
 | 
			
		||||
	char	*s;
 | 
			
		||||
	int		i;
 | 
			
		||||
	int		start;
 | 
			
		||||
 | 
			
		||||
	s = ft_calloc(ft_strlen(str), sizeof(char));
 | 
			
		||||
	start = get_index(str, '=');
 | 
			
		||||
	i = start;
 | 
			
		||||
	while (str[++i])
 | 
			
		||||
		s[i - start - 1] = str[i];
 | 
			
		||||
	return (s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	*get_key(char *str)
 | 
			
		||||
{
 | 
			
		||||
	char	*s;
 | 
			
		||||
	int		i;
 | 
			
		||||
 | 
			
		||||
	s = ft_calloc(ft_strlen(str), sizeof(char));
 | 
			
		||||
	i = -1;
 | 
			
		||||
	while (str[++i] != '=')
 | 
			
		||||
		s[i] = str[i];
 | 
			
		||||
	return (s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	create_value_by_key_dup(char *key, char *value, t_list **env)
 | 
			
		||||
{
 | 
			
		||||
	char	*key_dup;
 | 
			
		||||
	char	*value_dup;
 | 
			
		||||
 | 
			
		||||
	if (set_value_by_key(key, value, env) == 0)
 | 
			
		||||
		return (0);
 | 
			
		||||
	key_dup = ft_strdup(key);
 | 
			
		||||
	if (key_dup == NULL)
 | 
			
		||||
		return (1);
 | 
			
		||||
	if (value != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		value_dup = ft_strdup(value);
 | 
			
		||||
		if (value_dup == NULL)
 | 
			
		||||
		{
 | 
			
		||||
			free(key);
 | 
			
		||||
			return (1);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
		value_dup = value;
 | 
			
		||||
	if (create_value_by_key(key_dup, value_dup, env))
 | 
			
		||||
		return (1);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	delete_by_key(char *key, t_list **head)
 | 
			
		||||
{
 | 
			
		||||
	t_list	*last;
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
 | 
			
		||||
	current = *head;
 | 
			
		||||
	while (current->next != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
 | 
			
		||||
		{
 | 
			
		||||
			free(((t_env *)current->content)->key);
 | 
			
		||||
			free(((t_env *)current->content)->value);
 | 
			
		||||
			free(current->content);
 | 
			
		||||
			if (last->next != NULL)
 | 
			
		||||
				last->next = current->next;
 | 
			
		||||
			else
 | 
			
		||||
				*head = current->next;
 | 
			
		||||
			free(current);
 | 
			
		||||
			return (0);
 | 
			
		||||
		}
 | 
			
		||||
		last = current;
 | 
			
		||||
		current = current->next;
 | 
			
		||||
	}
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	possible_key(char *key)
 | 
			
		||||
{
 | 
			
		||||
	int		i;
 | 
			
		||||
 | 
			
		||||
	i = -1;
 | 
			
		||||
	if (ft_isdigit(key[i + 1]))
 | 
			
		||||
		return (0);
 | 
			
		||||
	while (key[++i + 1])
 | 
			
		||||
		if (!ft_isalnum(key[i]) && key[i] != '_')
 | 
			
		||||
			return (0);
 | 
			
		||||
	if (key[i] == '+')
 | 
			
		||||
		return (2);
 | 
			
		||||
	else if (!ft_isalnum(key[i]) && key[i] != '_')
 | 
			
		||||
		return (0);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										117
									
								
								env/env_fill.c
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										117
									
								
								env/env_fill.c
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,117 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   env_fill.c                                         :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/16 16:29:08 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/29 19:06:44 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./env_private.h"
 | 
			
		||||
 | 
			
		||||
static char	*ft_getkey(const char *str)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	char	*key;
 | 
			
		||||
 | 
			
		||||
	if (ft_strncmp(str, "$$", 2) == 0)
 | 
			
		||||
		key = ft_strdup("$");
 | 
			
		||||
	else if (ft_strncmp(str, "$?", 2) == 0)
 | 
			
		||||
		key = ft_strdup("?");
 | 
			
		||||
	else if (str[1] == '\0')
 | 
			
		||||
		key = ft_strdup("");
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		i = 1;
 | 
			
		||||
		while (str[i] != '\0' && !ft_is_in("$?\'\" ", str[i]))
 | 
			
		||||
			i++;
 | 
			
		||||
		key = ft_strndup(str + 1, i - 1);
 | 
			
		||||
	}
 | 
			
		||||
	if (key == NULL)
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
	return (key);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char	*ft_getvalue(t_data *data, char *key)
 | 
			
		||||
{
 | 
			
		||||
	char	*value;
 | 
			
		||||
 | 
			
		||||
	if (ft_strcmp(key, "?") == 0)
 | 
			
		||||
		value = ft_itoa(data->exit_code);
 | 
			
		||||
	else if (ft_strcmp(key, "$") == 0)
 | 
			
		||||
		value = ft_strdup("PID");
 | 
			
		||||
	else if (key[0] == '\0')
 | 
			
		||||
		value = ft_strdup("$");
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		value = get_value_by_key(key, data->env);
 | 
			
		||||
		if (value == NULL)
 | 
			
		||||
			value = ft_strdup("");
 | 
			
		||||
		else
 | 
			
		||||
			value = ft_strdup(value);
 | 
			
		||||
	}
 | 
			
		||||
	if (value == NULL)
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
	return (value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char	*ft_getvalue_by_str(t_data *data, const char *str,
 | 
			
		||||
		size_t	*key_len)
 | 
			
		||||
{
 | 
			
		||||
	char	*key;
 | 
			
		||||
	char	*value;
 | 
			
		||||
 | 
			
		||||
	key = ft_getkey(str);
 | 
			
		||||
	if (key == NULL)
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	*key_len = ft_strlen(key) + 1;
 | 
			
		||||
	value = ft_getvalue(data, key);
 | 
			
		||||
	free(key);
 | 
			
		||||
	return (value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	*ft_str_formator(t_data *data, char *str, size_t *i)
 | 
			
		||||
{
 | 
			
		||||
	char	*value;
 | 
			
		||||
	size_t	key_len;
 | 
			
		||||
	char	*out;
 | 
			
		||||
 | 
			
		||||
	value = ft_getvalue_by_str(data, str + *i, &key_len);
 | 
			
		||||
	if (value == NULL)
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	out = ft_strreplace(str, value, *i, key_len + *i);
 | 
			
		||||
	*i = *i + ft_strlen(value);
 | 
			
		||||
	free(value);
 | 
			
		||||
	return (out);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	*ft_env_filler(t_data *data, const char *str)
 | 
			
		||||
{
 | 
			
		||||
	char	*out;
 | 
			
		||||
	char	*temp;
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	out = ft_strdup(str);
 | 
			
		||||
	if (out == NULL)
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (out[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		while (ft_is_in_quote(out, i) == 1)
 | 
			
		||||
			i++;
 | 
			
		||||
		while (out[i] == '$')
 | 
			
		||||
		{
 | 
			
		||||
			temp = ft_str_formator(data, out, &i);
 | 
			
		||||
			if (temp == NULL)
 | 
			
		||||
				return (NULL);
 | 
			
		||||
			free(out);
 | 
			
		||||
			out = temp;
 | 
			
		||||
		}
 | 
			
		||||
		if (out[i] != '\0')
 | 
			
		||||
			i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (out);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								env/env_private.h
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								env/env_private.h
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   env_private.h                                      :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:40:24 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/27 13:40:25 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef ENV_PRIVATE_H
 | 
			
		||||
# define ENV_PRIVATE_H
 | 
			
		||||
# include "./env.h"
 | 
			
		||||
# include "../libftx/libftx.h"
 | 
			
		||||
# include "../utils/utils.h"
 | 
			
		||||
 | 
			
		||||
void		swap_env_3(void **a, void **b, void **c);
 | 
			
		||||
void		swap_env(void **a, void **b);
 | 
			
		||||
char		*get_value(char *str);
 | 
			
		||||
char		*get_key(char *str);
 | 
			
		||||
int			get_index(char *str, char c);
 | 
			
		||||
char		*ft_env_filler(t_data *data, const char *str);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										76
									
								
								env_fill.c
									
									
									
									
									
								
							
							
						
						
									
										76
									
								
								env_fill.c
									
									
									
									
									
								
							@ -1,76 +0,0 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   env_fill.c                                         :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/16 16:29:08 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/17 13:41:51 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "libftx/libftx.h"
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
#include "utils/utils.h"
 | 
			
		||||
 | 
			
		||||
static char	*ft_get_value(t_list **env, const char *str, size_t start,
 | 
			
		||||
		size_t stop)
 | 
			
		||||
{
 | 
			
		||||
	char	*key;
 | 
			
		||||
	char	*value;
 | 
			
		||||
 | 
			
		||||
	key = ft_strndup(str + start, stop);
 | 
			
		||||
	if (key == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	value = get_value_by_key(key, env);
 | 
			
		||||
	if (value == NULL)
 | 
			
		||||
		value = "";
 | 
			
		||||
	free(key);
 | 
			
		||||
	return (value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	*ft_env_filler(t_list **env, const char *str)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	size_t	y;
 | 
			
		||||
	char	*out;
 | 
			
		||||
	char	*temp;
 | 
			
		||||
	char	*value;
 | 
			
		||||
 | 
			
		||||
	out = ft_strdup(str);
 | 
			
		||||
	if (out == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (out[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		while (ft_is_in_quote(out, i) == 1)
 | 
			
		||||
			i++;
 | 
			
		||||
		while (out[i] == '$')
 | 
			
		||||
		{
 | 
			
		||||
			y = i + 1;
 | 
			
		||||
			while (out[y] != '\0' && out[y] != '$' && out[y] != ' ')
 | 
			
		||||
				y++;
 | 
			
		||||
			value = ft_get_value(env, out, i + 1, y - i - 1);
 | 
			
		||||
			if (value == NULL)
 | 
			
		||||
				return (NULL);
 | 
			
		||||
			temp = ft_strreplace(out, value, i, y);
 | 
			
		||||
			free(out);
 | 
			
		||||
			if (temp == NULL)
 | 
			
		||||
			{
 | 
			
		||||
				ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
				return (NULL);
 | 
			
		||||
			}
 | 
			
		||||
			out = temp;
 | 
			
		||||
		}
 | 
			
		||||
		if (out[i] != '\0')
 | 
			
		||||
			i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (out);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										116
									
								
								execution.c
									
									
									
									
									
								
							
							
						
						
									
										116
									
								
								execution.c
									
									
									
									
									
								
							@ -1,116 +0,0 @@
 | 
			
		||||
#include "libftx/libftx.h"
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
#include "utils/utils.h"
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
static char	*ft_get_executable_path(char *executable_name, t_list **env)
 | 
			
		||||
{
 | 
			
		||||
	char	*path;
 | 
			
		||||
	char	*temp;
 | 
			
		||||
	char	**tab;
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	path = NULL;
 | 
			
		||||
	if (executable_name[0] == '.' || executable_name[0] == '/')
 | 
			
		||||
	{
 | 
			
		||||
		path = ft_strdup(executable_name);
 | 
			
		||||
		if (path == NULL)
 | 
			
		||||
		{
 | 
			
		||||
			ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
			return (NULL);
 | 
			
		||||
		}
 | 
			
		||||
		if (access(path, X_OK) == 0)
 | 
			
		||||
		{
 | 
			
		||||
			ft_eprintf("minishell: %s: permission denied\n", path);
 | 
			
		||||
			return (NULL);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		tab = ft_split(get_value_by_key("PATH", env), ':');
 | 
			
		||||
		if (tab == NULL)
 | 
			
		||||
			return (NULL);
 | 
			
		||||
		i = 0;
 | 
			
		||||
		while (tab[i] != NULL)
 | 
			
		||||
		{
 | 
			
		||||
			temp = ft_strmerger(3, tab[i], "/", executable_name);
 | 
			
		||||
			if (temp == NULL)
 | 
			
		||||
			{
 | 
			
		||||
				ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
				free(executable_name);
 | 
			
		||||
				ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
			}
 | 
			
		||||
			if (access(temp, X_OK) == 0)
 | 
			
		||||
			{
 | 
			
		||||
				path = temp;
 | 
			
		||||
				break ;
 | 
			
		||||
			}
 | 
			
		||||
			free(temp);
 | 
			
		||||
			i++;
 | 
			
		||||
		}
 | 
			
		||||
		if (path == NULL)
 | 
			
		||||
		{
 | 
			
		||||
			ft_eprintf("%s: command not found\n", executable_name);
 | 
			
		||||
		}
 | 
			
		||||
		ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
	}
 | 
			
		||||
	return (path);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_excutor(t_cmd *cmd, t_list **env)
 | 
			
		||||
{
 | 
			
		||||
	int		pid;
 | 
			
		||||
	int		return_value;
 | 
			
		||||
	char	**tab;
 | 
			
		||||
 | 
			
		||||
	if (cmd->fd_in == -1 || cmd->fd_out == -1)
 | 
			
		||||
		return (1);
 | 
			
		||||
	pid = fork();
 | 
			
		||||
	if (pid == -1)
 | 
			
		||||
		return (1);
 | 
			
		||||
	if (pid == 0)
 | 
			
		||||
	{
 | 
			
		||||
		tab = env_to_strs(env);
 | 
			
		||||
		if (tab == NULL)
 | 
			
		||||
			return (1);
 | 
			
		||||
		tab = NULL;
 | 
			
		||||
		dup2(cmd->fd_out, 1);
 | 
			
		||||
		dup2(cmd->fd_in, 0);
 | 
			
		||||
		execve(cmd->executable, cmd->args, tab);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
		waitpid(pid, &return_value, 0);
 | 
			
		||||
	return (return_value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_cmds_executor(t_list **cmds, t_list **env)
 | 
			
		||||
{
 | 
			
		||||
	t_cmd	*content;
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
	int		fds[2];
 | 
			
		||||
 | 
			
		||||
	current = *cmds;
 | 
			
		||||
	while (current != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		content = current->content;
 | 
			
		||||
		if (current->next != NULL)
 | 
			
		||||
		{
 | 
			
		||||
			if (pipe(fds) == -1)
 | 
			
		||||
			{
 | 
			
		||||
				ft_eprintf("minishell: pipe failed\n");
 | 
			
		||||
				return (1);
 | 
			
		||||
			}
 | 
			
		||||
			content->fd_out = fds[1];
 | 
			
		||||
			((t_cmd *) current->next->content)->fd_in = fds[0];
 | 
			
		||||
		}
 | 
			
		||||
		content->executable = ft_get_executable_path(content->executable, env);
 | 
			
		||||
		if (content->executable != NULL)
 | 
			
		||||
			ft_excutor(content, env);
 | 
			
		||||
		if (content->fd_in != 0)
 | 
			
		||||
			close(content->fd_in);
 | 
			
		||||
		if (content->fd_out != 1 && content->fd_out != 2)
 | 
			
		||||
			close(content->fd_out);
 | 
			
		||||
		current = current->next;
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										119
									
								
								execution/execution.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										119
									
								
								execution/execution.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,119 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   execution.c                                        :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/21 12:45:16 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/30 14:03:07 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "execution_private.h"
 | 
			
		||||
 | 
			
		||||
static int	ft_execute_own_cmd(t_data *data, t_cmd *cmd)
 | 
			
		||||
{
 | 
			
		||||
	int	return_code;
 | 
			
		||||
 | 
			
		||||
	if (ft_strcmp(cmd->executable, "pwd") == 0)
 | 
			
		||||
		return_code = pwd(cmd->fd_out[0]);
 | 
			
		||||
	else if (ft_strcmp(cmd->executable, "env") == 0)
 | 
			
		||||
		return_code = print_env(data->env, cmd->fd_out[0]);
 | 
			
		||||
	else if (ft_strcmp(cmd->executable, "export") == 0)
 | 
			
		||||
		return_code = ft_export(data->env, cmd->args + 1, cmd->fd_out[0]);
 | 
			
		||||
	else if (ft_strcmp(cmd->executable, "cd") == 0)
 | 
			
		||||
		return_code = (move_folder(cmd->args + 1, cmd->fd_out[0]));
 | 
			
		||||
	else if (ft_strcmp(cmd->executable, "unset") == 0)
 | 
			
		||||
		return_code = (unset(data->env, cmd->args, cmd->fd_out[0]));
 | 
			
		||||
	else if (ft_strcmp(cmd->executable, "echo") == 0)
 | 
			
		||||
		return_code = (echo(cmd->fd_out[0], cmd->args + 1));
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		return_code = ft_exit(cmd->args + 1);
 | 
			
		||||
		if (return_code >= 0)
 | 
			
		||||
		{
 | 
			
		||||
			data->exit_code = return_code;
 | 
			
		||||
			return (-1);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	data->exit_code = return_code;
 | 
			
		||||
	return (return_code);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool	ft_executor(t_cmd *cmd, char **env, int fd)
 | 
			
		||||
{
 | 
			
		||||
	if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL)
 | 
			
		||||
		return (0);
 | 
			
		||||
	cmd->pid = fork();
 | 
			
		||||
	ft_get_data()->child_pid = cmd->pid;
 | 
			
		||||
	if (cmd->pid == -1)
 | 
			
		||||
		return (1);
 | 
			
		||||
	if (cmd->pid == 0)
 | 
			
		||||
	{
 | 
			
		||||
		dup2(cmd->fd_in[0], 0);
 | 
			
		||||
		dup2(cmd->fd_out[0], 1);
 | 
			
		||||
		if (fd != -1)
 | 
			
		||||
			close(fd);
 | 
			
		||||
		ft_closer(cmd->fd_in);
 | 
			
		||||
		ft_closer(cmd->fd_out);
 | 
			
		||||
		execve(cmd->executable, cmd->args, env);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_cmd_executor(t_data *data, t_cmd *cmd, int fd)
 | 
			
		||||
{
 | 
			
		||||
	int		exit_code;
 | 
			
		||||
	char	**env;
 | 
			
		||||
 | 
			
		||||
	if (cmd->own_cmd == 1)
 | 
			
		||||
	{
 | 
			
		||||
		exit_code = ft_execute_own_cmd(data, cmd);
 | 
			
		||||
		ft_closer(cmd->fd_in);
 | 
			
		||||
		ft_closer(cmd->fd_out);
 | 
			
		||||
		if (exit_code == -1)
 | 
			
		||||
			return (1);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		env = env_to_strs(data->env);
 | 
			
		||||
		if (env == NULL)
 | 
			
		||||
			return (1);
 | 
			
		||||
		exit_code = ft_executor(cmd, env, fd);
 | 
			
		||||
		ft_closer(cmd->fd_in);
 | 
			
		||||
		ft_closer(cmd->fd_out);
 | 
			
		||||
		ft_freer_tab_ultimate(1, env);
 | 
			
		||||
		if (exit_code == 1)
 | 
			
		||||
			return (1);
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_cmds_executor(t_data *data)
 | 
			
		||||
{
 | 
			
		||||
	int		fds[2];
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
	t_cmd	*content;
 | 
			
		||||
 | 
			
		||||
	current = *data->cmds;
 | 
			
		||||
	while (current != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		content = current->content;
 | 
			
		||||
		fds[0] = -1;
 | 
			
		||||
		if (current->next != NULL)
 | 
			
		||||
		{
 | 
			
		||||
			if (pipe(fds) == -1)
 | 
			
		||||
				return (1);
 | 
			
		||||
			ft_add_fd(content->fd_out, fds[1]);
 | 
			
		||||
			ft_add_fd(((t_cmd *)(current->next->content))->fd_in, fds[0]);
 | 
			
		||||
		}
 | 
			
		||||
		if (content->fd_in[0] == -2 || content->fd_out[0] == -2)
 | 
			
		||||
			ft_mega_closer(content->fd_in, content->fd_out);
 | 
			
		||||
		else if (ft_cmd_executor(data, content, fds[0]))
 | 
			
		||||
			return (1);
 | 
			
		||||
		current = current->next;
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								execution/execution.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								execution/execution.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   execution.h                                        :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/28 15:46:52 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/28 15:46:53 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef EXECUTION_H
 | 
			
		||||
# define EXECUTION_H
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
 | 
			
		||||
int	ft_cmds_executor(t_data *data);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										22
									
								
								execution/execution_private.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								execution/execution_private.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   execution_private.h                                :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/28 15:45:53 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/29 18:55:22 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef EXECUTION_PRIVATE_H
 | 
			
		||||
# define EXECUTION_PRIVATE_H
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
# include "../libftx/libftx.h"
 | 
			
		||||
# include "../cmd/cmd.h"
 | 
			
		||||
# include "../env/env.h"
 | 
			
		||||
# include "../builtins/builtins.h"
 | 
			
		||||
# include "../utils/utils.h"
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
@ -1,23 +1,35 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   spacer.c                                           :+:      :+:    :+:   */
 | 
			
		||||
/*   format.c                                           :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/15 13:35:50 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/16 16:26:15 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*   Updated: 2023/03/30 16:01:56 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "libftx/libftx.h"
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
#include "utils/utils.h"
 | 
			
		||||
#include "./format_private.h"
 | 
			
		||||
 | 
			
		||||
static int	ft_replace(char **str, size_t i)
 | 
			
		||||
{
 | 
			
		||||
	char	*temp;
 | 
			
		||||
 | 
			
		||||
	temp = ft_strreplace(*str, " ", i, i);
 | 
			
		||||
	free(*str);
 | 
			
		||||
	if (temp == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	*str = temp;
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char	*ft_spacer_after(const char *str)
 | 
			
		||||
{
 | 
			
		||||
	char	*out;
 | 
			
		||||
	char	*temp;
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	out = ft_strdup(str);
 | 
			
		||||
@ -26,18 +38,18 @@ static char	*ft_spacer_after(const char *str)
 | 
			
		||||
	i = 1;
 | 
			
		||||
	while (out[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		while (ft_is_in_quote(out, i))
 | 
			
		||||
		while (ft_is_in_quote(out, i - 1))
 | 
			
		||||
			i++;
 | 
			
		||||
		if (out[i] == '\0')
 | 
			
		||||
			break ;
 | 
			
		||||
		if (ft_is_in("><|", out[i - 1]))
 | 
			
		||||
		{
 | 
			
		||||
			while (str[i] == out[i - 1])
 | 
			
		||||
			while (out[i] == out[i - 1])
 | 
			
		||||
				i++;
 | 
			
		||||
			temp = ft_strreplace(out, " ", i, i);
 | 
			
		||||
			free(out);
 | 
			
		||||
			out = temp;
 | 
			
		||||
			if (out == NULL)
 | 
			
		||||
			if (ft_replace(&out, i))
 | 
			
		||||
				return (NULL);
 | 
			
		||||
		}
 | 
			
		||||
		if (out[i] != '\0')
 | 
			
		||||
			i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (out);
 | 
			
		||||
@ -46,30 +58,27 @@ static char	*ft_spacer_after(const char *str)
 | 
			
		||||
static char	*ft_spacer_before(const char *str)
 | 
			
		||||
{
 | 
			
		||||
	char	*out;
 | 
			
		||||
	char	*temp;
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	ssize_t	i;
 | 
			
		||||
 | 
			
		||||
	out = ft_strdup(str);
 | 
			
		||||
	if (out == NULL)
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (out[i + 1] != '\0')
 | 
			
		||||
	i = -1;
 | 
			
		||||
	while (out[++i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		while (ft_is_in_quote(out, i))
 | 
			
		||||
		while (ft_is_in_quote(out, i + 1))
 | 
			
		||||
			i++;
 | 
			
		||||
		if (out[i] == '\0')
 | 
			
		||||
			break ;
 | 
			
		||||
		if (ft_is_in("><|", out[i + 1]))
 | 
			
		||||
		{
 | 
			
		||||
			while (out[i] == ' ')
 | 
			
		||||
				i++;
 | 
			
		||||
			while (out[i] == out[i + 1])
 | 
			
		||||
				i++;
 | 
			
		||||
			temp = ft_strreplace(out, " ", i + 1, i + 1);
 | 
			
		||||
			free(out);
 | 
			
		||||
			out = temp;
 | 
			
		||||
			if (out == NULL)
 | 
			
		||||
			if (ft_replace(&out, i + 1))
 | 
			
		||||
				return (NULL);
 | 
			
		||||
		}
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (out);
 | 
			
		||||
}
 | 
			
		||||
@ -84,6 +93,8 @@ static void	ft_space_simplifier(char *str)
 | 
			
		||||
	{
 | 
			
		||||
		if (ft_is_in_quote(str, i))
 | 
			
		||||
			i++;
 | 
			
		||||
		if (str[i] != '\0')
 | 
			
		||||
			break ;
 | 
			
		||||
		y = 0;
 | 
			
		||||
		while (str[y + i] == ' ')
 | 
			
		||||
			y++;
 | 
			
		||||
@ -96,7 +107,7 @@ static void	ft_space_simplifier(char *str)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	*ft_normalizer(char *str)
 | 
			
		||||
char	*ft_formater(t_data *data, const char *str)
 | 
			
		||||
{
 | 
			
		||||
	char	*out;
 | 
			
		||||
	char	*temp;
 | 
			
		||||
@ -113,5 +124,7 @@ char	*ft_normalizer(char *str)
 | 
			
		||||
	ft_space_simplifier(out);
 | 
			
		||||
	if (out[ft_strlen(out) - 1] == ' ')
 | 
			
		||||
		out[ft_strlen(out) - 1] = '\0';
 | 
			
		||||
	return (out);
 | 
			
		||||
	temp = ft_env_filler(data, out);
 | 
			
		||||
	free(out);
 | 
			
		||||
	return (temp);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								format/format.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								format/format.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   format.h                                           :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/28 15:44:56 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/28 15:44:57 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef FORMAT_H
 | 
			
		||||
# define FORMAT_H
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
 | 
			
		||||
char	*ft_formater(t_data *data, const char *str);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										18
									
								
								format/format_private.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								format/format_private.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,18 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   format_private.h                                   :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/28 15:44:59 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/28 15:45:00 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef FORMAT_PRIVATE_H
 | 
			
		||||
# define FORMAT_PRIVATE_H
 | 
			
		||||
# include "../libftx/libftx.h"
 | 
			
		||||
# include "../utils/utils.h"
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										113
									
								
								infile.c
									
									
									
									
									
								
							
							
						
						
									
										113
									
								
								infile.c
									
									
									
									
									
								
							@ -1,113 +0,0 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   infile.c                                           :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/15 17:52:10 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/17 13:19:09 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "libftx/libftx.h"
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
#include "utils/utils.h"
 | 
			
		||||
 | 
			
		||||
static int	ft_infile_is_valid(const char *line)
 | 
			
		||||
{
 | 
			
		||||
	char	**tab;
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	tab = ft_split_quoted(line, ' ');
 | 
			
		||||
	if (tab == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	if (tab[0] == NULL)
 | 
			
		||||
		return (1);
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (tab[i + 1] != NULL)
 | 
			
		||||
		i++;
 | 
			
		||||
	if (tab[i][0] == '<')
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]);
 | 
			
		||||
		ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_get_infile(const char *line)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	int		fd;
 | 
			
		||||
	char	**tab;
 | 
			
		||||
 | 
			
		||||
	tab = ft_split_quoted(line, ' ');
 | 
			
		||||
	if (tab == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (-2);
 | 
			
		||||
	}
 | 
			
		||||
	fd = 0;
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (tab[i + 1] != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		if (tab[i][0] == '<')
 | 
			
		||||
			if (fd != 0)
 | 
			
		||||
				close(fd);
 | 
			
		||||
		if (ft_strcmp("<", tab[i]) == 0)
 | 
			
		||||
			fd = ft_file_is_readable(ft_quote_remover(tab[i + 1]));
 | 
			
		||||
		else if (ft_strcmp("<<", tab[i]) == 0)
 | 
			
		||||
			fd = ft_heredoc(tab[i + 1]);
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
	return (fd);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_remove_infile(char *line)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	size_t	y;
 | 
			
		||||
	char	**tab;
 | 
			
		||||
 | 
			
		||||
	tab = ft_split_quoted(line, ' ');
 | 
			
		||||
	if (tab == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	i = 0;
 | 
			
		||||
	y = 0;
 | 
			
		||||
	while (tab[i] != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		if (tab[i][0] == '<')
 | 
			
		||||
		{
 | 
			
		||||
			ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
 | 
			
		||||
			i++;
 | 
			
		||||
			ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
			y = y + ft_strlen(tab[i]);
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_infile(char *line)
 | 
			
		||||
{
 | 
			
		||||
	int	fd;
 | 
			
		||||
 | 
			
		||||
	if (ft_infile_is_valid(line) == 0)
 | 
			
		||||
		return (-2);
 | 
			
		||||
	fd = ft_get_infile(line);
 | 
			
		||||
	if (fd == -2)
 | 
			
		||||
		return (-2);
 | 
			
		||||
	ft_remove_infile(line);
 | 
			
		||||
	return (fd);
 | 
			
		||||
}
 | 
			
		||||
@ -1,3 +1,15 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   ft_swap.c                                          :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:43:25 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/27 13:43:26 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "extra.h"
 | 
			
		||||
 | 
			
		||||
void	ft_swap(void *a, void *b)
 | 
			
		||||
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							@ -36,4 +36,4 @@ fclean: clean
 | 
			
		||||
 | 
			
		||||
re: fclean all
 | 
			
		||||
 | 
			
		||||
.PHONY: all bonus clean fclean re
 | 
			
		||||
.PHONY: all clean fclean re
 | 
			
		||||
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							@ -14,6 +14,7 @@
 | 
			
		||||
# define LIBFT_H
 | 
			
		||||
# include <stdlib.h>
 | 
			
		||||
# include <unistd.h>
 | 
			
		||||
# include "./list.h"
 | 
			
		||||
 | 
			
		||||
void	*ft_cancel(void **tab, size_t len);
 | 
			
		||||
int		ft_atoi(const char *nptr);
 | 
			
		||||
@ -52,20 +53,4 @@ void	ft_putstr_fd(char *s, int fd);
 | 
			
		||||
void	ft_putendl_fd(char *s, int fd);
 | 
			
		||||
void	ft_putnbr_fd(int n, int fd);
 | 
			
		||||
 | 
			
		||||
typedef struct s_list
 | 
			
		||||
{
 | 
			
		||||
	void			*content;
 | 
			
		||||
	struct s_list	*next;
 | 
			
		||||
}	t_list;
 | 
			
		||||
 | 
			
		||||
t_list	*ft_lstnew(void *content);
 | 
			
		||||
void	ft_lstadd_front(t_list **lst, t_list *nouveau);
 | 
			
		||||
int		ft_lstsize(t_list *lst);
 | 
			
		||||
t_list	*ft_lstlast(t_list *lst);
 | 
			
		||||
void	ft_lstadd_back(t_list **lst, t_list *nouveau);
 | 
			
		||||
void	ft_lstdelone(t_list *lst, void (*del)(void *));
 | 
			
		||||
void	ft_lstclear(t_list **lst, void (*del)(void *));
 | 
			
		||||
void	ft_lstiter(t_list *lst, void (*f)(void *));
 | 
			
		||||
t_list	*ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										32
									
								
								libftx/libft/list.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								libftx/libft/list.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   list.h                                             :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:43:53 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/27 13:43:54 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef LIST_H
 | 
			
		||||
# define LIST_H
 | 
			
		||||
 | 
			
		||||
typedef struct s_list
 | 
			
		||||
{
 | 
			
		||||
	void			*content;
 | 
			
		||||
	struct s_list	*next;
 | 
			
		||||
}	t_list;
 | 
			
		||||
 | 
			
		||||
t_list	*ft_lstnew(void *content);
 | 
			
		||||
void	ft_lstadd_front(t_list **lst, t_list *nouveau);
 | 
			
		||||
int		ft_lstsize(t_list *lst);
 | 
			
		||||
t_list	*ft_lstlast(t_list *lst);
 | 
			
		||||
void	ft_lstadd_back(t_list **lst, t_list *nouveau);
 | 
			
		||||
void	ft_lstdelone(t_list *lst, void (*del)(void *));
 | 
			
		||||
void	ft_lstclear(t_list **lst, void (*del)(void *));
 | 
			
		||||
void	ft_lstiter(t_list *lst, void (*f)(void *));
 | 
			
		||||
t_list	*ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
@ -6,7 +6,7 @@
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2022/09/26 14:47:54 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/17 16:24:42 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*   Updated: 2023/02/21 13:28:19 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
@ -15,10 +15,13 @@
 | 
			
		||||
# include <stdlib.h>
 | 
			
		||||
# include <unistd.h>
 | 
			
		||||
# include <stdarg.h>
 | 
			
		||||
# include "./libft/libft.h"
 | 
			
		||||
 | 
			
		||||
char	*ft_ultoa_base(unsigned long long n, char *base);
 | 
			
		||||
int		ft_printf(const char *format, ...);
 | 
			
		||||
int		ft_eprintf(const char *format, ...);
 | 
			
		||||
int		ft_dprintf(int fd, const char *format, ...);
 | 
			
		||||
int		ft_dprintf(int fd, const char *format, ...);
 | 
			
		||||
char	*get_next_line(int fd);
 | 
			
		||||
size_t	ft_random_generator(size_t start, size_t stop);
 | 
			
		||||
void	ft_freer_tab_ultimate(size_t len, ...);
 | 
			
		||||
@ -37,57 +40,41 @@ void	ft_swap(void *a, void *b);
 | 
			
		||||
void	ft_swap_int(int *a, int *b);
 | 
			
		||||
void	ft_swap_char(char *a, char *b);
 | 
			
		||||
 | 
			
		||||
void	*ft_cancel(void **tab, size_t len);
 | 
			
		||||
int		ft_atoi(const char *nptr);
 | 
			
		||||
void	ft_bzero(void *s, size_t n);
 | 
			
		||||
void	*ft_calloc(size_t nmemb, size_t size);
 | 
			
		||||
int		ft_isalnum(int c);
 | 
			
		||||
int		ft_isalpha(int c);
 | 
			
		||||
int		ft_isascii(int c);
 | 
			
		||||
int		ft_isdigit(int c);
 | 
			
		||||
int		ft_isprint(int c);
 | 
			
		||||
void	*ft_memchr(const void *s, int c, size_t n);
 | 
			
		||||
int		ft_memcmp(const void *s1, const void *s2, size_t n);
 | 
			
		||||
void	*ft_memcpy(void *dest, const void *src, size_t n);
 | 
			
		||||
void	*ft_memmove(void *dest, const void *src, size_t n);
 | 
			
		||||
void	*ft_memset(void *s, int c, size_t n);
 | 
			
		||||
char	*ft_strchr(const char *s, int c);
 | 
			
		||||
char	*ft_strdup(const char *s);
 | 
			
		||||
size_t	ft_strlcat(char *dst, const char *src, size_t size);
 | 
			
		||||
size_t	ft_strlcpy(char *dst, const char *src, size_t size);
 | 
			
		||||
size_t	ft_strlen(const char *s);
 | 
			
		||||
int		ft_strncmp(const char *s1, const char *s2, size_t n);
 | 
			
		||||
char	*ft_strnstr(const char *big, const char *little, size_t len);
 | 
			
		||||
char	*ft_strrchr(const char *s, int c);
 | 
			
		||||
int		ft_tolower(int c);
 | 
			
		||||
int		ft_toupper(int c);
 | 
			
		||||
/* void	*ft_cancel(void **tab, size_t len); */
 | 
			
		||||
/* int		ft_atoi(const char *nptr); */
 | 
			
		||||
/* void	ft_bzero(void *s, size_t n); */
 | 
			
		||||
/* void	*ft_calloc(size_t nmemb, size_t size); */
 | 
			
		||||
/* int		ft_isalnum(int c); */
 | 
			
		||||
/* int		ft_isalpha(int c); */
 | 
			
		||||
/* int		ft_isascii(int c); */
 | 
			
		||||
/* int		ft_isdigit(int c); */
 | 
			
		||||
/* int		ft_isprint(int c); */
 | 
			
		||||
/* void	*ft_memchr(const void *s, int c, size_t n); */
 | 
			
		||||
/* int		ft_memcmp(const void *s1, const void *s2, size_t n); */
 | 
			
		||||
/* void	*ft_memcpy(void *dest, const void *src, size_t n); */
 | 
			
		||||
/* void	*ft_memmove(void *dest, const void *src, size_t n); */
 | 
			
		||||
/* void	*ft_memset(void *s, int c, size_t n); */
 | 
			
		||||
/* char	*ft_strchr(const char *s, int c); */
 | 
			
		||||
/* char	*ft_strdup(const char *s); */
 | 
			
		||||
/* size_t	ft_strlcat(char *dst, const char *src, size_t size); */
 | 
			
		||||
/* size_t	ft_strlcpy(char *dst, const char *src, size_t size); */
 | 
			
		||||
/* size_t	ft_strlen(const char *s); */
 | 
			
		||||
/* int		ft_strncmp(const char *s1, const char *s2, size_t n); */
 | 
			
		||||
/* char	*ft_strnstr(const char *big, const char *little, size_t len); */
 | 
			
		||||
/* char	*ft_strrchr(const char *s, int c); */
 | 
			
		||||
/* 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(char const *s1, char const *s2);
 | 
			
		||||
char	*ft_strtrim(char const *s1, char const *set);
 | 
			
		||||
char	**ft_split(char const *s, char c);
 | 
			
		||||
char	*ft_itoa(int n);
 | 
			
		||||
char	*ft_strmapi(char const *s, char (*f)(unsigned int, char));
 | 
			
		||||
void	ft_striteri(char *s, void (*f)(unsigned int, char*));
 | 
			
		||||
void	ft_putchar_fd(char c, int fd);
 | 
			
		||||
void	ft_putstr_fd(char *s, int fd);
 | 
			
		||||
void	ft_putendl_fd(char *s, int fd);
 | 
			
		||||
void	ft_putnbr_fd(int n, int fd);
 | 
			
		||||
 | 
			
		||||
typedef struct s_list
 | 
			
		||||
{
 | 
			
		||||
	void			*content;
 | 
			
		||||
	struct s_list	*next;
 | 
			
		||||
}	t_list;
 | 
			
		||||
 | 
			
		||||
t_list	*ft_lstnew(void *content);
 | 
			
		||||
void	ft_lstadd_front(t_list **lst, t_list *nouveau);
 | 
			
		||||
int		ft_lstsize(t_list *lst);
 | 
			
		||||
t_list	*ft_lstlast(t_list *lst);
 | 
			
		||||
void	ft_lstadd_back(t_list **lst, t_list *nouveau);
 | 
			
		||||
void	ft_lstdelone(t_list *lst, void (*del)(void *));
 | 
			
		||||
void	ft_lstclear(t_list **lst, void (*del)(void *));
 | 
			
		||||
void	ft_lstiter(t_list *lst, void (*f)(void *));
 | 
			
		||||
t_list	*ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
 | 
			
		||||
/* char	*ft_substr(char const *s, unsigned int start, size_t len); */
 | 
			
		||||
/* char	*ft_strjoin(char const *s1, char const *s2); */
 | 
			
		||||
/* char	*ft_strtrim(char const *s1, char const *set); */
 | 
			
		||||
/* char	**ft_split(char const *s, char c); */
 | 
			
		||||
/* char	*ft_itoa(int n); */
 | 
			
		||||
/* char	*ft_strmapi(char const *s, char (*f)(unsigned int, char)); */
 | 
			
		||||
/* void	ft_striteri(char *s, void (*f)(unsigned int, char*)); */
 | 
			
		||||
/* void	ft_putchar_fd(char c, int fd); */
 | 
			
		||||
/* void	ft_putstr_fd(char *s, int fd); */
 | 
			
		||||
/* void	ft_putendl_fd(char *s, int fd); */
 | 
			
		||||
/* void	ft_putnbr_fd(int n, int fd); */
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
@ -6,7 +6,7 @@
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2022/10/06 22:01:28 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2022/10/12 16:34:31 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*   Updated: 2023/02/21 13:27:31 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
@ -22,3 +22,14 @@ int	ft_printf(const char *format, ...)
 | 
			
		||||
	va_end(va);
 | 
			
		||||
	return (i);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_dprintf(int fd, const char *format, ...)
 | 
			
		||||
{
 | 
			
		||||
	va_list	va;
 | 
			
		||||
	int		i;
 | 
			
		||||
 | 
			
		||||
	va_start(va, format);
 | 
			
		||||
	i = ft_vdprintf(fd, format, va);
 | 
			
		||||
	va_end(va);
 | 
			
		||||
	return (i);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -6,7 +6,7 @@
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2022/09/26 14:47:54 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/17 16:29:18 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*   Updated: 2023/02/21 13:27:51 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
@ -32,6 +32,7 @@ int		ft_dprintarg(int fd, int c, va_list va);
 | 
			
		||||
int		ft_dprintstrtab(int fd, char **tab);
 | 
			
		||||
 | 
			
		||||
int		ft_printf(const char *format, ...);
 | 
			
		||||
int		ft_dprintf(int fd, const char *format, ...);	
 | 
			
		||||
int		ft_eprintf(const char *format, ...);
 | 
			
		||||
 | 
			
		||||
int		ft_vdprintf(int fd, const char *format, va_list va);
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										186
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										186
									
								
								main.c
									
									
									
									
									
								
							@ -6,145 +6,137 @@
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/16 15:16:14 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/17 16:06:02 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*   Updated: 2023/03/30 13:12:41 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "data/data.h"
 | 
			
		||||
#include "env/env.h"
 | 
			
		||||
#include "libftx/libftx.h"
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
 | 
			
		||||
static char	*ft_get_user_input(t_list **env)
 | 
			
		||||
static char	*ft_get_user_input(void)
 | 
			
		||||
{
 | 
			
		||||
	char	*line;
 | 
			
		||||
	char	*prompt;
 | 
			
		||||
	char	*pwd;
 | 
			
		||||
 | 
			
		||||
	prompt = ft_strmerger(2, get_value_by_key("PWD", env), "$ ");
 | 
			
		||||
	pwd = get_pwd(2);
 | 
			
		||||
	if (pwd == NULL)
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	prompt = ft_strmerger(2, pwd, "$ ");
 | 
			
		||||
	free(pwd);
 | 
			
		||||
	if (prompt == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	line = readline(prompt);
 | 
			
		||||
	if (line != NULL && ft_strcmp(line, "") != 0)
 | 
			
		||||
		add_history(line);
 | 
			
		||||
	free(prompt);
 | 
			
		||||
	if (line == NULL)
 | 
			
		||||
		ft_printf("exit\n");
 | 
			
		||||
	return (line);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_minishell(t_list **env, char *line)
 | 
			
		||||
static void	ft_cmds_waiter(t_data *data)
 | 
			
		||||
{
 | 
			
		||||
	t_list	*current;
 | 
			
		||||
	t_cmd	*cmd;
 | 
			
		||||
	int		exit_status;
 | 
			
		||||
 | 
			
		||||
	current = *data->cmds;
 | 
			
		||||
	while (current != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		cmd = current->content;
 | 
			
		||||
		if (cmd->executable != NULL && cmd->own_cmd == 0
 | 
			
		||||
			&& cmd->pid != -1 && cmd->fd_in[0] != -2 && cmd->fd_out[0] != -2)
 | 
			
		||||
		{
 | 
			
		||||
			waitpid(cmd->pid, &exit_status, 0);
 | 
			
		||||
			if (WIFSIGNALED(exit_status))
 | 
			
		||||
			{
 | 
			
		||||
				if (WTERMSIG(exit_status) == SIGKILL)
 | 
			
		||||
					data->exit_code = 131;
 | 
			
		||||
				else
 | 
			
		||||
					data->exit_code = 130;
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
				data->exit_code = WEXITSTATUS(exit_status);
 | 
			
		||||
		}
 | 
			
		||||
		current = current->next;
 | 
			
		||||
	}
 | 
			
		||||
	data->child_pid = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_minishell(t_data *data, char *line)
 | 
			
		||||
{
 | 
			
		||||
	t_list	**cmds;
 | 
			
		||||
	char	*line_clean;
 | 
			
		||||
	int		infile;
 | 
			
		||||
	int		outfile;
 | 
			
		||||
 | 
			
		||||
	line_clean = ft_normalizer(line);
 | 
			
		||||
	if (line_clean == NULL)
 | 
			
		||||
		return (1);
 | 
			
		||||
	if (ft_syntatic_verif(line_clean))
 | 
			
		||||
	if (ft_syntax_verif(data, line))
 | 
			
		||||
		return (0);
 | 
			
		||||
	line_clean = ft_formater(data, line);
 | 
			
		||||
	if (line_clean == NULL || line_clean[0] == '\0')
 | 
			
		||||
		return (0);
 | 
			
		||||
	if (ft_cmds_parser(data, line_clean))
 | 
			
		||||
	{
 | 
			
		||||
		free(line_clean);
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	free(line_clean);
 | 
			
		||||
	if (ft_cmds_executor(data) == 1)
 | 
			
		||||
		return (1);
 | 
			
		||||
	ft_cmds_waiter(data);
 | 
			
		||||
	ft_lstclear(data->cmds, ft_cmddel);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_init_data(t_data *data, char **env)
 | 
			
		||||
{
 | 
			
		||||
	data->exit_code = 0;
 | 
			
		||||
	data->child_pid = 0;
 | 
			
		||||
	data->cmds = malloc(sizeof(t_cmd *));
 | 
			
		||||
	if (data->cmds == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	outfile = ft_outfile(line_clean);
 | 
			
		||||
	if (outfile == -2)
 | 
			
		||||
	*data->cmds = NULL;
 | 
			
		||||
	data->env = init_env(env);
 | 
			
		||||
	if (data->env == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		free(line_clean);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	infile = ft_infile(line_clean);
 | 
			
		||||
	if (infile == -2)
 | 
			
		||||
	{
 | 
			
		||||
		close(outfile);
 | 
			
		||||
		free(line_clean);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	cmds = ft_parse_cmds(line_clean, env, infile, outfile);
 | 
			
		||||
	if (cmds == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		close(outfile);
 | 
			
		||||
		close(infile);
 | 
			
		||||
		ft_lstclear(cmds, ft_cmddel);
 | 
			
		||||
		free(cmds);
 | 
			
		||||
		free(line_clean);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	if (ft_cmds_executor(cmds, env) == 1)
 | 
			
		||||
	{
 | 
			
		||||
		close(outfile);
 | 
			
		||||
		close(infile);
 | 
			
		||||
		ft_lstclear(cmds, ft_cmddel);
 | 
			
		||||
		free(cmds);
 | 
			
		||||
		free(line_clean);
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		free(data->cmds);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if DEBUG
 | 
			
		||||
 | 
			
		||||
int	main(int ac, char **av, char **env)
 | 
			
		||||
{
 | 
			
		||||
	t_data	data;
 | 
			
		||||
 | 
			
		||||
	if (ac == 1)
 | 
			
		||||
		return (1);
 | 
			
		||||
	data.env = init_env(env);
 | 
			
		||||
	if (data.env == NULL)
 | 
			
		||||
		return (1);
 | 
			
		||||
		free(data.env);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	if (ft_minishell(data.env, av[1]) == 1)
 | 
			
		||||
	{
 | 
			
		||||
		ft_lstclear(data.env, env_del);
 | 
			
		||||
		free(data.env);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	ft_lstclear(data.env, env_del);
 | 
			
		||||
	free(data.env);
 | 
			
		||||
	free(line);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#else
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int	main(int ac, char **av, char **env)
 | 
			
		||||
{
 | 
			
		||||
	t_data	data;
 | 
			
		||||
	t_data	*data;
 | 
			
		||||
	char	*line;
 | 
			
		||||
 | 
			
		||||
	data.env = init_env(env);
 | 
			
		||||
	if (data.env == NULL)
 | 
			
		||||
	(void) ac;
 | 
			
		||||
	(void) av;
 | 
			
		||||
	signal(SIGINT, ft_ctrlc);
 | 
			
		||||
	signal(SIGQUIT, ft_quit);
 | 
			
		||||
	data = ft_get_data();
 | 
			
		||||
	if (ft_init_data(data, env))
 | 
			
		||||
		return (1);
 | 
			
		||||
	line = ft_get_user_input(data.env);
 | 
			
		||||
	if (line == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_lstclear(data.env, env_del);
 | 
			
		||||
		free(data.env);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	line = ft_get_user_input();
 | 
			
		||||
	while (line != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		if (ft_minishell(data.env, line) == 1)
 | 
			
		||||
		{
 | 
			
		||||
			ft_lstclear(data.env, env_del);
 | 
			
		||||
			free(data.env);
 | 
			
		||||
		if (ft_minishell(data, line) == 1)
 | 
			
		||||
			break ;
 | 
			
		||||
		free(line);
 | 
			
		||||
			return (1);
 | 
			
		||||
		}
 | 
			
		||||
		free(line);
 | 
			
		||||
		line = ft_get_user_input(data.env);
 | 
			
		||||
		line = ft_get_user_input();
 | 
			
		||||
		if (line == NULL)
 | 
			
		||||
		{
 | 
			
		||||
			ft_lstclear(data.env, env_del);
 | 
			
		||||
			free(data.env);
 | 
			
		||||
			return (1);
 | 
			
		||||
			break ;
 | 
			
		||||
	}
 | 
			
		||||
	}
 | 
			
		||||
	ft_lstclear(data.env, env_del);
 | 
			
		||||
	free(data.env);
 | 
			
		||||
	ft_lstclear(data->cmds, ft_cmddel);
 | 
			
		||||
	free(data->cmds);
 | 
			
		||||
	ft_lstclear(data->env, env_del);
 | 
			
		||||
	free(data->env);
 | 
			
		||||
	return (data->exit_code);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										66
									
								
								minishell.h
									
									
									
									
									
								
							
							
						
						
									
										66
									
								
								minishell.h
									
									
									
									
									
								
							@ -6,62 +6,28 @@
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/14 13:45:30 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/17 14:29:56 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*   Updated: 2023/03/28 16:03:54 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef MINISHELL_H
 | 
			
		||||
# define MINISHELL_H
 | 
			
		||||
# include "libftx/libftx.h"
 | 
			
		||||
# include "utils/utils.h"
 | 
			
		||||
# include <sys/types.h>
 | 
			
		||||
# include <sys/stat.h>
 | 
			
		||||
# include <fcntl.h>
 | 
			
		||||
# include <sys/wait.h>
 | 
			
		||||
# include <stdio.h>
 | 
			
		||||
# include "./env/env.h"
 | 
			
		||||
# include "./cmd/cmd.h"
 | 
			
		||||
# include "./parse/parse.h"
 | 
			
		||||
# include "./syntax/syntax.h"
 | 
			
		||||
# include "./execution/execution.h"
 | 
			
		||||
# include "./builtins/builtins.h"
 | 
			
		||||
# include "./format/format.h"
 | 
			
		||||
# include "./redirection/redirection.h"
 | 
			
		||||
# include "./libftx/libftx.h"
 | 
			
		||||
# include "./utils/utils.h"
 | 
			
		||||
# include "./data/data.h"
 | 
			
		||||
# include "./signal/signal.h"
 | 
			
		||||
# include <readline/readline.h>
 | 
			
		||||
# include <readline/history.h>
 | 
			
		||||
# define DEBUG 0
 | 
			
		||||
t_list		**init_env(char **env);
 | 
			
		||||
int			set_value_by_key(char *key, char *value, t_list **env);
 | 
			
		||||
char		*get_value_by_key(char *key, t_list **head);
 | 
			
		||||
int			ft_syntatic_verif(const char *str);
 | 
			
		||||
int			ft_file_is_readable(const char *path);
 | 
			
		||||
int			ft_file_is_writable(const char *path);
 | 
			
		||||
int			ft_file_is_appendable(const char *path);
 | 
			
		||||
char		*ft_get_file_path(const char *infile);
 | 
			
		||||
int			ft_infile(char *line);
 | 
			
		||||
int			ft_outfile(char *line);
 | 
			
		||||
int			ft_heredoc(char *stop);
 | 
			
		||||
size_t		ft_seglen_quoted(const char *str, char c);
 | 
			
		||||
int			ft_cmds_executor(t_list **cmds, t_list **env);
 | 
			
		||||
char		**ft_split_quoted(const char *s, char c);
 | 
			
		||||
void		ft_cmddel(void *content);
 | 
			
		||||
void		env_del(void *content);
 | 
			
		||||
t_list		**ft_parse_cmds(char *line, t_list **env, int infile, int outfile);
 | 
			
		||||
int			ft_cmd_filler(t_list *current, char **args, t_list **env);
 | 
			
		||||
char		*ft_normalizer(char *str);
 | 
			
		||||
char		*ft_env_filler(t_list **env, const char *str);
 | 
			
		||||
char		**env_to_strs(t_list **head);
 | 
			
		||||
 | 
			
		||||
typedef struct s_cmd
 | 
			
		||||
{
 | 
			
		||||
	int		fd_in;
 | 
			
		||||
	int		fd_out;
 | 
			
		||||
	char	*executable;
 | 
			
		||||
	char	**args;
 | 
			
		||||
}	t_cmd;
 | 
			
		||||
 | 
			
		||||
typedef struct s_data
 | 
			
		||||
{
 | 
			
		||||
	t_list	**env;
 | 
			
		||||
	int		heredoc;
 | 
			
		||||
}	t_data;
 | 
			
		||||
 | 
			
		||||
typedef struct s_env
 | 
			
		||||
{
 | 
			
		||||
	void	*key;
 | 
			
		||||
	void	*value;
 | 
			
		||||
}	t_env;
 | 
			
		||||
# include <signal.h>
 | 
			
		||||
# include <sys/types.h>
 | 
			
		||||
# include <sys/wait.h>
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										112
									
								
								outfile.c
									
									
									
									
									
								
							
							
						
						
									
										112
									
								
								outfile.c
									
									
									
									
									
								
							@ -1,112 +0,0 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   outfile.c                                          :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/15 18:01:07 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/17 13:19:46 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "libftx/libftx.h"
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
 | 
			
		||||
static int	ft_outfile_is_valid(const char *line)
 | 
			
		||||
{
 | 
			
		||||
	char	**tab;
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	tab = ft_split_quoted(line, ' ');
 | 
			
		||||
	if (tab == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	if (tab[0] == NULL)
 | 
			
		||||
		return (1);
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (tab[i + 1] != NULL)
 | 
			
		||||
		i++;
 | 
			
		||||
	if (tab[i][0] == '>')
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]);
 | 
			
		||||
		ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_get_outfile(const char *line)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	int		fd;
 | 
			
		||||
	char	**tab;
 | 
			
		||||
 | 
			
		||||
	tab = ft_split_quoted(line, ' ');
 | 
			
		||||
	if (tab == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (-2);
 | 
			
		||||
	}
 | 
			
		||||
	fd = 1;
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (tab[i + 1] != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		if (tab[i][0] == '>')
 | 
			
		||||
			if (fd != 1)
 | 
			
		||||
				close(fd);
 | 
			
		||||
		if (ft_strcmp(">", tab[i]) == 0)
 | 
			
		||||
			fd = ft_file_is_writable(ft_quote_remover(tab[i + 1]));
 | 
			
		||||
		else if (ft_strcmp(">>", tab[i]) == 0)
 | 
			
		||||
			fd = ft_file_is_appendable(ft_quote_remover(tab[i + 1]));
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
	return (fd);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_remove_outfile(char *line)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	size_t	y;
 | 
			
		||||
	char	**tab;
 | 
			
		||||
 | 
			
		||||
	tab = ft_split_quoted(line, ' ');
 | 
			
		||||
	if (tab == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	i = 0;
 | 
			
		||||
	y = 0;
 | 
			
		||||
	while (tab[i] != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		if (tab[i][0] == '>')
 | 
			
		||||
		{
 | 
			
		||||
			ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
 | 
			
		||||
			i++;
 | 
			
		||||
			ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
			y = y + ft_strlen(tab[i]);
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_outfile(char *line)
 | 
			
		||||
{
 | 
			
		||||
	int	fd;
 | 
			
		||||
 | 
			
		||||
	if (ft_outfile_is_valid(line) == 0)
 | 
			
		||||
		return (-2);
 | 
			
		||||
	fd = ft_get_outfile(line);
 | 
			
		||||
	if (fd == -2)
 | 
			
		||||
		return (-2);
 | 
			
		||||
	ft_remove_outfile(line);
 | 
			
		||||
	return (fd);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										123
									
								
								parse/parse.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										123
									
								
								parse/parse.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,123 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   parse.c                                            :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:44:38 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/29 19:31:35 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./parse_private.h"
 | 
			
		||||
 | 
			
		||||
static int	ft_args_parse(char *cmd_str, t_cmd *cmd)
 | 
			
		||||
{
 | 
			
		||||
	char	**tab;
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	tab = ft_split_quoted(cmd_str, ' ');
 | 
			
		||||
	if (tab == NULL)
 | 
			
		||||
		return (1);
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (tab[i] != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_quote_remover(tab[i]);
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	cmd->args = tab;
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_executable_parse(t_data *data, t_cmd *cmd)
 | 
			
		||||
{
 | 
			
		||||
	bool	own;
 | 
			
		||||
	char	*path;
 | 
			
		||||
 | 
			
		||||
	path = cmd->args[0];
 | 
			
		||||
	own = 0;
 | 
			
		||||
	if (cmd->args[0] == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_closer(cmd->fd_in);
 | 
			
		||||
		ft_closer(cmd->fd_out);
 | 
			
		||||
	}
 | 
			
		||||
	else if (ft_strcmp(cmd->args[0], "env") == 0 || (ft_strcmp(cmd->args[0],
 | 
			
		||||
				"export") == 0) || (ft_strcmp(cmd->args[0], "echo") == 0)
 | 
			
		||||
		|| (ft_strcmp(cmd->args[0], "unset") == 0) || (ft_strcmp(cmd->args[0],
 | 
			
		||||
				"exit") == 0) || (ft_strcmp(cmd->args[0], "pwd") == 0)
 | 
			
		||||
		|| (ft_strcmp(cmd->args[0], "cd") == 0))
 | 
			
		||||
		own = 1;
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		path = ft_get_executable(data, cmd->args[0]);
 | 
			
		||||
		if (path == NULL)
 | 
			
		||||
			return (1);
 | 
			
		||||
	}
 | 
			
		||||
	cmd->own_cmd = own;
 | 
			
		||||
	cmd->executable = path;
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_cmd_adder(t_data *data, t_cmd *cmd)
 | 
			
		||||
{
 | 
			
		||||
	t_list	*element;
 | 
			
		||||
 | 
			
		||||
	element = ft_lstnew(cmd);
 | 
			
		||||
	if (element == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_cmddel(cmd);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	ft_lstadd_back(data->cmds, element);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_cmd_parser(t_data *data, char *cmd_str)
 | 
			
		||||
{
 | 
			
		||||
	t_cmd	*cmd;
 | 
			
		||||
 | 
			
		||||
	cmd = ft_calloc(sizeof(t_cmd), 1);
 | 
			
		||||
	if (cmd == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	ft_redirection(data, cmd, cmd_str);
 | 
			
		||||
	if (ft_args_parse(cmd_str, cmd))
 | 
			
		||||
	{
 | 
			
		||||
		ft_cmddel(cmd);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	ft_executable_parse(data, cmd);
 | 
			
		||||
	return (ft_cmd_adder(data, cmd));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_cmds_parser(t_data *data, const char *line)
 | 
			
		||||
{
 | 
			
		||||
	char	**tab;
 | 
			
		||||
	ssize_t	i;
 | 
			
		||||
 | 
			
		||||
	tab = ft_split_quoted(line, '|');
 | 
			
		||||
	if (tab == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	i = -1;
 | 
			
		||||
	while (tab[++i] != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		if (ft_cmd_parser(data, tab[i]))
 | 
			
		||||
		{
 | 
			
		||||
			ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
			return (1);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if (*data->cmds != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_add_fd(((t_cmd *)(*data->cmds)->content)->fd_in, 0);
 | 
			
		||||
		ft_add_fd(((t_cmd *)(ft_lstlast(*data->cmds))->content)->fd_out, 1);
 | 
			
		||||
	}
 | 
			
		||||
	ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								parse/parse.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								parse/parse.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   parse.h                                            :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/28 15:42:38 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/28 15:42:39 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef PARSE_H
 | 
			
		||||
# define PARSE_H
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
 | 
			
		||||
int	ft_cmds_parser(t_data *data, const char *line);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										21
									
								
								parse/parse_private.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								parse/parse_private.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   parse_private.h                                    :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/28 15:42:44 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/28 15:42:45 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef PARSE_PRIVATE_H
 | 
			
		||||
# define PARSE_PRIVATE_H
 | 
			
		||||
# include "../redirection/redirection.h"
 | 
			
		||||
# include "../libftx/libftx.h"
 | 
			
		||||
# include "../utils/utils.h"
 | 
			
		||||
# include "../env/env.h"
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
# include "../cmd/cmd.h"
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										85
									
								
								redirection/check.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								redirection/check.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,85 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   check.c                                            :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/29 17:32:06 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/29 19:21:59 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./redirection_private.h"
 | 
			
		||||
 | 
			
		||||
static bool	ft_check_heredoc(t_data *data, t_cmd *cmd,
 | 
			
		||||
				char *redirection_identifier, char *redirection)
 | 
			
		||||
{
 | 
			
		||||
	if (ft_strcmp(redirection_identifier, "<<") == 0)
 | 
			
		||||
	{
 | 
			
		||||
		cmd->fd_in[0] = ft_heredoc(data, redirection);
 | 
			
		||||
		if (cmd->fd_in[0] == -2)
 | 
			
		||||
			return (1);
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool	ft_check_infile(t_data *data, t_cmd *cmd,
 | 
			
		||||
				char *redirection_identifier, char *redirection)
 | 
			
		||||
{
 | 
			
		||||
	if (ft_strcmp(redirection_identifier, "<") == 0)
 | 
			
		||||
	{
 | 
			
		||||
		if (ft_file_is_readable(data, redirection))
 | 
			
		||||
			cmd->fd_in[0] = open(redirection, O_RDONLY);
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			cmd->fd_in[0] = -2;
 | 
			
		||||
			return (1);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool	ft_check_outfile(t_data *data, t_cmd *cmd,
 | 
			
		||||
				char *redirection_identifier, char *redirection)
 | 
			
		||||
{
 | 
			
		||||
	if (ft_strcmp(redirection_identifier, ">") == 0)
 | 
			
		||||
	{
 | 
			
		||||
		if (ft_file_is_writable(data, redirection))
 | 
			
		||||
			cmd->fd_out[0] = open(redirection,
 | 
			
		||||
					O_WRONLY | O_TRUNC | O_CREAT, 0644);
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			cmd->fd_out[0] = -2;
 | 
			
		||||
			return (1);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool	ft_check_outfile_append(t_data *data, t_cmd *cmd,
 | 
			
		||||
				char *redirection_identifier, char *redirection)
 | 
			
		||||
{
 | 
			
		||||
	if (ft_strcmp(redirection_identifier, ">>") == 0)
 | 
			
		||||
	{
 | 
			
		||||
		if (ft_file_is_writable(data, redirection))
 | 
			
		||||
			cmd->fd_out[0] = open(redirection,
 | 
			
		||||
					O_WRONLY | O_APPEND | O_CREAT, 0644);
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			cmd->fd_out[0] = -2;
 | 
			
		||||
			return (1);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool	ft_check_redirection(t_data *data, t_cmd *cmd,
 | 
			
		||||
				char *redirection_identifier, char *redirection)
 | 
			
		||||
{
 | 
			
		||||
	return (ft_check_heredoc(data, cmd, redirection_identifier, redirection)
 | 
			
		||||
		|| ft_check_infile(data, cmd, redirection_identifier, redirection)
 | 
			
		||||
		|| ft_check_outfile(data, cmd, redirection_identifier, redirection)
 | 
			
		||||
		|| ft_check_outfile_append(data, cmd, redirection_identifier,
 | 
			
		||||
			redirection));
 | 
			
		||||
}
 | 
			
		||||
@ -6,14 +6,13 @@
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/15 17:36:11 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/15 17:41:13 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*   Updated: 2023/03/30 13:26:31 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include "./redirection_private.h"
 | 
			
		||||
 | 
			
		||||
int	ft_file_is_readable(const char *path)
 | 
			
		||||
int	ft_file_is_readable(t_data *data, const char *path)
 | 
			
		||||
{
 | 
			
		||||
	int	readable;
 | 
			
		||||
	int	fd;
 | 
			
		||||
@ -21,59 +20,63 @@ int	ft_file_is_readable(const char *path)
 | 
			
		||||
	fd = open(path, O_RDONLY);
 | 
			
		||||
	if (fd == -1)
 | 
			
		||||
	{
 | 
			
		||||
		data->exit_code = 1;
 | 
			
		||||
		ft_eprintf("minishell: %s: No such file or directory\n", path);
 | 
			
		||||
		return (-1);
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	readable = read(fd, "", 0);
 | 
			
		||||
	if (readable == -1)
 | 
			
		||||
	{
 | 
			
		||||
		data->exit_code = 1;
 | 
			
		||||
		ft_eprintf("minishell: %s: Permission denied\n", path);
 | 
			
		||||
		return (-1);
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	return (fd);
 | 
			
		||||
	close(fd);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_file_is_writable(const char *path)
 | 
			
		||||
int	ft_file_is_writable(t_data *data, const char *path)
 | 
			
		||||
{
 | 
			
		||||
	int	writeable;
 | 
			
		||||
	int	fd;
 | 
			
		||||
 | 
			
		||||
	fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
 | 
			
		||||
	fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644);
 | 
			
		||||
	if (fd == -1)
 | 
			
		||||
	{
 | 
			
		||||
		data->exit_code = 1;
 | 
			
		||||
		ft_eprintf("minishell: %s: Permission denied\n", path);
 | 
			
		||||
		return (-1);
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	writeable = write(fd, "", 0);
 | 
			
		||||
	if (writeable == -1)
 | 
			
		||||
	{
 | 
			
		||||
		data->exit_code = 1;
 | 
			
		||||
		ft_eprintf("minishell: %s: Permission denied\n", path);
 | 
			
		||||
		return (-1);
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	return (fd);
 | 
			
		||||
	close(fd);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_file_is_appendable(const char *path)
 | 
			
		||||
int	ft_file_is_appendable(t_data *data, const char *path)
 | 
			
		||||
{
 | 
			
		||||
	int	writeable;
 | 
			
		||||
	int	fd;
 | 
			
		||||
 | 
			
		||||
	fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
 | 
			
		||||
	fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0644);
 | 
			
		||||
	if (fd == -1)
 | 
			
		||||
	{
 | 
			
		||||
		data->exit_code = 1;
 | 
			
		||||
		ft_eprintf("minishell: %s: Permission denied\n", path);
 | 
			
		||||
		return (-1);
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	writeable = write(fd, "", 0);
 | 
			
		||||
	if (writeable == -1)
 | 
			
		||||
	{
 | 
			
		||||
		data->exit_code = 1;
 | 
			
		||||
		ft_eprintf("minishell: %s: Permission denied\n", path);
 | 
			
		||||
		return (-1);
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	return (fd);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_file_is_executable(const char *path)
 | 
			
		||||
{
 | 
			
		||||
	return (access(path, X_OK) == 0);
 | 
			
		||||
	close(fd);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										81
									
								
								redirection/heredoc.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								redirection/heredoc.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,81 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   heredoc.c                                          :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/17 15:36:26 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/29 16:37:05 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./redirection_private.h"
 | 
			
		||||
 | 
			
		||||
int	*ft_get_heredoc(void)
 | 
			
		||||
{
 | 
			
		||||
	static int	heredoc = -1;
 | 
			
		||||
 | 
			
		||||
	return (&heredoc);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_format_and_write(t_data *data, const char *str, int fd)
 | 
			
		||||
{
 | 
			
		||||
	char	*line_clean;
 | 
			
		||||
 | 
			
		||||
	line_clean = ft_env_filler(data, str);
 | 
			
		||||
	if (line_clean == NULL)
 | 
			
		||||
		return (1);
 | 
			
		||||
	ft_putendl_fd(line_clean, fd);
 | 
			
		||||
	free(line_clean);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_heredoc2(t_data *data, char *stop, char *line, int fds[2])
 | 
			
		||||
{
 | 
			
		||||
	line[ft_strlen(line) - 1] = '\0';
 | 
			
		||||
	if (ft_strcmp(line, stop) == 0)
 | 
			
		||||
	{
 | 
			
		||||
		free(line);
 | 
			
		||||
		return (2);
 | 
			
		||||
	}
 | 
			
		||||
	if (ft_format_and_write(data, line, fds[1]))
 | 
			
		||||
	{
 | 
			
		||||
		ft_closer(fds);
 | 
			
		||||
		free(line);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	free(line);
 | 
			
		||||
	ft_printf("> ");
 | 
			
		||||
	line = get_next_line(*ft_get_heredoc());
 | 
			
		||||
	if (line == NULL && *ft_get_heredoc() == -1)
 | 
			
		||||
	{
 | 
			
		||||
		ft_closer(fds);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_heredoc(t_data *data, char *stop)
 | 
			
		||||
{
 | 
			
		||||
	char	*line;
 | 
			
		||||
	int		fds[2];
 | 
			
		||||
	int		return_code;
 | 
			
		||||
 | 
			
		||||
	if (pipe(fds) == -1)
 | 
			
		||||
		return (-1);
 | 
			
		||||
	*ft_get_heredoc() = dup(0);
 | 
			
		||||
	ft_printf("> ");
 | 
			
		||||
	line = get_next_line(*ft_get_heredoc());
 | 
			
		||||
	while (line != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		return_code = ft_heredoc2(data, stop, line, fds);
 | 
			
		||||
		if (return_code == 2)
 | 
			
		||||
			break ;
 | 
			
		||||
		else if (return_code)
 | 
			
		||||
			return (-2);
 | 
			
		||||
	}
 | 
			
		||||
	close(fds[1]);
 | 
			
		||||
	*ft_get_heredoc() = -1;
 | 
			
		||||
	return (fds[0]);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										111
									
								
								redirection/redirection.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								redirection/redirection.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,111 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   redirection.c                                      :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:44:22 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/29 19:19:51 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "redirection_private.h"
 | 
			
		||||
 | 
			
		||||
int	ft_replace_file(t_data *data, char **tab)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	char	*redirection;
 | 
			
		||||
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (tab[i] != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		if (ft_is_in("<>", tab[i][0]))
 | 
			
		||||
		{
 | 
			
		||||
			i++;
 | 
			
		||||
			redirection = ft_env_filler(data, tab[i]);
 | 
			
		||||
			if (redirection == NULL)
 | 
			
		||||
				return (1);
 | 
			
		||||
			free(tab[i]);
 | 
			
		||||
			tab[i] = redirection;
 | 
			
		||||
			ft_quote_remover(tab[i]);
 | 
			
		||||
		}
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void	ft_skip(char *str, size_t *i, ssize_t *start)
 | 
			
		||||
{
 | 
			
		||||
	while (str[*i] == str[*start])
 | 
			
		||||
		(*i)++;
 | 
			
		||||
	while (str[*i] == ' ')
 | 
			
		||||
		(*i)++;
 | 
			
		||||
	while (str[*i] != '\0' && (str[*i] != ' '
 | 
			
		||||
			|| ft_is_in_quote(str, *i)))
 | 
			
		||||
		(*i)++;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void	ft_remove_redirection(char *cmd_str)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	ssize_t	start;
 | 
			
		||||
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (cmd_str[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		start = -1;
 | 
			
		||||
		while ((ft_is_in_quote(cmd_str, i) || !ft_is_in("<>", cmd_str[i]))
 | 
			
		||||
			&& cmd_str[i] != '\0')
 | 
			
		||||
			i++;
 | 
			
		||||
		if (ft_is_in("<>", cmd_str[i]))
 | 
			
		||||
			start = i;
 | 
			
		||||
		else
 | 
			
		||||
			continue ;
 | 
			
		||||
		ft_skip(cmd_str, &i, &start);
 | 
			
		||||
		if (start != -1)
 | 
			
		||||
		{
 | 
			
		||||
			ft_strshift(cmd_str + start, -1 * (i - start));
 | 
			
		||||
			i = start;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_set_redirection(t_data *data, t_cmd *cmd, char **tab)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (tab[i + 1] != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_quote_remover(tab[i + 1]);
 | 
			
		||||
		if (ft_check_redirection(data, cmd, tab[i], tab[i + 1]))
 | 
			
		||||
			return (1);
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_redirection(t_data *data, t_cmd *cmd, char *cmd_str)
 | 
			
		||||
{
 | 
			
		||||
	char	**tab;
 | 
			
		||||
 | 
			
		||||
	cmd->fd_in[0] = -1;
 | 
			
		||||
	cmd->fd_in[1] = -1;
 | 
			
		||||
	cmd->fd_out[0] = -1;
 | 
			
		||||
	cmd->fd_out[1] = -1;
 | 
			
		||||
	tab = ft_split_quoted(cmd_str, ' ');
 | 
			
		||||
	if (tab == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	ft_remove_redirection(cmd_str);
 | 
			
		||||
	if (ft_set_redirection(data, cmd, tab))
 | 
			
		||||
	{
 | 
			
		||||
		ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	ft_freer_tab_ultimate(1, tab);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								redirection/redirection.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								redirection/redirection.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   redirection.h                                      :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:44:19 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/27 13:44:20 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef REDIRECTION_H
 | 
			
		||||
# define REDIRECTION_H
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
# include "../cmd/cmd.h"
 | 
			
		||||
 | 
			
		||||
int			ft_redirection(t_data *data, t_cmd *cmd, char *cmd_str);
 | 
			
		||||
int			*ft_get_heredoc(void);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										32
									
								
								redirection/redirection_private.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								redirection/redirection_private.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   redirection_private.h                              :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:44:31 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/29 19:19:21 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef REDIRECTION_PRIVATE_H
 | 
			
		||||
# define REDIRECTION_PRIVATE_H
 | 
			
		||||
# include <sys/stat.h>
 | 
			
		||||
# include <fcntl.h>
 | 
			
		||||
# include <stddef.h>
 | 
			
		||||
# include <unistd.h>
 | 
			
		||||
# include "../libftx/libftx.h"
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
# include "../env/env.h"
 | 
			
		||||
# include "../utils/utils.h"
 | 
			
		||||
# include "../cmd/cmd.h"
 | 
			
		||||
 | 
			
		||||
int			ft_file_is_readable(t_data *data, const char *path);
 | 
			
		||||
int			ft_file_is_writable(t_data *data, const char *path);
 | 
			
		||||
bool		ft_check_redirection(t_data *data, t_cmd *cmd,
 | 
			
		||||
				char *redirection_identifier, char *redirection);
 | 
			
		||||
int			ft_file_is_appendable(t_data *data, const char *path);
 | 
			
		||||
int			ft_heredoc(t_data *data, char *stop);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										65
									
								
								signal/signal.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								signal/signal.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,65 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   signal.c                                           :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/28 15:59:01 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/28 16:09:37 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "signal_private.h"
 | 
			
		||||
 | 
			
		||||
static void	ft_new_line(void)
 | 
			
		||||
{
 | 
			
		||||
	rl_replace_line("", 0);
 | 
			
		||||
	rl_on_new_line();
 | 
			
		||||
	ft_putchar_fd('\n', 1);
 | 
			
		||||
	rl_redisplay();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	ft_ctrlc(int num)
 | 
			
		||||
{
 | 
			
		||||
	t_data	*data;
 | 
			
		||||
 | 
			
		||||
	if (num == SIGQUIT)
 | 
			
		||||
		return ;
 | 
			
		||||
	data = ft_get_data();
 | 
			
		||||
	data->exit_code = 130;
 | 
			
		||||
	if (*ft_get_heredoc() != -1)
 | 
			
		||||
	{
 | 
			
		||||
		close(*ft_get_heredoc());
 | 
			
		||||
		*ft_get_heredoc() = -1;
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		if (data->child_pid > 1)
 | 
			
		||||
		{
 | 
			
		||||
			data->child_pid = 0;
 | 
			
		||||
			ft_putchar_fd('\n', 1);
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
			ft_new_line();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	ft_quit(int num)
 | 
			
		||||
{
 | 
			
		||||
	t_data	*data;
 | 
			
		||||
 | 
			
		||||
	(void) num;
 | 
			
		||||
	data = ft_get_data();
 | 
			
		||||
	data->exit_code = 131;
 | 
			
		||||
	if (data->child_pid > 1)
 | 
			
		||||
	{
 | 
			
		||||
		ft_printf("Quit (core dumped)\n");
 | 
			
		||||
		data->child_pid = 0;
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		rl_replace_line("", 0);
 | 
			
		||||
		rl_redisplay();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								signal/signal.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								signal/signal.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   signal.h                                           :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/28 16:02:31 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/28 16:03:11 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef SIGNAL_H
 | 
			
		||||
# define SIGNAL_H
 | 
			
		||||
 | 
			
		||||
void	ft_quit(int num);
 | 
			
		||||
void	ft_ctrlc(int num);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										21
									
								
								signal/signal_private.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								signal/signal_private.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   signal_private.h                                   :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/28 15:59:42 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/28 16:04:46 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef SIGNAL_PRIVATE_H
 | 
			
		||||
# define SIGNAL_PRIVATE_H
 | 
			
		||||
# include <signal.h>
 | 
			
		||||
# include <readline/readline.h>
 | 
			
		||||
# include <unistd.h>
 | 
			
		||||
# include "../libftx/libftx.h"
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
# include "../redirection/redirection.h"
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								subject.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								subject.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										98
									
								
								syntatics.c
									
									
									
									
									
								
							
							
						
						
									
										98
									
								
								syntatics.c
									
									
									
									
									
								
							@ -1,98 +0,0 @@
 | 
			
		||||
#include "libftx/libftx.h"
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
#include "utils/utils.h"
 | 
			
		||||
 | 
			
		||||
static int	ft_quote_verif(const char *str)
 | 
			
		||||
{
 | 
			
		||||
	if (ft_is_in_quote(str, ft_strlen(str)))
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: Quote is note closed");
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
		return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_pipe_is_alone(const char *str)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	int		check;
 | 
			
		||||
 | 
			
		||||
	check = 0;
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (str[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		while (str[i] == ' ')
 | 
			
		||||
			i++;
 | 
			
		||||
		if (str[i] == '\0')
 | 
			
		||||
			break ;
 | 
			
		||||
		if (str[i] != '|')
 | 
			
		||||
			check = 1;
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			if (check == 0)
 | 
			
		||||
			{
 | 
			
		||||
				ft_eprintf("minishell: Pipe must be followed and ");
 | 
			
		||||
				ft_eprintf("preced by a command or redirection\n");
 | 
			
		||||
				return (1);
 | 
			
		||||
			}
 | 
			
		||||
			check = 0;
 | 
			
		||||
		}
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	if (check == 0)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: Pipe must be followed and ");
 | 
			
		||||
		ft_eprintf("preced by a command or redirection\n");
 | 
			
		||||
	}
 | 
			
		||||
	return (check == 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_special_char_dub(const char *str)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	size_t	y;
 | 
			
		||||
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (str[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		while (ft_is_in_quote(str, i))
 | 
			
		||||
			i++;
 | 
			
		||||
		if (ft_is_in("|<>", str[i]))
 | 
			
		||||
		{
 | 
			
		||||
			y = 0;
 | 
			
		||||
			while (str[i] == str[i + y])
 | 
			
		||||
				y++;
 | 
			
		||||
			if ((y > 2 && (str[i] == '>' || str[i] == '<'))
 | 
			
		||||
				|| (y > 1 && str[i] == '|'))
 | 
			
		||||
			{
 | 
			
		||||
				ft_eprintf("minishell: to many %c in a row", str, str[i]);
 | 
			
		||||
				return (1);
 | 
			
		||||
			}
 | 
			
		||||
			i = i + y;
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
			i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_empty_verif(const char *str)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (str[i] == ' ')
 | 
			
		||||
		i++;
 | 
			
		||||
	if (str[i] == '\0')
 | 
			
		||||
		ft_eprintf("minishell: %s: command not found \n", str);
 | 
			
		||||
	return (str[i] == '\0');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_syntatic_verif(const char *str)
 | 
			
		||||
{
 | 
			
		||||
	return (ft_quote_verif(str)
 | 
			
		||||
		|| ft_empty_verif(str)
 | 
			
		||||
		|| ft_pipe_is_alone(str)
 | 
			
		||||
		|| ft_special_char_dub(str));
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										106
									
								
								syntax/syntax.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								syntax/syntax.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,106 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   syntax.c                                           :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/21 13:00:05 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/30 15:58:39 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "../libftx/libftx.h"
 | 
			
		||||
#include "../utils/utils.h"
 | 
			
		||||
 | 
			
		||||
static int	ft_quote_verif(const char *str)
 | 
			
		||||
{
 | 
			
		||||
	if (ft_is_in_quote(str, ft_strlen(str)))
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: Quote is not closed\n");
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
		return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_pipe_is_alone(const char *str)
 | 
			
		||||
{
 | 
			
		||||
	ssize_t	i;
 | 
			
		||||
	int		check;
 | 
			
		||||
 | 
			
		||||
	check = 0;
 | 
			
		||||
	i = -1;
 | 
			
		||||
	while (str[++i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		while (str[i] == ' ' || ft_is_in_quote(str, i))
 | 
			
		||||
			i++;
 | 
			
		||||
		if (str[i] == '\0')
 | 
			
		||||
			break ;
 | 
			
		||||
		if (str[i] != '|')
 | 
			
		||||
			check = 1;
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			if (check == 0)
 | 
			
		||||
				break ;
 | 
			
		||||
			check = 0;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if (check == 0)
 | 
			
		||||
		ft_eprintf("minishell: Pipe must be followed and %s",
 | 
			
		||||
			"preced by a command or redirection\n");
 | 
			
		||||
	return (check == 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_special_char_dub(const char *str)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	size_t	y;
 | 
			
		||||
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (str[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		while (ft_is_in_quote(str, i))
 | 
			
		||||
			i++;
 | 
			
		||||
		if (ft_is_in("|<>", str[i]))
 | 
			
		||||
		{
 | 
			
		||||
			y = 0;
 | 
			
		||||
			while (str[i] == str[i + y])
 | 
			
		||||
				y++;
 | 
			
		||||
			if ((y > 2 && (str[i] == '>' || str[i] == '<'))
 | 
			
		||||
				|| (y > 1 && str[i] == '|'))
 | 
			
		||||
			{
 | 
			
		||||
				ft_eprintf("minishell: too many %s in a row\n", str);
 | 
			
		||||
				return (1);
 | 
			
		||||
			}
 | 
			
		||||
			i = i + y;
 | 
			
		||||
		}
 | 
			
		||||
		else if (str[i] != '\0')
 | 
			
		||||
			i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_empty_verif(const char *str)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (str[i] == ' ')
 | 
			
		||||
		i++;
 | 
			
		||||
	return (str[i] == '\0');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_syntax_verif(t_data *data, const char *str)
 | 
			
		||||
{
 | 
			
		||||
	if (ft_empty_verif(str))
 | 
			
		||||
		return (1);
 | 
			
		||||
	if (ft_quote_verif(str)
 | 
			
		||||
		|| ft_pipe_is_alone(str)
 | 
			
		||||
		|| ft_special_char_dub(str))
 | 
			
		||||
	{
 | 
			
		||||
		data->exit_code = 2;
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								syntax/syntax.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								syntax/syntax.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   syntax.h                                           :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/28 15:42:54 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/28 15:42:55 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#ifndef SYNTAX_H
 | 
			
		||||
# define SYNTAX_H
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
 | 
			
		||||
int	ft_syntax_verif(t_data *data, const char *str);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										1
									
								
								tester
									
									
									
									
									
										Submodule
									
								
							
							
								
								
								
								
								
							
						
						
									
										1
									
								
								tester
									
									
									
									
									
										Submodule
									
								
							 Submodule tester added at 1c6111b2fd
									
								
							
							
								
								
									
										40
									
								
								utils/fd.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								utils/fd.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,40 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   fd.c                                               :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:39:58 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/29 18:51:58 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./utils.h"
 | 
			
		||||
#include <stdio.h> 
 | 
			
		||||
 | 
			
		||||
void	ft_closer(int fds[2])
 | 
			
		||||
{
 | 
			
		||||
	if (fds[0] > 2)
 | 
			
		||||
	{
 | 
			
		||||
		close(fds[0]);
 | 
			
		||||
	}
 | 
			
		||||
	if (fds[1] > 2)
 | 
			
		||||
	{
 | 
			
		||||
		close(fds[1]);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	ft_mega_closer(int fds1[2], int fds2[2])
 | 
			
		||||
{
 | 
			
		||||
	ft_closer(fds1);
 | 
			
		||||
	ft_closer(fds2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void	ft_add_fd(int fds[2], int fd)
 | 
			
		||||
{
 | 
			
		||||
	if (fds[0] == -1)
 | 
			
		||||
		fds[0] = fd;
 | 
			
		||||
	else
 | 
			
		||||
		fds[1] = fd;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										50
									
								
								utils/ft_atoi_check.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								utils/ft_atoi_check.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,50 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   ft_atoi_check.c                                    :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2022/07/21 08:21:05 by erey-bet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/28 13:52:13 by erey-bet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "utils.h"
 | 
			
		||||
 | 
			
		||||
static int	ft_isspace(char c)
 | 
			
		||||
{
 | 
			
		||||
	if (c == ' ' || c == '\f'
 | 
			
		||||
		||c == '\n' || c == '\r' || c == '\t' || c == '\v')
 | 
			
		||||
		return (1);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_atoi_check(const char *nptr)
 | 
			
		||||
{
 | 
			
		||||
	long	result;
 | 
			
		||||
	int		sign;
 | 
			
		||||
 | 
			
		||||
	while (ft_isspace(*nptr))
 | 
			
		||||
		nptr++;
 | 
			
		||||
	sign = 1;
 | 
			
		||||
	if (*nptr == '+' || *nptr == '-')
 | 
			
		||||
	{
 | 
			
		||||
		if (*nptr == '-')
 | 
			
		||||
			sign = -1;
 | 
			
		||||
		nptr++;
 | 
			
		||||
	}
 | 
			
		||||
	result = 0;
 | 
			
		||||
	while (*nptr >= '0' && *nptr <= '9')
 | 
			
		||||
	{
 | 
			
		||||
		result = result * 10 + *nptr++ - '0';
 | 
			
		||||
		if ((result > 2147483647 && sign == 1)
 | 
			
		||||
			|| (result > 2147483647 && sign == 1))
 | 
			
		||||
			return (2);
 | 
			
		||||
	}
 | 
			
		||||
	if (*nptr--)
 | 
			
		||||
		return (1);
 | 
			
		||||
	if (*nptr == '-' || *nptr == '+')
 | 
			
		||||
		return (1);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
@ -1,36 +1,27 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   heredoc.c                                          :+:      :+:    :+:   */
 | 
			
		||||
/*   ft_change_exit_code.c                              :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/17 15:36:26 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/17 16:21:02 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*   Created: 2023/02/24 19:04:04 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/24 19:05:57 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "libftx/libftx.h"
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
#include "../minishell.h"
 | 
			
		||||
 | 
			
		||||
int	ft_heredoc(char *stop)
 | 
			
		||||
int	ft_change_exit_code(t_data *data, int new_value)
 | 
			
		||||
{
 | 
			
		||||
	int		fds[2];
 | 
			
		||||
	char	*line;
 | 
			
		||||
	char	*exit_code_str;
 | 
			
		||||
 | 
			
		||||
	pipe(fds);
 | 
			
		||||
	line = readline("> ");
 | 
			
		||||
	while (line != NULL)
 | 
			
		||||
	data->exit_code = new_value;
 | 
			
		||||
	exit_code_str = ft_itoa(new_value);
 | 
			
		||||
	if (exit_code_str == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		if (ft_strcmp(line, stop) == 0)
 | 
			
		||||
		{
 | 
			
		||||
			free(line);
 | 
			
		||||
			break ;
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (1);
 | 
			
		||||
	}
 | 
			
		||||
		ft_putendl_fd(line, fds[1]);
 | 
			
		||||
		free(line);
 | 
			
		||||
		line = readline("> ");
 | 
			
		||||
	}
 | 
			
		||||
	close(fds[1]);
 | 
			
		||||
	return (fds[0]);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										115
									
								
								utils/ft_get_executable.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										115
									
								
								utils/ft_get_executable.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,115 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   ft_get_executable.c                                :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/27 13:39:48 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/30 13:04:53 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "./utils.h"
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
char	*ft_get_executable_with_path(t_data *data, const char *name)
 | 
			
		||||
{
 | 
			
		||||
	char	*path;
 | 
			
		||||
 | 
			
		||||
	if (access(name, F_OK) != 0)
 | 
			
		||||
	{
 | 
			
		||||
		data->exit_code = 127;
 | 
			
		||||
		ft_eprintf("minishell: %s: No such file or directery\n", name);
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	if (access(name, X_OK) != 0)
 | 
			
		||||
	{
 | 
			
		||||
		data->exit_code = 126;
 | 
			
		||||
		ft_eprintf("minishell: %s: permission denied\n", name);
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	path = ft_strdup(name);
 | 
			
		||||
	if (path == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	return (path);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char	**ft_get_paths(t_data *data, const char *name)
 | 
			
		||||
{
 | 
			
		||||
	char	*paths;
 | 
			
		||||
	char	**tab;
 | 
			
		||||
 | 
			
		||||
	paths = get_value_by_key("PATH", data->env);
 | 
			
		||||
	if (paths == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		data->exit_code = 127;
 | 
			
		||||
		ft_eprintf("minishell: %s: command not found\n", name);
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	tab = ft_split(paths, ':');
 | 
			
		||||
	if (tab == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	return (tab);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char	*ft_file_is_executable(const char *path, const char *name)
 | 
			
		||||
{
 | 
			
		||||
	char	*out;
 | 
			
		||||
 | 
			
		||||
	out = ft_strmerger(3, path, "/", name);
 | 
			
		||||
	if (out == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: malloc failed\n");
 | 
			
		||||
		free(out);
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	if (access(out, X_OK) == 0)
 | 
			
		||||
		return (out);
 | 
			
		||||
	free(out);
 | 
			
		||||
	return (NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char	*ft_get_executable_without_path(t_data *data, const char *name)
 | 
			
		||||
{
 | 
			
		||||
	char	**paths;
 | 
			
		||||
	char	*path;
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	paths = ft_get_paths(data, name);
 | 
			
		||||
	if (paths == NULL)
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	path = NULL;
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (paths[i] != NULL)
 | 
			
		||||
	{
 | 
			
		||||
		path = ft_file_is_executable(paths[i], name);
 | 
			
		||||
		if (path != NULL)
 | 
			
		||||
			break ;
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	ft_freer_tab_ultimate(1, paths);
 | 
			
		||||
	if (path == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		data->exit_code = 127;
 | 
			
		||||
		ft_eprintf("minishell: %s: command not found\n", name);
 | 
			
		||||
	}
 | 
			
		||||
	return (path);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	*ft_get_executable(t_data *data, const char *name)
 | 
			
		||||
{
 | 
			
		||||
	char	*path;
 | 
			
		||||
 | 
			
		||||
	if (name[0] == '.' || name[0] == '/')
 | 
			
		||||
		path = ft_get_executable_with_path(data, name);
 | 
			
		||||
	else
 | 
			
		||||
		path = ft_get_executable_without_path(data, name);
 | 
			
		||||
	return (path);
 | 
			
		||||
}
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							@ -1,29 +1,40 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   ft_is_in_quote.c                                   :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/03/30 15:39:25 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/03/30 15:52:10 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "utils.h"
 | 
			
		||||
 | 
			
		||||
int	ft_is_in_quote(const char *str, size_t n)
 | 
			
		||||
{
 | 
			
		||||
	size_t	double_quoted;
 | 
			
		||||
	size_t	simple_quoted;
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	ssize_t	start;
 | 
			
		||||
 | 
			
		||||
	double_quoted = 0;
 | 
			
		||||
	simple_quoted = 0;
 | 
			
		||||
	start = -1;
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (str[i] != '\0' && i < n)
 | 
			
		||||
	while (str[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		if (str[i] == '"')
 | 
			
		||||
		if (i == n)
 | 
			
		||||
			break ;
 | 
			
		||||
		if (str[i] == '\"' || str[i] == '\'')
 | 
			
		||||
		{
 | 
			
		||||
			if (simple_quoted == 0)
 | 
			
		||||
				double_quoted = !double_quoted;
 | 
			
		||||
			
 | 
			
		||||
		}
 | 
			
		||||
		if (str[i] == '\'')
 | 
			
		||||
		{
 | 
			
		||||
			if (double_quoted == 0)
 | 
			
		||||
				simple_quoted = !simple_quoted;
 | 
			
		||||
			
 | 
			
		||||
			if (start == -1)
 | 
			
		||||
				start = i;
 | 
			
		||||
			else if (str[i] == str[start])
 | 
			
		||||
				start = -1;
 | 
			
		||||
		}
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (simple_quoted == 1 + (double_quoted == 1) * 2);
 | 
			
		||||
	if (start == -1)
 | 
			
		||||
		return (0);
 | 
			
		||||
	if (str[start] == '\'')
 | 
			
		||||
		return (1);
 | 
			
		||||
	return (2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							@ -6,12 +6,20 @@
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/15 14:12:00 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/16 13:20:50 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*   Updated: 2023/03/29 15:11:44 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "utils.h"
 | 
			
		||||
 | 
			
		||||
static void	ft_remove_quote_and_reset(char *str, ssize_t *start, ssize_t *stop)
 | 
			
		||||
{
 | 
			
		||||
	ft_strshift(str + *start, -1);
 | 
			
		||||
	ft_strshift(str + *stop - 1, -1);
 | 
			
		||||
	*start = -1;
 | 
			
		||||
	*stop = -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	*ft_quote_remover(char *str)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
@ -19,6 +27,7 @@ char	*ft_quote_remover(char *str)
 | 
			
		||||
	ssize_t	stop;
 | 
			
		||||
 | 
			
		||||
	start = -1;
 | 
			
		||||
	stop = -1;
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (str[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
@ -27,17 +36,15 @@ char	*ft_quote_remover(char *str)
 | 
			
		||||
			if (start == -1)
 | 
			
		||||
				start = i;
 | 
			
		||||
			else if (str[i] == str[start])
 | 
			
		||||
			{
 | 
			
		||||
				stop = i;
 | 
			
		||||
				break ;
 | 
			
		||||
		}
 | 
			
		||||
		}
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	if (start != -1)
 | 
			
		||||
		if (stop != -1)
 | 
			
		||||
		{
 | 
			
		||||
		ft_strshift(str, -1);
 | 
			
		||||
		ft_strshift(str + stop - 1, -1);
 | 
			
		||||
			ft_remove_quote_and_reset(str, &start, &stop);
 | 
			
		||||
			i = i - 1;
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
			i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (str);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,15 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   ft_strnchr.c                                       :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/21 12:59:06 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/21 12:59:07 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "utils.h"
 | 
			
		||||
 | 
			
		||||
ssize_t	ft_strnchr(const char *str, char c)
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,15 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   ft_strncpy.c                                       :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/21 12:59:16 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/21 12:59:19 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "utils.h"
 | 
			
		||||
 | 
			
		||||
size_t	ft_strncpy(char *dst, const char *src, size_t n)
 | 
			
		||||
 | 
			
		||||
@ -1,11 +1,25 @@
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/*                                                        :::      ::::::::   */
 | 
			
		||||
/*   ft_strreplace.c                                    :+:      :+:    :+:   */
 | 
			
		||||
/*                                                    +:+ +:+         +:+     */
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/21 12:46:20 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/21 12:58:44 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
#include "utils.h"
 | 
			
		||||
 | 
			
		||||
char	*ft_strreplace(const char *str, const char *fill, size_t start, size_t stop)
 | 
			
		||||
char	*ft_strreplace(const char *str, const char *fill,
 | 
			
		||||
		size_t start, size_t stop)
 | 
			
		||||
{
 | 
			
		||||
	char	*out;
 | 
			
		||||
	size_t	sum;
 | 
			
		||||
 | 
			
		||||
	out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1 * sizeof(char)));
 | 
			
		||||
	out = malloc((ft_strlen(str) + ft_strlen(fill) -\
 | 
			
		||||
				(stop - start) + 1 * sizeof(char)));
 | 
			
		||||
	if (out == NULL)
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	ft_strncpy(out, str, start);
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										68
									
								
								utils/tags
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								utils/tags
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,68 @@
 | 
			
		||||
!_TAG_EXTRA_DESCRIPTION	anonymous	/Include tags for non-named objects like lambda/
 | 
			
		||||
!_TAG_EXTRA_DESCRIPTION	fileScope	/Include tags of file scope/
 | 
			
		||||
!_TAG_EXTRA_DESCRIPTION	pseudo	/Include pseudo tags/
 | 
			
		||||
!_TAG_EXTRA_DESCRIPTION	subparser	/Include tags generated by subparsers/
 | 
			
		||||
!_TAG_FIELD_DESCRIPTION	epoch	/the last modified time of the input file (only for F\/file kind tag)/
 | 
			
		||||
!_TAG_FIELD_DESCRIPTION	file	/File-restricted scoping/
 | 
			
		||||
!_TAG_FIELD_DESCRIPTION	input	/input file/
 | 
			
		||||
!_TAG_FIELD_DESCRIPTION	name	/tag name/
 | 
			
		||||
!_TAG_FIELD_DESCRIPTION	pattern	/pattern/
 | 
			
		||||
!_TAG_FIELD_DESCRIPTION	typeref	/Type and name of a variable or typedef/
 | 
			
		||||
!_TAG_FIELD_DESCRIPTION!C++	name	/aliased names/
 | 
			
		||||
!_TAG_FILE_FORMAT	2	/extended format; --format=1 will not append ;" to lines/
 | 
			
		||||
!_TAG_FILE_SORTED	1	/0=unsorted, 1=sorted, 2=foldcase/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C	d,macro	/macro definitions/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C	e,enumerator	/enumerators (values inside an enumeration)/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C	f,function	/function definitions/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C	g,enum	/enumeration names/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C	h,header	/included header files/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C	m,member	/struct, and union members/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C	s,struct	/structure names/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C	t,typedef	/typedefs/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C	u,union	/union names/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C	v,variable	/variable definitions/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C++	c,class	/classes/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C++	d,macro	/macro definitions/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C++	e,enumerator	/enumerators (values inside an enumeration)/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C++	f,function	/function definitions/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C++	g,enum	/enumeration names/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C++	h,header	/included header files/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C++	m,member	/class, struct, and union members/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C++	n,namespace	/namespaces/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C++	s,struct	/structure names/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C++	t,typedef	/typedefs/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C++	u,union	/union names/
 | 
			
		||||
!_TAG_KIND_DESCRIPTION!C++	v,variable	/variable definitions/
 | 
			
		||||
!_TAG_OUTPUT_EXCMD	mixed	/number, pattern, mixed, or combineV2/
 | 
			
		||||
!_TAG_OUTPUT_FILESEP	slash	/slash or backslash/
 | 
			
		||||
!_TAG_OUTPUT_MODE	u-ctags	/u-ctags or e-ctags/
 | 
			
		||||
!_TAG_OUTPUT_VERSION	0.0	/current.age/
 | 
			
		||||
!_TAG_PARSER_VERSION!C	0.0	/current.age/
 | 
			
		||||
!_TAG_PARSER_VERSION!C++	0.0	/current.age/
 | 
			
		||||
!_TAG_PATTERN_LENGTH_LIMIT	96	/0 for no limit/
 | 
			
		||||
!_TAG_PROC_CWD	/nfs/homes/cchauvet/42/minishell/utils/	//
 | 
			
		||||
!_TAG_PROGRAM_AUTHOR	Universal Ctags Team	//
 | 
			
		||||
!_TAG_PROGRAM_NAME	Universal Ctags	/Derived from Exuberant Ctags/
 | 
			
		||||
!_TAG_PROGRAM_URL	https://ctags.io/	/official site/
 | 
			
		||||
!_TAG_PROGRAM_VERSION	6.0.0	//
 | 
			
		||||
!_TAG_ROLE_DESCRIPTION!C!header	local	/local header/
 | 
			
		||||
!_TAG_ROLE_DESCRIPTION!C!header	system	/system header/
 | 
			
		||||
!_TAG_ROLE_DESCRIPTION!C!macro	undef	/undefined/
 | 
			
		||||
!_TAG_ROLE_DESCRIPTION!C++!header	local	/local header/
 | 
			
		||||
!_TAG_ROLE_DESCRIPTION!C++!header	system	/system header/
 | 
			
		||||
!_TAG_ROLE_DESCRIPTION!C++!macro	undef	/undefined/
 | 
			
		||||
UTILS_H	utils.h	/^# define UTILS_H$/;"	d
 | 
			
		||||
ft_atoi_check	ft_atoi_check.c	/^int	ft_atoi_check(const char *nptr)$/;"	f	typeref:typename:int
 | 
			
		||||
ft_change_exit_code	ft_change_exit_code.c	/^int	ft_change_exit_code(t_data *data, int new_value)$/;"	f	typeref:typename:int
 | 
			
		||||
ft_is_in_quote	ft_is_in_quote.c	/^int	ft_is_in_quote(const char *str, size_t n)$/;"	f	typeref:typename:int
 | 
			
		||||
ft_isspace	ft_atoi_check.c	/^static int	ft_isspace(char c)$/;"	f	typeref:typename:int	file:
 | 
			
		||||
ft_printn	ft_printn.c	/^void	ft_printn(const char *str, size_t n)$/;"	f	typeref:typename:void
 | 
			
		||||
ft_quote_remover	ft_quote_remover.c	/^char	*ft_quote_remover(char *str)$/;"	f	typeref:typename:char *
 | 
			
		||||
ft_seglen_quoted	ft_split_quoted.c	/^size_t	ft_seglen_quoted(const char *str, char c)$/;"	f	typeref:typename:size_t
 | 
			
		||||
ft_segsplitter	ft_split_quoted.c	/^static char	**ft_segsplitter(char **tab, size_t len, const char *s, char c)$/;"	f	typeref:typename:char **	file:
 | 
			
		||||
ft_split_quoted	ft_split_quoted.c	/^char	**ft_split_quoted(const char *s, char c)$/;"	f	typeref:typename:char **
 | 
			
		||||
ft_str_is_empty	ft_str_is_empty.c	/^int	ft_str_is_empty(const char *str)$/;"	f	typeref:typename:int
 | 
			
		||||
ft_strnchr	ft_strnchr.c	/^ssize_t	ft_strnchr(const char *str, char c)$/;"	f	typeref:typename:ssize_t
 | 
			
		||||
ft_strncpy	ft_strncpy.c	/^size_t	ft_strncpy(char *dst, const char *src, size_t n)$/;"	f	typeref:typename:size_t
 | 
			
		||||
ft_strreplace	ft_strreplace.c	/^char	*ft_strreplace(const char *str, const char *fill,$/;"	f	typeref:typename:char *
 | 
			
		||||
ft_strshift	ft_strshift.c	/^void	ft_strshift(char *str, int shift)$/;"	f	typeref:typename:void
 | 
			
		||||
@ -6,7 +6,7 @@
 | 
			
		||||
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | 
			
		||||
/*                                                +#+#+#+#+#+   +#+           */
 | 
			
		||||
/*   Created: 2023/02/14 14:46:40 by cchauvet          #+#    #+#             */
 | 
			
		||||
/*   Updated: 2023/02/16 16:13:47 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*   Updated: 2023/03/30 15:38:56 by cchauvet         ###   ########.fr       */
 | 
			
		||||
/*                                                                            */
 | 
			
		||||
/* ************************************************************************** */
 | 
			
		||||
 | 
			
		||||
@ -14,6 +14,8 @@
 | 
			
		||||
# define UTILS_H
 | 
			
		||||
# include <stdlib.h>
 | 
			
		||||
# include "../libftx/libftx.h"
 | 
			
		||||
# include "../data/data.h"
 | 
			
		||||
# include "../env/env.h"
 | 
			
		||||
 | 
			
		||||
size_t		ft_strncpy(char *dst, const char *src, size_t n);
 | 
			
		||||
int			ft_is_in_quote(const char *str, size_t n);
 | 
			
		||||
@ -25,5 +27,10 @@ int			ft_str_is_empty(const char *str);
 | 
			
		||||
char		**ft_split_quoted(const char *s, char c);
 | 
			
		||||
void		ft_strshift(char *str, int shift);
 | 
			
		||||
char		*ft_quote_remover(char *str);
 | 
			
		||||
int			ft_atoi_check(const char *nptr);
 | 
			
		||||
char		*ft_get_executable(t_data *data, const char *name);
 | 
			
		||||
void		ft_closer(int fds[2]);
 | 
			
		||||
void		ft_mega_closer(int fds1[2], int fds2[2]);
 | 
			
		||||
void		ft_add_fd(int fds[2], int fd);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user