Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static private int execute_command(string[] cmd)
- {
- switch (Interpreter.is_keyword(cmd[0])) //cmd0=first token
- {
- case Keyword.ls: return execute_ls(cmd);
- case Keyword.cd: return execute_cd(cmd);
- case Keyword.cat: return execute_cat(cmd);
- case Keyword.touch: return execute_touch(cmd);
- case Keyword.rm: return execute_rm(cmd);
- case Keyword.rmdir: return execute_rmdir(cmd);
- case Keyword.mkdir: return execute_mkdir(cmd);
- case Keyword.pwd: return execute_pwd(cmd);
- case Keyword.clear: return execute_clear(cmd);
- case Keyword.whoami: return execute_whoami(cmd);
- default: return execute_unknown_cmd(cmd);
- }
- }
- static public int execute_input(string[][] input)
- {
- int final = 0;
- for(int i =0; i<input.Length;i++)
- {
- final = execute_command(input[i]);
- }
- return final;
- }
- static private int show_content(string entry)
- {
- int final = 0;
- if(File.Exists(entry))
- {
- Console.WriteLine(entry);
- return 0;
- }
- else
- if (Directory.Exists(entry))
- {
- foreach (string s in Directory.GetFiles(entry))
- {
- Console.WriteLine(s + "/");
- }
- final= 0;
- }
- else
- {
- final = 1;
- }
- return final;
- }
- static private int execute_ls(string[] cmd)
- {
- if (cmd.Length == 1)
- {
- return show_content("");
- }
- int final = 0;
- for (int i = 0; i < cmd.Length; i++)
- {
- final = show_content(cmd[i]);
- }
- return final;
- }
- static private int execute_cd(string[] cmd)
- {
- int result=0;
- if (Directory.Exists(path + "\\" + cmd[1]))
- {
- path += "\\" + cmd[1];
- result = 0;
- }
- if (File.Exists(path + "\\" + cmd[1]))
- {
- Console.WriteLine(cmd[1] + ":" + "Is not a directory");
- result = 1;
- }
- return result;
- }
- static private int execute_cat(string[] cmd)
- {
- int result = 0;
- for (int i = 0; i < cmd.Length; i++)
- {
- if (File.Exists(path + "\\" + cmd[i]))
- {
- StreamReader streamreader = new StreamReader(path + "\\" + cmd[i]);
- string a = streamreader.ReadToEnd();
- Console.WriteLine(a);
- streamreader.Close();
- result = 0;
- }
- else
- {
- Console.WriteLine("This is not a file");
- result = 1;
- }
- }
- return result;
- }
- static private int execute_touch(string[] cmd)
- {
- int result = 0;
- for (int i = 1; i < cmd.Length; i++)
- {
- if (File.Exists(cmd[i]))
- {
- Directory.SetLastAccessTime(cmd[i], DateTime.Now);
- result = 0;
- }
- else if (Directory.Exists(cmd[i]))
- {
- Directory.SetLastAccessTime(cmd[i], DateTime.Now);
- result = 0;
- }
- else
- {
- File.Create(cmd[i]);
- result = 0;
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement