-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalias.c
More file actions
99 lines (90 loc) · 1.51 KB
/
alias.c
File metadata and controls
99 lines (90 loc) · 1.51 KB
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
#include "hsh.h"
#include <string.h>
/**
* print_alias - print alias
*
* @alias: alias node
*/
void print_alias(node_t *alias)
{
_puts(alias->key);
_putchar('=');
_putchar('\'');
_puts(alias->value);
_putchar('\'');
_putchar('\n');
}
/**
* print_aliases - print all aliases
*
* @ctx: shell context
*/
void print_aliases(context_t *ctx)
{
node_t *alias = ctx->aliases;
while (alias)
{
print_alias(alias);
alias = alias->next;
}
}
/**
* set_alias - set a new alias
*
* @ctx: shell context
* @key: alias name
* @value: alias value
*
* Return: 0 on success, 1 on failure
*/
int set_alias(context_t *ctx, const char *key, const char *value)
{
if (add_node_end(&ctx->aliases, key, value) == NULL)
return (1);
return (0);
}
/**
* get_alias - get an alias
*
* @ctx: shell context
* @key: alias name
*
* Return: node_t pointer or null
*/
node_t *get_alias(context_t *ctx, const char *key)
{
return (get_node(ctx->aliases, key));
}
/**
* builtin_alias - set or get alias
*
* @ctx: shell context
*
* Return: 0 on success, 1 on error
*/
int builtin_alias(context_t *ctx)
{
char *token, *tmp, *value, *p;
char **args = ctx->cmd->args + 1;
if (*args == NULL)
print_aliases(ctx);
while (*args)
{
tmp = _strdup(*args);
p = tmp; /* for free() */
token = strtok(tmp, "=");
if (token == NULL)
{
free(tmp);
continue;
}
value = strtok(NULL, "=");
if (value == NULL)
print_alias(get_alias(ctx, token));
else
set_alias(ctx, token, value);
free(p);
args++;
}
return (0);
}