-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCat.java
36 lines (31 loc) · 928 Bytes
/
Cat.java
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
package builtins;
import bntler.BashCommand;
import parser.Bash;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class Cat {
public static String execute(BashCommand command, String stdin) throws IOException {
List<String> args = command.parts();
int i = 1;
while (i < args.size()) {
if (!args.get(i).startsWith("-")) {
break;
}
i++;
}
if (i == args.size()) {
return stdin;
}
StringBuilder concatenated = new StringBuilder();
while (i < args.size()) {
Path path = Paths.get(args.get(i));
concatenated.append(Files.readString(path, StandardCharsets.UTF_8));
i++;
}
return concatenated.toString();
}
}