-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherrors.c
46 lines (43 loc) · 1.23 KB
/
errors.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
#include "shell.h"
/**
* ctrl_c - detect the CTRL+C signal and print another line with the prompt.
* @x: idk.
*/
void ctrl_c(__attribute__((unused)) int x)
{
signal(SIGINT, ctrl_c);
write(1, "\n", 1);
write(STDOUT_FILENO, "\033[0;36mhsh# \033[0m", 16);
}
/**
* print_error - print specific errors to standard output.
* @program_name: argv[0] of main.
* @input: command that produces the error.
* @error_num: error number - identifies the error type.
*/
void print_error(char *program_name, char *input, int error_num)
{
char *str;
if (error_num == 127) /* command not found */
{
write(STDOUT_FILENO, program_name, str_len(program_name));
write(STDOUT_FILENO, ": 1: ", 5);
write(STDOUT_FILENO, input, str_len(input));
write(STDOUT_FILENO, ": not found\n", 12);
}
if (error_num == 2) /* syntax error*/
{
str = "sh: 1: Syntax error: \"";
write(STDOUT_FILENO, str, str_len(str));
write(STDOUT_FILENO, ";", 1);
str = "\" unexpected\n";
write(STDOUT_FILENO, str, str_len(str));
}
if (error_num == 3) /* malloc can't allocate memory */
{
write(STDOUT_FILENO, program_name, str_len(program_name));
write(STDOUT_FILENO, ": 1: ", 5);
str = "internal error allocating memory\n";
write(STDOUT_FILENO, str, str_len(str));
}
}