blob: f33005045bd657216dc914f85fc0f2ff9fd03718 (
plain)
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
|
#include <stdio.h>
#include <string.h>
#include "builtins.h"
static void print_echo(struct string **args, int escapes, int newline, int i)
{
while (args[i] != NULL)
{
if (escapes)
{
size_t j = 0;
while (j < args[i]->length)
{
if (args[i]->data[j] == '\\' && args[i]->data[j] != '\0')
{
j += 1;
switch (args[i]->data[j])
{
case 'n':
putchar('\n');
break;
case 't':
putchar('\t');
break;
case '\\':
putchar('\\');
break;
default:
putchar('\\');
putchar(args[i]->data[j]);
break;
}
j += 1;
}
else
{
putchar(args[i]->data[j]);
j += 1;
}
}
}
else
{
fputs(args[i]->data, stdout);
}
if (args[i + 1] != NULL)
{
putchar(' ');
}
i += 1;
}
if (newline)
{
putchar('\n');
}
}
int echo(struct string **args)
{
int newline = 1;
int escapes = 0;
size_t i = 0;
if (args[0] == NULL)
{
putchar('\n');
return 0;
}
while (args[i] != NULL && args[i]->data[0] == '-')
{
if (args[i]->length == 2 && args[i]->data[1] == 'n')
{
newline = 0;
i += 1;
}
else if (args[i]->length == 2 && args[i]->data[1] == 'e')
{
escapes = 1;
i += 1;
}
else if (args[i]->length == 2 && args[i]->data[1] == 'E')
{
escapes = 0;
i += 1;
}
else
{
break;
}
}
print_echo(args, escapes, newline, i);
fflush(stdout);
return 0;
}
|