minishell/libft/srcs/ft_printf_check_fields.c
tvdu29 2a2f5bd4d3
Dev (#2)
* Restart from scratch

branched out on dev
removed all previous source files
started test on new env2lst function

* norm: splitted functions in main into appropriate file

Moved lstdelenvelem & env2lst to ms_env.c
Normed out files

* Feat(WIP): Added possibility to cmd_env to execute commands with
altered env.

Added dummy function for cd, echo, setenv & unsetenv
Started work on env copy feature

* feature: switched env from ft_list to specific list

* feature: added execution of commands from path

* feature: added env, setenv & unsetenv builtins

added -i option & command execution to env
added setenv & unsetenv builtins

* Clean-up: normed out files

* Changed comportment on error for check_path functions

* feature: added completion for ~ and ${}

WIP leaks detected on ${} completion

* fix leak on  ext need test

* feature: added echo cmd

changed printf ref to ft_printf

* feature: add cd built-in

* Simple norm-out

* moved env functions to libft

* fixed out-of-memory access in extension

* Resolved infinite loop in extension revolving

WIP norming in cd

* Normedout cmd_cd.c

* Normed out functions in cmd_cd

* Normed out funtions

sorting out needed

* removed -fsanitize from Makefile

* corrected env -i crash & SHLVL

* Delete norm.txt

* added put_error_cd2 in put_error_cd.c

* added inline environment
2020-01-31 15:06:17 +01:00

143 lines
3.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_fields.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: klebon <klebon@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/29 12:33:40 by klebon #+# #+# */
/* Updated: 2019/03/07 22:16:54 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void check_flags(const char **str, t_conv *field)
{
while (**str == '-' || **str == '+' || **str == '0' || **str == '#'
|| **str == ' ')
{
if (**str == '-')
field->fl_minus = 1;
else if (**str == '+')
field->fl_plus = 1;
else if (**str == '0')
field->fl_zero = 1;
else if (**str == '#')
field->fl_hashtag = 1;
else if (**str == ' ')
field->fl_space = 1;
(*str)++;
}
}
void check_width(const char **str, t_conv *field, va_list ap)
{
int tmp;
if (**str == '*')
{
tmp = va_arg(ap, int);
field->fl_witdth = (tmp < 0) ? -tmp : tmp;
if (tmp < 0)
field->fl_minus = 1;
(*str)++;
}
else
{
field->fl_witdth = 0;
while (**str >= '0' && **str <= '9')
{
field->fl_witdth *= 10;
field->fl_witdth += **str - '0';
(*str)++;
}
}
}
ssize_t check_precision(const char **str, t_conv *field, va_list ap)
{
int tmp;
if (**str == '.')
{
(*str)++;
if (**str == '*')
{
if ((tmp = va_arg(ap, int)) >= 0)
field->fl_prec = tmp;
(*str)++;
}
else if (**str >= '0' && **str <= '9')
{
field->fl_prec = 0;
while (**str >= '0' && **str <= '9')
{
field->fl_prec *= 10;
field->fl_prec += **str - '0';
(*str)++;
}
}
else
field->fl_prec = 0;
}
return (1);
}
void check_size(const char **str, t_conv *field)
{
if (**str == 'h' || **str == 'l' || **str == 'j' || **str == 'z'
|| **str == 'L')
{
if (**str == 'h' && *(*str + 1) == 'h')
{
field->fl_size = hh;
(*str)++;
}
else if (**str == 'h')
field->fl_size = h;
else if (**str == 'l' && *(*str + 1) == 'l')
{
field->fl_size = ll;
(*str)++;
}
else if (**str == 'l')
field->fl_size = l;
else if (**str == 'j')
field->fl_size = l;
else if (**str == 'z')
field->fl_size = l;
else if (**str == 'L')
field->fl_size = L;
(*str)++;
}
}
ssize_t check_fields(const char **str, va_list ap, t_conv *field)
{
init_struct_conv(field);
check_flags(str, field);
check_width(str, field, ap);
if (!(check_precision(str, field, ap)))
return (0);
check_size(str, field);
if (!(check_type_one(str, field)) || !(check_type_two(str, field)))
return (0);
if ((field->fl_type != d && field->fl_type != D && field->fl_type != i
&& field->fl_type != f) || field->fl_plus == 1)
field->fl_space = 0;
if (field->fl_type < s && field->fl_prec != -1)
field->fl_zero = 0;
else if (field->fl_type < s && field->fl_prec == -1)
field->fl_prec = 1;
if (field->fl_type == C && field->fl_size == h)
field->fl_type = c;
if (field->fl_type == c && field->fl_size == l)
field->fl_type = C;
if (field->fl_type == S && field->fl_size == h)
field->fl_type = s;
if (field->fl_type == s && field->fl_size == l)
field->fl_type = S;
return (1);
}