Ejemplo n.º 1
0
int	exec_pipe(char **cmd, char **env)
{
    t_pip	p;

    p.ret = 0;
    if (pipe(p.fd) == -1)
        return (my_putstr("QuadriSH: Error pipe.\n", 2));
    if ((p.pid = fork()) < 0)
        return (1);
    if (p.pid == 0)
    {
        close(p.fd[0]);
        dup2(p.fd[1], 1);
        my_parser(my_str_to_wordtab(CMD(cmd[0])), 1, env);
        exit(0);
    }
    else
    {
        close(p.fd[1]);
        dup2(p.fd[0], 0);
        my_parser(my_str_to_wordtab(CMD(cmd[1])), 1, env);
        wait(&p.ret);
        exit(0);
    }
    return (p.ret);
}
Ejemplo n.º 2
0
void		pipe_in_list(char **tmp, t_list *list, int **pipe_fd, int nb_pipe)
{
  t_command	*command;
  int		fd[2];
  int		i;
  int		j;

  i = 0;
  j = 0;
  if ((command = malloc(sizeof(t_command))) == NULL)
    exit (0);
  init_struct(command);
  while (nb_pipe > 0)
    {
      add_pipe(pipe_fd, command, fd, j);
      command->data = my_str_to_wordtab(tmp[i++], ' ');
      my_insert_pushfront(list, command);
      command->input = fd[0];
      j += 2;
      nb_pipe -= 1;
    }
  command->output = -1;
  command->data = my_str_to_wordtab(tmp[i], ' ');
  my_insert_pushfront(list, command);
}
Ejemplo n.º 3
0
void	check_cmd_n(t_chkcmd ch, t_label *list)
{
  if (ch.i == 0)
    {
      ch.line = my_str_to_wordtab(ch.tmp, "\t");
      if (ch.line != NULL && ch.line[0] != NULL && ch.line[1] != NULL)
        check_cmd_arg(ch.line[1], ch.cmd, list);
    }
  else
    {
      ch.line = my_str_to_wordtab(ch.tmp, "\t");
      if (ch.line != NULL && ch.line[0] != NULL && ch.line[2] != NULL)
        check_cmd_arg(ch.line[2], ch.cmd, list);
    }
}
Ejemplo n.º 4
0
int		check_cmd(t_shell *sh, t_node *tree)
{
  int		i;

  i = -1;
  number_reset(sh);
  sh->cmd = my_str_to_wordtab(tree->str);
  dollar(sh);
  check_point_slash(sh, sh->env);
  if (built_in(sh) == -1)
    return (-1);
  while (sh->path != NULL && sh->path[++i] != NULL && sh->ch == 0)
    if (access(strcat(sh->path[i], sh->cmd[0]), X_OK) == 0)
      exec_cmd(sh->path[i], sh->cmd, sh->env, sh);
  if (sh->ch == 0)
    exec_slah_bin(sh->cmd, sh);
  if (sh->ch == 0)
    {
      fprintf(stderr, "Error: '%s' command not found\n", sh->cmd[0]);
      sh->ok_cmd = -1;
      return (0);
    }
  my_free(sh->cmd);
  return (0);
}
Ejemplo n.º 5
0
struct s_stock_par	*my_param_to_tab(int ac, char **av)
{
  struct s_stock_par	*ptrstruct;
  int			i;

