Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1.  static private int execute_command(string[] cmd)
  2.         {
  3.  
  4.             switch (Interpreter.is_keyword(cmd[0])) //cmd0=first token
  5.             {
  6.                 case Keyword.ls: return execute_ls(cmd);
  7.                 case Keyword.cd: return execute_cd(cmd);
  8.                 case Keyword.cat: return execute_cat(cmd);
  9.                 case Keyword.touch: return execute_touch(cmd);
  10.                 case Keyword.rm: return execute_rm(cmd);
  11.                 case Keyword.rmdir: return execute_rmdir(cmd);
  12.                 case Keyword.mkdir: return execute_mkdir(cmd);
  13.                 case Keyword.pwd: return execute_pwd(cmd);
  14.                 case Keyword.clear: return execute_clear(cmd);
  15.                 case Keyword.whoami: return execute_whoami(cmd);
  16.                 default: return execute_unknown_cmd(cmd);
  17.             }
  18.         }
  19.         static public int execute_input(string[][] input)
  20.         {
  21.             int final = 0;
  22.             for(int i =0; i<input.Length;i++)
  23.             {
  24.              final = execute_command(input[i]);
  25.             }
  26.             return final;
  27.         }
  28.         static private int show_content(string entry)
  29.         {
  30.             int final = 0;
  31.             if(File.Exists(entry))
  32.             {
  33.                 Console.WriteLine(entry);
  34.                 return 0;
  35.             }
  36.             else
  37.             if (Directory.Exists(entry))
  38.             {
  39.                 foreach (string s in Directory.GetFiles(entry))
  40.                 {
  41.                     Console.WriteLine(s + "/");
  42.                 }
  43.                final= 0;
  44.             }
  45.             else
  46.             {
  47.                final = 1;
  48.             }
  49.             return final;
  50.  
  51.         }
  52.         static private int execute_ls(string[] cmd)
  53.         {
  54.             if (cmd.Length == 1)
  55.             {
  56.                 return show_content("");
  57.             }
  58.             int final = 0;
  59.             for (int i = 0; i < cmd.Length; i++)
  60.             {
  61.                 final = show_content(cmd[i]);
  62.             }
  63.             return final;
  64.         }
  65.         static private int execute_cd(string[] cmd)
  66.     {
  67.         int result=0;
  68.         if (Directory.Exists(path + "\\" + cmd[1]))
  69.         {
  70.             path += "\\" + cmd[1];
  71.             result = 0;
  72.            
  73.         }
  74.         if (File.Exists(path + "\\" + cmd[1]))
  75.         {
  76.             Console.WriteLine(cmd[1] + ":" + "Is not a directory");
  77.             result = 1;
  78.            
  79.         }
  80.         return result;
  81.     }
  82.         static private int execute_cat(string[] cmd)
  83.         {
  84.  
  85.             int result = 0;
  86.             for (int i = 0; i < cmd.Length; i++)
  87.             {
  88.                 if (File.Exists(path + "\\" + cmd[i]))
  89.                 {
  90.                     StreamReader streamreader = new StreamReader(path + "\\" + cmd[i]);
  91.                     string a = streamreader.ReadToEnd();
  92.                     Console.WriteLine(a);
  93.                     streamreader.Close();
  94.                     result = 0;
  95.                 }
  96.                 else
  97.                 {
  98.                     Console.WriteLine("This is not a file");
  99.                     result = 1;
  100.                 }
  101.             }
  102.             return result;
  103.         }
  104.         static private int execute_touch(string[] cmd)
  105.         {
  106.             int result = 0;
  107.             for (int i = 1; i < cmd.Length; i++)
  108.             {
  109.                 if (File.Exists(cmd[i]))
  110.                 {
  111.                     Directory.SetLastAccessTime(cmd[i], DateTime.Now);
  112.                     result = 0;
  113.                 }
  114.                 else if (Directory.Exists(cmd[i]))
  115.                 {
  116.                     Directory.SetLastAccessTime(cmd[i], DateTime.Now);
  117.                     result = 0;
  118.                 }
  119.                 else
  120.                 {
  121.                     File.Create(cmd[i]);
  122.                     result = 0;
  123.                 }
  124.             }
  125.             return result;
  126.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement