Question

I am currently implementing a shell with limited functionality using Java programming language. The scope of the shell has restricted requirement too. The task is to model a Unix shell as much as I can.

When I am implementing the cd command option, I reference a Basic Shell Commands page, it mentions that a cd is able to go back to the last directory I am in with the command "cd -".

As I am given only a interface with the method public String execute(File presentWorkingDirectory, String stdin).

I will like to know if there is API call from Java which I can retrieve the previous working directory, or if there any implementation for this command?

I know one of the simple implementation is to declare a variable to store the previous working directory. However I am currently having the shell itself (the one that take in the command with options), and each time a command tool is executed, a new thread is created. Hence I do not think it is advisable for the "main" thread to store the previous working directory.

Update (6-Mar-'14): Thank for the suggestion! I have now discussed with the coder for shell, and have added an additional variable to store the previous working directory. Below is the sample code for sharing:

public class CdTool extends ATool implements ICdTool {
    private static String previousDirectory;

    //Constructor
    /**
     * Create a new CdTool instance so that it represents an unexecuted cd command. 
     * 
     * @param arguments
     *  the argument that is to be passed in to execute the command
     */
    public CdTool(final String[] arguments) {
        super(arguments);
    }

    /**
     * Executes the tool with arguments provided in the constructor
     * 
     * @param workingDir
     *            the current working directory path
     * 
     * @param stdin
     *            the additional input from the stdin
     * 
     * @return the message to be shown on the shell, null if there is no error
     *         from the command
     */
    @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;
}

}

P.S: Please note that this is not the full implementation of the code, and this is not the full functionality of cd.

Was it helpful?

Solution

Real shell (at least Bash) shell stores current working directory path in PWD environment variable and old working directory path in OLDPWD. Rewriting PWD does not change your working directory, but rewriting OLDPWD really changes where cd - will take you.

Try this:

cd /tmp
echo "$OLDPWD"          # /home/palec
export OLDPWD='/home'
cd -                    # changes working directory to /home

I don’t know how you implement the shell functionality (namely how you represent current working directory; usually it’s an inherent property of the process, implemented by the kernel) but I think that you really have to keep the old working directory in an extra variable.

By the way shell also forks for each command executed (except for the internal ones). Current working directory is a property of a process. When a command is started, it can change its inner current working directory, but it does not affect the shell’s one. Only cd command (which is internal) can change shell’s current working directory.

OTHER TIPS

If you want to keep more than one working directory just create a LinkedList where you add each new presentWorkingDirectory at the and and if you want to return use linkedList.popLast to get the last workingDirectory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top