forked from Ana-Morales/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
121 lines (116 loc) · 2.61 KB
/
shell.c
File metadata and controls
121 lines (116 loc) · 2.61 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
#include "holberton.h"
/**
* main - main function for our shell
*
* Return: Always 0.
*/
int main(void)
{
int chars, count = 0, exit_status = 0;
char *args[100], *comm, *buffer = NULL;
size_t bufsize = 1024;
while (1)
{
if (isatty(STDIN_FILENO) == 1)
write(STDOUT_FILENO, "$ ", 2);
count++;
signal(SIGINT, SIG_IGN);
chars = getline(&buffer, &bufsize, stdin);
if (chars == 1)
continue;
if (chars != -1)
{
token_func(buffer, args);
if (args[0] == NULL || check_builtin(&args[0]) == 0)
{
continue;
}
if (_strcmp(args[0], "exit") == 0)
{
exit_func(&args[0], &exit_status);
continue;
}
comm = _which(args[0]);
fork_process(comm, args, count, &exit_status, buffer);
}
else
{
free(buffer);
exit(exit_status);
}
}
return (EXIT_SUCCESS);
}
/**
* token_func - function to call strtok function
* @buffer: buffer with string to be broke into tokens
* @args: array of strings where tokens will be stored
*
* Return: pointer to array with tokens
*/
void token_func(char *buffer, char **args)
{
int i = 0;
char *token;
token = strtok(buffer, " \n\t");
for (i = 0; token != NULL; i++)
{
args[i] = token;
token = strtok(NULL, " \n\t");
}
args[i] = NULL;
}
/**
* execute - executes the associated command
* @comm: command to be executed
* @args: array of argument strings passed to the command
* @env: array of strings passed as environment to command
* @count: counter for the number of executed commands
* @b: Pointer to buffer.
* Return: Always 0.
*/
int execute(char *comm, char *args[], char *env[], int count, char *b)
{
int len1 = 0;
(void)count;
if (execve(comm, args, env) == -1)
{
len1 = _strlen(program_invocation_name);
write(STDERR_FILENO, program_invocation_name, len1);
write(STDERR_FILENO, ": 1: ", 5);
write(STDERR_FILENO, args[0], _strlen(args[0]));
write(STDERR_FILENO, ": not found\n", 12);
free(b);
exit(127);
}
return (0);
}
/**
* fork_process - creates a child process
* @comm: command to be executed by the child process
* @args: array of argument strings passed to the command
* @count: counter for the number of executed commands
* @exit_stat: pointer to set the exit status of the children process
* @b: pointer to buffer
*/
void fork_process(char *comm, char *args[], int count, int *exit_stat, char *b)
{
int status;
pid_t child_pid;
child_pid = fork();
if (child_pid == -1)
{
perror("Error");
exit(1);
}
if (child_pid == 0)
execute(comm, args, NULL, count, b);
else
{
wait(&status);
if (_strcmp(comm, args[0]) != 0)
free(comm);
if (WIFEXITED(status))
*exit_stat = WEXITSTATUS(status);
}
}