Exemple #1
0
char	*get_prev_chars(t_term *term)
{
	char	*buf;
	char	*rev;
	int		i;

	i = 0;
	if ((buf = ft_strnew(COMP_SIZE)))
	{
		term = get_current_cursor(term);
		if (term->c == ' ' && term->prev)
			term = term->prev;
		while (term && i < COMP_SIZE && term->c != ' ')
		{
			buf[i] = (char)term->c;
			term = term->prev;
			i++;
		}
	}
	rev = ft_strrev(buf);
	free(buf);
	return (rev);
}
Exemple #2
0
int				main(int ac, char **av)
{
	t_coord	**coord;
	t_varf	v;

	(ac != 2) ? (ft_exit()) : (0);
	v.tab = NULL;
	v.file = ft_strnew(1);
	((v.fd = open(av[1], O_RDONLY)) == -1) ? (ft_exit()) : (0);
	ft_check_tetri(v.fd);
	(close(v.fd) == -1) ? (ft_exit()) : (0);
	((v.fd = open(av[1], O_RDONLY)) == -1) ? (ft_exit()) : (0);
	ft_errors(v.fd, &v.file);
	(close(v.fd) == -1) ? (ft_exit()) : (0);
	v.file = ft_letters(v.file);
	coord = ft_store(v.file);
	free(v.file);
	v.tab = ft_result(*coord, ft_nbr_blk(*coord));
	free(coord);
	ft_display(v.tab);
	free(v.tab);
	return (0);
}
static int		ft_readfile(int const fd, char **file)
{
	char	*buff;
	char	*tmp;
	int		ret;

	if ((buff = ft_strnew(BUFF_SIZE + 1)) == NULL)
		return (-1);
	while ((ret = read(fd, buff, BUFF_SIZE)) > 0)
	{
		tmp = *file;
		*file = ft_strjoin(*file, buff);
		ft_strdel(&tmp);
		if (ft_strchr(buff, '\n') != NULL)
		{
			ft_strdel(&buff);
			return (ret);
		}
	}
	ft_strdel(&buff);
	ft_strdel(file);
	return (ret);
}
Exemple #4
0
static char	*get_word_and_append_tab(char **tab, char *buf, char c)
{
	size_t	len;
	size_t	i;
	char	*word;

	len = 0;
	i = 0;
	while (buf[len] && buf[len] != c)
		len++;
	word = ft_strnew(len);
	if (word)
	{
		while (i < len)
		{
			word[i] = buf[i];
			i++;
		}
		word[i] = '\0';
		append_tab(tab, word);
	}
	return (NULL);
}
Exemple #5
0
char				*ft_mtoa(intmax_t n)
{
	char			*str;
	size_t			neg;
	size_t			len;

	len = ft_nb_digit(n);
	neg = (n < 0) ? 1 : 0;
	str = ft_strnew(len + neg + 1);
	if (n == 0)
	{
		str[0] = '0';
		return (str);
	}
	if (neg == 1)
	{
		n = -n;
		str[0] = '-';
	}
	str[len + neg] = '\0';
	ft_num_to_char(str, n, len, neg);
	return (str);
}
Exemple #6
0
static char		*ft_modif_retour(char *str)
{
	int		i;
	int		j;
	char	*new_str;

	i = 0;
	j = 0;
	while (str[i] != '\0')
		if (str[i++] == '\n')
			j++;
	new_str = ft_strnew(ft_strlen(str) + j);
	i = 0;
	j = 0;
	while (str[i] != '\0')
	{
		if (str[i] == '\n')
			i++;
		else
			new_str[j++] = str[i++];
	}
	return (new_str);
}
Exemple #7
0
char	*ft_strsub(char const *s, unsigned int start, size_t len)
{
	char	*result;
	int		i;

	i = 0;
	if (start == 0 && len == 0)
		return (ft_strnew(1));
	if (s && len && (result = (char*)malloc(sizeof(char)
	* (len + 1))))
	{
		while (s[start] && len)
		{
			result[i] = s[start];
			start++;
			i++;
			len--;
		}
		result[i] = '\0';
		return (result);
	}
	return (NULL);
}
Exemple #8
0
static char *ft_itoaalloc(int n)
{
	char 	*snum;
	size_t 	size;
	int 	sign;

	sign = 1;
	size = 0;
	if (n < 0)
	{
		size++;
		sign *= -1;
	}
	while (n > 9)
	{
		size++;
		n /= 10;
	}
	snum = ft_strnew(size + 1);
	if (sign < 0)
		snum[0] = '-';
	return (snum);
}
Exemple #9
0
char	*ft_itoa(int n)
{
	int		i;
	long	j;
	char	*str;

	i = ft_intlen(n);
	if (!(str = ft_strnew(i)))
		return (NULL);
	j = n;
	if (n < 0)
		j = -j;
	str[i--] = '\0';
	while (i >= 0 && str[i] != '-')
	{
		str[i] = (j % 10) + 48;
		j = j / 10;
		i--;
	}
	if (n < 0)
		str[0] = '-';
	return (str);
}
Exemple #10
0
void			env_set(t_vector *env, char *name, char *value)
{
	t_env_item		*item;
	char			*full;

	if (ft_strchr(name, '=') || ft_strchr(value, '='))
	{
		error_print(0, NULL, "variable or value can not contain =");
		return ;
	}
	full = ft_strnew(ft_strlen(value) + ft_strlen(name) + 1);
	full = ft_strcat(full, name);
	full = ft_strcat(full, "=");
	full = ft_strcat(full, value);
	item = env_item_find(env, name);
	if (item)
	{
		item->value = value;
		item->full = full;
	}
	else
		vector_add(env, env_item_new(full));
}
Exemple #11
0
char			*ft_strtrim(char const *s)
{
	int		deb;
	int		end;
	int		i;
	char	*str;

	if (!s)
		return (NULL);
	deb = ft_deb((char *)s);
	end = ft_end((char *)s);
	if (deb > end)
		return (NULL);
	str = ft_strnew((end - deb) + 1);
	if (!str)
		return (NULL);
	if (end == 0)
		return (str);
	i = 0;
	while (deb <= end)
		str[i++] = s[deb++];
	return (str);
}
Exemple #12
0
char			*ft_base16(uintmax_t n)
{
	long unsigned int	div;
	int					mod;
	char				*convert;
	int					i;

	i = 0;
	convert = ft_strnew(11);
	div = n;
	while (div != 0)
	{
		div = n / 16;
		mod = n % 16;
		n = div;
		if (mod > 9)
			convert[i++] = mod + 55;
		else
			convert[i++] = mod + 48;
	}
	convert = ft_strrev(convert);
	return (convert);
}
Exemple #13
0
static char	*get_permission(t_stat stat)
{
	char	*perm;

	perm = ft_strnew(10);
	perm[0] = '-';
	perm[0] = (S_ISDIR(stat.st_mode)) ? 'd' : perm[0];
	perm[0] = (S_ISFIFO(stat.st_mode)) ? 'p' : perm[0];
	perm[0] = (S_ISCHR(stat.st_mode)) ? 'c' : perm[0];
	perm[0] = (S_ISBLK(stat.st_mode)) ? 'b' : perm[0];
	perm[0] = (S_ISSOCK(stat.st_mode)) ? 's' : perm[0];
	perm[0] = (S_ISLNK(stat.st_mode)) ? 'l' : perm[0];
	perm[1] = stat.st_mode & S_IRUSR ? 'r' : '-';
	perm[2] = stat.st_mode & S_IWUSR ? 'w' : '-';
	perm[3] = stat.st_mode & S_IXUSR ? 'x' : '-';
	perm[4] = stat.st_mode & S_IRGRP ? 'r' : '-';
	perm[5] = stat.st_mode & S_IWGRP ? 'w' : '-';
	perm[6] = stat.st_mode & S_IXGRP ? 'x' : '-';
	perm[7] = stat.st_mode & S_IROTH ? 'r' : '-';
	perm[8] = stat.st_mode & S_IWOTH ? 'w' : '-';
	perm[9] = stat.st_mode & S_IXOTH ? 'x' : '-';
	return (perm);
}
Exemple #14
0
char		*ft_strtrim(char const *s)
{
	char	*tmp;
	size_t	len;

	if (s == NULL)
		return (NULL);
	tmp = (char *)s;
	while (ft_isspace(*tmp))
		tmp++;
	len = ft_strlen(tmp);
	if (len)
	{
		while (ft_isspace(tmp[--len]) || *tmp == '\0')
			;
		tmp = ft_strsub(tmp, 0, (len + 1));
	}
	else
	{
		tmp = ft_strnew(1);
	}
	return (tmp);
}
Exemple #15
0
char	*ft_itoa(int n)
{
	int  x;
	char *rpce;

	rpce = ft_strnew(ft_intlen(n));
	if (n < 0)
	{
		*rpce = '-';
		n = n * (-1);
		rpce++;
	}
	x = n % 10;
	if (n > 9)
	{
		rpce = ft_strjoin(rpce, ft_itoa((n - x) / 10));
		*rpce = '0' + x;
	}
	else
		*rpce = '0' + n;

	return (rpce);
}
Exemple #16
0
void			handle_key(t_elem **list, struct termios *backup)
{
	char	*buf;
	int		update_screen;
	int		*enought_space;

	buf = ft_strnew(4);
	update_screen = 1;
	enought_space = get_enought_space();
	while (!is_esc(buf))
	{
		*enought_space = check_window_size(*list);
		if (update_screen && *enought_space)
			print_arg_list(*list);
		ft_bzero(buf, ft_strlen(buf));
		if (read(0, buf, 3) == -1)
			ft_error("Read error", 1);
		if (*enought_space
				&& (update_screen = check_key(list, backup, buf)) == -1)
			break ;
	}
	ft_strdel(&buf);
}
Exemple #17
0
char		*ft_itobase(unsigned int n, unsigned int b)
{
    char	*str;
    char	*tmp;
    int		i;
    int		len;

    len = sizeof(unsigned int) * 8;
    str = ft_strnew(len);
    ft_memset(str, ' ', len);
    i = 1;
    while (n >= b)
    {
        str[len - i] = ft_getchar(n % b);
        n = n / b;
        i++;
    }
    str[len - i] = ft_getchar(n);
    tmp = str;
    str = ft_strtrim(str);
    free(tmp);
    return (str);
}
Exemple #18
0
char	*ft_strtrim(char const *s)
{
	int		ln;
	int		tr_beg;
	int		tr_end;
	char	*str_cpy_s;
	int		ln_t;

	tr_beg = 0;
	tr_end = 0;
	ln = ft_strlen(s);
	ln_t = ln;
	str_cpy_s = ft_strnew(ln);
	while (s[tr_beg] == ' ' || s[tr_beg] == '\n' || s[tr_beg] == '\t')
		tr_beg++;
	while (s[ln_t - 1] == ' ' || s[ln_t - 1] == '\n' || s[ln_t - 1] == '\t')
	{
		ln_t--;
		tr_end++;
	}
	ft_strncpy(str_cpy_s, s + tr_beg, ln - (tr_beg + tr_end));
	return (str_cpy_s);
}
Exemple #19
0
char	*ft_citoa_base(char n, int base, char *trans_table)
{
	char	sgn;
	char	*aux;
	int		i;

	i = 0;
	sgn = 0;
	if (n < 0)
		sgn = 1;
	aux = ft_strnew(15);
	if (n == 0)
		aux[0] = '0';
	while (n != 0)
	{
		aux[i++] = trans_table[ft_abs(n % base)];
		n /= base;
	}
	if (sgn && base == 10)
		aux[i] = '-';
	aux = ft_strrev(aux);
	return (aux);
}
Exemple #20
0
static t_gnl		*manage_static_struct(t_gnl **rec, const int fd)
{
	t_gnl	*rest;

	rest = *rec;
	while (rest)
	{
		rest->line = 0;
		if (rest->fd == fd)
			return (rest);
		rest = rest->next;
	}
	if (!(rest = (t_gnl *)malloc(sizeof(t_gnl))))
		return (NULL);
	rest->fd = fd;
	rest->rest = ft_strnew(BUFF_SIZE);
	rest->size_rest = 0;
	rest->read_ret = -2;
	rest->line = 0;
	rest->next = *rec;
	*rec = rest;
	return (rest);
}
Exemple #21
0
char	*ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
	char	*p_s;
	char	*s_new;
	char	*p_s_new;
	size_t	n;

	if (!s || !f)
		return (NULL);
	n = 0;
	s_new = ft_strnew(ft_strlen(s));
	if (s_new != NULL)
	{
		p_s = (char *)s;
		p_s_new = s_new;
		while (*p_s)
		{
			*p_s_new++ = f(n, *p_s++);
			n++;
		}
	}
	return (s_new);
}
Exemple #22
0
static int			check_end_gnl(t_gnl *rest)
{
	char			*buff;
	int				ret;
	int				fd;

	fd = rest->fd;
	if (rest->size_rest == 0 && fd != 0 && fd != 1 && fd != 2)
	{
		buff = ft_strnew(BUFF_SIZE + 1);
		ft_bzero(rest->rest, BUFF_SIZE + 1);
		if ((ret = read(rest->fd, buff, BUFF_SIZE)) == 0)
			return (1);
		else
		{
			ft_strcpy(rest->rest, buff);
			rest->size_rest = ft_strlen(buff);
			rest->read_ret = ret;
		}
		free(buff);
	}
	return (0);
}
Exemple #23
0
static inline char	*ft_print_fmode(mode_t details)
{
	char	*rights;

	rights = ft_strnew(10);
	rights[0] = file_mode(details, 0);
	rights[1] = details & S_IRUSR ? 'r' : '-';
	rights[2] = details & S_IWUSR ? 'w' : '-';
	rights[3] = details & S_IXUSR ? 'x' : '-';
	if (details & S_ISUID)
		rights[3] = details & S_IXUSR ? 's' : 'S';
	rights[4] = details & S_IRGRP ? 'r' : '-';
	rights[5] = details & S_IWGRP ? 'w' : '-';
	rights[6] = details & S_IXGRP ? 'x' : '-';
	if (details & S_ISGID)
		rights[6] = details & S_IXGRP ? 's' : 'S';
	rights[7] = details & S_IROTH ? 'r' : '-';
	rights[8] = details & S_IWOTH ? 'w' : '-';
	rights[9] = details & S_IXGRP ? 'x' : '-';
	if (details & S_ISVTX)
		rights[9] = details & S_IXGRP ? 't' : 'T';
	return (rights);
}
Exemple #24
0
char			*ft_itoa(int n)
{
	char	*str;
	char	*strtemp;
	size_t	negative;
	size_t	i;

	negative = (n < 0) ? 1 : 0;
	n = (negative) ? n : -n;
	i = (n) ? ft_itoa_count(n) : 1;
	if ((str = ft_strnew(i + negative)) == NULL)
		return (NULL);
	strtemp = str;
	if (negative)
		*str++ = '-';
	str += i - 1;
	while (i-- > 0)
	{
		*str-- = -(n % 10) + 48;
		n /= 10;
	}
	return (strtemp);
}
Exemple #25
0
char	*ft_lltoa_base(long long n, int base, char *table)
{
	int		i;
	char	sign;
	char	*str;

	i = 0;
	sign = 0;
	if (n < 0)
		sign = 1;
	str = ft_strnew(32);
	if (n == 0)
		str[0] = '0';
	while (n != 0)
	{
		str[i++] = table[ft_abs(n % base)];
		n /= base;
	}
	if (sign && base == 10)
		str[i] = '-';
	str = ft_strrev(str);
	return (str);
}
Exemple #26
0
int			main(int listsize, char **list)
{
	int				i;
	char			*options;
	t_filedir		*tmp_fldr;
	t_arraylist		*filedirs;

	options = check_malloc(ft_strnew(0));
	i = deal_options(&options, listsize, list);
	list = list + i;
	listsize = listsize - i;
	if (listsize <= 1)
	{
		if (listsize < 1)
			tmp_fldr = direct_filedir(".");
		else
			tmp_fldr = direct_filedir(*list);
		low_main(options, tmp_fldr);
	}
	else if ((filedirs = filedirs_from_list(listsize, list)) != NULL)
		ls_first(options, filedirs);
	return (0);
}
Exemple #27
0
static char		*add_w(size_t *i, char const *s, char c)
{
	size_t		size;
	size_t		j;
	char		*word;

	size = *i;
	j = 0;
	while (s[size] && s[size] != c)
		size++;
	word = ft_strnew(size - *i);
	if (word)
	{
		while (*i < size)
		{
			word[j] = s[*i];
			j++;
			*i += 1;
		}
		return (word);
	}
	return (0);
}
Exemple #28
0
char	*get_inst_name(char **line)
{
	char	*start;
	char	*name;
	int		i;
	t_op	*tab;

	start = *line;
	while (**line && !ft_isalpha(**line))
		(*line)++;
	name = ft_strnew(1);
	i = 0;
	while (**line && ft_isalpha(**line))
	{
		name[i++] = **line;
		name = ft_str_realloc(name, ft_strlen(name) + 1);
		(*line)++;
	}
	if ((tab = return_op_tab(name)))
		return (name);
	*line = start;
	return (NULL);
}
Exemple #29
0
char	*ft_strjoin(char const *s1, char const *s2)
{
	char	*str;
	int		len_s1;
	int		len_s2;

	if (!s1 && s2)
		return (ft_strdup(s1));
	if (!s2 && s1)
		return (ft_strdup(s2));
	if (!s1 && !s2)
		return (NULL);
	len_s1 = ft_strlen(s1);
	len_s2 = ft_strlen(s2);
	str = ft_strnew(len_s1 + len_s2 + 1);
	if (!str)
		return (NULL);
	if (!ft_strcat(str, s1))
		return (NULL);
	if (!ft_strcat(str, (char *)s2))
		return (NULL);
	return (str);
}
Exemple #30
0
static char		*ft_read(int fd, int *ret)
{
	char	buff[BUFF_SIZE + 1];
	char	*tmpl;
	int		eof;

	ft_bzero(buff, BUFF_SIZE + 1);
	tmpl = ft_strnew(BUFF_SIZE);
	while (!ft_strchr(buff, '\n'))
	{
		eof = read(fd, buff, BUFF_SIZE);
		if (eof < 0)
			return (NULL);
		if (!eof)
		{
			*ret = 1;
			return (tmpl);
		}
		buff[eof] = '\0';
		tmpl = ft_realloc_join(&tmpl, buff);
	}
	return (tmpl);
}