Solved

i/o   for terminals

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux 10 112
Issues with Ports in Linux (Hyper-V Guest) 2 110
Linux : taking backup different mount points under the same directory 8 132
python - find anything after $ question. 9 84
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 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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

734 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