-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltins.c
More file actions
57 lines (56 loc) · 997 Bytes
/
builtins.c
File metadata and controls
57 lines (56 loc) · 997 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
#include "shell.h"
/**
*builtins - command is built-in ?
*@input: the command
*@buff: string of the input
*@exitv: how to exit ?
*Return: 1 if yes 0 if not a built-in
*/
int builtins(char **input, char *buff, int exitv)
{
int i;
if (_strcmp(input[0], "env") == 0)
{
_environ();
for (i = 0; input[i]; i++)
free(input[i]);
free(input);
free(buff);
return (1);
}
else if (_strcmp(input[0], "exit") == 0)
{
for (i = 0; input[i]; i++)
free(input[i]);
free(input);
free(buff);
exit(exitv);
}
else if (_strcmp(input[0], "cd") == 0)
{
_cd(input);
for (i = 0; input[i]; i++)
free(input[i]);
free(input);
free(buff);
return (1);
}
else
return (0);
}
/**
* _cd - change directory.
* @input: List of input. input[0] is "cd". input[1] is the directory.
* Return: Always returns 1.
*/
int _cd(char **input)
{
if (input[1] == NULL)
{
if (chdir(_getenv("HOME")) != 0)
perror("hsh");
}
else if (chdir(input[1]) != 0)
perror("hsh");
return (1);
}