maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
ft_strndup.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ft_strndup.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: rmikhayl <rmikhayl@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2023/12/17 17:25:28 by rmikhayl #+# #+# */
9/* Updated: 2024/06/03 13:45:23 by rmikhayl ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "libft.h"
14
15/* *************************** ft_strdup ************************************ */
16/* Duplicates n chars string 's' and returns a new identical string. */
17/* If 's' is NULL or memory allocation fails, returns NULL. */
18/* */
19/* In layman's terms: It's like making a photocopy of a piece of text, so */
20/* you have an exact copy of it to use or store separately. */
21/* ************************************************************************** */
22
23char *ft_strndup(const char *s, size_t n)
24{
25 size_t len;
26 char *new;
27
28 len = ft_strlen(s);
29 if (!s)
30 return (NULL);
31 new = malloc((n + 1) * sizeof(char));
32 if (!new)
33 return (NULL);
34 if (n > len)
35 n = len;
36 ft_strlcpy(new, s, n + 1);
37 return (new);
38}
char * ft_strndup(const char *s, size_t n)
Definition ft_strndup.c:23
size_t ft_strlcpy(char *dest, const char *src, size_t size)
Definition ft_strlcpy.c:25
size_t ft_strlen(const char *s)
Definition ft_strlen.c:15