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.