Ejemplo n.º 1
0
void	ft_parse_input(t_shun **shell)
{
	if (*(*shell)->input)
	{
		(*shell)->argv = ft_parse_get_args((*shell)->input);
		(*shell)->argc = ft_tablen((*shell)->argv);
		if ((*shell)->argc != 0)
		{
			if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "exit") == 0)
				ft_exit(&(*shell));
			else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "env") == 0)
				ft_print_environ(*shell);
			else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "setenv") == 0)
				ft_setenv(&(*shell));
			else if (ft_strcmp(ft_strtolower((*shell)->argv[0]),
							"unsetenv") == 0)
				ft_unsetenv(&(*shell));
			else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "cd") == 0)
				ft_cd(&(*shell));
			else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "prompt") == 0)
				ft_chg_prompt(&(*shell));
			else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "pwd") == 0)
				ft_putendl(ft_get_pwd());
			else
				ft_exec_bin(&(*shell));
		}
	}
}
Ejemplo n.º 2
0
int			is_exe2(char *dir, char *file, t_shell *shell)
{
	int			ret;
	char		*infos;
	struct stat *stats;

	ret = 0;
	if (!(stats = malloc(sizeof(struct stat))))
		print_error_malloc("[is_exe] struct stat");
	infos = ft_strjoin(ft_strjoin(dir, "/"), file);
	if (lstat(infos, stats) != 0 || (file[0] != '.' && file[1] != '/'))
		return (1);
	if (is_directory(infos))
		ret = 2;
	else if ((stats->st_mode & S_IXUSR) && (stats->st_mode & S_IXGRP)
		&& (stats->st_mode & S_IXOTH))
		ret = 1;
	if (ret == 0)
	{
		ft_putstr_fd("21sh: permission denied: ", 2);
		ft_putstr_fd(file, 2);
		ft_putstr_fd("\n", 2);
	}
	else if (ret == 2 && !(ret = 0))
		ft_cd(file, shell);
	ft_strdel(&infos);
	return (ret);
}
Ejemplo n.º 3
0
int		ft_builtin(t_env *env, char *line)
{
	int		i;
	char	**line2;

	i = 0;
	line2 = NULL;
	line2 = ft_strsplit(line, ' ');
	line2[0] = ft_strtrim(line2[0]);
	if (ft_strcmp(line2[0], "echo") == 0 && (i = 1))
	{
		line = ft_strfcut(line, 5);
		ft_echo(env, line);
	}
	else if (ft_strncmp(line, "cd", 2) == 0 && (i = 1))
		ft_cd(line, env);
	else if (ft_strcmp(line, "clear") == 0 && (i = 1))
		tputs(tgetstr("cl", NULL), 1, ft_ft_putchar);
	else if (ft_strncmp(line, "unsetenv", 7) == 0 && (i = 1))
	{
		line2 = ft_strsplit(line, ' ');
		env = ft_unsetenv(env, line2[1]);
	}
	else if (i == 0)
		i = ft_builtin2(env, line);
	return (i);
}
Ejemplo n.º 4
0
int			main(int ac, char **av, char **env)
{
	char		*line;
	char		**env_copy;

	ac = 0;
	av[0] = "allez";
	env_copy = ft_cpy_char2(env);
	ft_putstr("jaubert$> ");
	while (get_next_line(0, &line) && ft_strcmp(line, "exit") != 0)
	{
		if (ft_strcmp(line, "pwd") == 0)
			ft_pwd();
		else if (ft_strncmp(line, "cd", 2) == 0)
			ft_cd(line);
		else if (ft_strncmp(line, "env", 3) == 0)
			ft_env(line, env_copy);
		else if (ft_strncmp(line, "setenv", 6) == 0)
			env_copy = ft_setenv(line, env_copy);
		else if (line)
			ft_launch_bin(line, env_copy);
		ft_putstr("jaubert$> ");
		free(line);
	}
	return (0);
}
Ejemplo n.º 5
0
Archivo: main.c Proyecto: bibop-qu/SH1
void	ft_loop(char **cmd, t_env **e, char **tab_e)
{
	if (!ft_strcmp(cmd[0], "echo"))
		ft_echo(cmd, *e);
	else if (!ft_strcmp(cmd[0], "exit"))
	{
		free_list(*e);
		exit(0);
	}
	else if (!ft_strcmp(cmd[0], "env"))
	{
		tab_e = listtotab(*e);
		ft_env(*e, cmd, tab_e);
	}
	else if (!ft_strcmp(cmd[0], "setenv"))
		ft_setenv(e, cmd);
	else if (!ft_strcmp(cmd[0], "unsetenv"))
		ft_unsetenv(e, cmd);
	else if (!ft_strcmp(cmd[0], "cd") && *e)
		ft_cd(*e, cmd);
	else
	{
		tab_e = listtotab(*e);
		do_fork(cmd, *e, tab_e);
	}
	free_tab(cmd);
}
Ejemplo n.º 6
0
int is_builtin(char *line, t_env **env)
{
	(*env)->av = ft_strsplit(line, ' ');	
	if (ft_strcmp(ft_strtolower((*env)->av[0]), "setenv") == 0)
	{
		ft_setenv(&(*env));
		return (1);	
	}	
	else if (ft_strcmp(ft_strtolower((*env)->av[0]), "unsetenv") == 0)
	{
		ft_unsetenv(&(*env));
		return (1);
	}	
	else if (ft_strcmp(ft_strtolower((*env)->av[0]), "cd") == 0)
	{
		ft_cd(&(*env));
		return (1);
	}
	else if (ft_strcmp(ft_strtolower((*env)->av[0]), "exit") == 0)
	{
		exit (0);
		return (1);	
	}
	else
		return (0);
}
Ejemplo n.º 7
0
int				cmd(char *s, int cs)
{
	pid_t	pid;
	int		status;
	char	**spt;

	if (!s || !*s)
		return (0);
	spt = ft_getarg(s);
	if (!ft_strcmp(spt[0], "get"))
	{
		ft_get(spt, cs);
		return (0);
	}
	if (!ft_strcmp(spt[0], "cd"))
	{
		ft_cd(spt[1], cs);
		return (0);
	}
	if (!(pid = fork()))
		ft_launch(s, cs);
	else if (pid > 0)
		wait4(pid, &status, 0, NULL);
	else if (pid == -1)
		ft_putendl_fd("fork", 2);
	return (1);
}
Ejemplo n.º 8
0
void		ft_create_builtin(t_term *term)
{
	char	*tmp;
	int		i;

	i = 0;
	tmp = NULL;
	if (ft_strcmp(term->cmds[0], "cd") == 0)
		ft_cd(term);
	else if (ft_strcmp(term->cmds[0], "env") == 0)
		(!term->cmds[1]) ? ft_display_env(term) : ft_env_options(term);
	else if (ft_strcmp(term->cmds[0], "setenv") == 0)
		ft_process_setenv(term, term->cmds[1], term->cmds[2]);
	else if (ft_strcmp(term->cmds[0], "unsetenv") == 0)
		ft_process_unsetenv(term, term->cmds[1]);
	else if (ft_strcmp(term->cmds[0], "exit") == 0)
	{
		reset_shell();
		ft_putendl("Exiting shell");
		if (term->cmds[1])
			i = ft_atoi(term->cmds[1]);
		ft_free_struct(term);
		exit(i);
	}
	term->i = 0;
	term->u = NULL;
}
Ejemplo n.º 9
0
static void		check_cmd2(char **str, t_env *e)
{
	int			stat;
	pid_t		fat;
	char		*ret;

	ret = NULL;
	if (str[0] && ft_strcmp(str[0], "cd") == 0)
		ft_cd(str[1], e);
	else if (str[0] && ft_strcmp(str[0], "env") == 0)
		print_env(e->env);
	else if (str[0] && ft_strcmp(str[0], "exit") == 0)
		ft_exit(str);
	else if (str[0] && (ret = pathsys(str[0], e)) != NULL)
	{
		fat = fork();
		if (fat != -1)
		{
			if (fat == 0)
				execve(ret, str, e->env);
			else
				waitpid(fat, &stat, 0);
		}
		if (WIFSIGNALED(stat))
			ft_wrong_exit(e->shell, WTERMSIG(stat), ret);
	}
	safe_free(ret);
}
Ejemplo n.º 10
0
void	do_stuff(char *line, char ***e)
{
	int		i;
	char	**split;

	i = 0;
	split = ft_strsplit(line, ' ');
	if (ft_strequ(split[0], "exit"))
	{
		ft_del_tab_char(split);
		ft_strdel(&line);
		exit(0);
	}
	else if (ft_strcmp(split[0], "cd") == 0)
		*e = ft_cd(split, *e);
	else if (ft_strcmp(split[0], "env") == 0)
	{
		if (*e)
			aff_env(*e);
	}
	else if (ft_strcmp(split[0], "setenv") == 0)
		*e = ft_new_env(*e, split[1]);
	else if (ft_strcmp(split[0], "unsetenv") == 0)
		*e = ft_del_env(*e, split[1]);
	else if (split[0])
		ft_check_path(split, *e);
	else
	{
		ft_putstr("zsh: command not found: ");
		ft_putendl(split[0]);
	}
	ft_del_tab_char(split);
}
Ejemplo n.º 11
0
int		ft_isbuiltin(t_env *env, char *line)
{
	char	**line2;
	int		i;
	t_main w;

	i = 0;
	line2 = ft_strsplit(line, ' ');
	if (ft_strcmp(line2[0], "echo") == 0 && (i = 1))
	{
		line = ft_strfcut(line, 5);
		ft_echo(line);
	}
	else if (ft_strncmp(line, "cd", 2) == 0 && (i = 1))
		ft_cd(line);
	else if (ft_strncmp(line, "unsetenv", 7) == 0 && (i = 1))
		env = ft_unsetenv(env);
	else if (ft_strncmp(line, "env", 2) == 0 && (i = 1))
		print_env(env);
	else if (ft_strncmp(line, "setenv", 5) == 0 && (i = 1))
	{
		line = ft_strrw(line);
		line2 = ft_strsplit(line, ' ');
		env = set_env(line2, env);
	}
	if (i == 1)
		ft_doublecoms(env, &w, 0);
	return (1);
}
Ejemplo n.º 12
0
int		ft_command(char *data, t_uenv *user, char *pwd)
{
	int			wt;
	char		**param;

	if (data[0] == '\0')
	{
		send(user->cs, "Wrong command\n", 14, 0);
		send(user->cs, "ERROR\n", 6, 0);
		return (1);
	}
	param = ft_split_whitespaces(data);
	wt = 300;
	(void)pwd;
	if (ft_strcmp(param[0], "ls") == 0)
	{
		user = core(user, data);
		send(user->cs, "SUCCES\n", 7, 0);
		return (1);
	}
	else if (ft_strcmp(param[0], "cd") == 0)
	{
		ft_cd(&user, ft_strsplit(data, ' '));
		//send(user->cs, ft_strjoin(user->pwd, "\n"), ft_strlen(user->pwd) + 1, 0);
		send(user->cs, "SUCCES\n", 7, 0);
		return (1);
	}
	else if (ft_strcmp(param[0], "get") == 0)
	{
		file_get(user, data);
		//send(user->cs, "SUCCES\n", 7, 0);
		return (1);
	}
	else if (ft_strcmp(param[0], "put") == 0)
	{
		file_put(user, param[1]);
		//send(user->cs, "SUCCES\n", 7, 0);
		return (1);
	}
	else if (ft_strcmp(param[0], "pwd") == 0 && param[1] == NULL)
	{
		send(user->cs, ft_strjoin(user->pwd, "\n"), ft_strlen(user->pwd) + 1, 0);
		send(user->cs, "SUCCES\n", 7, 0);
		return (1);
	}
	else if (ft_strcmp(param[0], "quit") == 0 && param[1] == NULL)
	{
		send(user->cs, "Exit\n", 5, 0);
		close(user->cs);
		return (0);
	}
	else
	{
		send(user->cs, "Wrong command\n", 14, 0);
		send(user->cs, "ERROR\n", 6, 0);
	}
	return (1);
}
Ejemplo n.º 13
0
Archivo: build.c Proyecto: zhasni/SH1
int		ft_check_cd(char *line, t_env *env)
{
	char	**cdtab;

	cdtab = ft_strsplit(line, ' ');
	if (ft_strcmp(line, "cd") == 0)
		ft_cd_home(env);
	else if (cdtab[1][0] == '-')
		ft_cd_minus(env, line);
	else if (cdtab[1][0] == '~')
		ft_cd_tilde(env, cdtab);
	else if (cdtab[0] && cdtab[1])
		ft_cd(line, env);
	return (0);
}
Ejemplo n.º 14
0
Archivo: shell.c Proyecto: mrachid/SH2
int		check_cmd(char **arg)
{
	if (*arg == (void*)0)
		return (0);
	else if (ft_strcmp(arg[0], "exit") == 0)
		exit(1);
	else if (ft_strcmp(arg[0], "env") == 0)
		return (print_env());
	else if (ft_strcmp(arg[0], "cd") == 0)
		return (ft_cd(arg[1]));
	else if (ft_strcmp(arg[0], "unsetenv") == 0)
		return (ft_unsetenv(arg[1]));
	else if (ft_strcmp(arg[0], "setenv") == 0)
		return (ft_setenv(arg[1], arg[2], arg[3]));
	return (1);
}
Ejemplo n.º 15
0
int		check_builtin(t_env *e)
{
	int		ret;

	if (!e->av[0])
		return (0);
	ret = (ft_strequ(e->av[0], "env") && !(e->av[1])) ? display_env(e) : 0;
	if (ret != 2)
		ret = (ft_strequ(e->av[0], "cd")) ? ft_cd(e) : 0;
	if (ret != 2)
		ret = (ft_strequ(e->av[0], "setenv")) ? ft_setenv(e) : 0;
	if (ret != 2)
		ret = (ft_strequ(e->av[0], "unsetenv")) ? ft_unsetenv(e) : 0;
	if (ret != 2)
		ret = (!(ft_strncmp(e->av[0], "./", 2))) ? ft_exec(e) : 0;
	if (ret != 2)
		ret = (ft_strcmp(e->av[0], "exit")) ? 0 : 1;
	return (ret);
}
Ejemplo n.º 16
0
Archivo: main.c Proyecto: kelu27/42
void	ft_read_server(t_serv *e)
{
	while ((e->r = read(e->cs, e->buff, 1023)) > 0)
	{
		e->buff[e->r - 1] = '\0';
		printf("received %d bytes: [%s] from client N%d\n", e->r, e->buff,
			e->counter);
		if (ft_strcmp("ls", e->buff) == 0)
			ft_ls(e->cs);
		else if (ft_strcmp("pwd", e->buff) == 0)
			ft_pwd(e->cs, e->pwd);
		else if (ft_strncmp("cd ", e->buff, 3) == 0)
			ft_cd(e);
		else if (ft_strncmp("get ", e->buff, 4) == 0)
			ft_get_serv(e);
		else if (ft_strncmp("put ", e->buff, 4) == 0)
			ft_put_serv(e);
	}
}
Ejemplo n.º 17
0
char		*verif_env(char *name, char *value, t_shell *shell)
{
	char	**tmp;
	int		i;

	i = 0;
	if (ft_strcmp(name, "PWD") == 0 || ft_strcmp(name, "OLDPWD") == 0
		|| ft_strcmp(name, "HOME") == 0 || ft_strcmp(name, "TMPDIR") == 0)
	{
		tmp = ft_strsplit(value, ':');
		if (!is_directory(value))
			return (NULL);
		else if (ft_strcmp(name, "HOME") != 0 && ft_strcmp(name, "OLDPWD") != 0)
		{
			ft_cd(value, shell);
			return (value[0] == '/' ? value : ft_strdup(shell->get_pwd));
		}
	}
	return (value);
}
Ejemplo n.º 18
0
void	execute(t_env *env)
{
	if (ft_strcmp(env->com, "exit") == 0 || ft_strcmp(env->com, "quit") == 0)
		env->done = 1;
	else if (ft_strcmp(env->com, "clear") == 0)
		ft_printf("\033c");
	else if (ft_strcmp(env->com, "echo") == 0)
		echo(env);
	else if (ft_strcmp(env->com, "env") == 0)
		show_env(env->env_var);
	else if (ft_strcmp(env->com, "setenv") == 0)
		ft_setenv(env);
	else if (ft_strcmp(env->com, "unsetenv") == 0)
		ft_unsetenv(env);
	else if (ft_strcmp(env->com, "cd") == 0)
		ft_cd(env);
	else if (ft_strcmp(env->com, "pwd") == 0)
		ft_pwd(env);
	else
		ft_run(env);
}
Ejemplo n.º 19
0
int			ft_perform_cmd(t_tree *t, t_env *e)
{
	int		value;

	value = 0;
	if (!ft_strcmp(t->p->tok, "exit"))
		value = ft_exit(t->p);
	else if (!ft_strcmp(t->p->tok, "echo"))
		ft_echo(t->p);
	else if (!(ft_strcmp(t->p->tok, "cd")))
		value = ft_cd(t->p, e);
	else if (!(ft_strcmp(t->p->tok, "env")))
		value = ft_env(t->p, e);
	else if (!(ft_strcmp(t->p->tok, "unsetenv")))
		value = ft_unsetenv(t->p, &(e->env));
	else if (!(ft_strcmp(t->p->tok, "setenv")))
		value = ft_setenv(t->p, &(e->env));
	else
		value = ft_perform_exe(t->p, e);
	return (value);
}
Ejemplo n.º 20
0
int implemented_function(char **cmd)
{
	if (!cmd)
		return (1);
	if (ft_strcmp(cmd[0], "exit") == 0)
	{
		free_arr(&cmd);
		return (-1);
	}
	else if (ft_strcmp(cmd[0], "env") == 0)
		ft_env(cmd);
	else if (ft_strcmp(cmd[0], "cd") == 0)
		ft_cd(cmd);
	else if (ft_strcmp(cmd[0], "setenv") == 0)
		ENVP = ft_setenv(cmd);
	else if (ft_strcmp(cmd[0], "unsetenv") == 0)
		ENVP = ft_unsetenv(cmd);
	else
		return (0);
	return (1);
}
Ejemplo n.º 21
0
int					check_bultins(char **av, t_env *env)
{
	if (!ft_strcmp(av[0], "exit"))
		ft_exit(av, env);
	else if (!ft_strcmp(av[0], "cd"))
		ft_cd(av, env);
	else if (!ft_strcmp(av[0], "setenv"))
		ft_setenv(av, env);
	else if (!ft_strcmp(av[0], "unsetenv"))
		ft_unsetenv(av, env);
	else if (!ft_strcmp(av[0], "env"))
	{
		ft_env(av, env->environ);
		resumed_terminal();
	}
	else if (!ft_strcmp(av[0], "echo"))
		ft_echo(av, env->environ);
	else
		return (0);
	free_double_array(av);
	return (1);
}
Ejemplo n.º 22
0
void	own_command(t_env *env, char **sa, char *s)
{
	if (ft_memcmp(sa[0], "echo", 4) == 0)
		ft_echo(sa);
	else if (ft_memcmp(sa[0], "cd", 2) == 0)
		ft_cd(env, sa);
	else if (ft_memcmp(sa[0], "setenv", 6) == 0)
		ft_setenv(env, sa);
	else if (ft_memcmp(sa[0], "unsetenv", 8) == 0)
		ft_unsetenv(env, sa);
	else if (ft_memcmp(sa[0], "env", 3) == 0)
		call_env(*env, s);
	else if (ft_memcmp(sa[0], "help", 4) == 0)
		ft_help(env, sa);
	else if (ft_memcmp(sa[0], "easter", 6) == 0)
		easteregg(env, sa);
	else if (ft_memcmp(sa[0], "history", 7) == 0)
		list_history(env, sa);
	else
		ft_putstr(E_MESS05);
	FREE_(s);
}
Ejemplo n.º 23
0
void	ft_parse_input(t_shell **shell)
{
	if (*(*shell)->input)
	{
		(*shell)->argv = ft_parse_get_args((*shell)->input);
		(*shell)->argc = ft_count_arg((*shell)->argv);
		if ((*shell)->argc != 0)
		{
			if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "env") == 0)
				ft_print_environ(*shell);
			else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "setenv") == 0)
				ft_setenv(&(*shell));
			else if (ft_strcmp(ft_strtolower((*shell)->argv[0]),
								"unsetenv") == 0)
				ft_unsetenv(&(*shell));
			else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "exit") == 0)
				ft_exit(&(*shell));
			else if (ft_strcmp(ft_strtolower((*shell)->argv[0]), "cd") == 0)
				ft_cd(&(*shell));
			else
				ft_exec_bin(&(*shell));
		}
	}
}
Ejemplo n.º 24
0
void		soon(int cs)
{
	char				buf[1024];
	int					r;
	char				*home;

	home = get_pwd();
	while ((r = read(cs, buf, 1023)) > 0)
	{
		buf[r - 1] = '\0';
		ft_printf("received %d bytes: [%s]\n", r, buf);
		if (!ft_strcmp(buf, "ls"))
			ft_ls(cs);
		else if (!ft_strcmp(buf, "pwd"))
			send_msg(cs, get_pwd(), 0);
		else if (!ft_strncmp(buf, "cd ", 3) || !ft_strcmp(buf, "cd"))
			ft_cd(cs, buf, home);
		else if (!ft_strcmp(buf, "quit"))
			break ;
		else
			send_error(cs, "invalid_cmd");
		ft_bzero(buf, 1024);
	}
}