-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
181 lines (172 loc) · 4 KB
/
shell.c
File metadata and controls
181 lines (172 loc) · 4 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* This file contains the core shell functionality including the main loop
* and command execution logic for both interactive and non-interactive modes
*/
#include "main.h"
/**
* main - entry point for the shell program
* Initializes shell environment and chooses appropriate execution mode
* Handles both interactive (terminal) and non-interactive (pipe/file) modes
*
* @argc: argument count
* @argv: argument vector
* @env: environment variables array
* Return: 0 on Success, -1 on failure
*/
int main(int argc, char **argv, char **env)
{
path_l *path;
size_t j;
struct stat st;
(void)(argc);
j = where_null(env);
path = build_path();
if (isatty(0))
interactive(argv, j, st, path);
else
none_interactive(argv, j, st, path);
return (0);
}
/**
* none_interactive - handles shell execution in non-interactive mode
* Used when input comes from a pipe or file rather than terminal
* Processes commands until EOF without displaying prompts
*
* @argv: program arguments
* @j: environment null pointer index
* @st: file status struct
* @path: linked list of PATH directories
*/
void none_interactive(char **argv, size_t j, struct stat st, path_l *path)
{
char **args, *com;
size_t com_size, i;
ssize_t c_s;
int w;
for (com = NULL, i = 1;; free_strings(args), i++)
{
com_size = 0;
c_s = getline(&com, &com_size, stdin);
if (c_s == -1)
{
free_list(path);
free(com);
exit(0);
}
com[c_s - 1] = '\0';
set_null(com);
args = strtoargs(com);
free(com);
if (!args)
continue;
if (stat(args[0], &st) == 0)
execute(args, &w);
else if (_strcmp(args[0], "env") != 0 &&
find_in_path(args, path) == 0)
execute(args, &w);
else if (does_it_Builtin(argv, args, i, path, j) == -1)
not_found(argv, args, i);
}
}
/**
* interactive - handles shell execution in interactive mode
* Displays prompt, reads user input, and executes commands
* Provides command-line interface when shell runs in terminal
*
* @argv: program arguments
* @j: environment null pointer index
* @st: file status struct
* @path: linked list of PATH directories
*/
void interactive(char **argv, size_t j, struct stat st, path_l *path)
{
char **args, *com;
size_t com_size, i;
ssize_t c_s;
int w;
for (com = NULL, i = 1;; free_strings(args), i++)
{
com_size = 0;
write(1, "($) ", 5);
c_s = getline(&com, &com_size, stdin);
if (c_s == -1)
{
free_list(path);
free(com);
write(1, "\n", 2);
exit(0);
}
com[c_s - 1] = '\0';
set_null(com);
args = strtoargs(com);
free(com);
if (!args)
continue;
if (stat(args[0], &st) == 0)
execute(args, &w);
else if (_strcmp(args[0], "env") != 0 &&
find_in_path(args, path) == 0)
execute(args, &w);
else if (does_it_Builtin(argv, args, i, path, j) == -1)
not_found(argv, args, i);
}
}
/**
* execute - creates child process and executes command
* Forks process and uses execve to run commands
* Parent process waits for child completion
*
* @as: array of command and arguments
* @w: pointer to store wait status
*/
void execute(char **as, int *w)
{
if (fork() == 0)
{
if (execve(as[0], as, environ) == -1)
perror("execve\n");
}
else
wait(w);
}
/**
* does_it_Builtin - checks if command is a builtin and executes if so
* Handles built-in commands: exit, env, setenv, unsetenv, cd
* Returns success/failure to indicate if command was handled
*
* @av: program arguments
* @as: command arguments
* @i: process ID
* @path: PATH linked list
* @j: environment null pointer index
* Return: 0 if builtin executed, -1 if not a builtin
*/
int does_it_Builtin(char **av, char **as, size_t i, path_l *path, int j)
{
if (_strcmp(as[0], "exit") == 0)
{
ex(av, as, i, path, j);
return (0);
}
else if (_strcmp(as[0], "env") == 0)
{
_env(environ);
return (0);
}
else if (_strcmp(as[0], "setenv") == 0)
{
_setenv(as[1], as[2], 1);
return (0);
}
else if (_strcmp(as[0], "unsetenv") == 0)
{
_unsetenv(as[1]);
return (0);
}
else if (_strcmp(as[0], "cd") == 0)
{
_cd(av, as, i);
return (0);
}
return (-1);
}