-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalias_manager.c
110 lines (102 loc) · 2.27 KB
/
alias_manager.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
#include "main.h"
/* The array of aliases in this shell program */
static alias_t **Alias_List;
/* The number of aliases in this shell program */
static int Alias_Count;
/**
* manage_aliases - Performs some management operations \
* on the aliases
* @op: The operation to perform
*/
void manage_aliases(char op)
{
int i;
if (op == MO_INIT)
{
Alias_Count = 0;
Alias_List = NULL;
}
else if (op == MO_FREE)
{
if (Alias_List != NULL)
{
for (i = 0; i < Alias_Count; i++)
{
if (*(Alias_List + i) != NULL)
{
if ((*(Alias_List + i))->name != NULL)
free((*(Alias_List + i))->name);
if ((*(Alias_List + i))->value != NULL)
free((*(Alias_List + i))->value);
free(*(Alias_List + i));
}
}
if (Alias_List != NULL)
free(Alias_List);
}
}
}
/**
* add_alias - Adds an alias to the list of aliases \
* or replaces the value if it exists
* @name: The name of the alias
* @value: The value of the alias
*/
void add_alias(char *name, char *value)
{
int i;
if (is_alias(name))
{
for (i = 0; i < Alias_Count; i++)
{
if (str_eql(name, (*(Alias_List + i))->name))
{
if ((*(Alias_List + i))->value != NULL)
free((*(Alias_List + i))->value);
(*(Alias_List + i))->value = str_copy(value);
}
}
}
else
{
Alias_List = _realloc(Alias_List, sizeof(alias_t) * Alias_Count,
sizeof(alias_t) * (Alias_Count + 1));
*(Alias_List + Alias_Count) = malloc(sizeof(alias_t));
if (*(Alias_List + Alias_Count) != NULL)
{
(*(Alias_List + Alias_Count))->name = str_copy(name);
(*(Alias_List + Alias_Count))->value = str_copy(value);
}
Alias_Count++;
}
}
/**
* get_alias_value - Retrieves the value of an alias
* @str: The name of the alias to retrieve
*
* Return: The value of the alias, otherwise NULL
*/
char *get_alias_value(char *str)
{
int i;
if (Alias_List == NULL)
return (NULL);
for (i = 0; i < Alias_Count; i++)
{
if (str_eql(str, (*(Alias_List + i))->name))
return ((*(Alias_List + i))->value);
}
return (NULL);
}
/**
* get_aliases - Retrieves the aliases available and stores the \
* length in the given parameter
* @len: The pointer that contains the number of aliases available
*
* Return: The list of aliases
*/
alias_t **get_aliases(int *len)
{
*len = Alias_Count;
return (Alias_List);
}