Writing a Simple Shell

Поділитися
Вставка

КОМЕНТАРІ • 49

  • @realzguardian
    @realzguardian 2 роки тому +49

    Btw guys the code is:
    #include "stdio.h"
    #include "string.h"
    #include "stdlib.h"
    #include "sys/wait.h"
    #include "unistd.h"
    void read_command(char cmd[], char *par[]) {
    char line[1024];
    int count = 0, i = 0, j = 0;
    char *array[100], *pch;
    for (;;) {
    int c = fgetc(stdin);
    line[count++] = (char) c;
    if (c == '
    ') {
    break;
    }
    if (count == 1) {
    return;
    }
    pch = strtok(line, "
    ");
    while (pch != NULL)
    {
    array[i++] = strdup(pch);
    pch = strtok(NULL, "
    ");
    }
    strcpy(cmd, array[0]);
    for (int j = 0; j < i; j++) {
    par[j] = array[j];
    par[i] = NULL;
    }
    }
    }
    void type_prompt() {
    static int first_time = 1;
    if (first_time) {
    const char* CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";
    write(STDERR_FILENO, CLEAR_SCREEN_ANSI, 12);
    first_time = 0;
    }
    printf("#");
    }
    int main() {
    char cmd[100], command[100], *parameters[20];
    char *envp[] = { (char *) "PATH=C:\\Windows\\System32", 0 };
    while(1) {
    type_prompt();
    read_command(command, parameters);
    if (fork() != 0) {
    wait(NULL);
    } else {
    strcpy(cmd, "C:\\Windows\\System32");
    strcpy(cmd, command);
    execvp(cmd, parameters, envp);
    }
    if (strcmp(command, "exit") == 0) {
    break;
    }
    }
    return 0;
    }

  • @infinitedonuts
    @infinitedonuts 2 роки тому +12

    Good video; it helped me get pointed in the right direction in regards to my assignment.

  • @hts2370
    @hts2370 3 роки тому +3

    you sould initiablize parameters everytime otherwise the parameters stored that last command's para

  • @eslamgamal94
    @eslamgamal94 6 років тому +15

    its good video for a simple shell i could understand , thank you

  • @fresk1to814
    @fresk1to814 Рік тому +3

    Thank you very much , of course im not using your code , neither the same logic probably , but it helped me get some idea of the project also , the way you took care of some cases , pretty intresting , now i have a clearer idea

  • @FooSo684
    @FooSo684  2 роки тому +1

    You may be interested in ua-cam.com/video/7Mn24vDuPgU/v-deo.html about an outstanding student who quit graduate school!

  • @seungrateful
    @seungrateful 2 роки тому +1

    Thank you. This was very helpful.

  • @gloriavlogs2874
    @gloriavlogs2874 3 роки тому +10

    This is the code, to help the next group of people who need it.😊

    • @hajraali1205
      @hajraali1205 2 роки тому +4

      there is nothing, do not make joke when we are serious, this isn't a movie theater

  • @hajraali1205
    @hajraali1205 2 роки тому +1

    how do I know my shell is working?

  • @avengedknot5319
    @avengedknot5319 6 років тому +12

    Your code does work fine, but it has a problem. If I put ls -l, and then try to execute ls command, it will execute ls -l, you need to clear the arg[1] before executing the next command.

    • @MiguelDiaz-jz1nh
      @MiguelDiaz-jz1nh 6 років тому

      you are right, but what arg[1] we need to clear?

    • @avengedknot5319
      @avengedknot5319 6 років тому +1

      Miguel Diaz Like when you do ls -l your arg[1] has -l, hence when you execute only ls, the arg[1] still has -l, so it will execute ls -l, so try freeing the arg[1] before sending the arg array to your exec. I'd suggest you to use a lex and yacc to get the inputs in and store them in a command table than this method, it's way more flexible and helps you deal with pipes, grep etc.

    • @erenyalcn31
      @erenyalcn31 5 років тому

      How exactly are we going to do that in C? I tried everything I found on internet but it didnt work.

    • @zihanbu8972
      @zihanbu8972 5 років тому

      @@erenyalcn31 Hi I am facing the same problem now. Did you figure out how to fix this bug?

    • @erenyalcn31
      @erenyalcn31 5 років тому +4

      @@zihanbu8972 Hi, I don't exactly remember how I solved it but I'm 100% sure that it was about the way we allocated memory for the arrays. When we say *parameters[20], we allocate static memory for our parameters and we cannot free the memory for our next commands. What you should do is that allocate memory dynamically by using malloc function, and maybe reallocate memory by using realloc function to change the memory section reserved for our array. I suggest you to learn about dynamic memory allocation in C, especially malloc and realloc functions.

  • @jeanrigo
    @jeanrigo 6 років тому

    Tks for share the vid. Great job!

  • @farahelyassir5174
    @farahelyassir5174 4 роки тому +1

    please can i get the code

  • @josueteodoromoreira8921
    @josueteodoromoreira8921 3 роки тому +25

    3 years later and nobody got the code.

  • @naukarikikhabar
    @naukarikikhabar 2 роки тому +1

    his code not working

  • @hananessa7846
    @hananessa7846 3 роки тому +1

    can you write a link for your code plz,i need it???

    • @Nunya58294
      @Nunya58294 3 роки тому +3

      Lmao noob.

    • @realzguardian
      @realzguardian 2 роки тому

      #include "stdio.h"
      #include "string.h"
      #include "stdlib.h"
      #include "sys/wait.h"
      #include "unistd.h"
      void read_command(char cmd[], char *par[]) {
      char line[1024];
      int count = 0, i = 0, j = 0;
      char *array[100], *pch;
      for (;;) {
      int c = fgetc(stdin);
      line[count++] = (char) c;
      if (c == '
      ') {
      break;
      }
      if (count == 1) {
      return;
      }
      pch = strtok(line, "
      ");
      while (pch != NULL)
      {
      array[i++] = strdup(pch);
      pch = strtok(NULL, "
      ");
      }
      strcpy(cmd, array[0]);
      for (int j = 0; j < i; j++) {
      par[j] = array[j];
      par[i] = NULL;
      }
      }
      }
      void type_prompt() {
      static int first_time = 1;
      if (first_time) {
      const char* CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";
      write(STDERR_FILENO, CLEAR_SCREEN_ANSI, 12);
      first_time = 0;
      }
      printf("#");
      }
      int main() {
      char cmd[100], command[100], *parameters[20];
      char *envp[] = { (char *) "PATH=C:\\Windows\\System32", 0 };
      while(1) {
      type_prompt();
      read_command(command, parameters);
      if (fork() != 0) {
      wait(NULL);
      } else {
      strcpy(cmd, "C:\\Windows\\System32");
      strcpy(cmd, command);
      execvp(cmd, parameters, envp);
      }
      if (strcmp(command, "exit") == 0) {
      break;
      }
      }
      return 0;
      }

  • @hajraali1205
    @hajraali1205 2 роки тому

    ls -l does not work

  • @sanatmollick9182
    @sanatmollick9182 3 роки тому +2

    Can you give me the code?

  • @tonytam1185
    @tonytam1185 3 роки тому +1

    trebla

  • @BUTTERFLY-sr4jl
    @BUTTERFLY-sr4jl 5 років тому

    code plz!

  • @SandyRocks007
    @SandyRocks007 7 років тому +14

    code please

  • @emmanuelchalo4530
    @emmanuelchalo4530 2 роки тому

    85k views but less 1000 subscribers tells how wicked all these viewers are. Shame on you mean souls.