-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute_echo.c
55 lines (54 loc) · 855 Bytes
/
execute_echo.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
#include "shell.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void execute_echo (char* args)
{
for (int i=0; args[i]; ++i)
if (args[i] == '>')
{
args[i] = '\0';
break;
}
for (int i=0, j; args[i];)
{
if (isspace(args[i]))
{
++i;
continue;
}
if (args[i] == '$')
{
++i;
j = 0;
char s[1024];
while (args[i] && !isspace(args[i]))
s[j++] = args[i++];
s[j] = '\0';
char *ret = getenv(s);
if (ret == NULL)
fprintf(stderr, "Error: variable \"%s\" does not exist\n", s);
else printf("%s ", ret);
continue;
}
if (args[i] == '"')
{
++i;
while (args[i] && args[i] != '"')
{
printf("%c", args[i]);
++i;
}
printf(" ");
++i;
continue;
}
while (args[i] && !isspace(args[i]))
{
printf("%c", args[i]);
++i;
}
printf(" ");
}
printf("\n");
}