-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_inf0_1.c
77 lines (70 loc) · 1.65 KB
/
get_inf0_1.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
#include "shell.h"
/**
* clear_info - a function that clears information in the provided structure.
* @info: A pointer to a structure containing information about the program.
*/
void clear_info(info_t *info)
{
info->arg = NULL;
info->argv = NULL;
info->path = NULL;
info->argc = 0;
}
/**
* set_info - sets information in the provided info_t structure
* based on command line arguments.
* @info: A pointer to a structure containing information about the program.
* @av: An array of strings representing command line arguments.
*/
void set_info(info_t *info, char **av)
{
int x = 0;
info->fname = av[0];
if (info->arg)
{
info->argv = strtow(info->arg, " \t");
if (!info->argv)
{
info->argv = malloc(sizeof(char *) * 2);
if (info->argv)
{
info->argv[0] = _strdup(info->arg);
info->argv[1] = NULL;
}
}
for (x = 0; info->argv && info->argv[x]; x++)
;
info->argc = x;
replace_alias(info);
replace_vars(info);
}
}
/**
* free_info - function that frees memory allocated in the provided structure.
* @info: A pointer to a structure containing information about the program.
* @all: If true, frees all allocated resources; otherwise,
* only basic fields are cleared.
*/
void free_info(info_t *info, int all)
{
ffree(info->argv);
info->argv = NULL;
info->path = NULL;
if (all)
{
if (!info->cmd_buf)
free(info->arg);
if (info->env)
free_list(&(info->env));
if (info->history)
free_list(&(info->history));
if (info->alias)
free_list(&(info->alias));
ffree(info->environ);
info->environ = NULL;
bfree((void **)info->cmd_buf);
if (info->readfd > 2)
close(info->readfd);
_putchar(BUF_FLUSH);
}
}