-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecute.c
More file actions
61 lines (60 loc) · 848 Bytes
/
Copy pathexecute.c
File metadata and controls
61 lines (60 loc) · 848 Bytes
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
#include "main.h"
char *builtin_str[] = {
"cd",
"exit",
};
int (*builtinfunc[])(char **) = {
&_cd,
&our_exit,
};
int _numbuiltins()
{
return sizeof(builtin_str) / sizeof(char *);
}
/**
* _cd - change directory
* @argv: array of tokens
* Return: integer
*/
int _cd(char **argv)
{
if (argv[1] == NULL)
{
fprintf(stderr, "cd: expected argument to \"cd\"\n");
}
else
{
if (chdir(argv[1]) != 0)
{
perror("cd");
}
}
return 1;
}
int our_exit(char **argv)
{
(void)argv;
return (0);
}
/**
* _execute - execute builtin commands
* @argv: array of tokens
* Return: an int
*/
int _execute(char **argv)
{
int i;
if (argv[0] == NULL)
{
/* empty command */
return 1;
}
for (i = 0; i < _numbuiltins(); i++)
{
if (_strcmp(argv[0], builtin_str[i]) == 0)
{
return (*builtinfunc[i])(argv);
}
}
return exe_cmd();
}