minishell
ft_strndup.c File Reference
#include <stddef.h>
#include <stdlib.h>
#include "../ft_math/ft_math.h"
#include "ft_string.h"
Include dependency graph for ft_strndup.c:

Go to the source code of this file.

Functions

char * ft_strndup (const char *s1, size_t n)
 

Function Documentation

◆ ft_strndup()

char* ft_strndup ( const char *  s1,
size_t  n 
)

Definition at line 18 of file ft_strndup.c.

19 {
20  size_t s1_len;
21  char *p_ret;
22  char *p_ret_top;
23 
24  if (n <= 0)
25  return (ft_strdup(""));
26  s1_len = ft_minp(ft_strlen(s1), n);
27  p_ret = (char *)malloc(s1_len + 1);
28  p_ret_top = p_ret;
29  if (p_ret == NULL)
30  return (NULL);
31  while (s1_len-- > 0)
32  *p_ret++ = *s1++;
33  *p_ret = '\0';
34  return (p_ret_top);
35 }
size_t ft_minp(size_t a, size_t b)
Definition: ft_min.c:32
char * ft_strdup(const char *s1)
Definition: ft_strdup.c:16
size_t ft_strlen(const char *s)
Definition: ft_strlen.c:15
Here is the call graph for this function:
Here is the caller graph for this function: