-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlsh_utils.c
76 lines (67 loc) · 1.54 KB
/
lsh_utils.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
#include "liteshell.h"
/**
* lsh_init - initializes the relevant objects needed for the shell's
* operation
*
* Return: the initialized shell objects
*/
lsh_t *lsh_init(void)
{
lsh_t *lsh = malloc(sizeof(lsh_t));
/*
* Let's handle memory allocation errors. This could happen if the system
* is out of memory to allocate for a new process, in which case our can't
* even start. We will graciously exit with -1
*/
if (check_err(lsh, 'm') == -1)
{
exit(EXIT_FAILURE);
}
lsh->aliases = malloc(sizeof(alias_t));
if (check_err(lsh->aliases, 'm') == -1)
{
exit(EXIT_FAILURE);
}
lsh->exit_code = 0;
lsh->user_input = NULL;
lsh->aliases->head = NULL;
lsh->aliases->tail = NULL;
lsh->aliases->size = 0;
return (lsh);
}
/**
* check_err - a mini error checker
* @ptr: the pointer to check for NULL
* @ptr_type: the type of data the pointer points
*
* Pointer Types: 'l' is used to check for uninitialized lists.
* 'm' checks for memory allocation failures, 'a' is for all other pointer
* types that needs to checked for NULL
*
* Return: 0 if not NULL, -1 otherwise
*/
int check_err(void *ptr, char ptr_type)
{
if (ptr == NULL)
{
if (ptr_type == 'l')
{
fprintf(stderr, "The list is not initialized\n");
return (-1);
}
/* let's check for memory allocation errors */
if (ptr_type == 'm')
{
errno = ENOMEM;
perror("check_err");
return (-1);
}
/* all other NULL pointer checks */
if (ptr_type == 'a')
{
fprintf(stderr, "This is a NULL pointer, don't proceed\n");
return (-1);
}
}
return (0);
}