-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltins.c
75 lines (71 loc) · 1.26 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "shell.h"
/**
* exitme - function to exit
* @command: input from cmd
*
* Description: exit from shell
* Return: 0 for success
*/
int exitme(char **command)
{
if (*command)
exit(1);
return (0);
}
/**
* cd - function to change directory
* @command: input from cmd
*
* Description: change directory
* Return: 0 for success
*/
int cd(char **command)
{
chdir(command[1]);
return (0);
}
/**
* printenv - function with one argument
* @command: pointer to cmd
*
* Description: print env
* Return: 0 for success
*/
int printenv(char **command)
{
int i;
if (*command)
{
i = 0;
while (environ[i])
{
write(1, environ[i], _strlen(environ[i]));
write(1, "\n", 1);
i++;
}
}
return (0);
}
/**
* checkBuiltins - function with two arguments
* @combine: full dir
* @command: cmd input
*
* Description: check for builtins and call function
* Return: path to builtin or process from dir
*/
int checkBuiltins(char *combine, char **command)
{
int i;
char *array[] = {"exit", "cd", "env", NULL};
typedef int (*Builtins)(char **);
Builtins functions[] = {&exitme, &cd, &printenv};
i = 0;
while (array[i] != NULL)
{
if (_strcmp(array[i], command[0]) == 0)
return (functions[i](command));
i++;
}
return (execute(combine, command));
}