Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. #include <sys/wait.h>
  2.  
  3. #include <string.h>
  4.  
  5. #include <stdlib.h>
  6.  
  7. #include <stdio.h>
  8.  
  9. #include <unistd.h>
  10.  
  11. #include <dirent.h>
  12.  
  13.  
  14.  
  15. int parse(char* st, char** toks)
  16.  
  17. {
  18.  
  19.   char* tok = strtok(st," \t");
  20.  
  21.   int t = 0;
  22.  
  23.   while((tok != NULL) && (t < 10))
  24.  
  25.   {
  26.  
  27.     toks[t] = tok;
  28.  
  29.     t++;
  30.  
  31.     tok = strtok(NULL," \t");
  32.  
  33.   }
  34.  
  35.   toks[t] = NULL;
  36.  
  37.   return t;
  38.  
  39. }
  40.  
  41.  
  42.  
  43. void f_echo(char* st)
  44.  
  45. {
  46.  
  47.   write(1,st,strlen(st));
  48.  
  49.   //write(1,"\n",1);
  50.  
  51. }
  52.  
  53.  
  54.  
  55. void f_cd(char* st)
  56.  
  57. {
  58.  
  59.   chdir(st);
  60.  
  61. }
  62.  
  63.  
  64.  
  65. void f_pwd()
  66.  
  67. {
  68.  
  69.   char dir[50];
  70.  
  71.   getcwd(dir, 50);
  72.   write(1,"Current directory")
  73.  
  74.   write(1,dir,strlen(dir));
  75.  
  76.   write(1,"\n",1);
  77.  
  78. }
  79.  
  80.  
  81.  
  82. void f_ls(char* st)
  83.  
  84. {
  85.  
  86.   DIR* dir;
  87.  
  88.   struct dirent* de;
  89.  
  90.  
  91.  
  92.   dir = opendir(st);
  93.  
  94.   while((de = readdir(dir)) != NULL)
  95.  
  96.   {
  97.  
  98.     write(1, de->d_name, strlen(de->d_name));
  99.  
  100.     write(1,"\n",1);
  101.  
  102.   }
  103.  
  104.  
  105.  
  106.   close(dir);
  107.  
  108. }
  109.  
  110.  
  111.  
  112. int main()
  113.  
  114. {
  115.  
  116.   char cmd[256];
  117.  
  118.   char* toks[10];
  119.  
  120.   int t = 0;
  121.  
  122.   do{
  123.  
  124.     write(1,">",1);
  125.  
  126.     int n = read(0,&cmd,256);
  127.  
  128.     cmd[n-1] = '\0';
  129.  
  130.     int tt = parse(cmd, toks);
  131.  
  132.     if (strcmp(toks[0],"cd") == 0)
  133.  
  134.     {
  135.  
  136.       f_cd(toks[1]);
  137.  
  138.       t=1;
  139.  
  140.     }
  141.  
  142.  
  143.  
  144.     if (strcmp(toks[0],"echo") == 0)
  145.  
  146.     {
  147.  
  148.       int c = 1;
  149.  
  150.       while (c < tt)
  151.  
  152.       {
  153.  
  154.         f_echo(toks[c]);
  155.  
  156.         write(1," ",1);
  157.  
  158.         c++;
  159.  
  160.       }
  161.  
  162.       write(1,"\n",1);
  163.  
  164.       t=1;
  165.  
  166.     }
  167.  
  168.  
  169.  
  170.     if (strcmp(toks[0],"pwd") == 0)
  171.  
  172.     {
  173.  
  174.       f_pwd();
  175.  
  176.       t=1;
  177.  
  178.     }
  179.  
  180.    
  181.  
  182.     if (strcmp(toks[0],"myls") == 0)
  183.  
  184.     {
  185.  
  186.       if (tt==1)
  187.  
  188.       {
  189.  
  190.          f_ls("./");
  191.  
  192.       }
  193.  
  194.       else
  195.  
  196.       {
  197.  
  198.         f_ls(toks[1]);
  199.  
  200.         t=1;
  201.  
  202.       }
  203.  
  204.     }
  205.  
  206.     if (t == 0)
  207.  
  208.     {
  209.  
  210.       int p=fork();
  211.  
  212.       if (p==0)
  213.  
  214.       {
  215.  
  216.         execvp(toks[0],toks);
  217.  
  218.         perror("Unknown command");
  219.  
  220.         exit(1);
  221.  
  222.       }
  223.  
  224.       else if (p>0)
  225.  
  226.       {
  227.  
  228.         wait(&p);
  229.  
  230.       }
  231.  
  232.     }
  233.  
  234.   } while (strcmp(toks[0],"exit") != 0);
  235.  
  236.   return 0;
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement