-
Notifications
You must be signed in to change notification settings - Fork 1
/
replaceVar.c
41 lines (39 loc) · 842 Bytes
/
replaceVar.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
#include "shell.h"
/**
* replaceVar - Replace environment var
* @arg: the argument following echo
* @status: previous code exit code
*
* Return: the argument remplaced
*/
char *replaceVar(char *arg, int status)
{
char *result = (char *)malloc(BUFSIZE);
char *env_var = _getenv(arg + 1);
char *str;
if (arg[0] == '$')
{
if (_strcmp(arg, "$?") == 0)
{
str = intToString(WEXITSTATUS(status));
_strncpy(result, str, BUFSIZE);
/*snprintf(result, BUFSIZE, "%d", WEXITSTATUS(status));*/
return (result);
}
else if (_strcmp(arg, "$$") == 0)
{
str = intToString(getpid());
_strncpy(result, str, BUFSIZE);
/*snprintf(result, BUFSIZE, "%d", getpid());*/
return (result);
}
else
{
if (env_var != NULL)
return (_strdup(env_var));
return (_strdup(""));
}
}
else
return (_strdup(arg));
}