-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.c
66 lines (54 loc) · 982 Bytes
/
state.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
#include "main.h"
static struct State *state;
/**
* init_state - initializes the state
*/
static void init_state(void)
{
state = malloc(sizeof(struct State));
if (state == NULL)
{
perror("malloc");
exit(1);
}
state->name = NULL;
state->prompt = NULL;
state->fd = -1;
state->status = 0;
state->count = 0;
state->env = NULL;
state->aliases = NULL;
state->commands = NULL;
}
/**
* get_state - gets the state
*
* Return: pointer to the state
*/
struct State *get_state(void)
{
if (state == NULL)
init_state();
return (state);
}
/**
* free_state - frees the state
*/
void free_state(void)
{
if (state == NULL)
return;
state->name = NULL;
state->prompt = NULL;
state->fd = -1;
state->status = 0;
state->count = 0;
if (state->env != NULL)
string_array_free(&state->env);
if (state->aliases != NULL)
string_array_free(&state->aliases);
if (state->commands != NULL)
string_array_free(&state->commands);
free(state);
state = NULL;
}