Solved

i/o   for terminals

Posted on 2004-04-09
7
202 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
  • 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 25 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 25 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
A short film showing how OnPage and Connectwise integration works.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now