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:
parent
b1f1b8a13f
commit
07704a2d59
3 changed files with 64 additions and 2 deletions
5
Makefile
5
Makefile
|
|
@ -6,7 +6,7 @@
|
|||
# By: kcolin <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/10/14 13:43:59 by kcolin #+# #+# #
|
||||
# Updated: 2024/10/18 15:24:03 by kcolin ### ########.fr #
|
||||
# Updated: 2024/10/18 16:25:26 by kcolin ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
|
@ -44,7 +44,8 @@ SOURCES = ft_isalpha.c \
|
|||
ft_striteri.c \
|
||||
ft_putchar_fd.c \
|
||||
ft_putstr_fd.c \
|
||||
ft_putendl_fd.c
|
||||
ft_putendl_fd.c \
|
||||
ft_putnbr_fd.c
|
||||
OBJECTS = $(SOURCES:.c=.o)
|
||||
CC = gcc
|
||||
|
||||
|
|
|
|||
59
ft_putnbr_fd.c
Normal file
59
ft_putnbr_fd.c
Normal 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);
|
||||
}
|
||||
*/
|
||||
2
libft.h
2
libft.h
|
|
@ -61,4 +61,6 @@ void ft_putchar_fd(char c, int fd);
|
|||
void ft_putstr_fd(char *s, int fd);
|
||||
void ft_putendl_fd(char *s, int fd);
|
||||
|
||||
void ft_putnbr_fd(int n, int fd);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue