-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
135 lines (123 loc) · 2.69 KB
/
main.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
#include <stdio.h>
#include "shell.h"
/**
* main - The shell entry.
*
* Return: Always 0.
*/
int main(void)
{
ssize_t response;
if (isatty(STDIN_FILENO))
{
while (5)
{
entrance();
response = get_exec_command();
/* If user types exit or EXIT break the loop*/
if (response == 99)
{
exit (EXIT_SUCCESS);
}
if (response == 90)
continue;
}
}
else
{
response = get_exec_command();
if (response == 99)
{
exit(EXIT_SUCCESS);
}
}
return (0);
}
/**
* entrance - Prints the shell prompt
*/
void entrance(void)
{
write(STDOUT_FILENO, "#cisfun$ ", strlen("#cisfun$ "));
}
/**
* get_exec_command - A function that accept and execute requests from cli
*
* Return: 0 or 99 for success.
*/
ssize_t get_exec_command(void)
{
char *mesg = NULL, *token, *mesg_copy, **argv, *delim = " ";
int i, val, response, num_token = 0;
size_t n = 0;
val = getline(&mesg, &n, stdin); /*Get user input*/
if (val == -1)
{
if (feof(stdin))
write(STDOUT_FILENO, "\n", 1), exit(EXIT_SUCCESS);
else
perror("getline"), exit(EXIT_FAILURE);
}
mesg_copy = strdup(mesg); /* FREE ME LATER !!! */
if (mesg_copy == NULL)
perror("strdup"), exit(EXIT_FAILURE);
mesg_copy[strcspn(mesg_copy, "\n")] = '\0';
if (*mesg_copy == '\0')
return (90);
token = strtok(mesg_copy, delim);
while (token)
num_token++, token = strtok(NULL, delim);
argv = malloc(sizeof(char *) * (num_token + 1)); /*Null terminate*/
if (argv == NULL)
perror("malloc"), exit(EXIT_FAILURE);
token = strtok(mesg_copy, delim); /*Write each token into argv*/
for (i = 0; token; i++)
{
argv[i] = strdup(token);
if (argv[i] == NULL)
break;
token = strtok(NULL, delim);
}
argv[i] = NULL, response = execute_command(argv); /* Execute argv*/
for (i = 0; i < num_token; i++)
free(argv[i]);
free(argv), free(mesg), free(mesg_copy);
return (response);
}
/**
* execute_command - A function that execute the command line requests.
* @command: The request made by the user.
*
* Return: 0 on success and 99 if the user types exit or EXIT.
*/
int execute_command(char **command)
{
char *first_command;
pid_t val;
/* If user type exit or EXIT return 99*/
if ((strcmp(command[0], "exit") == 0) || (strcmp(command[0], "EXIT") == 0))
return (99);
if (*command == NULL)
write(STDOUT_FILENO, "\n", 1);
val = fork();
if (val == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
if (val == 0)
{
/*Check the path and then execute child process*/
first_command = pathfinder(command[0]);
execve(first_command, command, environ);
/*If command is not executed due to error*/
perror("execve");
exit(EXIT_FAILURE);
}
else
{
/* Hold on with parent process*/
wait(NULL);
}
return (0);
}