Пример #1
0
char	*my_getabsolutepath(char *str, char **env)
{
  char	*tmp;
  char	*out;
  char	**path;
  int	i;

  if (str == NULL || env == NULL || (path = my_getpath(env)) == NULL)
    return (NULL);
  i = -1;
  if (str[0] == '.' || str[0] == '/')
    return (my_strdup(str));
  while (path[++i] != NULL)
    {
      if ((tmp = my_strstick(path[i], "/")) == NULL ||
	  (out = my_strstick(tmp, str)) == NULL)
	return (NULL);
      if (!access(out, F_OK))
	{
	  gbgc_free(NULL, tmp);
	  return (out);
	}
      gbgc_free(NULL, tmp);
      gbgc_free(NULL, out);
    }
  free_strtab(path);
  return (NULL);
}
Пример #2
0
void	free_token(void *data)
{
  t_tok	*tok;

  tok = data;
  if (tok == NULL)
    return ;
  if (tok->word)
    gbgc_free(NULL, tok->word);
  gbgc_free(NULL, tok);
}
Пример #3
0
void	free_optab(t_opc **tab)
{
  int	i;

  if (tab == NULL)
    return;
  i = -1;
  while (tab[++i] != NULL)
    gbgc_free(NULL, tab[i]);
  gbgc_free(NULL, tab);
}
Пример #4
0
void	free_envlist(t_lnv *list)
{
  if (list == NULL)
    return;
  while (list->next != NULL)
    {
      list = list->next;
      gbgc_free(NULL, list->prev->name);
      gbgc_free(NULL, list->prev->value);
      gbgc_free(NULL, list->prev);
    }
  gbgc_free(NULL, list);
}
Пример #5
0
t_dlist	*tokenizer(char *line, t_lnv *envlist)
{
  t_dlist	*list;
  t_dlist	*cur;
  int		i;
  char		*exp;

  i = 0;
  if (line == NULL || !line[(i)])
    return (NULL);
  list = NULL;
  if ((exp = expand_variables(line, envlist)) == NULL)
    return (NULL);
  while ((exp[i]))
    {
      skipspace(exp, &i);
      if (exp[i])
  	{
  	  if (((list = add_dlist_elem(list)) == NULL)	 ||
  	      ((cur = goto_last_dlist(list)) == NULL)	 ||
  	      ((cur->data = get_next_token(exp, &i)) == NULL))
  	    return (NULL);
  	}
    }
  gbgc_free(NULL, exp);
  return (list);
}
Пример #6
0
void	my_cd2(t_lnv *env, char *path, char *npath, char *pwd)
{
  if (env == NULL)
    return;
  pwd = get_value(env, "PWD");
  if (path == NULL)
    npath = get_value(env, "HOME");
  else if (pwd == NULL)
    npath = my_strdup(path);
  else if (!my_strcmp("-", path) && get_value(env, "OLDPWD") != NULL)
    {
      npath = get_value(env, "OLDPWD");
      set_value(env, "OLDPWD", pwd);
    }
  else
    {
      npath = "";
      set_oldpwd(env, &pwd, &path, &npath);
    }
  if (npath)
    {
      set_value(env, "PWD", npath);
      chdir(npath);
    }
  gbgc_free(NULL, npath);
}
Пример #7
0
char		*mallocat(char *line, char *buffer, int *j, int lus)
{
  int		i;
  char		*dest;
  static int	iter = 1;

  i = 0;
  if (!(dest = gbgc_malloc(NULL, sizeof(char) * (gnl_len(&iter) + BUFF + 1))))
    return (NULL);
  if (line)
    while (line[i])
      {
	dest[i] = line[i];
	i++;
      }
  while ((*j) < lus && buffer[*j] != '\n')
    {
      dest[i] = buffer[(*j)];
      (*j)++;
      i++;
    }
  dest[i] = 0;
  gbgc_free(NULL, line);
  if (buffer[(*j)] == '\n')
    iter = 1;
  return (dest);
}
Пример #8
0
int	loop_main(t_sh *sh, pid_t pid)
{
    if (sh == NULL)
        return (EXIT_FAILURE);
    while (is_open(NULL))
    {
        my_printf("%s", sh->rdl.prompt);
        if ((get_readline(sh)) == false)
            return (unload_sh(sh));
        sh->tok_list = tokenizer((char*)sh->line, sh->env);
        sh->line = gbgc_free(NULL, sh->line);
        parser(sh);
        my_main(sh, sh->env);
        if (!is_open(NULL))
        {
            if (pid == getpid())
            {
                dezombificator2(0);
                unload_sh(sh);
            }
            return (get_exitstate(NULL));
        }
        sh->prs_list = destroy_dlist(sh->prs_list, free_t_prs);
    }
    if ((unload_sh(sh)) == FALSE)
        return (EXIT_FAILURE);
    return (get_exitstate(NULL));
}
Пример #9
0
int	my_fclose(t_fle *file)
{
  if (file == NULL)
    return (0);
  gbgc_free(NULL, file->name);
  if ((file->fmode & O_WRONLY) || (file->fmode & O_RDWR))
    my_flush(file);
  if (file->fd >= 0)
    {
      if (close(file->fd) < 0)
	{
	  gbgc_free(NULL, file);
	  return (0);
	}
    }
  gbgc_free(NULL, file);
  return (1);
}
Пример #10
0
int	getent_term(char **envp)
{
    char	*term_name;

    if (envp == NULL)
    {
        my_perror("no environment detected !");
        return (FALSE);
    }
    if (!(term_name = get_term_name(envp)))
        return (FALSE);
    if (tgetent(NULL, term_name) != 1)
    {
        my_perror("tgetent failed");
        gbgc_free(NULL, term_name);
        return (FALSE);
    }
    gbgc_free(NULL, term_name);
    return (TRUE);
}
Пример #11
0
int	hist_down_management(t_line *rdl)
{
  if ((rdl->cur_hist->prev) == NULL)
    {
      gbgc_free(NULL, rdl->line);
      if ((rdl->line = (unsigned char*)my_strdup(rdl->cur_hist->data)) == NULL)
	return (my_perror(MALLOC_FAILED));
      rdl->pos = (my_strlen((char*)rdl->line));
      rdl->cur_hist = NULL;
      gbgc_free(NULL, rdl->hist->data);
      rdl->hist = rm_first_dlist(rdl->hist, NULL);
    }
  else
    {
      gbgc_free(NULL, rdl->line);
      if ((rdl->line = (unsigned char*)my_strdup(rdl->cur_hist->data)) == NULL)
	return (my_perror(MALLOC_FAILED));
      rdl->pos = (my_strlen((char*)rdl->line));
    }
  return (TRUE);
}
Пример #12
0
int	dtf_delete(t_dtf *data)
{
  if (get_crtreturn(NULL))
    return (0);
  if (data == NULL)
    return (0);
  if (data->map != NULL)
    free_strtab((char **)data->map);
  if (data->units != NULL)
    freelist(data->units, NULL);
  gbgc_free(NULL, data);
  return (1);
}
Пример #13
0
void	get_raminfoloop(t_ram *ram, char **data, int i)
{
  if (ram == NULL || data == NULL)
    return;
  while (data[++i] != NULL)
    {
      if (my_strcmp(data[i], MEMTOTAL) == 0)
	ram->memtotal = my_atoi(data[i + 1]);
      else if (my_strcmp(data[i], MEMFREE) == 0)
	ram->memfree = my_atoi(data[i + 1]);
      else if (my_strcmp(data[i], MEMAVAILABLE) == 0)
	ram->memavailable = my_atoi(data[i + 1]);
    }
  gbgc_free(NULL, data);
}
Пример #14
0
void		*gbgc_realloc(void *ptr, size_t oldsize, size_t newsize)
{
  unsigned char	*nptr;
  unsigned char	*cpy;
  unsigned int	i;

  if ((cpy = ptr) == NULL || oldsize <= 0 || newsize <= 0 ||
      (nptr = gbgc_malloc(NULL, newsize)) == NULL)
    return (NULL);
  i = 0;
  while (i++ < oldsize)
    nptr[i - 1] = cpy[i - 1];
  gbgc_free(NULL, ptr);
  return (nptr);
}
Пример #15
0
int     hist_down(t_line *rdl, void *data)
{
  (void)data;
  if (rdl->cur_hist == NULL)
    return (TRUE);
  gbgc_free(NULL, rdl->cur_hist->data);
  if ((rdl->cur_hist->data =
       (unsigned char*)my_strdup((char*)rdl->line)) == NULL)
    return (my_perror(MALLOC_FAILED));
  rdl->cur_hist = rdl->cur_hist->prev;
  return_to_beg(rdl, data, 1);
  if (!(hist_down_management(rdl)))
    return (FALSE);
  my_putstr((char*)rdl->line);
  return (TRUE);
}
Пример #16
0
void		fill_wordtab(t_my_wd *wd, t_my_lwd *lwd)
{
  int		lp;
  t_my_lwd	*act;
  t_my_lwd	*prev;

  lp = 0;
  act = lwd->next;
  while (lp < wd->nb_words)
    {
      wd->my_wordtab[lp] = act->str;
      prev = act;
      act = act->next;
      gbgc_free(prev);
      ++lp;
    }
  wd->my_wordtab[lp] = 0;
}
Пример #17
0
int	dtf_addunittolist(t_dtf *map, t_uni *unit)
{
  t_mls	*cpy;

  if (get_crtreturn(NULL))
    return (0);
  if (map == NULL || unit == NULL || (cpy = map->units) == NULL)
    return (dbgerr("Error: dtf_addunittolist: Invalid or NULL parameter"));
  if (is_unitinlist(map, unit->id))
    {
      gbgc_free(NULL, unit);
      return (dbgcrt("Error: dtf_addunittolist: Units multiple declaration"));
    }
  while (cpy->next != NULL)
    cpy = cpy->next;
  if (!list_addel(cpy, unit, NULL))
    return (dbgcrt("Error: dtf_addunittolist: Can't add the unit to the list"));
  return (1);
}
Пример #18
0
char	*get_globbed_str(char *raw)
{
  char		*res;
  glob_t	globb;

  res = NULL;
  if (raw == NULL)
    return (NULL);
  if ((glob(raw, GLOB_NOCHECK | GLOB_TILDE, 0, &globb)) != 0)
    return (NULL);
  if ((globb.gl_pathc == 0 || globb.gl_pathv == NULL))
    return (raw);
  if ((res = glob_str_malloc(&globb)) == NULL)
    return ((char*)(long)my_perror(MALLOC_FAILED));
  cpy_glob_str(&globb, res);
  gbgc_free(NULL, raw);
  globfree(&globb);
  return (res);
}
Пример #19
0
int	createpipe(t_lid *data)
{
  t_pip	*pipe;
  char	**tab;
  char	*line;

  if (data == NULL || (line = get_last_line(data->file)) == NULL)
    return (FALSE);
  if (line[0] == '#')
    return (TRUE);
  if ((tab = my_strtok(line, "-")) == NULL || tab_length(tab) != 2 ||
      (pipe = create_pipe(tab[0], tab[1])) == NULL ||
      data->existPipe(data, pipe))
    return (FALSE);
  data->addPipe(data, pipe);
  gbgc_free(NULL, line);
  free_strtab(tab);
  return (TRUE);
}
Пример #20
0
void	get_raminfo(t_ram *ram)
{
  int	info_size;
  int	fd;
  char	*buffer;
  char	**data;
  int	j;

  if (ram == NULL || (info_size = my_fgetsize("/proc/meminfo")) < 0 ||
      (buffer = gbgc_malloc(NULL, info_size + 50)) == NULL)
    return;
  if ((fd = open("/proc/meminfo", O_RDONLY)) < 0 ||
      (j = read(fd, buffer, info_size)) < 0)
    return;
  buffer[j] = '\0';
  close(fd);
  if ((data = my_str_to_wordtab(buffer)) == NULL)
    return;
  gbgc_free(NULL, buffer);
  ram->unit = get_memunit(data[2]);
  get_raminfoloop(ram, data, -1);
}