-
Notifications
You must be signed in to change notification settings - Fork 0
/
alias_builtins.c
158 lines (146 loc) · 3.09 KB
/
alias_builtins.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* File: builtin.c
* Auth: Carol and Daniel
*/
#include "shell.h"
int shellby_alias(char **args, char __attribute__((__unused__)) **front);
void set_alias(char *var_name, char *value);
void print_alias(alias_t *alias);
/**
* shellby_alias - Builtin command that either prints all aliases, specific
* aliases, or sets an alias.
* @args: An array of arguments.
* @front: A double pointer to the beginning of args.
*
* Return: If an error occurs - -1.
* Otherwise - 0.
*/
int shellby_alias(char **args, char __attribute__((__unused__)) **front)
{
alias_t *temp = aliases;
int i, ret = 0;
char *value;
if (!args[0])
{
while (temp)
{
print_alias(temp);
temp = temp->next;
}
return (ret);
}
for (i = 0; args[i]; i++)
{
temp = aliases;
value = _strchr(args[i], '=');
if (!value)
{
while (temp)
{
if (_strcmp(args[i], temp->name) == 0)
{
print_alias(temp);
break;
}
temp = temp->next;
}
if (!temp)
ret = create_error(args + i, 1);
}
else
set_alias(args[i], value);
}
return (ret);
}
/**
* set_alias - Will either set an existing alias 'name' with a new value,
* 'value' or creates a new alias with 'name' and 'value'.
* @var_name: Name of the alias.
* @value: Value of the alias. First character is a '='.
*/
void set_alias(char *var_name, char *value)
{
alias_t *temp = aliases;
int len, j, k;
char *new_value;
*value = '\0';
value++;
len = _strlen(value) - _strspn(value, "'\"");
new_value = malloc(sizeof(char) * (len + 1));
if (!new_value)
return;
for (j = 0, k = 0; value[j]; j++)
{
if (value[j] != '\'' && value[j] != '"')
new_value[k++] = value[j];
}
new_value[k] = '\0';
while (temp)
{
if (_strcmp(var_name, temp->name) == 0)
{
free(temp->value);
temp->value = new_value;
break;
}
temp = temp->next;
}
if (!temp)
add_alias_end(&aliases, var_name, new_value);
}
/**
* print_alias - Prints the alias in the format name='value'.
* @alias: Pointer to an alias.
*/
void print_alias(alias_t *alias)
{
char *alias_string;
int len = _strlen(alias->name) + _strlen(alias->value) + 4;
alias_string = malloc(sizeof(char) * (len + 1));
if (!alias_string)
return;
_strcpy(alias_string, alias->name);
_strcat(alias_string, "='");
_strcat(alias_string, alias->value);
_strcat(alias_string, "'\n");
write(STDOUT_FILENO, alias_string, len);
free(alias_string);
}
/**
* replace_aliases - Goes through the arguments and replace any matching alias
* with their value.
* @args: 2D pointer to the arguments.
*
* Return: 2D pointer to the arguments.
*/
char **replace_aliases(char **args)
{
alias_t *temp;
int i;
char *new_value;
if (_strcmp(args[0], "alias") == 0)
return (args);
for (i = 0; args[i]; i++)
{
temp = aliases;
while (temp)
{
if (_strcmp(args[i], temp->name) == 0)
{
new_value = malloc(sizeof(char) * (_strlen(temp->value) + 1));
if (!new_value)
{
free_args(args, args);
return (NULL);
}
_strcpy(new_value, temp->value);
free(args[i]);
args[i] = new_value;
i--;
break;
}
temp = temp->next;
}
}
return (args);
}