-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handler.c
More file actions
76 lines (69 loc) · 1.37 KB
/
error_handler.c
File metadata and controls
76 lines (69 loc) · 1.37 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
#include "shell.h"
/**
* print_error - it prints error message to the standard error
* @vars: points to variables struct
* @msg: the message to print out
* by Stanley Ibeneme and Philadelphia Olawale-Adedotun
* Return: void
*/
void print_error(vars_t *vars, char *msg)
{
char *count;
_puts2(vars->argv[0]);
_puts2(": ");
count = _uitoa(vars->count);
_puts2(count);
free(count);
_puts2(": ");
_puts2(vars->av[0]);
if (msg)
{
_puts2(msg);
}
else
perror("");
}
/**
* _puts2 - prints string to the standard error
* @str: string to be printed
* by Stanley Ibeneme and Philadelphia Olawale-Adedotun
* Return: void
*/
void _puts2(char *str)
{
ssize_t num, len;
num = _strlen(str);
len = write(STDERR_FILENO, str, num);
if (len != num)
{
perror("Fatal Error");
exit(1);
}
}
/**
* _uitoa - it converts an unsigned integer to string
* @count: unsigned int to be converted
* by Stanley Ibeneme and Philadelphia Olawale-Adedotun
* Return: the pointer to converted string
*/
char *_uitoa(unsigned int count)
{
char *numstr;
unsigned int tmp, digits;
tmp = count;
for (digits = 0; tmp != 0; digits++)
tmp /= 10;
numstr = malloc(sizeof(char) * (digits + 1));
if (numstr == NULL)
{
perror("Fatal Error1");
exit(127);
}
numstr[digits] = '\0';
for (--digits; count; --digits)
{
numstr[digits] = (count % 10) + '0';
count /= 10;
}
return (numstr);
}