Enhanced version of the system function that can return execution results

Source: Internet
Author: User

Enhanced version of the system function that can return execution results

/********************************************************************* * Author  : Samson * Date    : 03/13/2015 * Test platform: *              3.13.0-24-generic *              GNU bash, 4.3.11(1)-release  * *******************************************************************/

In GNU Linux C programming, to execute system commands, only the system interface is provided, but this interface cannot obtain the value output after the command is executed, you can only get the result of successful command execution. Such a function is not enough. Sometimes it is necessary to judge the result or step of the next step through command output. How can we get the result of executing the system command? Then you can use the popen function and the fgets function to obtain the command output information. The actual example is as follows:

Note: This interface can only obtain the information of the last line of command output. If multiple lines of output information are available, this encapsulated interface is only applicable to the information of the last line in the command execution result.


# Include
# Include
# Include
# Include

Int super_system (const char * cmd, char * retmsg, int msg_len)
{
FILE * fp;
Int res =-1;
If (cmd = NULL | retmsg = NULL | msg_len <0)
{
Printf ("Err: Fuc: % s system paramer invalid! \ N ", _ func __);
Return 1;
}
If (fp = popen (cmd, "r") = NULL)
{
Perror ("popen ");
Printf ("Err: Fuc: % s popen error: % s \ n", _ func __, strerror (errno ));
Return 2;
}
Else
{
Memset (retmsg, 0, msg_len );
While (fgets (retmsg, msg_len, fp ));
{
Printf ("Fuc: % s fgets buf is % s \ n", _ func __, retmsg );
}
If (res = pclose (fp) =-1)
{
Printf ("Fuc: % s close popen file pointer fp error! \ N ", _ func __);
Return 3;
}
// Drop #012 from system result retmsg.
Retmsg [strlen (retmsg)-1] = '\ 0 ';
Return 0;
}
}

Int main ()
{
Char * cmd = "whoami ";
Char * cmd1 = "initctl list ";
Char retmsg [1024] = {0 };
Int ret = 0;
Ret = super_system (cmd, retmsg, sizeof (retmsg ));
Printf ("system ret is % d retmsg is \ n % s \ n", ret, retmsg );
Return 0;
}

The main function uses the whoami command. The execution result is the current user name.
Execution result:
Ufo @ ufo :~ $./A. out
Fuc: super_system fgets buf is ufo

System ret is 0 retmsg is
Ufo

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.