summaryrefslogtreecommitdiff
path: root/libft/ft_substr.c
blob: 283db6ee755976185027d80400392df1ab356ebd (plain)
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_substr.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: kdx    <kdx   @student.42angouleme.fr      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/09/28 06:35:57 by kdx               #+#    #+#             */
/*   Updated: 2022/10/14 02:37:58 by kdx              ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"

char	*ft_substr(char const *s, unsigned int start, size_t len)
{
	ssize_t	size;
	char	*ptr;

	if (s == NULL)
		return (NULL);
	size = ft_strlen(s);
	size -= start;
	if (size < 0)
		size = 0;
	if ((size_t)size > len)
		size = len;
	ptr = malloc(size + 1);
	if (ptr == NULL)
		return (NULL);
	ptr[size] = '\0';
	while (size > 0)
	{
		size -= 1;
		ptr[size] = s[start + size];
	}
	return (ptr);
}