-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr.c
113 lines (105 loc) · 1.94 KB
/
err.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
105
106
107
108
109
110
111
112
113
#include "shell.h"
/**
* errorHandler - prints error message for shell
* @build: the build vars_t
*/
void errorHandler(vars_t *build)
{
register int len;
static char error[BUFSIZE];
char *ptr, *alpha;
alpha = itoa(build->count);
_strcat(error, build->shellName);
_strcat(error, ": ");
_strcat(error, alpha);
_strcat(error, ": ");
_strcat(error, build->args[0]);
_strcat(error, getErrorMessage());
if (build->args[1])
{
if (errno != EBADCD)
_strcat(error, ": ");
_strcat(error, build->args[1]);
}
_strcat(error, "\n");
ptr = _strchr(error, '\n');
len = ptr - error;
write(STDERR_FILENO, error, len + 1);
free(alpha);
insertNullByte(error, 0);
}
/**
* getErrorMessage - matches errno to corresponding string
* Return: string of error message
*/
char *getErrorMessage(void)
{
char *str;
switch (errno)
{
case EBADCD:
str = ": No such file or directory ";
break;
case ENOENT:
str = ": command not found";
break;
case ENOSTRING:
str = ": bad variable name";
break;
case EILLEGAL:
str = ": Illegal number";
break;
case EWSIZE:
str = ": invalid number of arguments";
break;
case ENOBUILTIN:
str = ": type help for a list of built-ins";
break;
case EACCES:
str = ": Permission denied";
break;
default:
str = ": no error number assigned";
}
return (str);
}
/**
* countDigits - count number of digits in a number
* @num: input number
* Return: number of digits
*/
unsigned int countDigits(int num)
{
register int digits = 0;
while (num > 0)
{
digits++;
num /= 10;
}
return (digits);
}
/**
* itoa - converts integer to string
* @num: input integer
* Return: string type of number
*/
char *itoa(unsigned int num)
{
register int digits = 0;
char *str;
digits += countDigits(num);
str = malloc(sizeof(char) * (digits + 1));
if (!str)
{
perror("Malloc: failed\n");
exit(errno);
}
insertNullByte(str, digits);
while (num > 0)
{
str[digits - 1] = num % 10 + '0';
num = num / 10;
digits--;
}
return (str);
}