file norming
This commit is contained in:
36
srcs/check_path_slash_cd.c
Normal file
36
srcs/check_path_slash_cd.c
Normal file
@@ -0,0 +1,36 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* check_path_slach_cd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/02/01 15:29:57 by tmaze #+# #+# */
|
||||
/* Updated: 2020/02/01 15:31:19 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
char *check_path_slash_cd(char *exec)
|
||||
{
|
||||
int i;
|
||||
struct stat info;
|
||||
struct stat info2;
|
||||
|
||||
if (exec[0] == '/')
|
||||
{
|
||||
if ((i = access(exec, F_OK)) != 0)
|
||||
return (put_error_cd(exec, "no such file or directory"));
|
||||
if ((i = lstat(exec, &info)) != 0)
|
||||
return (put_error_cd(exec, "can't determine info"));
|
||||
if ((S_ISLNK(info.st_mode)
|
||||
&& ((i = stat(exec, &info2)) != 0 || !S_ISDIR(info2.st_mode)))
|
||||
&& !S_ISDIR(info.st_mode))
|
||||
return (put_error_cd(exec, "not a directory"));
|
||||
if ((i = access(exec, X_OK)) != 0)
|
||||
return (put_error_cd(exec, "permission denied"));
|
||||
return (exec);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
@@ -6,35 +6,12 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */
|
||||
/* Updated: 2020/01/26 22:17:48 by tmaze ### ########.fr */
|
||||
/* Updated: 2020/02/01 15:32:59 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
char *check_path_slash_cd(char *exec)
|
||||
{
|
||||
int i;
|
||||
struct stat info;
|
||||
struct stat info2;
|
||||
|
||||
if (exec[0] == '/')
|
||||
{
|
||||
if ((i = access(exec, F_OK)) != 0)
|
||||
return (put_error_cd(exec, "no such file or directory"));
|
||||
if ((i = lstat(exec, &info)) != 0)
|
||||
return (put_error_cd(exec, "can't determine info"));
|
||||
if ((S_ISLNK(info.st_mode)
|
||||
&& ((i = stat(exec, &info2)) != 0 || !S_ISDIR(info2.st_mode)))
|
||||
&& !S_ISDIR(info.st_mode))
|
||||
return (put_error_cd(exec, "not a directory"));
|
||||
if ((i = access(exec, X_OK)) != 0)
|
||||
return (put_error_cd(exec, "permission denied"));
|
||||
return (exec);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int cmd_cd_update_env(char *path, t_env **env, char opt)
|
||||
{
|
||||
t_env *pwd;
|
||||
|
40
srcs/def_env.c
Normal file
40
srcs/def_env.c
Normal file
@@ -0,0 +1,40 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* def_env.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/02/01 15:53:22 by tmaze #+# #+# */
|
||||
/* Updated: 2020/02/01 15:53:34 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
t_env *def_env(void)
|
||||
{
|
||||
t_env *ret;
|
||||
t_env *n;
|
||||
char p[PATH_MAX];
|
||||
|
||||
n = ft_memalloc(sizeof(t_env));
|
||||
if (n != NULL && ((n->key = ft_strnew(3)) == NULL || !getcwd(p, PATH_MAX)))
|
||||
ft_envdelelem(&n);
|
||||
if (n != NULL)
|
||||
ft_strcpy(n->key, "PWD");
|
||||
if (n != NULL && (n->val = ft_strdup(p)) == NULL)
|
||||
ft_envdelelem(&n);
|
||||
if (n != NULL)
|
||||
ft_envaddend(&ret, n);
|
||||
n = ft_memalloc(sizeof(t_env));
|
||||
if (n != NULL && (n->key = ft_strnew(5)) == NULL)
|
||||
ft_envdelelem(&n);
|
||||
if (n != NULL)
|
||||
ft_strcpy(n->key, "SHLVL");
|
||||
if (n != NULL && (n->val = ft_strdup("1")) == NULL)
|
||||
ft_envdelelem(&n);
|
||||
if (n != NULL)
|
||||
ft_envaddend(&ret, n);
|
||||
return (ret);
|
||||
}
|
29
srcs/main.c
29
srcs/main.c
@@ -6,39 +6,12 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */
|
||||
/* Updated: 2020/01/30 13:47:35 by tmaze ### ########.fr */
|
||||
/* Updated: 2020/02/01 15:54:34 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
t_env *def_env(void)
|
||||
{
|
||||
t_env *ret;
|
||||
t_env *n;
|
||||
char p[PATH_MAX];
|
||||
|
||||
n = ft_memalloc(sizeof(t_env));
|
||||
if (n != NULL && ((n->key = ft_strnew(3)) == NULL || !getcwd(p, PATH_MAX)))
|
||||
ft_envdelelem(&n);
|
||||
if (n != NULL)
|
||||
ft_strcpy(n->key, "PWD");
|
||||
if (n != NULL && (n->val = ft_strdup(p)) == NULL)
|
||||
ft_envdelelem(&n);
|
||||
if (n != NULL)
|
||||
ft_envaddend(&ret, n);
|
||||
n = ft_memalloc(sizeof(t_env));
|
||||
if (n != NULL && (n->key = ft_strnew(5)) == NULL)
|
||||
ft_envdelelem(&n);
|
||||
if (n != NULL)
|
||||
ft_strcpy(n->key, "SHLVL");
|
||||
if (n != NULL && (n->val = ft_strdup("1")) == NULL)
|
||||
ft_envdelelem(&n);
|
||||
if (n != NULL)
|
||||
ft_envaddend(&ret, n);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
char *inc_shlvl(char **shlvl)
|
||||
{
|
||||
char *ret;
|
||||
|
@@ -6,32 +6,12 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/09/26 16:25:00 by tmaze #+# #+# */
|
||||
/* Updated: 2020/01/30 11:48:25 by tmaze ### ########.fr */
|
||||
/* Updated: 2020/02/01 15:47:08 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int ft_isin(char *str, char c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str && str[i])
|
||||
if (str[i++] == c)
|
||||
return (i - 1);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
void *put_error(char *file, char *msg)
|
||||
{
|
||||
ft_putstr("minishell: ");
|
||||
ft_putstr(file);
|
||||
ft_putstr(": ");
|
||||
ft_putendl(msg);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
char *check_path_slash(char *exec)
|
||||
{
|
||||
int i;
|
||||
@@ -82,12 +62,6 @@ char *check_path_dot(char *exec, t_env *env)
|
||||
return (put_error(exec, "no such file or directory"));
|
||||
}
|
||||
|
||||
void *ft_strdel_null(char **s)
|
||||
{
|
||||
ft_strdel(s);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
char *check_path_elems(char **path_elems, char *exec)
|
||||
{
|
||||
int i;
|
||||
@@ -146,17 +120,6 @@ char *check_path(char *exec, t_env *env)
|
||||
return (ret);
|
||||
}
|
||||
|
||||
void exec_cmd_child(char *path, char **argv, char **env_tab, t_env **env)
|
||||
{
|
||||
execve(path, argv, env_tab);
|
||||
ft_putendl_fd("minishell: error", 1);
|
||||
if (ft_strcmp(path, argv[0]) != 0)
|
||||
ft_strdel(&path);
|
||||
ft_del_words_tables(&env_tab);
|
||||
ft_envdel(env);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
int exec_cmd(char **argv, t_env **env)
|
||||
{
|
||||
int ret;
|
||||
|
56
srcs/ms_exec_utils.c
Normal file
56
srcs/ms_exec_utils.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ms_exec_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/02/01 15:43:41 by tmaze #+# #+# */
|
||||
/* Updated: 2020/02/01 15:56:49 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void exec_cmd_child(char *path, char **argv, char **env_tab, t_env **env)
|
||||
{
|
||||
execve(path, argv, env_tab);
|
||||
ft_putendl_fd("minishell: error", 1);
|
||||
if (ft_strcmp(path, argv[0]) != 0)
|
||||
ft_strdel(&path);
|
||||
ft_del_words_tables(&env_tab);
|
||||
ft_envdel(env);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
void *ft_strdel_null(char **s)
|
||||
{
|
||||
ft_strdel(s);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int ft_isin(char *str, char c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str && str[i])
|
||||
if (str[i++] == c)
|
||||
return (i - 1);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
void *put_error(char *file, char *msg)
|
||||
{
|
||||
ft_putstr("minishell: ");
|
||||
ft_putstr(file);
|
||||
ft_putstr(": ");
|
||||
ft_putendl(msg);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void *return_null(char *msg)
|
||||
{
|
||||
ft_printf("%s\n", msg);
|
||||
return (NULL);
|
||||
}
|
@@ -6,18 +6,12 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/10/25 18:21:47 by tmaze #+# #+# */
|
||||
/* Updated: 2020/01/30 15:42:17 by tmaze ### ########.fr */
|
||||
/* Updated: 2020/02/01 15:56:43 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void *return_null(char *msg)
|
||||
{
|
||||
ft_printf("%s\n", msg);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void ft_strreplace(char **dst, char *src, int *index, t_env *elem)
|
||||
{
|
||||
if (*dst != NULL)
|
||||
@@ -57,7 +51,6 @@ char *replace_env(char *src, t_env *elem, int key_size, int i)
|
||||
int res_len;
|
||||
|
||||
res_len = ft_strlen(src) - key_size + ft_strlen(elem->val);
|
||||
ft_printf("size replace: %d\n", res_len);
|
||||
if ((ret = ft_strnew(res_len)) != NULL)
|
||||
{
|
||||
ft_strncpy(ret, src, i);
|
||||
|
Reference in New Issue
Block a user