summaryrefslogtreecommitdiffstats
path: root/src/p_args_next.c
blob: 8eb8e21c67aa686514d70f91ffb80993831d2a01 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   p_args_next.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#ifdef __linux__
# include <linux/limits.h>
#else
# include <limits.h>
#endif

#include "d_define.h"
#include "p_args.h"
#include "p_args_len.h"

static char		*p_give_me_an_arg(char tmp[],
								const char word[],
								const unsigned short i,
								const size_t start[])
{
	tmp[0] = '\0';
	ft_strlcpy(tmp,
		word + start[i],
		(p_arg_len(word, start[i]) - start[i]) + 1);
	return (tmp);
}

void			p_del_alloced_words(char *words[], unsigned short to_del)
{
	unsigned short	i;

	i = 0;
	while (i < to_del)
	{
		ft_memdel((void*)&words[i]);
		i++;
	}
}

unsigned short	p_dup_words(char *words[],
							const char word[],
							const unsigned short argc,
							const size_t start[])
{
	char			tmp[ARG_MAX];
	unsigned short	i;

	i = 0;
	while (i < argc)
	{
		if ((words[i] = ft_strdup(
			p_give_me_an_arg(tmp, word, i, start))) == NULL)
			return (i);
		i++;
	}
	return (i);
}