-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
189 lines (156 loc) · 6.71 KB
/
Copy pathMain.java
File metadata and controls
189 lines (156 loc) · 6.71 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.util.Scanner;
import java.util.Arrays;
import java.io.*;
public class Main {
public static void println(String str) {
System.out.println(str);
}
public static void print(String str) {
System.out.print(str);
}
public static void notFound(String str) {
System.out.println(str + ": not found");
}
public static void debugOn(String str) {
System.out.printf("%s%n", str);
}
public static String getCwd(String command) throws Exception {
File pwd = new File(command);
return pwd.getCanonicalFile().getParent();
}
public static String getPath(String typeString) {
// Boolean pathFlag = Boolean.FALSE;
String path = System.getenv("PATH");
String typeStringLoc = typeString + ": not found";
// String typeStringLocdebug = "";
if (path != null || !(path.equalsIgnoreCase("")) || typeString != null) {
// System.out.println("INSIDE PATH LIST TYPE ");
// System.out.printf("HELLO %s", path);
String[] paths = path.split(":");
// System.out.printf(Arrays.toString(paths));
// System.out.println("BEFORE PRINTING PATHS ");
for (String p : paths) {
// pathFlag = Boolean.FALSE;
// System.out.printf("%s%n", p);
// System.out.println("FILES ->" + p);
File file = new File(p);
File[] files = file.listFiles();
// System.out.printf(Arrays.toString(files));
if (files != null) {
// System.out.println(Arrays.asList(files).contains(typeString));
for (File f : files) {
// System.out.printf("%s%n", f);
String fstr = f.toString();
String subFstr = fstr.substring(fstr.lastIndexOf(File.separator) + 1);
if (subFstr.equalsIgnoreCase(typeString)) {
// pathFlag = Boolean.TRUE;
typeStringLoc = typeString + " is " + f;
break;
}
}
}
}
}
return typeStringLoc;
}
public static String getPlainPath(String typeString) {
// Boolean pathFlag = Boolean.FALSE;
String path = System.getenv("PATH");
String typeStringLoc = typeString + ": not found";
// String typeStringLocdebug = "";
if (path != null || !(path.equalsIgnoreCase("")) || typeString != null) {
// System.out.println("INSIDE PATH LIST TYPE ");
// System.out.printf("HELLO %s", path);
String[] paths = path.split(":");
// System.out.printf(Arrays.toString(paths));
// System.out.println("BEFORE PRINTING PATHS ");
for (String p : paths) {
// pathFlag = Boolean.FALSE;
// System.out.printf("%s%n", p);
// System.out.println("FILES ->" + p);
File file = new File(p);
File[] files = file.listFiles();
// System.out.printf(Arrays.toString(files));
if (files != null) {
// System.out.println(Arrays.asList(files).contains(typeString));
for (File f : files) {
// System.out.printf("%s%n", f);
String fstr = f.toString();
String subFstr = fstr.substring(fstr.lastIndexOf(File.separator) + 1);
if (subFstr.equalsIgnoreCase(typeString)) {
// pathFlag = Boolean.TRUE;
typeStringLoc = f.toString();
break;
}
}
}
}
}
return typeStringLoc;
}
public static void executeCommand(String input) throws Exception {
// pass the entire command because the command might come with its own arguments
String command = input.split(" ")[0];
// String commandArgs = input.split(" ")[1];
// debugOn(command);
// debugOn(commandArgs);
String path = getPlainPath(command);
File fPath = new File(path);
// debugOn(path);
if (command.equalsIgnoreCase("pwd")) {
String cmd = "pwd";
String printPWD = getCwd(cmd);
System.out.println(printPWD);
} else if (!fPath.isFile()) {
println(command + ": command not found");
} else {
// println("HELLO AM HERE");
String commandArgs = input.split(" ")[1];
// println("IS TERE AN ARG");
// println(commandArgs);
// debugOn(command);
// debugOn(commandArgs);
String path0 = getPath(command);
File fPath0 = new File(path0);
String fullPath = (!commandArgs.equalsIgnoreCase("")) ? command + " " + commandArgs
: path + input.substring(command.length());
// debugOn(fullPath);
// System.out.println(Arrays.toString(fullPath.split(" ")));
Process p = Runtime.getRuntime().exec(fullPath.split(" "));
p.getInputStream().transferTo(System.out);
}
}
public static void main(String[] args) throws Exception {
String[] built_in_commands = { "echo", "exit", "type", "pwd" };
Scanner scanner = new Scanner(System.in);
while (true) {
print("$ ");
String input = scanner.nextLine();
if (!input.equalsIgnoreCase("exit 0")) {
if (input.startsWith("echo")) {
println(input.substring("echo".length() + 1, input.length()));
} else if (input.startsWith("type")) {
String typeString = input.substring("type".length() + 1, input.length());
if (Arrays.asList(built_in_commands).contains(typeString)) {
println(typeString + " is a shell builtin");
// break;
} else {
// debugOn("BEFORE PATH");
String typePath = getPath(typeString);
println(typePath);
// debugOn("AFTER PATH" + typePath);
}
} else {
// debugOn("AM I HERE");
// notFound(input);
// String typePath = getPath(input);
// System.out.println(typePath);
executeCommand(input);
}
} else {
break;
}
// debugOn(input);
}
}
}