Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have to make a simple app for school.

I have to write arguments one per line on the terminal, and return on stdout the user choices.

For example, I write cat my_app main.c main.h, if the user choose main.c, then I return main.c to cat. The problem is that if I write the menu on the standard output (aka STDOUT_FILENO), I return it to cat. I could write my menu on STDERR, but it's a little ugly.

I don't really know how to use devices/files like /dev/tty* or /dev/pty*, but I feel I could use them to write something on the terminal without having to write it on STDOUT or STDERR.

Basically, I want to open a new output, which would be shown by the terminal (STDOUT, STDERR, MYOUT).

I don't know if I explained it correctly.

share|improve this question
    
Why do you want to use cat? Do you want to output the contents of the file selected? –  Kevin Nov 25 '11 at 2:41
    
The rule is to output the user selection. For example, cat would show content of the user selection –  Max13 Nov 25 '11 at 7:08

1 Answer 1

up vote 4 down vote accepted

To build an interactive application you can open /dev/tty, it will return a file descriptor to the controlling terminal:

int ttyfd = open("/dev/tty", O_RDWR);

You can use it instead of STDIN_FILENO or STDOUT_FILENO (those could be redirected to something different than the terminal when the program is started).


Here is some example:

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main() {
  int ttyfd = open("/dev/tty", O_RDWR);
  printf("fd: %d\n", ttyfd);
  write(ttyfd, "hello tty!\n", 11);
  return 0;
}

When invoked with ./test >out, it should print the hello message on the terminal and something like fd: 3 in the out file.

share|improve this answer
    
Currently, open returned 0, so standard output. Does it mean, writing there (same fd as std* or not) will write on the console but NOT in the standard output? –  Max13 Nov 24 '11 at 23:54
    
@Max13: I added some example, the open call should return the first non allocated fd number (usually 3 unless you make some strange redirections). Does it work for you? –  Stéphane Gimenez Nov 25 '11 at 0:27
    
Also note that I used open because you mentioned STDOUT_FILENO, but if you want a FILE* (like stdout) then you should use fopen. –  Stéphane Gimenez Nov 25 '11 at 0:29
    
@Max13 In an application that has a terminal UI, you would use /dev/tty for both input and input, and not care what file descriptor it's opened on. –  Gilles Nov 25 '11 at 2:04
1  
@StéphaneGimenez: Thank you, it solves my issue. –  Max13 Nov 25 '11 at 11:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.