Files
ft_printf/main.c

129 lines
3.0 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: baco <baco@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/13 06:58:46 by gbaconni #+# #+# */
/* Updated: 2022/04/15 17:19:36 by gbaconni ### lausanne.ch */
/* */
/* ************************************************************************** */
#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;
int x;
void *ptr;
d = 0;
c = '\0';
s = "";
x = 42;
ptr = &x;
(void)s;
if (argc > 1)
{
format = argv[1];
if (isnumber(argv[2]))
{
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 (ft_strlen(argv[2]) == 1 && isalnum(argv[2][0]))
{
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 (ft_strlen(argv[2]) > 1 && isstring(argv[2]))
{
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 (ft_strlen(argv[2]) == 0)
{
s = argv[2];
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
{
printf("Error: invalid argument\n");
}
}
return (0);
}