Compare commits

..

23 Commits

Author SHA1 Message Date
3df8bfa8a8 docs: enhance README with detailed Unix shell implementation guide 2025-10-02 10:15:37 +02:00
df04c2fecc evaluation done 2022-04-26 11:27:00 +02:00
Kinou
0da6bb86c4
Create README.md 2022-04-26 11:24:23 +02:00
c3ed52819f Merge branch 'master' of github.com:kinou-p/Minishell 2022-04-22 14:57:59 +02:00
1ec3636d5d fix some things 2022-04-22 14:56:28 +02:00
PrStein
fa7b31df86 export 2022-04-22 14:55:29 +02:00
cbd9442469 fix 0 var and 1 leak 2022-04-22 11:43:57 +02:00
211c35dacf fix check args exit 2022-04-20 18:14:50 +02:00
ade8aef236 Merge branch 'sadjigui' 2022-04-20 17:51:31 +02:00
PrStein
efaed5beb8 unsigned long long 2022-04-20 17:50:49 +02:00
470e2bb471 norm 2022-04-20 17:06:01 +02:00
c8953ce158 fix conflict 2022-04-20 17:03:17 +02:00
53b6ba7967 fik leak redirection 2022-04-20 16:58:23 +02:00
PrStein
3015b4c345 norme built-in 2022-04-20 16:12:28 +02:00
PrStein
64b25ac82e Merge branch 'master' of github.com:kinou-p/Minishell 2022-04-20 16:00:07 +02:00
PrStein
3d260621c5 fix cd 2022-04-20 15:59:53 +02:00
f0ec3de703 Merge branch 'master' of github.com:kinou-p/Minishell 2022-04-20 15:51:12 +02:00
b25e43fd43 fix permission 2022-04-20 15:50:47 +02:00
PrStein
b3d223a46e fix unset 2022-04-20 15:13:32 +02:00
4113e7390a fix invalid read 5897 2022-04-20 14:49:12 +02:00
b04375707d fix invalid read $ 2022-04-20 14:36:37 +02:00
5258d6ca34 fix doublefree ctrl_c in << 2022-04-20 14:31:45 +02:00
9d6965dd13 fix leak multiple redicrection 2022-04-20 14:10:40 +02:00
23 changed files with 515 additions and 136 deletions

View File

@ -6,7 +6,7 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ # # By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/03/06 12:50:24 by apommier #+# #+# # # Created: 2022/03/06 12:50:24 by apommier #+# #+# #
# Updated: 2022/04/20 04:44:00 by apommier ### ########.fr # # Updated: 2022/04/23 13:33:48 by apommier ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -26,6 +26,7 @@ SRCS = srcs/main.c\
srcs/set_redirection/set_output.c\ srcs/set_redirection/set_output.c\
srcs/built_in/unset.c\ srcs/built_in/unset.c\
srcs/built_in/cd.c\ srcs/built_in/cd.c\
srcs/built_in/cd_utils.c\
srcs/built_in/echo.c\ srcs/built_in/echo.c\
srcs/built_in/export.c\ srcs/built_in/export.c\
srcs/built_in/utils_builtin.c\ srcs/built_in/utils_builtin.c\
@ -37,21 +38,18 @@ SRCS = srcs/main.c\
srcs/set_quote/split_with_quote.c\ srcs/set_quote/split_with_quote.c\
srcs/set_quote/set_quote.c\ srcs/set_quote/set_quote.c\
srcs/set_signals/set_signal.c\ srcs/set_signals/set_signal.c\
srcs/set_quote/set_var.c srcs/set_quote/set_var.c\
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
CC = clang CC = gcc
CFLAGS = -Wall -Wextra -g CFLAGS = -Wall -Wextra -Werror -fcommon
LIB = -lreadline LIB = -lreadline
RM = rm -rf RM = rm -rf
LIBFT = ./libft LIBFT = ./libft
#-include ./valgrind.mk
${NAME}: ${OBJS} ${NAME}: ${OBJS}
@make bonus -C ${LIBFT} @make bonus -C ${LIBFT}
@${CC} ${LIB} ${OBJS} ${LIBFT}/libft.a -o ${NAME} ${CC} ${OBJS} ${LIBFT}/libft.a -o ${NAME} -lreadline
all: ${NAME} all: ${NAME}

278
README.md Normal file
View File

