-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_messages.c
76 lines (64 loc) · 1.3 KB
/
error_messages.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
66
67
68
69
70
71
72
73
74
75
76
#include "shell.h"
/**
* fork_fail - function that handles a fork fail
* Return: Nothing
*/
void fork_fail(void)
{
perror("Error: ");
exit(EXIT_FAILURE);
}
/**
* build_message - Function to write an error message
*@av: argument vector
*@fir_com: first command to print if not found
*@count: number of times executed
*Return: Nothing(void)
*/
void build_message(char **av, char *fir_com, int count)
{
int num_length = 1, cp, mult = 1;
write(STDERR_FILENO, av[0], _strlen(av[0]));
write(STDERR_FILENO, ": ", 2);
cp = count;
while (cp >= 10)
{
cp /= 10;
mult *= 10;
num_length++;
}
while (num_length > 1)
{
if ((count / mult) < 10)
_puterror((count / mult + '0'));
else
_puterror((count / mult) % 10 + '0');
--num_length;
mult /= 10;
}
_puterror(count % 10 + '0');
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, fir_com, _strlen(fir_com));
write(STDERR_FILENO, ": not found\n", 12);
}
/**
* _puterror - Prints a char
*@c: character to write
*Return: int to print
*/
int _puterror(char c)
{
return (write(STDERR_FILENO, &c, 1));
}
/**
* end_file - function to control the interrupt signal
*@buffer: buffer array created by new line
*Return: Nothing(void)
*/
void end_file(char *buffer)
{
if (isatty(STDIN_FILENO))
write(STDOUT_FILENO, "\n", 1);
free(buffer);
exit(0);
}