-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_shell.c
More file actions
64 lines (63 loc) · 1.19 KB
/
simple_shell.c
File metadata and controls
64 lines (63 loc) · 1.19 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
#include "shell.h"
/**
* main - simple shell instance
*
* Return: 0
*/
int main(void)
{
char *buffer = NULL, **args = NULL, *real_arg = NULL;
size_t size = 0;
struct stat st;
int r = 0, i = 1;
while (1)
{
write(STDOUT_FILENO, "shell$ ", 8);
buffer = getline_func(buffer, size);
if (buffer == NULL)
continue;
args = tokenizer(buffer);
if (args == NULL)
return (-1);
real_arg = args[0];
if (builtins(args, buffer) == -1)
{
if (stat(args[0], &st) == 0)
r = run_command(args);
else if (stat(args[0], &st) == -1)
{
args[0] = pathfinder(args[0]);
if (args[0] == NULL)
{
error_msg(real_arg, i);
i++;
}
else
r = run_command(args);
}
if (r == -1)
{
error_msg(real_arg, i);
i++;
}
free(args); args = NULL;
free(buffer); buffer = NULL;
}
}
return (0);
}
/**
* error_msg - Function that prints error messages
* @real_arg: user input command
* @i: Error iteration
* Return: void
*/
void error_msg(char *real_arg, int i)
{
write(STDOUT_FILENO, real_arg, _strlen(real_arg));
write(STDOUT_FILENO, ": ", 2);
_putchar(i + '0');
write(STDOUT_FILENO, ": ", 2);
write(STDOUT_FILENO, "Not found", 10);
_putchar('\n');
}