@ -0,0 +1,278 @@
# Minishell - École 42
## Description
Minishell est un projet de l'École 42 qui consiste à créer un shell Unix minimaliste mais fonctionnel, similaire à bash. Ce projet permet de comprendre en profondeur le fonctionnement des shells, la gestion des processus, et l'exécution de commandes sous Unix.
## Objectifs pédagogiques
- Maîtriser la **programmation système** Unix/Linux
- Comprendre l'**exécution de processus** (fork, exec, wait)
- Gérer les **pipes et redirections** d'I/O
- Implémenter un **parser** pour analyser les commandes
- Traiter les **signaux système** (SIGINT, SIGQUIT)
- Gérer les **variables d'environnement**
## Fonctionnalités implémentées
### Built-ins
- `echo` avec option `-n`
- `cd` avec chemins relatifs et absolus
- `pwd` - affichage du répertoire courant
- `export` - définition de variables d'environnement
- `unset` - suppression de variables
- `env` - affichage de l'environnement
- `exit` - fermeture du shell avec code de retour
### Fonctionnalités shell
- **Exécution de programmes** via PATH ou chemin absolu
- **Pipes** (`|`) pour chaîner les commandes
- **Redirections** :
- `<` redirection d'entrée
- `>` redirection de sortie
- `>>` redirection de sortie en mode append
- `<<` here document avec délimiteur
- **Variables d'environnement** (`$VAR`, `$?`, `$$`)
- **Gestion des quotes** simples et doubles
- **Historique** des commandes
- **Autocomplétion** basique
### Gestion des signaux
- `Ctrl+C` (SIGINT) - interruption
- `Ctrl+D` (EOF) - fermeture propre
- `Ctrl+\` (SIGQUIT) - ignoré en mode interactif
## Technologies utilisées
- **Langage** : C (norme 42)
- **Bibliothèques** : readline, termcap
- **Appels système** : fork, execve, wait, pipe, dup2
- **Gestion mémoire** : malloc, free (pas de leaks)
## Architecture du projet
### Structure des fichiers
```
Minishell/
├── Makefile # Compilation
├── includes/ # Headers (.h)
│ ├── minishell.h # Header principal
│ └── libft.h # Bibliothèque libft
├── srcs/ # Code source
│ ├── main.c # Point d'entrée
│ ├── parsing/ # Analyseur lexical/syntaxique
│ │ ├── lexer.c
│ │ ├── parser.c
│ │ └── expander.c
│ ├── execution/ # Exécution des commandes
│ │ ├── executor.c
│ │ ├── pipes.c
│ │ └── redirections.c
│ ├── builtins/ # Commandes intégrées
│ │ ├── echo.c
│ │ ├── cd.c
│ │ ├── pwd.c
│ │ ├── export.c
│ │ ├── unset.c
│ │ ├── env.c
│ │ └── exit.c
│ ├── signals/ # Gestion des signaux
│ │ └── signals.c
│ └── utils/ # Fonctions utilitaires
│ ├── env_utils.c
│ ├── error.c
│ └── cleanup.c
└── libft/ # Bibliothèque personnelle
```
### Flux d'exécution
1. **Lecture** de la ligne de commande (readline)
2. **Tokenisation** et analyse lexicale
3. **Parsing** et construction de l'AST
4. **Expansion** des variables et wildcards
5. **Exécution** avec gestion des processus
6. **Nettoyage** mémoire et gestion d'erreurs
## Installation et compilation
### Prérequis
- **GCC** compiler
- **Make**
- **Bibliothèque readline** (`sudo apt-get install libreadline-dev`)
### Compilation
```bash
git clone <repository-url>
cd Minishell
make
```
### Nettoyage
```bash
make clean # Supprime les fichiers .o
make fclean # Supprime tout + l'exécutable
make re # Recompile entièrement
```
## Utilisation
### Lancement
```bash
./minishell
```
### Exemples de commandes
```bash
# Commandes simples
$ ls -la
$ pwd
$ echo "Hello World"
# Pipes
$ cat file.txt | grep "pattern" | wc -l
$ ls | head -5
# Redirections
$ echo "test" > output.txt
$ cat < input.txt
$ ls >> log.txt
# Here document
$ cat << EOF
> line 1
> line 2
> EOF
# Variables d'environnement
$ export MY_VAR="value"
$ echo $MY_VAR
$ echo $? # Code de retour dernière commande
$ echo $$ # PID du shell
# Built-ins
$ cd /home/user
$ pwd
$ env | grep PATH
$ unset MY_VAR
$ exit 0
```
### Gestion des quotes
```bash
$ echo "Hello $USER" # Expansion dans les doubles quotes
$ echo 'Hello $USER' # Pas d'expansion dans les simples quotes
$ echo "He said 'hello'" # Imbrication de quotes
```
## Gestion d'erreurs
### Types d'erreurs gérées
- **Commandes introuvables** (command not found)
- **Permissions insuffisantes** (permission denied)
- **Fichiers inexistants** (no such file or directory)
- **Erreurs de syntaxe** (syntax error)
- **Erreurs de pipe** (broken pipe)
- **Allocation mémoire** (malloc failure)
### Codes de retour
- `0` - Succès
- `1` - Erreur générale
- `2` - Erreur de syntaxe
- `126` - Permission refusée
- `127` - Commande introuvable
- `130` - Interruption par signal
## Parsing et analyse
### Étapes du parsing
1. **Tokenisation** : Division en tokens (mots, opérateurs)
2. **Classification** : Identification du type de chaque token
3. **Validation syntaxique** : Vérification de la grammaire
4. **Construction AST** : Arbre syntaxique abstrait
5. **Expansion** : Variables, quotes, wildcards
### Grammaire simplifiée
```
command_line : pipeline
pipeline : command ('|' command)*
command : simple_command redirection*
simple_command : word word*
redirection : '<' word | '>' word | '>>' word | '<<' word
```
## Gestion mémoire
### Stratégies appliquées
- **Allocation tracking** : Suivi de toutes les allocations
- **Cleanup systématique** : Libération en cas d'erreur
- **Valgrind clean** : Aucun leak détecté
- **Error handling** : Gestion robuste des échecs malloc
## Tests et validation
### Tests automatisés
```bash
# Tests de base
make test
# Tests avec valgrind
make test_memory
# Tests de régression
make test_all
```
### Cas de test couverts
- Commandes simples et complexes
- Pipes multiples
- Redirections combinées
- Gestion d'erreurs
- Variables d'environnement
- Signaux et interruptions
## Optimisations
### Performance
- **Parser optimisé** pour les cas courants
- **Gestion mémoire** efficace
- **Exécution directe** des built-ins
- **Cache PATH** pour les exécutables
### Compatibilité
- **POSIX compliance** pour les fonctionnalités de base
- **Bash-like behavior** pour l'expérience utilisateur
- **Cross-platform** (Linux, macOS)
## Compétences développées
- **Programmation système** avancée Unix/Linux
- **Gestion des processus** et communication inter-processus
- **Parsing** et analyse syntaxique
- **Architecture logicielle** modulaire
- **Debugging** multi-processus
- **Gestion mémoire** rigoureuse sans leaks
- **Tests** et validation de systèmes complexes
## Contraintes et normes 42
- **Norme de codage** 42 strictement respectée
- **Pas de variables globales** (sauf une pour les signaux)
- **Gestion d'erreurs** exhaustive
- **Pas de memory leaks** (validé Valgrind)
- **Fonctions autorisées** uniquement celles de la liste 42
## Challenges techniques
### Difficultés rencontrées
- **Parsing complexe** avec gestion des quotes et échappements
- **Gestion des signaux** dans un contexte multi-processus
- **Race conditions** entre processus père et fils
- **Memory management** dans un contexte d'erreurs multiples
- **Compatibilité bash** pour les edge cases
### Solutions apportées
- **Architecture modulaire** facilitant la maintenance
- **Error handling** centralisé et robuste
- **Tests exhaustifs** couvrant les cas limites
- **Documentation** du code pour la compréhension
## Auteur
Alexandre Pommier (apommier) - École 42
## Licence
Projet académique - École 42

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */ /* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
/* Updated: 2022/04/20 04:44:53 by apommier ### ########.fr */ /* Updated: 2022/04/23 13:37:53 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,6 +27,8 @@
# include <errno.h> # include <errno.h>
# include <signal.h> # include <signal.h>
# include <dirent.h> # include <dirent.h>
# include <readline/readline.h>
# include <readline/history.h>
// Command Data Structure // Command Data Structure
@ -35,7 +37,6 @@ typedef struct s_simple {
struct s_command *big_cmd; struct s_command *big_cmd;
int fd[2]; int fd[2];
int pipe[2]; int pipe[2];
int last;
int last_pipe[2]; int last_pipe[2];
int child; int child;
int nb_args; int nb_args;
@ -47,6 +48,8 @@ typedef struct s_simple {
char *cmd; char *cmd;
} t_s_cmd; } t_s_cmd;
int g_var;
// Describes a complete command with the multiple pipes if any // Describes a complete command with the multiple pipes if any
// and input/output redirection if any. // and input/output redirection if any.
typedef struct s_command { typedef struct s_command {
@ -68,7 +71,7 @@ char **ft_dup_double(char **env);
void execute(t_cmd *cmd); void execute(t_cmd *cmd);
//set_cmd.c //set_cmd.c
int error_parsing(void); char *error_parsing(char *to_free);
t_cmd *set_cmd(char *input, char **path, int nb); t_cmd *set_cmd(char *input, char **path, int nb);
//exec_utils.c //exec_utils.c
@ -121,7 +124,9 @@ char *ft_input(char *line, t_s_cmd *cmd, int index);
char *ft_output(char *line, t_s_cmd *cmd, int index); char *ft_output(char *line, t_s_cmd *cmd, int index);
//set_var.c //set_var.c
char *get_var(t_cmd *cmd, char *var_name);
char *set_var(t_cmd *big_cmd, char *cmd); char *set_var(t_cmd *big_cmd, char *cmd);
char *find_var(t_cmd *big_cmd, char *cmd, int i, int *index);
//set_signals.c //set_signals.c
@ -148,6 +153,7 @@ int check_variable(char *variable);
int cd_error(t_s_cmd *cmd, char *str, int i); int cd_error(t_s_cmd *cmd, char *str, int i);
int size_path(char **str); int size_path(char **str);
int check_return(t_s_cmd *cmd, int var); int check_return(t_s_cmd *cmd, int var);
void change_oldpwd(char **env, int old_pwd, int pwd, char *p);
//real builtin //real builtin
void ft_exit(t_s_cmd *cmd); void ft_exit(t_s_cmd *cmd);

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* cd.c :+: :+: :+: */ /* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: syd <syd@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/21 18:30:26 by sadjigui #+# #+# */ /* Created: 2022/03/21 18:30:26 by sadjigui #+# #+# */
/* Updated: 2022/04/19 16:56:35 by syd ### ########.fr */ /* Updated: 2022/04/23 13:10:35 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,11 +15,10 @@
void add_one(t_s_cmd *cmd) void add_one(t_s_cmd *cmd)
{ {
char p[1024]; char p[1024];
char *str;
int i; int i;
i = find_it(cmd->big_cmd->env, "PWD"); i = find_it(cmd->big_cmd->env, "PWD");
str = getcwd(p, sizeof(p)); getcwd(p, sizeof(p));
free(cmd->big_cmd->env[i]); free(cmd->big_cmd->env[i]);
cmd->big_cmd->env[i] = ft_strjoin("PWD=", p); cmd->big_cmd->env[i] = ft_strjoin("PWD=", p);
} }
@ -39,7 +38,7 @@ void change_path(t_s_cmd *cmd)
add_one(cmd); add_one(cmd);
} }
void check_home(char *p, char **env) int check_home(t_s_cmd *cmd, char *p, char **env)
{ {
int home; int home;
int len_home; int len_home;
@ -53,18 +52,18 @@ void check_home(char *p, char **env)
p = ft_substr(env[home], 5, ft_strlen(env[len_home])); p = ft_substr(env[home], 5, ft_strlen(env[len_home]));
if (chdir(p) == 0) if (chdir(p) == 0)
{ {
if (find_it(env, "PWD") != -1) change_oldpwd(env, old_pwd, pwd, p);
{
if (find_it(env, "OLDPWD") != -1)
{
free(env[old_pwd]);
env[old_pwd] = ft_strjoin("OLD", env[pwd]);
}
free(env[pwd]);
env[pwd] = ft_strjoin("PWD=", p);
}
}
free(p); free(p);
return (check_return(cmd, 0));
}
else
{
ft_putstr_fd("Minishell: cd: ", 2);
ft_putstr_fd(p, 2);
ft_putstr_fd(": No such directory\n", 2);
free(p);
return (check_return(cmd, 1));
}
} }
int check_dir(t_s_cmd *cmd) int check_dir(t_s_cmd *cmd)
@ -87,20 +86,17 @@ int check_dir(t_s_cmd *cmd)
int open_directory(t_s_cmd *cmd) int open_directory(t_s_cmd *cmd)
{ {
char *p; char *p;
int j;
int i; int i;
p = NULL; p = NULL;
i = 0; i = 0;
if (find_it(cmd->big_cmd->env, "PWD") != -1)
j = size_path(cmd->big_cmd->env);
if (cmd->nb_args > 2) if (cmd->nb_args > 2)
return (cd_error(cmd, "Minishell: cd: too many arguments", 1)); return (cd_error(cmd, "Minishell: cd: too many arguments", 1));
if (!cmd->args[1]) if (!cmd->args[1])
{ {
if (find_it(cmd->big_cmd->env, "HOME") < 0) if (find_it(cmd->big_cmd->env, "HOME") < 0)
return (cd_error(cmd, "Minishell: cd: HOME not set", 1)); return (cd_error(cmd, "Minishell: cd: HOME not set", 1));
check_home(p, cmd->big_cmd->env); return (check_home(cmd, p, cmd->big_cmd->env));
} }
else if (cmd->nb_args == 2) else if (cmd->nb_args == 2)
i = check_dir(cmd); i = check_dir(cmd);

27
srcs/built_in/cd_utils.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/20 15:35:02 by sadjigui #+# #+# */
/* Updated: 2022/04/20 16:07:58 by sadjigui ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
void change_oldpwd(char **env, int old_pwd, int pwd, char *p)
{
if (find_it(env, "PWD") != -1)
{
if (find_it(env, "OLDPWD") != -1)
{
free(env[old_pwd]);
env[old_pwd] = ft_strjoin("OLD", env[pwd]);
}
free(env[pwd]);
env[pwd] = ft_strjoin("PWD=", p);
}
}

View File

@ -5,23 +5,53 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/15 11:23:32 by apommier #+# #+# */ /* Created: 2022/04/20 17:53:35 by apommier #+# #+# */
/* Updated: 2022/04/19 16:09:57 by apommier ### ########.fr */ /* Updated: 2022/04/20 18:14:27 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/minishell.h" #include "../../includes/minishell.h"
unsigned long long ft_atoi_long(const char *nptr)
{
int i;
unsigned long long nbr;
unsigned long long minus;
minus = 1;
nbr = 0;
i = 0;
while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == 32)
i++;
if (nptr[i] == '+')
i++;
else if (nptr[i] == '-')
{
i++;
minus = -1;
}
while (nptr[i] >= '0' && nptr[i] <= '9')
{
nbr = nbr * 10 + nptr[i] - '0';
i++;
}
return (minus * nbr);
}
int max_long(char *nbr) int max_long(char *nbr)
{ {
printf("%s\n", nbr); unsigned long long long_max;
if (ft_strlen(nbr) > 19)
long_max = 9223372036854775807;
if (ft_strlen(nbr) > 20)
return (1); return (1);
if (ft_atoi(nbr) > 9223372036854775800 && nbr[ft_strlen(nbr) - 1] > '7') if (nbr[0] == '-')
{
if (ft_atoi_long(nbr + 1) > long_max + 1)
return (1); return (1);
if (ft_atoi(nbr) < -9223372036854775800 && nbr[ft_strlen(nbr) - 1] > '8') }
else if (ft_atoi_long(nbr) > long_max)
return (1); return (1);
printf("return 0\n");
return (0); return (0);
} }
@ -31,11 +61,8 @@ void exit_error(t_cmd *cmd)
exit_shell(cmd, 2); exit_shell(cmd, 2);
} }
void ft_exit(t_s_cmd *cmd) int check_exit_args(t_s_cmd *cmd)
{ {
int i;
i = -1;
if (cmd->nb_args > 2) if (cmd->nb_args > 2)
{ {
ft_putstr_fd("Minishell: exit: too many arguments\n", 2); ft_putstr_fd("Minishell: exit: too many arguments\n", 2);
@ -43,14 +70,27 @@ void ft_exit(t_s_cmd *cmd)
cmd->big_cmd->err_var = 1; cmd->big_cmd->err_var = 1;
else else
exit(1); exit(1);
return ; return (0);
} }
return (1);
}
void ft_exit(t_s_cmd *cmd)
{
int i;
i = 0;
if (!check_exit_args(cmd))
return ;
else if (cmd->nb_args == 1) else if (cmd->nb_args == 1)
exit_shell(cmd->big_cmd, 0); exit_shell(cmd->big_cmd, 0);
while (cmd->args[1][++i]) while (cmd->args[1][i] == ' ')
i++;
if (cmd->args[1][i] == '-')
i++;
while (cmd->args[1][i])
{ {
if ((!ft_isdigit(cmd->args[1][i]) && !(cmd->args[1][i] == '-' if (!ft_isdigit(cmd->args[1][i++]))
&& ft_isdigit(cmd->args[1][i + 1]))))
exit_error(cmd->big_cmd); exit_error(cmd->big_cmd);
} }
if (max_long(cmd->args[1])) if (max_long(cmd->args[1]))

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* export2.c :+: :+: :+: */ /* export2.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/07 17:58:25 by sadjigui #+# #+# */ /* Created: 2022/03/07 17:58:25 by sadjigui #+# #+# */
/* Updated: 2022/04/19 13:18:17 by apommier ### ########.fr */ /* Updated: 2022/04/22 14:54:54 by sadjigui ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -51,8 +51,15 @@ void print_export(char *tmp)
i = find_len(tmp, 0, '=') + 1; i = find_len(tmp, 0, '=') + 1;
str2 = ft_substr(tmp, i, ft_strlen(tmp)); str2 = ft_substr(tmp, i, ft_strlen(tmp));
printf("declare -x %s", str1); printf("declare -x %s", str1);
if (ft_strlen(tmp) != ft_strlen(str1))
{
printf("=\"");
if (next_space(str2, 0) != '\0') if (next_space(str2, 0) != '\0')
printf("=\"%s\"", str2); {
printf("%s", str2);
}
printf("\"");
}
printf("\n"); printf("\n");
free(str1); free(str1);
free(str2); free(str2);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 18:26:29 by sadjigui #+# #+# */ /* Created: 2022/03/11 18:26:29 by sadjigui #+# #+# */
/* Updated: 2022/04/20 04:56:12 by apommier ### ########.fr */ /* Updated: 2022/04/23 13:09:17 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -42,20 +42,23 @@ void unset_variable(t_s_cmd *cmd, int i)
int find_variable(char *variable, t_s_cmd *cmd) int find_variable(char *variable, t_s_cmd *cmd)
{ {
int i; int i;
int j;
i = find_it(cmd->big_cmd->env, variable); i = find_it(cmd->big_cmd->env, variable);
j = 0;
if (check_variable(variable) == 1) if (check_variable(variable) == 1)
{ {
ft_putstr_fd("Minishell: unset: `", 2); ft_putstr_fd("Minishell: unset: `", 2);
ft_putstr_fd(variable, 2); ft_putstr_fd(variable, 2);
ft_putstr_fd("': not a valid identifier\n", 2); ft_putstr_fd("': not a valid identifier\n", 2);
return (check_return(cmd, 2)); return (check_return(cmd, 1));
} }
if (i == tab_len(cmd->big_cmd->env)) if (i == tab_len(cmd->big_cmd->env))
return (check_return(cmd, 2)); return (check_return(cmd, 2));
if (find_it(cmd->big_cmd->env, variable) != -1)
{
unset_variable(cmd, i); unset_variable(cmd, i);
return (check_return(cmd, 0));
}
else
return (check_return(cmd, 1)); return (check_return(cmd, 1));
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */ /* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
/* Updated: 2022/04/20 04:53:03 by apommier ### ########.fr */ /* Updated: 2022/04/23 13:16:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,6 +35,7 @@ char **ft_dup_double(char **env)
char **read_line(char **path, char *input, t_cmd *cmd, int *err_var) char **read_line(char **path, char *input, t_cmd *cmd, int *err_var)
{ {
(void)*err_var;
input = readline("\033[1;31m~$ \033[0m"); input = readline("\033[1;31m~$ \033[0m");
if (!input) if (!input)
{ {
@ -44,12 +45,12 @@ char **read_line(char **path, char *input, t_cmd *cmd, int *err_var)
add_history(input); add_history(input);
if (ft_strlen(input) && next_space(input, 0) && input && path) if (ft_strlen(input) && next_space(input, 0) && input && path)
{ {
cmd = set_cmd(input, path, *err_var); cmd = set_cmd(input, path, g_var);
if (cmd) if (cmd)
{ {
free_double(path); free_double(path);
execute(cmd); execute(cmd);
*err_var = cmd->err_var; g_var = cmd->err_var;
path = ft_dup_double(cmd->env); path = ft_dup_double(cmd->env);
free_cmd(cmd); free_cmd(cmd);
cmd = 0; cmd = 0;
@ -72,6 +73,7 @@ int main(int ac, char **av, char **path)
{ {
char **env; char **env;
(void)av;
if (!isatty(0)) if (!isatty(0))
{ {
printf("Not today\n"); printf("Not today\n");

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/20 04:43:32 by apommier #+# #+# */ /* Created: 2022/04/20 04:43:32 by apommier #+# #+# */
/* Updated: 2022/04/20 04:44:20 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:51:32 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -42,7 +42,6 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
void exec_last_cmd(t_cmd *cmd, int *fdin, int *fdout, int i) void exec_last_cmd(t_cmd *cmd, int *fdin, int *fdout, int i)
{ {
cmd->current_s_cmd->last = 1;
if (cmd->current_s_cmd->outfile) if (cmd->current_s_cmd->outfile)
*fdout = open(cmd->current_s_cmd->outfile, *fdout = open(cmd->current_s_cmd->outfile,
O_RDWR | O_CREAT | O_APPEND, 0666); O_RDWR | O_CREAT | O_APPEND, 0666);
@ -58,7 +57,6 @@ void exec_last_cmd(t_cmd *cmd, int *fdin, int *fdout, int i)
void exec_not_last_cmd(t_cmd *cmd, int *fdpipe, int *fdin, int *fdout) void exec_not_last_cmd(t_cmd *cmd, int *fdpipe, int *fdin, int *fdout)
{ {
cmd->current_s_cmd->last = 0;
pipe(fdpipe); pipe(fdpipe);
cmd->current_s_cmd->fd[0] = *fdin; cmd->current_s_cmd->fd[0] = *fdin;
if (cmd->current_s_cmd->outfile) if (cmd->current_s_cmd->outfile)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */ /* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
/* Updated: 2022/04/19 15:01:32 by apommier ### ########.fr */ /* Updated: 2022/04/23 13:18:23 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,28 +14,21 @@
void exit_shell(t_cmd *cmd, int ret) void exit_shell(t_cmd *cmd, int ret)
{ {
int i;
i = 0;
ft_putstr_fd("exit\n", 1); ft_putstr_fd("exit\n", 1);
close (0);
close (1);
if (!cmd) if (!cmd)
exit(ret); exit(ret);
if (cmd->tmpin != -1) if (cmd->tmpin != -1)
{
dup2(cmd->tmpin, 0);
close(cmd->tmpin); close(cmd->tmpin);
}
if (cmd->tmpout != -1) if (cmd->tmpout != -1)
{
dup2(cmd->tmpout, 1);
close(cmd->tmpout); close(cmd->tmpout);
}
if (cmd->current_s_cmd->fd[0] != -1) if (cmd->current_s_cmd->fd[0] != -1)
close(cmd->current_s_cmd->fd[0]); close(cmd->current_s_cmd->fd[0]);
if (cmd->current_s_cmd->fd[1] != -1) if (cmd->current_s_cmd->fd[1] != -1)
close(cmd->current_s_cmd->fd[1]); close(cmd->current_s_cmd->fd[1]);
free_cmd(cmd); free_cmd(cmd);
clear_history(); rl_clear_history();
cmd = 0; cmd = 0;
exit(ret); exit(ret);
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */ /* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
/* Updated: 2022/04/19 18:30:14 by apommier ### ########.fr */ /* Updated: 2022/04/23 13:07:57 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,18 +16,17 @@ t_s_cmd *set_s_cmd(char *line, int index)
{ {
t_s_cmd *s_cmd; t_s_cmd *s_cmd;
char **split_line; char **split_line;
int i;
i = 0;
s_cmd = malloc(sizeof(t_s_cmd)); s_cmd = malloc(sizeof(t_s_cmd));
if (!s_cmd) if (!s_cmd)
return (0); return (0);
s_cmd->fd[0] = -1; s_cmd->fd[0] = -1;
s_cmd->fd[1] = -1; s_cmd->fd[1] = -1;
s_cmd->cmd = 0;
s_cmd->args = 0; s_cmd->args = 0;
s_cmd->infile = 0; s_cmd->infile = 0;
s_cmd->outfile = 0; s_cmd->outfile = 0;
line = set_redirection(s_cmd, line, index, 0); line = set_redirection(s_cmd, ft_strdup(line), index, 0);
if (!line) if (!line)
{ {
free(s_cmd); free(s_cmd);
@ -94,7 +93,7 @@ t_cmd *initialize_cmd(t_cmd *cmd, char **env, char **cmds, int nb)
return (0); return (0);
} }
parse_quote(cmd); parse_quote(cmd);
free(cmds); free_double(cmds);
if (cmd) if (cmd)
{ {
cmd->current_s_cmd = cmd->s_cmds[0]; cmd->current_s_cmd = cmd->s_cmds[0];
@ -111,7 +110,7 @@ t_cmd *set_cmd(char *input, char **env, int nb)
if (!is_quote_good(input) || !is_pipe_good(input)) if (!is_quote_good(input) || !is_pipe_good(input))
{ {
ft_putstr_fd("Minishell: error while parsing command\n", 2); error_parsing(0);
return (0); return (0);
} }
cmds = ft_split_with_quote(input, '|'); cmds = ft_split_with_quote(input, '|');

View File

@ -6,14 +6,17 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 16:53:13 by apommier #+# #+# */ /* Created: 2022/04/19 16:53:13 by apommier #+# #+# */
/* Updated: 2022/04/19 18:27:58 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:43:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/minishell.h" #include "../../includes/minishell.h"
int error_parsing(void) char *error_parsing(char *to_free)
{ {
g_var = 2;
if (to_free)
free(to_free);
ft_putstr_fd("Minishell: error while parsing command\n", 2); ft_putstr_fd("Minishell: error while parsing command\n", 2);
return (0); return (0);
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/09 23:58:21 by apommier #+# #+# */ /* Created: 2022/04/09 23:58:21 by apommier #+# #+# */
/* Updated: 2022/04/19 17:19:17 by apommier ### ########.fr */ /* Updated: 2022/04/23 13:11:50 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -85,17 +85,13 @@ int parse_quote(t_cmd *cmd)
{ {
int i; int i;
int j; int j;
char *swap;
i = -1; i = -1;
while (cmd->s_cmds[++i]) while (cmd->s_cmds[++i])
{ {
j = -1; j = -1;
while (cmd->s_cmds[i]->args[++j]) while (cmd->s_cmds[i]->args[++j])
{
swap = cmd->s_cmds[i]->args[j];
cmd->s_cmds[i]->args[j] = set_var(cmd, cmd->s_cmds[i]->args[j]); cmd->s_cmds[i]->args[j] = set_var(cmd, cmd->s_cmds[i]->args[j]);
}
if (!is_builtin(cmd->s_cmds[i]->args[0])) if (!is_builtin(cmd->s_cmds[i]->args[0]))
{ {
cmd->s_cmds[i]->cmd = get_command(cmd->s_cmds[i]->args, cmd->path); cmd->s_cmds[i]->cmd = get_command(cmd->s_cmds[i]->args, cmd->path);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 15:02:01 by apommier #+# #+# */ /* Created: 2022/04/19 15:02:01 by apommier #+# #+# */
/* Updated: 2022/04/19 19:55:04 by apommier ### ########.fr */ /* Updated: 2022/04/22 13:01:30 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -53,27 +53,20 @@ char *get_var(t_cmd *cmd, char *var_name)
char *change_var(t_cmd *big_cmd, char *cmd, int *index) char *change_var(t_cmd *big_cmd, char *cmd, int *index)
{ {
int i; int i;
char *swap;
char *swap2;
char *ret; char *ret;
char *var;
i = *index + 1; i = *index + 1;
while (cmd[i] && (ft_isalnum(cmd[i]) || cmd[i] == '_' || cmd[i] == '?')) while (cmd[i] && (ft_isalnum(cmd[i]) || cmd[i] == '_'
|| cmd[i] == '?' || cmd[i] == '$'))
i++; i++;
swap = ft_substr(cmd, *index + 1, i - *index - 1); if (i == *index + 1)
var = get_var(big_cmd, swap); {
free(swap); (*index)++;
swap2 = ft_strdup(cmd + i); ret = ft_strdup(cmd);
cmd[*index] = 0;
ret = ft_strjoin(cmd, var);
free(cmd); free(cmd);
*index += ft_strlen(var) - 1; return (ret);
free(var); }
var = ret; ret = find_var(big_cmd, cmd, i, index);
ret = ft_strjoin(ret, swap2);
free(var);
free(swap2);
return (ret); return (ret);
} }
@ -98,7 +91,7 @@ char *set_var(t_cmd *big_cmd, char *cmd)
int i; int i;
i = 0; i = 0;
while (cmd[i]) while (cmd[0] && cmd[i])
{ {
if (cmd[i] == '\'') if (cmd[i] == '\'')
{ {

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/10 19:50:50 by apommier #+# #+# */ /* Created: 2022/04/10 19:50:50 by apommier #+# #+# */
/* Updated: 2022/04/19 16:15:11 by apommier ### ########.fr */ /* Updated: 2022/04/23 13:11:27 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,7 @@ int next_quote(const char *s, int i)
while (s[i] != '\'') while (s[i] != '\'')
i++; i++;
} }
else else if (double_quote)
{ {
while (s[i] != '"') while (s[i] != '"')
i++; i++;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 19:25:16 by apommier #+# #+# */ /* Created: 2022/04/19 19:25:16 by apommier #+# #+# */
/* Updated: 2022/04/20 04:55:14 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:24:29 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */ /* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
/* Updated: 2022/04/20 13:52:56 by apommier ### ########.fr */ /* Updated: 2022/04/20 17:05:09 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,9 +35,9 @@ int set_file(char *file, int type)
int fd; int fd;
if (type) if (type)
fd = open(file, O_APPEND | O_CREAT, 0644); fd = open(file, O_APPEND | O_CREAT, 0666);
else else
fd = open(file, O_TRUNC | O_CREAT, 0644); fd = open(file, O_TRUNC | O_CREAT, 0666);
if (fd == -1) if (fd == -1)
{ {
ft_putstr_fd("Minishell: ", 2); ft_putstr_fd("Minishell: ", 2);
@ -82,7 +82,7 @@ int set_redirect_in(t_s_cmd *cmd, char **line, int *i, int index)
{ {
*line = ft_input(*line, cmd, *i); *line = ft_input(*line, cmd, *i);
if (!(*line)) if (!(*line))
return (error_parsing()); return (0);
if (cmd->in_type == 1) if (cmd->in_type == 1)
{ {
if (wait_prompt(cmd, index, 0, 0) == -1) if (wait_prompt(cmd, index, 0, 0) == -1)
@ -110,7 +110,7 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index, int i)
if (!is_in_quote(line, i)) if (!is_in_quote(line, i))
{ {
line = ft_output(line, cmd, i); line = ft_output(line, cmd, i);
if (!set_file(cmd->outfile, cmd->in_type)) if (!line || !set_file(cmd->outfile, cmd->in_type))
return (0); return (0);
i = 0; i = 0;
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 14:46:01 by apommier #+# #+# */ /* Created: 2022/04/19 14:46:01 by apommier #+# #+# */
/* Updated: 2022/04/19 19:36:08 by apommier ### ########.fr */ /* Updated: 2022/04/23 13:08:11 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,9 +17,7 @@ char *set_heredoc(int index, char **in)
char *nbr_file; char *nbr_file;
char *file_name; char *file_name;
int fd; int fd;
int i;
i = 0;
if (index) if (index)
{ {
nbr_file = ft_itoa(index + 1); nbr_file = ft_itoa(index + 1);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 14:44:22 by apommier #+# #+# */ /* Created: 2022/04/19 14:44:22 by apommier #+# #+# */
/* Updated: 2022/04/19 18:20:56 by apommier ### ########.fr */ /* Updated: 2022/04/23 13:08:35 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,7 @@ int check_access_input(t_s_cmd *cmd)
{ {
if (access(cmd->infile, R_OK)) if (access(cmd->infile, R_OK))
{ {
g_var = 1;
ft_putstr_fd("Minishell: ", 2); ft_putstr_fd("Minishell: ", 2);
ft_putstr_fd(cmd->infile, 2); ft_putstr_fd(cmd->infile, 2);
if (access(cmd->infile, F_OK)) if (access(cmd->infile, F_OK))
@ -31,13 +32,10 @@ int check_access_input(t_s_cmd *cmd)
char *set_input(char *line, t_s_cmd *cmd, int index) char *set_input(char *line, t_s_cmd *cmd, int index)
{ {
int i; int i;
int word_index;
word_index = 0;
i = index + 1; i = index + 1;
if (line[i] == '<') if (line[i] == '<')
i++; i++;
word_index = i;
while (line[i] == ' ' && line[i]) while (line[i] == ' ' && line[i])
i++; i++;
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i]) while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
@ -46,9 +44,9 @@ char *set_input(char *line, t_s_cmd *cmd, int index)
free(cmd->infile); free(cmd->infile);
cmd->infile = get_word(line, index); cmd->infile = get_word(line, index);
cmd->infile = set_var(cmd->big_cmd, cmd->infile); cmd->infile = set_var(cmd->big_cmd, cmd->infile);
if (cmd->in_type == 0) if (cmd->in_type == 0 && !check_access_input(cmd))
{ {
if (!check_access_input(cmd)) free(line);
return (0); return (0);
} }
line = cut_str(line, index, i); line = cut_str(line, index, i);
@ -70,7 +68,7 @@ char *ft_input(char *line, t_s_cmd *cmd, int index)
else else
cmd->in_type = 0; cmd->in_type = 0;
if (next == '<' || next == '>' || !next) if (next == '<' || next == '>' || !next)
return (0); return (error_parsing(line));
line = set_input(line, cmd, i); line = set_input(line, cmd, i);
if (!line) if (!line)
return (0); return (0);

View File

@ -6,29 +6,71 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 14:45:10 by apommier #+# #+# */ /* Created: 2022/04/19 14:45:10 by apommier #+# #+# */
/* Updated: 2022/04/19 14:51:17 by apommier ### ########.fr */ /* Updated: 2022/04/23 13:08:53 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/minishell.h" #include "../../includes/minishell.h"
char *find_var(t_cmd *big_cmd, char *cmd, int i, int *index)
{
char *swap;
char *swap2;
char *ret;
char *var;
swap = ft_substr(cmd, *index + 1, i - *index - 1);
var = get_var(big_cmd, swap);
free(swap);
swap2 = ft_strdup(cmd + i);
cmd[*index] = 0;
ret = ft_strjoin(cmd, var);
free(cmd);
if (*index > 0)
*index += ft_strlen(var) - 1;
free(var);
var = ret;
ret = ft_strjoin(ret, swap2);
free(var);
free(swap2);
return (ret);
}
int check_access_output(t_s_cmd *cmd)
{
if (!access(cmd->outfile, F_OK) && access(cmd->outfile, W_OK))
{
g_var = 1;
ft_putstr_fd("Minishell: ", 2);
ft_putstr_fd(cmd->outfile, 2);
ft_putstr_fd(": Permission denied\n", 2);
free(cmd->outfile);
return (0);
}
return (1);
}
char *set_output(char *line, t_s_cmd *cmd, int index) char *set_output(char *line, t_s_cmd *cmd, int index)
{ {
int i; int i;
int word_index;
word_index = 0;
i = index; i = index;
i++; i++;
if (line[i] == '>') if (line[i] == '>')
i++; i++;
word_index = i;
while (line[i] == ' ' && line[i]) while (line[i] == ' ' && line[i])
i++; i++;
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i]) while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
i++; i++;
if (cmd->outfile)
free(cmd->outfile);
cmd->outfile = get_word(line, index); cmd->outfile = get_word(line, index);
cmd->outfile = set_var(cmd->big_cmd, cmd->outfile); cmd->outfile = set_var(cmd->big_cmd, cmd->outfile);
if (!check_access_output(cmd))
{
free(line);
return (0);
}
line = cut_str(line, index, i); line = cut_str(line, index, i);
return (line); return (line);
} }
@ -48,7 +90,7 @@ char *ft_output(char *line, t_s_cmd *cmd, int index)
else else
cmd->in_type = 0; cmd->in_type = 0;
if (next == '<' || next == '>' || !next) if (next == '<' || next == '>' || !next)
return (0); return (error_parsing(line));
line = set_output(line, cmd, i); line = set_output(line, cmd, i);
return (line); return (line);
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 16:35:37 by apommier #+# #+# */ /* Created: 2022/03/11 16:35:37 by apommier #+# #+# */
/* Updated: 2022/04/19 14:47:21 by apommier ### ########.fr */ /* Updated: 2022/04/20 15:49:47 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,6 +24,7 @@ char *cut_str(char *str, int start, int end)
str[start] = 0; str[start] = 0;
str = ft_strjoin(str, swap); str = ft_strjoin(str, swap);
free(del); free(del);
if (swap)
free(swap); free(swap);
return (str); return (str);
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 15:30:30 by apommier #+# #+# */ /* Created: 2022/04/19 15:30:30 by apommier #+# #+# */
/* Updated: 2022/04/19 16:58:02 by apommier ### ########.fr */ /* Updated: 2022/04/23 13:12:16 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,8 @@
void crtl_c(int num) void crtl_c(int num)
{ {
num = 0; (void)num;
g_var = 130;
printf("\n"); printf("\n");
rl_replace_line("", 0); rl_replace_line("", 0);
rl_on_new_line(); rl_on_new_line();