termios help

P

Paolo Picci

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


}
 
S

Spiros Bousbouras


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


}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top