summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-04-20 14:51:45 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-04-20 14:51:45 +0200
commitc9e4fb6e2ad07e13cf41d6ce6afad30b04011765 (patch)
tree74d816c107c959315ae52166dac96fe2fe0c2a5e /src
parentWell well well that wasn't too bad, now remake everything (diff)
download42-minishell-c9e4fb6e2ad07e13cf41d6ce6afad30b04011765.tar.gz
42-minishell-c9e4fb6e2ad07e13cf41d6ce6afad30b04011765.tar.bz2
42-minishell-c9e4fb6e2ad07e13cf41d6ce6afad30b04011765.tar.xz
42-minishell-c9e4fb6e2ad07e13cf41d6ce6afad30b04011765.tar.zst
42-minishell-c9e4fb6e2ad07e13cf41d6ce6afad30b04011765.zip
Solid base
Diffstat (limited to '')
-rw-r--r--src/ft_d_enum.h29
-rw-r--r--src/ft_echo.c40
-rw-r--r--src/ft_error.c34
-rw-r--r--src/ft_exec.c39
-rw-r--r--src/ft_exit.c39
-rw-r--r--src/ft_history.c123
-rw-r--r--src/ft_process_arg.c51
-rw-r--r--src/ft_pwd.c26
-rw-r--r--src/ft_s_destroy.c20
-rw-r--r--src/ft_s_destroy.h20
-rw-r--r--src/ft_s_init.c25
-rw-r--r--src/ft_s_init.h21
-rw-r--r--src/ft_s_struct.h21
-rw-r--r--src/main.c41
-rw-r--r--src/minishell.c48
-rw-r--r--src/minishell.h20
16 files changed, 204 insertions, 393 deletions
diff --git a/src/ft_d_enum.h b/src/ft_d_enum.h
new file mode 100644
index 0000000..ef276c8
--- /dev/null
+++ b/src/ft_d_enum.h
@@ -0,0 +1,29 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_d_enum.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef FT_D_ENUM_H
+#define FT_D_ENUM_H
+
+/*
+** ret vals:
+** ---------
+** 0: cool
+** 1: alloc err
+*/
+
+enum
+{
+ FT_RET_FINE,
+ FT_RET_ALLOC
+}
+
+#endif
diff --git a/src/ft_echo.c b/src/ft_echo.c
deleted file mode 100644
index e8ef75c..0000000
--- a/src/ft_echo.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/* ************************************************************************** */
-/* LE - / */
-/* / */
-/* ft_echo.c .:: .:/ . .:: */
-/* +:+:+ +: +: +:+:+ */
-/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */
-/* #+# #+ #+ #+# */
-/* Created: 2019/11/01 18:46:54 by rbousset #+# ## ## #+# */
-/* Updated: 2019/11/01 18:46:55 by rbousset ### #+. /#+ ###.fr */
-/* / */
-/* / */
-/* ************************************************************************** */
-
-#include <libft.h>
-#include <minishell.h>
-#include <inttypes.h>
-
-int
-ft_echo(char **com, uint8_t n)
-{
- uint8_t i;
- int fd;
-
- i = 1;
- fd = 1;
- if (!com[1])
- ft_dprintf(fd, "\n");
- else if (!ft_strncmp(com[1], "-n", ft_strlen(com[1])))
- i = 2;
- while (i < n)
- {
- ft_dprintf(fd, "%s", ft_strtrim(com[i], "\""));
- if (i != n - 1)
- ft_dprintf(fd, " ");
- i++;
- }
- if (ft_strncmp(com[1], "-n", ft_strlen(com[1])))
- ft_dprintf(fd, "\n");
- return (0);
-}
diff --git a/src/ft_error.c b/src/ft_error.c
deleted file mode 100644
index 933a22b..0000000
--- a/src/ft_error.c
+++ /dev/null
@@ -1,34 +0,0 @@
-/* ************************************************************************** */
-/* LE - / */
-/* / */
-/* ft_error.c .:: .:/ . .:: */
-/* +:+:+ +: +: +:+:+ */
-/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */
-/* #+# #+ #+ #+# */
-/* Created: 2019/10/30 17:21:53 by rbousset #+# ## ## #+# */
-/* Updated: 2019/11/01 18:47:03 by rbousset ### #+. /#+ ###.fr */
-/* / */
-/* / */
-/* ************************************************************************** */
-
-#include <libft.h>
-#include <minishell.h>
-
-int
-ft_error(const char *com, int err)
-{
- ft_putstr("joe-sh: ");
- ft_putstr(com);
- if (err == 1)
- ft_putendl(": too many arguments");
- else if (err == 127)
- {
- if (!ft_strncmp(com, "./", 2))
- ft_putendl(": no such file or directory");
- else
- ft_putendl(": command not found");
- }
- else if (err == 255)
- ft_putendl(": numeric argument required");
- return (err);
-}
diff --git a/src/ft_exec.c b/src/ft_exec.c
deleted file mode 100644
index 408697d..0000000
--- a/src/ft_exec.c
+++ /dev/null
@@ -1,39 +0,0 @@
-/* ************************************************************************** */
-/* LE - / */
-/* / */
-/* ft_exec.c .:: .:/ . .:: */
-/* +:+:+ +: +: +:+:+ */
-/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */
-/* #+# #+ #+ #+# */
-/* Created: 2019/11/01 18:46:50 by rbousset #+# ## ## #+# */
-/* Updated: 2019/11/01 18:47:21 by rbousset ### #+. /#+ ###.fr */
-/* / */
-/* / */
-/* ************************************************************************** */
-
-#include <libft.h>
-#include <stdlib.h>
-#include <minishell.h>
-#include <unistd.h>
-
-int
-ft_exec(char **com)
-{
- uint8_t i;
-
- i = 0;
- while (com[i])
- i++;
- if (fork() == 0)
- {
- if (!ft_strncmp(com[0], "./", 2))
- {
- if ((execve(com[0] + 2, com, NULL)) != 0)
- return (ft_error(com[0], 127));
- }
- else
- if ((execve(com[0], com, NULL)) != 0)
- return (ft_error(com[0], 127));
- }
- return (0);
-}
diff --git a/src/ft_exit.c b/src/ft_exit.c
deleted file mode 100644
index 5285289..0000000
--- a/src/ft_exit.c
+++ /dev/null
@@ -1,39 +0,0 @@
-/* ************************************************************************** */
-/* LE - / */
-/* / */
-/* ft_exit.c .:: .:/ . .:: */
-/* +:+:+ +: +: +:+:+ */
-/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */
-/* #+# #+ #+ #+# */
-/* Created: 2019/10/30 16:05:48 by rbousset #+# ## ## #+# */
-/* Updated: 2019/11/01 18:47:18 by rbousset ### #+. /#+ ###.fr */
-/* / */
-/* / */
-/* ************************************************************************** */
-
-#include <libft.h>
-#include <minishell.h>
-#include <stdlib.h>
-
-uint8_t
-ft_exit(char **com)
-{
- uint8_t i;
-
- i = 0;
- while (com[i])
- i++;
- if (i == 2)
- {
- i = 0;
- while (ft_isdigit(com[1][i]))
- i++;
- if (i != ft_strlen(com[1]))
- exit(ft_error(com[0], 255));
- else
- exit((uint8_t)ft_atoi(com[1]));
- }
- else if (i > 2)
- return (ft_error(com[0], 1));
- exit(0);
-}
diff --git a/src/ft_history.c b/src/ft_history.c
deleted file mode 100644
index 936b9a2..0000000
--- a/src/ft_history.c
+++ /dev/null
@@ -1,123 +0,0 @@
-/* ************************************************************************** */
-/* LE - / */
-/* / */
-/* ft_history.c .:: .:/ . .:: */
-/* +:+:+ +: +: +:+:+ */
-/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */
-/* #+# #+ #+ #+# */
-/* Created: 2019/10/30 20:36:05 by rbousset #+# ## ## #+# */
-/* Updated: 2019/10/30 20:36:07 by rbousset ### #+. /#+ ###.fr */
-/* / */
-/* / */
-/* ************************************************************************** */
-
-#include <libft.h>
-#include <minishell.h>
-#include <stddef.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-
-static size_t
-ft_count_lines(int fd)
-{
- char c;
- size_t lines;
-
- lines = 0;
- while (read(fd, &c, 1) > 0)
- if (c == '\n')
- lines++;
- return (lines);
-}
-
-static size_t
-ft_last_line_len(int fd, size_t lines_max)
-{
- char c;
- size_t len;
- size_t lines;
-
- len = 0;
- lines = 0;
- while (read(fd, &c, 1) > 0)
- {
- if (c == '\n')
- lines++;
- if (lines == lines_max - 1)
- break ;
- }
- while (read(fd, &c, 1) > 0)
- len++;
- return (len);
-}
-
-
-char
-*ft_get_last_line(void)
-{
- char *line;
- char c;
- int fd;
- size_t lines;
- size_t i;
-
- if ((fd = open("joe-sh_history", O_CREAT | O_RDWR, 0644)) == -1)
- return (NULL);
- i = 0;
- lines = ft_count_lines(fd);
- close(fd);
- if ((fd = open("joe-sh_history", O_CREAT | O_RDWR, 0644)) == -1)
- return (NULL);
- if (!(line = (char*)malloc(ft_last_line_len(fd, lines) * sizeof(char))))
- return (NULL);
- close(fd);
- if ((fd = open("joe-sh_history", O_CREAT | O_RDWR, 0644)) == -1)
- return (NULL);
- while (read(fd, &c, 1) > 0)
- {
- if (c == '\n')
- i++;
- if (i == lines - 1)
- break ;
- }
- i = 0;
- while (read(fd, &c, 1) > 0)
- {
- line[i] = c;
- i++;
- }
- line[i - 1] = '\0';
- close(fd);
- return (line);
-}
-
-/*
-** Prints user-given line into
-** joe-sh_hisotry file
-*/
-
-int
-ft_history(char *arg)
-{
- char *buff;
- int fd;
- int ret;
- struct stat info;
-
- if (!*arg)
- return (0);
- if ((fd = open("joe-sh_history", O_CREAT | O_RDWR, 0644)) == -1)
- return (1);
- fstat(fd, &info);
- if (!(buff = (char*)malloc(info.st_size * sizeof(char))))
- return (0);
- ret = read(fd, buff, info.st_size);
- ft_putendl_fd(arg, fd);
- free(buff);
- close(fd);
- return (ret);
-}
diff --git a/src/ft_process_arg.c b/src/ft_process_arg.c
deleted file mode 100644
index 8057e42..0000000
--- a/src/ft_process_arg.c
+++ /dev/null
@@ -1,51 +0,0 @@
-/* ************************************************************************** */
-/* LE - / */
-/* / */
-/* ft_process_arg.c .:: .:/ . .:: */
-/* +:+:+ +: +: +:+:+ */
-/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */
-/* #+# #+ #+ #+# */
-/* Created: 2019/11/01 18:47:30 by rbousset #+# ## ## #+# */
-/* Updated: 2019/11/01 18:47:32 by rbousset ### #+. /#+ ###.fr */
-/* / */
-/* / */
-/* ************************************************************************** */
-
-#include <libft.h>
-#include <stdlib.h>
-#include <minishell.h>
-#include <inttypes.h>
-
-int
-ft_process_arg(char *arg)
-{
- char **com;
- char *prev;
- uint8_t i;
-
- if (arg[0] == '\0')
- return (0);
- if (ft_strncmp(arg, "r", 1))
- ft_history(arg);
- i = 0;
- com = ft_split(arg, ' ');
- while (com[i])
- i++;
- if (!ft_strncmp(com[0], "exit", ft_strlen(com[0])))
- return (ft_exit(com));
- else if (!ft_strncmp(com[0], "echo", ft_strlen(com[0])))
- return (ft_echo(com, i));
- else if (!ft_strncmp(com[0], "pwd", ft_strlen(com[0])))
- return (ft_pwd());
- else if (!ft_strncmp(com[0], "r", 1))
- {
- prev = ft_get_last_line();
- ft_putendl(prev);
- ft_history("r");
- ft_process_arg(prev);
- free(prev);
- }
- else
- return (ft_exec(com));
- return (0);
-}
diff --git a/src/ft_pwd.c b/src/ft_pwd.c
deleted file mode 100644
index 07e5622..0000000
--- a/src/ft_pwd.c
+++ /dev/null
@@ -1,26 +0,0 @@
-/* ************************************************************************** */
-/* LE - / */
-/* / */
-/* ft_pwd.c .:: .:/ . .:: */
-/* +:+:+ +: +: +:+:+ */
-/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */
-/* #+# #+ #+ #+# */
-/* Created: 2019/11/01 18:47:41 by rbousset #+# ## ## #+# */
-/* Updated: 2019/11/01 18:47:47 by rbousset ### #+. /#+ ###.fr */
-/* / */
-/* / */
-/* ************************************************************************** */
-
-#include <libft.h>
-#include <stddef.h>
-#include <unistd.h>
-
-int
-ft_pwd(void)
-{
- char *buff;
-
- buff = NULL;
- ft_putendl(getcwd(buff, 1000));
- return (0);
-}
diff --git a/src/ft_s_destroy.c b/src/ft_s_destroy.c
new file mode 100644
index 0000000..ffb8381
--- /dev/null
+++ b/src/ft_s_destroy.c
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_s_destroy.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include "ft_s_destroy.h"
+
+void
+ft_s_destroy(t_data *data)
+{
+ ft_memdel((void*)&data);
+}
diff --git a/src/ft_s_destroy.h b/src/ft_s_destroy.h
new file mode 100644
index 0000000..0d6d030
--- /dev/null
+++ b/src/ft_s_destroy.h
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_s_destroy.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef FT_S_DESTROY_H
+#define FT_S_DESTROY_H
+
+#include "ft_s_struct.h"
+
+void ft_s_destroy(t_data *data);
+
+#endif
diff --git a/src/ft_s_init.c b/src/ft_s_init.c
new file mode 100644
index 0000000..82293be
--- /dev/null
+++ b/src/ft_s_init.c
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_s_init.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <ft_s_init.h>
+
+t_data
+*ft_init_data(void)
+{
+ t_data *data;
+
+ if (!(data = (t_data*)malloc(sizeof(t_data))))
+ {
+ return (NULL);
+ }
+ return (data);
+}
diff --git a/src/ft_s_init.h b/src/ft_s_init.h
new file mode 100644
index 0000000..8297a5e
--- /dev/null
+++ b/src/ft_s_init.h
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_s_init.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef FT_S_INIT_H
+#define FT_S_INIT_H
+
+#include <ft_s_struct.h>
+#include <stdlib.h>
+
+t_data *ft_init_data(void);
+
+#endif
diff --git a/src/ft_s_struct.h b/src/ft_s_struct.h
new file mode 100644
index 0000000..3d3d8a7
--- /dev/null
+++ b/src/ft_s_struct.h
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_s_struct.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef FT_S_STRUCT_H
+#define FT_S_STRUCT_H
+
+typedef struct s_data
+{
+ char **envp;
+} t_data;
+
+#endif
diff --git a/src/main.c b/src/main.c
deleted file mode 100644
index beafbb5..0000000
--- a/src/main.c
+++ /dev/null
@@ -1,41 +0,0 @@
-/* ************************************************************************** */
-/* LE - / */
-/* / */
-/* main.c .:: .:/ . .:: */
-/* +:+:+ +: +: +:+:+ */
-/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */
-/* #+# #+ #+ #+# */
-/* Created: 2019/10/29 08:47:37 by rbousset #+# ## ## #+# */
-/* Updated: 2019/10/29 08:47:39 by rbousset ### #+. /#+ ###.fr */
-/* / */
-/* / */
-/* ************************************************************************** */
-
-#include <libft.h>
-#include <minishell.h>
-#include <stdlib.h>
-#include <stddef.h>
-#include <stdint.h>
-#include <unistd.h>
-
-int
-main(int argc,
- const char *argv[],
- const char *envp[])
-{
- char *arg;
- int8_t gnlret;
-
- (void)argc;
- (void)argv;
- (void)envp;
- ft_printf(FT_PS1);
- while ((gnlret = get_next_line(STDIN_FILENO, &arg)) > 0)
- {
- ft_process_arg(arg);
- ft_memdel((void*)&arg);
- ft_printf(FT_PS1);
- }
- free(arg);
- return (0);
-}
diff --git a/src/minishell.c b/src/minishell.c
new file mode 100644
index 0000000..e5b4777
--- /dev/null
+++ b/src/minishell.c
@@ -0,0 +1,48 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* minishell.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include "minishell.h"
+
+int
+main(int argc,
+ const char *argv[],
+ char *envp[])
+{
+ t_data *data;
+
+ (void)argc;
+ (void)argv;
+ (void)envp;
+ if (!(data = ft_init_data()))
+ {
+ return (1);
+ }
+ data->envp = envp;
+ while (*data->envp)
+ {
+ ft_printf("%s\n", *data->envp);
+ data->envp++;
+ }
+ ft_s_destroy(data);
+ return (0);
+}
+
+/*
+** ====== INFO ======
+** Files prefixes info
+** -------------------
+** ft_ -> 42
+** s_ -> structs related
+** d_ -> defines related
+** p_ -> parse related
+*/
diff --git a/src/minishell.h b/src/minishell.h
new file mode 100644
index 0000000..5e50303
--- /dev/null
+++ b/src/minishell.h
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* minishell.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef MINISHELL_H
+#define MINISHELL_H
+
+#include "ft_s_struct.h"
+#include "ft_s_init.h"
+#include "ft_s_destroy.h"
+
+#endif