-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinterr.c
96 lines (84 loc) · 1.71 KB
/
printerr.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
#include "shell.h"
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
/**
* printerr - print error message
*
* @prog_name: name of the program
* @lines : lines run
* @command : the invalidd command
*
* Return: void
*/
void printerr(char *prog_name, size_t lines, char *command)
{
char *error_prefix = ": ";
char *error_separator = ": ";
char *error_msg = "not found";
char *error_suffix = "\n";
write(STDERR_FILENO, prog_name, _strlen(prog_name));
write(STDERR_FILENO, error_prefix, _strlen(error_prefix));
print_d(lines);
write(STDERR_FILENO, error_separator, _strlen(error_separator));
write(STDERR_FILENO, command, _strlen(command));
write(STDERR_FILENO, error_separator, _strlen(error_separator));
write(STDERR_FILENO, error_msg, _strlen(error_msg));
write(STDERR_FILENO, error_suffix, _strlen(error_suffix));
}
/**
* _putchar - writes the character c to stdout
*
* @c: The character to print
*
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
*/
int _putchar(char c) { return (write(2, &c, 1)); }
/**
* print_d - print digits
*
* @n: the number to print
*
* Return: Description of the returned value
*/
int print_d(int n)
{
int digits[30];
int i, j, written;
int skip_zeros;
i = 0;
written = 0;
if (n == 0)
{
written += _putchar('0');
return (written);
}
else if (n == INT_MIN)
{
digits[i] = -(n % 10);
n /= 10;
i++;
}
if (n < 0)
{
written += _putchar('-');
n *= -1;
}
while (n != 0)
{
digits[i] = n % 10;
n /= 10;
i++;
}
skip_zeros = 1;
for (j = i - 1; j >= 0; j--)
{
if (digits[j] != 0)
skip_zeros = 0;
if (!skip_zeros)
written += _putchar(digits[j] + '0');
}
return (written);
}