Home » Developer & Programmer » Precompilers, OCI & OCCI » capture output from execl() or system()
capture output from execl() or system() [message #163213] Wed, 15 March 2006 09:31
bulldog1
Messages: 1
Registered: March 2006
Location: Des Moines, IA
Junior Member
I am using pipes in my program to capture output from execl(). However, the command I am executing returns an error every time. When I execute the same command from the UNIX command line, there are no errors and I get the expected result. When I execute the same command from within the program using system() I get no errors, but I don't know how to capture output from that command. Anyone know how I can resolve this? Here is my code:

#include <stdio.h>
#include "guarpfe.h"
#define ENUF(msg, value) {perror(msg); exit(value); }

EXEC SQL INCLUDE guaorac.c;
static FNSTRUC outfile;
static FILE *outpfile;
short sqltrace_flag;

EXEC SQL BEGIN DECLARE SECTION;
static int status=0;
static CHAR31 return_pipe;
static CHAR50 uid="";
static CHAR51 url;
static int status2;
static CHAR201 parm_string;
static CHAR251 command;
static CHAR251 result;
EXEC SQL END DECLARE SECTION;

main(argc, argv)
int argc;
char *argv[];
{
int i, pid, fd[2];
char receive[1000];
char *gets();

EXEC SQL CONNECT :uid;
printf("Daemon connected.\n");
printf("Daemon waiting...\n");
while (1) {
EXEC SQL EXECUTE
BEGIN
:status := DBMS_PIPE.RECEIVE_MESSAGE('sct_cc_pipe');
IF :status = 0 THEN
DBMS_PIPE.unpack_message (:return_pipe);
END IF;
:status := DBMS_PIPE.RECEIVE_MESSAGE('sct_cc_pipe');
IF :status = 0 THEN
DBMS_PIPE.unpack_message (:url);
END IF;
:status := DBMS_PIPE.RECEIVE_MESSAGE('sct_cc_pipe');
IF :status = 0 THEN
DBMS_PIPE.unpack_message (:parm_string);
END IF;
:command := :url || :parm_string;
END;
END-EXEC;
printf("return pipe: %s\n", return_pipe);
printf("url: %s\n", url);
printf("parms: %s\n", parm_string);

if ( pipe(fd) != 0) ENUF("pipe creation problem", 1);

i = 0;
while ( (pid=fork()) == -1) /* try a few times before giving up */
if (++i > 5 ) ENUF("fork failed after 5 attempts", 2) else sleep(5);

if (pid == 0) { /* CHILD process */
close(fd[1]); /* not using pipe write fd=4 */
i = read(fd[0], receive, 1000);
receive[i] = '\0';
fprintf(stdout, "Child received message: %s \n", receive);
close(fd[0]); /* close pipe read after use fd=3 */
}

else { /* Parent process */
fprintf(stderr,
"pipe file descriptors before dup:fd[0] %d,fd[1] %d \n", fd[0], fd[1]);
close(fd[0]); /* not using pipe read fd=3 */
close(1); /* redirecting stdout fd=1 */
i = dup(fd[1]); /* duplicate pipe write with stdout i.e. 1=4 */
fprintf(stderr,
"pipe file descriptors after dup: fd[0] %d,fd[1] %d \n", fd[0], i);
close(fd[1]); /* close pipe write after use fd=4 */
if (execl("/usr/bin/pfpro", command, 0) == -1) ENUF("Unable to run command", 2);
}

system(command);

EXEC SQL EXECUTE
BEGIN
IF :status = 0 THEN
DBMS_PIPE.PACK_MESSAGE('done');
ELSE
DBMS_PIPE.PACK_MESSAGE('not done');
END IF;
:status := DBMS_PIPE.SEND_MESSAGE(:return_pipe);
END;
END-EXEC;
printf("exiting daemon \n");
break;

}
}
Previous Topic: Environment::createEnvironment() & Unicode support
Next Topic: code is not working properly
Goto Forum:
  


Current Time: Wed Jul 30 07:44:41 CDT 2014

Total time taken to generate the page: 0.21005 seconds