-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_error2.c
More file actions
108 lines (99 loc) · 2.03 KB
/
_error2.c
File metadata and controls
108 lines (99 loc) · 2.03 KB
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
#include "main.h"
/**
* get_error - calls the error according the builtin,
* syntax or permission
* @datash: the data structure
* @eval: error value
* Return: error
*/
int get_error(data_shell *datash, int eval)
{
char *error;
switch (eval)
{
case -1:
error = error_env(datash);
break;
case 126:
error = error_path_126(datash);
break;
case 127:
error = error_not_found(datash);
break;
case 2:
if (_strcmp("exit", datash->args[0]) == 0)
error = error_exit_shell(datash);
else if (_strcmp("cd", datash->args[0]) == 0)
error = error_get_cd(datash);
break;
}
if (error)
{
_printerr(error);
free(error);
}
datash->status = eval;
return (eval);
}
/**
* error_env - error message
* @datash: data relevant
* Return: error message
*/
char *error_env(data_shell *datash)
{
int length;
char *error;
char *ver_str;
char *msg;
ver_str = get_itoa(datash->counter);
msg = ": Unable to add/remove from environment\n";
length = _strlen(datash->av[0]) + _strlen(ver_str);
length += _strlen(datash->args[0]) + _strlen(msg) + 4;
error = malloc(sizeof(char) * (length + 1));
if (error == 0)
{
free(error);
free(ver_str);
return (NULL);
}
_strcpy(error, datash->av[0]);
_strcat(error, ": ");
_strcat(error, ver_str);
_strcat(error, ": ");
_strcat(error, datash->args[0]);
_strcat(error, msg);
_strcat(error, "\0");
free(ver_str);
return (error);
}
/**
* error_path_126 - error message
* @datash: data relevant
* Return: error string
*/
char *error_path_126(data_shell *datash)
{
int length;
char *ver_str;
char *error;
ver_str = get_itoa(datash->counter);
length = _strlen(datash->av[0]) + _strlen(ver_str);
length += _strlen(datash->args[0]) + 24;
error = malloc(sizeof(char) * (length + 1));
if (error == 0)
{
free(error);
free(ver_str);
return (NULL);
}
_strcpy(error, datash->av[0]);
_strcat(error, ": ");
_strcat(error, ver_str);
_strcat(error, ": ");
_strcat(error, datash->args[0]);
_strcat(error, ": Permission denied\n");
_strcat(error, "\0");
free(ver_str);
return (error);
}