diff --git a/commands/cd.php b/commands/cd.php new file mode 100644 index 0000000..5a8573e --- /dev/null +++ b/commands/cd.php @@ -0,0 +1,26 @@ +2){ + file_put_contents("recent.txt", "Too many arguments.
", FILE_APPEND); + return; + } + $pwd = file_get_contents("pwd.txt"); + if ($commands[1] == ".." || $commands[1] == NULL) { + $pwd = substr($pwd, 0, strrpos($pwd, "/")); + file_put_contents("pwd.txt", $pwd); + } else if (str_starts_with($commands[1], "/")) { + file_put_contents("pwd.txt", $commands[1]); + } else { + if (opendir("fileroot" . $pwd . "/" . $commands[1])) { + if ($pwd == "/") + file_put_contents("pwd.txt", $pwd . $commands[1]); + else { + file_put_contents("pwd.txt", $pwd . "/" . $commands[1]); + } + } else { + file_put_contents("recent.txt", "Directory does not exist.
", FILE_APPEND); + } + } +} +?> \ No newline at end of file diff --git a/commands/rm.php b/commands/rm.php new file mode 100644 index 0000000..763378a --- /dev/null +++ b/commands/rm.php @@ -0,0 +1,35 @@ +", FILE_APPEND); + } else { + file_put_contents("recent.txt", "Error deleting.
", FILE_APPEND); + } + } else { + file_put_contents("recent.txt", "Not a directory.
", FILE_APPEND); + } + } else { + if (is_dir("fileroot" . $pwd . "/" . $commands[1])) { + file_put_contents("recent.txt", "Is a directory.
Try rm -r
", FILE_APPEND); + } else { + if (unlink("fileroot" . $pwd . "/" . $commands[1])) { + file_put_contents("recent.txt", "File deleted.
", FILE_APPEND); + } else { + file_put_contents("recent.txt", "Error deleting.
", FILE_APPEND); + } + } + } +} +?> \ No newline at end of file diff --git a/static/help/rm.txt b/static/help/rm.txt new file mode 100644 index 0000000..e4dbcd1 --- /dev/null +++ b/static/help/rm.txt @@ -0,0 +1,3 @@ +rm - deletes a file or directory
+-r: delete directories and their contents recursively
+Example: rm -r /path/to/directory
\ No newline at end of file diff --git a/static/terminal.php/help.txt b/static/terminal.php/help.txt index 660de35..62a4fa1 100644 --- a/static/terminal.php/help.txt +++ b/static/terminal.php/help.txt @@ -2,5 +2,6 @@ clear - clears the screen
pwd - shows the working directory
mkdir - creates a new directory
cd - changes the working directory
+rm - deletes a file or directory
help - displays this help message
type help [command] to show more details
\ No newline at end of file diff --git a/terminal.php b/terminal.php index 3fe06f0..564fd42 100644 --- a/terminal.php +++ b/terminal.php @@ -50,25 +50,14 @@ if (!opendir("fileroot")) { } else { file_put_contents("recent.txt", "Directory already exists.
", FILE_APPEND); } + } else if ($commands[0] == "rm") { + include "commands/rm.php"; + rm($commands); } else if ($commands[0] == "clear" & sizeof($commands) == 1) { file_put_contents("recent.txt", ""); } else if ($commands[0] == "cd") { - if ($commands[1] == ".." || sizeof($commands) == 1) { - $pwd = substr($pwd, 0, strrpos($pwd, "/")); - file_put_contents("pwd.txt", $pwd); - } else if (str_starts_with($commands[1], "/")) { - file_put_contents("pwd.txt", $commands[1]); - } else { - if (opendir("fileroot" . $pwd . "/" . $commands[1])) { - if ($pwd == "/") - file_put_contents("pwd.txt", $pwd . $commands[1]); - else { - file_put_contents("pwd.txt", $pwd . "/" . $commands[1]); - } - } else { - file_put_contents("recent.txt", "Directory does not exist.
", FILE_APPEND); - } - } + include "commands/cd.php"; + cd($commands); } else if ($commands[0] == "help") { $help = file_get_contents("static/terminal.php/help.txt"); if (sizeof($commands) == 1) { @@ -79,7 +68,7 @@ if (!opendir("fileroot")) { file_put_contents("recent.txt", "Unknown command.
", FILE_APPEND); } } else { - file_put_contents("recent.txt", "Unknown command.
", FILE_APPEND); + file_put_contents("recent.txt", "Unknown command.
try 'help' or 'help [command]'
", FILE_APPEND); } echo ""; ?>