CGI Program Learning (2)-cgi cannot display the problem correctly in the browser after executing the shell script

Source: Internet
Author: User
Tags function prototype

My logic is to execute a shell script in the CGI that shows the WiFi data in the script-generated text in the browser. But the browser has been unable to display the normal, tangled for several days.

The shell script probably does this:

#! /bin/sh
  echo "12345" > Wifi_info

If the script is not executed > wifi_info, the browser is displayed normally

After learning the basic principles of CGI, I finally know why. The data displayed on the user's browser is executed. The/CGI program prints the data on the channel, and after the shell script executes >wifi_info, the channel is redirected to the text, so the data printed later does not print correctly on the channel of the current user's browser and needs to be redirected back to the original channel.


DUP dup2 function Learning

DUP and dup2 are also two very useful calls that are used to copy the descriptor of a file. They are often used to redirect stdin, stdout, and stderr of processes.

The function prototype is as follows:

#include <unistd.h>
    int dup (int oldfd);
    int dup2 (int oldfd, int targetfd)


Using this function, I can use the DUP to copy the original channel, and then execute the shell script to write the text, use DUP2 to redirect the channel to the original channel, and then print the content that needs to be displayed in the channel, it can be displayed correctly

Based on the above principles, the final code is as follows:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h >
#include <stdlib.h>
#define STDIN_FILENO 0

int main ()
{/
    *-------------------- Redirection Settings----------------*
    /int fd = DUP (Stdout_fileno);//copy original channel/
    *-------------------- Execute Shell-----------------*/
    pid_t status;

    Status = System ("wifi.sh");//execute Shell command, will be directed to write to the file
	
    if ( -1 = = status)//To determine whether the shell executes successfully
    {
        printf ("System Error! ");
	return-1;
    }
	
    Dup2 (FD, Stdout_fileno);//redirect to Browser channel	
    show ();//Read text and show
    close (FD);//close identity                     
                       
    return 0;
}

The show () function involves reading text and printf to the channel code, and the next chapter reads and displays methods.


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.