-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror_operations.c
66 lines (57 loc) · 1.57 KB
/
error_operations.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
/* error_operations.c */
#include "shell.h"
/**
* _error_handler - Handles and prints error messages during command execution.
*
* @name: Name of the shell.
* @count: Command counter.
* @command: Array of strings representing the command.
* @status: Error status (0 for exit, 1 otherwise).
*/
void _error_handler(char *name, int count, char **command, int status)
{
char *string_counter;
string_counter = _itoa(count);
write(STDERR_FILENO, name, _strlen(name));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, string_counter, _strlen(string_counter));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, command[0], _strlen(command[0]));
write(STDERR_FILENO, ": ", 2);
if (status == 1)
write(STDERR_FILENO, "not found", _strlen("not found"));
else
{
write(STDERR_FILENO, "Illegal number", _strlen("Illegal number"));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, command[1], _strlen(command[1]));
}
write(STDERR_FILENO, "\n", 1);
free(string_counter);
}
/**
* _check_exit - Checks if the command is an exit command.
*
* @full_command: Array of strings representing the full command.
*
* Return: -1 if an invalid number, 0 on success.
*/
int _check_exit(char **full_command)
{
long int status;
if (_strncmp(full_command[0], "exit", 4) == 0 && !full_command[1])
{
_free_command(full_command);
exit(0);
}
status = _atoi(full_command[1]);
if (_strncmp(full_command[0], "exit", 4) == 0 && status != -1)
{
_free_command(full_command);
exit(status);
}
else if (_strncmp(full_command[0], "exit", 4) != 0)
return (1);
else
return (-1);
}