-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_errors.c
More file actions
42 lines (35 loc) · 795 Bytes
/
handle_errors.c
File metadata and controls
42 lines (35 loc) · 795 Bytes
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
#include "shell.h"
/**
* print_error - writes a string to the stderr
* @str: the string to be written
* Return: the number of character written
*/
int print_error(char *str)
{
int i = 0;
if (!(str))
return (0);
while (str[i])
{
write(STDERR_FILENO, &str[i], 1);
i++;
}
return (i);
}
/**
* _perror - prints the error format to the stderr
* @cmd: the command passed by the user
* @error_message: the error message to be displayed by the user
*/
void _perror(const char *cmd, char *error_message)
{
char *program_name = _strdup((char *)cmd);
char *first_command = strtok(program_name, " \t\r\n");
print_error(_getenv("_"));
print_error(": 1: ");
print_error(first_command);
print_error(": ");
print_error(error_message);
print_error("\n");
free(program_name);
}