Skip to content

Commit

Permalink
optimize the code in terminal.php;
Browse files Browse the repository at this point in the history
  • Loading branch information
hanyixuanten committed Jul 15, 2024
1 parent 0279ae6 commit 36a7eec
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 25 deletions.
5 changes: 2 additions & 3 deletions commands/cd.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
function cd($commands)
{
if(sizeof($commands)>2){
file_put_contents("recent.txt", "Too many arguments.<br/>", FILE_APPEND);
if (sizeof($commands) > 2) {
file_put_contents("recent.txt", "Invalid number of arguments.<br/>", FILE_APPEND);
return;
}
$pwd = file_get_contents("pwd.txt");
Expand All @@ -23,4 +23,3 @@ function cd($commands)
}
}
}
?>
9 changes: 9 additions & 0 deletions commands/clear.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
function clear($commands)
{
if (sizeof($commands) == 1) {
file_put_contents("recent.txt", "");
} else {
file_put_contents("recent.txt", "Invalid number of arguments.<br/>", FILE_APPEND);
}
}
12 changes: 12 additions & 0 deletions commands/help.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
function help($commands)
{
$help = file_get_contents("static/terminal.php/help.txt");
if (sizeof($commands) == 1) {
file_put_contents("recent.txt", $help, FILE_APPEND);
} else if (sizeof($commands) == 2 && file_get_contents("static/help/$commands[1].txt") != NULL) {
file_put_contents("recent.txt", file_get_contents("static/help/$commands[1].txt"), FILE_APPEND);
} else {
file_put_contents("recent.txt", "Invalid number of arguments.<br/>", FILE_APPEND);
}
}
16 changes: 16 additions & 0 deletions commands/mkdir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
function mdir($commands)
{
$pwd = file_get_contents("pwd.txt");
if (sizeof($commands) == 2) {
if (!opendir("fileroot" . $pwd . "/" . $commands[1])) {
if (!mkdir("fileroot" . $pwd . "/" . $commands[1])) {
file_put_contents("recent.txt", "Error creating directory.No permissions?<br/>", FILE_APPEND);
}
} else {
file_put_contents("recent.txt", "Directory already exists.<br/>", FILE_APPEND);
}
} else {
file_put_contents("recent.txt", "Invalid number of arguments.<br/>", FILE_APPEND);
}
}
10 changes: 10 additions & 0 deletions commands/pwd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
function pwd($commands)
{
$pwd = file_get_contents("pwd.txt");
if (sizeof($commands) == 1) {
file_put_contents("recent.txt", $pwd . "<br/>", FILE_APPEND);
} else {
file_put_contents("recent.txt", "Invalid number of arguments.<br/>", FILE_APPEND);
}
}
5 changes: 3 additions & 2 deletions commands/rm.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function rm($commands)
} else {
file_put_contents("recent.txt", "Not a directory.<br/>", FILE_APPEND);
}
} else {
} else if (sizeof($commands) == 2) {
if (is_dir("fileroot" . $pwd . "/" . $commands[1])) {
file_put_contents("recent.txt", "Is a directory.<br/>Try rm -r<br/>", FILE_APPEND);
} else {
Expand All @@ -30,6 +30,7 @@ function rm($commands)
file_put_contents("recent.txt", "Error deleting.<br/>", FILE_APPEND);
}
}
} else {
file_put_contents("recent.txt", "Invalid number of arguments.<br/>", FILE_APPEND);
}
}
?>
31 changes: 11 additions & 20 deletions terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,24 @@
echo "<script>window.scrollTo(0,document.body.scrollHeight)</script>";
exit(0);
}
if ($commands[0] == "pwd" & sizeof($commands) == 1) {
file_put_contents("recent.txt", $pwd . "<br/>", FILE_APPEND);
} else if ($commands[0] == "mkdir" && sizeof($commands) == 2) {
if (!opendir("fileroot" . $pwd . "/" . $commands[1])) {
if (!mkdir("fileroot" . $pwd . "/" . $commands[1])) {
file_put_contents("recent.txt", "Error creating directory.No permissions?<br/>", FILE_APPEND);
}
} else {
file_put_contents("recent.txt", "Directory already exists.<br/>", FILE_APPEND);
}
if ($commands[0] == "pwd") {
include "commands/pwd.php";
pwd($commands);
} else if ($commands[0] == "mkdir") {
include "commands/mkdir.php";
mdir($commands);
} 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] == "clear") {
include "commands/clear.php";
clear($commands);
} else if ($commands[0] == "cd") {
include "commands/cd.php";
cd($commands);
} else if ($commands[0] == "help") {
$help = file_get_contents("static/terminal.php/help.txt");
if (sizeof($commands) == 1) {
file_put_contents("recent.txt", $help, FILE_APPEND);
} else if (sizeof($commands) == 2 && file_get_contents("static/help/$commands[1].txt") != NULL) {
file_put_contents("recent.txt", file_get_contents("static/help/$commands[1].txt"), FILE_APPEND);
} else {
file_put_contents("recent.txt", "Unknown command.<br/>", FILE_APPEND);
}
include "commands/help.php";
help($commands);
} else {
file_put_contents("recent.txt", "Unknown command.<br/>try 'help' or 'help [command]'<br/>", FILE_APPEND);
}
Expand Down

0 comments on commit 36a7eec

Please sign in to comment.