  if (ac == 0)
    return (0);
  ptrstruct = malloc(sizeof(*ptrstruct) * (ac + 1));
  if (ptrstruct == NULL)
    return (0);
  i = 0;
  while (i < ac)
    {
      (ptrstruct + i)->size_param = my_strlen(av[i]);
      (ptrstruct + i)->str = av[i];
      (ptrstruct + i)->copy = malloc(sizeof(char) * my_strlen(av[i]) + 1);
      if ((ptrstruct + i)->copy == NULL)
	return (0);
      my_strcpy((ptrstruct + i)->copy, av[i]);
      (ptrstruct + i)->tab = my_str_to_wordtab(av[i]);
      i = i + 1;
    }
  (ptrstruct + i)->str = 0;
  return (ptrstruct);
}
Ejemplo n.º 6
0
int		main(int ac, char **av)
{
  char		*line;
  char		**maze;
  t_path	*path;
  int           fd;
  struct stat   fileStat;

  if (ac != 2)
    {
      printf("Utilisation : ./solver maze.txt,\n");
      return (0);
    }
  if ((fd = open(av[1], O_RDONLY)) < -1)
    return (0);
  if (fstat(fd, &fileStat) < 0)
    return (0);
  path = NULL;
  line = NULL;
  line = malloc(sizeof(char) * (fileStat.st_size + 1));
  read(fd, line, fileStat.st_size);
  line[fileStat.st_size] = '\0';
  maze = my_str_to_wordtab(line);
  find_path(maze, &path);
  free_fonc(maze, path);
  free(line);
  return 0;
}
Ejemplo n.º 7
0
int			move_all_ants(t_visu *visu)
{
  char			*line;
  char			**tab;
  int			i;

  i = 0;
  if (visu->mem != NULL)
    {
      line = my_strdup(visu->mem);
      visu->mem = NULL;
    }
  else if ((line = get_next_line(0)) == NULL)
    return (-1);
  if ((tab = my_str_to_wordtab(line)) == NULL)
    return (-1);
  free(line);
  while (tab[i] != NULL)
    if (get_ant_positions(visu, tab[i++]) == -1)
      {
	free_args(tab);
	return (-1);
      }
  free_args(tab);
  make_ants_move(visu);
  return (0);
}
Ejemplo n.º 8
0
/*
** brief: it will add other operator like | ...
** we will add the new elt in the right son while the intern node is
** an operator
** @ast: our binary tree
** @oper: the operator like "|"
** @id_ope: the operator id like ID_PIPE
** return: return the new ast
*/
t_ast	*add_operator(t_ast *ast, char *oper, char id_ope)
{
  t_ast	*tmp;
  t_ast	*prev;
  t_ast	*node;
  char	**arr;

  if (id_ope == -1)
    return (ast);
  tmp = ast;
  prev = ast;
  if ((arr = my_str_to_wordtab(oper, my_strlen(oper), 0, -1)) == NULL)
    return (NULL);
  while (tmp != NULL && tmp->oper != -1)
    {
      prev = tmp;
      tmp = tmp->right;
    }
  if ((node = create_element_ast(arr, id_ope)) == NULL)
    return (NULL);
  free_arr(arr);
  node->left = tmp;
  if (tmp == prev)
    ast = node;
  else
    prev->right = node;
  return (ast);
}
Ejemplo n.º 9
0
/*
** brief: it will add our command in our ast
** to add it, we will travel our ast while we've a command
** after it, if the left son is empty we will add our command here,
** else we'll add in the right son
** @ast: the binary tree struct
** @cmd : the char * with the cmd
** end: then end string to stop the str_to_wordtab magueule
** return: return the new AST
*/
t_ast	*add_command(t_ast *ast, char *cmd, char **array, int end)
{
  t_ast	*tmp;
  t_ast	*prev;
  t_ast	*node;
  char	**arr;

  tmp = ast;
  prev = ast;
  arr = (cmd != NULL || array == NULL) ?
    (my_str_to_wordtab(cmd == NULL ? "" : cmd, cmd == NULL ? 1 : end, 0, -1))
    : NULL;
  while (tmp != NULL && tmp->oper != -1)
    {
      prev = tmp;
      tmp = tmp->right;
    }
  if (!(node = create_element_ast(arr == 0 ? array : arr, -1)))
    return (NULL);
  free_arr(arr);
  if (tmp == prev)
    ast = node;
  else if (prev->left == NULL)
    prev->left = node;
  else
    prev->right = node;
  return (ast);
}
Ejemplo n.º 10
0
int		fill_tree(t_shell *shell, char *command_line)
{
  t_tree	*up;
  char		**commands;
  int		i;

  if ((commands = my_str_to_wordtab(command_line, " \t")) == NULL)
    return (nothing_more(shell));
  i = -1;
  while (commands != NULL && commands[++i] != NULL)
    {
      if ((is_token(shell->tokens, commands[i]) >= 0) &&
	  ((shell->tree->right = malloc(sizeof(t_tree))) != NULL))
	{
	  shell->tree->token = is_token(shell->tokens, commands[i]);
	  if ((shell->tree->left = get_left(commands, i)) == NULL)
	    return (-1);
	  up = shell->tree;
	  shell->tree = shell->tree->right;
	  shell->tree->up = up;
	  return (fill_tree(shell, new_command(commands, i)));
	}
    }
  return (last_command(shell, commands, i));
}
Ejemplo n.º 11
0
void	execution(char **tabb, t_env *chaine_env)
{
  int	j;
  char	**path;
  char	*str;
  char	**env;
  char	*value_path;

  j = 0;
  env = tab_env(chaine_env);
  if ((tabb[0][0] == '.' || tabb[0][0] == '/') && access(tabb[0], R_OK) == 0)
    execve(tabb[0], tabb, env);
  else if ((value_path = my_getenv("PATH", chaine_env)) != NULL)
    {
      path = my_str_to_wordtab(value_path, ':');
      while (path[j] != NULL)
        {
	  str = my_complete_str(path[j], tabb[0]);
          if (access(str, R_OK) == 0)
            execve(str, tabb, env);
          j++;
          xfree(str);
        }
    }
  printf("42sh: command not found : %s\n", tabb[0]);
  exit(-1);
}
Ejemplo n.º 12
0
Archivo: parser.c Proyecto: fave-r/RT
int		parser(char *name, t_flag tab[], t_obj *obj, t_spot *spot)
{
  int		fd;
  char		*str;
  char		**tmp;

  tmp = NULL;
  if ((fd = open(name, O_RDONLY)) == -1)
    {
      fprintf(stderr, "Cannot read file %s\n", name);
      return (-1);
    }
  while ((str = get_next_line(fd)) != NULL)
    {
      if (str[0])
	{
	  tmp = my_str_to_wordtab(tmp, str, ", :\t");
	  fill_list(tmp, tab, obj, spot);
	  free(str);
	}
    }
  free_tab(tmp);
  close(fd);
  return (1);
}
Ejemplo n.º 13
0
char	*get_infos(t_client *clt, char const *input)
{
  char	**tab;
  char	**tab2;

  tab = my_str_to_wordtab((char*)input, ' ');
  if (tab[1] == NULL || strlen(tab[1]) > 26)
    return ((char*)"-<strong>[Error]: </strong>must specify an address");
  tab2 = my_str_to_wordtab(tab[1], ':');
  clt->ip = realIp(tab2[0]);
  if (tab2[1] == NULL)
    clt->port = 6667;
  else
    clt->port = atoi(tab2[1]);
  return (NULL);
}
Ejemplo n.º 14
0
int	my_cd(t_shell *shell)
{
  char	**args;
  int	i;

  i = 0;
  args = my_str_to_wordtab(shell->buffer);
  if (args[1] != NULL)
    {
      if ((shell->cd_move = malloc(my_strlen(args[1]) + 1)) == NULL)
	exit(84);
      shell->cd_move = my_strcpy(shell->cd_move, args[1]);
      move_to_it(shell);
    }
  else if (shell->env != NULL)
    {
      while (unstrict_cmp(shell->env[i], "HOME=") != 0)
	i += 1;
      if ((shell->cd_move = malloc(my_strlen(shell->env[i]))) == NULL)
	exit(84);
      shell->cd_move = envline_cpy(shell->cd_move, shell->env[i]);
      move_to_it(shell);
    }
  free_tab(args);
  return (0);
}
Ejemplo n.º 15
0
int	unsetenv_manager(t_manage *man, char *cmd)
{
  int	pos;
  char	**d_cmd;
  char	**d_all;
  int	i;
  int	nb_of_args;
  int	j;

  i = -1;
  j = 1;
  d_all = my_str_to_wordtab(cmd, ' ');
  while (d_all[++i])
    {
      pos = 0;
      if ((nb_of_args = wordtab_count(d_all)) == 1)
	{
	  err_report(3, "NULL");
	  return (-1);
	}
      d_cmd = get_args(cmd);
      if ((pos = my_getenv(man->all_env->my_env, d_cmd[j])) == -1)
	return (-1);
      man->all_env->my_env = d_tab_unset_doc(man->all_env->my_env, pos - 1);
      j = j + 1;
    }
  return (0);
}
Ejemplo n.º 16
0
int			run_clt(t_env *e)
{
  char			*msg;
  size_t		lmsg;
  size_t		alloc_size;
  char			**tab;

  msg = NULL;
  alloc_size = 0;
  while (42)
    {
      if (e->chan == NULL)
	print_prompt(e);
      if ((lmsg = getline(&msg, &alloc_size, stdin)) > 0)
	{
	  (lmsg) ? (msg[lmsg - 1] = 0) : (msg[lmsg] = 0);
	  tab = NULL;
	  if ((tab = my_str_to_wordtab(msg)))
	    {
	      (tab[0][0] == '/') ? check_cmd(e, tab) : send_msg(e, msg);
	      delete_tab(tab);
	    }
	}
    }
  if (alloc_size)
    free(msg);
  return (EXIT_SUCCESS);
}
Ejemplo n.º 17
0
void	my_clear(t_env *e)
{
  char	**clear;
  char	*cmd;
  int	i;
  pid_t	pid;
  int	status;

  cmd = "clear clear";
  i = 0;
  clear = my_str_to_wordtab(cmd, ' ');
  if ((pid = fork()) == -1)
    my_exit2("Error -> Fork Failed :(");
  if (pid == 0)
    {
      while (e->path[i])
	{
	  e->file = my_strcat_separe(e->path[i], clear[0], '/');
	  execve(e->file, clear, e->env);
	  i++;
	}
    }
  else
    {
      wait(&status);
      my_printf("\033[32m[%s@localhost ~]$> \033[0m", e->user);
    }
}
Ejemplo n.º 18
0
void		prend_objet(t_msg *msg, t_client *cl, t_map **map, t_opt *o)
{
  char		**tab_cmd;
  int		i;
  static char	tab[7][12] = { "nourriture", "linemate", "deraumere", "sibur",
			       "mendiane", "phiras", "thystame" };

  (void)o;
  map = (void *)map;
  i = -1;
  msg->time = get_time_client(cl, 7);
  tab_cmd = my_str_to_wordtab(msg->comand, ' ');
  while (strlen_tab(tab_cmd) == 2 && ++i < MAX)
    if (strcmp(tab[i], tab_cmd[1]) == 0 && cl->map->ress[i] > 0)
      {
	cl->map->ress[i] -= 1;
	cl->ress[i] += (i > 0 ? 1 : 126);
	sub_food(msg, cl, "ok\n");
	take_ress(cl->id, i, cl);
	giveinvall(cl);
	givecaseall(cl);
	repop(map, i);
	return;
      }
  sub_food(msg, cl, "ko\n");
}
Ejemplo n.º 19
0
/*
** brief: used if the operator is ; -> travel while the intern node is ;
** @ast: the ast
** return: the entire tree
*/
t_ast	*add_wait_opera(t_ast *ast)
{
  t_ast	*tmp;
  t_ast	*prev;
  t_ast	*node;
  char	**arr;

  tmp = ast;
  prev = ast;
  if ((arr = my_str_to_wordtab(";", 1, 0, -1)) == NULL)
    return (NULL);
  while (tmp != NULL && tmp->oper == ID_WAIT)
    {
      prev = tmp;
      tmp = tmp->right;
    }
  if ((node = create_element_ast(arr, ID_WAIT)) == NULL)
    return (NULL);
  free_arr(arr);
  node->left = tmp;
  if (tmp == prev)
    ast = node;
  else
    prev->right = node;
  return (ast);
}
Ejemplo n.º 20
0
int	main()
{
  char	c[] = "Je ne pense,pas*que!je vais_beaucoup-dormir";

  my_str_to_wordtab(c);
  return (0);
}
Ejemplo n.º 21
0
void	exec(t_env *envp, char *buffer)
{
  char	**tab;
  char	*link;
  char	**path;
  int	i;

  tab = my_str_to_wordtab(buffer);
  while (envp != NULL && my_strcmp(envp->name, "PATH") != 0)
    envp = envp->next;
  if (envp != NULL)
    {
      path = my_wordtab(envp->content);
      i = 0;
      while (tab[0] != NULL && path != NULL && path[i] != NULL)
	{
	  link = link_cat(tab[0], path[i]);
	  access(link, X_OK) == 0 ? execve(link, tab, environ) : i++;
	  free(link);
	}
    }
  if (tab[0] != NULL)
    access(tab[0], X_OK) == 0 ? execve(tab[0], tab, environ) : i++;
  if (tab[0] != NULL)
    my_printf("[%s] : command not found\n", tab[0]);
  make_free(&tab, &path);
}
Ejemplo n.º 22
0
char		my_execve(char **command, t_list *env, char *path)
{
  char		**tabenv;
  int		status;
  pid_t		pid;
  int		i;
  char		*error;

  pid = fork();
  tabenv = NULL;
  error = NULL;
  if (pid == -1)
    my_puterror("Impossible, error with pid");
  else if (pid == 0)
    {
      i = 0;
      tabenv = conv_list(env);
      error = recursive_execve(my_str_to_wordtab(path, ':'),
			       check_alias(command), tabenv, i);
      execve_error(error, command, tabenv);
    }
  else
    if (wait(&status) == -1)
      my_puterror("Impossible to use wait\n");
  if (WTERMSIG(status) == 11)
    my_putstr("Segmentation fault (core dumped)\n");
  return (0);
}
Ejemplo n.º 23
0
int	get_light_param(t_all *all, char **tab, int nb)
{
  char	**light_list;
  char	**line_tab;
  int	param;
  int	i;

  if ((light_list = create_light_list()) == NULL)
    return (put_error_int("Error : CREATE_NAME_LIST failed\n"));
  param = 0;
  while (param < 3)
    {
      i = 0;
      while (i < 5)
	{
	  if (strncmp(tab[i], light_list[param], strlen(light_list[param])) == 0)
	    {
	      if ((line_tab = my_str_to_wordtab(tab[i], " ")) == NULL)
		return (put_error_int("Error : STR_TO_WORDTAB failed\n"));
	      get_light_param_value(all, line_tab, param, nb);
	    }
	  ++i;
	}
      ++param;
    }
}
Ejemplo n.º 24
0
int		error_boss(char *map)
{
  char		*file;
  char		**tab;
  int		width;
  int		height;

  if (check_existence(map) == -1
      || (file = get_file_content(map)) == NULL
      || check_integer(file, "ibwsm.lg\n") == -1
      || (file = put_return(file)) == NULL
      || (tab = my_str_to_wordtab(file, '\n')) == NULL
      || check_largest(tab) == -1
      || is_present("i", file) != 1
      || is_present("b", file) != 1
      || check_tab(tab) == -1)
    return (puterror("One of Boss map isn't correct.\n"));
  width = my_strlen(tab[0]) * 29;
  height = tablen(tab) * 26;
  if (width > 1300 || height > 750)
    return (puterror("Error: Dimension are too big in Boss Map.\n"));
  free(file);
  free_tab(tab);
  return (0);
}
Ejemplo n.º 25
0
int main(int argc, char** argv, char** env)
{
    char buffer[4096];
    int len;
    char** env_path;
    char** cmd;
    int permission;
    int status;

    env_path = find_path(env);
    my_putstr("$>");
    while((len = read(STDIN_FILENO, buffer, 4096)) > 0)
    {
        cmd = my_str_to_wordtab(&buffer[1]);
        if(cmd[0] != 0)
        {
            permission = my_exec(env_path, cmd, env);
            if (permission == 0)
                my_putstr("Permission denied.\n");
            else if (permission == -1)
                my_putstr(my_strcat(buffer, " : command not found.\n"));
            else
                waitpid(permission, &status, 0);
        }
        my_putstr("$>");
    }
    return (0);
}
Ejemplo n.º 26
0
static int	launch_cmd(char *line, t_client *client, t_kernel *kernel)
{
  char		**av;
  int		i;
  int		ret;

  while (line && *line)
    {
      if (!(av = my_str_to_wordtab(line)))
	return (-1);
      i = 0;
      ret = -1;
      while (ret == -1 && av[0] && g_functions[i].name)
	{
	  if (!strcmp(av[0], g_functions[i].name))
	    {
	      if ((ret = call_cmd(i, client, av, kernel)))
		return (ret);
	    }
	  ++i;
	}
      if (!g_functions[i].name)
	if ((ret = game_auth(av, client, kernel)))
	  return (ret);
      line = find_end(line);
    }
  return (0);
}
Ejemplo n.º 27
0
Archivo: pars.c Proyecto: desomb/42sh
t_pars		*my_pars(t_struct *struc, char ***env, t_pars *pars)
{
  int	pid;

  if (*env == NULL)
    return (error_env(pars));
  if ((my_pars_check(pars, struc)) == 1)
    {
      pars->last_op = struc->operateur;
      pars->tab = my_str_to_wordtab(struc->instruction);
      if ((my_echo_check(pars, struc, *env, pid)) == NULL)
	return (pars);
      pars = my_echo_check(pars, struc, *env, pid);
      pars = my_path_remp(struc, *env, pars, 0);
      if (my_strcmp(pars->tab[0], "cd") == 0)
	env = my_cd(pars->tab, env);
      else if (pars->path != NULL)
	{
	  pid = fork();
	  if (pid == 0)
	    pars = pars_norme(struc, pars, env);
	  wait(&pid);
	}
      else
	pars = error_execve(struc->instruction, pars);
    }
  return (pars);
}
Ejemplo n.º 28
0
int		open_file(t_maze *maze, char *filepath)
{
  char		**wt;
  struct stat	my_stat;
  int		ret;

  if ((maze->file_fd = open(filepath, O_RDONLY)) == -1 ||
      (fstat(maze->file_fd, &my_stat)) == -1 ||
      (maze->file = malloc(sizeof(char) * (my_stat.st_size + 1))) == NULL ||
      (ret = read(maze->file_fd, maze->file, my_stat.st_size)) <= 0 ||
      (maze_checker(maze->file, ret) == -1))
    return (0);
  wt = my_str_to_wordtab(maze->file);
  ret = 0;
  while (wt[ret])
    {
      maze->w = strlen(wt[ret]);
      if (ret > 0)
	if (strlen(wt[ret - 1]) != strlen(wt[ret]))
	  return (0);
      ret++;
    }
  if (transfer_wt_to_cells(maze, wt))
    return (0);
  return (1);
}
Ejemplo n.º 29
0
static int	data_line(t_objs *obj, char *str)
{
  static t_set	set[SIZE] = {{"origin", recup_origin}, {"rayon", recup_rayon},
			     {"color", recup_color}, {"rotate", recup_rotate},
			     {"damier", recup_damier}, {"limit", recup_limit},
			     {"reflexion", recup_reflexion},
			     {"commissaire", recup_commissaire}};
  static char	*form[8] = {"plan", "sphere", "cylindre", "cone",
			    "hyperboloide", "light", "eye", NULL};
  char		**line;
  int		i;

  if ((line = my_str_to_wordtab(str, " \t")) == NULL)
    return (-1);
  i = -1;
  while (++i < SIZE)
    if (my_strcmp(set[i].key, line[0]) == 0)
      if (set[i].ptr(obj, line) == -1)
	return (-1);
  i = -1;
  while (form[++i])
    if (my_strcmp(form[i], line[0]) == 0)
      obj->id = i;
  free_tab(line);
  return (0);
}
Ejemplo n.º 30
0
int		get_nbr_ants(t_lemin *lemin)
{
    char		**argv;
    char		*str;

    if ((str = get_next_line(0)) == NULL)
        return (1);
    while (str[0] == '#' && str[1] != '#')
        if ((str = get_next_line(0)) == NULL)
            return (1);
    argv = my_str_to_wordtab(str);
    if ((argv == NULL)
            || (argv[0] == NULL || argv[0][0] == '\0' || argv[0][0] == '\n')
            || (argv[1] != NULL) || (my_hard_getnbr(argv[0]) < 0))
    {
        free(str);
        free_args(argv);
        return (1);
    }
    lemin->nb_ants = my_hard_getnbr(argv[0]);
    my_putstr(str);
    my_putstr("\n");
    free(str);
    free_args(argv);
    return (0);
}