• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

i/o for terminals

sir ,


 **********     1.c *********

        int main()
               {
                  write(0 ,"ratheesh" ,8);
                 }

this program will output   : ratheesh on terminal
in which it is executed .

There r 3 terminals i hve opened on my p3 system
working on RedHat 8 os.

tty1  , tty2 ,tty3 .

Is there any method to change output from terminal to other without  using like the below
          int a =open("/dev/tty2" ,O_WRONLY);



0
karana
Asked:
karana
  • 2
  • 2
2 Solutions
 
Alf666Commented:
First of all, I would not write the the file descriptor 0. 0 is stdin (the input buffer). You should write to stdout (1) or stderr (2).

But there is no other way than the one you're mentioning, except using shell redirection :

echo "foo" > /dev/tty2

0
 
ray_pro2Commented:
There is a way, but it is much less sophisticated than you would expect. If your greatest intension is to avert use of open( ), try this instead
#include<unistd.h>
---
---
system("echo ratheesh >> /dev/tty2");
---
---
which internally does nothing but redirection. But this is not recomended style of programming.
Obviously the best way of doing thsi is openning the file corrosponding to the terminal in which you want to place your output using open, and placing its file descriptor instead of 0 as the first argument to open( ) function. This should mostly work.
0
 
Alf666Commented:
Urgl !!! Oh no :-))) NOT system().

0
 
timbauerCommented:
There is an easier way to do this. Open the remote terminal with open and write to it, via write.
Here is a simple program to illustrate the idea.

nt main( int argc, char **argv )
{
    int rtty; /*target terminal file descriptor */

    if( argc<2 ){
      char *err = "usage: sendto <terminal> \"message\"\n";
      write(STDERR_FILENO, err, strlen(err) );
      return 1;
    }

    /* Try and open the terminal. About 10 million
     *things can go wrong here so it is good to check the error too.
     *You need correct permissions to do this. */
    if( (rtty = open( argv[1], O_RDWR ) ) < 0 ){
      perror(argv[1]); /* if it fails, print out why */
      return 1;
    }

    /* Send the string to them */
    write( rtty, argv[2], strlen(argv[2]) );

    /* close the descritpor when done */
    close(rtty);

    return 0;
}

Also regarding write(1,...)
You also might consider using STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO instead
of 0, 1, and 2 since they make things easier to read.

Happy coding,
- Tim
0
 
timbauerCommented:
Sorry forgot to show the includes.

#include <stdio.h> /* perror */
#include <unistd.h> /* write*/
#include <fcntl.h>
#include <sys/types.h>

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now