-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_variables.c
More file actions
53 lines (43 loc) · 794 Bytes
/
replace_variables.c
File metadata and controls
53 lines (43 loc) · 794 Bytes
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
#include <stdlib.h>
#include "main.h"
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
/**
* replace_variables - replaces variables in a shell program denoted by $
* @args: array strings containing the command and its arguments
* @status: exit status of previously executed program
*
* Return:
*/
void replace_variables(char **args, int status)
{
int i = 1;
char *str;
if (!args)
return;
while (args[i])
{
if (args[i][0] == '$' && args[i][1])
{
switch (args[i][1])
{
case '$':
str = _itoa((int) getpid());
break;
case '?':
str = _itoa(WEXITSTATUS(status));
break;
default:
str = _getenv(&args[i][1]);
break;
}
free(args[i]);
if (str)
args[i] = str;
else
args[i] = _strdup("");
}
i++;
}
}