-
Notifications
You must be signed in to change notification settings - Fork 0
/
geterror.c
90 lines (85 loc) · 1.27 KB
/
geterror.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
#include "main.h"
/**
* intlen - a func that ... later
* @n: a parameter
* Return: the lenth.
*/
int intlen(int n)
{
unsigned int num;
int len = 1;
if (n < 0)
{
len++;
num = n * -1;
}
else
{
num = n;
}
while (num > 9)
{
len++;
num = num / 10;
}
return (len);
}
/**
* _itoa - a func that .. later
* @num: a passing param.
* Return: buff if not NULL.
*/
char *_itoa(int num)
{
char *buff;
size_t n;
int len = intlen(num);
buff = malloc(len + 1);
if (!buff)
{
return (NULL);
}
buff[len] = '\0';
if (num < 0)
{
n = num * -1;
*buff = '-';
}
else
n = num;
len--;
do {
*(buff + len) = (n % 10) + '0';
n /= 10;
len--;
} while (n > 0);
return (buff);
}
/**
* geterror - a function that run command
* @n: argument
* @arv: arv argument
* @cmd: the command.
* Return: void.
*/
void geterror(denum *n, char **arv, char *cmd)
{
int len;
char *errmsg, *cnt_str;
cnt_str = _itoa(n->cnt);
len = _strlen(arv[0]) + _strlen(cmd) + _strlen(cnt_str) + 17;
errmsg = malloc(len);
if (!errmsg)
{
return;
}
_strcpy(errmsg, arv[0]);
_strcat(errmsg, ": ");
_strcat(errmsg, cnt_str);
_strcat(errmsg, ": ");
_strcat(errmsg, cmd);
_strcat(errmsg, ": not found\n");
_strcat(errmsg, "\0");
write(STDOUT_FILENO, errmsg, len);
free(errmsg);
}