2022-04-13 08:01:46 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* main.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: baco <baco@student.42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2022/04/13 06:58:46 by gbaconni #+# #+# */
|
2022-04-15 20:55:58 +02:00
|
|
|
/* Updated: 2022/04/15 20:48:55 by gbaconni ### lausanne.ch */
|
2022-04-13 08:01:46 +02:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#include <stdio.h> // printf
|
|
|
|
|
#include <stdarg.h> // va_list, va_start, va_end
|
|
|
|
|
#include <unistd.h> // write
|
|
|
|
|
#include <string.h> // strlen
|
|
|
|
|
#include <stdlib.h> // malloc, free
|
|
|
|
|
#include <ctype.h> // isdigit
|
|
|
|
|
#include "libftprintf/libftprintf.h"
|
|
|
|
|
|
|
|
|
|
int isnumber(char *s);
|
|
|
|
|
int isstring(char *s);
|
|
|
|
|
|
|
|
|
|
int isnumber(char *s)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
while (s[i])
|
|
|
|
|
{
|
|
|
|
|
if (!isdigit(s[i]))
|
|
|
|
|
return (0);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int isstring(char *s)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
while (s[i])
|
|
|
|
|
{
|
|
|
|
|
if (!isprint(s[i]))
|
|
|
|
|
return (0);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
int diff_printf_decimal(const char *format, va_list ap);
|
|
|
|
|
int diff_printf_decimal(const char *format, va_list ap)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
int d;
|
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
d = 0;
|
|
|
|
|
d = atoi(argv[2]);
|
|
|
|
|
ret = printf(format, d);
|
|
|
|
|
printf(": %d = printf(\"%s\", %d)\n", ret, format, d);
|
|
|
|
|
ret = ft_printf(format, d);
|
|
|
|
|
printf(": %d = ft_printf(\"%s\", %d)\n", ret, format, d);
|
|
|
|
|
return (ret);
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
char *format;
|
|
|
|
|
int d;
|
|
|
|
|
char c;
|
|
|
|
|
char *s;
|
2022-04-15 17:20:15 +02:00
|
|
|
int x;
|
|
|
|
|
void *ptr;
|
2022-04-13 08:01:46 +02:00
|
|
|
|
|
|
|
|
d = 0;
|
|
|
|
|
c = '\0';
|
|
|
|
|
s = "";
|
2022-04-15 17:20:15 +02:00
|
|
|
x = 42;
|
|
|
|
|
ptr = &x;
|
2022-04-13 08:01:46 +02:00
|
|
|
(void)s;
|
|
|
|
|
if (argc > 1)
|
|
|
|
|
{
|
|
|
|
|
format = argv[1];
|
2022-04-15 20:55:58 +02:00
|
|
|
if (format[0] == '%')
|
|
|
|
|
{
|
|
|
|
|
if (format[1] == 'c')
|
|
|
|
|
{
|
|
|
|
|
c = argv[2][0];
|
|
|
|
|
ret = printf(format, c);
|
|
|
|
|
printf(": %d = printf(\"%s\", '%c')\n", ret, format, c);
|
|
|
|
|
ret = ft_printf(format, c);
|
|
|
|
|
printf(": %d = ft_printf(\"%s\", '%c')\n", ret, format, c);
|
|
|
|
|
}
|
|
|
|
|
else if (format[1] == 's')
|
|
|
|
|
{
|
|
|
|
|
s = argv[2];
|
|
|
|
|
ret = printf(format, s);
|
|
|
|
|
printf(": %d = printf(\"%s\", \"%s\")\n", ret, format, s);
|
|
|
|
|
ret = ft_printf(format, s);
|
|
|
|
|
printf(": %d = ft_printf(\"%s\", \"%s\")\n", ret, format, s);
|
|
|
|
|
}
|
|
|
|
|
else if (format[1] == 'p')
|
|
|
|
|
{
|
|
|
|
|
s = argv[2];
|
|
|
|
|
if (ft_strlen(s) == 0)
|
|
|
|
|
{
|
|
|
|
|
s = NULL;
|
|
|
|
|
ptr = NULL;
|
|
|
|
|
}
|
|
|
|
|
ret = printf(format, ptr);
|
|
|
|
|
printf(": %d = printf(\"%s\", \"%s\")\n", ret, format, s);
|
|
|
|
|
ret = ft_printf(format, ptr);
|
|
|
|
|
printf(": %d = ft_printf(\"%s\", \"%s\")\n", ret, format, s);
|
|
|
|
|
}
|
|
|
|
|
else if (format[1] == 'd' || format[1] == 'i' || format[1] == 'u')
|
|
|
|
|
{
|
|
|
|
|
d = atoi(argv[2]);
|
|
|
|
|
ret = printf(format, d);
|
|
|
|
|
printf(": %d = printf(\"%s\", %d)\n", ret, format, d);
|
|
|
|
|
ret = ft_printf(format, d);
|
|
|
|
|
printf(": %d = ft_printf(\"%s\", %d)\n", ret, format, d);
|
|
|
|
|
}
|
|
|
|
|
else if (format[1] == 'x' || format[1] == 'X')
|
|
|
|
|
{
|
|
|
|
|
d = atoi(argv[2]);
|
|
|
|
|
ret = printf(format, d);
|
|
|
|
|
printf(": %d = printf(\"%s\", %d)\n", ret, format, d);
|
|
|
|
|
ret = ft_printf(format, d);
|
|
|
|
|
printf(": %d = ft_printf(\"%s\", %d)\n", ret, format, d);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2022-04-13 08:01:46 +02:00
|
|
|
{
|
|
|
|
|
s = argv[2];
|
|
|
|
|
ret = printf(format, s);
|
|
|
|
|
printf(": %d = printf(\"%s\", \"%s\")\n", ret, format, s);
|
|
|
|
|
ret = ft_printf(format, s);
|
|
|
|
|
printf(": %d = ft_printf(\"%s\", \"%s\")\n", ret, format, s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (0);
|
|
|
|
|
}
|