-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
52 lines (50 loc) · 1.31 KB
/
main.c
File metadata and controls
52 lines (50 loc) · 1.31 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
#include "main.h"
/**
* main - entry point
* @argc: number of arguments
* @argv: array of string
* @env: to get enviroment
* Return: alwayd int
*/
int main(int argc, char *argv[], char *env[])
{
int pipe = 1, err_count = 1, no_exc = 1, status = 0;
const char *del = " ";
size_t n_buffer = 0;
char *buffer = NULL, command[50], *args[20], *only_command;
signal(SIGINT, handle_sigint);
/* Initialize PWD environment variable with current directory */
update_pwd_env();
if (argc != 1)
_printf("%s: 0: Can't open %s\n", argv[0], argv[1]), exit(1);
if (isatty(STDIN_FILENO) == 0)
non_interactive(argc, argv, env, &pipe);
while (pipe)
{
no_exc = 1;
print_prompt();
fflush(stdout);
handle_input_command(&buffer, &n_buffer, &no_exc,
&only_command, status, argc, argv, &err_count);
if (buffer && *buffer && no_exc)
{
/* Check for cd command first */
if (_strcmp(only_command, "cd") == 0)
{
change_dir(buffer, command, argc, argv, &err_count);
update_pwd_env();
}
else
{
tok_buf(buffer, args, del, command, env);
if (access(command, X_OK) == 0)
_fork(argc, argv, buffer, args, only_command, &status);
else
fprintf(stderr, "%s: %d: %s: not found\n",
argv[argc - 1], err_count++, only_command), fflush(stdout);
}
}
free(only_command);
}
return (0);
}