-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalias.c
58 lines (46 loc) · 1014 Bytes
/
alias.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
#include "main.h"
/**
* get_alias - gets value of an alias
*
* @name: name of alias
*
* Return: value
*/
char *get_alias(const char *name)
{
char **aliases = get_state()->aliases;
int alias_index;
if (name == NULL || *name == '\0')
return (NULL);
alias_index = get_alias_index(name);
if (alias_index == -1)
return (NULL);
return (strchr(aliases[alias_index], '=') + 1);
}
/**
* set_alias - sets alias
*
* @name: name of alias
* @value: value of alias
*
* Return: 0 on success, otherwise -1
*/
int set_alias(const char *name, const char *value)
{
char **aliases = get_state()->aliases;
int alias_index;
char *new_alias;
if (name == NULL || *name == '\0' || strchr(name, '=') != NULL)
return (-1);
alias_index = get_alias_index(name);
new_alias = assemble_alias(name, value);
if (alias_index != -1)
{
free(aliases[alias_index]);
aliases[alias_index] = new_alias;
return (0);
}
string_array_push(&get_state()->aliases, new_alias);
free(new_alias);
return (0);
}