Files
ft_printf/main.c

161 lines
3.9 KiB
C
Raw Normal View History

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-17 09:59:41 +02:00
/* Updated: 2022/04/17 09:59:04 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
2022-04-17 09:59:41 +02:00
#include <assert.h> // assert
#include <fcntl.h>
2022-04-13 08:01:46 +02:00
#include "libftprintf/libftprintf.h"
2022-04-17 09:59:41 +02:00
int isnumber(char *s);
int isstring(char *s);
2022-04-13 08:01:46 +02:00
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);
}
2022-04-17 09:59:41 +02:00
void ft_begin(int *fd)
2022-04-13 08:01:46 +02:00
{
2022-04-17 09:59:41 +02:00
pipe(fd);
fd[2] = dup(STDOUT_FILENO);
dup2(fd[1], STDOUT_FILENO);
}
2022-04-13 08:01:46 +02:00
2022-04-17 09:59:41 +02:00
void ft_end(int *fd, char *str)
{
close(fd[1]);
read(fd[0], str, sizeof(str));
close(fd[0]);
dup2(fd[2], STDOUT_FILENO);
2022-04-13 08:01:46 +02:00
}
int main(int argc, char *argv[])
{
2022-04-17 09:59:41 +02:00
int ft_ret;
2022-04-13 08:01:46 +02:00
int ret;
2022-04-17 09:59:41 +02:00
char out[256];
char ft_out[256];
2022-04-13 08:01:46 +02:00
char *format;
int d;
char c;
char *s;
int x;
void *ptr;
2022-04-17 09:59:41 +02:00
int fd[3];
2022-04-13 08:01:46 +02:00
d = 0;
c = '\0';
s = "";
x = 42;
ptr = &x;
2022-04-13 08:01:46 +02:00
(void)s;
if (argc > 1)
{
format = argv[1];
if (format[0] == '%')
{
if (format[1] == 'c')
{
c = argv[2][0];
ret = printf(format, c);
2022-04-17 09:59:41 +02:00
printf("%d = printf(\"%s\", '%c')\n%s\n", ret, format, c, out);
ft_begin(fd);
ft_ret = ft_printf(format, c);
ft_end(fd, ft_out);
printf("%d = ft_printf(\"%s\", '%c')\n%s\n", ret, format, c, ft_out);
assert(ret == ft_ret);
assert(strcmp(out, ft_out) == 0);
}
else if (format[1] == 's')
{
s = argv[2];
ret = printf(format, s);
printf(": %d = printf(\"%s\", \"%s\")\n", ret, format, s);
2022-04-17 09:59:41 +02:00
ft_ret = ft_printf(format, s);
printf(": %d = ft_printf(\"%s\", \"%s\")\n", ret, format, s);
2022-04-17 09:59:41 +02:00
assert(ret == ft_ret);
}
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);
2022-04-17 09:59:41 +02:00
ft_ret = ft_printf(format, ptr);
printf(": %d = ft_printf(\"%s\", \"%s\")\n", ret, format, s);
2022-04-17 09:59:41 +02:00
assert(ret == ft_ret);
}
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);
2022-04-17 09:59:41 +02:00
ft_ret = ft_printf(format, d);
printf(": %d = ft_printf(\"%s\", %d)\n", ret, format, d);
2022-04-17 09:59:41 +02:00
assert(ret == ft_ret);
}
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);
2022-04-17 09:59:41 +02:00
ft_ret = ft_printf(format, d);
printf(": %d = ft_printf(\"%s\", %d)\n", ret, format, d);
2022-04-17 09:59:41 +02:00
assert(ret == ft_ret);
}
}
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);
2022-04-17 09:59:41 +02:00
ft_ret = ft_printf(format, s);
2022-04-13 08:01:46 +02:00
printf(": %d = ft_printf(\"%s\", \"%s\")\n", ret, format, s);
2022-04-17 09:59:41 +02:00
assert(ret == ft_ret);
2022-04-13 08:01:46 +02:00
}
}
return (0);
}