-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpy_str_to_av.c
85 lines (78 loc) · 1.29 KB
/
cpy_str_to_av.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "main.h"
int is_end(int c);
/**
* strtoav - copy words from string into argument vector
* @str: the source to copy from
* @av: the array of string to copy each word into
*
* Return: the end of command (0 if the end of file)
*/
char strtoav(char *str, char **av)
{
int comment = 0, n = 0, w, wi, c, len;
static int i = 1;
if (i == 1 && str[i])
i = 0;
c = !(is_end(str[i]));
len = RD_BUF;
while (i < len && c)
{
if (str[i] == '#')
comment = 1;
if (str[i] == ' ' || comment)
i++;
else
{
w = i;
c = !(is_end(str[i]));
while (i < len && str[i] != ' ' && c)
{
i++;
c = !(is_end(str[i]));
}
av[n] = malloc((1 + i - w) * sizeof(char));
wi = 0;
while (w < i)
{
av[n][wi] = str[w];
w++;
wi++;
} av[n][wi] = '\0';
n++;
}
c = !(is_end(str[i]));
}
av[n] = NULL;
c = (str[i] == '\0');
if (c)
i = 0;
else
i++;
return (i);
}
/**
* free_av - frees memory allocated by strtoav
* @av: same argument vector used in strtoav
*/
void free_av(char **av)
{
while (*av)
{
free(*av);
*av = NULL;
av++;
}
}
/**
* is_end - checks if a char is the end of command
* @c: the character to check
*
* Return: 1 if c is end character
*/
int is_end(int c)
{
if (c == 0 || c == 10 || c == 59)
return (1);
else
return (0);
}