-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_shell.c
162 lines (145 loc) · 3 KB
/
simple_shell.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "shell.h"
#include <signal.h>
/**
* sigintHandler - handles ctrl c
* @cow: an int of the signal
*
* Description: this is the helper function for the siugnal
* Return: none it is void
*/
void sigintHandler(int cow)
{
(void) cow;
signal(SIGINT, sigintHandler);
write(1, "\n", 1);
write(STDOUT_FILENO, "$ ", 2);
fflush(stdout);
}
/**
* main - super simple shell
* @ac: the argument count that we have
* @av: the array of strings that are our CL arguments
* Return: 0 upon success
*/
int main(int ac, char **av)
{
int status, i, is_on;
int counter = 1;
int exit_stat = 0;
size_t too_big = 0;
char *buf = NULL;
char *path = _getenv("PATH");
char *to_string, *full_command = NULL;
char **argv = NULL;
pid_t child_pid;
size_t buf_size = 0;
list_t *head = NULL;
/* build linked list of PATH directories */
build_linked_list(path, &head);
is_on = 1;
to_string = malloc(sizeof(char) * 17);
(void) ac;
while (is_on)
{
signal(SIGINT, sigintHandler);
/* only prints prompt if interactive mode */
if (isatty(0) == 1)
write(STDOUT_FILENO, "$ ", 2);
/* prompt user for command and handles EOF */
if (getline(&buf, &buf_size, stdin) == EOF)
{
/* doesn't print newline if non-interactive mode */
if (isatty(0) == 1)
write(STDOUT_FILENO, "\n", 1);
break;
}
/* remove newline from string so program can execute*/
i = _strlen(buf);
/* preserves single characters i.e. '\n' */
if (i > 1)
{
buf[i - 1] = '\0';
}
/* create argument vector of CL arguments*/
argv = split_string(buf);
/* functionality for spaces */
if (!argv)
{
counter++;
continue;
}
child_pid = fork();
if (child_pid == -1)
{
perror("Error:");
exit(1);
}
if (child_pid == 0)
{
execve(argv[0], argv, NULL);
/* if doesn't execute: */
if (_strcmp(argv[0], "exit") == 0)
{
free(argv);
break;
}
if (_strcmp(argv[0], "env") == 0)
{
print_env();
free(argv);
break;
}
else if (argv[0][0] != '/')
{
counter_to_string(counter, to_string);
full_command = search_path(head,
argv[0], av, to_string);
if (full_command)
execve(full_command, argv, NULL);
}
if (argv[0][0] != '\n' && full_command == NULL)
{
counter_to_string(counter, to_string);
error_helper(
&av[0], &argv[0], to_string);
free(to_string);
exit_stat = 127;
free(argv);
}
exit(exit_stat);
}
else
{
wait(&status);
if (WIFEXITED(status))
exit_stat = WEXITSTATUS(status);
if (_strcmp(argv[0], "exit") == 0)
{
is_on = 0;
if (argv[1])
{
counter_to_string(counter, to_string);
too_big = string_to_int(argv[1]);
if (too_big > 2147483647
|| _strlen(argv[1]) > 10)
{
exit_helper(&av[0],
&argv[0], to_string);
is_on = 1;
exit_stat = 2;
continue;
}
exit(string_to_int(argv[1]));
}
}
if (full_command)
free(full_command);
}
counter++;
free(argv);
}
free_list(head);
free(buf);
free(to_string);
return (exit_stat);
}