Added ft_strndup, because it is usefull for ft_printf.
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 24 Aug 2023 07:47:08 +0000 (09:47 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 24 Aug 2023 07:47:08 +0000 (09:47 +0200)
Changed libft.h accordingly.

ft_strndup.c [new file with mode: 0644]
libft.h

diff --git a/ft_strndup.c b/ft_strndup.c
new file mode 100644 (file)
index 0000000..84e4bde
--- /dev/null
@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strndup.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/08/24 09:18:49 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/08/24 09:24:51 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <stdlib.h>    // For the malloc function
+#include "libft.h"
+
+char   *ft_strndup(const char *s, size_t n)
+{
+       char    *dest;
+       size_t  s_len;
+
+       s_len = ft_strlen(s);
+       if (s_len < n)
+               n = s_len;
+       dest = malloc((n + 1) * sizeof(char));
+       if (dest == NULL)
+               return (dest);
+       ft_strlcpy(dest, s, n);
+       dest[n + 1] = '\0';
+       return (dest);
+}
diff --git a/libft.h b/libft.h
index 1d8eb2581ae34249c6826649d4cd79267578651d..9008b3fa076f7b1a1a4642800e19e13bd3492b62 100644 (file)
--- a/libft.h
+++ b/libft.h
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/08/15 12:58:15 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/08/16 17:12:57 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/08/24 09:24:32 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -40,6 +40,7 @@ char  *ft_strchr(const char *s, int c);
 char   *ft_strrchr(const char *s, int c);
 char   *ft_strnstr(const char *big, const char *little, size_t len);
 char   *ft_strdup(const char *s);
+char   *ft_strndup(const char *s, size_t n);
 
 int            ft_toupper(int c);
 int            ft_tolower(int c);