-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.c
More file actions
223 lines (187 loc) · 3.92 KB
/
func.c
File metadata and controls
223 lines (187 loc) · 3.92 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
/**
* exec_c - Execute a command with arguments.
*
* @command: The command to execute.
* @arguments: An array of arguments for the command.
* @argv: The name of the program executing the command.
*/
void exec_c(const char *command, char *const arguments[], const char *argv)
{
int status;
pid_t pid = fork();
if (pid == -1)
{
perror(argv);
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
if (strchr(command, '/') != NULL)
execvp(command, arguments);
else
{
char *path = getenv("PATH");
if (path != NULL)
{
char *path_copy = strdup(path);
char *token = strtok(path_copy, ":");
char command_path[100];
while (token != NULL)
{
snprintf(command_path, sizeof(command_path), "%s/%s", token, command);
execvp(command_path, arguments);
token = strtok(NULL, ":");
}
free(path_copy);
}
fprintf(stderr, "%s: command not found\n", argv);
exit(EXIT_FAILURE);
}
}
else
{
waitpid(pid, &status, 0);
}
}
/**
* read_input - Read a line of input from the user
* @buffer: Pointer to store the input string
* @n: Pointer to store the size of the input buffer
*/
void read_input(char **buffer, size_t *n)
{
char prompt[] = " :)> ";
char tempBuffer[256];
size_t len;
if (buffer == NULL || n == NULL)
return;
if (isatty(STDIN_FILENO) == 1)
write(STDOUT_FILENO, prompt, strlen(prompt));
if (fgets(tempBuffer, sizeof(tempBuffer), stdin) == NULL)
{
if (feof(stdin))
{
printf("\n");
exit(EXIT_SUCCESS);
}
else
{
perror("(fgets)");
exit(EXIT_FAILURE);
}
}
len = strlen(tempBuffer);
if (len > 0 && tempBuffer[len - 1] == '\n')
tempBuffer[len - 1] = '\0';
*n = len + 1;
*buffer = (char *)malloc(*n);
if (*buffer == NULL)
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
strcpy(*buffer, tempBuffer);
}
/**
* get_command - Splits an ipt an array of tokens based on whitespace.
* @input: The input string to split.
*
* Return: A dynamically allocated array of token strings, or NULL on failure.
*/
char **get_command(char *input)
{
int i = 0;
char *token;
char **command_args = (char **)malloc((MAX_COMMANDS + 1) * sizeof(char *));
if (command_args == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
token = strtok(input, " \t\n");
while (token != NULL && i < MAX_COMMANDS)
{
command_args[i] = (char *)malloc(strlen(token) + 1);
if (command_args[i] == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
strcpy(command_args[i], token);
token = strtok(NULL, " \t\n");
i++;
}
command_args[i] = NULL;
return (command_args);
}
/**
* parse_input - Tokenizes an input string into commands.
*
* @input: Input string to tokenize.
* @commands: Array to store tokenized commands.
* @num_commands: Pointer to the number of commands.
*/
void parse_input(const char *input, char **commands, int *num_commands)
{
char *token;
int count = 0;
char *input_copy = strdup(input);
if (input_copy == NULL)
{
*num_commands = -1;
return;
}
token = strtok(input_copy, " \t\n");
while (token != NULL)
{
commands[count] = strdup(token);
if (commands[count] == NULL)
{
*num_commands = -1;
free(input_copy);
return;
}
token = strtok(NULL, " \t\n");
count++;
}
commands[count] = NULL;
free(input_copy);
*num_commands = count;
}
/**
* _atoi - converts a string to an integer.
* @s: input string.
* Return: integer.
*/
int _atoi(char *s)
{
unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i;
while (*(s + count) != '\0')
{
if (size > 0 && (*(s + count) < '0' || *(s + count) > '9'))
break;
if (*(s + count) == '-')
pn *= -1;
if ((*(s + count) >= '0') && (*(s + count) <= '9'))
{
if (size > 0)
m *= 10;
size++;
}
count++;
}
for (i = count - size; i < count; i++)
{
oi = oi + ((*(s + i) - 48) * m);
m /= 10;
}
return (oi * pn);
}