Solved

i/o   for terminals

Posted on 2004-04-09
7
200 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

23 Experts available now in Live!

Get 1:1 Help Now