-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin_helper.c
More file actions
154 lines (129 loc) · 2.76 KB
/
builtin_helper.c
File metadata and controls
154 lines (129 loc) · 2.76 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "shell.h"
/**
* _env - print environment variables
* @s: status
* Return: NULL
*/
void _env(int *s)
{
int j;
for (j = 0; environ[j] != NULL; j++)
{
write(STDOUT_FILENO, environ[j], _strlen(environ[j]));
write(STDOUT_FILENO, "\n", 1);
}
*s = 0; /* success status */
}
/**
* _exit_logic - check if exit is cmd entered and perform terminal exit
* @cmd: command
* @cmd_arr: command and arguments
* @shell: shell name
* @curr: current path
* @status: status
* @num: count
* @cmd_lines: cmd lines
* @env: env
* Return: NULL
*/
void _exit_logic(char *cmd, char **cmd_arr, lst_path *curr, char *shell,
int num, int *status, lst_path *env, char **cmd_lines)
{
int num_exit;
if (_strcmp(cmd_arr[0], "exit") == 0)
{
if (cmd_arr[1] == NULL)
{
free_everything(cmd_lines, num, env, curr, cmd, cmd_arr);
exit(*status);
}
else if (cmd_arr[1] != NULL)
{
if (_strlen(cmd_arr[1]) <= 9)
{
num_exit = _atoi(cmd_arr[1]);
if (num_exit != -1)
{
free_everything(cmd_lines, num, env, curr, cmd, cmd_arr);
exit(num_exit);
}
else
{
*status = EXIT_ERROR;
_print_errors(shell, num, cmd_arr[1], EXIT_ERROR);
}
}
else
{
*status = EXIT_ERROR;
_print_errors(shell, num, cmd_arr[1], EXIT_ERROR);
}
}
}
}
/**
* _setenv - set an environment variable
* @key: environment variable name
* @value: environment variable value
* @curr: lst_paths structure
* Return : NULL
*/
void _setenv(char *key, char *value, lst_path *curr)
{
char *env_full = NULL;
int len_full = 0;
if (value == NULL || key == NULL)
return;
if (curr == NULL)
return;
len_full = 2 + _strlen(key) + _strlen(value);
env_full = malloc(len_full);
if (env_full == NULL)
return;
_strcpy(env_full, key);
_strcat(env_full, "=");
_strcat(env_full, value);
env_full[len_full - 1] = '\0';
free(env_full);
}
/**
* _cd - change current directory
* @cmd_arr: command array
* @argv: argument vector
* Return: NULL
*/
int _cd(char **cmd_arr, char **argv)
{
char _cwd[1024];
char *dir = cmd_arr[1];
if (getcwd(_cwd, sizeof(_cwd)) == NULL)
{
perror("error in getcwd()");
return (1);
}
if (dir == NULL || _strcmp(dir, "~") == 0)
dir = _get_env_value("HOME");
else if (_strcmp(dir, "-") == 0)
dir = _get_env_value("OLDPWD");
if (chdir(dir) == -1)
{
write(STDERR_FILENO, argv[0], _strlen(argv[0]));
write(STDERR_FILENO, ": 1", 3);
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, cmd_arr[0], _strlen(cmd_arr[0]));
write(STDERR_FILENO, ": can't cd to ", 14);
write(STDERR_FILENO, cmd_arr[1], _strlen(cmd_arr[1]));
write(STDERR_FILENO, "\n", 1);
return (1);
}
else
{
setenv("OLDPWD", _cwd, 1);
if (getcwd(_cwd, sizeof(_cwd)) == NULL)
{
perror("error in getcwd()");
return (1);
}
}
return (0);
}