C programs under Linux, using function EXECLP to run shell commands

Source: Internet
Author: User

Run the shell command through the C program, using the EXECLP function

EXECLP Several parameters are: file name, each parameter, (char*) 0

Where the first parameter in the "Parameters" section is the file name (equivalent to the file name entered two times)

Last parameter Write (char*) 0 Yes

File B.cpp Code:

Loop through the commands → execute the command until you press CTRL + C to end

#include  <stdio.h> #include  <string.h> #include  <unistd.h> #include  < stdlib.h>using namespace std; #define  max_para_nums      10# Define max_char_each_para 1000int split (char* input, char output[max_para_nums][ Max_char_each_para]) {    int counter = 0;    int  Len = strlen (Input);    bool btemp = false;     int i, j = 0;    for (i = 0; i < len;  i++)     {        if (input[i] ==  '   ')         {             if (j != 0 && counter > 0)              {                 output[counter - 1][j] =  ';         '     }            btemp =  false;        }         else        {             if (Btemp == false)              {                j  = 0;                 counter++;                 btemp = true;            }             output[counter - 1][j] = input[i];             j++;        }     }        return counter;} Int main () {    int counter = 0;    char  params[max_para_nums][max_char_each_para];    //using g++ to compile Files &NBSP;&NBSP;&NBSP;&NBSP;//EXECLP ( "g++",  "g++",  "A.cpp",  "-O",  "a",  (char*) 0);         char buf[4096];    pid_t pid;    int status ;     //input prompt string     printf ("console-x\n");     printf ("=========\n");     while  (fgets (Buf, 4096, stdin)  != null)     {         if  (Buf[strlen (buf)  - 1] ==  ' \ n ')          {            buf[ Strlen (buf)  - 1] =  ';        }   '       pid = fork ();         if   (pid < 0)         {             printf ("Fork error");         }        else if  (pid == 0)          {             Counter = split (BUF,&NBsp;params);             if (counter != 0)             {                 printf ("---------\ n");             }             switch (counter)             {                 case 0:  Break;                case  1:                      {                   &nbSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXECLP (params[0], params[0],  (char*) 0);                     }                          break;                 case 2:                     {            &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXECLP (Params[0], params[0] , params[1],  (char*) 0);                     }                     break;                 case 3:                     {           &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXECLP (params[0],  params[0], params[1], params[2],                              (char*) 0 );                     }                     break;                 case 4:                     {               &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXECLP (Params[0], params[0], params[1],  params[2],                              params[3],  (char*) 0);                      }                     break;                 case 5:                     {                &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXECLP (params[0], params[0], params[1],  params[2],                              params[3], params[4],  (char*) 0);                     }                     break;                 case 6:                     {               &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXECLP (Params[0], params[0], params[1],  params[2],                &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PARAMS[3],&NBSP;PARAMS[4],  params[5],  (char*) 0);                     }                     break;                 case 7:                     {                               EXECLP (params[0], params[0], params[1], params[2],                               params[3], params[4], params[5], params[6],  (char*) 0);                     }                      break;                 case 8:                     {              &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXECLP (Params[0],  params[0], params[1], params[2],                              params[3],  params[4], params[5], params[6],                             params[7 ],  (char*) 0);                     }                     break;                 case 9:                     {             &nbSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXECLP (Params[0],  params[0], params[1], params[2],                               params[3], params[4], params[5], params[6],                              params[7], params[8],  (char*) 0);                     }                     break;                 case 10:                     {                         &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXECLP (params[0], params[0], params[1], params[2],                               params[3], params[4], params[5], params[6 ],                             params[7], params[8], params[9],  ( char*) 0);                     }                     break;                 default:                     {                         printf ("illegal input!\n");                     }                      break;            }         }        else         {            //input Prompt string               //printf ("console-x> ");         }    }    return 0;}

Compile b.cpp into executable file B (input command: g++ b.cpp-o b)

Then open file B and enter the command in turn

Ls-lscat a.cppg++ A.cpp-o a./a

File A.cpp Code:

#include <iostream>using namespace Std;int main () {cout << "Hello world!" << Endl; return 0;}

Examples of running results:


END

C programs under Linux, using function EXECLP to run shell commands

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.