minishell/libft/srcs/ft_printf.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

92 lines
2.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: klebon <klebon@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/28 22:27:58 by klebon #+# #+# */
/* Updated: 2019/03/07 22:16:43 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void init_handler_tab(char *(*t[16])(t_conv *, va_list))
{
t[d] = &handle_output_i_d;
t[D] = &handle_output_i_d;
t[i] = &handle_output_i_d;
t[x] = &handle_output_hexa;
t[X] = &handle_output_hexa;
t[p] = &handle_output_hexa;
t[o] = &handle_output_oct;
t[O] = &handle_output_oct;
t[u] = handle_output_u;
t[U] = handle_output_u;
t[c] = &handle_output_char;
t[C] = &handle_output_wchar;
t[s] = &handle_output_str;
t[S] = &handle_output_wstr;
t[percent] = &handle_output_char;
t[f] = &handle_output_float;
}
void clean_mem(t_conv *field, va_list ap)
{
free(field);
va_end(ap);
}
int handle_conv(const char **str, t_conv *field, va_list ap, int *nb_print)
{
char *(*handler[16])(t_conv *, va_list);
init_handler_tab(handler);
(*str)++;
if (check_fields(str, ap, field))
{
if (!(field->str = handler[field->fl_type](field, ap)))
{
clean_mem(field, ap);
return (0);
}
write(1, field->str, field->str_size);
*nb_print += field->str_size;
free(field->str);
}
else
{
clean_mem(field, ap);
return (0);
}
return (1);
}
int ft_printf(const char *format, ...)
{
va_list ap;
const char *str;
int nb_print_chars;
t_conv *field;
if (!(field = (t_conv *)malloc(sizeof(t_conv))))
return (-1);
va_start(ap, format);
str = format;
nb_print_chars = 0;
while (str && *str)
{
if (*str != '%')
{
write(1, str, 1);
++nb_print_chars;
}
else if (!(handle_conv(&str, field, ap, &nb_print_chars)))
return (-1);
++str;
}
clean_mem(field, ap);
return (nb_print_chars);
}