-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin_func.c
128 lines (104 loc) · 2.04 KB
/
builtin_func.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "shell.h"
/**
* change_working_dir - changes working directory
* @argv: argument vector
*
* Return: 0 on success
*/
int change_working_dir(char **argv)
{
char *path, old_dir[PATH_MAX], new_dir[PATH_MAX];
if (argv[1] == NULL || _strcmp(argv[1], "~") == 0)
path = _getenv("HOME");
else if (_strcmp(argv[1], "-") == 0)
path = _getenv("OLDPWD");
else
path = argv[1];
if (access(path, X_OK) != 0)
(path)
? dprintf(STDERR_FILENO,
"./hsh: 1: cd: can't cd to %s\n", path)
: 0;
/*Get old pwd*/
if (getcwd(old_dir, sizeof(old_dir)) == NULL)
return (-1);
/*Change directory*/
if (chdir(path) != 0)
return (-1);
/*Update old pwd*/
if (_setenv("OLDPWD", old_dir, 1) != 0)
return (-1);
/*Get new pwd*/
if (getcwd(new_dir, sizeof(new_dir)) == NULL)
return (-1);
/*Update new pwd*/
if (_setenv("PWD", new_dir, 1) != 0)
return (-1);
return (0);
}
/**
* printenv - prints environment variable
* @argv: argument vector
*
* Return: 0 if successful
*/
int printenv(char **argv)
{
int i = 0;
(void)argv;
while (environ[i] != NULL)
{
printf("%s\n", environ[i]);
i++;
}
return (0);
}
/**
* exit_simple_shell - exit shell
* @argv: argument vector
*
* Return: 0 on exit and status if exit code is given
*/
int exit_simple_shell(char **argv)
{
char *exit_code = argv[1];
if (!exit_code)
{
free_argv(argv);
exit(0);
}
else
{
int code = atoi(exit_code);
free_argv(argv);
exit(code);
}
return (0);
}
/**
* modifyenv - modifies current environment using setenv or unsetenv
* @argv: argument vector
*
* Return: 0 if successful
*/
int modifyenv(char **argv)
{
char *command = argv[0];
int result;
if (_strcmp(command, "setenv") == 0 &&
(argv[1] != NULL) && (argv[2] != NULL))
{
char *name = argv[1];
char *value = argv[2];
result = _setenv(name, value, 1);
return (result);
}
if (_strcmp(command, "unsetenv") == 0 && (argv[1] != NULL))
{
char *name = argv[1];
result = _unsetenv(name);
return (result);
}
perror(": Environment not modified");
return (-1);
}