-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminishell_error_msg.c
More file actions
49 lines (45 loc) · 1.16 KB
/
Copy pathminishell_error_msg.c
File metadata and controls
49 lines (45 loc) · 1.16 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
#include "minishell.h"
/*
* Output error messages of the following format to stderr.
* "minishell: {cmd_name}: {msg}\n"
*/
void put_minish_err_msg(const char *cmd_name, const char *msg)
{
write(STDERR_FILENO, "minishell: ", 11);
if (cmd_name)
write(STDERR_FILENO, cmd_name, ft_strlen(cmd_name));
write(STDERR_FILENO, ": ", 2);
if (msg)
write(STDERR_FILENO, msg, ft_strlen(msg));
write(STDERR_FILENO, "\n", 1);
}
/*
* Output error messages of the following format to stderr and return ret_val.
* "minishell: {cmd_name}: {msg}\n"
*/
int put_minish_err_msg_and_ret(int ret_val,
const char *cmd_name, const char *msg)
{
put_minish_err_msg(cmd_name, msg);
return (ret_val);
}
/*
* Output error messages of the following format to stderr and exit(status).
* "minishell: {cmd_name}: {msg}\n"
*/
void put_minish_err_msg_and_exit(int status,
const char *cmd_name, const char *msg)
{
put_minish_err_msg(cmd_name, msg);
exit(status);
}
void check_malloc_has_succeeded(char *cmd_name, void *ptr)
{
if (!ptr)
{
if (cmd_name)
put_minish_err_msg_and_exit(1, cmd_name, "malloc() failed");
else
put_minish_err_msg_and_exit(1, "malloc", "malloc() failed");
}
}