-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_msg.c
215 lines (182 loc) · 4.42 KB
/
error_msg.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "shs.h"
/**
* strcat_error_msg - Concatenates the error message for the cd command.
*
* @shdata: Data structure containing relevant information.
* @msg: Message to print.
* @error: Output error message.
* @var_str: Counter lines.
*
* Return: Error message.
*/
char *strcat_error_msg(shell_data *shdata,
char *msg, char *error, char *var_str)
{
char *illegal_flag;
/* Copy program name*/
_strcpy(error, shdata->av[0]);
/* Concatenate ": ", counter, ": ", and command */
_strcat(error, ": ");
_strcat(error, var_str);
_strcat(error, ": ");
_strcat(error, shdata->args[0]);
/* Concatenate error message */
_strcat(error, msg);
/* Check if an illegal flag is present */
if (shdata->args[1][0] == '-')
{
illegal_flag = malloc(3);
illegal_flag[0] = '-';
illegal_flag[1] = shdata->args[1][1];
illegal_flag[2] = '\0';
/* Concatenate illegal flag */
_strcat(error, illegal_flag);
free(illegal_flag);
}
else
{
/* Concatenate argument */
_strcat(error, shdata->args[1]);
}
/* Concatenate new line and null terminator */
_strcat(error, "\n");
_strcat(error, "\0");
return (error);
}
/**
* error_env - Generates an error message for the env command in get_env.
*
* @shdata: Data structure containing relevant information.
*
* Return: Error message.
*/
char *error_env(shell_data *shdata)
{
int length;
char *error;
char *var_str;
char *msg;
var_str = int_to_string(shdata->counter);
msg = ": Unable to add/remove from environment\n";
length = _strlen(shdata->av[0]) + _strlen(var_str);
length += _strlen(shdata->args[0]) + _strlen(msg) + 4;
error = malloc(sizeof(char) * (length + 1));
if (error == 0)
{
free(error);
free(var_str);
return (NULL);
}
/* Concatenate program name, counter, command, and error message */
_strcpy(error, shdata->av[0]);
_strcat(error, ": ");
_strcat(error, var_str);
_strcat(error, ": ");
_strcat(error, shdata->args[0]);
_strcat(error, msg);
_strcat(error, "\0");
free(var_str);
return (error);
}
/**
* error_path_126 - Generates an error
* message for path and permission denied (126).
*
* @shdata: Data structure containing relevant information.
*
* Return: Error message.
*/
char *error_path_126(shell_data *shdata)
{
int length;
char *var_str;
char *error;
var_str = int_to_string(shdata->counter);
length = _strlen(shdata->av[0]) + _strlen(var_str);
length += _strlen(shdata->args[0]) + 24;
error = malloc(sizeof(char) * (length + 1));
if (error == 0)
{
free(error);
free(var_str);
return (NULL);
}
/* Concatenate program name, counter, command, and error message */
_strcpy(error, shdata->av[0]);
_strcat(error, ": ");
_strcat(error, var_str);
_strcat(error, ": ");
_strcat(error, shdata->args[0]);
_strcat(error, ": Permission denied\n");
_strcat(error, "\0");
free(var_str);
return (error);
}
/**
* error_message - Generates the error message for the cd command.
*
* @shdata: Data structure containing relevant information.
*
* Return: Error message.
*/
char *error_message(shell_data *shdata)
{
int length, len_id;
char *error, *var_str, *msg;
var_str = int_to_string(shdata->counter);
/* Check if an illegal option or directory is present */
if (shdata->args[1][0] == '-')
{
msg = ": Illegal option ";
len_id = 2;
}
else
{
msg = ": can't cd to ";
len_id = _strlen(shdata->args[1]);
}
length = _strlen(shdata->av[0]) + _strlen(shdata->args[0]);
length += _strlen(var_str) + _strlen(msg) + len_id + 5;
error = malloc(sizeof(char) * (length + 1));
if (error == 0)
{
free(var_str);
return (NULL);
}
error = strcat_error_msg(shdata, msg, error, var_str);
free(var_str);
return (error);
}
/**
* cmd_not_found - Generates a generic error message for a command not found.
*
* @shdata: Data structure containing relevant information.
*
* Return: Error message.
*/
char *cmd_not_found(shell_data *shdata)
{
int length;
char *error;
char *var_str;
var_str = int_to_string(shdata->counter);
length = _strlen(shdata->av[0]) + _strlen(var_str);
length += _strlen(shdata->args[0]) + 16;
error = malloc(sizeof(char) * (length + 1));
if (error == 0)
{
free(error);
free(var_str);
return (NULL);
}
/* Concatenate program name, counter, command, and error message */
_strcpy(error, shdata->av[0]);
_strcat(error, ": ");
_strcat(error, var_str);
_strcat(error, ": ");
_strcat(error, shdata->args[0]);
_strcat(error, ": not found\n");
_strcat(error, "\0");
free(var_str);
return (error);
}