Re: C System Call
Posted in 1998
Albert Geisser wrote:
>
> I want to read the result of a dbaccess command into a C programm.
FILE *pip;
...
pip=popen("dbaccess mydatabase - <myscript.sql", "r" );
...
for (;;) {
if (fgets( mystr, sizeof mystr, pip ) == (char *)NULL)
break;
printf( stdout, "> %s", mystr );
}
...
If you need bidirectional communication with dbaccess you will need to
set up the pipes and child process manually. Here is a quicky overview:
Call pipe twice (once for input handles to dbaccess and once for output
handles), fork, close stdin & stdout in the child, dup one side of one
pipe into stdin of the other into stdout, close the other side of each
pipe, exec dbaccess in commandline mode. In the parent, close the
side of each pipe that the child is using, the parent uses the other
side. Now the pipe which was duped into stdin in the child is the
handle that you will write to to send SQL to dbaccess and the pipe
duped into stdout is the one you will read to get the results. You can
use fdopen to set up a FILE * for each of these handles so you can use
stream functions (fread, fwrite, fgets, etc). This is just an outline
if you cannot work it out get a good book on UNIX system programming.
Two of the best are by W. Richard Stevens: UNIX Network Programming
(don't let the title fool you), and Advanced Programming in the UNIX
Environment. Also see the "Logical log complete" section of my
alarmprogram in the IIUG Software Repository submission utils3_ak for
an example of setting up such a bidirectional pipe.
Art S. Kagel