-
Notifications
You must be signed in to change notification settings - Fork 1
/
builtins.c
59 lines (55 loc) · 1.16 KB
/
builtins.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
#include "shell.h"
/**
* find_builtins - finds builtin from the list of builtins based on user input
* @tokens: pointer to user input
* Return: 0 if found a builtin or else -1
**/
int find_builtins(char **tokens)
{
int i;
in_built list[] = {{"alias", whichAlias},
{"cd", chng_dr},
{"env", printEnv},
{"exit", ext},
{"help", hlp},
{"history", printHistory},
{"printenv", printEnv},
{"setenv", _setenv},
{"unsetenv", _unsetenv},
{NULL, NULL}
};
for (i = 0; list[i].s != NULL; i++)
{
if (_strcmp(tokens[0], list[i].s) == 0)
{
list[i].func(tokens);
return (0);
}
}
return (-1);
}
/**
* check_path - checks the PATH variable for finding the binary of given program
* @tokens: pointer to user input
* @p: pointer to memory
* Return: nothing
**/
void check_path(char **tokens, char *p)
{
char *path, **tokn, *concat;
path = _getenv("PATH");
tokn = parse_path(path, ':');
while (*tokn)
{
concat = _strcat(*tokn, tokens[0], '/');
if (concat)
{
if (execve(concat, tokens, NULL) != -1)
break;
}
tokn++;
}
_puts("No such command found\n");
_ref_mem(&p, "remove child");
_exit(1);
}