This repository has been archived by the owner on Nov 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
alias_builtin.c
144 lines (131 loc) · 3.28 KB
/
alias_builtin.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
#include "main.h"
/**
* check_alias - is a function that check if an alias exist
* @neww: is a list of arguments
* Return: Name of alias if success or NULL if not
*/
char *check_alias(char *neww)
{
alias *temp = get_alias_list();
while (temp != NULL)
{
if (_strcmp(neww, temp->new_command) == 0)
{
return (temp->new_command);
}
temp = temp->next;
}
return (NULL);
}
/**
* execute_alias - is a function that check if an alias exist
* @args: is a list of arguments
* @main: is the value
* Return: Name of alias if success or NULL if not
*/
int execute_alias(char *main, char **args)
{
if (execve(main, args, environ) == -1)
{
fprintf(stderr, "%s: No such file or directory\n", args[0]);
exit(EXIT_FAILURE);
}
return (0);
}
/**
* create_keyvalue_pair - is a function that creates an alias
* @name: is the the argument to be initialized into the name_command
* @alias_list: is a pointer to the alias list
* @equals: is a pointer to the '=' in the args
* Return: the new node
*/
int create_keyvalue_pair(alias **alias_list, char *name, char *equals)
{
alias *list_copy = *alias_list;
int i, b = 0, j, value_length;
char *main_command, *new_command = malloc((strlen(name) + 1) * sizeof(char));
while (name[b] != '=')
{
new_command[b] = name[b];
b++;
}
*equals = '\0';
equals = equals + 1;
printf("now allocating memory for value_command equals = %s\n", equals);
value_length = strlen(equals) - strspn(equals, "'\"");
main_command = malloc(sizeof(char) * (value_length + 1));
printf("now entering initialization loop\n");
for (i = 0, j = 0; equals[i] != '\0'; i++)
{
if (equals[i] != '\'' && equals[i] != '"')
{
main_command[j] = equals[i];
j++;
}
}
main_command[j] = '\0';
printf("before add name = %s and value = %s\n", new_command, main_command);
*alias_list = add_alias(&list_copy, new_command, main_command);
return (0);
}
/**
* add_alias - is a function that adds an alias to the list
* @new: is the name of the new command
* @main: is the name fo the main command
* @head: is a pointer to a list of aliases
* Return: the head of the list
*/
alias *add_alias(alias **head, char *new, char *main)
{
alias *new_node = NULL;
alias *temp;
new_node->main_command = malloc(sizeof(char) * 50);
new_node->new_command = malloc(sizeof(char) * 50);
new_node->main_command = strdup(main);
new_node->new_command = strdup(new);
new_node->next = NULL;
if (*head == NULL)
{
*head = new_node;
}
else
{
temp = *head;
while (temp->next != NULL)
{
if (strcmp(new, temp->new_command) == 0)
{
free(temp->main_command);
temp->main_command = strdup(main);
free(new_node->main_command);
free(new_node->new_command);
return (*head);
}
temp = temp->next;
}
temp->next = new_node;
}
printf("added alias to list successfully\n");
while (temp != NULL)
{
printf("alias is %s='%s'\n", temp->new_command, temp->main_command);
temp = temp->next;
}
return (*head);
}
/**
* print_alias_list - is a function that prints out the alias list
* @head: is a pointer to a list of aliases
* Return: void
*/
int print_alias_list(alias *head)
{
alias *temp = head;
while (temp != NULL)
{
printf("alias %s=\'%s\'\n", temp->new_command, temp->main_command);
temp = temp->next;
}
printf("print list successfully\n");
return (0);
}