Hvala na odgovoru, ovaj kod odradjuje posao, samo read iz fajla radi probleme, nekad nece sve ucitat u buffer makar jer buffer 1000 karaktera pa moram 2 puta readat za po 15-20 karaktera.
Code:
#include <sys/types.h>
#include <unistd.h>
#define SIZE 1000
struct popen2
{
pid_t proc_pid;
int read, write;
};
int popen2(struct popen2 *pinfo)
{
pid_t p;
int pin[2], pout[2];
if(pipe(pin)) return -1;
if(pipe(pout)) return -1;
if((p=fork()) < 0) return p;
if(p == 0)
{
close(pin[1]);
dup2(pin[0], 0);
close(pout[0]);
dup2(pout[1], 1);
execl("/usr/bin/telnet", "telnet", "localhost", "25", 0);
exit(99);
}
pinfo->proc_pid = p;
pinfo->write = pin[1];
pinfo->read = pout[0];
return 0;
}
void wwrite(char *msg,struct popen2 *id)
{
int len=strlen(msg);
write(id->write,msg,len);
}
int main(void)
{
char buf[SIZE];
struct popen2 prog;
memset(buf,0,SIZE);
popen2(&prog);
read(prog.read,buf,SIZE);
printf("from: %s\n",buf);
wwrite("helo asda\n",&prog);
wwrite("QUIT\n",&prog);
memset(buf, 0, SIZE);
read(prog.read, buf,SIZE);
printf("from child: %s", buf);
memset(buf, 0, SIZE);
read(prog.read, buf,SIZE);
printf("from child: %s", buf);
memset(buf, 0, SIZE);
read(prog.read, buf,SIZE);
printf("from child: %s", buf);
close(prog.write);
kill(prog.proc_pid, 0);
return 0;
}