ft_putnbr_fd: initial implementation

NOTE: When compiling with gcc and checking the produced library with nm,
we get the following output:

ft_putnbr_fd.o:
                 U ft_putchar_fd
0000000000000000 T ft_putnbr_fd
                 U __stack_chk_fail
                 U write

As you can see, there is an undefined function that is used in this
object file, "__stack_chk_fail". This function is not allowed by the
subject! Only write is allowed!

I am told that the moulinette compiles with clang, which does not
exhibit this behaviour. However, it may be that gcc is used instead,
which may cause my project to fail. (Also, an evaluator may mark me down
for that). I don't want either of those two things to happen, so I will
code another implementation which uses recursion, as this kind of
impelmentation does not seem to cause this behaviour with gcc.

Thanks to tchampio for his help debugging this issue.
This commit is contained in:
Khaïs COLIN 2024-10-18 16:31:16 +02:00
parent b1f1b8a13f
commit 07704a2d59
3 changed files with 64 additions and 2 deletions

59
ft_putnbr_fd.c Normal file
View file

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 15:45:29 by kcolin #+# #+# */
/* Updated: 2024/10/18 16:16:54 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
char buf[10];
int buf_idx;
long num;
num = n;
buf_idx = 10;
if (num < 0)
{
ft_putchar_fd('-', fd);
num = -num;
}
if (num == 0)
{
ft_putchar_fd('0', fd);
return ;
}
while (num != 0)
{
buf_idx--;
buf[buf_idx] = (num % 10) + '0';
num /= 10;
}
write(fd, buf + buf_idx, 10 - buf_idx);
}
/*
#include <limits.h>
int main(void)
{
ft_putnbr_fd(0, 1);
ft_putchar_fd('\n', 1);
ft_putnbr_fd(42, 1);
ft_putchar_fd('\n', 1);
ft_putnbr_fd(-42, 1);
ft_putchar_fd('\n', 1);
ft_putnbr_fd(INT_MAX, 1);
ft_putchar_fd('\n', 1);
ft_putnbr_fd(INT_MIN, 1);
ft_putchar_fd('\n', 1);
return (0);
}
*/