This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
forked from roryfahy/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seterror.c
87 lines (79 loc) · 2.02 KB
/
seterror.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
#include "shell.h"
/**
* _error - handle errors
*/
void _error(void)
{
char *prompt_count;
int pc_len;
prompt_count = _itoa(globes.prompt_count);
pc_len = _strlen(prompt_count);
if (errno == EILLEGALNUMB)
print_error("Illegal number: ", globes.argTokes[1], prompt_count, pc_len);
else if (errno == ENOENT || errno == ENOTDIR)
{
if (errno == ENOENT)
globes.last_exit_status = 127;
else
globes.last_exit_status = 2;
print_error("not found\n", NULL, prompt_count, pc_len);
}
else
{
if (errno == EACCES)
globes.last_exit_status = 126;
else
globes.last_exit_status = 2;
create_perror_string(prompt_count, pc_len);
perror(globes.errstr);
if (globes.errstr != NULL)
free(globes.errstr);
}
free(prompt_count);
}
/**
* print_error - print proper error message
* @errstr: error message
* @opt: optional string
* @prompt_count: prompt count string
* @pc_len: length of pc str
* Return: void
*/
void print_error(char *errstr, char *opt, char *prompt_count, int pc_len)
{
write(STDERR_FILENO, globes.argv0, globes.argv0_len);
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, prompt_count, pc_len);
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, globes.argTokes[0], _strlen(globes.argTokes[0]));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, errstr, _strlen(errstr));
if (opt != NULL)
{
write(STDERR_FILENO, opt, _strlen(opt));
write(STDERR_FILENO, "\n", 1);
}
}
/**
* create_perror_string - stuff
* @prompt_count: prompt count string
* @pc_len: length of pc str
* Return: pointer to string
*/
char *create_perror_string(char *prompt_count, int pc_len)
{
int msize = 5;
int j = 0;
msize += globes.argv0_len + pc_len + _strlen(globes.argTokes[0]);
globes.errstr = malloc(msize);
if (globes.errstr == NULL)
return (NULL);
for (; j < msize; j++)
globes.errstr[j] = '\0';
_strcat(globes.errstr, globes.argv0);
_strcat(globes.errstr, ": ");
_strcat(globes.errstr, prompt_count);
_strcat(globes.errstr, ": ");
_strcat(globes.errstr, globes.argTokes[0]);
return (globes.errstr);
}