#include #include int main() { /* simulation: calling an external Perl module */ char *cmd = "/usr/bin/perl -e ' print q{PerlEcho: }, join(q{, },@ARGV), qq{\n} '"; /* first example: system() */ char sys_cmd[300]; /* hey, just a demo ... */ strcpy(sys_cmd, cmd); strcat(sys_cmd, " par1 par2 par3 its the 'system()' call "); /* a realistic example would redirect STDOUT and STDERR into files */ /* e.g: strcat(sys_cmd," par1 par2 >output.1 2>output.2"); */ /* (shell-interpreted: standard disclaimers apply) */ printf("Calling perl via system():\n>>>"); fflush(stdout); system(sys_cmd); /* todo: check result */ /* second example: popen() */ char ppn_cmd[300]; strcpy(ppn_cmd, cmd); strcat(ppn_cmd, " now via 'popen()' "); printf("Calling perl via popen():\n>>>"); FILE *fh = popen(ppn_cmd, "r"); /* read-only for now */ if ( fh ) { char buffer[101]; while( !feof(fh) ) { int len = fread(buffer, 1, 100, fh); if ( len>0 ) { buffer[len] = '\0'; printf("%d: %s",len,buffer); } } if ( pclose(fh) ) perror("ERROR - Failed to close pipe properly"); } else { perror("cannot popen"); printf("ERROR - cannot run popen(%s,\"r\",)\n", ppn_cmd); } return 0; } /* pb:sopw > gcc sys_n_popen.c && a.out Calling perl via system(): >>>PerlEcho: par1, par2, par3, its, the, system(), call Calling perl via popen(): >>>28: PerlEcho: now, via, popen() */