-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_functions.c
More file actions
167 lines (146 loc) · 3.25 KB
/
shell_functions.c
File metadata and controls
167 lines (146 loc) · 3.25 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
161
162
163
164
165
166
167
#include "shell.h"
/**
* get_input - Read a line of text from standard input.
* @cmd: The command data
*
* Return:
* - The number of characters read (including the newline character).
* - -1 if an error occurs or EOF.
*/
ssize_t get_input(UCommand *cmd)
{
ssize_t chars_read;
char *prompt = NULL;
size_t len;
/* Read a line of text from standard input using getline */
chars_read = getline(&prompt, &len, stdin);
if (chars_read == -1) /* Error condition or EOF */
{
if (feof(stdin))
{
free(prompt);
return (-1);
}
else
{
perror("Error: Unable to read command");
free(prompt);
exit(EXIT_FAILURE);
}
}
prompt[strcspn(prompt, "\n")] = '\0';
trim_string(prompt);
cmd->prompt = strdup(prompt);
free(prompt);
return (chars_read);
}
/**
* reallocArray - Reallocate an array of strings
* @oldArray: Old array
* @oldSize: Old size
* @newSize: New size
*
* Return: New array
*/
char **reallocArray(char **oldArray, size_t oldSize, size_t newSize)
{
size_t i;
char **newArray = (char **)malloc(newSize * sizeof(char *));
if (newArray == NULL)
{
perror("Error: Memory allocation failed");
exit(EXIT_FAILURE);
}
for (i = 0; i < oldSize && i < newSize; i++)
{
newArray[i] = oldArray[i];
}
free(oldArray);
return (newArray);
}
/**
* splitString - Split a string into an array of words
* @cmd: The command data
* @delimiters: Delimiters to split by
*
* Return: Array of words
*/
int splitString(UCommand *cmd, char *delimiters)
{
char *token;
int wordCount = 0;
char *inputCopy = strdup(cmd->prompt);
if (inputCopy == NULL || cmd->prompt == NULL)
{
perror("Error: Memory allocation failed");
exit(EXIT_FAILURE);
}
token = strtok(inputCopy, delimiters);
while (token != NULL && (strcmp(token, "#") != 0)) /* Handles comment */
{
wordCount++;
cmd->av = reallocArray(cmd->av, wordCount - 1, wordCount);
cmd->av[wordCount - 1] = strdup(token);
if (cmd->av[wordCount - 1] == NULL)
{
int i;
perror("Error: Memory allocation failed");
for (i = 0; i < wordCount - 1; i++)
{
free(cmd->av[i]);
}
free(cmd->av);
free(inputCopy);
return (-1);
}
token = strtok(NULL, delimiters);
}
cmd->av = reallocArray(cmd->av, wordCount, wordCount + 1);
cmd->av[wordCount] = NULL;
cmd->ac = wordCount;
free(inputCopy);
return (wordCount);
}
/**
* splitString2 - Split a string into an array of words
* @cmd: The command data
* @delimiters: Delimiters to split by
*
* Return: Array of words
*/
int splitString2(UCommand *cmd, char *delimiters)
{
char *input_cpy = NULL;
int i = 0;
char **copy_string = NULL;
if (strcmp(cmd->prompt, "") == 0 || is_only_spaces(cmd->prompt))
{
return (-1);
}
input_cpy = strdup(cmd->prompt);
copy_string = _my_strtok(input_cpy, delimiters);
cmd->av = remove_comments(copy_string);
i = string_array_len(cmd->av);
cmd->ac = string_array_len(copy_string);
free(input_cpy);
return (i);
}
/**
* free_cmd - Free an array of strings
* @cmd: The command data
*/
void free_cmd(UCommand *cmd)
{
if (cmd->shell_av != NULL)
free_string(cmd->shell_av);
if (cmd->av != NULL)
free_string(cmd->av);
if (cmd->prompt != NULL)
free(cmd->prompt);
if (cmd->path != NULL)
free(cmd->path);
cmd->is_path = 0;
cmd->av = NULL;
cmd->prompt = NULL;
cmd->path = NULL;
}