home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.programmer
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!agate!linus!linus.mitre.org!elara.mitre.org!bdickens
- From: bdickens@elara.mitre.org (Brian Dickens)
- Subject: Pipes/Sockets/Pty's/Communications
- Message-ID: <1992Dec30.160512.10958@linus.mitre.org>
- Keywords: pipe socket pty
- Sender: news@linus.mitre.org (NONUSER)
- Nntp-Posting-Host: elara.mitre.org
- Organization: Research Computer Facility, MITRE Corporation, Bedford, MA
- Date: Wed, 30 Dec 1992 16:05:12 GMT
- Lines: 72
-
- I am attempting to remote-control a program that I did not write, which was
- written to get its input from and send its output to a terminal.
-
- I am calling this program from a nice fancy Motif application, written in C.
-
- I want to be able to send commands to the program and get answers back,
- but using standard piping techniques have been unable to help me so far,
- since the output of the program seems to be *buffered*!!!
-
- What's the best way to set up such a remote-control link? Should I use
- pseudo-terminal devices? This was one reccomendation I received --
- but I haven't a clue what they are or how to use them. Or are standard
- pipes okay, and I'm just doing something wrong? Let me include the
- code I have so far, which tries to start up the program (called ARC/info,
- executed by typing "arc") and execute its "help" command... which is
- supposed to print a listing to stdout of the commands.
-
- ----------------------- CODE PART ----------------------------
- #include <stdio.h>
-
- int in, out;
-
- int talkto( cmd )
- char *cmd;
- {
- int pid;
- int to_child[2];
- int to_parent[2];
-
- pipe( to_child );
- pipe( to_parent );
- if( pid = fork(), pid == 0 )
- {
- close( 0 );
- dup( to_parent[0] );
- close( 1 );
- dup( to_child[1] );
- close( to_child[0] );
- close( to_child[1] );
- close( to_parent[0] );
- close( to_parent[1] );
- execlp( cmd, cmd, NULL);
- }
- else
- if( pid > 0 )
- {
- in = dup( to_parent[0] );
- out = dup( to_child[1] );
- close( to_child[0] );
- close( to_child[1] );
- close( to_parent[0] );
- close( to_parent[1] );
- }
- else
- {
- fprintf( stderr, "Couldn't fork process.\n" );
- exit( 1 );
- }
-
- return( 0 );
- }
-
- int main( )
- {
- char x[255];
-
- talkto( "arc" );
- write( out, "help\n", strlen( "help\n" ) );
- read( in, x, 255 );
- printf( "%s", x );
- return( 0 );
- }
-