-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
46 lines (41 loc) · 1.44 KB
/
Copy pathft_substr.c
File metadata and controls
46 lines (41 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsargsya <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/09 16:00:23 by tsargsya #+# #+# */
/* Updated: 2024/12/12 13:52:55 by tsargsya ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static char *ft_return_empty_str(void)
{
char *res;
res = malloc(1);
if (!res)
return (NULL);
res[0] = '\0';
return (res);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *res;
size_t s_len;
size_t substr_len;
if (!s)
return (NULL);
s_len = ft_strlen(s);
if (start >= s_len)
return (ft_return_empty_str());
substr_len = s_len - start;
if (substr_len > len)
substr_len = len;
res = malloc(substr_len + 1);
if (!res)
return (NULL);
ft_strlcpy(res, s + start, substr_len + 1);
return (res);
}