add command:rm;optimize cd

This commit is contained in:
2024-07-15 08:46:15 +08:00
parent 94a5dadbec
commit 0279ae6493
5 changed files with 71 additions and 17 deletions
+26
View File
@@ -0,0 +1,26 @@
<?php
function cd($commands)
{
if(sizeof($commands)>2){
file_put_contents("recent.txt", "Too many arguments.<br/>", 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.<br/>", FILE_APPEND);
}
}
}
?>
+35
View File
@@ -0,0 +1,35 @@
<?php
function deleteDirectory($dir)
{
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? deleteDirectory("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
function rm($commands)
{
$pwd = file_get_contents("pwd.txt");
if ($commands[1] == "-r") {
if (is_dir("fileroot" . $pwd . "/" . $commands[2])) {
if (deleteDirectory("fileroot" . $pwd . "/" . $commands[2])) {
file_put_contents("recent.txt", "Directory deleted.<br/>", FILE_APPEND);
} else {
file_put_contents("recent.txt", "Error deleting.<br/>", FILE_APPEND);
}
} else {
file_put_contents("recent.txt", "Not a directory.<br/>", FILE_APPEND);
}
} else {
if (is_dir("fileroot" . $pwd . "/" . $commands[1])) {
file_put_contents("recent.txt", "Is a directory.<br/>Try rm -r<br/>", FILE_APPEND);
} else {
if (unlink("fileroot" . $pwd . "/" . $commands[1])) {
file_put_contents("recent.txt", "File deleted.<br/>", FILE_APPEND);
} else {
file_put_contents("recent.txt", "Error deleting.<br/>", FILE_APPEND);
}
}
}
}
?>
+3
View File
@@ -0,0 +1,3 @@
rm - deletes a file or directory<br/>
-r: delete directories and their contents recursively<br/>
Example: rm -r /path/to/directory<br/>
+1
View File
@@ -2,5 +2,6 @@ clear - clears the screen<br/>
pwd - shows the working directory<br/>
mkdir - creates a new directory<br/>
cd - changes the working directory<br/>
rm - deletes a file or directory<br/>
help - displays this help message<br/>
type help [command] to show more details<br/>
+6 -17
View File
@@ -50,25 +50,14 @@ if (!opendir("fileroot")) {
} else {
file_put_contents("recent.txt", "Directory already exists.<br/>", 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.<br/>", 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.<br/>", FILE_APPEND);
}
} else {
file_put_contents("recent.txt", "Unknown command.<br/>", FILE_APPEND);
file_put_contents("recent.txt", "Unknown command.<br/>try 'help' or 'help [command]'<br/>", FILE_APPEND);
}
echo "<script>window.location.href=\"terminal.php\";</script>";
?>