Talk C chestnuts together (71st back: C language instance -- DIY shell)

Source: Internet
Author: User

Talk C chestnuts together (71st back: C language instance -- DIY shell)

Hello, everyone. In the previous session, we talked about the string initialization example. This example is as follows:DIY shell. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

Look, we use shell every day, and it seems that we can't do without it. This is what our predecessors have left us. Today we will introduce how to DIY a shell, which is a tribute to our predecessors.

Our DIY ideas are as follows:

1. enter a character similar to $, prompting the user to input content to the terminal; 2. read user-input commands from the terminal; 3. judge the command entered by the user and execute the corresponding command; 4. repeat steps 1 to 3 until the user enters the exit command to end the program and exit shell.

The following is a simple framework of the shell program I wrote. For details, refer:

int main(int argc, char *argv[]){    char buf[BUF_SIZE];    int res = 1;    int flag = 1;    int index = 0;    while(flag)    // do this untial input exit commond    {        printf("|->");        if(NULL == fgets(buf,BUF_SIZE,stdin))   // get the input            return 0;        index = sizeof(input)/sizeof(input_type);        while(index-- > 0)        {            res = strncmp(buf,input[index].str,input[index].size); // find the commond            if(res == 0)            {                switch(index)                {                case 0: // exec exit commond                    flag = 0;                    break;                case 1: // exec cd commond                    cds(buf);                    break;                case 2: // exec ls commond                    lss(buf);                    break;                default:                    printf("can 's \n");                    break;                }                index = -1; // if find the commond, stop finding            }        }        if(index == -1)            printf("can't find commond: %s ",buf);    }    return 0;}

The cd and ls functions in the program are not fully implemented yet. In addition, this is just a simple program framework, and many shell functions are not implemented. We will introduce and implement these content in the following sections.

The readers will not write the code in the body, and the detailed code will be put into my resources. You can click here to download and use it. The following is the running result of the program. In the program, we use $ in the "|->" code shell. Although the form is different, it is used to prompt the user to enter the command.

 |->abc                      //abc is not a commondcan't find commond: abc |->cd                       // exec the cd commondcd running |->ls                        // exec the cd commondls running |->exit                      // exit the shell program

Let's talk about the DIY shell example. I want to know what examples will be provided later, and I will try again.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.