controlling terminal question

This is a discussion on controlling terminal question within the Linux Programming forums, part of the Platform Specific Boards category; Given the following... Code: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { int ...

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    controlling terminal question

    Given the following...

    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(void)
    {
      int fd;
      char *name;
    
      if ((fd = open("/dev/tty", O_RDONLY)) < 0) {
        fprintf(stderr, "open error\n");
        exit(1);
      }
    
      name = ttyname(fd);
      printf("real ttyname is: %s\n", name);
    
      close(fd);
      exit(0);
    }
    When I run it on my local linux box, I see the following...

    $ ./me
    real ttyname is: /dev/tty
    even though real actual tty is..

    $ tty
    /dev/pts/3
    When I run this same code on a remote machine that uses FreeBSD, I see the following..,

    % ./me
    real ttyname is: /dev/ttyp5
    In this cae it matches what tty produces...

    % tty
    /dev/ttyp5
    Why wouldn't the Linux box produce the actual ttyname in this case? Why does it just show /dev/tty and not the actual terminal name?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Posix says the tty utility returns ttyname() of standard input for the process. You're doing something else.

    gg

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by Codeplug View Post
    Posix says the tty utility returns ttyname() of standard input for the process. You're doing something else.

    gg
    What else would I be doing?
    Last edited by Overworked_PhD; 10-01-2008 at 09:09 PM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    If you want to mimic Posix tty, then you should be calling ttyname(0).

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interactive vs non-interactive terminal session question
    By Overworked_PhD in forum Tech Board
    Replies: 2
    Last Post: 06-18-2009, 07:30 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. Controlling terminal tty
    By pdstatha in forum C Programming
    Replies: 0
    Last Post: 04-02-2002, 02:57 AM

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