-
Notifications
You must be signed in to change notification settings - Fork 1
/
alias.c
174 lines (153 loc) · 2.96 KB
/
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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "shell.h"
/**
* add_alias - add node to linked list
* @head: first node
* @key: key to add in node
* @value: value to be added with the key
*
* Return: 0 on sucess -1 on faliure
*/
int add_alias(alias **head, char *key, char *value)
{
alias *node, *_head;
if (!head)
return (-1);
node = _malloc(sizeof(alias));
node->key = _strdup(key);
node->value = _strdup(value);
node->next = NULL;
if (!*head)
{
*head = node;
return (0);
}
_head = *head;
while (_head->next)
{
if (!_strcmp(_head->key, key))
{
free(_head->value);
_head->value = _strdup(value);
freealias(node);
return (0);
}
_head = _head->next;
}
_head->next = node;
return (0);
}
/**
* print_lalias - print contents of linked list
* @head: first node
* Return: 0 on sucess -1 on faliure
*/
int print_lalias(alias *head)
{
if (!head)
return (-1);
while (head)
{
_write(-1, NULL, 0);
_write(1, head->key, _strlen(head->key));
_write(1, "='", 2);
_write(1, head->value, _strlen(head->value));
_write(1, "'\n", 2);
_write(1, NULL, 0);
head = head->next;
}
return (0);
}
/**
* print_alias - print contents of a node whose key is key
* @head: first node
* @key: key of node to be printed
*
* Return: 0 on sucess -1 on faliure
*/
int print_alias(alias *head, char *key)
{
char *msg, *smn;
if (!head)
{
errno = -5;
msg = _malloc(_strlen("not found ") + _strlen(key) + 4);
_strcpy(msg, "not found ");
smn = _malloc(_strlen("alias: ") + _strlen(key) + 4);
_strcpy(smn, "alias: "), _strcat(smn, key);
print_error(smn, NULL, msg);
free(msg), free(smn);
return (-1);
}
while (head)
{
if (!_strcmp(head->value, key))
{
_write(-1, NULL, 0);
_write(1, head->key, _strlen(head->key));
_write(1, "='", 2);
_write(1, head->value, _strlen(head->value));
_write(1, "'\n", 2);
_write(1, NULL, 0);
return (0);
}
head = head->next;
}
errno = -3;
msg = _malloc(_strlen("not found ") + _strlen(key) + 4);
_strcpy(msg, "not found ");
smn = _malloc(_strlen("alias: ") + _strlen(key) + 4);
_strcpy(smn, "alias: "), _strcat(smn, key);
print_error(smn, NULL, msg);
free(msg), free(smn);
return (-1);
}
/**
* handle_alias - handle flags for aliases
* @arg: arguments
* @aliashead: first node
*
* Return: 0 on sucess -1 on faliure
*/
int handle_alias(char **arg, alias **aliashead)
{
int argc = _arlen(arg);
int i = 0;
char **tmp = NULL;
if (argc == 0)
return (-1);
if (argc == 1)
print_lalias(*aliashead);
if (argc > 1)
{
i += 1;
while (arg[i])
{
parse_args(arg[i], "=", &tmp, 0);
if (_arlen(tmp) > 1)
add_alias(aliashead, tmp[0], tmp[1]);
else
print_alias(*aliashead, tmp[0]);
free_pp(tmp);
i++;
}
}
return (0);
}
/**
* freealias - free alias linked list
* @head: first node
* Return: 0 on sucess -1 on faliure
*/
int freealias(alias *head)
{
alias *tmp;
while (head)
{
tmp = head->next;
free(head->key);
free(head->value);
free(head);
head = tmp;
}
return (0);
}