[LCP]system funtion or fork or exec
Gonzalo Sanchez
Gsanchez at roinetwork.com
Fri Feb 14 22:58:01 UTC 2003
thanks for your help mehul. I found out that I could use ssh without password. However I am gonna try the first option that you mention..just for fun..and see.
thanks again
-----Original Message-----
From: mehul radheshyam choube [mailto:mrc_cprog at rediffmail.com]
Sent: Friday, February 14, 2003 2:25 AM
To: linuxcprogramming at lists.linux.org.au
Cc: Gsanchez at roinenetwork.com
Subject: Re: [LCP]system funtion or fork or exec
hi sanchez,
i m sorry i m late to answer maybe by this time u must
have solved ur
problem. now ur case is a classic example for Interprocess
Communication(IPC).
i had a similar problem a few months back and got it solved. i
will explain u
using the problem i had. i wanted to do the following :-
I wanted to write wrappers to an existing command-line
based cvs
commands The existing cvs login cmd goes like this
$ cvs login
(Logging in to mehul at 192.168.0.25)
CVS password: (enter password here)
$ messages..... eg login sucessful or not.
Basically my program needs to issue the cvs login
command, wait for the
password prompt, sends the password and wait for the login
messages.
now this can be done in 3 ways. choose one which is
suitable for u.
1. using popen().
#include <stdio.h>
FILE *popen(const char *command, const char *type);
eg.
FILE *fpCmd = popen("cvs login", "w");
if(!fpCmd) {
fwrite( password, 1, strlen(password), fpCmd);
}/* if(!fpCmd) */
2. using fork(), execvp(), pipe() & select() combination.
1) Create a pair of pipes. One will be for the parent app to
send
instructions to the child, the other will be for the child
to respond to
the parent.
2) Fork the app
3a) In the child, set the parent->child pipe to be stdin, and
the
child->parent to be stdout
3b) In the parent, send any data you need to through the
parent->child pipe.
4a) Have the child execute the command with exec. The output
will be sent
on stdout which is now a pipe going to the parent.
4b) Have the parent wait with select until there is data on
the
child->parent pipe. Read the data in, and parse the
output message.
Take the appropriate actions.
5b) If there are more than 1 output statements that need
parsing, goto 4b.i
3. using pseudo-terminal concept. i went for this option. the
reason is some
utilities use terminals to when they interact. cvs uses
terminals so above
two options failed. using pseudo-terminal programming i was
able to overcome
the problem. i have attached a tarred file
pseudo-terminals.tgz it contains
info related to pseudo-terminals. if u want more info u can
always go for
search on google.com.
eg.
i have attached a tarred file pseudo_code.tgz t contains
code that i use to
execute cvs cmds using pseudo-terminals.
u can modify execute_cvs_cmd.cpp into C code.
i will be glad if above information is useful to u. this
is best to my
knowledge. i m sorry if it has any short commings. i think this
info can at
least give u a start so that u can go ahead.
mehul.
More information about the linuxCprogramming
mailing list