Skip to content

Instantly share code, notes, and snippets.

@engina
Created April 3, 2014 12:30
Show Gist options
  • Save engina/9953422 to your computer and use it in GitHub Desktop.
Save engina/9953422 to your computer and use it in GitHub Desktop.
CGI application written in C that forks a command
#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