-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo_functions.c
More file actions
118 lines (97 loc) · 2.67 KB
/
info_functions.c
File metadata and controls
118 lines (97 loc) · 2.67 KB
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
#include "shell.h"
/* Infos functions */
/**
* set_info - Set information in the data structure based
* on command line arguments
*
* @data_use: Pointer to the data structure containing shell information
* @av: Array of strings representing command line arguments (char)
*
* Return: None
*/
void set_info(data_t *data_use, char **av)
{
int i = 0;
/* Set the filename from command line arguments */
data_use->filename = av[0];
/* Parse the argument to create argv */
if (data_use->arg != 0)
{
data_use->argv = split_string(data_use->arg, " \t");
/* Handle the case when split_string fails */
if (data_use->argv == 0)
{
data_use->argv = malloc(2 * sizeof(char *));
if (data_use->argv != 0)
{
data_use->argv[0] = duplicate_string(data_use->arg);
data_use->argv[1] = NULL;
}
}
/* Count the number of arguments in argv */
while (data_use->argv && data_use->argv[i])
i++;
/* set argc based on the number of arguments */
data_use->argc = i;
/* Replace aliases and variables in argv */
replace_alias(data_use);
replace_variables(data_use);
}
}
/**
* free_info - Free memory allocated for shell information
* in the data structure
*
* @data_use: Pointer to the data structure containing shell information
* @global_set: Flag to indicate whether global data should be freed (int)
*
* Return : None
*/
void free_info(data_t *data_use, int global_set)
{
/* Free memory allocated for argv */
free_string(data_use->argv);
data_use->argv = NULL;
/* Set path to NULL */
data_use->path = NULL;
/* Free resources related to global_set */
if (global_set)
{
/* Free argument if buffer_command is not set */
if (data_use->buffer_command == 0)
free(data_use->arg);
/* Free environment variables list */
if (data_use->env != 0)
free_list(&(data_use->env));
/* Free history list */
if (data_use->history != 0)
free_list(&(data_use->history));
/* Free alias list */
if (data_use->alias != 0)
free_list(&(data_use->alias));
/* Free environment string */
free_string(data_use->environ);
data_use->environ = NULL;
/* Free buffer_command and close file descriptor */
free_address((void **)data_use->buffer_command);
if (data_use->read_file_descriptor > 2)
close(data_use->read_file_descriptor);
/* Flush the buffer */
_putchar(BUFFER_FLUSH);
}
}
/**
* clear_info - Clear information in the data structure
*
* @data_use: Pointer to the data structure containing shell information
*
* Return: None
*/
void clear_info(data_t *data_use)
{
/* Set argument, argc, argv, and path to NULL or zero */
data_use->arg = NULL;
data_use->argc = 0;
data_use->argv = NULL;
data_use->path = NULL;
}