Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > termios help

Reply
Thread Tools

termios help

 
 
Paolo Picci
Guest
Posts: n/a
 
      03-19-2011
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");
}
}


}
 
Reply With Quote
 
 
 
 
Spiros Bousbouras
Guest
Posts: n/a
 
      03-19-2011
On Sat, 19 Mar 2011 07:11:26 -0700 (PDT)
Paolo Picci <(E-Mail Removed)> 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");
> }
> }
>
>
> }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help using termios goblin C Programming 2 05-22-2008 09:39 PM
Changing UNIX tty driver keys -- Suggested changes to "termios" module Derek Peschel Python 0 08-08-2006 02:36 AM
termios.tcgetattr(fd) error: (22, 'Invalid argument') Petr Jakes Python 3 11-21-2005 12:28 PM
TERMIOS.py henry xie Python 3 06-30-2004 07:02 PM
termios POSIX implementation Jonathan Hodges Python 0 01-26-2004 11:30 PM



Advertisments