termios help

Discussion in 'C Programming' started by Paolo Picci, Mar 19, 2011.

  1. Paolo Picci

    Paolo Picci Guest

    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");
    }
    }


    }
     
    Paolo Picci, Mar 19, 2011
    #1
    1. Advertising

  2. On Sat, 19 Mar 2011 07:11:26 -0700 (PDT)
    Paolo Picci <> 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");
    > }
    > }
    >
    >
    > }
     
    Spiros Bousbouras, Mar 19, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan Hodges

    termios POSIX implementation

    Jonathan Hodges, Jan 26, 2004, in forum: Python
    Replies:
    0
    Views:
    382
    Jonathan Hodges
    Jan 26, 2004
  2. henry xie

    TERMIOS.py

    henry xie, Jun 30, 2004, in forum: Python
    Replies:
    3
    Views:
    3,671
  3. Petr Jakes
    Replies:
    3
    Views:
    735
  4. Derek Peschel
    Replies:
    0
    Views:
    368
    Derek Peschel
    Aug 8, 2006
  5. goblin

    Help using termios

    goblin, May 22, 2008, in forum: C Programming
    Replies:
    2
    Views:
    381
    Antoninus Twink
    May 22, 2008
Loading...

Share This Page