-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_functions.c
145 lines (120 loc) · 2.45 KB
/
execute_functions.c
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
#include "shell.h"
/**
* print_prompt - Print the prompt to the user
*/
void print_prompt(void)
{
if (isatty(STDIN_FILENO))
write(STDOUT_FILENO, "MyShell$ ", 9);
}
/**
* read_input - Reads the input from the users
*
* Return: Character variable to the program
*/
char *read_input(void)
{
char *input_buffer;
size_t buf_size;
ssize_t nread;
input_buffer = NULL;
buf_size = 0;
nread = getline(&input_buffer, &buf_size, stdin);
if (nread == -1)
{
free(input_buffer);
exit(0);
}
input_buffer = handle_comment(input_buffer);
return (input_buffer);
}
/**
* execute_command - Executes the input from the buffer
* @input: The argument from the buffer
* @argv: Array of argument
* @env: Environment variables
*/
void execute_command(char *input, char *argv[], char **env)
{
char *args[10];
char *path, *shell_name;
int status, num_args;
pid_t child_pid;
shell_name = argv[0];
num_args = tokenize_input(input, args);
if (num_args == 0)
return;
if (handle_builtin_commands(args, num_args, input, env) == 1)
return;
path = get_file_path(args[0]);
child_pid = fork();
if (child_pid == -1)
{
perror("Error: Failed to create");
free(input);
exit(1);
}
if (child_pid == 0)
{
if (execve(path, args, NULL) == -1)
{
write(2, shell_name, strlen(shell_name));
write(2, ": 1: ", 5);
write(2, args[0], strlen(args[0]));
write(2, ": not found\n", 12);
exit(127);
}
}
else
wait(&status);
free(path);
}
/**
* tokenize_input - Tokenizes the input strings
* @input: Argument input
* @args: The array of strings
*
* Return: Number of the items tokenized
*/
int tokenize_input(char *input, char *args[])
{
int count;
char *token;
count = 0;
token = strtok(input, " \n");
while (token)
{
args[count] = token;
token = strtok(NULL, " \n");
count++;
}
args[count] = NULL;
return (count);
}
/**
* handle_builtin_commands - Handle all the built in commands
* @args: Arguments to the built in commands
* @num_args: Number of argument
* @input: The input command
* @env: The environment variables
*
* Return: 1 if successful 0, if unsuccessful
*/
int handle_builtin_commands(char **args, int num_args, char *input, char **env)
{
if (strcmp(args[0], "exit") == 0)
{
return (shell_exit(args, input));
}
else if (strcmp(args[0], "cd") == 0)
{
handle_cd(args, num_args);
return (1);
}
else if (strcmp(args[0], "env") == 0)
{
print_env(env);
return (1);
}
return (0);
}