Question
Write a program simulating a cd Unix command that changes a current directory in an abstract file system. The simulated command takes two path strings
Write a program simulating a "cd" Unix command that changes a current directory in an abstract file system. The simulated command takes two path strings from the command line and prints either a new path or an error. The first path is a current directory. The second path is a new directory. To make it simple let's assume that a directory name can only contain alphanumeric characters. A single dot (".") indicates a current directory, and the two dots ("..") indicate a step to a previous directory, up from the current one. A single forward slash "/" indicates a root directory. Multiple consecutive slashes are treated as equivalent to a single one. The program needs to check that the new directory path is valid without relying on any OS system call such as chdir() for verification, to construct a new path, and print it out. Since it is a simulator, it should be OS-independent. Write a program using Java only. Automated unit tests are a plus.
Examples ('#' is a shell prompt in Unix): #mycd / abc /abc # mycd /abc/def ghi /abc/def/ghi # mycd /abc/def .. /abc #mycd /abc/def /abc /abc # mycd /abc/def /abc/klm /abc/klm # mycd /abc/def..... #mycd /abc/def../../.. # mycd /abc/def. /abc/def # mycd /abc/def ..klm ..klm: No such file or directory #mycd /abc/def III / #mycd /abc/def ......: No such file or directory #mycd /abc/def ../gh///../klm/. /abc/klmStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started