-
Notifications
You must be signed in to change notification settings - Fork 3
/
alias.c
142 lines (130 loc) · 2 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
#include "holberton.h"
/**
* add_alias - add alias.
* @head: node tha has the builtin command
* @name: name
* @value: value
* Return: no return
*/
alias *add_alias(alias **head, char *name, char *value)
{
int flag = 1;
alias *p = *head;
alias *new = malloc(sizeof(alias));
alias *cpy = NULL;
cpy = buscar_alias(p, name);
if (cpy)
{
free(cpy->value);
free(name);
free(new);
cpy->value = value;
return (cpy);
}
do {
if (!p || !(p->next))
{
if (!new)
return (NULL);
new->name = name;
new->value = value;
new->next = NULL;
if (!p)
{
*head = new;
return (new);
}
p->next = new;
flag = 0;
}
else
p = p->next;
} while (flag);
return (new);
}
/**
* _strcmp - compare string.
* @s1: string 1
* @s2: string 1
* Return: no return
*/
int _strcmp(char *s1, char *s2)
{
int i = 0, j = 0;
for (; s1[i] && s2[j]; i++, j++)
if (s1[i] != s2[j])
return (0);
if (!s1[i] && !s2[j])
return (1);
return (0);
}
/**
* buscar_alias - buscar alias
* @head: head
* @name: name
*
* Return: no return
*/
alias *buscar_alias(alias *head, char *name)
{
char *cpyname = NULL;
while (head)
{
cpyname = head->name;
if (_strcmp(cpyname, name))
return (head);
head = head->next;
}
return (NULL);
}
/**
* setalias - set alias.
* @tokens: node tha has the builtin command
*
* Return: no return
*/
alias *setalias(char **tokens)
{
static alias *head;
char *name = NULL, *value = NULL;
alias *res = NULL;
static int flag = 1;
if (!tokens)
return (head);
name = tokens[0];
value = tokens[1];
if (flag && !value)
return (head);
if (name && !value)
{
res = buscar_alias(head, name);
return (res);
}
if (!name && !value)
{
return (head);
}
if (name && value)
{
res = add_alias(&head, name, value);
return (res);
}
return (NULL);
}
/**
* _strlen - longitud string.
* @str: pointer to string
*
* Return: size
*/
int _strlen(char *str)
{
int i = 0;
if (str)
{
for (; str[i]; i++)
;
return (i + 1);
}
return (-1);
}