This commit is contained in:
joris schaft 2024-04-23 16:04:24 +02:00
commit 17770aa6ae
6 changed files with 348 additions and 0 deletions

BIN
a.out Executable file

Binary file not shown.

97
ft_split.c Normal file
View file

@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/03 10:13:52 by jschaft #+# #+# */
/* Updated: 2024/04/23 16:00:34 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
#include "minish.h"
static int get_word_count(char const *s, char c)
{
int count;
int i;
count = 0;
i = 0;
while (s[i] != '\0')
{
while (s[i] == c)
i++;
if (s[i] != '\0' && s[i] != c)
{
count++;
while (s[i] != '\0' && s[i] != c)
i++;
}
}
return (count);
}
static char *strdup_split(const char *src, int start, int len)
{
char *dst;
int i;
dst = (char *)malloc(len + 1);
if (dst == NULL)
{
free(dst);
return (NULL);
}
i = 0;
while (i < len)
{
dst[i] = src[start + i];
i++;
}
dst[i] = '\0';
return (dst);
}
static void fill_re(char const *s, char c, char **re)
{
int i;
int y;
int start;
i = 0;
y = 0;
while (s[i] != '\0')
{
while (s[i] == c)
i++;
if (s[i] != '\0' && s[i] != c)
{
start = i;
while (s[i] != '\0' && s[i] != c)
i++;
re[y] = strdup_split(s, start, i - start);
if (re[y] == NULL)
return ;
y++;
}
}
re[y] = NULL;
}
char **ft_split(char const *s, char c)
{
char **re;
if (s == NULL)
return (NULL);
re = malloc(sizeof(char *) * (get_word_count(s, c) + 1));
if (re == NULL)
{
free(re);
return (NULL);
}
fill_re(s, c, re);
return (re);
}

54
main.c Normal file
View file

@ -0,0 +1,54 @@
#include "minish.h"
void execclear(char *p, char **envp)
{
execve(p, &p, envp);
}
static void print_hi(void)
{
printf("Welcome to____________________________________\n");
printf("|| ___ ___ ||\n");
printf("|| | \\ / | ||\n");
printf("|| | |\\ \\ / /| | __ __ __ __ ||\n");
printf("|| | | \\ \\ / / | | |__| ||\\ || |__| ||\n");
printf("|| | | \\ \\/ / | | __ || \\ || __ ||\n");
printf("|| | | \\ / | | || || \\ || || ||\n");
printf("|| | | \\/ | | || || \\ || || ||\n");
printf("|| | | | | || || \\ || || ||\n");
printf("|| |_| |_| || || \\|| || ||\n");
printf("|| _____ ||\n");
printf("|| / ___| By: Mylan COLONNA ||\n");
printf("|| \\ \\ And: Joris SCHAFT ||\n");
printf("|| \\ \\ ||\n");
printf("|| \\ \\ __ __ __ __ __ ||\n");
printf("|| \\ \\ || || ||=== || || ||\n");
printf("|| \\ \\ || || || || || ||\n");
printf("|| / / ||===|| ||=== || || ||\n");
printf("|| ___/ / || || || || || ||\n");
printf("|| |____/ || || ||=== ||=== ||=== ||\n");
printf("|_____________________________________________|\n\n");
}
void hello_message(char **path, char **envp)
{
char **p = cp_path(path);
int i = 0;
pid_t pid;
while (p[i] != NULL)
{
p[i] = ft_strjoin(p[i], "/");
p[i] = ft_strjoin(p[i], "clear");
i++;
}
i = search_path(p);
pid = fork();
if (pid == 0)
execclear(p[i], envp);
}
int main(char **envp)
{
char **path = get_path(envp);
}

22
minish.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef MINISH_H
# define MINISH_H
# include <unistd.h>
# include <fcntl.h>
# include <stdlib.h>
# include <strings.h>
# include <stdio.h>
# include <sys/types.h>
char **get_path(char **envp);
int search_path(char **path);
char **cp_path(char **path);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
int ft_strncmp(char *s1, char *s2, size_t n);
char **ft_split(char const *s, char c);
size_t ft_strlen(char const *s);
char *ft_strdup(const char *s);
#endif

61
path.c Normal file
View file

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */
/* Updated: 2024/04/23 15:08:11 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
#include "minish.h"
char **get_path(char **envp)
{
int i;
char **re;
char *path;
i = 0;
while (ft_strncmp(envp[i], "PATH=", 5) != 0)
i++;
path = ft_substr(envp[i], 5, ft_strlen(envp[i]) - 5);
re = ft_split(path, ':');
free(path);
return (re);
}
int search_path(char **path)
{
int i;
i = 0;
while (path[i] != NULL)
{
if (access(path[i], X_OK) >= 0)
return (i);
i++;
}
return (-1);
}
char **cp_path(char **path)
{
char **re;
int i;
i = 0;
while (path[i] != NULL)
i++;
re = malloc(sizeof(char *) * i + 1);
i = 0;
while (path[i] != NULL)
{
re[i] = ft_strdup(path[i]);
i++;
}
re[i] == NULL;
return (re);
}

114
util.c Normal file
View file

@ -0,0 +1,114 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* util.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */
/* Updated: 2024/04/23 16:00:41 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
#include "minish.h"
size_t ft_strlen(char const *s)
{
int i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
int ft_strncmp(char *s1, char *s2, size_t n)
{
int i;
unsigned char *st1;
unsigned char *st2;
i = 0;
st1 = (unsigned char *)s1;
st2 = (unsigned char *)s2;
if (n == 0)
{
return (0);
}
while (st1[i] == st2[i] && st1[i] != '\0' && st2[i] != '\0'
&& i < (int)n - 1)
i++;
return (st1[i] - st2[i]);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
int i;
char *null_re;
char *re;
i = 0;
null_re = malloc(sizeof(char));
re = malloc(sizeof(char) * (len + 1));
if (re == NULL || null_re == NULL)
return (NULL);
null_re[0] = '\0';
if (start >= ft_strlen(s))
return (null_re);
while (s[i + start] != '\0' && i < (int)len)
{
re[i] = s[i + start];
i++;
}
re[i] = '\0';
return (re);
}
char *ft_strjoin(char const *s1, char const *s2)
{
int i;
int y;
int size;
char *re;
i = 0;
y = 0;
size = ft_strlen(s1) + ft_strlen(s2) + 1;
re = malloc(sizeof(char) * size);
if (re == NULL)
return (NULL);
while (s1[i] != '\0')
{
re[i] = s1[i];
i++;
}
while (s2[y] != '\0')
{
re[i + y] = s2[y];
y++;
}
re[i + y] = '\0';
return (re);
}
char *ft_strdup(const char *s)
{
int i;
int len;
char *re;
len = 0;
while (s[len] != '\0')
len++;
re = malloc(sizeof(char) * (len + 1));
if (re == NULL)
return (NULL);
i = 0;
while (i < len)
{
re[i] = s[i];
i++;
}
re[i] = '\0';
return (re);
}