-
Notifications
You must be signed in to change notification settings - Fork 2
/
print_errors.c
143 lines (124 loc) · 3.74 KB
/
print_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
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
#include "shell.h"
/**
* print_error - prints a custom error message
* @i: index of the command in history
* @s: name of the program
* @argv: array of arguments from the command line
*
* Description: Concatenate strings to format the error message
* Free previous concatenation at every new string
* When the string is completed, write message to standard error
*/
void print_error(int *i, char *s, char **argv)
{
char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL, *buf4 = NULL, *buf5 = NULL;
char *number = NULL;
number = convert(*i, 10);
buf1 = str_concat(s, ": ");
buf2 = str_concat(buf1, number);
free(buf1);
buf3 = str_concat(buf2, ": ");
free(buf2);
buf4 = str_concat(buf3, argv[0]);
free(buf3);
buf5 = str_concat(buf4, ": not found\n");
free(buf4);
write(2, buf5, _strlen(buf5));
free(buf5);
}
/**
* print_error_env - prints a custom error message for env builtin
* @argv: array of arguments from the command line
* Description: Concatenate strings to format the error message
* Free previous concatenation at every new string
* When the string is completed, write message to standard error
*/
void print_error_env(char **argv)
{
char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL;
buf1 = str_concat(argv[0], ": ");
buf2 = str_concat(buf1, argv[1]);
free(buf1);
buf3 = str_concat(buf2, ": No such file or directory\n");
free(buf2);
write(2, buf3, _strlen(buf3));
free(buf3);
}
/**
* print_error_exit - prints a custom error message for exit builtin
* @i: index of the command in history
* @s: name of the program
* @argv: array of arguments from the command line
* Description: Concatenate strings to format the error message
* Free previous concatenation at every new string
* When the string is completed, write message to standard error
*/
void print_error_exit(int *i, char *s, char **argv)
{
char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL;
char *buf4 = NULL, *buf5 = NULL, *buf6 = NULL, *buf7 = NULL;
char *number = NULL;
number = convert(*i, 10);
buf1 = str_concat(s, ": ");
buf2 = str_concat(buf1, number);
free(buf1);
buf3 = str_concat(buf2, ": ");
free(buf2);
buf4 = str_concat(buf3, argv[0]);
free(buf3);
buf5 = str_concat(buf4, ": Illegal number: ");
free(buf4);
buf6 = str_concat(buf5, argv[1]);
free(buf5);
buf7 = str_concat(buf6, "\n");
free(buf6);
write(2, buf7, _strlen(buf7));
free(buf7);
}
/**
* print_error_main - prints a custom error message for main
* @av: array of arguments passed to main
* Description: Concatenate strings to format the error message
* Free previous concatenation at every new string
* When the string is completed, write message to standard error
*/
void print_error_main(char **av)
{
char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL;
buf1 = str_concat(av[0], ": 0: Can't open ");
buf2 = str_concat(buf1, av[1]);
free(buf1);
buf3 = str_concat(buf2, "\n");
free(buf2);
write(2, buf3, _strlen(buf3));
free(buf3);
}
/**
* print_error_cd - prints a custom error message for cd
* @i: index of the command in history
* @s: name of the program
* @argv: array of arguments from the command line
* Description: Concatenate strings to format the error message
* Free previous concatenation at every new string
* When the string is completed, write message to standard error
*/
void print_error_cd(int *i, char *s, char **argv)
{
char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL, *buf4 = NULL;
char *buf5 = NULL, *buf6 = NULL;
char *number = NULL;
number = convert(*i, 10);
buf1 = str_concat(s, ": ");
buf2 = str_concat(buf1, number);
free(buf1);
buf3 = str_concat(buf2, ": ");
free(buf2);
buf4 = str_concat(buf3, argv[0]);
free(buf3);
buf5 = str_concat(buf4, ": can't cd to ");
free(buf4);
buf6 = str_concat(buf5, argv[1]);
free(buf5);
write(2, buf6, _strlen(buf6));
free(buf6);
}