-
Notifications
You must be signed in to change notification settings - Fork 1
/
alias.c
160 lines (146 loc) · 2.93 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
#include "shell.h"
/* for tracking the last found alias match */
static alias *match = NULL;
/**
* resetAlias - resets an alias to given values, if found in the list
* @head: pointer to head of the alias list
* @key: key to look for
* @value: value to set to key
* Return: a pointer to found alias or null
**/
alias *resetAlias(alias **head, char *key, char *value)
{
alias *start;
start = *head;
while (start)
{
if (_strcmp(start->key, key) == 0)
{
start->value = _strcpy(value, _strlen(value));
return (start);
}
start = start->next;
}
return (NULL);
}
/**
* addAlias - either add an alias to given value or adds new
* @head: pointer to head of the alias list
* @key: key to look for
* @value: value to set to key
* Return: a pointer to new alias
**/
alias *addAlias(alias **head, char *key, char *value)
{
alias *new, *temp;
int valueLen;
new = _malloc(sizeof(alias));
if (new == NULL)
return (NULL);
new->key = _strcpy(key, _strlen(key));
if (new->key == NULL)
{
_free(new);
return (NULL);
}
valueLen = _strlen(value);
if (value[0] == '"')
{
value++;
value[valueLen - 1] = '\0';
valueLen -= 2;
}
new->value = _strcpy(value, valueLen);
if (new->value == NULL)
{
_free(new->key);
_free(new);
return (NULL);
}
new->next = NULL;
if (*head == NULL)
{
*head = new;
return (new);
}
/* check if it already exits in the list */
temp = resetAlias(head, key, value);
if (temp != NULL)
{
_free(new->key);
_free(new->value);
_free(new);
return (temp);
}
temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = new;
return (new);
}
/**
* printAlias - prints an alias list
* @head: pointer to head of the alias list
* Return: nothing
**/
void printAlias(alias **head)
{
alias *start;
if (*head == NULL)
{
_puts("No alias found\n");
return;
}
start = *head;
while (start)
{
_puts("alias ");
_puts(start->key);
_puts("='");
_puts(start->value);
_puts("'\n");
start = start->next;
}
}
/**
* findAlias - finds an alias to given key, if found in the list
* @head: pointer to head of the alias list
* @key: key to look for
* Return: a pointer to found alias or null
**/
alias *findAlias(alias **head, char *key)
{
alias *start;
start = *head;
while (start)
{
if (_strcmp(start->key, key) == 0)
{
match = start;
return (start);
}
start = start->next;
}
return (NULL);
}
/**
* find_aliasToalias - checks recursively for an alais to an alias to an alias
* @head: pointer to head of the alias list
* @key: key to look for
* Return: a pointer to found alias
**/
alias *find_aliasToalias(alias **head, char *key)
{
alias *temp;
temp = findAlias(head, key);
if (temp == NULL)
return (match);
/* check if its pointing to itself */
if (_strcmp(temp->key, temp->value) == 0)
{
match = temp;
return (temp);
}
/* recursively find the next match */
return (find_aliasToalias(head, temp->value));
}