-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorMessage.c
104 lines (91 loc) · 1.52 KB
/
ErrorMessage.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "shell.h"
/**
* errPuts - prints an input string
* @string: string
*/
void errPuts(char *string)
{
int i = 0;
if (!string)
return;
while (string[i] != '\0')
{
errPutchar(string[i]);
i++;
}
}
/**
* errPutchar - writes a character to stderr
* @c: character
*
* Return: 1 if succesful, otherwise -1 (error)
*/
int errPutchar(char c)
{
static int n;
static char buffer[1024];
if (c == -1 || n >= 1024)
{
write(2, buffer, n);
n = 0;
}
if (c != -1)
{
buffer[n++] = c;
}
return (1);
}
/**
* errMessage - prints error message
* @tokens: command or agruments
* @line_number: line count
*
* Return: 0 if no numbers in string, converted number otherwise
*/
void errMessage(char **tokens, int line_number)
{
errPuts("./hsh");
errPuts(": ");
printDecimal(line_number, STDERR_FILENO);
errPuts(": ");
errPuts(tokens[0]);
errPuts(": ");
errPuts("not found\n");
errPutchar(-1);
}
/**
* printDecimal - prints decimal number of base 10
* @input: line number
* @fd: file descriptor
*
* Return: number of chars printed
*/
int printDecimal(int input, int fd)
{
int (*shell_putchar)(char) = ourPutChar;
int i, counter = 0;
unsigned int abso, curr;
if (fd == STDERR_FILENO)
shell_putchar = errPutchar;
if (input < 0)
{
abso = -input;
shell_putchar('-');
counter++;
}
else
abso = input;
curr = abso;
for (i = 1000000000; i > 1; i /= 10)
{
if (abso / i)
{
shell_putchar('0' + curr / i);
counter++;
}
curr %= i;
}
shell_putchar('0' + curr);
counter++;
return (counter);
}