-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_strtok.c
More file actions
160 lines (139 loc) · 2.61 KB
/
_strtok.c
File metadata and controls
160 lines (139 loc) · 2.61 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "shell.h"
/**
* _strtok - Breaks a string into a sequence of zero or more nonempty tokens
* @str: The string to be parsed
* @delim: Set of bytes that delimit the tokens in the parsed string
*
* Return: Pointer to the next token,
* or NULL if there are no more tokens.
*/
char *_strtok(char *str, const char *delim)
{
char *tok_st = NULL, *tok_ed = NULL;
static char *saved_str;
if (str != NULL)
saved_str = str;
if (saved_str == NULL || *saved_str == '\0')
return (NULL);
tok_st = saved_str;
while (*tok_st != '\0' && _strchr(delim, *tok_st) != NULL)
tok_st++;
tok_ed = tok_st;
while (*tok_ed != '\0' && _strchr(delim, *tok_ed) == NULL)
tok_ed++;
if (*tok_ed != '\0')
{
*tok_ed = '\0';
saved_str = tok_ed + 1;
}
else
{
saved_str = NULL;
}
return (tok_st);
}
/**
* split_vector - parses input command to tokens
* @cmd: string to tokenize
* @dl: specify the delimiter to use
*
* Return: an array pointers to chracters
*/
char **split_vector(char *cmd, char *dl)
{
char *com_cpy = NULL, *tok, **command;
size_t count = 0, cnt = 0;
if (cmd)
{
com_cpy = _strdup(cmd);
tok = _strtok(cmd, dl);
while (tok)
{
cnt++;
tok = _strtok(NULL, dl);
}
cnt++;
command = malloc(sizeof(char *) * (cnt + 1));
tok = _strtok(com_cpy, dl);
while (tok)
{
command[count] = malloc(sizeof(char) * (_strlen(tok) + 1));
_strcpy(command[count], tok);
tok = _strtok(NULL, dl);
count++;
}
free(tok);
command[count] = NULL;
free(com_cpy);
com_cpy = NULL;
count = 0, cnt = 0;
return (command);
}
else
return (NULL);
}
/**
* free_arg- free's char **
* @agv: char **
* @colon: char **
*
* Return: void
*/
void free_arg(char **agv, char **colon)
{
int i, j;
for (i = 0; agv[i] != NULL; i++)
{
free(agv[i]);
}
free(agv);
for (j = 0; colon[j] != NULL; j++)
{
free(colon[j]);
}
free(colon);
}
/**
* _isdigit - Checks if variable is a digit.
* @c: The parameter to be checked.
*
* Return: 1 c is a number, 0 otherwise.
*/
int _isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
else
return (0);
}
/**
* _atoi - converts string to integer
* @nptr: string to be converted
*
* Return: void
*/
int _atoi(const char *nptr)
{
int result, sign, i, digit;
if (nptr == NULL)
return (0);
result = 0;
sign = 1;
i = 0;
while (_isspace(nptr[i]))
i++;
if (nptr[i] == '-' || nptr[i] == '+')
{
sign = (nptr[i] == '-') ? -1 : 1;
i++;
}
while (_isdigit(nptr[i]))
{
digit = nptr[i] - '0';
if (result > (INT_MAX - digit) / 10)
return (sign == 1 ? INT_MAX : INT_MIN);
result = result * 10 + digit;
i++;
}
return (sign * result);
}