-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
128 lines (125 loc) · 2.13 KB
/
main.c
File metadata and controls
128 lines (125 loc) · 2.13 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
#include "main.h"
#include "global.h"
/**
* main - ........
* @ac:argc.
* @av:argv.
* @env:the enviroment.
* Return:0.
*/
int main(__attribute__((unused)) int ac, __attribute__((unused)) char **av,
char **env)
{
char *command = NULL, *path, **array_path, **concat_ = NULL;
size_t n = 100;
ssize_t read_size;
d.env = env;
path = _path(d.env);
array_path = _array_path(path);
signal(SIGINT, sigint_handler);
command = (char *)malloc(sizeof(char) * n);
if (command == NULL)
return (1);
while (1)
{
if (isatty(0))
write(1, "$ ", 2);
read_size = getline(&command, &n, stdin);
if (read_size == -1)
{
if (feof(stdin))
{
if (isatty(0))
write(1, "\n", 1);
break;
}
else
continue;
}
if (command[read_size - 1] == '\n')
command[read_size - 1] = '\0';
if (just_spaces(command))
continue;
d.path = path;
d.array_path = array_path;
d.command = command;
_exec(concat_, command);
}
free(command);
free(path);
free(array_path);
return (0);
}
/**
* _exec - ...........
* @concat_:array of string.
* @line:string.
* Return:void.
*/
void _exec(char **concat_, char *line)
{
int i = 0, arg_count = 0;
pid_t pid;
char *token, *command[32];
token = strtok(line, " ");
while (token != NULL && arg_count < 32)
{
command[arg_count++] = token;
token = strtok(NULL, " ");
}
command[arg_count] = NULL;
if (my_command(command))
return;
concat_ = concat_command(d.array_path, command[0]);
d.concated_array = concat_;
while (concat_[i])
{
if (access(concat_[i], X_OK) != 0)
{
i++;
continue;
}
pid = fork();
if (pid == 0)
{
if (execve(concat_[i], command, d.env) == -1)
exit(1);
}
else
{
wait(NULL);
d.error = 0;
free_2darr(concat_);
return;
}
}
perror("Command not found");
d.error = 2;
free_2darr(concat_);
}
/**
* just_spaces - ............
* @command:the command.
* Return:0 or 1.
*/
int just_spaces(char *command)
{
size_t i;
for (i = 0; i < strlen(command); i++)
{
if (command[i] != ' ')
{
return (0);
}
}
return (1);
}
/**
* __exit - .........
* @value:string.
* Return:void.
*/
void __exit(int value)
{
exit(value);
}