-
Notifications
You must be signed in to change notification settings - Fork 0
/
alias_management.c
112 lines (100 loc) · 2.92 KB
/
alias_management.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
#include "shell.h"
/**
* print_alias - add, remove or show aliases
* @data: struct for the program's data
* @alias: name of the alias to be printed
* Return: zero if sucess, or other number if its declared in the arguments
*/
int print_alias(data_of_program *data, char *alias)
{
int i, j, alias_length;
char buffer[250] = {'\0'};
if (data->alias_list)
{
alias_length = str_length(alias);
for (i = 0; data->alias_list[i]; i++)
{
if (!alias || (str_compare(data->alias_list[i], alias, alias_length)
&& data->alias_list[i][alias_length] == '='))
{
for (j = 0; data->alias_list[i][j]; j++)
{
buffer[j] = data->alias_list[i][j];
if (data->alias_list[i][j] == '=')
break;
}
buffer[j + 1] = '\0';
buffer_add(buffer, "'");
buffer_add(buffer, data->alias_list[i] + j + 1);
buffer_add(buffer, "'\n");
_print(buffer);
}
}
}
return (0);
}
/**
* get_alias - add, remove or show aliases
* @data: struct for the program's data
* @name: name of the requested alias.
* Return: zero if sucess, or other number if its declared in the arguments
*/
char *get_alias(data_of_program *data, char *name)
{
int i, alias_length;
/* validate the arguments */
if (name == NULL || data->alias_list == NULL)
return (NULL);
alias_length = str_length(name);
for (i = 0; data->alias_list[i]; i++)
{/* Iterates through the environ and check for coincidence of the varname */
if (str_compare(name, data->alias_list[i], alias_length) &&
data->alias_list[i][alias_length] == '=')
{/* returns the value of the key NAME= when find it */
return (data->alias_list[i] + alias_length + 1);
}
}
/* returns NULL if did not find it */
return (NULL);
}
/**
* set_alias - add, or override alias
* @alias_string: alias to be seted in the form (name='value')
* @data: struct for the program's data
* Return: zero if sucess, or other number if its declared in the arguments
*/
int set_alias(char *alias_string, data_of_program *data)
{
int i, j;
char buffer[250] = {'0'}, *temp = NULL;
/* validate the arguments */
if (alias_string == NULL || data->alias_list == NULL)
return (1);
/* Iterates alias to find = char */
for (i = 0; alias_string[i]; i++)
if (alias_string[i] != '=')
buffer[i] = alias_string[i];
else
{/* search if the value of the alias is another alias */
temp = get_alias(data, alias_string + i + 1);
break;
}
/* Iterates through the alias list and check for coincidence of the varname */
for (j = 0; data->alias_list[j]; j++)
if (str_compare(buffer, data->alias_list[j], i) &&
data->alias_list[j][i] == '=')
{/* if the alias alredy exist */
free(data->alias_list[j]);
break;
}
/* add the alias */
if (temp)
{/* if the alias already exist */
buffer_add(buffer, "=");
buffer_add(buffer, temp);
data->alias_list[j] = str_duplicate(buffer);
}
else /* if the alias does not exist */
data->alias_list[j] = str_duplicate(alias_string);
return (0);
}