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");
}
}
}
|
|
0
|
|
|
|
Reply
|
paolo.picci (2)
|
3/19/2011 2:11:26 PM |
|
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");
> }
> }
>
>
> }
|
|
0
|
|
|
|
Reply
|
Spiros
|
3/19/2011 4:15:02 PM
|
|
> On Sat, 19 Mar 2011 07:11:26 -0700 (PDT)
> Paolo Picci <paolo.picci@gmail.com> wrote:
>> i need to reuse the otput of an application but i dont wanto to
>> redirect the output i need to display.
tee
http://empty.sourceforge.net
http://expect.nist.gov/
|
|
0
|
|
|
|
Reply
|
Ersek
|
3/19/2011 7:34:21 PM
|
|
|
2 Replies
167 Views
(page loaded in 0.462 seconds)
Similiar Articles: Only number input thru scanf() - comp.lang.cPlease help me out. If you are under a unix shell you can use termios A little example (never tested in real production, any advice is welcomed) #include <stdio ... C pipe error - comp.unix.programmer... it's stdin/stdout connected to this terminal, it can use ioctl() with a termios ... Obviously the pipes htat you used to connect to your childprogram do not support ... Connection refused problem - comp.unix.programmer... include <sys/time.h> #include <sys/socket.h> #include <netinet/in.h> #include <termios ... Date on host %s is: %s\n", argv[1], buf); close(sd); exit(0); } Any help ... Press any key to continue... - comp.unix.programmer#include <stdio.h> #include <termios.h> void switch_buffering(int onoff) { static struct termios stored_settings; struct termios new_settings; if (!onoff ... Problem with getch and nodelay, backspace key - comp.unix ...... int cnt = 0; int error; static struct termios ... However, even more conveniently, libcur.a does not support cbreak, you will get a ... Random NULL chars from serial port? - comp.unix.programmer ...... device; int port; int baudrate; char *parity; int stopbits; int debug; struct termios ... and writing from a serial port. ... write read string data - comp.lang.java.help ... How can I send a break to a remote machine? - comp.unix.solaris ...... ttyb_brk.c ===== #include <stdio.h> #include <termios.h ... Send remote computer a message - comp.lang.java.help Is there a way that I can ... Sockets in gfortran? - comp.lang.fortran... some systems still require this */ #include <sys/stat.h> #include <sys/termios.h ... Gib that I was leaving out part of the story, and this detail could really help ... Serial Programming/termios - Wikibooks, open books for an open worldIntroduction . termios is the newer (now already a few decades old) Unix API for terminal I/O. The anatomy of a program performing serial I/O with the help of termios ... Help using termios - C / C++Help using termios. C / C++ Forums on Bytes. ... Hi all I'm struggeling to get my serial port settings changed using termios. 7/25/2012 10:35:37 PM
|