-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathshellcpu.c
86 lines (78 loc) · 1.47 KB
/
shellcpu.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
#include "simishell.h"
/**
* getfunc - a function which selects a function based on the commands
* @str: a command string pointer
*
* Return: returns 1 in success and -1 if it fails.
*/
int (*getfunc(char *str))(char **line)
{
int i = 0;
selecte selector[] = {
{"ls", lister},
{"l", lister},
{"ll", lister},
{"exit", exitor},
{"echo", echorr},
{"cd", changedire},
{"cat", cater},
{"pwd", pwder},
{"env", envir},
{"printenv", envir},
{"/bin/printenv", envir},
{"/bin/env", envir},
{"mkdir", maker},
{NULL, NULL}};
while (!strcomp(selector[i].command, str) && (selector[i].command != NULL))
{
i++;
}
if (selector[i].command == NULL)
{
return (NULL);
}
return (selector[i].funcptr);
}
/**
* shellprocessor - a function which excutes the given commands
* @line: a pointer to an array of strings
* @argv: a pointer to the array of arguments
*
* Return: returns 1 in success and -1 if it fails
*/
int shellprocessor(char **line, char **argv)
{
int check;
int i = 0;
if (!line)
{
return (1); }
if (getfunc(line[0]) == NULL)
{
i = builtincom(line);
if (i != 0)
{
for (i = 0; line[0][i] != '\0'; i++)
{
if (line[0][i] == '/')
{
check = 1;
break; }}
if (check == 1)
{
write(1, argv[0], strleng(argv[0]));
write(1, ": ", 3);
write(1, line[0], strleng(line[0]));
write(1, ": No such file or directory\n", 28);
exit(EXIT_FAILURE); }
else
{
write(1, line[0], strleng(line[0]));
write(1, ": not found\n", strleng(": not found\n"));
exit(EXIT_FAILURE); }}
}
else
{
getfunc(line[0])(line); }
return (1);
}