forked from glyif/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliasB.c
97 lines (84 loc) · 1.68 KB
/
aliasB.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
#include "header.h"
/**
* save_alias - saves alias definitions to file
* @arginv: arguments inventory
*
* Return: 0 success, 1 failure
*/
int save_alias(arg_inventory_t *arginv)
{
alias_t *tmp = arginv->alias;
char *file, *buffer;
int fd;
file = arginv->alias_file;
fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
close(fd);
while (tmp)
{
buffer = (char *)safe_malloc(_strlen(tmp->alias) + _strlen(tmp->command)
+ 4);
_strcpy(buffer, tmp->alias);
_strcat(buffer, ":");
_strcat(buffer, tmp->command);
_strcat(buffer, "\n");
append_text_to_file(file, buffer);
tmp = tmp->next;
free(buffer);
}
return (0);
}
/**
* load_alias - loads alias definitions from file
* @arginv: arguments inventory
*
* Return: 0 success, 1 failure
*/
int load_alias(arg_inventory_t *arginv)
{
ssize_t count;
size_t sz = BUFSIZE;
char *file, *buffer, *val;
int fd;
file = arginv->alias_file;
fd = open(file, O_RDONLY);
if (fd == -1)
{
free(file);
return (1);
}
buffer = (char *)safe_malloc(sz);
while ((count = _readline(fd, &buffer, &sz)) != 0)
{
while (buffer[count - 1] == '\n')
buffer[count - 1] = '\0';
val = buffer;
while (*val && *val != ':')
val++;
if (!*val)
break;
*(val++) = '\0';
add_node_alias(&arginv->alias, buffer, val);
}
free(buffer);
close(fd);
return (0);
}
/**
* fetch_node_alias - fetches a node of a given alias
* @head: head of list
* @var: alias to match of the node to fetch
*
* Return: fetched node or NULL
*/
alias_t *fetch_node_alias(alias_t *head, char *var)
{
alias_t *tmp;
tmp = head;
while (tmp != NULL)
{
if (_strcmp(tmp->alias, var) == 0)
return (tmp);
tmp = tmp->next;
}
return (NULL);
}