Created
April 3, 2014 12:30
-
-
Save engina/9953422 to your computer and use it in GitHub Desktop.
CGI application written in C that forks a command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#define BUF_LEN (1024) | |
void print_command( const char* cmd ) | |
{ | |
char stdout_buf[ BUF_LEN ]; | |
size_t r; | |
FILE* p = popen( cmd, "r" ); | |
if( p ) | |
{ | |
memset( stdout_buf, 0, BUF_LEN ); | |
while( ( r = fread( stdout_buf, 1, BUF_LEN, p ) ) ) | |
{ | |
printf( "%s", stdout_buf ); | |
memset( stdout_buf, 0, BUF_LEN ); | |
} | |
if( !feof( p ) ) | |
{ | |
fprintf( stderr, "Could not read whole output.\n" ); | |
return; | |
} | |
pclose( p ); | |
return; | |
} | |
fprintf( stderr, "Could not open pipe.\n" ); | |
} | |
int main( int argc, char** argv ) | |
{ | |
printf( "Content-type: text/plain\r\n\n" ); | |
print_command( "/bin/uname -a" ); | |
print_command( "/usr/bin/uptime" ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment