-
Notifications
You must be signed in to change notification settings - Fork 0
/
18-replace_var.c
116 lines (98 loc) · 2.15 KB
/
18-replace_var.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
#include "main.h"
char *replace_implement(char *cmd, int stat, int pid);
/**
* int_to_str - Converts an integer to a string.
* @num: The integer to be converted.
* @str: The string to store the result.
*/
void int_to_str(int num, char *str)
{
snprintf(str, 32, "%d", num);
}
/**
* replacer - Replaces occurrences of a substring with another substring.
* @input: The input string.
* @prev: The substring to be replaced.
* @present: The new substring.
* Return: A newly allocated string with replacements.
*/
char *replacer(const char *input, const char *prev, const char *present)
{
int a, count = 0;
int prev_len = gb_strlen(prev), present_len = gb_strlen(present);
char *output;
for (a = 0; input[a] != '\0'; a++)
{
if (strstr(&input[a], prev) == &input[a])
{
++count;
a += prev_len - 1;
}
}
output = (char *)malloc(a + count * (present_len - prev_len) + 1);
a = 0;
while (*input)
{
if (strstr(input, prev) == input)
{
gb_strcpy(&output[a], present);
a += present_len;
input += prev_len;
}
else
output[a++] = *input;
}
output[a] = '\0';
return (output);
}
/**
* replace_implement - Replaces special variables in a command string.
* @cmd: The command string.
* @stat: The wait status.
* @pid: The child process ID.
* Return: A newly allocated string with replacements.
*/
char *replace_implement(char *cmd, int stat, int pid)
{
int a, len, output_len;
char *output, input_stat[32], input_pid[32];
len = gb_strlen(cmd);
output_len = len + 30;
output = (char *)malloc(output_len);
if (output == NULL)
{
perror("error");
exit(EXIT_FAILURE);
}
output[0] = '\0';
if (gb_strcmp(cmd, "&&") != 0 && gb_strcmp(cmd, "||") != 0)
{
for (a = 0; a < len; a++)
{
if (cmd[a] == '$' && cmd[a + 1] == '?')
{
int_to_str(stat, input_stat);
strcat(output, input_stat);
a++;
}
else if (cmd[a] == '$' && cmd[a + 1] == '$')
{
int_to_str(pid, input_pid);
strcat(output, input_pid);
a++;
}
else
{
/**output = strncat(output, &cmd[a], 1);**/
*output++ = cmd[a];
*output = '\0';
}
}
}
else
{
gb_strcpy(output, cmd);
}
printf("%s\n", output);
return (output);
}