Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

i/o   for terminals

Posted on 2004-04-09
7
Medium Priority
?
224 Views
Last Modified: 2010-04-22
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
Comment
Question by:karana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 9

Expert Comment

by:Alf666
ID: 10793314
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
 

Accepted Solution

by:
ray_pro2 earned 100 total points
ID: 10794298
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
 
LVL 9

Expert Comment

by:Alf666
ID: 10796375
Urgl !!! Oh no :-))) NOT system().

0
 
LVL 2

Assisted Solution

by:timbauer
timbauer earned 100 total points
ID: 10808808
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
 
LVL 2

Expert Comment

by:timbauer
ID: 10808818
Sorry forgot to show the includes.

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

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question