-
Notifications
You must be signed in to change notification settings - Fork 1
/
shellBuildIn.c
104 lines (95 loc) · 1.57 KB
/
shellBuildIn.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "shell.h"
/**
* exit_shell - Exit shell with code
* @code: the code
*
* Return: the code
*/
int exit_shell(char *code)
{
int exit_code;
if (code != NULL)
{
exit_code = atoi(code);
exit(exit_code);
}
else
return (0);
}
/**
* print_env - Print the environment
*
* Return: 1
*/
int print_env(void)
{
char **env = environ;
while (*env != NULL)
{
printf("%s\n", *env);
env++;
}
return (1);
}
/**
* change_dir - Change directory
* @dir: the directory to change to
*
* Return: 1
*/
int change_dir(char *dir)
{
char *homedir;
char *previousdir;
if (dir == NULL)
{
homedir = _getenv("HOME");
if (homedir != NULL)
{
if (chdir(homedir) != 0)
perror("sh");
}
else
print_err(NULL, "sh: HOME environment variable not set\n");
}
else if (_strcmp(dir, "-") == 0)
{
previousdir = _getenv("OLDPWD");
if (previousdir == NULL)
print_err(NULL, "sh: OLDPWD environment variable not set.\n");
else
{
if (chdir(previousdir) != 0)
perror("sh");
}
}
else
{
if (chdir(dir) != 0)
perror("sh");
}
return (1);
}
/**
* shellBuildIn - Call shell buildin command
* @parsedtxt: The command entered by user
*
* Return: 0 or 1 depending on the command
*/
int shellBuildIn(char **parsedtxt)
{
int i = 0;
if (_strcmp(parsedtxt[0], "exit") == 0)
i = exit_shell(parsedtxt[1]);
else if (_strcmp(parsedtxt[0], "env") == 0)
i = print_env();
else if (_strcmp(parsedtxt[0], "cd") == 0)
i = change_dir(parsedtxt[1]);
else
{
print_err("./hsh: 1: ", NULL);
print_err(parsedtxt[0], " :not found\n");
i = 127;
}
return (i);
}