-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_builtins.c
More file actions
85 lines (70 loc) · 1.11 KB
/
exec_builtins.c
File metadata and controls
85 lines (70 loc) · 1.11 KB
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
#include "shell.h"
/**
* exec_cd_dir - a function that changes the working directory
* @argv: arguments
*
* Return: 1 on success, 0 on failure.
*/
int exec_cd_dir(char **argv)
{
char *new_path = argv[1];
char *current_path;
if (new_path == NULL)
new_path = getenv("HOME");
else
{
switch (new_path[0])
{
case '~':
new_path = getenv("HOME");
break;
case '-':
new_path = getenv("OLDPWD");
break;
default:
/* No need to do change 'new_path' here, */
break;
}
}
if (chdir(new_path) != 0)
{
perror("cd failed");
return (1);
}
current_path = getcwd(NULL, 0);
setenv("PWD", current_path, 1);
free(current_path);
return (0);
}
/**
* exec_env - a function that prints environment variables.
* @str: The string.
*
* Return: 0 on success.
*/
int exec_env(char **str)
{
int i = 0;
(void)str;
while (environ[i])
{
_puts(environ[i]);
i++;
}
return (0);
}
/**
* exec_exit - A function that terminates a process.
* @str: argument.
*
* Return: 0 on success.
*/
int exec_exit(char **str)
{
if (str[1])
{
return (atoi(str[1]));
}
else
return (0);
}