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 20:15:09 +02:00
|
|
|
/* Updated: 2022/04/17 18:51:22 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-17 20:15:09 +02:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/wait.h> // wait
|
2022-04-17 09:59:41 +02:00
|
|
|
|
2022-04-13 08:01:46 +02:00
|
|
|
#include "libftprintf/libftprintf.h"
|
|
|
|
|
|
2022-04-17 20:15:09 +02:00
|
|
|
void ft_begin(int *fd)
|
|
|
|
|
{
|
|
|
|
|
pipe(fd);
|
|
|
|
|
fd[2] = dup(STDOUT_FILENO);
|
|
|
|
|
dup2(fd[1], STDOUT_FILENO);
|
|
|
|
|
}
|
2022-04-13 08:01:46 +02:00
|
|
|
|
2022-04-17 20:15:09 +02:00
|
|
|
void ft_end(int *fd, char *str)
|
2022-04-13 08:01:46 +02:00
|
|
|
{
|
2022-04-17 20:15:09 +02:00
|
|
|
char c;
|
2022-04-13 08:01:46 +02:00
|
|
|
|
2022-04-17 20:15:09 +02:00
|
|
|
dup2(fd[2], STDOUT_FILENO);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
close(fd[1]);
|
|
|
|
|
while (read(fd[0], &c, 1) > 0)
|
|
|
|
|
*str++ = c;
|
|
|
|
|
*str = '\0';
|
|
|
|
|
close(fd[0]);
|
2022-04-13 08:01:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-17 20:15:09 +02:00
|
|
|
void ft_begin2(char *out)
|
2022-04-13 08:01:46 +02:00
|
|
|
{
|
2022-04-17 20:15:09 +02:00
|
|
|
freopen("/dev/null", "a", stdout);
|
|
|
|
|
setbuf(stdout, out);
|
2022-04-13 08:01:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-17 20:15:09 +02:00
|
|
|
void ft_end2(void)
|
2022-04-13 08:01:46 +02:00
|
|
|
{
|
2022-04-17 20:15:09 +02:00
|
|
|
freopen("/dev/tty", "a", stdout);
|
2022-04-17 09:59:41 +02:00
|
|
|
}
|
2022-04-13 08:01:46 +02:00
|
|
|
|
2022-04-17 20:15:09 +02:00
|
|
|
int ft_sprintf(char *str, const char *format, ...)
|
2022-04-17 09:59:41 +02:00
|
|
|
{
|
2022-04-17 20:15:09 +02:00
|
|
|
va_list ap;
|
|
|
|
|
int ret;
|
|
|
|
|
int state;
|
|
|
|
|
char c;
|
|
|
|
|
pid_t cpid;
|
|
|
|
|
int pipefd[3];
|
|
|
|
|
const int piperead = 0;
|
|
|
|
|
const int pipewrite = 1;
|
|
|
|
|
const int pipestdout = 2;
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
pipe(pipefd);
|
|
|
|
|
cpid = fork();
|
|
|
|
|
if (cpid == 0)
|
|
|
|
|
{
|
|
|
|
|
close(pipefd[piperead]);
|
|
|
|
|
pipefd[pipestdout] = dup(STDOUT_FILENO);
|
|
|
|
|
dup2(pipefd[pipewrite], STDOUT_FILENO);
|
|
|
|
|
va_start(ap, format);
|
|
|
|
|
ret = ft_vprintf(format, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
dup2(pipefd[pipestdout], STDOUT_FILENO);
|
|
|
|
|
close(pipefd[pipestdout]);
|
|
|
|
|
close(pipefd[pipewrite]);
|
|
|
|
|
exit (ret);
|
|
|
|
|
} else {
|
|
|
|
|
close(pipefd[pipewrite]);
|
|
|
|
|
while (read(pipefd[piperead], &c, 1) > 0)
|
|
|
|
|
*str++ = c;
|
|
|
|
|
close(pipefd[piperead]);
|
|
|
|
|
wait(&state);
|
|
|
|
|
return (WEXITSTATUS(state));
|
|
|
|
|
}
|
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;
|
2022-04-15 17:20:15 +02:00
|
|
|
int x;
|
|
|
|
|
void *ptr;
|
2022-04-17 20:15:09 +02:00
|
|
|
int fd[3];
|
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];
|
2022-04-17 20:15:09 +02:00
|
|
|
ret = sprintf(out, 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);
|
2022-04-17 20:15:09 +02:00
|
|
|
printf("%d = ft_printf(\"%s\", '%c')\n%s\n\n", ft_ret, format, c, ft_out);
|
2022-04-17 09:59:41 +02:00
|
|
|
assert(ret == ft_ret);
|
|
|
|
|
assert(strcmp(out, ft_out) == 0);
|
2022-04-15 20:55:58 +02:00
|
|
|
}
|
|
|
|
|
else if (format[1] == 's')
|
|
|
|
|
{
|
|
|
|
|
s = argv[2];
|
2022-04-17 20:15:09 +02:00
|
|
|
ret = sprintf(out, format, s);
|
|
|
|
|
printf("%d = printf(\"%s\", \"%s\")\n%s\n", ret, format, s, out);
|
|
|
|
|
ft_begin(fd);
|
2022-04-17 09:59:41 +02:00
|
|
|
ft_ret = ft_printf(format, s);
|
2022-04-17 20:15:09 +02:00
|
|
|
ft_end(fd, ft_out);
|
|
|
|
|
printf("%d = ft_printf(\"%s\", \"%s\")\n%s\n\n", ft_ret, format, s, ft_out);
|
2022-04-17 09:59:41 +02:00
|
|
|
assert(ret == ft_ret);
|
2022-04-17 20:15:09 +02:00
|
|
|
assert(strcmp(out, ft_out) == 0);
|
2022-04-15 20:55:58 +02:00
|
|
|
}
|
|
|
|
|
else if (format[1] == 'p')
|
|
|
|
|
{
|
|
|
|
|
s = argv[2];
|
|
|
|
|
if (ft_strlen(s) == 0)
|
|
|
|
|
{
|
|
|
|
|
s = NULL;
|
|
|
|
|
ptr = NULL;
|
|
|
|
|
}
|
2022-04-17 20:15:09 +02:00
|
|
|
ret = sprintf(out, format, ptr);
|
|
|
|
|
printf("%d = printf(\"%s\", %p)\n%s\n", ret, format, ptr, out);
|
|
|
|
|
ft_begin(fd);
|
2022-04-17 09:59:41 +02:00
|
|
|
ft_ret = ft_printf(format, ptr);
|
2022-04-17 20:15:09 +02:00
|
|
|
ft_end(fd, ft_out);
|
|
|
|
|
printf("%d = ft_printf(\"%s\", %p)\n%s\n\n", ft_ret, format, ptr, ft_out);
|
2022-04-17 09:59:41 +02:00
|
|
|
assert(ret == ft_ret);
|
2022-04-17 20:15:09 +02:00
|
|
|
assert(strcmp(out, ft_out) == 0);
|
2022-04-15 20:55:58 +02:00
|
|
|
}
|
|
|
|
|
else if (format[1] == 'd' || format[1] == 'i' || format[1] == 'u')
|
|
|
|
|
{
|
|
|
|
|
d = atoi(argv[2]);
|
2022-04-17 20:15:09 +02:00
|
|
|
ret = sprintf(out, format, d);
|
|
|
|
|
printf("%d = printf(\"%s\", %d)\n%s\n", ret, format, d, out);
|
|
|
|
|
ft_begin(fd);
|
2022-04-17 09:59:41 +02:00
|
|
|
ft_ret = ft_printf(format, d);
|
2022-04-17 20:15:09 +02:00
|
|
|
ft_end(fd, ft_out);
|
|
|
|
|
printf("%d = ft_printf(\"%s\", %d)\n%s\n\n", ft_ret, format, d, ft_out);
|
2022-04-17 09:59:41 +02:00
|
|
|
assert(ret == ft_ret);
|
2022-04-17 20:15:09 +02:00
|
|
|
assert(strcmp(out, ft_out) == 0);
|
2022-04-15 20:55:58 +02:00
|
|
|
}
|
|
|
|
|
else if (format[1] == 'x' || format[1] == 'X')
|
|
|
|
|
{
|
|
|
|
|
d = atoi(argv[2]);
|
2022-04-17 20:15:09 +02:00
|
|
|
ret = sprintf(out, format, d);
|
|
|
|
|
printf("%d = printf(\"%s\", %d)\n%s\n", ret, format, d, out);
|
|
|
|
|
ft_begin(fd);
|
2022-04-17 09:59:41 +02:00
|
|
|
ft_ret = ft_printf(format, d);
|
2022-04-17 20:15:09 +02:00
|
|
|
ft_end(fd, ft_out);
|
|
|
|
|
printf("%d = ft_printf(\"%s\", %d)\n%s\n\n", ft_ret, format, d, ft_out);
|
2022-04-17 09:59:41 +02:00
|
|
|
assert(ret == ft_ret);
|
2022-04-17 20:15:09 +02:00
|
|
|
assert(strcmp(out, ft_out) == 0);
|
2022-04-15 20:55:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2022-04-13 08:01:46 +02:00
|
|
|
{
|
|
|
|
|
s = argv[2];
|
2022-04-17 20:15:09 +02:00
|
|
|
ret = sprintf(out, format, s);
|
|
|
|
|
printf("%d = printf(\"%s\", \"%s\")\n%s\n", ret, format, s, out);
|
|
|
|
|
ft_begin(fd);
|
2022-04-17 09:59:41 +02:00
|
|
|
ft_ret = ft_printf(format, s);
|
2022-04-17 20:15:09 +02:00
|
|
|
ft_end(fd, ft_out);
|
|
|
|
|
printf("%d = ft_printf(\"%s\", \"%s\")\n%s\n\n", ft_ret, format, s, ft_out);
|
2022-04-17 09:59:41 +02:00
|
|
|
assert(ret == ft_ret);
|
2022-04-17 20:15:09 +02:00
|
|
|
assert(strcmp(out, ft_out) == 0);
|
2022-04-13 08:01:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (0);
|
|
|
|
|
}
|