-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_operations_functions.c
More file actions
184 lines (162 loc) · 4.46 KB
/
shell_operations_functions.c
File metadata and controls
184 lines (162 loc) · 4.46 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "shell.h"
/* Shell operations functions */
/**
* interactive_mode - Check if the shell is running in interactive mode
*
* @data_use: Pointer to the data structure containing information
* about the shell
*
* Return: 1 if the shell is in interactive mode, 0 otherwise
*/
int interactive_mode(data_t *data_use)
{
/* Check if the standard input is associated with a terminal */
/* read file descriptor is less than or equal to 2 */
return (isatty(STDIN_FILENO) && data_use->read_file_descriptor <= 2);
}
/**
* exit_shell - Exit the shell with a specified status or the default status
*
* @data_use: Pointer to the data structure containing information
* about the shell
*
* Return: -2 to signal the shell to exit,
* with the specified or default status
*/
int exit_shell(data_t *data_use)
{
int converted_value;
/* Check if a command-line argument is provided for the exit status */
if (data_use->argv[1] != 0)
{
/* Convert the argument to an integer */
converted_value = string_to_integer(data_use->argv[1]);
/* Check if the conversion is successful */
if (converted_value == -1)
{
/* Print an error message for an illegal number */
data_use->status = 2;
print_error(data_use, "Illegal number: ");
_eputs(data_use->argv[1]);
_eputchar('\n');
return (1);
}
/* Set the error_number to the converted value */
data_use->error_number = converted_value;
/* Signal the shell to exit with the specified status */
return (-2);
}
/* Set the default error_number for exit */
data_use->error_number = -1;
/* Signal the shell to exit with the default status */
return (-2);
}
/**
* change_directory - Change the current working directory
*
* @data_use: Pointer to the data structure containing information
* about the shell
*
* Return: 0 on successful directory change, 1 on error
*/
int change_directory(data_t *data_use)
{
char *str, *dir, buffer[1024];
int temp_dir;
str = getcwd(buffer, 1024);
if (str == NULL)
_puts("TODO: >>getcwd failure emsg here<<\n");
if (data_use->argv[1] == 0)
{
dir = get_environ_var(data_use, "HOME=");
if (dir == NULL)
temp_dir =
chdir((dir = get_environ_var(data_use, "PWD=")) ? dir : "/");
else
temp_dir = chdir(dir);
}
else if (compare_string(data_use->argv[1], "-") == 0)
{
if (!get_environ_var(data_use, "OLDPWD="))
{
_puts(str);
_putchar('\n');
return (1);
}
_puts(get_environ_var(data_use, "OLDPWD=")), _putchar('\n');
temp_dir =
chdir((dir = get_environ_var(data_use, "OLDPWD=")) ? dir : "/");
}
else
temp_dir = chdir(data_use->argv[1]);
if (temp_dir == -1)
{
print_error(data_use, "can't cd to ");
_eputs(data_use->argv[1]), _eputchar('\n');
}
else
{
set_environ_variable(data_use, "OLDPWD", get_environ_var(data_use, "PWD="));
set_environ_variable(data_use, "PWD", getcwd(buffer, 1024));
}
return (0);
}
/**
* help_shell - Display help information about the shell
*
* @data_use: Pointer to the data structure containing information
* about the shell
*
* Return: 0 on successful execution
*/
int help_shell(data_t *data_use)
{
char **arg_array;
/* Get the array of arguments */
arg_array = data_use->argv;
/* Display a message indicating that the function is not yet implemented */
_puts("help call works. Function not yet implemented \n");
/* Unused condition, kept for future use */
if (0)
_puts(*arg_array);
return (0);
}
/**
* find_shell - Find and execute the appropriate shell command
*
* @data_use: Pointer to the data structure containing information
* about the shell
*
* Return: 0 on successful execution of the built-in command,
* -1 if the command is not found
*/
int find_shell(data_t *data_use)
{
int i = 0;
/* Table of built-in commands with corresponding handlers */
shellcmd_t shell_table[] = {
{"exit", exit_shell},
{"env", print_environ},
{"help", help_shell},
{"history", history_list},
{"setenv", set_environ},
{"unsetenv", remove_environ},
{"cd", change_directory},
{"alias", man_alias},
{NULL, NULL}
};
/* Loop through the built-in command table */
while (shell_table[i].command != NULL)
{
/* Check if the entered command matches a built-in command */
if (compare_string(data_use->argv[0], shell_table[i].command) == 0)
{
/* Execute the corresponding handler func for the built-in cmd */
data_use->line_count++;
return (shell_table[i].handler(data_use));
}
i++;
}
/* Command not found in the built-in command table */
return (-1);
}