-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
171 lines (138 loc) · 2.97 KB
/
main.c
File metadata and controls
171 lines (138 loc) · 2.97 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
#include "main.h"
/**
* handle_SIGINT - ctrl-C singnal
* @sig: Signal number
* Return: void
*/
void handle_SIGINT(int sig)
{
(void)sig;
_puts("\n");
_puts(PROMPT);
}
/**
* main - Parent Process of shell
* @ac: shell argument count
* @av: shell arguments
*
* Return: 0 (exit success)
*/
int main(int ac, char **av)
{
int ret;
CommandInfo cmd_info = {NULL, NULL, NULL, NULL, NULL, 0, 0};
(void) ac;
cmd_info.av = av;
if (set_data(&cmd_info))
{
free_env(&cmd_info);
return (get_error(&cmd_info, -1));
}
signal(SIGINT, handle_SIGINT);
do {
++(cmd_info.count);
if (readline(&cmd_info) && isatty(STDIN_FILENO))
continue;
tokenizer(&cmd_info);
if (!cmd_info.args)
break;
ret = run_args(&cmd_info);
if (ret && ret != EXIT_LOOP)
{
cmd_info.status = get_error(&cmd_info, ret);
if (isatty(STDIN_FILENO))
{
clean_up(&cmd_info);
continue;
}
else
break;
}
clean_up(&cmd_info);
} while (ret != EXIT_LOOP);
clean_up(&cmd_info);
free_env(&cmd_info);
return (cmd_info.status);
}
/**
* readline - reads from tty stdin STREAM to a buffer
* @cmd_info: data structure of shell
* getline: man 3 getline
* Return: pointer to buffer
*/
int readline(CommandInfo *cmd_info)
{
char *input = NULL;
size_t bufsize = BUF_SIZE;
int chars_count;
if (isatty(STDIN_FILENO))
_puts(PROMPT);
chars_count = getline(&input, &bufsize, stdin);
if (chars_count == -1 || is_whitespace(input))
{
free(input);
return (1);
}
if (chars_count > 0 && input[chars_count - 1] == '\n')
input[chars_count - 1] = '\0';
cmd_info->line = input;
return (0);
}
/**
* tokenizer - tokenizes a string into words (man 3 strtok)
* @cmd_info: shell cmd_info structure
*
* Return: a memory address with pointrs to each word
*/
int tokenizer(CommandInfo *cmd_info)
{
char *token, *delim = " \n\t\r\a\v";
char **tokens = NULL;
size_t idx = 0;
if (!cmd_info->line || cmd_info->line[0] == '\0')
return (1);
token = _strtok(cmd_info->line, delim);
while (token)
{
tokens = _reallocdp(tokens, idx, idx + 1);
if (!tokens)
return (1);
tokens[idx] = _strdup(token);
if (!tokens[idx])
return (1);
idx++;
token = _strtok(NULL, delim);
}
tokens = _reallocdp(tokens, idx, idx + 1);
if (!tokens)
return (1);
tokens[idx] = NULL;
cmd_info->args = tokens;
return (0);
}
/**
* execute - execute absolute path if execve
* @cmd_info: shell data structure
*
* Return: exit status of the executed command
*/
int execute(CommandInfo *cmd_info)
{
pid_t child_pid;
int status;
if (access(cmd_info->command, F_OK) != 0 || is_directory(cmd_info->command))
return (127); /* File exists */
if (access(cmd_info->command, X_OK))
return (126); /* permission denied */
child_pid = fork();
if (child_pid == 0)
{
if (execve(cmd_info->command, cmd_info->args, cmd_info->env) == -1)
return (127); /* Not found */
}
else
do {
waitpid(child_pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
return (0);
}