-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell.c
146 lines (129 loc) · 3.03 KB
/
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
#include "shell.h"
/**
* readIn - a function to reads the input string .
* Return: the inpute string.
*/
char *readIn()
{
char *strRead = NULL;
size_t num = 0;
ssize_t numCharsRead;
numCharsRead = getLineMod(&strRead, &num, STDIN_FILENO);
/*numCharsRead = getline(&strRead, &num, stdin);*/
if (numCharsRead == -1)
{
free(strRead);
return (NULL);
}
return (strRead);
}
/**
* checkEmpty - a function to check if the input is empty or not.
* @strRead: the input string we read.
* Return: 0 if not empty, 1 if empty.
*/
int checkEmpty(char *strRead)
{
int i;
if (strRead[0] == '\0' || _strcmp(strRead, "\n") == 0)
return (1);
for (i = 0; strRead[i]; i++)
if (strRead[i] != ' ' && strRead[i] != '\t' && strRead[i] != '\n')
return (0);
return (1);
}
/**
* exeFile - a function to execute the command we typed.
* @cmd: the command we typed.
* @av: It's commonly used to pass command-line arguments to the executed.
* @argv: cointans the file name.
* @numCount: count the number of errors in the shell command.
* Return: -1 if there is an error, executed if not error.
*/
int exeFile(char *cmd, char **av, char *argv, int numCount)
{
char **path_dir = NULL;
char *path_found = NULL, *prompt, *path = _getenv("PATH"); /*handling PATH */
int execute = checkExE(cmd), isPATH = 0, err = 0;
if (execute == 1)
{
err = createChild(cmd, av);
if (err == -1)
{
return (-1);
}
}
else if (isPATH == 0 && path != NULL)
{
path_dir = get_dirs(path);
/*returns the executable PATH, NULL otherwise */
path_found = check_path(cmd, path_dir);
if (path_found != NULL)
{
isPATH = 1; /*there is an executable in PATH */
err = createChild(path_found, av);
if (err == -1)
return (-1);
}
}
if (isPATH == 0 && execute <= 0)
{
prompt = (execute == 0 ? "permission denied\n" : "not found\n");
err = print_stderr(argv, numCount, av[0], prompt);
}
if (path)
free(path);
if (path_found)
free(path_found);
if (path_dir)
free_2d(path_dir);
return (err);
}
/**
* main - a main function.
* @argc: size of an args array.
* @argv: array of arguments.
* Return: stats if success.
*/
int main(notUsed int argc, char *argv[])
{
char *strRead = NULL, **strRead_cp, *wt = "x_x : ", **simiColon;
unsigned int numCount = 0, i;
int stat = 0, err = 0;
while (++numCount)
{
if (isatty(0))
write(STDOUT_FILENO, wt, 6);
strRead = readIn();
if (strRead == NULL) /*Exsit Imput or not */
exit(err);
if (checkEmpty(strRead))
{
free(strRead);
continue;
}
simiColon = getSemiColon(strRead);
free(strRead);
if (simiColon == NULL)
free_2d(simiColon), exit(1);
for (i = 0; simiColon[i] != NULL; i++)
{
strRead_cp = get_argv(simiColon[i]);
if (strRead_cp == NULL) /* if return NULL */
{
free_2d(strRead_cp);
freeExit(simiColon, 1);
}
if (strRead_cp[0] == NULL)
{
stat = 0, free_2d(strRead_cp);
continue;
}
stat = choseOrder(strRead_cp, argv, numCount, err, simiColon);
err = errno = stat;
free_2d(strRead_cp);
}
free_2d(simiColon);
}
return (err);
}