예제 #1
0
파일: ft_cd.c 프로젝트: vilsbole/42sh
static int	ft_cd_check_opts(char *str, char *opts)
{
	int			i;
	char		tmp[20];

	i = 1;
	ft_bzero(tmp, 20);
	while (str[i] != '\0')
	{
		if (ft_strichr(FTSH_BLT_OPTS, str[i]) >= 0)
		{
			if (ft_strichr(opts, str[i]) < 0)
				opts[ft_strlen(opts)] = str[i];
		}
		else
		{
			ft_strcpy(tmp, "illegal option -- ");
			tmp[18] = str[i];
			ft_error("cd", tmp, NULL);
			return (ft_error("Usage", FTSH_BLT_USAGE, NULL));
		}
		i++;
	}
	return (0);
}
void		ft_deal_with_types_and_conversion(t_op *op, t_arg *lnk)
{
	if (op->conv == 'd' || op->conv == 'i')
	{
		ft_create_padding(op, lnk);
		if (op->flag && (ft_strichr(op->flag, ' ') != -1) && ((int)lnk->i >= 0)
			&& (ft_strichr(op->flag, '+') == -1))
		{
			lnk->ps += 1;
			ft_putchar(' ');
		}
		else if (op->flag && ((int)lnk->i >= 0)
				&& (ft_strichr(op->flag, '+') != -1))
		{
			lnk->ps += 1;
			ft_putchar('+');
		}
		ft_deal_with_int_conversion_part1(op, lnk);
	}
	else if (op->conv == 'u')
	{
		ft_create_padding(op, lnk);
		ft_deal_with_uint_conversion_part1(op, lnk);
	}
	ft_deal_with_types_and_conversion_2(op, lnk);
}
void		ft_deal_with_octal_conversion(t_op *op, t_arg *lnk)
{
	if (op->flag && ft_strichr(op->flag, '#') != -1 && lnk->ull != 0)
		ft_putchar('0');
	if (ft_strncmp(op->mod, "hh", 2) == 0)
	{
		ft_putchar((UCHAR)lnk->ull);
		lnk->ps += 1;
	}
	else if (ft_strncmp(op->mod, "h", 1) == 0)
	{
		ft_putstr(ft_convert_to_octal((USHORT)lnk->ull));
		lnk->ps += ft_strlen(ft_convert_to_octal((USHORT)lnk->ull));
	}
	else if (CMP(op->mod, "ll", 2) == 0 || CMP(op->mod, "l", 1) == 0
			|| CMP(op->mod, "q", 1) == 0)
	{
		ft_putstr(ft_convert_to_octal((ULONG)lnk->ull));
		lnk->ps += ft_strlen(ft_convert_to_octal((ULONG)lnk->ull));
	}
	else
	{
		ft_putstr(ft_convert_to_octal((UINT)lnk->ull));
		lnk->ps += ft_strlen(ft_convert_to_octal((UINT)lnk->ull));
	}
	if (op->flag && ft_strichr(op->flag, '#') != -1 && lnk->ull != 0)
		lnk->ps += 1;
}
예제 #4
0
int			ft_env_setep(t_datas *datas, t_bltenv *env, char **cmd, int i)
{
	int			n;

	env->ep = NULL;
	n = ft_env_setep_count(cmd, i);
	if (n == 0 && ft_strichr(env->opts, 'i') >= 0)
		return (i);
	else if (n != 0 && ft_strichr(env->opts, 'i') >= 0)
		return (ft_env_setep_new(env, cmd, i, n));
	env->ep = ft_arrdup(datas->env);
	if (env->ep == NULL)
		return (-1);
	return (ft_env_setep_dup(env, cmd, i, n));
}
예제 #5
0
파일: get_next_line.c 프로젝트: Succubae/42
int				get_next_line(int const fd, char **line)
{
	static t_buf	**buflst = NULL;
	int				n;
	char			*buf;

	if (BUF_SIZE < 1 || !(*line = ft_strnew(0)))
		return (-1);
	if (!buflst)
	{
		buflst = (t_buf**)malloc(sizeof(t_buf*));
		*buflst = NULL;
	}
	buf = choose_buf(fd, buflst);
	n = ft_strichr(buf, '\n');
	if (n != -1)
	{
		*line = ft_strsub(buf, 0, n);
		ft_strcpy(buf, buf + n + 1);
		return (1);
	}
	else if (buf[0])
		*line = ft_strdup(buf);
	return (read_file(fd, line, buf));
}
예제 #6
0
파일: get_next_line.c 프로젝트: Succubae/42
static int		read_file(int const fd, char **line, char *buf)
{
	int		n;
	int		ret;
	char	*tmp;

	while ((ret = read(fd, buf, BUF_SIZE)) > 0)
	{
		buf[ret] = '\0';
		if (ret == -1)
			return (-1);
		n = ft_strichr(buf, '\n');
		if (n == -1)
		{
			tmp = ft_strnjoin(*line, buf, ret);
			free(*line);
			*line = tmp;
		}
		else
		{
			*line = ft_strnjoin(*line, buf, n);
			ft_strcpy(buf, buf + n + 1);
			return (1);
		}
	}
	ft_bzero(buf, BUF_SIZE);
	return (**line != '\0');
}
예제 #7
0
int				get_next_line(int const fd, char **line)
{
	static char		*buffer;
	char			buf[BUFF_SIZE + 1];
	int				ret;

	*line = NULL;
	buffer = ((!buffer) ? ft_strnew(BUFF_SIZE + 1) : buffer);
	if (ft_gnl_setline(&buffer, line) == 1)
		return (1);
	while ((ret = read(fd, buf, BUFF_SIZE)))
	{
		buf[ret] = '\0';
		if (ft_strichr(buf, '\n') >= 0)
		{
			ft_strcpy(buffer, buf);
			return (ft_gnl_setline(&buffer, line));
		}
		*line = ft_strfjoin(*line, ft_strdup(buf));
	}
	free(buffer);
	if (ft_strlen(*line) > 0)
		return (1);
	if (ret <= 0)
		return (ret);
	return (0);
}
void		ft_deal_with_hexa_conversion(t_op *op, t_arg *lnk)
{
	if ((op->flag && ft_strichr(op->flag, '#') != -1 && lnk->ull != 0)
		|| op->conv == 'p')
		ft_putstr("0x");
	ft_create_padding(op, lnk);
	if (ft_strncmp(op->mod, "hh", 2) == 0)
	{
		ft_putchar((UCHAR)lnk->ull);
		lnk->ps += 1;
	}
	else if (ft_strncmp(op->mod, "h", 1) == 0)
	{
		ft_putstr(ft_convert_to_hexa((USHORT)lnk->ull, op->conv));
		lnk->ps += ft_strlen(ft_convert_to_hexa((USHORT)lnk->ull, op->conv));
	}
	else if (ft_strncmp(op->mod, "ll", 2) == 0
			|| ft_strncmp(op->mod, "l", 1) == 0
			|| ft_strncmp(op->mod, "q", 1) == 0 || op->conv == 'p')
	{
		ft_putstr(ft_convert_to_hexa((ULONG)lnk->ull, op->conv));
		lnk->ps += ft_strlen(ft_convert_to_hexa((ULONG)lnk->ull, op->conv));
	}
	else
		ft_deal_with_hexa_conversion_2(op, lnk);
}
void		ft_deal_with_hexa_conversion_2(t_op *op, t_arg *lnk)
{
	ft_putstr(ft_convert_to_hexa((UINT)lnk->ull, op->conv));
	lnk->ps += ft_strlen(ft_convert_to_hexa((UINT)lnk->ull, op->conv));
	if ((op->flag && ft_strichr(op->flag, '#') != -1 && lnk->ull != 0)
		|| op->conv == 'p')
		lnk->ps += 2;
}
예제 #10
0
static int	ft_env_setep_count(char **cmd, int i)
{
	int			n;

	n = 0;
	while (cmd[i] != NULL)
	{
		if (ft_strichr(cmd[i], '=') <= 0)
			return (n);
		n++;
		i++;
	}
	return (n);
}
예제 #11
0
파일: ft_cd.c 프로젝트: vilsbole/42sh
static int	ft_cd_get_opts(char **cmd, char *opts)
{
	int			i;

	i = 1;
	while (cmd[i] != NULL)
	{
		if (ft_strcmp(cmd[i], "-") == 0 || ft_strichr(cmd[i], '-') != 0)
			return (i);
		if (ft_strcmp(cmd[i], "--") == 0)
			return (i + 1);
		if (ft_cd_check_opts(cmd[i], opts) != 0)
			return (-1);
		i++;
	}
	return (i);
}
예제 #12
0
char	*ft_getenv(char *s, char **env)
{
	int		i;
	int		j;

	i = j = 0;
	while (env[i] != NULL)
	{
		if (ft_strncmp(s, env[i], ft_strlen(s)) == 0)
		{
			j = ft_strichr(env[i], '=');
			return (s = env[i] + j + 1);
		}
		i++;
	}
	return (NULL);
}
예제 #13
0
int				ft_set_line(t_cache *cache, char **line)
{
	int		fin;

	fin = -1;
	if ((fin = ft_strichr(cache->str, '\n')) == -1)
	{
		*line = ft_strdup(cache->str);
		cache->str = "";
		return (0);
	}
	else
	{
		*line = ft_strsub(cache->str, 0, fin);
		cache->str = ft_strdup(&cache->str[fin + 1]);
		return (1);
	}
}
예제 #14
0
static int	ft_env_setep_dup(t_bltenv *env, char **cmd, int i, int n)
{
	char		*val;
	char		key[FTSH_MAXLEN_LINE];

	ft_bzero(key, FTSH_MAXLEN_LINE);
	while (n--)
	{
		ft_strncpy(key, cmd[i], ft_strichr(cmd[i], '='));
		val = ft_strchr(cmd[i], '=') + 1;
		if (ft_setvar(&(env->ep), key, val) != 0)
		{
			ft_arrdel(&(env->ep));
			return (-1);
		}
		i++;
	}
	return (i);
}
예제 #15
0
int				ft_read(char **line, int fd, t_cache *cache, int fin)
{
	int		bytes;
	char	buff[BUFF_SIZE + 1];

	bytes = 0;
	while (fin == -1 && (bytes = read(fd, buff, BUFF_SIZE)) > 0)
	{
		buff[bytes] = '\0';
		if ((fin = ft_strichr(buff, '\n')) == -1)
			*line = ft_strjoin(*line, buff);
		else
		{
			*line = ft_strjoin(*line, ft_strsub(buff, 0, fin));
			cache->str = ft_strdup(&buff[fin + 1]);
			return (-2);
		}
	}
	return (bytes);
}
예제 #16
0
static int		ft_gnl_setline(char **buffer, char **line)
{
	int				i;

	i = ft_strichr(*buffer, '\n');
	if (i < 0 && *line == NULL)
	{
		*line = ft_strdup(*buffer);
		return (0);
	}
	else if (i == 0 && *line == NULL)
		*line = ft_strnew(1);
	else if (i > 0)
	{
		if (*line == NULL)
			*line = ft_strsub(*buffer, 0, i);
		else
			*line = ft_strfjoin(*line, ft_strsub(*buffer, 0, i));
	}
	ft_strcpy(*buffer, ft_strchr(*buffer, '\n') + 1);
	return (1);
}