From 36a7eecf9420391edc8a9262953e1492dd6f9c54 Mon Sep 17 00:00:00 2001 From: hanyixuanten Date: Mon, 15 Jul 2024 09:38:43 +0800 Subject: [PATCH] optimize the code in terminal.php; --- commands/cd.php | 5 ++--- commands/clear.php | 9 +++++++++ commands/help.php | 12 ++++++++++++ commands/mkdir.php | 16 ++++++++++++++++ commands/pwd.php | 10 ++++++++++ commands/rm.php | 5 +++-- terminal.php | 31 +++++++++++-------------------- 7 files changed, 63 insertions(+), 25 deletions(-) create mode 100644 commands/clear.php create mode 100644 commands/help.php create mode 100644 commands/mkdir.php create mode 100644 commands/pwd.php diff --git a/commands/cd.php b/commands/cd.php index 5a8573e..f522b57 100644 --- a/commands/cd.php +++ b/commands/cd.php @@ -1,8 +1,8 @@ 2){ - file_put_contents("recent.txt", "Too many arguments.
", FILE_APPEND); + if (sizeof($commands) > 2) { + file_put_contents("recent.txt", "Invalid number of arguments.
", FILE_APPEND); return; } $pwd = file_get_contents("pwd.txt"); @@ -23,4 +23,3 @@ function cd($commands) } } } -?> \ No newline at end of file diff --git a/commands/clear.php b/commands/clear.php new file mode 100644 index 0000000..d7b030d --- /dev/null +++ b/commands/clear.php @@ -0,0 +1,9 @@ +", FILE_APPEND); + } +} diff --git a/commands/help.php b/commands/help.php new file mode 100644 index 0000000..d87bbf7 --- /dev/null +++ b/commands/help.php @@ -0,0 +1,12 @@ +", FILE_APPEND); + } +} diff --git a/commands/mkdir.php b/commands/mkdir.php new file mode 100644 index 0000000..4dbaa8b --- /dev/null +++ b/commands/mkdir.php @@ -0,0 +1,16 @@ +", FILE_APPEND); + } + } else { + file_put_contents("recent.txt", "Directory already exists.
", FILE_APPEND); + } + } else { + file_put_contents("recent.txt", "Invalid number of arguments.
", FILE_APPEND); + } +} diff --git a/commands/pwd.php b/commands/pwd.php new file mode 100644 index 0000000..c5029cb --- /dev/null +++ b/commands/pwd.php @@ -0,0 +1,10 @@ +", FILE_APPEND); + } else { + file_put_contents("recent.txt", "Invalid number of arguments.
", FILE_APPEND); + } +} diff --git a/commands/rm.php b/commands/rm.php index 763378a..a5cdd71 100644 --- a/commands/rm.php +++ b/commands/rm.php @@ -20,7 +20,7 @@ function rm($commands) } else { file_put_contents("recent.txt", "Not a directory.
", FILE_APPEND); } - } else { + } else if (sizeof($commands) == 2) { if (is_dir("fileroot" . $pwd . "/" . $commands[1])) { file_put_contents("recent.txt", "Is a directory.
Try rm -r
", FILE_APPEND); } else { @@ -30,6 +30,7 @@ function rm($commands) file_put_contents("recent.txt", "Error deleting.
", FILE_APPEND); } } + } else { + file_put_contents("recent.txt", "Invalid number of arguments.
", FILE_APPEND); } } -?> \ No newline at end of file diff --git a/terminal.php b/terminal.php index 564fd42..226f3a3 100644 --- a/terminal.php +++ b/terminal.php @@ -40,33 +40,24 @@ if (!opendir("fileroot")) { echo ""; exit(0); } - if ($commands[0] == "pwd" & sizeof($commands) == 1) { - file_put_contents("recent.txt", $pwd . "
", 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?
", FILE_APPEND); - } - } else { - file_put_contents("recent.txt", "Directory already exists.
", 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.
", FILE_APPEND); - } + include "commands/help.php"; + help($commands); } else { file_put_contents("recent.txt", "Unknown command.
try 'help' or 'help [command]'
", FILE_APPEND); }