-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
71 lines (58 loc) · 1.09 KB
/
shell.c
File metadata and controls
71 lines (58 loc) · 1.09 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
#include "main.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stddef.h>
#include <string.h>
/**
* main - Entry point for a custom shell program.
*
* Description: This function implements a simple shell
* that reads user input,
* parses commands, and executes them.
* It provides a command-line interface with
* basic functionality, including command execution
* and the "exit" command to
* exit the shell.
* @argc: input
* @argv: input
* Return: Always returns 0.
*/
int main(int argc, char *argv[])
{
char *cmds[MAX_COMMANDS];
int f_pipe = 0;
size_t bs = 0;
int i;
int n_cmds;
char *input = NULL;
int exit_status = 0;
(void)argc;
while (1 && !f_pipe)
{
if (!isatty(STDIN_FILENO))
f_pipe = 1;
read_input(&input, &bs);
parse_input(input, cmds, &n_cmds);
if (n_cmds > 0)
{
if (strcmp(cmds[0], "exit") == 0)
{
if (n_cmds > 1)
{
exit_status = _atoi(cmds[1]);
}
free(input);
exit(exit_status);
}
exec_c(cmds[0], cmds, argv[0]);
for (i = 0; i < n_cmds; i++)
{
free(cmds[i]);
}
}
free(input);
input = NULL;
}
return (0);
}