[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

termios help

Paolo Picci

3/19/2011 2:11:00 PM

Hi all
i need to reuse the otput of an application but i dont wanto to
redirect the output i need to display.
my idea is: open a new terminal, copy the the configuration of the
current terminal into the new one, fork the child read from new term
and write to output the parent dup2(newterm_fd, 1) so exec the
program.
if i use a normal fifo its work but if i exec /bin/ls who call istty()
on the fd do a oter output.
the code dont work.... but i do know why???
ideas??
tnks!

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <util.h>
#include <errno.h>


main() {
int pid;

int amaster;
int aslave;
char *name = valloc(1024);
//for new terminal
struct termios * termp = valloc(sizeof(struct termios));
//current terminal
struct termios * ioterm = valloc(sizeof(struct termios));
struct winsize *win = valloc(sizeof(struct winsize));

int ret = openpty(&amaster, &aslave, name, termp, win);

//opening new terminal
int fifofd = open(name , O_RDWR | O_NOCTTY | O_NDELAY);

//copyng the configuration of the current terminal on the new
terminal
tcgetattr(STDOUT_FILENO, ioterm);
tcsetattr(fifofd, TCSANOW ,ioterm);

//now whe fork the child read from the new terminal and print on
STDOUT
// the parent close STDOUT and print on the new terminal
pid = fork();

if (pid == 0 ){
//child
char *buff[512];
int br = 0;
int r = 0;
//reopen for non sharing seek position maybe util?
fifofd = open(name , O_RDWR | O_NOCTTY | O_NDELAY);
while(1){
br = read(fifofd, buff, 1);
if (br >0 ) {
write(STDOUT_FILENO, buff, br);
// use buff in oter way :)
} else {
r = errno;
}

}

}else {
//parent
//close stdout now all operation are on fifofd
dup2(fifofd, STDOUT_FILENO);
///exec application :)
while (1) {
printf("TEST: Can you see this?\n");
}
}


}
1 Answer

Spiros Bousbouras

3/19/2011 4:15:00 PM

0

On Sat, 19 Mar 2011 07:11:26 -0700 (PDT)
Paolo Picci <paolo.picci@gmail.com> wrote:
> Hi all

Hello. For your question you need comp.unix.programmer not comp.lang.c
I have crossposted and set follow-ups.

> i need to reuse the otput of an application but i dont wanto to
> redirect the output i need to display.
> my idea is: open a new terminal, copy the the configuration of the
> current terminal into the new one, fork the child read from new term
> and write to output the parent dup2(newterm_fd, 1) so exec the
> program.
> if i use a normal fifo its work but if i exec /bin/ls who call istty()
> on the fd do a oter output.
> the code dont work.... but i do know why???
> ideas??
> tnks!
>
> #include <stdio.h>
> #include <unistd.h>
> #include <fcntl.h>
> #include <util.h>
> #include <errno.h>
>
>
> main() {
> int pid;
>
> int amaster;
> int aslave;
> char *name = valloc(1024);
> //for new terminal
> struct termios * termp = valloc(sizeof(struct termios));
> //current terminal
> struct termios * ioterm = valloc(sizeof(struct termios));
> struct winsize *win = valloc(sizeof(struct winsize));
>
> int ret = openpty(&amaster, &aslave, name, termp, win);
>
> //opening new terminal
> int fifofd = open(name , O_RDWR | O_NOCTTY | O_NDELAY);
>
> //copyng the configuration of the current terminal on the new
> terminal
> tcgetattr(STDOUT_FILENO, ioterm);
> tcsetattr(fifofd, TCSANOW ,ioterm);
>
> //now whe fork the child read from the new terminal and print on
> STDOUT
> // the parent close STDOUT and print on the new terminal
> pid = fork();
>
> if (pid == 0 ){
> //child
> char *buff[512];
> int br = 0;
> int r = 0;
> //reopen for non sharing seek position maybe util?
> fifofd = open(name , O_RDWR | O_NOCTTY | O_NDELAY);
> while(1){
> br = read(fifofd, buff, 1);
> if (br >0 ) {
> write(STDOUT_FILENO, buff, br);
> // use buff in oter way :)
> } else {
> r = errno;
> }
>
> }
>
> }else {
> //parent
> //close stdout now all operation are on fifofd
> dup2(fifofd, STDOUT_FILENO);
> ///exec application :)
> while (1) {
> printf("TEST: Can you see this?\n");
> }
> }
>
>
> }