Compare commits
155 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3df8bfa8a8 | |||
| df04c2fecc | |||
|
|
0da6bb86c4 | ||
| c3ed52819f | |||
| 1ec3636d5d | |||
|
|
fa7b31df86 | ||
| cbd9442469 | |||
| 211c35dacf | |||
| ade8aef236 | |||
|
|
efaed5beb8 | ||
| 470e2bb471 | |||
| c8953ce158 | |||
| 53b6ba7967 | |||
|
|
3015b4c345 | ||
|
|
64b25ac82e | ||
|
|
3d260621c5 | ||
| f0ec3de703 | |||
| b25e43fd43 | |||
|
|
b3d223a46e | ||
| 4113e7390a | |||
| b04375707d | |||
| 5258d6ca34 | |||
| 9d6965dd13 | |||
| 41f2ee15d4 | |||
| 97474fff51 | |||
| 495a9cf884 | |||
| 892d8f5b1e | |||
|
|
f9a2acb10b | ||
|
|
908fc5de8c | ||
| fb20146090 | |||
| f0ed610dd0 | |||
| b622f7dc0e | |||
| 1bb471f7a9 | |||
|
|
3abc8c5738 | ||
| d44a1b5e44 | |||
| 854e71e5f1 | |||
|
|
75f1eabfea | ||
| 0af263e106 | |||
| c492d6416d | |||
| 954e8c47fb | |||
| 10abbb572d | |||
| 13ab6456d0 | |||
| 568213dea4 | |||
| aeed660535 | |||
| 0d69254798 | |||
|
|
ab68d684f4 | ||
| fa522d064f | |||
|
|
c1520f2728 | ||
| 6e4c557f3d | |||
| 0f485659bc | |||
|
|
b9853ef760 | ||
| e392ba03c4 | |||
| a199160270 | |||
|
|
84eb4c9b2c | ||
|
|
c85c8975a3 | ||
| ca29e120e5 | |||
| c7da1acd4e | |||
| 72d7d2ebbf | |||
|
|
4e4fd52670 | ||
|
|
8c0116d52d | ||
| d413fed41c | |||
| af92795001 | |||
|
|
7568a9aa41 | ||
| 4495ac459c | |||
| ec1272258d | |||
| 50b63411bf | |||
| 8d6301e9e0 | |||
| fff746af50 | |||
| 7f1f57de51 | |||
|
|
c28ab971fb | ||
|
|
63aeb7843e | ||
| 20ca6b1b19 | |||
|
|
0b302e0514 | ||
| d824423822 | |||
| 0413133db9 | |||
|
|
183bf5c8b6 | ||
|
|
cc07376040 | ||
|
|
c6e34f8eaf | ||
|
|
9684b5e00e | ||
| 2d307e55af | |||
|
|
42a77e1986 | ||
| 3b8d055260 | |||
| 63ec3979b3 | |||
| d36888521a | |||
| 1b6f434978 | |||
|
|
d6cd561d91 | ||
| 3931c09970 | |||
| b0cd55b11c | |||
|
|
bdccd9f1e4 | ||
|
|
c2f08b31e3 | ||
|
|
5bdde9b3da | ||
| 0ea63d6d6c | |||
| 64071b0127 | |||
|
|
4d803fd575 | ||
|
|
a7e04285e0 | ||
|
|
f0a3e14afe | ||
| c9125238a0 | |||
|
|
467e46a948 | ||
| 9ddd5fb339 | |||
|
|
706a073e28 | ||
|
|
fb2cc23009 | ||
| 07f9ec1cea | |||
|
|
718927b928 | ||
| d4e928144b | |||
|
|
12529d99a5 | ||
|
|
965c2bb0f0 | ||
| 1fecbee182 | |||
| 76020ac4b0 | |||
| a672862c9f | |||
| dc31af91eb | |||
|
|
85bd3e4609 | ||
| 8bf7652650 | |||
| 6be968d204 | |||
| f6ded3dd69 | |||
| 5f8412098f | |||
| 86d4bbcd25 | |||
| 608abcf8fd | |||
|
|
7df8a60a2b | ||
|
|
f9aaa30064 | ||
|
|
2c2a293581 | ||
| c51d4e63c3 | |||
|
|
eea55c30d6 | ||
|
|
2841d5b920 | ||
|
|
1e8fe6e5a7 | ||
|
|
b771550397 | ||
| f396a7529f | |||
|
|
761794e503 | ||
|
|
ad18ecdeac | ||
| c11e2ebf1c | |||
| 01d80ba1cd | |||
| d78384b4e9 | |||
| cd750a160f | |||
| be031a442f | |||
| 7bb7b4afb2 | |||
| af438b8db4 | |||
| c27d653eaf | |||
| 610073ec06 | |||
| 795ad81851 | |||
| 0981534a07 | |||
| 5178f7181c | |||
| 46b4a154d2 | |||
| e6bba78db6 | |||
| 14e7abeedb | |||
| 830a977272 | |||
| 5c83326d9b | |||
| 43007c1c31 | |||
| 35f6c8bbaa | |||
| 0d864ea39b | |||
| 914d6ceafd | |||
| 8f391e5c2a | |||
| bb3c586aae | |||
| 287b1178fd | |||
| 500562e256 | |||
|
|
848012de8d | ||
| 409478c9be |
87
Makefile
87
Makefile
@ -1,35 +1,68 @@
|
|||||||
NAME = Minishell
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2022/03/06 12:50:24 by apommier #+# #+# #
|
||||||
|
# Updated: 2022/04/23 13:33:48 by apommier ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
CC = clang
|
NAME = minishell
|
||||||
|
SRCS = srcs/main.c\
|
||||||
|
srcs/pipe/execution.c\
|
||||||
|
srcs/pipe/pipex_utils.c\
|
||||||
|
srcs/pipe/exec_utils.c\
|
||||||
|
srcs/set_cmd/free_cmd.c\
|
||||||
|
srcs/set_cmd/set_cmd.c\
|
||||||
|
srcs/set_cmd/set_cmd_utils.c\
|
||||||
|
srcs/set_redirection/redirection.c\
|
||||||
|
srcs/set_redirection/utils.c\
|
||||||
|
srcs/set_redirection/set_heredoc.c\
|
||||||
|
srcs/set_redirection/heredoc_utils.c\
|
||||||
|
srcs/set_redirection/set_input.c\
|
||||||
|
srcs/set_redirection/set_output.c\
|
||||||
|
srcs/built_in/unset.c\
|
||||||
|
srcs/built_in/cd.c\
|
||||||
|
srcs/built_in/cd_utils.c\
|
||||||
|
srcs/built_in/echo.c\
|
||||||
|
srcs/built_in/export.c\
|
||||||
|
srcs/built_in/utils_builtin.c\
|
||||||
|
srcs/built_in/init_builtin.c\
|
||||||
|
srcs/built_in/export2.c\
|
||||||
|
srcs/built_in/env.c\
|
||||||
|
srcs/built_in/exit.c\
|
||||||
|
srcs/built_in/choose_builtin.c\
|
||||||
|
srcs/set_quote/split_with_quote.c\
|
||||||
|
srcs/set_quote/set_quote.c\
|
||||||
|
srcs/set_signals/set_signal.c\
|
||||||
|
srcs/set_quote/set_var.c\
|
||||||
|
|
||||||
FLAGS = -Wall -Wextra -Werror
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
CC = gcc
|
||||||
|
CFLAGS = -Wall -Wextra -Werror -fcommon
|
||||||
|
LIB = -lreadline
|
||||||
|
RM = rm -rf
|
||||||
|
LIBFT = ./libft
|
||||||
|
|
||||||
DEL = /bin/rm -f
|
${NAME}: ${OBJS}
|
||||||
|
@make bonus -C ${LIBFT}
|
||||||
|
${CC} ${OBJS} ${LIBFT}/libft.a -o ${NAME} -lreadline
|
||||||
|
|
||||||
SRCS = ./srcs/main.c\
|
all: ${NAME}
|
||||||
./srcs/parser.c\
|
|
||||||
./srcs/init.c\
|
|
||||||
./srcs/exit.c\
|
|
||||||
./srcs/export_utils.c\
|
|
||||||
|
|
||||||
SRCS_O = ${SRCS:.c=.o}
|
|
||||||
all: $(NAME)
|
|
||||||
|
|
||||||
LIBC = ar -rcs
|
|
||||||
|
|
||||||
%.o: %.c
|
|
||||||
${CC} ${FLAGS} -c $< -o ${<:.c=.o}
|
|
||||||
|
|
||||||
$(NAME): ${SRCS_O}
|
|
||||||
make bonus -C ./libft/
|
|
||||||
$(CC) $(FLAGS) $(SRCS_O) -o $(NAME) -L ./libft/ -lft -lreadline
|
|
||||||
|
|
||||||
fclean: clean
|
|
||||||
$(DEL) $(NAME)
|
|
||||||
make fclean -C ./libft/
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(DEL) $(SRCS_O)
|
@${RM} ${OBJS}
|
||||||
make clean -C ./libft/
|
@make clean -C ${LIBFT}
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
@${RM} ${NAME}
|
||||||
|
@make fclean -C ${LIBFT}
|
||||||
|
|
||||||
re: fclean all
|
re: fclean all
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
278
README.md
Normal file
278
README.md
Normal 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
|
||||||
12
ignoreliberror
Normal file
12
ignoreliberror
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
leak readline
|
||||||
|
Memcheck:Leak
|
||||||
|
...
|
||||||
|
fun:readline
|
||||||
|
}
|
||||||
|
{
|
||||||
|
leak add_history
|
||||||
|
Memcheck:Leak
|
||||||
|
...
|
||||||
|
fun:add_history
|
||||||
|
}
|
||||||
@ -1,46 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* mini.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/02/26 16:00:38 by sadjigui #+# #+# */
|
|
||||||
/* Updated: 2022/03/01 17:12:12 by sadjigui ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef MINI_H
|
|
||||||
# define MINI_H
|
|
||||||
|
|
||||||
# include <stdio.h>
|
|
||||||
# include <unistd.h>
|
|
||||||
# include <stdlib.h>
|
|
||||||
# include <readline/readline.h>
|
|
||||||
# include <readline/history.h>
|
|
||||||
# include "../libft/libft.h"
|
|
||||||
|
|
||||||
typedef struct s_struct {
|
|
||||||
char **envy;
|
|
||||||
} t_struct;
|
|
||||||
|
|
||||||
int find_len(char *input, int i);
|
|
||||||
int check_variable(char *variable);
|
|
||||||
|
|
||||||
void normal();
|
|
||||||
void red();
|
|
||||||
void lexer(char *input, t_struct *data);
|
|
||||||
void print_env(t_struct *data);
|
|
||||||
void init_struct(t_struct *data, char **env);
|
|
||||||
void ft_env(t_struct *data, char **env);
|
|
||||||
void ft_exit(t_struct *data);
|
|
||||||
void ft_export(char *input, t_struct *data);
|
|
||||||
void ft_export_variable(t_struct *data, char *variable);
|
|
||||||
void register_env(t_struct *data, char **variable);
|
|
||||||
void free_char_tab(char **libre);
|
|
||||||
void join_variable(t_struct *data, char **v_v, int size);
|
|
||||||
|
|
||||||
char *check_value(char *value);
|
|
||||||
char *define_value(char *value);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
173
includes/minishell.h
Normal file
173
includes/minishell.h
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* minishell.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/23 13:37:53 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef MINISHELL_H
|
||||||
|
# define MINISHELL_H
|
||||||
|
|
||||||
|
# include "../libft/libft.h"
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <readline/readline.h>
|
||||||
|
# include <readline/history.h>
|
||||||
|
# include <sys/types.h>
|
||||||
|
# include <sys/stat.h>
|
||||||
|
# include <sys/wait.h>
|
||||||
|
# include <fcntl.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <errno.h>
|
||||||
|
# include <signal.h>
|
||||||
|
# include <dirent.h>
|
||||||
|
# include <readline/readline.h>
|
||||||
|
# include <readline/history.h>
|
||||||
|
|
||||||
|
// Command Data Structure
|
||||||
|
|
||||||
|
// Describes a simple command and arguments
|
||||||
|
typedef struct s_simple {
|
||||||
|
struct s_command *big_cmd;
|
||||||
|
int fd[2];
|
||||||
|
int pipe[2];
|
||||||
|
int last_pipe[2];
|
||||||
|
int child;
|
||||||
|
int nb_args;
|
||||||
|
char *infile;
|
||||||
|
int in_type;
|
||||||
|
int out_type;
|
||||||
|
char *outfile;
|
||||||
|
char **args;
|
||||||
|
char *cmd;
|
||||||
|
} t_s_cmd;
|
||||||
|
|
||||||
|
int g_var;
|
||||||
|
|
||||||
|
// Describes a complete command with the multiple pipes if any
|
||||||
|
// and input/output redirection if any.
|
||||||
|
typedef struct s_command {
|
||||||
|
int tmpin;
|
||||||
|
int tmpout;
|
||||||
|
char **env;
|
||||||
|
int nb_s_cmd;
|
||||||
|
struct s_simple **s_cmds;
|
||||||
|
int err_var;
|
||||||
|
struct s_simple *current_s_cmd;
|
||||||
|
char **path;
|
||||||
|
} t_cmd;
|
||||||
|
|
||||||
|
//main.c
|
||||||
|
int main(int ac, char **av, char **path);
|
||||||
|
char **ft_dup_double(char **env);
|
||||||
|
|
||||||
|
//pipe.c
|
||||||
|
void execute(t_cmd *cmd);
|
||||||
|
|
||||||
|
//set_cmd.c
|
||||||
|
char *error_parsing(char *to_free);
|
||||||
|
t_cmd *set_cmd(char *input, char **path, int nb);
|
||||||
|
|
||||||
|
//exec_utils.c
|
||||||
|
int wait_exit(t_cmd *cmd);
|
||||||
|
void exit_child(t_cmd *cmd, int exit_pid, int status, int i);
|
||||||
|
void check_access(t_cmd *cmd);
|
||||||
|
void close_pipe(t_cmd *cmd);
|
||||||
|
|
||||||
|
//pipex_utils.c
|
||||||
|
void set_fdin(t_cmd *cmd, int *fdin);
|
||||||
|
void reset_fds(t_cmd *cmd);
|
||||||
|
char **get_path(char **env);
|
||||||
|
char *get_command(char **exec, char **env);
|
||||||
|
|
||||||
|
//free_cmd
|
||||||
|
void free_cmd(t_cmd *cmd);
|
||||||
|
void exit_shell(t_cmd *cmd, int ret);
|
||||||
|
|
||||||
|
//set_quote.c
|
||||||
|
char *set_var(t_cmd *big_cmd, char *cmd);
|
||||||
|
int is_in_quote(char *str, int index);
|
||||||
|
int is_quote_good(char *str);
|
||||||
|
char **ft_split_with_quote(char const *s, char c);
|
||||||
|
|
||||||
|
//signals
|
||||||
|
void crtl_c(int num);
|
||||||
|
void sig_heredoc(int num);
|
||||||
|
void sig_quit(int num);
|
||||||
|
|
||||||
|
//redirection.c set redirection and input good
|
||||||
|
char *get_word(char *str, int start);
|
||||||
|
char **add_line(char **tab, char *line);
|
||||||
|
char *set_redirection(t_s_cmd *cmd, char *line, int index, int i);
|
||||||
|
char next_space(char *str, int i);
|
||||||
|
|
||||||
|
//set_heredoc
|
||||||
|
int wait_prompt(t_s_cmd *cmd, int index, int i, char *input);
|
||||||
|
|
||||||
|
//heredoc_utils.c
|
||||||
|
void sig_heredoc(int num);
|
||||||
|
int free_wait_prompt(char *in, char**history);
|
||||||
|
void change_signal(void);
|
||||||
|
void sig_heredoc(int num);
|
||||||
|
char **fill_history(t_s_cmd *cmd, char *input, char *in, char **history);
|
||||||
|
|
||||||
|
//set_input.c
|
||||||
|
char *ft_input(char *line, t_s_cmd *cmd, int index);
|
||||||
|
|
||||||
|
//set_output.c
|
||||||
|
char *ft_output(char *line, t_s_cmd *cmd, int index);
|
||||||
|
|
||||||
|
//set_var.c
|
||||||
|
char *get_var(t_cmd *cmd, char *var_name);
|
||||||
|
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
|
||||||
|
|
||||||
|
//utils redirection
|
||||||
|
int parse_quote(t_cmd *cmd);
|
||||||
|
char *get_str(char *str, int start, int end);
|
||||||
|
char *cut_str(char *str, int start, int end);
|
||||||
|
int double_size(char **tab);
|
||||||
|
void print_double_fd(char **tab, int fd);
|
||||||
|
void free_double(char **tab);
|
||||||
|
|
||||||
|
//builtins utils
|
||||||
|
void ft_shlvl(char **env);
|
||||||
|
void register_env(t_s_cmd *cmd, char *variable);
|
||||||
|
void ft_env(t_s_cmd *cmd, char **env);
|
||||||
|
int find_pwd(t_s_cmd *cmd);
|
||||||
|
void init_s_cmd(t_s_cmd *cmd, char **env);
|
||||||
|
int tab_len(char **tab);
|
||||||
|
int find_len(char *input, int i, char c);
|
||||||
|
void lone_export(t_s_cmd *cmd);
|
||||||
|
int find_variable(char *variable, t_s_cmd *cmd);
|
||||||
|
int find_it(char **str, char *s);
|
||||||
|
int check_variable(char *variable);
|
||||||
|
int cd_error(t_s_cmd *cmd, char *str, int i);
|
||||||
|
int size_path(char **str);
|
||||||
|
int check_return(t_s_cmd *cmd, int var);
|
||||||
|
void change_oldpwd(char **env, int old_pwd, int pwd, char *p);
|
||||||
|
|
||||||
|
//real builtin
|
||||||
|
void ft_exit(t_s_cmd *cmd);
|
||||||
|
int print_env(t_cmd *cmd);
|
||||||
|
void ft_env(t_s_cmd *cmd, char **env);
|
||||||
|
void ft_exit(t_s_cmd *cmd);
|
||||||
|
int ft_export(t_s_cmd *cmd);
|
||||||
|
int ft_unset(t_s_cmd *cmd);
|
||||||
|
int ft_echo(t_s_cmd *cmd);
|
||||||
|
int ft_pwd(t_s_cmd *cmd);
|
||||||
|
int open_directory(t_s_cmd *cmd);//cd
|
||||||
|
|
||||||
|
//parse builtin
|
||||||
|
int is_builtin(char *cmd);
|
||||||
|
void call_builtin(t_cmd *cmd);
|
||||||
|
|
||||||
|
#endif
|
||||||
1
launch_minishell
Executable file
1
launch_minishell
Executable file
@ -0,0 +1 @@
|
|||||||
|
valgrind --leak-check=full --show-leak-kinds=all --suppressions=ignoreliberror --track-fds=yes ./minishell
|
||||||
106
libft/Makefile
106
libft/Makefile
@ -1,78 +1,68 @@
|
|||||||
NAME = libft.a
|
NAME = libft.a
|
||||||
|
SRCS = ft_memset.c \
|
||||||
CC = gcc
|
|
||||||
|
|
||||||
FLAGS = -Wall -Wextra -Werror
|
|
||||||
|
|
||||||
DEL = /bin/rm -f
|
|
||||||
|
|
||||||
SRCS = ft_atoi.c \
|
|
||||||
ft_bzero.c \
|
ft_bzero.c \
|
||||||
ft_calloc.c \
|
ft_memcpy.c \
|
||||||
ft_isalnum.c \
|
|
||||||
ft_isalpha.c \
|
|
||||||
ft_isascii.c \
|
|
||||||
ft_isdigit.c \
|
|
||||||
ft_isprint.c \
|
|
||||||
ft_itoa.c \
|
|
||||||
ft_memccpy.c \
|
ft_memccpy.c \
|
||||||
|
ft_memmove.c \
|
||||||
ft_memchr.c \
|
ft_memchr.c \
|
||||||
ft_memcmp.c \
|
ft_memcmp.c \
|
||||||
ft_memcpy.c \
|
|
||||||
ft_memmove.c \
|
|
||||||
ft_memset.c \
|
|
||||||
ft_split.c \
|
|
||||||
ft_strchr.c \
|
|
||||||
ft_strdup.c \
|
|
||||||
ft_strjoin.c \
|
|
||||||
ft_strlcat.c \
|
|
||||||
ft_strlcpy.c \
|
|
||||||
ft_strlen.c \
|
ft_strlen.c \
|
||||||
ft_strmapi.c \
|
ft_isalpha.c \
|
||||||
ft_strncmp.c \
|
ft_isdigit.c \
|
||||||
ft_strnstr.c \
|
ft_isalnum.c \
|
||||||
|
ft_isascii.c \
|
||||||
|
ft_isprint.c \
|
||||||
|
ft_toupper.c \
|
||||||
|
ft_tolower.c \
|
||||||
|
ft_strchr.c \
|
||||||
ft_strrchr.c \
|
ft_strrchr.c \
|
||||||
ft_strtrim.c \
|
ft_strncmp.c \
|
||||||
|
ft_strlcpy.c \
|
||||||
|
ft_strlcat.c \
|
||||||
|
ft_strnstr.c \
|
||||||
|
ft_atoi.c \
|
||||||
|
ft_calloc.c \
|
||||||
|
ft_strdup.c \
|
||||||
ft_substr.c \
|
ft_substr.c \
|
||||||
|
ft_strtrim.c \
|
||||||
|
ft_split.c \
|
||||||
|
ft_strjoin.c \
|
||||||
|
ft_itoa.c \
|
||||||
|
ft_strmapi.c \
|
||||||
ft_putchar_fd.c \
|
ft_putchar_fd.c \
|
||||||
ft_putstr_fd.c \
|
ft_putstr_fd.c \
|
||||||
ft_putendl_fd.c \
|
ft_putendl_fd.c \
|
||||||
ft_putnbr_fd.c \
|
ft_putnbr_fd.c \
|
||||||
ft_tolower.c \
|
get_next_line.c \
|
||||||
ft_toupper.c \
|
get_next_line_utils.c
|
||||||
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
BONUS_C = ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c ft_lstadd_back.c \
|
||||||
|
ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c ft_lstbeforelast.c
|
||||||
|
BONUS_O = ${BONUS_C:.c=.o}
|
||||||
|
CC = gcc
|
||||||
|
CFLAGS = -Wall -Wextra
|
||||||
|
RM = rm -rf
|
||||||
|
|
||||||
BONUS = ft_lstnew.c \
|
.c.o:
|
||||||
ft_lstsize.c \
|
@${CC} ${CFLAGS} -c $< -o ${<:.c=.o}
|
||||||
ft_lstlast.c \
|
|
||||||
ft_lstadd_front.c \
|
|
||||||
ft_lstadd_back.c \
|
|
||||||
ft_lstdelone.c \
|
|
||||||
ft_lstiter.c \
|
|
||||||
ft_lstmap.c \
|
|
||||||
ft_lstclear.c \
|
|
||||||
ft_putchar.c \
|
|
||||||
ft_putstr.c \
|
|
||||||
ft_strcmp.c \
|
|
||||||
|
|
||||||
SRCS_O = ${SRCS:.c=.o}
|
${NAME}:${OBJS}
|
||||||
BONUS_O = ${BONUS:.c=.o}
|
@ar -rcs ${NAME} ${OBJS}
|
||||||
all: $(NAME)
|
|
||||||
|
|
||||||
LIBC = ar -rcs
|
all: ${NAME}
|
||||||
|
|
||||||
%.o: %.c
|
bonus: all
|
||||||
${CC} ${FLAGS} -c $< -o ${<:.c=.o}
|
@${CC} ${CFLAGS} -c ${BONUS_C}
|
||||||
|
@ar -rcs ${NAME} ${BONUS_O}
|
||||||
$(NAME): ${SRCS_O}
|
|
||||||
${LIBC} $(NAME) $(SRCS_O)
|
|
||||||
|
|
||||||
bonus: $(SRCS_O) $(BONUS_O)
|
|
||||||
$(LIBC) $(NAME) $(SRCS_O) $(BONUS_O)
|
|
||||||
|
|
||||||
fclean: clean
|
|
||||||
$(DEL) $(NAME)
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(DEL) $(SRCS_O) $(BONUS_O)
|
@${RM} ${OBJS}
|
||||||
|
@${RM} ${BONUS_O}
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
@${RM} ${NAME}
|
||||||
|
|
||||||
re: fclean all
|
re: fclean all
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re bonus
|
||||||
|
|
||||||
|
|||||||
@ -3,47 +3,37 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_atoi.c :+: :+: :+: */
|
/* ft_atoi.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 12:44:41 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:09:17 by apommier #+# #+# */
|
||||||
/* Updated: 2021/12/08 19:13:51 by sadjigui ### ########.fr */
|
/* Updated: 2022/04/19 12:11:56 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
static int ft_space(const char *str)
|
long long ft_atoi(const char *nptr)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
long long nbr;
|
||||||
|
long long minus;
|
||||||
|
|
||||||
|
minus = 1;
|
||||||
|
nbr = 0;
|
||||||
i = 0;
|
i = 0;
|
||||||
while ((str[i] == 32) || (str[i] >= 9 && str[i] <= 13))
|
while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == 32)
|
||||||
i++;
|
i++;
|
||||||
return (i);
|
if (nptr[i] == '+')
|
||||||
}
|
i++;
|
||||||
|
else if (nptr[i] == '-')
|
||||||
long ft_atoi(const char *str)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int sign;
|
|
||||||
long result;
|
|
||||||
|
|
||||||
result = 0;
|
|
||||||
i = ft_space(str);
|
|
||||||
sign = 0;
|
|
||||||
if (str[i] == '-' || str[i] == '+')
|
|
||||||
{
|
{
|
||||||
if (str[i] == '-')
|
i++;
|
||||||
sign = 1;
|
minus = -1;
|
||||||
|
}
|
||||||
|
while (nptr[i] >= '0' && nptr[i] <= '9')
|
||||||
|
{
|
||||||
|
nbr = nbr * 10 + nptr[i] - '0';
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
while (str[i] >= '0' && str[i] <= '9')
|
return (minus * nbr);
|
||||||
{
|
|
||||||
result = result * 10 + str[i] - 48;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
if (sign == 1)
|
|
||||||
return (-result);
|
|
||||||
else
|
|
||||||
return (result);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,23 +3,26 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_bzero.c :+: :+: :+: */
|
/* ft_bzero.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 12:54:03 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:09:48 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/31 16:52:41 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:26:30 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void ft_bzero(void *str, size_t n)
|
void ft_bzero(void *s, size_t n)
|
||||||
{
|
{
|
||||||
size_t i;
|
char *p;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
p = (char *)s;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < n)
|
while (n != 0)
|
||||||
{
|
{
|
||||||
*(char *)(str + i) = 0;
|
p[i] = 0;
|
||||||
i++;
|
i++;
|
||||||
|
n--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,23 +3,29 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_calloc.c :+: :+: :+: */
|
/* ft_calloc.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 12:55:08 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:09:57 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/22 12:55:45 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 21:18:04 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void *ft_calloc(size_t count, size_t size)
|
void *ft_calloc(size_t nmemb, size_t size)
|
||||||
{
|
{
|
||||||
void *trace;
|
char *new;
|
||||||
|
int i;
|
||||||
|
|
||||||
trace = malloc(count * size);
|
i = 0;
|
||||||
if (!trace)
|
new = malloc(size * nmemb);
|
||||||
return (NULL);
|
if (new)
|
||||||
if (trace)
|
{
|
||||||
ft_bzero(trace, count * size);
|
while (size * nmemb - i)
|
||||||
return (trace);
|
{
|
||||||
|
new[i] = 0;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ((void *)new);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,19 +3,20 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_isalnum.c :+: :+: :+: */
|
/* ft_isalnum.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 12:56:29 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:10:08 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/22 13:06:39 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/12 09:26:43 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
int ft_isalnum(int i)
|
int ft_isalnum(int c)
|
||||||
{
|
{
|
||||||
if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z')
|
if (c <= '9' && c >= '0')
|
||||||
|| (i >= '0' && i <= '9'))
|
return (1);
|
||||||
|
else if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
|
||||||
return (1);
|
return (1);
|
||||||
else
|
else
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
@ -3,18 +3,18 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_isalpha.c :+: :+: :+: */
|
/* ft_isalpha.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 13:07:14 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:10:17 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/22 13:08:24 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/12 09:28:12 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
int ft_isalpha(int i)
|
int ft_isalpha(int c)
|
||||||
{
|
{
|
||||||
if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z'))
|
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
|
||||||
return (1);
|
return (1);
|
||||||
else
|
else
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
@ -3,18 +3,18 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_isascii.c :+: :+: :+: */
|
/* ft_isascii.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 13:08:54 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:10:30 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/22 13:10:00 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/11 18:11:18 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
int ft_isascii(int i)
|
int ft_isascii(int c)
|
||||||
{
|
{
|
||||||
if (i >= 0 && i <= 127)
|
if (c >= 0 && c <= 127)
|
||||||
return (1);
|
return (1);
|
||||||
else
|
else
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_isdigit.c :+: :+: :+: */
|
/* ft_isdigit.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 13:10:25 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:10:39 by apommier #+# #+# */
|
||||||
/* Updated: 2021/12/08 19:18:53 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/12 09:27:06 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,5 +14,8 @@
|
|||||||
|
|
||||||
int ft_isdigit(int c)
|
int ft_isdigit(int c)
|
||||||
{
|
{
|
||||||
return (c >= '0' && c <= '9');
|
if (c <= '9' && c >= '0')
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,18 +3,18 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_isprint.c :+: :+: :+: */
|
/* ft_isprint.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 13:11:25 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:10:55 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/22 13:12:26 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/11 18:11:43 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
int ft_isprint(int i)
|
int ft_isprint(int c)
|
||||||
{
|
{
|
||||||
if (i >= 32 && i <= 126)
|
if (c > 31 && c < 127)
|
||||||
return (1);
|
return (1);
|
||||||
else
|
else
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
@ -3,67 +3,57 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_itoa.c :+: :+: :+: */
|
/* ft_itoa.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 13:13:20 by sadjigui #+# #+# */
|
/* Created: 2020/12/08 18:20:19 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/31 17:09:12 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:28:01 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int base_len(long nb)
|
static char *fill(long n, int j, int minus)
|
||||||
{
|
{
|
||||||
int len;
|
char *dest;
|
||||||
|
|
||||||
len = 1;
|
j += minus;
|
||||||
if (nb < 0)
|
dest = (char *)malloc(sizeof(char) * (j + 1));
|
||||||
{
|
if (dest == 0)
|
||||||
nb = -nb;
|
return (0);
|
||||||
len++;
|
dest[j] = 0;
|
||||||
}
|
|
||||||
while (nb >= 10)
|
|
||||||
{
|
|
||||||
nb /= 10;
|
|
||||||
len++;
|
|
||||||
}
|
|
||||||
return (len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void filler(char *str, int i, long n)
|
|
||||||
{
|
|
||||||
if (n < 0)
|
|
||||||
{
|
|
||||||
str[0] = '-';
|
|
||||||
n = -n;
|
|
||||||
}
|
|
||||||
while (n > 0)
|
|
||||||
{
|
|
||||||
str[i] = 48 + (n % 10);
|
|
||||||
n /= 10;
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char *ft_itoa(int nb)
|
|
||||||
{
|
|
||||||
long n;
|
|
||||||
int i;
|
|
||||||
char *str;
|
|
||||||
|
|
||||||
n = nb;
|
|
||||||
i = base_len(n);
|
|
||||||
str = (char *)malloc(sizeof(char) * i + 1);
|
|
||||||
if (!str)
|
|
||||||
return (NULL);
|
|
||||||
str[i] = '\0';
|
|
||||||
i--;
|
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
|
dest[j - 1] = '0';
|
||||||
|
while (n)
|
||||||
{
|
{
|
||||||
str[0] = 48;
|
dest[j - 1] = n % 10 + '0';
|
||||||
return (str);
|
j--;
|
||||||
|
n /= 10;
|
||||||
}
|
}
|
||||||
filler(str, i, n);
|
if (minus)
|
||||||
return (str);
|
dest[j - 1] = '-';
|
||||||
|
return (dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_itoa(int n)
|
||||||
|
{
|
||||||
|
long i;
|
||||||
|
long k;
|
||||||
|
int j;
|
||||||
|
int minus;
|
||||||
|
|
||||||
|
k = n;
|
||||||
|
minus = 0;
|
||||||
|
j = 1;
|
||||||
|
if (k < 0)
|
||||||
|
{
|
||||||
|
minus = 1;
|
||||||
|
k = k * -1;
|
||||||
|
}
|
||||||
|
i = k;
|
||||||
|
while (k >= 10)
|
||||||
|
{
|
||||||
|
k /= 10;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
return (fill(i, j, minus));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_lstadd_back.c :+: :+: :+: */
|
/* ft_lstadd_back.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/03 15:05:04 by sadjigui #+# #+# */
|
/* Created: 2020/12/09 19:30:14 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/07 11:37:08 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/11 15:32:53 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,16 +14,8 @@
|
|||||||
|
|
||||||
void ft_lstadd_back(t_list **alst, t_list *new)
|
void ft_lstadd_back(t_list **alst, t_list *new)
|
||||||
{
|
{
|
||||||
t_list *yup;
|
if (*alst == 0)
|
||||||
|
|
||||||
if (alst)
|
|
||||||
{
|
|
||||||
if (*alst)
|
|
||||||
{
|
|
||||||
yup = ft_lstlast(*alst);
|
|
||||||
yup->next = new;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
*alst = new;
|
*alst = new;
|
||||||
}
|
else
|
||||||
|
ft_lstlast(*alst)->next = new;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_lstadd_front.c :+: :+: :+: */
|
/* ft_lstadd_front.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/03 14:47:57 by sadjigui #+# #+# */
|
/* Created: 2020/12/09 01:31:45 by apommier #+# #+# */
|
||||||
/* Updated: 2021/12/08 19:18:16 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/11 17:34:24 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,8 +14,6 @@
|
|||||||
|
|
||||||
void ft_lstadd_front(t_list **alst, t_list *new)
|
void ft_lstadd_front(t_list **alst, t_list *new)
|
||||||
{
|
{
|
||||||
if (!alst)
|
|
||||||
return ;
|
|
||||||
new->next = *alst;
|
new->next = *alst;
|
||||||
*alst = new;
|
*alst = new;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,24 +1,28 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_putstr.c :+: :+: :+: */
|
/* ft_lstbeforelast.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/12/08 19:23:23 by sadjigui #+# #+# */
|
/* Created: 2021/12/09 19:16:55 by apommier #+# #+# */
|
||||||
/* Updated: 2021/12/08 19:23:28 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:28:22 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void ft_putstr(char const *str)
|
t_list *ft_lstbeforelast(t_list *lst)
|
||||||
{
|
{
|
||||||
int i;
|
t_list *save;
|
||||||
|
|
||||||
if (!str)
|
save = 0;
|
||||||
return ;
|
if (!lst)
|
||||||
i = 0;
|
return (0);
|
||||||
while (str[i])
|
while (lst->next)
|
||||||
write(1, &str[i++], 1);
|
{
|
||||||
|
save = lst;
|
||||||
|
lst = lst->next;
|
||||||
|
}
|
||||||
|
return (save);
|
||||||
}
|
}
|
||||||
@ -3,25 +3,26 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_lstclear.c :+: :+: :+: */
|
/* ft_lstclear.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/03 16:03:18 by sadjigui #+# #+# */
|
/* Created: 2020/12/09 19:58:04 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/07 12:13:31 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:28:39 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void ft_lstclear(t_list **lst, void (*del)(void *))
|
void ft_lstclear(t_list **lst, void (*del)(void*))
|
||||||
{
|
{
|
||||||
t_list *nap;
|
t_list *chr;
|
||||||
|
|
||||||
if (!lst || !del || !*lst)
|
chr = *lst;
|
||||||
return ;
|
while (*lst)
|
||||||
while (lst && *lst)
|
|
||||||
{
|
{
|
||||||
nap = (*lst)->next;
|
chr = (*lst)->next;
|
||||||
ft_lstdelone(*lst, del);
|
del((*lst)->nbr);
|
||||||
*lst = nap;
|
free(*lst);
|
||||||
|
*lst = chr;
|
||||||
}
|
}
|
||||||
|
lst = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_lstdelone.c :+: :+: :+: */
|
/* ft_lstdelone.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/03 15:56:45 by sadjigui #+# #+# */
|
/* Created: 2020/12/09 19:54:40 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/03 16:02:20 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/12 09:10:11 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,8 +14,8 @@
|
|||||||
|
|
||||||
void ft_lstdelone(t_list *lst, void (*del)(void*))
|
void ft_lstdelone(t_list *lst, void (*del)(void*))
|
||||||
{
|
{
|
||||||
if (!del || !lst)
|
if (!lst)
|
||||||
return ;
|
return ;
|
||||||
(del)(lst->content);
|
del(lst->nbr);
|
||||||
free (lst);
|
free(lst);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_lstiter.c :+: :+: :+: */
|
/* ft_lstiter.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/07 11:34:11 by sadjigui #+# #+# */
|
/* Created: 2020/12/09 20:14:47 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/07 11:48:46 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/11 17:47:39 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,11 +14,9 @@
|
|||||||
|
|
||||||
void ft_lstiter(t_list *lst, void (*f)(void *))
|
void ft_lstiter(t_list *lst, void (*f)(void *))
|
||||||
{
|
{
|
||||||
if (!f || !lst)
|
|
||||||
return ;
|
|
||||||
while (lst)
|
while (lst)
|
||||||
{
|
{
|
||||||
(*f)(lst->content);
|
f(lst->nbr);
|
||||||
lst = lst->next;
|
lst = lst->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_lstlast.c :+: :+: :+: */
|
/* ft_lstlast.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/03 15:00:53 by sadjigui #+# #+# */
|
/* Created: 2020/12/09 19:16:55 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/03 16:09:33 by sadjigui ### ########.fr */
|
/* Updated: 2021/11/26 01:21:39 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -15,11 +15,8 @@
|
|||||||
t_list *ft_lstlast(t_list *lst)
|
t_list *ft_lstlast(t_list *lst)
|
||||||
{
|
{
|
||||||
if (!lst)
|
if (!lst)
|
||||||
return (NULL);
|
return (0);
|
||||||
if (lst)
|
|
||||||
{
|
|
||||||
while (lst->next)
|
while (lst->next)
|
||||||
lst = lst->next;
|
lst = lst->next;
|
||||||
}
|
|
||||||
return (lst);
|
return (lst);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_lstmap.c :+: :+: :+: */
|
/* ft_lstmap.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/07 12:31:29 by sadjigui #+# #+# */
|
/* Created: 2020/12/11 00:10:12 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/07 12:37:22 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/13 22:37:30 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,7 +14,20 @@
|
|||||||
|
|
||||||
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
|
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
|
||||||
{
|
{
|
||||||
(void)*f;
|
t_list *new;
|
||||||
(void)*del;
|
t_list *begin;
|
||||||
return (lst);
|
|
||||||
|
begin = 0;
|
||||||
|
while (lst)
|
||||||
|
{
|
||||||
|
new = ft_lstnew(f(lst->nbr));
|
||||||
|
if (!new)
|
||||||
|
{
|
||||||
|
ft_lstclear(&begin, *del);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
ft_lstadd_back(&begin, new);
|
||||||
|
lst = lst->next;
|
||||||
|
}
|
||||||
|
return (begin);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_lstnew.c :+: :+: :+: */
|
/* ft_lstnew.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/03 12:53:17 by sadjigui #+# #+# */
|
/* Created: 2020/12/09 01:06:20 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/07 11:49:54 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:29:09 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -16,10 +16,11 @@ t_list *ft_lstnew(void *content)
|
|||||||
{
|
{
|
||||||
t_list *new;
|
t_list *new;
|
||||||
|
|
||||||
new = malloc(sizeof(t_list));
|
new = (t_list *)malloc(sizeof(t_list));
|
||||||
if (!new)
|
if (!new)
|
||||||
return (NULL);
|
return (0);
|
||||||
new->content = content;
|
new->swap = 0;
|
||||||
new->next = NULL;
|
new->nbr = content;
|
||||||
|
new->next = 0;
|
||||||
return (new);
|
return (new);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_lstsize.c :+: :+: :+: */
|
/* ft_lstsize.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/03 14:58:00 by sadjigui #+# #+# */
|
/* Created: 2020/12/09 03:24:50 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/03 16:10:11 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/11 15:33:49 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -17,13 +17,10 @@ int ft_lstsize(t_list *lst)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if (lst)
|
|
||||||
{
|
|
||||||
while (lst)
|
while (lst)
|
||||||
{
|
{
|
||||||
lst = lst->next;
|
lst = lst->next;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,26 +3,30 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_memccpy.c :+: :+: :+: */
|
/* ft_memccpy.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/31 17:10:05 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:11:04 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/31 17:10:57 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:29:38 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void *ft_memccpy(void *dest, const void *src, int ch, size_t maxSize)
|
void *ft_memccpy(void *dest, const void *src, int c, size_t n)
|
||||||
{
|
{
|
||||||
|
char *p;
|
||||||
|
const char *p1;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < maxSize)
|
p = (char *)dest;
|
||||||
|
p1 = (const char *)src;
|
||||||
|
while (i < n)
|
||||||
{
|
{
|
||||||
*(unsigned char *)(dest + i) = *(unsigned char *)(src + i);
|
p[i] = p1[i];
|
||||||
if (*(unsigned char *)(src + i) == (unsigned char)ch)
|
if ((unsigned char)p[i] == (unsigned char)c)
|
||||||
return (dest + i + 1);
|
return (dest + i + 1);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,25 +3,26 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_memchr.c :+: :+: :+: */
|
/* ft_memchr.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/25 14:48:34 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:11:39 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/25 15:57:53 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:29:51 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void *ft_memchr(const void *mem, int ch, size_t size)
|
void *ft_memchr(const void *s, int c, size_t n)
|
||||||
{
|
{
|
||||||
size_t i;
|
unsigned char *str;
|
||||||
|
|
||||||
i = 0;
|
str = (unsigned char *)s;
|
||||||
while (i < size)
|
while (n)
|
||||||
{
|
{
|
||||||
if (*(unsigned char *)(mem + i) == (unsigned char)ch)
|
if ((unsigned char)c == *str)
|
||||||
return ((unsigned char *)(mem + i));
|
return (str);
|
||||||
i++;
|
n--;
|
||||||
|
str++;
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,24 +3,25 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_memcmp.c :+: :+: :+: */
|
/* ft_memcmp.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/31 17:11:51 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:11:51 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/31 17:11:55 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:30:32 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
int ft_memcmp(const void *p1, const void *p2, size_t size)
|
int ft_memcmp(const void *s1, const void *s2, size_t n)
|
||||||
{
|
{
|
||||||
size_t i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < size)
|
while (n)
|
||||||
{
|
{
|
||||||
if (!(*(unsigned char *)(p1 + i) == *(unsigned char *)(p2 + i)))
|
if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i])
|
||||||
return (*(unsigned char *)(p1 + i) - *(unsigned char *)(p2 + i));
|
return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
|
||||||
|
n--;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
@ -3,26 +3,29 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_memcpy.c :+: :+: :+: */
|
/* ft_memcpy.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/31 17:46:43 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:12:03 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/31 17:47:17 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:30:43 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void *ft_memcpy(void *dest, const void *src, size_t size)
|
void *ft_memcpy(void *dest, const void *src, size_t n)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
char *p;
|
||||||
|
const char *p1;
|
||||||
|
|
||||||
i = 0;
|
|
||||||
if (!dest && !src)
|
if (!dest && !src)
|
||||||
return (NULL);
|
return (dest);
|
||||||
while (size > 0)
|
p = (char *)dest;
|
||||||
|
p1 = (const char *)src;
|
||||||
|
i = 0;
|
||||||
|
while (i < n)
|
||||||
{
|
{
|
||||||
*(unsigned char *)(dest + i) = *(unsigned char *)(src + i);
|
p[i] = p1[i];
|
||||||
size--;
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (dest);
|
return (dest);
|
||||||
|
|||||||
@ -3,35 +3,37 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_memmove.c :+: :+: :+: */
|
/* ft_memmove.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/31 17:14:39 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:12:14 by apommier #+# #+# */
|
||||||
/* Updated: 2021/12/08 19:22:11 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:31:05 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void *ft_memmove(void *dest, const void *src, size_t size)
|
void *ft_memmove(void *dest, const void *src, size_t n)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if (src == dest)
|
if (!dest && !src)
|
||||||
return (dest);
|
return (dest);
|
||||||
if (dest > src)
|
if (dest > src)
|
||||||
{
|
{
|
||||||
while (size > 0)
|
while (n)
|
||||||
{
|
{
|
||||||
*(char *)(dest + (size - 1)) = *(char *)(src + (size - 1));
|
((unsigned char *)dest)[n - 1] = ((unsigned char *)src)[n - 1];
|
||||||
size--;
|
n--;
|
||||||
}
|
}
|
||||||
return (dest);
|
|
||||||
}
|
}
|
||||||
while (i < size)
|
else
|
||||||
{
|
{
|
||||||
*(char *)(dest + i) = *(char *)(src + i);
|
while (i < n)
|
||||||
|
{
|
||||||
|
((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return (dest);
|
return (dest);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,24 +3,27 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_memset.c :+: :+: :+: */
|
/* ft_memset.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/25 15:58:25 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:12:24 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/31 17:17:00 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:31:28 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void *ft_memset(void *pointer, int value, size_t count)
|
void *ft_memset(void *s, int c, size_t n)
|
||||||
{
|
{
|
||||||
size_t i;
|
int i;
|
||||||
|
unsigned char *p;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < count)
|
p = (unsigned char *)s;
|
||||||
|
while (n > 0)
|
||||||
{
|
{
|
||||||
*(char *)(pointer + i) = (char)value;
|
p[i] = (unsigned char)c;
|
||||||
i++;
|
i++;
|
||||||
|
n--;
|
||||||
}
|
}
|
||||||
return (pointer);
|
return (s);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_putchar_fd.c :+: :+: :+: */
|
/* ft_putchar_fd.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 14:44:48 by sadjigui #+# #+# */
|
/* Created: 2020/12/08 23:11:29 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/31 17:18:07 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/11 17:02:13 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,6 +14,5 @@
|
|||||||
|
|
||||||
void ft_putchar_fd(char c, int fd)
|
void ft_putchar_fd(char c, int fd)
|
||||||
{
|
{
|
||||||
if (fd >= 0)
|
|
||||||
write(fd, &c, 1);
|
write(fd, &c, 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_putendl_fd.c :+: :+: :+: */
|
/* ft_putendl_fd.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 14:46:41 by sadjigui #+# #+# */
|
/* Created: 2020/12/08 23:22:10 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/31 17:19:23 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/11 17:14:52 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,15 +14,8 @@
|
|||||||
|
|
||||||
void ft_putendl_fd(char *s, int fd)
|
void ft_putendl_fd(char *s, int fd)
|
||||||
{
|
{
|
||||||
int i;
|
if (s == 0)
|
||||||
|
|
||||||
i = 0;
|
|
||||||
if (!s)
|
|
||||||
return ;
|
return ;
|
||||||
while (s[i])
|
write(fd, s, ft_strlen(s));
|
||||||
{
|
ft_putchar_fd('\n', fd);
|
||||||
write(fd, &s[i], 1);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
write(fd, "\n", 1);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_putnbr_fd.c :+: :+: :+: */
|
/* ft_putnbr_fd.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 14:48:10 by sadjigui #+# #+# */
|
/* Created: 2020/12/08 23:25:57 by apommier #+# #+# */
|
||||||
/* Updated: 2021/12/08 19:11:58 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:31:56 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,18 +14,15 @@
|
|||||||
|
|
||||||
void ft_putnbr_fd(int n, int fd)
|
void ft_putnbr_fd(int n, int fd)
|
||||||
{
|
{
|
||||||
if (n < 0)
|
long nbr;
|
||||||
|
|
||||||
|
nbr = n;
|
||||||
|
if (nbr < 0)
|
||||||
{
|
{
|
||||||
ft_putchar_fd('-', fd);
|
ft_putchar_fd('-', fd);
|
||||||
if (n == -2147483648)
|
nbr *= -1;
|
||||||
write(fd, "2147483648", 10);
|
|
||||||
n = -n;
|
|
||||||
}
|
}
|
||||||
if (n >= 10)
|
if (nbr >= 10)
|
||||||
{
|
ft_putnbr_fd(nbr / 10, fd);
|
||||||
ft_putnbr_fd(n / 10, fd);
|
ft_putchar_fd(nbr % 10 + '0', fd);
|
||||||
ft_putnbr_fd(n % 10, fd);
|
|
||||||
}
|
|
||||||
else if (n >= 0 && n <= 9)
|
|
||||||
ft_putchar_fd(n + '0', fd);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_putstr_fd.c :+: :+: :+: */
|
/* ft_putstr_fd.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 14:51:56 by sadjigui #+# #+# */
|
/* Created: 2020/12/08 23:15:25 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/03 12:41:49 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/11 17:15:10 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,14 +14,7 @@
|
|||||||
|
|
||||||
void ft_putstr_fd(char *s, int fd)
|
void ft_putstr_fd(char *s, int fd)
|
||||||
{
|
{
|
||||||
int i;
|
if (s == 0)
|
||||||
|
|
||||||
i = 0;
|
|
||||||
if (!s || fd <= 0)
|
|
||||||
return ;
|
return ;
|
||||||
while (s[i])
|
write(fd, s, ft_strlen(s));
|
||||||
{
|
|
||||||
write(fd, &s[i], 1);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
109
libft/ft_split.c
109
libft/ft_split.c
@ -3,89 +3,78 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_split.c :+: :+: :+: */
|
/* ft_split.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 14:53:23 by sadjigui #+# #+# */
|
/* Created: 2020/12/07 00:54:12 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/03 12:10:50 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/21 08:09:38 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
static size_t ft_wordcount(char const *s, char c)
|
static int fill_tab(char *s, char c, char **dest, size_t index)
|
||||||
{
|
{
|
||||||
size_t word;
|
int i;
|
||||||
size_t state;
|
|
||||||
|
|
||||||
state = 1;
|
|
||||||
word = 0;
|
|
||||||
while (*s)
|
|
||||||
{
|
|
||||||
if (*s == c)
|
|
||||||
state = 1;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (state == 1)
|
|
||||||
word++;
|
|
||||||
state = 0;
|
|
||||||
}
|
|
||||||
s++;
|
|
||||||
}
|
|
||||||
return (word);
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t ft_word_length(char const *s, char c)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (*s && *s != c)
|
while (s[i] != c && s[i])
|
||||||
{
|
i++;
|
||||||
|
dest[index] = (char *)ft_calloc(i + 1, sizeof(char));
|
||||||
|
if (dest[index] == 0)
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
while (s[i] != c && s[i])
|
||||||
|
{
|
||||||
|
dest[index][i] = s[i];
|
||||||
i++;
|
i++;
|
||||||
s++;
|
|
||||||
}
|
}
|
||||||
return (i);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char **ft_free(char **str)
|
static void call(char *s, char c, char **dest, int j)
|
||||||
{
|
{
|
||||||
size_t i;
|
int index;
|
||||||
|
int k;
|
||||||
|
|
||||||
i = 0;
|
k = 0;
|
||||||
while (str[i])
|
index = 0;
|
||||||
|
while (j > index)
|
||||||
{
|
{
|
||||||
free(str[i]);
|
while (s[k] == c && s[k])
|
||||||
i++;
|
k++;
|
||||||
|
if (!s[k])
|
||||||
|
return ;
|
||||||
|
fill_tab(s + k, c, dest, index);
|
||||||
|
index++;
|
||||||
|
while (s[k] != c && s[k])
|
||||||
|
k++;
|
||||||
}
|
}
|
||||||
free(str);
|
|
||||||
return (NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char **ft_split(char const *s, char c)
|
char **ft_split(char const *s, char c)
|
||||||
{
|
{
|
||||||
size_t i;
|
int i;
|
||||||
size_t j;
|
int j;
|
||||||
char **splited;
|
char **dest;
|
||||||
|
|
||||||
i = 0;
|
|
||||||
if (s == 0)
|
|
||||||
return (NULL);
|
|
||||||
splited = (char **)malloc(sizeof(char *) * (ft_wordcount(s, c) + 1));
|
|
||||||
if (!splited)
|
|
||||||
return (NULL);
|
|
||||||
while (ft_wordcount(s, c))
|
|
||||||
{
|
|
||||||
while (*s && *s == c)
|
|
||||||
s++;
|
|
||||||
splited[i] = (char *)malloc(sizeof(char) * (ft_word_length(s, c) + 1));
|
|
||||||
if (!splited[i])
|
|
||||||
return (ft_free(splited));
|
|
||||||
j = 0;
|
j = 0;
|
||||||
while (*s != c && *s)
|
i = 0;
|
||||||
splited[i][j++] = *s++;
|
if (!s)
|
||||||
splited[i][j] = 0;
|
return (0);
|
||||||
|
while (s[i] == c && s[i])
|
||||||
|
i++;
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
while (s[i] != c && s[i])
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
while (s[i] == c && s[i])
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
splited[i] = 0;
|
dest = (char **)ft_calloc(sizeof(char *), (i + j));
|
||||||
return (splited);
|
if (!dest)
|
||||||
|
return (0);
|
||||||
|
dest[j] = 0;
|
||||||
|
call((char *)s, c, dest, j);
|
||||||
|
return (dest);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,27 +3,26 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strchr.c :+: :+: :+: */
|
/* ft_strchr.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/20 13:33:40 by sadjigui #+# #+# */
|
/* Created: 2020/12/12 13:57:59 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/03 12:40:48 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 21:18:59 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
char *ft_strchr(const char *str, int c)
|
char *ft_strchr(const char *s, int c)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
char *str;
|
||||||
|
|
||||||
i = 0;
|
if (!s)
|
||||||
while (str[i])
|
return ("no s");
|
||||||
{
|
str = (char *)s;
|
||||||
if (str[i] == (char)c)
|
while ((*str != c) && (*str != 0))
|
||||||
return ((char *)str + i);
|
str++;
|
||||||
i++;
|
if (*str == c)
|
||||||
}
|
return ((char *)str);
|
||||||
if (str[i] == (char)c)
|
else
|
||||||
return ((char *)str + i);
|
return (0);
|
||||||
return (NULL);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,39 +3,32 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strdup.c :+: :+: :+: */
|
/* ft_strdup.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 15:05:42 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:12:43 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/03 12:38:05 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/11 18:35:10 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
static char *ft_strcpy(char *dest, const char *src)
|
char *ft_strdup(const char *s)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
char *sdup;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (src[i])
|
while (s[i])
|
||||||
|
i++;
|
||||||
|
sdup = malloc(sizeof(char) * (i + 1));
|
||||||
|
if (sdup == 0)
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
while (s[i])
|
||||||
{
|
{
|
||||||
dest[i] = src[i];
|
sdup[i] = s[i];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
dest[i] = '\0';
|
sdup[i] = 0;
|
||||||
return (dest);
|
return (sdup);
|
||||||
}
|
|
||||||
|
|
||||||
char *ft_strdup(const char *src)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
char *copy;
|
|
||||||
|
|
||||||
copy = NULL;
|
|
||||||
i = ft_strlen(src);
|
|
||||||
copy = malloc(sizeof(char) * i + 1);
|
|
||||||
if (!copy)
|
|
||||||
return (NULL);
|
|
||||||
copy = ft_strcpy(copy, src);
|
|
||||||
return (copy);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,51 +3,39 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strjoin.c :+: :+: :+: */
|
/* ft_strjoin.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 15:26:12 by sadjigui #+# #+# */
|
/* Created: 2022/01/20 21:44:01 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/22 15:40:20 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/21 08:07:04 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
static char *ft_strcat_j(char const *s1, char const *s2, char *dest)
|
char *ft_strjoin(char *save, char *s2)
|
||||||
{
|
{
|
||||||
|
char *dest;
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
j = 0;
|
j = 0;
|
||||||
while (s1[i])
|
if (!save && !s2)
|
||||||
|
return (0);
|
||||||
|
dest = malloc(ft_strlen(save) + ft_strlen(s2) + 1);
|
||||||
|
while (save && save[i])
|
||||||
{
|
{
|
||||||
dest[j] = s1[i];
|
dest[j] = save[i];
|
||||||
i++;
|
|
||||||
j++;
|
j++;
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
i = 0;
|
i = 0;
|
||||||
while (s2[i])
|
while (s2 && s2[i])
|
||||||
{
|
{
|
||||||
dest[j] = s2[i];
|
dest[j] = s2[i];
|
||||||
i++;
|
|
||||||
j++;
|
j++;
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
dest[j] = '\0';
|
dest[j] = 0;
|
||||||
return (dest);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *ft_strjoin(char const *s1, char const *s2)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char *dest;
|
|
||||||
|
|
||||||
dest = NULL;
|
|
||||||
if (!(s1 && s2))
|
|
||||||
return (0);
|
|
||||||
i = ft_strlen(s1) + ft_strlen(s2);
|
|
||||||
dest = malloc(sizeof(char) * i + 1);
|
|
||||||
if (!dest)
|
|
||||||
return (NULL);
|
|
||||||
dest = ft_strcat_j(s1, s2, dest);
|
|
||||||
return (dest);
|
return (dest);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,35 +3,32 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strlcat.c :+: :+: :+: */
|
/* ft_strlcat.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/25 15:07:11 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:12:57 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/07 11:46:21 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/12 15:40:58 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
size_t ft_strlcat(char *dest, const char *src, size_t size)
|
size_t ft_strlcat(char *dst, const char *src, size_t size)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
size_t j;
|
size_t k;
|
||||||
size_t res;
|
|
||||||
|
|
||||||
j = 0;
|
if (!size)
|
||||||
i = ft_strlen(dest);
|
return (ft_strlen(src));
|
||||||
res = ft_strlen(src);
|
i = 0;
|
||||||
if (size <= i)
|
k = ft_strlen(dst);
|
||||||
res = size + res;
|
if (k >= size)
|
||||||
else
|
return (ft_strlen(src) + size);
|
||||||
res = i + res;
|
while (size - k - 1 && src[i])
|
||||||
while (src[j] && i + 1 < size)
|
|
||||||
{
|
{
|
||||||
dest[i] = src[j];
|
dst[k + i] = src[i];
|
||||||
i++;
|
i++;
|
||||||
j++;
|
size--;
|
||||||
}
|
}
|
||||||
if (i < size)
|
dst[k + i] = 0;
|
||||||
dest[i] = '\0';
|
return (k + ft_strlen(src));
|
||||||
return (res);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,32 +3,34 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strlcpy.c :+: :+: :+: */
|
/* ft_strlcpy.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/22 15:43:29 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:13:07 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/03 12:34:12 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:38:48 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
size_t ft_strlcpy(char *dest, const char *src, size_t size)
|
size_t ft_strlcpy(char *dst, const char *src, size_t size)
|
||||||
{
|
{
|
||||||
size_t i;
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
if (!size)
|
j = 0;
|
||||||
return (ft_strlen(src));
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if (!dest && !src)
|
if (!src || !dst)
|
||||||
return (0);
|
return (0);
|
||||||
if (size == 0)
|
while (src[i])
|
||||||
return (0);
|
|
||||||
while (src[i] && i < size - 1)
|
|
||||||
{
|
|
||||||
dest[i] = src[i];
|
|
||||||
i++;
|
i++;
|
||||||
|
if (!size)
|
||||||
|
return (i);
|
||||||
|
while (size - 1 && src[j] != 0)
|
||||||
|
{
|
||||||
|
dst[j] = src[j];
|
||||||
|
j++;
|
||||||
|
size--;
|
||||||
}
|
}
|
||||||
if (size > 0)
|
dst[j] = 0;
|
||||||
dest[i] = '\0';
|
return (i);
|
||||||
return (ft_strlen(src));
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,21 +3,23 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strlen.c :+: :+: :+: */
|
/* ft_strlen.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/03 12:31:43 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:13:19 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/03 12:32:28 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 21:12:05 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
size_t ft_strlen(const char *str)
|
size_t ft_strlen(const char *s)
|
||||||
{
|
{
|
||||||
int i;
|
size_t i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (str[i])
|
if (!s)
|
||||||
|
return (0);
|
||||||
|
while (s[i] != 0)
|
||||||
i++;
|
i++;
|
||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,36 +3,30 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strmapi.c :+: :+: :+: */
|
/* ft_strmapi.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/25 16:09:10 by sadjigui #+# #+# */
|
/* Created: 2020/12/08 19:03:09 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/03 16:14:11 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:39:05 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
char *ft_strmapi(const char *s, char (*f)(unsigned int, char))
|
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
|
||||||
{
|
{
|
||||||
unsigned int i;
|
|
||||||
char *dest;
|
char *dest;
|
||||||
int a;
|
int i;
|
||||||
|
|
||||||
if (!s)
|
if (!s)
|
||||||
return (0);
|
return (0);
|
||||||
a = ft_strlen(s);
|
|
||||||
dest = NULL;
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if (!s || !f)
|
dest = (char *)ft_calloc(ft_strlen(s) + 1, sizeof(char));
|
||||||
return (NULL);
|
|
||||||
dest = malloc(sizeof(char) * a + 1);
|
|
||||||
if (!dest)
|
if (!dest)
|
||||||
return (NULL);
|
return (0);
|
||||||
while (s[i])
|
while (s[i])
|
||||||
{
|
{
|
||||||
dest[i] = (*f)(i, (char)s[i]);
|
dest[i] = f(i, s[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
dest[i] = 0;
|
|
||||||
return (dest);
|
return (dest);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strncmp.c :+: :+: :+: */
|
/* ft_strncmp.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/03 12:29:18 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:13:31 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/09 12:28:28 by sadjigui ### ########.fr */
|
/* Updated: 2022/02/14 00:27:01 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,13 +14,30 @@
|
|||||||
|
|
||||||
int ft_strncmp(const char *s1, const char *s2, size_t n)
|
int ft_strncmp(const char *s1, const char *s2, size_t n)
|
||||||
{
|
{
|
||||||
size_t i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while ((s1[i] || s2[i]) && i < n)
|
while (n && (s1[i] || s2[i]))
|
||||||
{
|
{
|
||||||
if (!(s1[i] == s2[i]))
|
if (s1[i] != s2[i])
|
||||||
return ((unsigned char)s1[i] - s2[i]);
|
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
|
||||||
|
n--;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_strcmp(const char *s1, const char *s2)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (!s1 || !s2)
|
||||||
|
return (1);
|
||||||
|
while (s1[i] || s2[i])
|
||||||
|
{
|
||||||
|
if (s1[i] != s2[i])
|
||||||
|
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
@ -3,35 +3,37 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strnstr.c :+: :+: :+: */
|
/* ft_strnstr.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/25 14:58:49 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:13:42 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/25 16:30:00 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:39:28 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
char *ft_strnstr(const char *str, const char *to_find, size_t size)
|
char *ft_strnstr(const char *big, const char *little, size_t len)
|
||||||
{
|
{
|
||||||
size_t i;
|
int i;
|
||||||
size_t j;
|
int j;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
j = 0;
|
if (!little[i])
|
||||||
if (*to_find == '\0')
|
return ((char *)big);
|
||||||
return ((char *)str);
|
if (!little[i])
|
||||||
while (i < size && str[i])
|
return ((char *)big);
|
||||||
|
while (big[i] && len - i)
|
||||||
{
|
{
|
||||||
if ((i + j) > size)
|
j = 0;
|
||||||
return (NULL);
|
if (little[j] != big[i])
|
||||||
if (to_find[j] == '\0')
|
i++;
|
||||||
return ((char *)&str[i]);
|
|
||||||
if (str[i + j] == to_find[j])
|
|
||||||
j++;
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
j = 0;
|
while (little[j] == big[i + j] && little[j] && len - i - j)
|
||||||
|
j++;
|
||||||
|
if (little[j] == 0)
|
||||||
|
return ((char *)&big[i]);
|
||||||
|
else
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,27 +3,33 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strrchr.c :+: :+: :+: */
|
/* ft_strrchr.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/20 13:38:42 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:13:52 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/09 12:59:30 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:39:37 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
char *ft_strrchr(const char *str, int c)
|
char *ft_strrchr(const char *s, int c)
|
||||||
{
|
{
|
||||||
int i;
|
char *str;
|
||||||
|
char *last;
|
||||||
|
|
||||||
i = ft_strlen(str) - 1;
|
str = (char *)s;
|
||||||
if (c == '\0')
|
if (c == 0)
|
||||||
return ((char *)&(str[i + 1]));
|
|
||||||
while (i >= 0)
|
|
||||||
{
|
{
|
||||||
if (str[i] == (char)c)
|
while (*str)
|
||||||
return ((char *)&(str[i]));
|
str++;
|
||||||
i--;
|
return (str);
|
||||||
}
|
}
|
||||||
return (0);
|
last = 0;
|
||||||
|
while (*str)
|
||||||
|
{
|
||||||
|
if (*str == c)
|
||||||
|
last = str;
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
return (last);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,28 +3,70 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strtrim.c :+: :+: :+: */
|
/* ft_strtrim.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/03 12:17:01 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 23:52:05 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/09 13:45:13 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 11:40:17 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
|
static int is_set(char const *set, char c)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (set[i])
|
||||||
|
{
|
||||||
|
if (set[i] == c)
|
||||||
|
return (1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int len_dest(const char *s1, char const *set)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
int j;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
len = ft_strlen(s1);
|
||||||
|
while (is_set(set, s1[i]))
|
||||||
|
i++;
|
||||||
|
while (is_set(set, s1[len - j - 1]))
|
||||||
|
j++;
|
||||||
|
len = len - j - i;
|
||||||
|
if (len < 0)
|
||||||
|
len = 0;
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
|
||||||
char *ft_strtrim(char const *s1, char const *set)
|
char *ft_strtrim(char const *s1, char const *set)
|
||||||
{
|
{
|
||||||
char *str;
|
int j;
|
||||||
size_t j;
|
int i;
|
||||||
|
int len;
|
||||||
|
char *dest;
|
||||||
|
|
||||||
if (!s1 || !set)
|
i = 0;
|
||||||
return (NULL);
|
j = 0;
|
||||||
while (*s1 && ft_strchr(set, *s1))
|
if (!s1)
|
||||||
s1++;
|
return (0);
|
||||||
j = ft_strlen((char *)s1);
|
len = len_dest(s1, set);
|
||||||
while (j && ft_strchr(set, s1[j]))
|
dest = ft_calloc(len + 1, 1);
|
||||||
j--;
|
if (!dest)
|
||||||
++j;
|
return (0);
|
||||||
str = ft_substr(s1, 0, j);
|
while (is_set(set, s1[i]))
|
||||||
return (str);
|
i++;
|
||||||
|
while (s1[i] && len - j && len > 0)
|
||||||
|
{
|
||||||
|
dest[j] = s1[i];
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
return (dest);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_substr.c :+: :+: :+: */
|
/* ft_substr.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/03 12:11:04 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 22:54:40 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/07 11:33:45 by sadjigui ### ########.fr */
|
/* Updated: 2020/12/16 16:45:34 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,25 +14,26 @@
|
|||||||
|
|
||||||
char *ft_substr(char const *s, unsigned int start, size_t len)
|
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||||
{
|
{
|
||||||
char *str;
|
char *dest;
|
||||||
size_t i;
|
unsigned int i;
|
||||||
size_t j;
|
|
||||||
|
|
||||||
i = -1;
|
|
||||||
if (!s)
|
if (!s)
|
||||||
return (NULL);
|
return (0);
|
||||||
j = ft_strlen(s);
|
i = 0;
|
||||||
if (!len || j <= start)
|
dest = malloc(1 * len + 1);
|
||||||
return (ft_strdup(""));
|
if (dest == 0)
|
||||||
str = (char *)malloc(sizeof(char) * len + 1);
|
return (0);
|
||||||
if (!str)
|
if (start > ft_strlen(s) || len == 0)
|
||||||
return (NULL);
|
|
||||||
j = 0;
|
|
||||||
while (s[++i] && j < len)
|
|
||||||
{
|
{
|
||||||
if (i >= start)
|
dest[i] = 0;
|
||||||
str[j++] = s[i];
|
return (dest);
|
||||||
}
|
}
|
||||||
str[j] = '\0';
|
while (len && s[i + start])
|
||||||
return (str);
|
{
|
||||||
|
dest[i] = s[i + start];
|
||||||
|
len--;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
dest[i] = 0;
|
||||||
|
return (dest);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,18 +3,18 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_tolower.c :+: :+: :+: */
|
/* ft_tolower.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/25 16:34:58 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:14:05 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/25 16:35:45 by sadjigui ### ########.fr */
|
/* Updated: 2020/11/29 17:20:51 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
int ft_tolower(int i)
|
int ft_tolower(int c)
|
||||||
{
|
{
|
||||||
if (i >= 'A' && i <= 'Z')
|
if (c >= 65 && c <= 90)
|
||||||
i += 32;
|
c = c + 32;
|
||||||
return (i);
|
return (c);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,18 +3,18 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_toupper.c :+: :+: :+: */
|
/* ft_toupper.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/25 16:36:03 by sadjigui #+# #+# */
|
/* Created: 2020/11/29 00:14:15 by apommier #+# #+# */
|
||||||
/* Updated: 2021/05/25 16:36:32 by sadjigui ### ########.fr */
|
/* Updated: 2020/11/29 17:21:12 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
int ft_toupper(int i)
|
int ft_toupper(int c)
|
||||||
{
|
{
|
||||||
if (i >= 'a' && i <= 'z')
|
if (c >= 'a' && c <= 'z')
|
||||||
i -= 32;
|
c = c - 32;
|
||||||
return (i);
|
return (c);
|
||||||
}
|
}
|
||||||
|
|||||||
95
libft/get_next_line.c
Normal file
95
libft/get_next_line.c
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: kinou <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/10/23 07:55:17 by kinou #+# #+# */
|
||||||
|
/* Updated: 2021/11/01 10:21:35 by kinou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_free(char *save, int *end)
|
||||||
|
{
|
||||||
|
free(save);
|
||||||
|
free(end);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *set_line(char *line, char *save)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
line = ft_strjoin(save, 0);
|
||||||
|
while (line[i] && line[i] != '\n')
|
||||||
|
i++;
|
||||||
|
if (line[i] == '\n')
|
||||||
|
{
|
||||||
|
while (line[i++])
|
||||||
|
line[i] = '\0';
|
||||||
|
}
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *set_save(char *save)
|
||||||
|
{
|
||||||
|
char *delete;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
delete = save;
|
||||||
|
while (save[i] && save[i] != '\n')
|
||||||
|
i++;
|
||||||
|
if (save[i] != '\n')
|
||||||
|
i = 0;
|
||||||
|
save = ft_strjoin((save + i + 1), 0);
|
||||||
|
free(delete);
|
||||||
|
return (save);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *next_line(char *save, int *end, int fd)
|
||||||
|
{
|
||||||
|
char *delete;
|
||||||
|
char *dest;
|
||||||
|
|
||||||
|
while (!ft_strchr(save, '\n') && *end > 0)
|
||||||
|
{
|
||||||
|
dest = ft_calloc(1, 1 + 1);
|
||||||
|
*end = read(fd, dest, 1);
|
||||||
|
delete = save;
|
||||||
|
save = ft_strjoin(save, dest);
|
||||||
|
free(delete);
|
||||||
|
free(dest);
|
||||||
|
}
|
||||||
|
return (save);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_next_line(int fd)
|
||||||
|
{
|
||||||
|
char *save;
|
||||||
|
int *end;
|
||||||
|
char *line;
|
||||||
|
|
||||||
|
save = 0;
|
||||||
|
line = 0;
|
||||||
|
if (fd < 0)
|
||||||
|
return (0);
|
||||||
|
end = malloc(sizeof(int *));
|
||||||
|
*end = 1;
|
||||||
|
save = ft_calloc(1, 1);
|
||||||
|
save = next_line(save, end, fd);
|
||||||
|
line = set_line(line, save);
|
||||||
|
if (ft_strlen(line) > 0)
|
||||||
|
{
|
||||||
|
save = set_save(save);
|
||||||
|
save = ft_free(save, end);
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
|
free(line);
|
||||||
|
save = ft_free(save, end);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
@ -1,18 +1,19 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_putchar.c :+: :+: :+: */
|
/* get_next_line.h :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/12/08 19:22:52 by sadjigui #+# #+# */
|
/* Created: 2020/12/14 05:58:49 by apommier #+# #+# */
|
||||||
/* Updated: 2021/12/08 19:22:56 by sadjigui ### ########.fr */
|
/* Updated: 2022/01/17 21:45:15 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#ifndef GET_NEXT_LINE_H
|
||||||
|
# define GET_NEXT_LINE_H
|
||||||
|
|
||||||
void ft_putchar(int c)
|
char *get_next_line(int fd);
|
||||||
{
|
char *ft_strjoin(char *save, char *s2);
|
||||||
write(1, &c, 1);
|
|
||||||
}
|
#endif
|
||||||
41
libft/get_next_line_utils.c
Normal file
41
libft/get_next_line_utils.c
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2020/12/14 06:05:54 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/01/20 21:58:39 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_strjoin(char *save, char *s2)
|
||||||
|
{
|
||||||
|
char *dest;
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
if (!save && !s2)
|
||||||
|
return (0);
|
||||||
|
dest = malloc(ft_strlen(save) + ft_strlen(s2) + 1);
|
||||||
|
while (save && save[i])
|
||||||
|
{
|
||||||
|
dest[j] = save[i];
|
||||||
|
j++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
while (s2 && s2[i])
|
||||||
|
{
|
||||||
|
dest[j] = s2[i];
|
||||||
|
j++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
dest[j] = 0;
|
||||||
|
return (dest);
|
||||||
|
}
|
||||||
@ -3,72 +3,75 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* libft.h :+: :+: :+: */
|
/* libft.h :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/06/07 12:34:41 by sadjigui #+# #+# */
|
/* Created: 2020/12/11 03:21:27 by apommier #+# #+# */
|
||||||
/* Updated: 2022/02/26 16:46:45 by sadjigui ### ########.fr */
|
/* Updated: 2022/04/19 08:44:37 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef LIBFT_H
|
#ifndef LIBFT_H
|
||||||
# define LIBFT_H
|
# define LIBFT_H
|
||||||
|
|
||||||
# include <unistd.h>
|
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
# include "get_next_line.h"
|
||||||
|
|
||||||
typedef struct s_list
|
typedef struct t_slist
|
||||||
{
|
{
|
||||||
void *content;
|
void *nbr;
|
||||||
struct s_list *next;
|
int index;
|
||||||
|
int swap;
|
||||||
|
struct t_slist *next;
|
||||||
} t_list;
|
} t_list;
|
||||||
|
|
||||||
void ft_putchar(int c);
|
void *ft_memset(void *s, int c, size_t n);
|
||||||
void ft_putstr(char const *str);
|
void ft_bzero(void *s, size_t n);
|
||||||
|
void *ft_memcpy(void *dest, const void *src, size_t n);
|
||||||
|
void *ft_memccpy(void *dest, const void *src, int c, size_t n);
|
||||||
|
void *ft_memmove(void *dest, const void *src, size_t n);
|
||||||
|
void *ft_memchr(const void *s, int c, size_t n);
|
||||||
|
int ft_memcmp(const void *s1, const void *s2, size_t n);
|
||||||
|
size_t ft_strlen(const char *s);
|
||||||
|
int ft_isalpha(int c);
|
||||||
|
int ft_isdigit(int c);
|
||||||
|
int ft_isalnum(int c);
|
||||||
|
int ft_isascii(int c);
|
||||||
|
int ft_isprint(int c);
|
||||||
|
int ft_toupper(int c);
|
||||||
|
int ft_tolower(int c);
|
||||||
|
char *ft_strchr(const char *s, int c);
|
||||||
|
char *ft_strrchr(const char *s, int c);
|
||||||
int ft_strcmp(const char *s1, const char *s2);
|
int ft_strcmp(const char *s1, const char *s2);
|
||||||
long ft_atoi(const char *str);
|
int ft_strncmp(const char *s1, const char *s2, size_t n);
|
||||||
void ft_bzero(void *str, size_t n);
|
size_t ft_strlcpy(char *dst, const char *src, size_t size);
|
||||||
void *ft_calloc(size_t count, size_t size);
|
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
||||||
int ft_isalnum(int i);
|
char *ft_strjoin(char *save, char *s2);
|
||||||
int ft_isalpha(int i);
|
char *ft_strnstr(const char *big, const char *little, size_t len);
|
||||||
int ft_isascii(int i);
|
long long ft_atoi(const char *nptr);
|
||||||
int ft_isdigit(int i);
|
void *ft_calloc(size_t nmenb, size_t size);
|
||||||
int ft_isprint(int i);
|
char *ft_strdup(const char *s);
|
||||||
|
|
||||||
|
char *ft_substr(char const *s, unsigned int start, size_t len);
|
||||||
|
char *ft_strtrim(char const *s1, char const *set);
|
||||||
|
char **ft_split(char const *s, char c);
|
||||||
char *ft_itoa(int n);
|
char *ft_itoa(int n);
|
||||||
void *ft_memccpy(void *dest, const void *src, int ch, size_t maxSize);
|
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||||
void *ft_memcpy(void *destination, const void *source, size_t size);
|
|
||||||
void *ft_memmove(void *dest, const void *src, size_t size);
|
|
||||||
void *ft_memchr(const void *mem, int ch, size_t size);
|
|
||||||
void *ft_memset(void *pointer, int value, size_t count);
|
|
||||||
int ft_memcmp(const void *p1, const void *p2, size_t size);
|
|
||||||
void ft_putchar_fd(char c, int fd);
|
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_putendl_fd(char *s, int fd);
|
||||||
void ft_putnbr_fd(int n, int fd);
|
void ft_putnbr_fd(int n, int fd);
|
||||||
void ft_putstr_fd(char *s, int fd);
|
|
||||||
char **ft_split(char const *s, char c);
|
|
||||||
char *ft_strdup(const char *src);
|
|
||||||
char *ft_strjoin(char const *s1, char const *s2);
|
|
||||||
size_t ft_strlcpy(char *dest, const char *src, size_t size);
|
|
||||||
size_t ft_strlen(const char *str);
|
|
||||||
int ft_strncmp(const char *s1, const char *s2, size_t n);
|
|
||||||
char *ft_strtrim(char const *s1, char const *set);
|
|
||||||
char *ft_substr(char const *s, unsigned int start, size_t len);
|
|
||||||
int ft_tolower(int i);
|
|
||||||
int ft_toupper(int i);
|
|
||||||
char *ft_strchr(const char *str, int c);
|
|
||||||
char *ft_strrchr(const char *str, int c);
|
|
||||||
size_t ft_strlcpy(char *dest, const char *src, size_t size);
|
|
||||||
size_t ft_strlcat(char *dest, const char *src, size_t size);
|
|
||||||
char *ft_strmapi(const char *s, char (*f)(unsigned int, char));
|
|
||||||
char *ft_strnstr(const char *str, const char *to_find, size_t size);
|
|
||||||
|
|
||||||
t_list *ft_lstnew(void *content);
|
t_list *ft_lstnew(void *content);
|
||||||
void ft_lstadd_front(t_list **alst, t_list *new);
|
void ft_lstadd_front(t_list **alst, t_list *new);
|
||||||
int ft_lstsize(t_list *lst);
|
int ft_lstsize(t_list *lst);
|
||||||
t_list *ft_lstlast(t_list *lst);
|
t_list *ft_lstlast(t_list *lst);
|
||||||
void ft_lstadd_back(t_list **alst, t_list *new);
|
void ft_lstadd_back(t_list **alst, t_list *new);
|
||||||
void ft_lstdelone(t_list *lst, void (*del)(void*));
|
void ft_lstdelone(t_list *lst, void (*del)(void *));
|
||||||
void ft_lstclear(t_list **lst, void (*del)(void *));
|
void ft_lstclear(t_list **lst, void (*del)(void *));
|
||||||
void ft_lstiter(t_list *lst, void (*f)(void *));
|
void ft_lstiter(t_list *lst, void (*f)(void *));
|
||||||
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
|
t_list *ft_lstmap(t_list *lst, void *(*f)(void *),
|
||||||
|
void (*del)(void *));
|
||||||
|
t_list *ft_lstbeforelast(t_list *lst);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
104
srcs/built_in/cd.c
Normal file
104
srcs/built_in/cd.c
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* cd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/21 18:30:26 by sadjigui #+# #+# */
|
||||||
|
/* Updated: 2022/04/23 13:10:35 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
void add_one(t_s_cmd *cmd)
|
||||||
|
{
|
||||||
|
char p[1024];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = find_it(cmd->big_cmd->env, "PWD");
|
||||||
|
getcwd(p, sizeof(p));
|
||||||
|
free(cmd->big_cmd->env[i]);
|
||||||
|
cmd->big_cmd->env[i] = ft_strjoin("PWD=", p);
|
||||||
|
}
|
||||||
|
|
||||||
|
void change_path(t_s_cmd *cmd)
|
||||||
|
{
|
||||||
|
int old;
|
||||||
|
int pwd;
|
||||||
|
|
||||||
|
if (find_it(cmd->big_cmd->env, "OLDPWD") != -1)
|
||||||
|
{
|
||||||
|
pwd = find_it(cmd->big_cmd->env, "PWD");
|
||||||
|
old = find_it(cmd->big_cmd->env, "OLDPWD");
|
||||||
|
free(cmd->big_cmd->env[old]);
|
||||||
|
cmd->big_cmd->env[old] = ft_strjoin("OLD", cmd->big_cmd->env[pwd]);
|
||||||
|
}
|
||||||
|
add_one(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_home(t_s_cmd *cmd, char *p, char **env)
|
||||||
|
{
|
||||||
|
int home;
|
||||||
|
int len_home;
|
||||||
|
int old_pwd;
|
||||||
|
int pwd;
|
||||||
|
|
||||||
|
home = find_it(env, "HOME");
|
||||||
|
len_home = ft_strlen(env[home]);
|
||||||
|
old_pwd = find_it(env, "OLDPWD");
|
||||||
|
pwd = find_it(env, "PWD");
|
||||||
|
p = ft_substr(env[home], 5, ft_strlen(env[len_home]));
|
||||||
|
if (chdir(p) == 0)
|
||||||
|
{
|
||||||
|
change_oldpwd(env, old_pwd, pwd, 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)
|
||||||
|
{
|
||||||
|
if (chdir(cmd->args[1]) == 0)
|
||||||
|
{
|
||||||
|
if (find_it(cmd->big_cmd->env, "PWD") != -1)
|
||||||
|
change_path(cmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Minishell: cd: ", 2);
|
||||||
|
ft_putstr_fd(cmd->args[1], 2);
|
||||||
|
ft_putstr_fd(": No such directory\n", 2);
|
||||||
|
return (check_return(cmd, 1));
|
||||||
|
}
|
||||||
|
return (check_return(cmd, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
int open_directory(t_s_cmd *cmd)
|
||||||
|
{
|
||||||
|
char *p;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
p = NULL;
|
||||||
|
i = 0;
|
||||||
|
if (cmd->nb_args > 2)
|
||||||
|
return (cd_error(cmd, "Minishell: cd: too many arguments", 1));
|
||||||
|
if (!cmd->args[1])
|
||||||
|
{
|
||||||
|
if (find_it(cmd->big_cmd->env, "HOME") < 0)
|
||||||
|
return (cd_error(cmd, "Minishell: cd: HOME not set", 1));
|
||||||
|
return (check_home(cmd, p, cmd->big_cmd->env));
|
||||||
|
}
|
||||||
|
else if (cmd->nb_args == 2)
|
||||||
|
i = check_dir(cmd);
|
||||||
|
return (check_return(cmd, i));
|
||||||
|
}
|
||||||
@ -1,36 +1,27 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* parser.c :+: :+: :+: */
|
/* cd_utils.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/02/28 17:12:52 by sadjigui #+# #+# */
|
/* Created: 2022/04/20 15:35:02 by sadjigui #+# #+# */
|
||||||
/* Updated: 2022/03/06 15:33:06 by sadjigui ### ########.fr */
|
/* Updated: 2022/04/20 16:07:58 by sadjigui ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/mini.h"
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
void print_env(t_struct *data)
|
void change_oldpwd(char **env, int old_pwd, int pwd, char *p)
|
||||||
{
|
{
|
||||||
int i;
|
if (find_it(env, "PWD") != -1)
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (data->envy[i])
|
|
||||||
{
|
{
|
||||||
printf("%s\n", data->envy[i]);
|
if (find_it(env, "OLDPWD") != -1)
|
||||||
i++;
|
{
|
||||||
|
free(env[old_pwd]);
|
||||||
|
env[old_pwd] = ft_strjoin("OLD", env[pwd]);
|
||||||
|
}
|
||||||
|
free(env[pwd]);
|
||||||
|
env[pwd] = ft_strjoin("PWD=", p);
|
||||||
}
|
}
|
||||||
// printf("ici\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void lexer(char *input, t_struct *data)
|
|
||||||
{
|
|
||||||
if (ft_strcmp(input, "exit") == 0)
|
|
||||||
ft_exit(data);
|
|
||||||
if (ft_strcmp(input, "env") == 0)
|
|
||||||
print_env(data);
|
|
||||||
if (ft_strncmp(input, "export", ft_strlen("export")) == 0)
|
|
||||||
ft_export(input, data);
|
|
||||||
}
|
}
|
||||||
54
srcs/built_in/choose_builtin.c
Normal file
54
srcs/built_in/choose_builtin.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* choose_builtin.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/02/28 17:12:52 by sadjigui #+# #+# */
|
||||||
|
/* Updated: 2022/04/20 12:46:41 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
int is_builtin(char *cmd)
|
||||||
|
{
|
||||||
|
if (!ft_strcmp(cmd, "exit"))
|
||||||
|
return (1);
|
||||||
|
if (!ft_strcmp(cmd, "env"))
|
||||||
|
return (1);
|
||||||
|
if (!ft_strcmp(cmd, "export"))
|
||||||
|
return (1);
|
||||||
|
if (!ft_strcmp(cmd, "unset"))
|
||||||
|
return (1);
|
||||||
|
if (!ft_strcmp(cmd, "echo"))
|
||||||
|
return (1);
|
||||||
|
if (!ft_strcmp(cmd, "cd"))
|
||||||
|
return (1);
|
||||||
|
if (!ft_strcmp(cmd, "pwd"))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void call_builtin(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
dup2(cmd->current_s_cmd->fd[0], 0);
|
||||||
|
dup2(cmd->current_s_cmd->fd[1], 1);
|
||||||
|
close(cmd->current_s_cmd->fd[0]);
|
||||||
|
close(cmd->current_s_cmd->fd[1]);
|
||||||
|
if (!ft_strcmp(cmd->current_s_cmd->cmd, "exit"))
|
||||||
|
ft_exit(cmd->current_s_cmd);
|
||||||
|
if (!ft_strcmp(cmd->current_s_cmd->cmd, "env"))
|
||||||
|
print_env(cmd);
|
||||||
|
if (!ft_strcmp(cmd->current_s_cmd->cmd, "export"))
|
||||||
|
ft_export(cmd->current_s_cmd);
|
||||||
|
if (!ft_strcmp(cmd->current_s_cmd->cmd, "unset"))
|
||||||
|
ft_unset(cmd->current_s_cmd);
|
||||||
|
if (!ft_strcmp(cmd->current_s_cmd->cmd, "echo"))
|
||||||
|
ft_echo(cmd->current_s_cmd);
|
||||||
|
if (!ft_strcmp(cmd->current_s_cmd->cmd, "cd"))
|
||||||
|
open_directory(cmd->current_s_cmd);
|
||||||
|
if (!ft_strcmp(cmd->current_s_cmd->cmd, "pwd"))
|
||||||
|
ft_pwd(cmd->current_s_cmd);
|
||||||
|
}
|
||||||
50
srcs/built_in/echo.c
Normal file
50
srcs/built_in/echo.c
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* echo.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: syd <syd@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/11 20:52:56 by sadjigui #+# #+# */
|
||||||
|
/* Updated: 2022/04/19 17:09:24 by syd ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
void echo_print(t_s_cmd *d, int i)
|
||||||
|
{
|
||||||
|
while (d->args[i])
|
||||||
|
{
|
||||||
|
printf("%s", d->args[i]);
|
||||||
|
if (d->args[i + 1] != NULL)
|
||||||
|
printf(" ");
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_echo(t_s_cmd *d)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
size_t j;
|
||||||
|
int is_option;
|
||||||
|
|
||||||
|
i = 1;
|
||||||
|
is_option = 0;
|
||||||
|
while (d->args[i] && d->args[i][0] == '-')
|
||||||
|
{
|
||||||
|
j = 1;
|
||||||
|
while (d->args[i][j] && d->args[i][j] == 'n')
|
||||||
|
j++;
|
||||||
|
if (j == ft_strlen(d->args[i]))
|
||||||
|
is_option = 1;
|
||||||
|
else
|
||||||
|
break ;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (d->args[i])
|
||||||
|
echo_print(d, i);
|
||||||
|
if (is_option == 0)
|
||||||
|
printf("\n");
|
||||||
|
return (check_return(d, 0));
|
||||||
|
}
|
||||||
51
srcs/built_in/env.c
Normal file
51
srcs/built_in/env.c
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/04/09 21:09:47 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/19 13:22:34 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
int print_env(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (cmd->current_s_cmd->nb_args > 1)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Minishell: env: too many arguments\n", 2);
|
||||||
|
return (check_return(cmd->current_s_cmd, 1));
|
||||||
|
}
|
||||||
|
else if (cmd->env)
|
||||||
|
{
|
||||||
|
while (cmd->env[i])
|
||||||
|
{
|
||||||
|
if (ft_strchr(cmd->env[i], '='))
|
||||||
|
ft_putendl_fd(cmd->env[i], 1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (check_return(cmd->current_s_cmd, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_pwd(t_s_cmd *cmd)
|
||||||
|
{
|
||||||
|
char p[1024];
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
str = getcwd(p, sizeof(p));
|
||||||
|
if (!str)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Minishell: pwd: Not found\n", 2);
|
||||||
|
return (check_return(cmd, 1));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ft_putendl_fd(p, 1);
|
||||||
|
return (check_return(cmd, 0));
|
||||||
|
}
|
||||||
99
srcs/built_in/exit.c
Normal file
99
srcs/built_in/exit.c
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* exit.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/04/20 17:53:35 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/20 18:14:27 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#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)
|
||||||
|
{
|
||||||
|
unsigned long long long_max;
|
||||||
|
|
||||||
|
long_max = 9223372036854775807;
|
||||||
|
if (ft_strlen(nbr) > 20)
|
||||||
|
return (1);
|
||||||
|
if (nbr[0] == '-')
|
||||||
|
{
|
||||||
|
if (ft_atoi_long(nbr + 1) > long_max + 1)
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
else if (ft_atoi_long(nbr) > long_max)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void exit_error(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Minishell: exit: numeric argument required\n", 2);
|
||||||
|
exit_shell(cmd, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_exit_args(t_s_cmd *cmd)
|
||||||
|
{
|
||||||
|
if (cmd->nb_args > 2)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Minishell: exit: too many arguments\n", 2);
|
||||||
|
if (cmd->child)
|
||||||
|
cmd->big_cmd->err_var = 1;
|
||||||
|
else
|
||||||
|
exit(1);
|
||||||
|
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)
|
||||||
|
exit_shell(cmd->big_cmd, 0);
|
||||||
|
while (cmd->args[1][i] == ' ')
|
||||||
|
i++;
|
||||||
|
if (cmd->args[1][i] == '-')
|
||||||
|
i++;
|
||||||
|
while (cmd->args[1][i])
|
||||||
|
{
|
||||||
|
if (!ft_isdigit(cmd->args[1][i++]))
|
||||||
|
exit_error(cmd->big_cmd);
|
||||||
|
}
|
||||||
|
if (max_long(cmd->args[1]))
|
||||||
|
exit_error(cmd->big_cmd);
|
||||||
|
exit_shell(cmd->big_cmd, ft_atoi(cmd->args[1]));
|
||||||
|
}
|
||||||
84
srcs/built_in/export.c
Normal file
84
srcs/built_in/export.c
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* export.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/04/20 00:31:25 by sadjigui #+# #+# */
|
||||||
|
/* Updated: 2022/04/20 00:31:35 by sadjigui ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
int check_variable(char *variable)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (!ft_isalpha(variable[i]) && variable[i] != '_')
|
||||||
|
return (1);
|
||||||
|
i++;
|
||||||
|
while (variable[i] && variable[i] != '=')
|
||||||
|
{
|
||||||
|
if (!ft_isalnum(variable[i]) && variable[i] != '_')
|
||||||
|
return (1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int reatribute_variable(t_s_cmd *cmd, int index, char *dest, char *unset)
|
||||||
|
{
|
||||||
|
free (cmd->big_cmd->env[index]);
|
||||||
|
cmd->big_cmd->env[index] = dest;
|
||||||
|
free(unset);
|
||||||
|
return (check_return(cmd, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_export_variable(t_s_cmd *cmd, char *variable)
|
||||||
|
{
|
||||||
|
char *dest;
|
||||||
|
char *unset;
|
||||||
|
int index;
|
||||||
|
|
||||||
|
if (check_variable(variable) == 1)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Minishell: export: `", 2);
|
||||||
|
ft_putstr_fd(variable, 2);
|
||||||
|
ft_putstr_fd("': not a valid identifier\n", 2);
|
||||||
|
return (check_return(cmd, 1));
|
||||||
|
}
|
||||||
|
dest = ft_strdup(variable);
|
||||||
|
unset = ft_substr(dest, 0, find_len(dest, 0, '='));
|
||||||
|
index = find_it(cmd->big_cmd->env, unset);
|
||||||
|
if (index != -1)
|
||||||
|
return (reatribute_variable(cmd, index, dest, unset));
|
||||||
|
register_env(cmd, dest);
|
||||||
|
if (dest)
|
||||||
|
free(dest);
|
||||||
|
if (unset)
|
||||||
|
free(unset);
|
||||||
|
return (check_return(cmd, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_export(t_s_cmd *cmd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
i = 1;
|
||||||
|
j = 0;
|
||||||
|
if (!cmd->args[i])
|
||||||
|
lone_export(cmd);
|
||||||
|
else if (cmd->args[i])
|
||||||
|
{
|
||||||
|
while (cmd->args[i])
|
||||||
|
{
|
||||||
|
j = ft_export_variable(cmd, cmd->args[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (j);
|
||||||
|
}
|
||||||
81
srcs/built_in/export2.c
Normal file
81
srcs/built_in/export2.c
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* export2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/07 17:58:25 by sadjigui #+# #+# */
|
||||||
|
/* Updated: 2022/04/22 14:54:54 by sadjigui ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
void ft_swap(char **a, char **b)
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
|
tmp = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort_tab(char **av)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int x;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (av[i])
|
||||||
|
{
|
||||||
|
x = i;
|
||||||
|
while (av[x])
|
||||||
|
{
|
||||||
|
if (ft_strcmp(av[i], av[x]) > 0)
|
||||||
|
ft_swap(&av[i], &av[x]);
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_export(char *tmp)
|
||||||
|
{
|
||||||
|
char *str1;
|
||||||
|
char *str2;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
str1 = ft_substr(tmp, 0, find_len(tmp, i, '='));
|
||||||
|
i = find_len(tmp, 0, '=') + 1;
|
||||||
|
str2 = ft_substr(tmp, i, ft_strlen(tmp));
|
||||||
|
printf("declare -x %s", str1);
|
||||||
|
if (ft_strlen(tmp) != ft_strlen(str1))
|
||||||
|
{
|
||||||
|
printf("=\"");
|
||||||
|
if (next_space(str2, 0) != '\0')
|
||||||
|
{
|
||||||
|
printf("%s", str2);
|
||||||
|
}
|
||||||
|
printf("\"");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
free(str1);
|
||||||
|
free(str2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lone_export(t_s_cmd *cmd)
|
||||||
|
{
|
||||||
|
char **tmp;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
tmp = cmd->big_cmd->env;
|
||||||
|
sort_tab(tmp);
|
||||||
|
while (tmp[i])
|
||||||
|
{
|
||||||
|
print_export(tmp[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
91
srcs/built_in/init_builtin.c
Normal file
91
srcs/built_in/init_builtin.c
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* init_builtin.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/02/28 17:17:01 by sadjigui #+# #+# */
|
||||||
|
/* Updated: 2022/04/20 04:56:50 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
void ft_shlvl(char **env)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int index;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
index = find_it(env, "SHLVL");
|
||||||
|
if (index != -1)
|
||||||
|
{
|
||||||
|
while (env[index][i] && env[index][i] != '=')
|
||||||
|
i++;
|
||||||
|
str = ft_substr(env[index], i + 1, ft_strlen(env[index]));
|
||||||
|
i = ft_atoi(str);
|
||||||
|
free (str);
|
||||||
|
i += 1;
|
||||||
|
str = ft_itoa(i);
|
||||||
|
free (env[index]);
|
||||||
|
env[index] = ft_strjoin("SHLVL=", str);
|
||||||
|
free (str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void register_env(t_s_cmd *cmd, char *variable)
|
||||||
|
{
|
||||||
|
char **tmp;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
tmp = NULL;
|
||||||
|
tmp = (char **)malloc(sizeof(char *) * (tab_len(cmd->big_cmd->env) + 2));
|
||||||
|
if (!tmp)
|
||||||
|
return ;
|
||||||
|
while (cmd->big_cmd->env[i])
|
||||||
|
{
|
||||||
|
tmp[i] = ft_strdup(cmd->big_cmd->env[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
tmp[i] = ft_strdup(variable);
|
||||||
|
tmp[i + 1] = NULL;
|
||||||
|
free_double(cmd->big_cmd->env);
|
||||||
|
cmd->big_cmd->env = ft_dup_double(tmp);
|
||||||
|
if (tmp)
|
||||||
|
free_double(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_env(t_s_cmd *cmd, char **env)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (env[i])
|
||||||
|
i++;
|
||||||
|
cmd->big_cmd->env = ft_calloc(sizeof(char *), i + 1);
|
||||||
|
i = 0;
|
||||||
|
while (env[i])
|
||||||
|
{
|
||||||
|
cmd->big_cmd->env[i] = ft_strdup(env[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
cmd->big_cmd->env[i] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_s_cmd(t_s_cmd *cmd, char **env)
|
||||||
|
{
|
||||||
|
ft_env(cmd, env);
|
||||||
|
}
|
||||||
|
|
||||||
|
int tab_len(char **tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (tab[i])
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
78
srcs/built_in/unset.c
Normal file
78
srcs/built_in/unset.c
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* unset.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/11 18:26:29 by sadjigui #+# #+# */
|
||||||
|
/* Updated: 2022/04/23 13:09:17 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
void unset_variable(t_s_cmd *cmd, int i)
|
||||||
|
{
|
||||||
|
int a;
|
||||||
|
int b;
|
||||||
|
char **tmp;
|
||||||
|
|
||||||
|
a = 0;
|
||||||
|
b = 0;
|
||||||
|
tmp = malloc(sizeof(char *) * tab_len(cmd->big_cmd->env));
|
||||||
|
if (!tmp)
|
||||||
|
return ;
|
||||||
|
while (cmd->big_cmd->env[a])
|
||||||
|
{
|
||||||
|
if (a != i)
|
||||||
|
{
|
||||||
|
tmp[b] = ft_strdup(cmd->big_cmd->env[a]);
|
||||||
|
b++;
|
||||||
|
}
|
||||||
|
a++;
|
||||||
|
}
|
||||||
|
tmp[b] = NULL;
|
||||||
|
free_double(cmd->big_cmd->env);
|
||||||
|
cmd->big_cmd->env = ft_dup_double(tmp);
|
||||||
|
if (tmp)
|
||||||
|
free_double(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
int find_variable(char *variable, t_s_cmd *cmd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = find_it(cmd->big_cmd->env, variable);
|
||||||
|
if (check_variable(variable) == 1)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Minishell: unset: `", 2);
|
||||||
|
ft_putstr_fd(variable, 2);
|
||||||
|
ft_putstr_fd("': not a valid identifier\n", 2);
|
||||||
|
return (check_return(cmd, 1));
|
||||||
|
}
|
||||||
|
if (i == tab_len(cmd->big_cmd->env))
|
||||||
|
return (check_return(cmd, 2));
|
||||||
|
if (find_it(cmd->big_cmd->env, variable) != -1)
|
||||||
|
{
|
||||||
|
unset_variable(cmd, i);
|
||||||
|
return (check_return(cmd, 0));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return (check_return(cmd, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_unset(t_s_cmd *cmd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
i = 1;
|
||||||
|
j = 0;
|
||||||
|
while (cmd->args[i])
|
||||||
|
{
|
||||||
|
j = find_variable(cmd->args[i], cmd);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (j);
|
||||||
|
}
|
||||||
73
srcs/built_in/utils_builtin.c
Normal file
73
srcs/built_in/utils_builtin.c
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils_builtin.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/16 15:04:12 by sadjigui #+# #+# */
|
||||||
|
/* Updated: 2022/04/20 04:42:35 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
int find_len(char *input, int i, char c)
|
||||||
|
{
|
||||||
|
int j;
|
||||||
|
|
||||||
|
j = i;
|
||||||
|
while (input[j] && input[j] != c)
|
||||||
|
j++;
|
||||||
|
return (j);
|
||||||
|
}
|
||||||
|
|
||||||
|
int find_it(char **str, char *s)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i] && (ft_strncmp(str[i], s, ft_strlen(s))
|
||||||
|
|| (!ft_strncmp(str[i], s, ft_strlen(s))
|
||||||
|
&& (str[i][ft_strlen(s)] != '=' && str[i][ft_strlen(s)] != 0))))
|
||||||
|
i++;
|
||||||
|
if (str[i] == NULL)
|
||||||
|
return (-1);
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int cd_error(t_s_cmd *cmd, char *str, int i)
|
||||||
|
{
|
||||||
|
ft_putstr_fd(str, 2);
|
||||||
|
ft_putstr_fd("\n", 2);
|
||||||
|
return (check_return(cmd, i));
|
||||||
|
}
|
||||||
|
|
||||||
|
int size_path(char **str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
char **s;
|
||||||
|
|
||||||
|
i = find_it(str, "PWD");
|
||||||
|
s = ft_split(str[i], '/');
|
||||||
|
j = double_size(s);
|
||||||
|
free_double(s);
|
||||||
|
return (j);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_return(t_s_cmd *cmd, int var)
|
||||||
|
{
|
||||||
|
if (cmd->child)
|
||||||
|
{
|
||||||
|
cmd->big_cmd->err_var = var;
|
||||||
|
return (var);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
close(cmd->big_cmd->tmpin);
|
||||||
|
close(cmd->big_cmd->tmpout);
|
||||||
|
free_cmd(cmd->big_cmd);
|
||||||
|
exit(var);
|
||||||
|
}
|
||||||
|
}
|
||||||
66
srcs/exit.c
66
srcs/exit.c
@ -1,66 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* exit.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/02/28 18:00:05 by sadjigui #+# #+# */
|
|
||||||
/* Updated: 2022/03/01 18:55:10 by sadjigui ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "../includes/mini.h"
|
|
||||||
|
|
||||||
int find_len(char *input, int i)
|
|
||||||
{
|
|
||||||
int j;
|
|
||||||
|
|
||||||
j = i;
|
|
||||||
while (input[j])
|
|
||||||
j++;
|
|
||||||
return (j);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ft_export(char *input, t_struct *data)
|
|
||||||
{
|
|
||||||
(void)data;
|
|
||||||
int i;
|
|
||||||
int j;
|
|
||||||
char *variable;
|
|
||||||
|
|
||||||
i = 6;
|
|
||||||
j = find_len(input, i);
|
|
||||||
variable = ft_substr(input, i, j);
|
|
||||||
if (j == i)
|
|
||||||
printf("check\n");
|
|
||||||
else if (j > i)
|
|
||||||
ft_export_variable(data, variable);
|
|
||||||
}
|
|
||||||
|
|
||||||
void free_char_tab(char **libre)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (libre[i])
|
|
||||||
{
|
|
||||||
free(libre[i]);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
free(libre);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ft_exit(t_struct *data)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (data->envy[i])
|
|
||||||
{
|
|
||||||
free(data->envy[i]);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
free(data->envy);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
@ -1,91 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* export_utils.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/02/28 23:26:59 by sadjigui #+# #+# */
|
|
||||||
/* Updated: 2022/03/01 18:33:45 by sadjigui ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "../includes/mini.h"
|
|
||||||
|
|
||||||
int check_variable(char *variable)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
if (!ft_isalpha(variable[i]))
|
|
||||||
return(0);
|
|
||||||
i++;
|
|
||||||
while(variable[i])
|
|
||||||
{
|
|
||||||
printf("%c\n", variable[i]);
|
|
||||||
if(!ft_isalnum(variable[i]))
|
|
||||||
return(0);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *define_value(char *value)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char *new_value;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
if (value[i] == ' ')
|
|
||||||
{
|
|
||||||
new_value = ft_strdup("");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("%s\n", value);
|
|
||||||
while (value[i])
|
|
||||||
{
|
|
||||||
printf("%d\n", i);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
new_value = ft_substr(value, 0, i);
|
|
||||||
}
|
|
||||||
return (new_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *check_value(char *value)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char *new_value;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
if (value[0] == '"')
|
|
||||||
{
|
|
||||||
new_value = NULL;
|
|
||||||
printf("jojo\n");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
new_value = define_value(value);
|
|
||||||
return(new_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ft_export_variable(t_struct *data, char *variable)
|
|
||||||
{
|
|
||||||
char *v_v[2];
|
|
||||||
int i;
|
|
||||||
char *tmp;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (variable[i] != '=')
|
|
||||||
i++;
|
|
||||||
v_v[0] = ft_substr(variable, 1, i - 1);
|
|
||||||
i++;
|
|
||||||
v_v[1] = ft_substr(variable, i, find_len(variable, i));
|
|
||||||
if (check_variable(v_v[0]) == 1)
|
|
||||||
{
|
|
||||||
tmp = ft_strdup(v_v[1]);
|
|
||||||
free(v_v[1]);
|
|
||||||
v_v[1] = check_value(tmp);
|
|
||||||
register_env(data, v_v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
81
srcs/init.c
81
srcs/init.c
@ -1,81 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* init.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/02/28 17:17:01 by sadjigui #+# #+# */
|
|
||||||
/* Updated: 2022/03/01 18:39:20 by sadjigui ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "../includes/mini.h"
|
|
||||||
|
|
||||||
void join_variable(t_struct *data, char **v_v, int size)
|
|
||||||
{
|
|
||||||
char *str;
|
|
||||||
char *tmp;
|
|
||||||
|
|
||||||
str = ft_strjoin(v_v[0], "=");
|
|
||||||
tmp = ft_strdup(str);
|
|
||||||
free(str);
|
|
||||||
str = ft_strjoin(tmp,v_v[1]);
|
|
||||||
data->envy[size] = ft_strdup(str);
|
|
||||||
free(str);
|
|
||||||
free(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
void register_env(t_struct *data, char **variable)
|
|
||||||
{
|
|
||||||
int size;
|
|
||||||
char **tmp = NULL;
|
|
||||||
|
|
||||||
// tmp = NULL;
|
|
||||||
size = 0;
|
|
||||||
while (data->envy[size])
|
|
||||||
size++;
|
|
||||||
tmp = malloc(sizeof(char *) * size);
|
|
||||||
if(!tmp)
|
|
||||||
ft_exit(data);
|
|
||||||
size = 0;
|
|
||||||
while (data->envy[size])
|
|
||||||
{
|
|
||||||
tmp[size] = ft_strdup(data->envy[size]);
|
|
||||||
printf("ici\n");
|
|
||||||
size++;
|
|
||||||
}
|
|
||||||
free_char_tab(data->envy);
|
|
||||||
data->envy = malloc(sizeof(char *) * size + 1);
|
|
||||||
if (!data->envy)
|
|
||||||
ft_exit(data);
|
|
||||||
size = 0;
|
|
||||||
while (tmp[size])
|
|
||||||
{
|
|
||||||
data->envy[size] = ft_strdup(tmp[size]);
|
|
||||||
size++;
|
|
||||||
}
|
|
||||||
join_variable(data, variable, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ft_env(t_struct *data, char **env)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (env[i])
|
|
||||||
i++;
|
|
||||||
data->envy = malloc(sizeof(char *) * i);
|
|
||||||
i = 0;
|
|
||||||
while (env[i])
|
|
||||||
{
|
|
||||||
data->envy[i] = ft_strdup(env[i]);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void init_struct(t_struct *data, char **env)
|
|
||||||
{
|
|
||||||
ft_env(data, env);
|
|
||||||
}
|
|
||||||
106
srcs/main.c
106
srcs/main.c
@ -3,44 +3,94 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* main.c :+: :+: :+: */
|
/* main.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/02/26 16:00:41 by sadjigui #+# #+# */
|
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
|
||||||
/* Updated: 2022/03/06 15:31:18 by sadjigui ### ########.fr */
|
/* Updated: 2022/04/23 13:16:15 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/mini.h"
|
#include "../includes/minishell.h"
|
||||||
|
|
||||||
void red()
|
char **ft_dup_double(char **env)
|
||||||
{
|
{
|
||||||
printf("\033[1;31m");
|
char **new_tab;
|
||||||
}
|
int i;
|
||||||
|
|
||||||
void normal()
|
i = 0;
|
||||||
{
|
if (!env)
|
||||||
printf("\033[0m");
|
return (0);
|
||||||
}
|
while (env[i])
|
||||||
|
i++;
|
||||||
void boot_shell(t_struct *data)
|
new_tab = ft_calloc(sizeof(char *), i + 1);
|
||||||
{
|
i = 0;
|
||||||
char *input;
|
while (env[i])
|
||||||
while(1)
|
|
||||||
{
|
{
|
||||||
red();
|
new_tab[i] = ft_strjoin(env[i], 0);
|
||||||
input = readline("Minishell> ");
|
i++;
|
||||||
lexer(input, data);
|
|
||||||
add_history(input);
|
|
||||||
normal();
|
|
||||||
}
|
}
|
||||||
|
new_tab[i] = NULL;
|
||||||
|
return (new_tab);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int ac, char **av, char **env)
|
char **read_line(char **path, char *input, t_cmd *cmd, int *err_var)
|
||||||
{
|
{
|
||||||
(void)ac;
|
(void)*err_var;
|
||||||
(void)av;
|
input = readline("\033[1;31m~$ \033[0m");
|
||||||
t_struct data;
|
if (!input)
|
||||||
|
{
|
||||||
init_struct(&data, env);
|
free_double(path);
|
||||||
boot_shell(&data);
|
exit_shell(cmd, 0);
|
||||||
|
}
|
||||||
|
add_history(input);
|
||||||
|
if (ft_strlen(input) && next_space(input, 0) && input && path)
|
||||||
|
{
|
||||||
|
cmd = set_cmd(input, path, g_var);
|
||||||
|
if (cmd)
|
||||||
|
{
|
||||||
|
free_double(path);
|
||||||
|
execute(cmd);
|
||||||
|
g_var = cmd->err_var;
|
||||||
|
path = ft_dup_double(cmd->env);
|
||||||
|
free_cmd(cmd);
|
||||||
|
cmd = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(input);
|
||||||
|
return (path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_prompt(char **path)
|
||||||
|
{
|
||||||
|
int err_var;
|
||||||
|
|
||||||
|
err_var = 0;
|
||||||
|
while (1)
|
||||||
|
path = read_line(path, 0, 0, &err_var);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int ac, char **av, char **path)
|
||||||
|
{
|
||||||
|
char **env;
|
||||||
|
|
||||||
|
(void)av;
|
||||||
|
if (!isatty(0))
|
||||||
|
{
|
||||||
|
printf("Not today\n");
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
env = ft_dup_double(path);
|
||||||
|
av = 0;
|
||||||
|
if (ac != 1)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Error: too much arguments\n", 2);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
printf("---MINISHELL START---\n");
|
||||||
|
signal(SIGINT, crtl_c);
|
||||||
|
signal(SIGQUIT, sig_quit);
|
||||||
|
if (env)
|
||||||
|
ft_shlvl(env);
|
||||||
|
print_prompt(env);
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
86
srcs/pipe/exec_utils.c
Normal file
86
srcs/pipe/exec_utils.c
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* exec_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/04/19 17:36:53 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/20 04:50:19 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
int wait_exit(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int ret;
|
||||||
|
int exit_pid;
|
||||||
|
int status;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
ret = 0;
|
||||||
|
exit_pid = 1;
|
||||||
|
while (exit_pid > 0)
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
exit_pid = wait(&status);
|
||||||
|
while (exit_pid != -1 && cmd->s_cmds[i])
|
||||||
|
{
|
||||||
|
exit_child(cmd, exit_pid, status, i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
void exit_child(t_cmd *cmd, int exit_pid, int status, int i)
|
||||||
|
{
|
||||||
|
if (exit_pid == cmd->s_cmds[i]->child)
|
||||||
|
{
|
||||||
|
close(cmd->s_cmds[i]->fd[0]);
|
||||||
|
close(cmd->s_cmds[i]->fd[1]);
|
||||||
|
if (WIFEXITED(status))
|
||||||
|
cmd->err_var = WEXITSTATUS(status);
|
||||||
|
else if (WIFSIGNALED(status))
|
||||||
|
{
|
||||||
|
if (WTERMSIG(status) != 13)
|
||||||
|
cmd->err_var = WTERMSIG(status);
|
||||||
|
ft_putstr_fd("\b\b\b\b\b", 1);
|
||||||
|
if (WTERMSIG(status) == 3)
|
||||||
|
ft_putstr_fd("^\\Quit\n", 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void check_access(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK))
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Minishell: command not found: ", 2);
|
||||||
|
if (cmd->current_s_cmd->cmd)
|
||||||
|
ft_putstr_fd(cmd->current_s_cmd->cmd, 2);
|
||||||
|
ft_putstr_fd("\n", 2);
|
||||||
|
close(0);
|
||||||
|
close(1);
|
||||||
|
close(cmd->tmpin);
|
||||||
|
close(cmd->tmpout);
|
||||||
|
free_cmd(cmd);
|
||||||
|
exit(127);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void close_pipe(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
close(0);
|
||||||
|
while (cmd->s_cmds[i])
|
||||||
|
{
|
||||||
|
close(cmd->s_cmds[i]->fd[0]);
|
||||||
|
close(cmd->s_cmds[i]->fd[1]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
110
srcs/pipe/execution.c
Normal file
110
srcs/pipe/execution.c
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* execution.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/04/20 04:43:32 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/22 11:51:32 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
|
||||||
|
{
|
||||||
|
cmd->current_s_cmd->child = fork();
|
||||||
|
if (cmd->current_s_cmd->child == 0)
|
||||||
|
{
|
||||||
|
if (fdpipe)
|
||||||
|
close(fdpipe[0]);
|
||||||
|
dup2(cmd->current_s_cmd->fd[0], 0);
|
||||||
|
dup2(cmd->current_s_cmd->fd[1], 1);
|
||||||
|
close(cmd->current_s_cmd->fd[0]);
|
||||||
|
close(cmd->current_s_cmd->fd[1]);
|
||||||
|
if (is_builtin(cmd->current_s_cmd->cmd))
|
||||||
|
{
|
||||||
|
call_builtin(cmd);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
check_access(cmd);
|
||||||
|
if (-1 == execve(cmd->current_s_cmd->cmd,
|
||||||
|
cmd->current_s_cmd->args, env))
|
||||||
|
ft_putstr_fd("Minishell: exec error\n", 2);
|
||||||
|
exit(126);
|
||||||
|
}
|
||||||
|
else if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK))
|
||||||
|
cmd->err_var = 127;
|
||||||
|
else
|
||||||
|
cmd->err_var = 131;
|
||||||
|
}
|
||||||
|
|
||||||
|
void exec_last_cmd(t_cmd *cmd, int *fdin, int *fdout, int i)
|
||||||
|
{
|
||||||
|
if (cmd->current_s_cmd->outfile)
|
||||||
|
*fdout = open(cmd->current_s_cmd->outfile,
|
||||||
|
O_RDWR | O_CREAT | O_APPEND, 0666);
|
||||||
|
else
|
||||||
|
*fdout = dup(cmd->tmpout);
|
||||||
|
cmd->current_s_cmd->fd[0] = *fdin;
|
||||||
|
cmd->current_s_cmd->fd[1] = *fdout;
|
||||||
|
if (i == 0 && is_builtin(cmd->current_s_cmd->cmd))
|
||||||
|
call_builtin(cmd);
|
||||||
|
else
|
||||||
|
exec_cmd(cmd, cmd->env, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void exec_not_last_cmd(t_cmd *cmd, int *fdpipe, int *fdin, int *fdout)
|
||||||
|
{
|
||||||
|
pipe(fdpipe);
|
||||||
|
cmd->current_s_cmd->fd[0] = *fdin;
|
||||||
|
if (cmd->current_s_cmd->outfile)
|
||||||
|
{
|
||||||
|
*fdout = open(cmd->current_s_cmd->outfile,
|
||||||
|
O_RDWR | O_CREAT | O_APPEND, 0666);
|
||||||
|
cmd->current_s_cmd->fd[1] = *fdout;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cmd->current_s_cmd->fd[1] = fdpipe[1];
|
||||||
|
*fdin = fdpipe[0];
|
||||||
|
exec_cmd(cmd, cmd->env, fdpipe);
|
||||||
|
close(cmd->current_s_cmd->fd[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_fdin_not_first(t_cmd *cmd, int *fdin)
|
||||||
|
{
|
||||||
|
close(*fdin);
|
||||||
|
*fdin = open(cmd->current_s_cmd->infile, O_RDWR);
|
||||||
|
if (*fdin < 0)
|
||||||
|
printf("Minishell: open : bad file descriptor\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void execute(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
int fdpipe[2];
|
||||||
|
int fdout;
|
||||||
|
int fdin;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
fdpipe[1] = -1;
|
||||||
|
cmd->tmpin = dup(0);
|
||||||
|
cmd->tmpout = dup(1);
|
||||||
|
set_fdin(cmd, &fdin);
|
||||||
|
while (cmd->current_s_cmd)
|
||||||
|
{
|
||||||
|
cmd->current_s_cmd->child = 1;
|
||||||
|
fdout = -1;
|
||||||
|
if (i > 0 && cmd->current_s_cmd->infile)
|
||||||
|
set_fdin_not_first(cmd, &fdin);
|
||||||
|
if (i == cmd->nb_s_cmd - 1)
|
||||||
|
exec_last_cmd(cmd, &fdin, &fdout, i);
|
||||||
|
else
|
||||||
|
exec_not_last_cmd(cmd, fdpipe, &fdin, &fdout);
|
||||||
|
if (fdpipe[1] != -1)
|
||||||
|
close(fdpipe[1]);
|
||||||
|
cmd->current_s_cmd = cmd->s_cmds[++i];
|
||||||
|
}
|
||||||
|
reset_fds(cmd);
|
||||||
|
}
|
||||||
105
srcs/pipe/pipex_utils.c
Normal file
105
srcs/pipe/pipex_utils.c
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* pipex_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/08 16:58:40 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/19 18:11:38 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
void set_fdin(t_cmd *cmd, int *fdin)
|
||||||
|
{
|
||||||
|
if (cmd->current_s_cmd->infile)
|
||||||
|
{
|
||||||
|
*fdin = open(cmd->current_s_cmd->infile, O_RDWR);
|
||||||
|
if (*fdin < 0)
|
||||||
|
printf("Minishell: open : bad file descriptor\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
*fdin = dup(cmd->tmpin);
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset_fds(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
close_pipe(cmd);
|
||||||
|
dup2(cmd->tmpin, 0);
|
||||||
|
dup2(cmd->tmpout, 1);
|
||||||
|
close(cmd->tmpin);
|
||||||
|
close(cmd->tmpout);
|
||||||
|
wait_exit(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **get_path(char **env)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char **line;
|
||||||
|
char *swap;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
line = 0;
|
||||||
|
swap = 0;
|
||||||
|
while (env[i++] && env[i])
|
||||||
|
{
|
||||||
|
if (!ft_strncmp(env[i], "PATH=", 5))
|
||||||
|
{
|
||||||
|
swap = ft_substr(env[i], 5, ft_strlen(env[i]));
|
||||||
|
if (!swap)
|
||||||
|
exit(1);
|
||||||
|
line = ft_split(swap, ':');
|
||||||
|
if (!line)
|
||||||
|
exit(1);
|
||||||
|
free(swap);
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *does_access(char **path, char **exec)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *cmd;
|
||||||
|
char *swap;
|
||||||
|
|
||||||
|
swap = 0;
|
||||||
|
cmd = 0;
|
||||||
|
i = 0;
|
||||||
|
if (exec[0][0] != '/')
|
||||||
|
swap = ft_strjoin(path[i], "/");
|
||||||
|
cmd = ft_strjoin(swap, exec[0]);
|
||||||
|
free(swap);
|
||||||
|
swap = cmd;
|
||||||
|
while (access(swap, F_OK) && path[i++])
|
||||||
|
{
|
||||||
|
free(swap);
|
||||||
|
swap = ft_strjoin(path[i], "/");
|
||||||
|
cmd = swap;
|
||||||
|
swap = ft_strjoin(swap, exec[0]);
|
||||||
|
free(cmd);
|
||||||
|
}
|
||||||
|
if (path[i])
|
||||||
|
return (swap);
|
||||||
|
free(swap);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_command(char **exec, char **path)
|
||||||
|
{
|
||||||
|
char *swap;
|
||||||
|
|
||||||
|
swap = 0;
|
||||||
|
if (!path)
|
||||||
|
return (0);
|
||||||
|
if ((exec[0][0] == '/' || exec[0][0] == '.') && !access(exec[0], F_OK))
|
||||||
|
return (exec[0]);
|
||||||
|
else if (exec[0][0] == '/')
|
||||||
|
return (0);
|
||||||
|
swap = does_access(path, exec);
|
||||||
|
return (swap);
|
||||||
|
}
|
||||||
85
srcs/set_cmd/free_cmd.c
Normal file
85
srcs/set_cmd/free_cmd.c
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* free_cmd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/23 13:18:23 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
void exit_shell(t_cmd *cmd, int ret)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("exit\n", 1);
|
||||||
|
close (0);
|
||||||
|
close (1);
|
||||||
|
if (!cmd)
|
||||||
|
exit(ret);
|
||||||
|
if (cmd->tmpin != -1)
|
||||||
|
close(cmd->tmpin);
|
||||||
|
if (cmd->tmpout != -1)
|
||||||
|
close(cmd->tmpout);
|
||||||
|
if (cmd->current_s_cmd->fd[0] != -1)
|
||||||
|
close(cmd->current_s_cmd->fd[0]);
|
||||||
|
if (cmd->current_s_cmd->fd[1] != -1)
|
||||||
|
close(cmd->current_s_cmd->fd[1]);
|
||||||
|
free_cmd(cmd);
|
||||||
|
rl_clear_history();
|
||||||
|
cmd = 0;
|
||||||
|
exit(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
void del_heredoc(void)
|
||||||
|
{
|
||||||
|
long i;
|
||||||
|
char *str;
|
||||||
|
char *nbr;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < 2147483647)
|
||||||
|
{
|
||||||
|
nbr = ft_itoa(i);
|
||||||
|
if (i == 0)
|
||||||
|
str = ft_strdup(".heredoc");
|
||||||
|
else
|
||||||
|
str = ft_strjoin(".heredoc", nbr);
|
||||||
|
free(nbr);
|
||||||
|
if (access(str, F_OK) == 0)
|
||||||
|
unlink(str);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
free(str);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
free(str);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_cmd(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
del_heredoc();
|
||||||
|
while (cmd->s_cmds && cmd->s_cmds[i])
|
||||||
|
{
|
||||||
|
free_double(cmd->s_cmds[i]->args);
|
||||||
|
if (cmd->s_cmds[i]->cmd)
|
||||||
|
free(cmd->s_cmds[i]->cmd);
|
||||||
|
if (cmd->s_cmds[i]->infile)
|
||||||
|
free(cmd->s_cmds[i]->infile);
|
||||||
|
if (cmd->s_cmds[i]->outfile)
|
||||||
|
free(cmd->s_cmds[i]->outfile);
|
||||||
|
free(cmd->s_cmds[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free_double(cmd->env);
|
||||||
|
free_double(cmd->path);
|
||||||
|
free(cmd->s_cmds);
|
||||||
|
free(cmd);
|
||||||
|
}
|
||||||
126
srcs/set_cmd/set_cmd.c
Normal file
126
srcs/set_cmd/set_cmd.c
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* set_cmd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/23 13:07:57 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
t_s_cmd *set_s_cmd(char *line, int index)
|
||||||
|
{
|
||||||
|
t_s_cmd *s_cmd;
|
||||||
|
char **split_line;
|
||||||
|
|
||||||
|
s_cmd = malloc(sizeof(t_s_cmd));
|
||||||
|
if (!s_cmd)
|
||||||
|
return (0);
|
||||||
|
s_cmd->fd[0] = -1;
|
||||||
|
s_cmd->fd[1] = -1;
|
||||||
|
s_cmd->cmd = 0;
|
||||||
|
s_cmd->args = 0;
|
||||||
|
s_cmd->infile = 0;
|
||||||
|
s_cmd->outfile = 0;
|
||||||
|
line = set_redirection(s_cmd, ft_strdup(line), index, 0);
|
||||||
|
if (!line)
|
||||||
|
{
|
||||||
|
free(s_cmd);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
split_line = ft_split_with_quote(line, ' ');
|
||||||
|
s_cmd->nb_args = double_size(split_line);
|
||||||
|
s_cmd->args = split_line;
|
||||||
|
free(line);
|
||||||
|
return (s_cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_cmd *split_cmd(t_cmd *cmd, char **cmds)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (cmds[i])
|
||||||
|
{
|
||||||
|
cmd->s_cmds[i] = set_s_cmd(cmds[i], i);
|
||||||
|
if (!cmd->s_cmds[i])
|
||||||
|
{
|
||||||
|
free_cmd(cmd);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
cmd->s_cmds[i]->big_cmd = cmd;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
cmd->s_cmds[i] = 0;
|
||||||
|
return (cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_pipe_good(char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char next;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i])
|
||||||
|
{
|
||||||
|
if (str[i] == '|')
|
||||||
|
{
|
||||||
|
next = next_space(str, i + 1);
|
||||||
|
if (!next || next == '|' || next == '<' || next == '>')
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_cmd *initialize_cmd(t_cmd *cmd, char **env, char **cmds, int nb)
|
||||||
|
{
|
||||||
|
cmd->tmpin = -1;
|
||||||
|
cmd->tmpout = -1;
|
||||||
|
cmd->err_var = nb;
|
||||||
|
cmd->path = get_path(env);
|
||||||
|
cmd->env = ft_dup_double(env);
|
||||||
|
cmd->nb_s_cmd = double_size(cmds);
|
||||||
|
cmd = split_cmd(cmd, cmds);
|
||||||
|
if (!cmd)
|
||||||
|
{
|
||||||
|
free_double(cmds);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
parse_quote(cmd);
|
||||||
|
free_double(cmds);
|
||||||
|
if (cmd)
|
||||||
|
{
|
||||||
|
cmd->current_s_cmd = cmd->s_cmds[0];
|
||||||
|
cmd->err_var = 0;
|
||||||
|
return (cmd);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_cmd *set_cmd(char *input, char **env, int nb)
|
||||||
|
{
|
||||||
|
t_cmd *cmd;
|
||||||
|
char **cmds;
|
||||||
|
|
||||||
|
if (!is_quote_good(input) || !is_pipe_good(input))
|
||||||
|
{
|
||||||
|
error_parsing(0);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
cmds = ft_split_with_quote(input, '|');
|
||||||
|
if (!cmds)
|
||||||
|
return (0);
|
||||||
|
cmd = malloc(sizeof(t_cmd));
|
||||||
|
if (!cmd)
|
||||||
|
return (0);
|
||||||
|
cmd->s_cmds = ft_calloc(sizeof(t_s_cmd), double_size(cmds) + 1);
|
||||||
|
if (!cmd->s_cmds)
|
||||||
|
return (0);
|
||||||
|
return (initialize_cmd(cmd, env, cmds, nb));
|
||||||
|
}
|
||||||
@ -1,25 +1,22 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strcmp.c :+: :+: :+: */
|
/* set_cmd_utils.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/12/08 19:23:07 by sadjigui #+# #+# */
|
/* Created: 2022/04/19 16:53:13 by apommier #+# #+# */
|
||||||
/* Updated: 2021/12/08 19:23:11 by sadjigui ### ########.fr */
|
/* Updated: 2022/04/22 11:43:15 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
int ft_strcmp(const char *s1, const char *s2)
|
#include "../../includes/minishell.h"
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
char *error_parsing(char *to_free)
|
||||||
while (s1[i] || s2[i])
|
{
|
||||||
{
|
g_var = 2;
|
||||||
if (s1[i] != s2[i])
|
if (to_free)
|
||||||
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
|
free(to_free);
|
||||||
i++;
|
ft_putstr_fd("Minishell: error while parsing command\n", 2);
|
||||||
}
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
107
srcs/set_quote/set_quote.c
Normal file
107
srcs/set_quote/set_quote.c
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* set_quote.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/04/09 23:58:21 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/23 13:11:50 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
int is_in_quote(char *str, int index)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int open;
|
||||||
|
|
||||||
|
open = 0;
|
||||||
|
i = 0;
|
||||||
|
while (i < index)
|
||||||
|
{
|
||||||
|
if (str[i] == '\'' && open != 2)
|
||||||
|
{
|
||||||
|
if (!open)
|
||||||
|
open = 1;
|
||||||
|
else if (open == 1)
|
||||||
|
open = 0;
|
||||||
|
}
|
||||||
|
else if (str[i] == '"' && open != 1)
|
||||||
|
{
|
||||||
|
if (!open)
|
||||||
|
open = 2;
|
||||||
|
else if (open == 2)
|
||||||
|
open = 0;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (open);
|
||||||
|
}
|
||||||
|
|
||||||
|
void count_quote(char c, int *open, int *simple_quote, int *double_quote)
|
||||||
|
{
|
||||||
|
if (c == '\'' && *open != 2)
|
||||||
|
{
|
||||||
|
if (!(*open))
|
||||||
|
(*open) = 1;
|
||||||
|
else if ((*open) == 1)
|
||||||
|
(*open) = 0;
|
||||||
|
(*simple_quote)++;
|
||||||
|
}
|
||||||
|
else if (c == '"' && (*open) != 1)
|
||||||
|
{
|
||||||
|
if (!(*open))
|
||||||
|
(*open) = 2;
|
||||||
|
else if ((*open) == 2)
|
||||||
|
(*open) = 0;
|
||||||
|
(*double_quote)++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_quote_good(char *str)
|
||||||
|
{
|
||||||
|
int simple_quote;
|
||||||
|
int double_quote;
|
||||||
|
int open;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
open = 0;
|
||||||
|
simple_quote = 0;
|
||||||
|
double_quote = 0;
|
||||||
|
while (str[i])
|
||||||
|
{
|
||||||
|
count_quote(str[i], &open, &simple_quote, &double_quote);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (simple_quote % 2 || double_quote % 2)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int parse_quote(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
while (cmd->s_cmds[++i])
|
||||||
|
{
|
||||||
|
j = -1;
|
||||||
|
while (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]))
|
||||||
|
{
|
||||||
|
cmd->s_cmds[i]->cmd = get_command(cmd->s_cmds[i]->args, cmd->path);
|
||||||
|
if (cmd->s_cmds[i]->cmd == cmd->s_cmds[i]->args[0])
|
||||||
|
cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->cmd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->args[0]);
|
||||||
|
if (!cmd->s_cmds[i]->cmd)
|
||||||
|
cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->args[0]);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
114
srcs/set_quote/set_var.c
Normal file
114
srcs/set_quote/set_var.c
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* set_var.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/04/19 15:02:01 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/22 13:01:30 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
char *del_char(char *str, int *index)
|
||||||
|
{
|
||||||
|
char *swap;
|
||||||
|
char *swap2;
|
||||||
|
|
||||||
|
swap = 0;
|
||||||
|
swap = ft_strdup(str + *index + 1);
|
||||||
|
str[*index] = 0;
|
||||||
|
swap2 = str;
|
||||||
|
str = ft_strjoin(str, swap);
|
||||||
|
free(swap2);
|
||||||
|
free(swap);
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_var(t_cmd *cmd, char *var_name)
|
||||||
|
{
|
||||||
|
char *line;
|
||||||
|
char **split_line;
|
||||||
|
int index;
|
||||||
|
|
||||||
|
line = 0;
|
||||||
|
if (!ft_strcmp(var_name, "?"))
|
||||||
|
return (ft_itoa(cmd->err_var));
|
||||||
|
index = find_it(cmd->env, var_name);
|
||||||
|
if (index >= 0)
|
||||||
|
{
|
||||||
|
line = cmd->env[index];
|
||||||
|
split_line = ft_split(line, '=');
|
||||||
|
if (split_line[1])
|
||||||
|
line = ft_strdup(split_line[1]);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
free_double(split_line);
|
||||||
|
}
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *change_var(t_cmd *big_cmd, char *cmd, int *index)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *ret;
|
||||||
|
|
||||||
|
i = *index + 1;
|
||||||
|
while (cmd[i] && (ft_isalnum(cmd[i]) || cmd[i] == '_'
|
||||||
|
|| cmd[i] == '?' || cmd[i] == '$'))
|
||||||
|
i++;
|
||||||
|
if (i == *index + 1)
|
||||||
|
{
|
||||||
|
(*index)++;
|
||||||
|
ret = ft_strdup(cmd);
|
||||||
|
free(cmd);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
ret = find_var(big_cmd, cmd, i, index);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *check_quote(t_cmd *big_cmd, char *cmd, int *i)
|
||||||
|
{
|
||||||
|
cmd = del_char(cmd, i);
|
||||||
|
if (cmd[*i])
|
||||||
|
{
|
||||||
|
while (cmd[*i] != '"')
|
||||||
|
{
|
||||||
|
if (cmd[*i] == '$')
|
||||||
|
cmd = change_var(big_cmd, cmd, i);
|
||||||
|
(*i)++;
|
||||||
|
}
|
||||||
|
cmd = del_char(cmd, i);
|
||||||
|
}
|
||||||
|
return (cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *set_var(t_cmd *big_cmd, char *cmd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (cmd[0] && cmd[i])
|
||||||
|
{
|
||||||
|
if (cmd[i] == '\'')
|
||||||
|
{
|
||||||
|
cmd = del_char(cmd, &i);
|
||||||
|
if (cmd[i])
|
||||||
|
{
|
||||||
|
while (cmd[i] != '\'')
|
||||||
|
i++;
|
||||||
|
cmd = del_char(cmd, &i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (cmd[i] == '"')
|
||||||
|
cmd = check_quote(big_cmd, cmd, &i);
|
||||||
|
else if (cmd[i] == '$')
|
||||||
|
cmd = change_var(big_cmd, cmd, &i);
|
||||||
|
else
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (cmd);
|
||||||
|
}
|
||||||
133
srcs/set_quote/split_with_quote.c
Normal file
133
srcs/set_quote/split_with_quote.c
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* split_with_quote.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/04/10 19:50:50 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/23 13:11:27 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
int next_quote(const char *s, int i)
|
||||||
|
{
|
||||||
|
int simple_quote;
|
||||||
|
int double_quote;
|
||||||
|
|
||||||
|
simple_quote = 0;
|
||||||
|
double_quote = 0;
|
||||||
|
if (s[i] == '"' || s[i] == '\'')
|
||||||
|
{
|
||||||
|
if (s[i] == '"')
|
||||||
|
double_quote = 1;
|
||||||
|
else if (s[i] == '\'')
|
||||||
|
simple_quote = 1;
|
||||||
|
i++;
|
||||||
|
if (simple_quote)
|
||||||
|
{
|
||||||
|
while (s[i] != '\'')
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else if (double_quote)
|
||||||
|
{
|
||||||
|
while (s[i] != '"')
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int fill_tab(char *s, char c, char **dest, size_t index)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int size;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (s[i] != c && s[i])
|
||||||
|
{
|
||||||
|
if (s[i] == '"' || s[i] == '\'')
|
||||||
|
i = next_quote(s, i);
|
||||||
|
else
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
dest[index] = (char *)ft_calloc(i + 1, sizeof(char));
|
||||||
|
if (dest[index] == 0)
|
||||||
|
return (0);
|
||||||
|
size = i;
|
||||||
|
i = 0;
|
||||||
|
while (i < size)
|
||||||
|
{
|
||||||
|
dest[index][i] = s[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void call(char *s, char c, char **dest, int j)
|
||||||
|
{
|
||||||
|
int index;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
index = 0;
|
||||||
|
while (j > index)
|
||||||
|
{
|
||||||
|
while (s[i] != '"' && s[i] != '\'' && s[i] == c && s[i])
|
||||||
|
i++;
|
||||||
|
if (!s[i])
|
||||||
|
return ;
|
||||||
|
fill_tab(s + i, c, dest, index);
|
||||||
|
index++;
|
||||||
|
while (s[i] != c && s[i])
|
||||||
|
{
|
||||||
|
if (s[i] == '"' || s[i] == '\'')
|
||||||
|
i = next_quote(s, i);
|
||||||
|
else
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void count_word(const char *s, int *i, int *j, char c)
|
||||||
|
{
|
||||||
|
while (s[*i])
|
||||||
|
{
|
||||||
|
while (s[*i] != c && s[*i])
|
||||||
|
{
|
||||||
|
if (s[*i] == '"' || s[*i] == '\'')
|
||||||
|
*i = next_quote(s, *i);
|
||||||
|
else
|
||||||
|
(*i)++;
|
||||||
|
}
|
||||||
|
(*j)++;
|
||||||
|
while (s[*i] == c && s[*i])
|
||||||
|
(*i)++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char **ft_split_with_quote(char const *s, char c)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
char **dest;
|
||||||
|
|
||||||
|
j = 0;
|
||||||
|
i = 0;
|
||||||
|
if (!s)
|
||||||
|
return (0);
|
||||||
|
i = next_quote(s, i);
|
||||||
|
if (i)
|
||||||
|
j++;
|
||||||
|
while (s[i] == c && s[i])
|
||||||
|
i++;
|
||||||
|
count_word(s, &i, &j, c);
|
||||||
|
dest = (char **)ft_calloc(sizeof(char *), (1 + j));
|
||||||
|
if (!dest)
|
||||||
|
return (0);
|
||||||
|
dest[j] = 0;
|
||||||
|
call((char *)s, c, dest, j);
|
||||||
|
return (dest);
|
||||||
|
}
|
||||||
69
srcs/set_redirection/heredoc_utils.c
Normal file
69
srcs/set_redirection/heredoc_utils.c
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* heredoc_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/04/19 19:25:16 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/22 11:24:29 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
void sig_heredoc(int num)
|
||||||
|
{
|
||||||
|
struct sigaction base;
|
||||||
|
|
||||||
|
(void)num;
|
||||||
|
memset(&base, 0, sizeof(base));
|
||||||
|
base.sa_handler = &crtl_c;
|
||||||
|
base.sa_flags = 0;
|
||||||
|
ft_putchar_fd('\n', 1);
|
||||||
|
if (sigaction(SIGINT, &base, 0) == -1)
|
||||||
|
{
|
||||||
|
printf("Minishell: sigaction error2\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void change_signal(void)
|
||||||
|
{
|
||||||
|
struct sigaction test;
|
||||||
|
|
||||||
|
memset(&test, 0, sizeof(test));
|
||||||
|
test.sa_handler = &sig_heredoc;
|
||||||
|
test.sa_flags = 0;
|
||||||
|
if (sigaction(SIGINT, &test, 0) == -1)
|
||||||
|
{
|
||||||
|
printf("Minishell: sigaction error\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int free_wait_prompt(char *in, char**history)
|
||||||
|
{
|
||||||
|
free(in);
|
||||||
|
free_double(history);
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **fill_history(t_s_cmd *cmd, char *input, char *in, char **history)
|
||||||
|
{
|
||||||
|
char *del;
|
||||||
|
char *dup;
|
||||||
|
|
||||||
|
if (ft_strcmp(input, in))
|
||||||
|
{
|
||||||
|
dup = ft_strdup(input);
|
||||||
|
dup[ft_strlen(input) - 1] = 0;
|
||||||
|
del = dup;
|
||||||
|
dup = set_var(cmd->big_cmd, dup);
|
||||||
|
history = add_line(history, dup);
|
||||||
|
if (dup != del)
|
||||||
|
free(del);
|
||||||
|
free(dup);
|
||||||
|
}
|
||||||
|
return (history);
|
||||||
|
}
|
||||||
125
srcs/set_redirection/redirection.c
Normal file
125
srcs/set_redirection/redirection.c
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* redirection.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/20 17:05:09 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
char *get_word(char *str, int start)
|
||||||
|
{
|
||||||
|
char *new;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[start + i] == '<' || str[start + i] == '>')
|
||||||
|
i++;
|
||||||
|
while (str[start + i] == ' ')
|
||||||
|
i++;
|
||||||
|
new = ft_strjoin(&str[start + i], 0);
|
||||||
|
i = 0;
|
||||||
|
while (new[i] && new[i] != ' ' && new[i] != '>' && new[i] != '<')
|
||||||
|
i++;
|
||||||
|
new[i] = 0;
|
||||||
|
return (new);
|
||||||
|
}
|
||||||
|
|
||||||
|
int set_file(char *file, int type)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
if (type)
|
||||||
|
fd = open(file, O_APPEND | O_CREAT, 0666);
|
||||||
|
else
|
||||||
|
fd = open(file, O_TRUNC | O_CREAT, 0666);
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Minishell: ", 2);
|
||||||
|
ft_putstr_fd(file, 2);
|
||||||
|
ft_putstr_fd(": Permission denied\n", 2);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
if (fd)
|
||||||
|
close(fd);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **add_line(char **tab, char *line)
|
||||||
|
{
|
||||||
|
int size;
|
||||||
|
char **ret;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
size = 0;
|
||||||
|
ret = 0;
|
||||||
|
if (tab)
|
||||||
|
size = double_size(tab);
|
||||||
|
ret = ft_calloc(size + 2, sizeof(char *));
|
||||||
|
if (!ret)
|
||||||
|
return (0);
|
||||||
|
while (tab && tab[i])
|
||||||
|
{
|
||||||
|
ret[i] = ft_strjoin(tab[i], 0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
ret[i] = ft_strjoin(line, 0);
|
||||||
|
ret[i + 1] = 0;
|
||||||
|
if (tab)
|
||||||
|
free_double(tab);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
int set_redirect_in(t_s_cmd *cmd, char **line, int *i, int index)
|
||||||
|
{
|
||||||
|
if (!is_in_quote(*line, *i))
|
||||||
|
{
|
||||||
|
*line = ft_input(*line, cmd, *i);
|
||||||
|
if (!(*line))
|
||||||
|
return (0);
|
||||||
|
if (cmd->in_type == 1)
|
||||||
|
{
|
||||||
|
if (wait_prompt(cmd, index, 0, 0) == -1)
|
||||||
|
{
|
||||||
|
free(*line);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*i = 0;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *set_redirection(t_s_cmd *cmd, char *line, int index, int i)
|
||||||
|
{
|
||||||
|
while (line[i])
|
||||||
|
{
|
||||||
|
if (line[i] == '<')
|
||||||
|
{
|
||||||
|
if (!set_redirect_in(cmd, &line, &i, index))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
else if (line[i] == '>')
|
||||||
|
{
|
||||||
|
if (!is_in_quote(line, i))
|
||||||
|
{
|
||||||
|
line = ft_output(line, cmd, i);
|
||||||
|
if (!line || !set_file(cmd->outfile, cmd->in_type))
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (line[i] && (line[i] == '<' || line[i] == '>')
|
||||||
|
&& is_in_quote(line, i))
|
||||||
|
i++;
|
||||||
|
else if (line[i] && line[i] != '<' && line[i] != '>')
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
64
srcs/set_redirection/set_heredoc.c
Normal file
64
srcs/set_redirection/set_heredoc.c
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* set_heredoc.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/04/19 14:46:01 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/23 13:08:11 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
char *set_heredoc(int index, char **in)
|
||||||
|
{
|
||||||
|
char *nbr_file;
|
||||||
|
char *file_name;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
if (index)
|
||||||
|
{
|
||||||
|
nbr_file = ft_itoa(index + 1);
|
||||||
|
file_name = ft_strjoin(".heredoc", nbr_file);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
file_name = ft_strjoin(".heredoc", 0);
|
||||||
|
fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
||||||
|
if (fd > 0)
|
||||||
|
{
|
||||||
|
print_double_fd(in, fd);
|
||||||
|
free_double(in);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
return (file_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
int wait_prompt(t_s_cmd *cmd, int index, int i, char *input)
|
||||||
|
{
|
||||||
|
char **history;
|
||||||
|
char *in;
|
||||||
|
|
||||||
|
change_signal();
|
||||||
|
in = ft_strjoin(cmd->infile, "\n");
|
||||||
|
free(cmd->infile);
|
||||||
|
cmd->infile = 0;
|
||||||
|
history = 0;
|
||||||
|
while (i == 0 || (input && ft_strlen(input) && ft_strcmp(input, in)))
|
||||||
|
{
|
||||||
|
i = 1;
|
||||||
|
if (input)
|
||||||
|
free(input);
|
||||||
|
ft_putstr_fd("> ", 0);
|
||||||
|
input = get_next_line(0);
|
||||||
|
if (!input)
|
||||||
|
return (free_wait_prompt(in, history));
|
||||||
|
history = fill_history(cmd, input, in, history);
|
||||||
|
}
|
||||||
|
free(in);
|
||||||
|
free(input);
|
||||||
|
cmd->infile = set_heredoc(index, history);
|
||||||
|
cmd->in_type = 0;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
76
srcs/set_redirection/set_input.c
Normal file
76
srcs/set_redirection/set_input.c
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* set_input.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/04/19 14:44:22 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/23 13:08:35 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
int check_access_input(t_s_cmd *cmd)
|
||||||
|
{
|
||||||
|
if (access(cmd->infile, R_OK))
|
||||||
|
{
|
||||||
|
g_var = 1;
|
||||||
|
ft_putstr_fd("Minishell: ", 2);
|
||||||
|
ft_putstr_fd(cmd->infile, 2);
|
||||||
|
if (access(cmd->infile, F_OK))
|
||||||
|
ft_putstr_fd(": no such file\n", 2);
|
||||||
|
else
|
||||||
|
ft_putstr_fd(": Permission denied\n", 2);
|
||||||
|
free(cmd->infile);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *set_input(char *line, t_s_cmd *cmd, int index)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = index + 1;
|
||||||
|
if (line[i] == '<')
|
||||||
|
i++;
|
||||||
|
while (line[i] == ' ' && line[i])
|
||||||
|
i++;
|
||||||
|
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||||
|
i++;
|
||||||
|
if (cmd->infile)
|
||||||
|
free(cmd->infile);
|
||||||
|
cmd->infile = get_word(line, index);
|
||||||
|
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
|
||||||
|
if (cmd->in_type == 0 && !check_access_input(cmd))
|
||||||
|
{
|
||||||
|
free(line);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
line = cut_str(line, index, i);
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_input(char *line, t_s_cmd *cmd, int index)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char next;
|
||||||
|
|
||||||
|
i = index;
|
||||||
|
next = next_space(line, i + 1);
|
||||||
|
if (line[i + 1] == '<')
|
||||||
|
{
|
||||||
|
cmd->in_type = 1;
|
||||||
|
next = next_space(line, i + 2);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cmd->in_type = 0;
|
||||||
|
if (next == '<' || next == '>' || !next)
|
||||||
|
return (error_parsing(line));
|
||||||
|
line = set_input(line, cmd, i);
|
||||||
|
if (!line)
|
||||||
|
return (0);
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
96
srcs/set_redirection/set_output.c
Normal file
96
srcs/set_redirection/set_output.c
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* set_output.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/04/19 14:45:10 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/23 13:08:53 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#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)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = index;
|
||||||
|
i++;
|
||||||
|
if (line[i] == '>')
|
||||||
|
i++;
|
||||||
|
while (line[i] == ' ' && line[i])
|
||||||
|
i++;
|
||||||
|
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||||
|
i++;
|
||||||
|
if (cmd->outfile)
|
||||||
|
free(cmd->outfile);
|
||||||
|
cmd->outfile = get_word(line, index);
|
||||||
|
cmd->outfile = set_var(cmd->big_cmd, cmd->outfile);
|
||||||
|
if (!check_access_output(cmd))
|
||||||
|
{
|
||||||
|
free(line);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
line = cut_str(line, index, i);
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_output(char *line, t_s_cmd *cmd, int index)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char next;
|
||||||
|
|
||||||
|
i = index;
|
||||||
|
next = next_space(line, i + 1);
|
||||||
|
if (line[i + 1] == '>')
|
||||||
|
{
|
||||||
|
cmd->in_type = 1;
|
||||||
|
next = next_space(line, i + 2);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cmd->in_type = 0;
|
||||||
|
if (next == '<' || next == '>' || !next)
|
||||||
|
return (error_parsing(line));
|
||||||
|
line = set_output(line, cmd, i);
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
78
srcs/set_redirection/utils.c
Normal file
78
srcs/set_redirection/utils.c
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/11 16:35:37 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/04/20 15:49:47 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
char *cut_str(char *str, int start, int end)
|
||||||
|
{
|
||||||
|
char *swap;
|
||||||
|
char *del;
|
||||||
|
|
||||||
|
swap = 0;
|
||||||
|
del = str;
|
||||||
|
if (str[end])
|
||||||
|
swap = ft_strjoin(&str[end], 0);
|
||||||
|
str[start] = 0;
|
||||||
|
str = ft_strjoin(str, swap);
|
||||||
|
free(del);
|
||||||
|
if (swap)
|
||||||
|
free(swap);
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
char next_space(char *str, int i)
|
||||||
|
{
|
||||||
|
while (str[i] == ' ')
|
||||||
|
i++;
|
||||||
|
return (str[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int double_size(char **tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (tab == 0)
|
||||||
|
return (0);
|
||||||
|
while (tab[i])
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_double(char **tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (tab)
|
||||||
|
{
|
||||||
|
while (tab[i])
|
||||||
|
free(tab[i++]);
|
||||||
|
free(tab);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_double_fd(char **tab, int fd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (tab)
|
||||||
|
{
|
||||||
|
while (tab[i])
|
||||||
|
{
|
||||||
|
ft_putstr_fd(tab[i], fd);
|
||||||
|
ft_putstr_fd("\n", fd);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,19 +1,29 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_isacii.c :+: :+: :+: */
|
/* set_signal.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/05/20 12:08:41 by sadjigui #+# #+# */
|
/* Created: 2022/04/19 15:30:30 by apommier #+# #+# */
|
||||||
/* Updated: 2021/06/07 12:49:44 by sadjigui ### ########.fr */
|
/* Updated: 2022/04/23 13:12:16 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
int ft_isascii(int c)
|
#include "../../includes/minishell.h"
|
||||||
|
|
||||||
|
void crtl_c(int num)
|
||||||
{
|
{
|
||||||
if (c >= 0 && c <= 127)
|
(void)num;
|
||||||
return (1);
|
g_var = 130;
|
||||||
else
|
printf("\n");
|
||||||
return (0);
|
rl_replace_line("", 0);
|
||||||
|
rl_on_new_line();
|
||||||
|
rl_redisplay();
|
||||||
|
}
|
||||||
|
|
||||||
|
void sig_quit(int num)
|
||||||
|
{
|
||||||
|
(void)num;
|
||||||
|
ft_putstr_fd("\b \b\b \b", 1);
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user