-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandleVariable.c
46 lines (44 loc) · 957 Bytes
/
handleVariable.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
#include "shell.h"
/**
* handleVar - Replace spacial variable in the command
* @command: The command entered by user
* @status: The status of the last process executed
*
* Return: updated cmd with var replacements
*/
char *handleVar(char *command, int status)
{
char *updatedCommand = (char *)malloc(BUFSIZE);
char *temp;
int i = 0;
while (*command != '\0')
{
if (*command == '$')
{
if (*(command + 1) == '?' || *(command + 1) == '$')
{
/* Replace $? with the exit code of the last process */
temp = intToString(WEXITSTATUS(status));
_strncpy(updatedCommand + i, temp, BUFSIZE - i);
i += strlen(temp);
free(temp);
command += 2;
}
else
{
/* If isn't a special var */
updatedCommand[i] = *command;
i++;
command++;
}
}
else
{
updatedCommand[i] = *command;
i++;
command++;
}
}
updatedCommand[i] = '\0'; /* Null end the updated command */
return (updatedCommand);
}