-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalt_functions.c
More file actions
144 lines (126 loc) · 3.17 KB
/
alt_functions.c
File metadata and controls
144 lines (126 loc) · 3.17 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
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 "shell.h"
/* Alt functions */
/**
* replace_string - Replace the content of an old string with a new string
*
* @old_str: Pointer to the old string (char)
* @new_str: New string to replace the old string (char)
*
* Return: 1 on successful replacement, 0 if old_str is NULL
*/
int replace_string(char **old_str, char *new_str)
{
/* Check if the old string is NULL */
if (old_str == NULL)
return (0);
/* Free the memory occupied by the old string */
free(*old_str);
/* Replace the old string with the new string */
*old_str = new_str;
return (1);
}
/**
* replace_variables - Replace variables in the command arguments
* with their values
*
* @data_use: Pointer to the data structure holding
* command-related information
*
* Return: 0 on successful replacement
*/
int replace_variables(data_t *data_use)
{
int i = 0;
node_t *node;
for (i = 0; data_use->argv[i]; i++)
{
if (data_use->argv[i][0] != '$' || !data_use->argv[i][1])
continue;
if (!compare_string(data_use->argv[i], "$?"))
{
/* Replace "$?" with the exit status converted to string */
replace_string(&(data_use->argv[i]),
duplicate_string(integer_convert(data_use->status, 10, 0)));
continue;
}
if (!compare_string(data_use->argv[i], "$$"))
{
/* Replace "$$" with the process ID converted to string */
replace_string(&(data_use->argv[i]),
duplicate_string(integer_convert(getpid(), 10, 0)));
continue;
}
node = node_needle(data_use->env, &data_use->argv[i][1], '=');
if (node)
{
/* Replace environment variables with their values */
replace_string(&(data_use->argv[i]),
duplicate_string(strchar_locate(node->str, '=') + 1));
continue;
}
/* If the variable is not recognized, replace it with an empty str */
replace_string(&data_use->argv[i], duplicate_string(""));
}
return (0);
}
/**
* delete_comments - Remove comments from a buffer
*
* @buffer: Pointer to the buffer containing the command line input (char)
*
* Return: None
*/
void delete_comments(char *buffer)
{
int i = 0;
while (buffer[i] != '\0')
{
if (buffer[i] == '#')
{
/* Check if '#' is at the beginning of the line or after a space */
if ((!i || buffer[i - 1] == ' '))
{
/* Remove the comment by null-terminating the string */
buffer[i] = '\0';
break;
}
}
i++;
}
}
/**
* print_decimal - Print an integer to a file descriptor
*
* @value: The integer value to be printed
* @file_descriptor: The file descriptor where the output is directed
*
* Return: The number of characters printed
*/
int print_decimal(int value, int file_descriptor)
{
int (*output_char)(char) = (file_descriptor == STDERR_FILENO)
? _eputchar : _putchar;
int count = 0, power_of_ten = 1, digit;
/* Handle negative numbers */
if (value < 0)
{
output_char('-');
count++;
value = -value;
}
/* Find the largest power of 10 that is less than or equal to the value */
while (power_of_ten <= value / 10)
{
power_of_ten *= 10;
}
/* Print each digit of the number */
while (power_of_ten > 0)
{
digit = value / power_of_ten;
output_char('0' + digit);
count++;
value %= power_of_ten;
power_of_ten /= 10;
}
return (count);
}