trying to rea from /dev/tty fails!

This is a discussion on trying to rea from /dev/tty fails! within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I am trying to run the following piece of code. It compiles fine [root@localhost test]# gcc -o prog prog.c ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    trying to rea from /dev/tty fails!

    Hi,

    I am trying to run the following piece of code. It compiles fine
    [root@localhost test]# gcc -o prog prog.c
    [root@localhost test]#

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    int main()
    {
    	int n;
    	int nread;
    	char s[15];
    	
    	n = open("/dev/tty", O_RDWR);
    	nread = read(n, s, 10);
    	write(n, s, nread);
    }
    But when I run it:
    [root@localhost test]# echo abcd|./prog
    Hello
    // halts here. comes to prompt after pressing Ctrl+c


    When I replace
    Code:
    nread = read(n, s, 10);
    write(n, s, nread);
    with
    Code:
    nread = read(0, s, 10);
    write(0, s, nread);
    it works fine.

    What is the scenario if I try to read from /dev/tty?

    Thanks and regards,

    Arun

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Does "open" return a sensible value? (greater than zero?)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    open returns 3.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ah, and the other thing is that the stdin is still tied to your /dev/tty - so you will probably need to do "fclose(stdin)" first.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Hi!

    It's working now.

    Thanks a lot for pointing it out.

    Arun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 06-24-2009, 09:49 AM
  2. bind() fails with WSAEACCES
    By pheres in forum Tech Board
    Replies: 2
    Last Post: 02-24-2009, 12:58 PM
  3. Mac - File locking with fcntl() fails on shared volumes!?
    By idelovski in forum Linux Programming
    Replies: 3
    Last Post: 11-10-2008, 06:37 PM
  4. Replies: 0
    Last Post: 05-23-2005, 11:39 AM
  5. Beginner Question: CreateWindowEx Fails.
    By dicky in forum Windows Programming
    Replies: 4
    Last Post: 05-30-2004, 08:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21