I am currently using a limited functionality shell using the Java programming language. Shell volume is also limited. The challenge is to model the Unix shell as much as possible.
When I implement the cd command, I refer to the "Basic console commands" page , it mentions that the CD is able to return to the last directory in which I am located with the "cd -" command.
Since only the interface with the method is provided to me public String execute(File presentWorkingDirectory, String stdin).
I would like to know if there is an API call from Java that I can get from the previous working directory, or if there is any implementation for this command?
I know that one of the simple implementation is to declare a variable to store the previous working directory. However, currently I have the shell itself (the one that takes the command with the parameters), and every time a command tool is executed, a new thread is created. Therefore, I do not think that the "main" thread is recommended to be stored in the previous working directory.
Update (March 6 -14) : Thanks for the offer! Now I discussed with the encoder for the shell and added an additional variable to store the previous working directory. The following is a sample code for sharing:
public class CdTool extends ATool implements ICdTool {
private static String previousDirectory;
public CdTool(final String[] arguments) {
super(arguments);
}
@Override
public String execute(final File workingDir, final String stdin) {
setStatusCode(0);
String output = "";
final String newDirectory;
if(this.args[0] == "-" && previousDirectory != null){
newDirectory = previousDirectory;
}
else{
newDirectory = this.args[0];
}
if( !newDirectory.equals(workingDir) &&
changeDirectory(newDirectory) == null){
setStatusCode(DIRECTORY_ERROR_CODE);
output = DIRECTORY_ERROR_MSG;
}
else{
previousDirectory = workingDir.getAbsolutePath();
output = changeDirectory(newDirectory).getAbsolutePath();
}
return output;
}
}
PS: Please note that this is not a complete implementation of the code, and this is not the full functionality of cd.
source
share