From d6912e149dc93c13d023c5bc047a8dd647626352 Mon Sep 17 00:00:00 2001 From: HarushiSingla Date: Mon, 13 Oct 2025 12:08:08 +0530 Subject: [PATCH 1/2] fix: prevent 'cd ..' from moving above root directory --- .../java/com/mycmd/commands/CdCommand.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/mycmd/commands/CdCommand.java b/src/main/java/com/mycmd/commands/CdCommand.java index bb9c3a3..ced2516 100644 --- a/src/main/java/com/mycmd/commands/CdCommand.java +++ b/src/main/java/com/mycmd/commands/CdCommand.java @@ -7,14 +7,36 @@ public class CdCommand implements Command { @Override public void execute(String[] args, ShellContext context) { + // No argument -> print current directory if (args.length == 0) { System.out.println(context.getCurrentDir().getAbsolutePath()); return; } - File newDir = new File(args[0]); + + String dir = args[0]; + + // Normalize "cd.." without space to ".." + if (dir.equals("cd..")) { + dir = ".."; + } + + File newDir = new File(dir); + + // If relative path, resolve from current directory if (!newDir.isAbsolute()) { - newDir = new File(context.getCurrentDir(), args[0]); + newDir = new File(context.getCurrentDir(), dir); + } + + // Handle "cd .." when already at root + if (dir.equals("..")) { + File parent = context.getCurrentDir().getParentFile(); + if (parent == null) { + System.out.println("Already at the root directory."); + return; + } + newDir = parent; } + if (newDir.exists() && newDir.isDirectory()) { context.setCurrentDir(newDir); } else { From b363c9a23c2224bad772163eefddaf39500f7189 Mon Sep 17 00:00:00 2001 From: HarushiSingla Date: Mon, 13 Oct 2025 23:05:49 +0530 Subject: [PATCH 2/2] fix: clean up CdCommand logic and remove redundant code --- .../java/com/mycmd/commands/CdCommand.java | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/mycmd/commands/CdCommand.java b/src/main/java/com/mycmd/commands/CdCommand.java index ced2516..1dc8b12 100644 --- a/src/main/java/com/mycmd/commands/CdCommand.java +++ b/src/main/java/com/mycmd/commands/CdCommand.java @@ -7,27 +7,16 @@ public class CdCommand implements Command { @Override public void execute(String[] args, ShellContext context) { - // No argument -> print current directory + // If no argument, print current directory if (args.length == 0) { System.out.println(context.getCurrentDir().getAbsolutePath()); return; } String dir = args[0]; + File newDir; - // Normalize "cd.." without space to ".." - if (dir.equals("cd..")) { - dir = ".."; - } - - File newDir = new File(dir); - - // If relative path, resolve from current directory - if (!newDir.isAbsolute()) { - newDir = new File(context.getCurrentDir(), dir); - } - - // Handle "cd .." when already at root + // Handle "cd .." (go to parent directory) if (dir.equals("..")) { File parent = context.getCurrentDir().getParentFile(); if (parent == null) { @@ -35,8 +24,14 @@ public void execute(String[] args, ShellContext context) { return; } newDir = parent; + } else { + newDir = new File(dir); + if (!newDir.isAbsolute()) { + newDir = new File(context.getCurrentDir(), dir); + } } + // Change directory if valid if (newDir.exists() && newDir.isDirectory()) { context.setCurrentDir(newDir); } else {