Repository navigation
Expand file tree
/
Copy pathtokenizers.c
More file actions
107 lines (105 loc) · 3.21 KB
/
Copy pathtokenizers.c
File metadata and controls
107 lines (105 loc) · 3.21 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
#include "shell.h"
/**
* hsh_tokenizer - Split the input string into a array of arguments (tokens)
* @input: String from input (main function)
* Return: Pointer to the array of tokens
*
* Overall, this function provides a useful way
* to tokenize a string into an array of tokens,
* which can be used for further processing in a shell program
*
* At the start char**tokens initializes variables for the buffer size (buffer)
* a new buffersize in case a reallocation is needed (newBuffer), the current
* position in the array of tokens (position)
*/
char **hsh_tokenizer(char *input)
{
char **tokens = NULL, *token = NULL;
int buffer = BUFSIZE, newBuffer = 0, position = 0;
char **backup_tokens = NULL;
/*allocate memory for the array of tokens */
tokens = malloc(buffer * sizeof(char *));
if (tokens == NULL)
{
fprintf(stderr, "memory allocation error\n");
exit(EXIT_FAILURE);
}
/* tokenize the input string, checking for delimiter to split*/
/*the string and replace them with \0 */
token = strtok(input, DELIM);
while (token != NULL)
{
/*the array of tokens and the position variable is incremeneted*/
tokens[position] = token;
position++;
/*if position variable is greater than or qual to buffer size, reallocate*/
/*memory for the array of tokens*/
if (position >= buffer)
{
newBuffer = BUFSIZE * 2;
backup_tokens = tokens;
tokens = _realloc(tokens, buffer, newBuffer * sizeof(char *));
/*if fail,frees backup array of tokens,frees current token,& prints error*/
if (tokens == NULL)
{
free(backup_tokens);
free(tokens);
fprintf(stderr, "memory allocation error\n");
exit(EXIT_FAILURE);
}
}
/*once memory reallocation, continue processing the input string */
token = strtok(NULL, DELIM);
}
/*null terminate the array of tokens */
tokens[position] = NULL;
return (tokens);
}
/**
* tokenizer_path - Split the environment variable PATH into an array of tokens
* @input: Pointer to environment variable PATH
* Return: Pointer to the array of tokens
*/
char **tokenizer_path(char *input)
{
int buffer = BUFSIZE, newBuffer = 0, position = 0;
char **tokens = NULL, *token = NULL;
char **backup_tokens = NULL;
/*allocate memory for the array of tokens */
tokens = malloc(buffer * sizeof(char *));
if (tokens == NULL)
{
fprintf(stderr, "memory allocation error\n");
exit(EXIT_FAILURE);
}
/*tokenize the PATH string, checking for deliminter :and replace it*/
/*with \0 and stores resulting tokens i nthe tokens array*/
token = strtok(input, ":");
while (token != NULL)
{
tokens[position] = token;
position++;
/*reallocate the array of tokens if necessary*/
if (position >= buffer)
{
/*doubles the buffer size*/
newBuffer = BUFSIZE * 2;
backup_tokens = tokens;
tokens = _realloc(tokens, buffer, newBuffer * sizeof(char *));
if (tokens == NULL)
{
free(backup_tokens);
free(tokens);
/*if error during realloc print error */
fprintf(stderr, "memory allocation error\n");
exit(EXIT_FAILURE);
}
}
/*once memory reallocation, continue processing the PATH string */
token = strtok(NULL, ":");
}
/*null terminate the array of tokens and*/
/*returns a pointer to the tokens array */
tokens[position] = NULL;
return (tokens);
}