-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.c
161 lines (147 loc) · 3.63 KB
/
helper.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* File: helper.c
* Auth: Alex Yu
* Brennan D Baraban
*/
#include "shell.h"
void free_args(char **args, char **front);
char *get_pid(void);
char *get_env_value(char *beginning, int len);
void variable_replacement(char **args, int *exe_ret);
/**
* free_args - Frees up memory taken by args.
* @args: A null-terminated double pointer containing commands/arguments.
* @front: A double pointer to the beginning of args.
*/
void free_args(char **args, char **front)
{
size_t i;
for (i = 0; args[i] || args[i + 1]; i++)
free(args[i]);
free(front);
}
/**
* get_pid - Gets the current process ID.
* Description: Opens the stat file, a space-delimited file containing
* information about the current process. The PID is the
* first word in the file. The function reads the PID into
* a buffer and replace the space at the end with a \0 byte.
*
* Return: The current process ID or NULL on failure.
*/
char *get_pid(void)
{
size_t i = 0;
char *buffer;
ssize_t file;
file = open("/proc/self/stat", O_RDONLY);
if (file == -1)
{
perror("Cant read file");
return (NULL);
}
buffer = malloc(120);
if (!buffer)
{
close(file);
return (NULL);
}
read(file, buffer, 120);
while (buffer[i] != ' ')
i++;
buffer[i] = '\0';
close(file);
return (buffer);
}
/**
* get_env_value - Gets the value corresponding to an environmental variable.
* @beginning: The environmental variable to search for.
* @len: The length of the environmental variable to search for.
*
* Return: If the variable is not found - an empty string.
* Otherwise - the value of the environmental variable.
*
* Description: Variables are stored in the format VARIABLE=VALUE.
*/
char *get_env_value(char *beginning, int len)
{
char **var_addr;
char *replacement = NULL, *temp, *var;
var = malloc(len + 1);
if (!var)
return (NULL);
var[0] = '\0';
_strncat(var, beginning, len);
var_addr = _getenv(var);
free(var);
if (var_addr)
{
temp = *var_addr;
while (*temp != '=')
temp++;
temp++;
replacement = malloc(_strlen(temp) + 1);
if (replacement)
_strcpy(replacement, temp);
}
return (replacement);
}
/**
* variable_replacement - Handles variable replacement.
* @line: A double pointer containing the command and arguments.
* @exe_ret: A pointer to the return value of the last executed command.
*
* Description: Replaces $$ with the current PID, $? with the return value
* of the last executed program, and envrionmental variables
* preceded by $ with their corresponding value.
*/
void variable_replacement(char **line, int *exe_ret)
{
int j, k = 0, len;
char *replacement = NULL, *old_line = NULL, *new_line;
old_line = *line;
for (j = 0; old_line[j]; j++)
{
if (old_line[j] == '$' && old_line[j + 1] &&
old_line[j + 1] != ' ')
{
if (old_line[j + 1] == '$')
{
replacement = get_pid();
k = j + 2;
}
else if (old_line[j + 1] == '?')
{
replacement = _itoa(*exe_ret);
k = j + 2;
}
else if (old_line[j + 1])
{
/* extract the variable name to search for */
for (k = j + 1; old_line[k] &&
old_line[k] != '$' &&
old_line[k] != ' '; k++)
;
len = k - (j + 1);
replacement = get_env_value(&old_line[j + 1], len);
}
new_line = malloc(j + _strlen(replacement)
+ _strlen(&old_line[k]) + 1);
if (!line)
return;
new_line[0] = '\0';
_strncat(new_line, old_line, j);
if (replacement)
{
_strcat(new_line, replacement);
free(replacement);
replacement = NULL;
}
_strcat(new_line, &old_line[k]);
free(old_line);
*line = new_line;
old_line = new_line;
j = -1;
}
}
}