-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.c
107 lines (97 loc) · 2.11 KB
/
errors.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
#include "shell.h"
int create_error(char **args, int err);
char *error_126(char **args);
char *error_127(char **args);
/**
* error_126 - Creates an error message for permission denied failures.
* @args: An array of arguments passed to the command.
* Return: The error string.
*/
char *error_126(char **args)
{
char *error, *hist_str;
int len;
hist_str = _itoa(hist);
if (!hist_str)
return (NULL);
len = _strlen(name) + _strlen(hist_str) + _strlen(args[0]) + 24;
error = malloc(sizeof(char) * (len + 1));
if (!error)
{
free(hist_str);
return (NULL);
}
_strcpy(error, name);
_strcat(error, ": ");
_strcat(error, hist_str);
_strcat(error, ": ");
_strcat(error, args[0]);
_strcat(error, ": Permission denied\n");
free(hist_str);
return (error);
}
/**
* error_127 - Creates an error message for command not found failures.
* @args: An array of arguments passed to the command.
* Return: The error string.
*/
char *error_127(char **args)
{
char *error, *hist_str;
int len;
hist_str = _itoa(hist);
if (!hist_str)
return (NULL);
len = _strlen(name) + _strlen(hist_str) + _strlen(args[0]) + 16;
error = malloc(sizeof(char) * (len + 1));
if (!error)
{
free(hist_str);
return (NULL);
}
_strcpy(error, name);
_strcat(error, ": ");
_strcat(error, hist_str);
_strcat(error, ": ");
_strcat(error, args[0]);
_strcat(error, ": not found\n");
free(hist_str);
return (error);
}
/**
* create_error - Writes a custom error message to stderr.
* @args: An array of arguments.
* @err: The error value.
* Return: The error value.
*/
int create_error(char **args, int err)
{
char *error;
switch (err)
{
case -1:
error = error_env(args);
break;
case 1:
error = error_1(args);
break;
case 2:
if (*(args[0]) == 'e')
error = error_2_exit(++args);
else if (args[0][0] == ';' || args[0][0] == '&' || args[0][0] == '|')
error = error_2_syntax(args);
else
error = error_2_cd(args);
break;
case 126:
error = error_126(args);
break;
case 127:
error = error_127(args);
break;
}
write(STDERR_FILENO, error, _strlen(error));
if (error)
free(error);
return (err);
}